[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Fortran in Octave
From: |
geordie . mcbain |
Subject: |
Re: Fortran in Octave |
Date: |
Mon, 22 Mar 2004 14:43:08 +1100 |
User-agent: |
KMail/1.5.4 |
On Sat, 20 Mar 2004 08:05 am, A S Hodel wrote:
> I'd suggest that you look in the liboctave directory where Octave calls
> the LAPACK fortran routines.
> That will take care of the fortran-specific issues in calling fortran
> from C++. The rest would be standard .oct file implementation.
>
> If I remember right, you should look for the F77_FUNCTION macro
> definition.
That's right. I'll add that there are more helpful examples in src/
DLD-FUNCTIONS. Particularly balance.cc and qz.cc.
For example, say the Fortran file tnine.f is
---tnine.f
SUBROUTINE TNINE (IOPT, PARMOD, PS, X, Y, Z, BX, BY, BZ)
INTEGER IOPT
DOUBLE PRECISION PARMOD(10), PS, X, Y, Z, BX, BY, BZ
C This is just a test subroutine body, to check connexions.
C Put the sum of PARMOD in PS, and X, Y, Z into BX, BY, BZ
INTEGER I
PS = 0D0
DO 1 I=1, 10
PS = PS + PARMOD (I)
1 CONTINUE
BX = X
BY = Y
BZ = Z
END
---end tnine.f
A minimal C++ wrapper would look like:
---t96.cc
#include <octave/oct.h>
#include "f77-fcn.h"
extern "C"
{
int F77_FUNC (tnine, TNINE) (const int& IOPT, const double* PARMOD,
double& PS,
const double& X, const double& Y,
const double &Z,
double& BX, double& BY, double& BZ );
}
DEFUN_DLD (t96, args, ,
"- Loadable Function: [PS, BX, BY, BZ] = t96 (PM, X, Y, Z)\n\
\n\
Returns the sum of PM in PS and X, Y, and Z in BX, BY, and BZ.")
{
octave_value_list retval;
const int dummy_integer = 0;
Matrix pm;
const double x = args(1).double_value(), y = args(2).double_value(),
z = args(3).double_value();
double ps, bx, by, bz;
pm = args(0).matrix_value ();
F77_XFCN (tnine, TNINE,
(dummy_integer, pm.fortran_vec(), ps, x, y, z, bx, by, bz) );
if (f77_exception_encountered)
{
error ("unrecoverable error in t96");
return retval;
}
retval(0) = octave_value (ps);
retval(1) = octave_value (bx);
retval(2) = octave_value (by);
retval(3) = octave_value (bz);
return retval;
}
--end t96.cc
Compile this (in the Bourne Again Shell) with
$ mkoctfile t96.cc tnine.f
and run it in Octave like:
octave> [p, x, y, z] = t96 (1:10, sqrt (2), pi, e)
p = 55
x = 1.4142
y = 3.1416
z = 2.7183
Hope this helps,
Geordie McBain
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------