From: Patrick Scheibe on
Hi,


> Just to be sure, the above chunk of code goes where
>
> > double multiply( double a, double b)
> > {
> > return fortfunc_(&a, &b);
> > }
>
> is, isn't it? If not, where in you earlier example?

Right. Just check the tutorials to MathLink inside the
DocumentationCenter of Mathematica. Pretty exhaustive.

And can please fix someone the documentation bug in the "MathLink
Developer Guide - Mac OS X"?

:Begin:
:Function: addtwo
:Pattern: AddTwo[i_Integer, j_Integer]
:Arguments: { i, j }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:End:

:Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine
integers x and y."

int addtwo( int i, int j)
{
return i+j;
}

int main(int argc; char* argv[])
{
return MLMain(argc, argv);
}

Really cool if you just copy and paste it.
Who sees it?

Cheers
Patrick


> >You need to set the :ReturnType: of the template to Manual and it
> >*should*
> >work. But hey, you are the fortran-guy. Those were my first 5 lines of
> >fortran,
> >so please take this as a hint and not as a working sample.
>
> Long time without coding in fortran. I became lazy due to the extensive use
> of another software.
>
> Cheers
>
> Ed
>
>
> Am Jul 26, 2010 um 6:44 PM schrieb Eduardo M. A. M.Mendes:
>
> > Hi Patrick
> >
> > Many many thanks. I shall try it on both linux and Windows.
> >
> > Do you think that I can swap g95 for gfortran for both Win and Linux?
> >
> > Since my routines have multiple output parameters, I wonder whether
> > if the
> > ReturnType could be something like {Real, Real} (that is, instead of
> > functions I have subroutines with multiple input and output
> > arguments}.
> >
> > Cheers
> >
> > Ed
> >
> >
> > -----Original Message-----
> > From: Patrick Scheibe [mailto:pscheibe(a)trm.uni-leipzig.de]
> > Sent: Monday, July 26, 2010 10:37 AM
> > To: Eduardo M. A. M.Mendes
> > Cc: mathgroup(a)smc.vnet.net
> > Subject: Re: Linking my fortran subroutines with
> > Mathematica
> >
> > Hi,
> >
> > I never wrote fortran but it shouldn't be too hard.
> > First the fortran-file "function.f90" with the content
> >
> >>>>>>
> > real*8 function fortfunc(a,b)
> > real*8 a,b
> > fortfunc=a*b
> > end function fortfunc
> >>>>>>
> >
> > Second the interface.tm for the MathLink
> >
> >>>>>>
> > #include "mathlink.h"
> >
> > extern "C" double fortfunc_(double *a, double *b);
> >
> > :Begin:
> > :Function: multiply
> > :Pattern: multiply[a_?NumericQ, b_?NumericQ]
> > :Arguments: { a, b }
> > :ArgumentTypes: { Real, Real }
> > :ReturnType: Real
> > :End:
> >
> > double multiply( double a, double b)
> > {
> > return fortfunc_(&a, &b);
> > }
> >
> > int main(int argc, char* argv[])
> > {
> > return MLMain(argc, argv);
> > }
> >>>>>>
> >
> > Compile the fortran file with
> >
> > g95 -Wall -c function.f90
> >
> > process the .tm file with the mprep program to create the .c file
> >
> > mprep -o interface.c interface.tm
> >
> > compile the file and link it with the objectfile from your fortran
> > code
> >
> > g++ -I$mpath interface.c function.o -L$mpath -lMLi3
> >
> > Here $mpath contains the path to the MathLink compiler stuff. On my
> > OSX this is
> >
> > mpath=/Applications/Development/Mathematica7.app/SystemFiles/Links/
> > MathLink/DeveloperKit/CompilerAdditions/
> >
> > on a linux box the Mathematica root is usually under /usr/local/
> > Wolfram/Mathematica/
> >
> > Then you can start the executable
> >
> > ./a.out -linkname blub -linkmode listen
> >
> > and use it inside a Mathematica session with e.g.
> >
> > lnk = Install[LinkConnect["blub"]]
> > LinkPatterns[lnk]
> > multiply[10.0, 11.0]
> > Uninstall[lnk]
> >
> > That's it.
> >
> > Cheers
> > Patrick
> >
> >
> > Am Jul 26, 2010 um 1:06 PM schrieb Eduardo M. A. M.Mendes:
> >
> >> Hi
> >>
> >> Many thanks.
> >>
> >> Won't you have an example?
> >>
> >> Cheers
> >>
> >> Ed
> >>
> >>
> >> -----Original Message-----
> >> From: Patrick Scheibe [mailto:pscheibe(a)trm.uni-leipzig.de]
> >> Sent: Sunday, July 25, 2010 10:35 PM
> >> To: emamm
> >> Cc: mathgroup(a)smc.vnet.net
> >> Subject: Re: Linking my fortran subroutines with
> >> Mathematica
> >>
> >> Hi,
> >>
> >> just make a wrapper for your Fortran functions so you can call them
> >> from
> >> C. Once this is done you can simply call them through the MathLink.
> >> You could start by compiling your Fortran functions into a .so
> >> or .dll
> >> or however the shared libraries are called on your OS. Then create a
> >> MathLink-template and call those functions from there..
> >>
> >> I really don't know how to explain this better since it is straight
> >> forward and you should have a running example in a few minutes.
> >>
> >> Cheers
> >> Patrick
> >>
> >> On Sun, 2010-07-25 at 01:58 -0400, emamm wrote:
> >>> Hello
> >>>
> >>> I am new to Mathematica. Although I have searched the web for
> >>> answers
> >>> on how to link fortran to Mathematica without buying MathCode F90, I
> >>> have not found good examples on how to link Fortran to Mathematica.
> >>>
> >>> All my fortran functions were modified to accept a C-wrapper. I did
> >>> that to link them to another system. I have no problem to create
> >>> mex files
> >> on
> >>> either Windows or Linux using gcc,gfortran or g95 (all free
> >>> compilers!). My fortran functions work exactly as m-file on the
> >>> other
> >> system
> >>> with lots of input and output arguments - The input arguments
> >>> determine the size of the output arguments via malloc on C.
> >>>
> >>> I believe that there is something similar to a mex file for
> >>> Mathematica but the documentation on Wolfram's site does not give me
> >>> much information.
> >>>
> >>> Would someone out there have a nice, neat example (not too simple,
> >>> please - addrow from Wolfram's site is way too simple!) on how to
> >>> link
> >>> fortran to Mathematica?
> >>>
> >>> Many thanks
> >>>
> >>> Ed
> >>>
> >>>
> >>> PS. MathCode F90 seems to do what I want but for a a price (and
> >>> what a
> >>> price!).
> >>>
> >>
> >>
> >
> >
>
>


From: Patrick Scheibe on
Hi,

I never wrote fortran but it shouldn't be too hard.
First the fortran-file "function.f90" with the content

>>>>>
real*8 function fortfunc(a,b)
real*8 a,b
fortfunc=a*b
end function fortfunc
>>>>>

Second the interface.tm for the MathLink

>>>>>
#include "mathlink.h"

extern "C" double fortfunc_(double *a, double *b);

:Begin:
:Function: multiply
:Pattern: multiply[a_?NumericQ, b_?NumericQ]
:Arguments: { a, b }
:ArgumentTypes: { Real, Real }
:ReturnType: Real
:End:

double multiply( double a, double b)
{
return fortfunc_(&a, &b);
}

int main(int argc, char* argv[])
{
return MLMain(argc, argv);
}
>>>>>

Compile the fortran file with

g95 -Wall -c function.f90

process the .tm file with the mprep program to create the .c file

mprep -o interface.c interface.tm

compile the file and link it with the objectfile from your fortran code

g++ -I$mpath interface.c function.o -L$mpath -lMLi3

Here $mpath contains the path to the MathLink compiler stuff. On my
OSX this is

mpath=/Applications/Development/Mathematica7.app/SystemFiles/Links/
MathLink/DeveloperKit/CompilerAdditions/

on a linux box the Mathematica root is usually under /usr/local/
Wolfram/Mathematica/

Then you can start the executable

../a.out -linkname blub -linkmode listen

and use it inside a Mathematica session with e.g.

lnk = Install[LinkConnect["blub"]]
LinkPatterns[lnk]
multiply[10.0, 11.0]
Uninstall[lnk]

That's it.

Cheers
Patrick


Am Jul 26, 2010 um 1:06 PM schrieb Eduardo M. A. M.Mendes:

> Hi
>
> Many thanks.
>
> Won't you have an example?
>
> Cheers
>
> Ed
>
>
> -----Original Message-----
> From: Patrick Scheibe [mailto:pscheibe(a)trm.uni-leipzig.de]
> Sent: Sunday, July 25, 2010 10:35 PM
> To: emamm
> Cc: mathgroup(a)smc.vnet.net
> Subject: Re: Linking my fortran subroutines with
> Mathematica
>
> Hi,
>
> just make a wrapper for your Fortran functions so you can call them
> from
> C. Once this is done you can simply call them through the MathLink.
> You could start by compiling your Fortran functions into a .so or .dll
> or however the shared libraries are called on your OS. Then create a
> MathLink-template and call those functions from there..
>
> I really don't know how to explain this better since it is straight
> forward and you should have a running example in a few minutes.
>
> Cheers
> Patrick
>
> On Sun, 2010-07-25 at 01:58 -0400, emamm wrote:
>> Hello
>>
>> I am new to Mathematica. Although I have searched the web for answers
>> on how to link fortran to Mathematica without buying MathCode F90, I
>> have not found good examples on how to link Fortran to Mathematica.
>>
>> All my fortran functions were modified to accept a C-wrapper. I did
>> that to link them to another system. I have no problem to create
>> mex files
> on
>> either Windows or Linux using gcc,gfortran or g95 (all free
>> compilers!). My fortran functions work exactly as m-file on the other
> system
>> with lots of input and output arguments - The input arguments
>> determine the size of the output arguments via malloc on C.
>>
>> I believe that there is something similar to a mex file for
>> Mathematica but the documentation on Wolfram's site does not give me
>> much information.
>>
>> Would someone out there have a nice, neat example (not too simple,
>> please - addrow from Wolfram's site is way too simple!) on how to
>> link
>> fortran to Mathematica?
>>
>> Many thanks
>>
>> Ed
>>
>>
>> PS. MathCode F90 seems to do what I want but for a a price (and
>> what a
>> price!).
>>
>
>


From: Frank K on
On Jul 26, 6:42 am, Patrick Scheibe <psche...(a)trm.uni-leipzig.de>
wrote:
> Hi,
>
> just make a wrapper for your Fortran functions so you can call them from
> C. Once this is done you can simply call them through the MathLink.
> You could start by compiling your Fortran functions into a .so or .dll
> or however the shared libraries are called on your OS. Then create a
> MathLink-template and call those functions from there..
>
> I really don't know how to explain this better since it is straight
> forward and you should have a running example in a few minutes.
>
> Cheers
> Patrick
>
>
>
> On Sun, 2010-07-25 at 01:58 -0400, emamm wrote:
> > Hello
>
> > I am new to Mathematica. Although I have searched the web for answers
> > on how to link fortran to Mathematica without buying MathCode F90, I
> > have not found good examples on how to link Fortran to Mathematica.
>
> > All my fortran functions were modified to accept a C-wrapper. I did
> > that to link them to another system. I have no problem to create mex files on
> > either Windows or Linux using gcc,gfortran or g95 (all free
> > compilers!). My fortran functions work exactly as m-file on the other system
> > with lots of input and output arguments - The input arguments
> > determine the size of the output arguments via malloc on C.
>
> > I believe that there is something similar to a mex file for
> > Mathematica but the documentation on Wolfram's site does not give me
> > much information.
>
> > Would someone out there have a nice, neat example (not too simple,
> > please - addrow from Wolfram's site is way too simple!) on how to link
> > fortran to Mathematica?
>
> > Many thanks
>
> > Ed
>
> > PS. MathCode F90 seems to do what I want but for a a price (and what a
> > price!).- Hide quoted text -
>
> - Show quoted text -

If you're using Windows, most windows Fortran compilers can generate a
dll (dynamic link library) which can be called through .NETLink's
DefineDllFunction. Remember that Fortran variables are passed by
reference.


From: Eduardo M. A. M.Mendes on
Hi Patrick

Many many thanks. I shall try it on both linux and Windows.

Do you think that I can swap g95 for gfortran for both Win and Linux?

Since my routines have multiple output parameters, I wonder whether if the
ReturnType could be something like {Real, Real} (that is, instead of
functions I have subroutines with multiple input and output arguments}.

Cheers

Ed


-----Original Message-----
From: Patrick Scheibe [mailto:pscheibe(a)trm.uni-leipzig.de]
Sent: Monday, July 26, 2010 10:37 AM
To: Eduardo M. A. M.Mendes
Cc: mathgroup(a)smc.vnet.net
Subject: Re: Linking my fortran subroutines with Mathematica

Hi,

I never wrote fortran but it shouldn't be too hard.
First the fortran-file "function.f90" with the content

>>>>>
real*8 function fortfunc(a,b)
real*8 a,b
fortfunc=a*b
end function fortfunc
>>>>>

Second the interface.tm for the MathLink

>>>>>
#include "mathlink.h"

extern "C" double fortfunc_(double *a, double *b);

:Begin:
:Function: multiply
:Pattern: multiply[a_?NumericQ, b_?NumericQ]
:Arguments: { a, b }
:ArgumentTypes: { Real, Real }
:ReturnType: Real
:End:

double multiply( double a, double b)
{
return fortfunc_(&a, &b);
}

int main(int argc, char* argv[])
{
return MLMain(argc, argv);
}
>>>>>

Compile the fortran file with

g95 -Wall -c function.f90

process the .tm file with the mprep program to create the .c file

mprep -o interface.c interface.tm

compile the file and link it with the objectfile from your fortran code

g++ -I$mpath interface.c function.o -L$mpath -lMLi3

Here $mpath contains the path to the MathLink compiler stuff. On my
OSX this is

mpath=/Applications/Development/Mathematica7.app/SystemFiles/Links/
MathLink/DeveloperKit/CompilerAdditions/

on a linux box the Mathematica root is usually under /usr/local/
Wolfram/Mathematica/

Then you can start the executable

../a.out -linkname blub -linkmode listen

and use it inside a Mathematica session with e.g.

lnk = Install[LinkConnect["blub"]]
LinkPatterns[lnk]
multiply[10.0, 11.0]
Uninstall[lnk]

That's it.

Cheers
Patrick


Am Jul 26, 2010 um 1:06 PM schrieb Eduardo M. A. M.Mendes:

> Hi
>
> Many thanks.
>
> Won't you have an example?
>
> Cheers
>
> Ed
>
>
> -----Original Message-----
> From: Patrick Scheibe [mailto:pscheibe(a)trm.uni-leipzig.de]
> Sent: Sunday, July 25, 2010 10:35 PM
> To: emamm
> Cc: mathgroup(a)smc.vnet.net
> Subject: Re: Linking my fortran subroutines with
> Mathematica
>
> Hi,
>
> just make a wrapper for your Fortran functions so you can call them
> from
> C. Once this is done you can simply call them through the MathLink.
> You could start by compiling your Fortran functions into a .so or .dll
> or however the shared libraries are called on your OS. Then create a
> MathLink-template and call those functions from there..
>
> I really don't know how to explain this better since it is straight
> forward and you should have a running example in a few minutes.
>
> Cheers
> Patrick
>
> On Sun, 2010-07-25 at 01:58 -0400, emamm wrote:
>> Hello
>>
>> I am new to Mathematica. Although I have searched the web for answers
>> on how to link fortran to Mathematica without buying MathCode F90, I
>> have not found good examples on how to link Fortran to Mathematica.
>>
>> All my fortran functions were modified to accept a C-wrapper. I did
>> that to link them to another system. I have no problem to create
>> mex files
> on
>> either Windows or Linux using gcc,gfortran or g95 (all free
>> compilers!). My fortran functions work exactly as m-file on the other
> system
>> with lots of input and output arguments - The input arguments
>> determine the size of the output arguments via malloc on C.
>>
>> I believe that there is something similar to a mex file for
>> Mathematica but the documentation on Wolfram's site does not give me
>> much information.
>>
>> Would someone out there have a nice, neat example (not too simple,
>> please - addrow from Wolfram's site is way too simple!) on how to
>> link
>> fortran to Mathematica?
>>
>> Many thanks
>>
>> Ed
>>
>>
>> PS. MathCode F90 seems to do what I want but for a a price (and
>> what a
>> price!).
>>
>
>