help-octave
[Top][All Lists]
Advanced

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

Calling a octave function from a fortran function that accepts a functio


From: John Weatherwax
Subject: Calling a octave function from a fortran function that accepts a function as an argument
Date: Mon, 23 May 2005 11:40:57 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803

Hello,
I am having trouble with "Calling a octave function from a fortran function that accepts a function as an argument"

   Details:
Many FORTRAN routines (think quadrature or ODE's) require a function to be given at the same time as the computational routine . Does anyone have a SIMPLE example of how to create an *.oct file that will allow an octave function to be called from the FORTRAN computational routine?
For instance in octave (testfun.m):

function [ res ] = testfun( x )
 res = sin( x ) ;
endfunction

In FORTRAN (fortsub2.f):

     subroutine fortsub2(f,xin,nxin,xout)
C     Call f(.) on every element of xin:
C     return in xout:
     external f
     real*8 xin(*),xout(*)
     integer*4 nxin
     do i=1,nin
        xout(i)=f(xin(i))
     enddo
     return
     end

Now in C++ I have been unsuccessfull in creating the *.cc file to make an *.oct file with mkoctfile. I want to call the routine with something like:

> xin = rand( 4,1 );
> [ xout ] = exOct2FortFnCall( "testfun", xin );

The result in xout should be xout = sin( xin );

My feable attempt at this procedure is included below. Any help pointers would be greatly appreciated. I know there are source code examples for things like LSODE but I was hoping for a smaller example that I could play with.

TIA,

John

The C++ example call (exOct2FortFnCall.cc):

#include <octave/oct.h>
#include <octave/parse.h>
#include <octave/ov-fcn.h>

// Function pointer to octave/FORTRAN function:
octave_function *octFnPtr;

typedef (double) (*fort_fcn_ptr) (double*)

extern "C" int F77_FUNC(fortsub2,FORTSUB2)\
                      (fort_fcn_ptr,double*,int&,double*);

DEFUN_DLD(exOct2FortFnCall,args, , \
     "An example passing an octave function INTO a FORTRAN subroutine, \
          computing with it and returning the result to octave."){

 octave_value_list retval;

 // Extract input arguments into octave api/C++:
 //
 // Octave function name:
 std::string myfunc = args(0).string_value();
 // Octave xin:
 ColumnVector xin( args(1).vector_value() );
 double* xinptr = xin.fortran_vec();
 int nxin = args(1).length();

 // Extract the octave function into a pointer:
 octFnPtr = extract_function( myfunc, "Err: exOct2FortFnCall", "", "" );

 ColumnVector xout;
 double *xoutptr = xout.fortran_vec();

 F77_FUNC(fortsub2,FORTSUB2)(octFnPtr,xinptr,nxin,xoutptr);

 retval.append( xout );
}

--

John L. Weatherwax

Lincoln Laboratory, Massachusetts Institute of Technology
244 Wood Street
Lexington, MA 02420-9108 USA

Phone : 781-981-5370
Fax   : 781-981-4739
Email : address@hidden

%--



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

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