help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Passing a "ColumnVector" to a Fortran subroutine


From: John W. Eaton
Subject: Passing a "ColumnVector" to a Fortran subroutine
Date: Wed, 3 Nov 1999 23:27:16 -0600 (CST)

On  4-Nov-1999, Eduardo Gallestey <address@hidden> wrote:

| I have a fortran program, which I want to call from the "octave"
| environment. Say
| 
| A=rand(3,1);
| B=fort_routine(A);
| 
| As far as I understand, the solution is to write a C++ function, which
| would then call the Fortran subroutine. I have been able to do that and
| it works correctly. 
| 
| However, the data transfer to and from FORTRAN is very  inefficient. The
| reason is that FORTRAN doesn't understand the structures "Matrix",
| "ColumnVector", etc., and I can not pass "ColumnVector" as an argument
| to the FORTRAN part. 
| 
| Now I am doing loops to copy the data "ColumnVector" into a a C++
| "array", acceptable for FORTRAN:

There are plenty of examples of doing this kind of thing in the Octave
sources.  They generally go something like this:

  DEFUN_DLD (Test, args , , "")
  {
    /* Get data */
    ColumnVector x = args(1) . vector_value ();
    double *px = x.fortran_vec ();

    int N = data.length();

    ColumnVector y (N);
    double *py = y.fortran_vec ();

    /* useful action */

    fortr_routine (px, py);

    return octave_value (y);
  }

If you know that the Fortran routine won't modify an argument, you can
use

  ColumnVector x = args(1) . vector_value ();
  const double *px = x.data ();

which is more efficient, because the data() method doesn't have to
ensure that the reference count is 1 for the vector.  The reference
count for the return value is 1 when you create it, so (unless you do
something that increments the count) no copy needs to be made.

jwe



---------------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.  To ensure
that development continues, see www.che.wisc.edu/octave/giftform.html
Instructions for unsubscribing: www.che.wisc.edu/octave/archive.html
---------------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]