<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7090518505459435252</id><updated>2024-08-28T16:17:11.163-07:00</updated><category term="C"/><category term="MathLink"/><category term="Mathematica"/><category term="Linux"/><category term="Lisp"/><category term="Fortran"/><title type='text'>Compotato</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://compotato.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://compotato.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Neutralino</name><uri>http://www.blogger.com/profile/04380605872780563173</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7090518505459435252.post-2687267998287485406</id><published>2011-09-19T15:16:00.000-07:00</published><updated>2011-09-19T15:16:21.105-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="Lisp"/><category scheme="http://www.blogger.com/atom/ns#" term="Mathematica"/><category scheme="http://www.blogger.com/atom/ns#" term="MathLink"/><title type='text'>Calling Lisp with Mathlink</title><content type='html'>My final example of using MathLink is a tutorial on calling Lisp code, from Mathematica.&amp;nbsp; The implementation I will show is probably quite crude to a computer scientist.&amp;nbsp; However to someone interested in producing a solution I think it is useful.&amp;nbsp; The method I put together is sufficiently general as to allow Mathematica to call any executable file.&amp;nbsp; This method is useful for calling code written in a language that is not easy to integrate in C.&lt;br /&gt;
&lt;br /&gt;
One language that is not very easy to call in C is Lisp.&amp;nbsp; First of all let me be clear, it is possible to use a foreign function interface in C to call Lisp.&amp;nbsp; However it is very difficult.&amp;nbsp; Limiting myself to the open source Lisp compilers I found no easy option.&amp;nbsp; I did find the following quote from the Steel Bank Common Lisp documentation,&lt;br /&gt;
&lt;br /&gt;
&quot;Calling Lisp functions from C is sometimes possible, but is extremely hackish and poorly supported&quot;&lt;br /&gt;
&lt;br /&gt;
Thus I was searching for an easy way that gave me results.&amp;nbsp; I also felt that if one were to work out how to write some particular program in Lisp, that C could call, then the problem would not be completely solved.&amp;nbsp; More complicated data structures, or any modest change to the Lisp program, could require more hacking to make the C calls work okay.&amp;nbsp; My method, whilst less elegant and a little slower, does not suffer from these problems.&lt;br /&gt;
&lt;br /&gt;
First of all I will run through an example program, written in Lisp, that can be called by C as a system call.&amp;nbsp; Then, using MathLink, I will show how to make Mathematica call the C code and return the output from the Lisp program to the Mathematica program.&amp;nbsp; I wish to emphasise that this method is not in any way particular to Lisp, it will work for any executable file.&amp;nbsp; After I have given an example I will briefly discuss the efficiency of the program.&amp;nbsp; I will attempt to argue the solution I propose is quick enough for many applications.&lt;br /&gt;
&lt;br /&gt;
Below is a some Lisp code for a factorial function,&lt;br /&gt;
&lt;br /&gt;
(defun factorial (x)&lt;br /&gt;
&amp;nbsp; (if (eql x 1) 1 (* x (factorial (1- x)))))&lt;br /&gt;
&lt;br /&gt;
(defun main (argv)&lt;br /&gt;
&amp;nbsp; (with-open-file (s &quot;$Path/output.dat&quot; :direction :output :if-exists :supersede)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; (format s &quot;~A&quot; (factorial (read-from-string (first (rest argv)))))))&lt;br /&gt;
&lt;br /&gt;
This code performs two basic tasks.&amp;nbsp; Firstly it takes the argument supplied at runtime, called &lt;i&gt;argv&lt;/i&gt;, and, secondly, writes the factorial of &lt;i&gt;argv&lt;/i&gt; to the file located at $Path/output.dat.&amp;nbsp; I will not explain the complete details of the Lisp code.&amp;nbsp; What is important is that the code computes functions of variables supplied at run time and writes the result(s) to a file.&amp;nbsp; If such an arrangement can be realised in any language then this tutorial shows how to run such code from Mathematica.&amp;nbsp; One important note is the location of the output file, here called output.dat.&amp;nbsp; When using Mathematica the default path will be changed.&amp;nbsp; Therefore it is a good idea to specify all paths in full in all files/code.&amp;nbsp; This will avoid any bugs occurring when trying to run code from Mathematica that causes files not to be found.&lt;br /&gt;
&lt;br /&gt;
Now I will take a short detour to explain how to obtain executable files from Lisp.&amp;nbsp; If you are not interested in Lisp then please skip on to the C code.&amp;nbsp; My solution to creating Lisp executables was to use the program BuildApp.&amp;nbsp; You can obtain BuildApp from their website, &lt;a href=&quot;http://www.xach.com/lisp/buildapp/&quot;&gt;here&lt;/a&gt;.&amp;nbsp; There are other options available to create executable files from Lisp.&amp;nbsp; After installing BuildApp you need to type in to a terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ BuildApp --entry main --output factorial --load factorial.lisp&lt;br /&gt;
&lt;br /&gt;
What this does is to compile the file factorial.lisp into an executable called factorial.&amp;nbsp; The flag --entry main is a technial point that tells the program how the executable will be ran, here it is as a top-level function.&amp;nbsp; The effect of doing this is that, using the Lisp code given, one can now type in to a terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ ./factorial 5&lt;br /&gt;
&lt;br /&gt;
and the output, of 120, will be written to a file.&amp;nbsp; This is the first goal for any language, Lisp or otherwise (that can not be embedded into C): producing an executable that can receive arguments from a terminal, as shown above.&amp;nbsp; How to do this depends on the compiler and language used.&lt;br /&gt;
&lt;br /&gt;
Next some C code is required that MathLink can talk to.&amp;nbsp; From previous blogs we know that what is required is to write a C function that takes the arguments and returns the relevant output.&amp;nbsp; I wrote the code,&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &quot;mathlink.h&quot;&lt;br /&gt;
&lt;br /&gt;
int f(int x) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; FILE *fp;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char output[100], fstr[100];&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; sprintf(fstr, &quot;$Path/factorial %d&quot;, x);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; system(fstr);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fp = fopen(&quot;$Path/output.dat&quot;, &quot;r&quot;);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fscanf(fp, &quot;%d&quot;, &amp;amp;i);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fclose(fp);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return i;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return MLMain(argc, argv);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
As before you should recognise the MathLink program code at the very bottom and the mathlink.h header file.&amp;nbsp; In the function f an integer x is converted in to a character.&amp;nbsp; This is concatenated to the path of the executable by &lt;i&gt;sprintf(fstr, &quot;$Path/factorial %d&quot;, x);&lt;/i&gt;.&amp;nbsp; In this case the executable is called factorial.&amp;nbsp; This is echoed to the terminal by the syntax &lt;i&gt;system(fstr);&lt;/i&gt; and &lt;i&gt;fstr&lt;/i&gt; is the string that the terminal will (try to) execute.&amp;nbsp; In short, the C code writes to the terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ $Path/factorial x&lt;br /&gt;
&lt;br /&gt;
where $Path is the complete path to the executable factorial and x is the variable whose factorial we are computing.&amp;nbsp; So now in the output file is the resulting factorial of x.&amp;nbsp; The C function, f, now reads the file output.dat and returns the number it finds.&amp;nbsp; I admit this is a crude implementation but its simplicity means that it does not matter how the executable was written.&amp;nbsp; It could be a terminal script, or anything.&amp;nbsp; This means that the solution I have written really is very powerful.&lt;br /&gt;
&lt;br /&gt;
There is now the formality of writing a template and compiling the template and C code together.&amp;nbsp; Here we can use the script mcc.&amp;nbsp; The template for the function f is,&lt;br /&gt;
&lt;br /&gt;
:Begin:&lt;br /&gt;
:Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;br /&gt;
:Pattern:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f[x_Integer]&lt;br /&gt;
:Arguments:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {x}&lt;br /&gt;
:ArgumentTypes: {Integer}&lt;br /&gt;
:ReturnType:&amp;nbsp;&amp;nbsp;&amp;nbsp; Integer&lt;br /&gt;
:End:&lt;br /&gt;
&lt;br /&gt;
Saving this file as f.tm it is just a task of using mcc.&amp;nbsp; In to a terminal type,&lt;br /&gt;
&lt;br /&gt;
terminal $ mcc f.tm f.c -o result&lt;br /&gt;
&lt;br /&gt;
Installing this to Mathematica gives the ability to call the Lisp program.&lt;br /&gt;
&lt;br /&gt;
I&#39;d like to close with some general comments on efficiency and the power of this solution.&amp;nbsp; Firstly the solution is crude and slow.&amp;nbsp; However the speed is lost by calling the executable then writing and reading to file.&amp;nbsp; These tasks are only performed once.&amp;nbsp; The cost will only be a small fraction of a second.&amp;nbsp; So in a program that takes a minute to run, for example, one would not mind wasting a quarter of a second to be able to code in any language whatsoever.&amp;nbsp; Such freedom might well allow one to use a program that is many hours faster than an equivalent Mathematica program.&amp;nbsp; Or perhaps the ability to code in some particular language, as compared to C, might offer a solution to the problem in days rather than weeks.&amp;nbsp; As a result I do not consider the efficiency loss as an issue for most situations.&amp;nbsp; I also consider the code I have given to be extremely flexible and powerful to a range of users.</content><link rel='replies' type='application/atom+xml' href='http://compotato.blogspot.com/feeds/2687267998287485406/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://compotato.blogspot.com/2011/09/calling-lisp-with-mathlink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/2687267998287485406'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/2687267998287485406'/><link rel='alternate' type='text/html' href='http://compotato.blogspot.com/2011/09/calling-lisp-with-mathlink.html' title='Calling Lisp with Mathlink'/><author><name>Neutralino</name><uri>http://www.blogger.com/profile/04380605872780563173</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7090518505459435252.post-1723666396686452552</id><published>2011-09-16T03:10:00.000-07:00</published><updated>2011-09-16T03:10:10.587-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="Fortran"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="Mathematica"/><category scheme="http://www.blogger.com/atom/ns#" term="MathLink"/><title type='text'>Calling FORTRAN Code with MathLink</title><content type='html'>This post will explain how to use MathLink to call FORTRAN code.&amp;nbsp; I forsee three reasons this may be interesting.&amp;nbsp; One, people still use FORTRAN code!&amp;nbsp; Two, it will demonstrate how to interface Mathematica, through MathLink, with any programming language that C can call.&amp;nbsp; Thirdly, the blog will explain more about how MathLink works and will allow users to accomplish more sophisticated implementations.&amp;nbsp; I will rely on ideas developed in previous blog entries.&lt;br /&gt;
&lt;br /&gt;
Here I will outline calling a simple program, adding two numbers together.&amp;nbsp; A FORTRAN (95) subroutine that achieves this is,&lt;br /&gt;
&lt;br /&gt;
subroutine addtwof(i,j,k)&lt;br /&gt;
implicit none&lt;br /&gt;
integer::i,j,k&lt;br /&gt;
k=i+j&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
This subroutine is saved in a text file, which I have called addtwof.f95.&amp;nbsp; It is possible to write C code that calls this subroutine.&amp;nbsp; Notice the FORTRAN code should not be a complete program, but needs to be a subroutine.&amp;nbsp; The C code to call a FORTRAN subroutine is compiler dependent.&amp;nbsp; However there are two common syntax.&amp;nbsp; Using the GCC compiler the syntax is,&lt;br /&gt;
&lt;br /&gt;
addtwof_(&amp;amp;i, &amp;amp;j, &amp;amp;result);&lt;br /&gt;
&lt;br /&gt;
Omitting the underscore, so just addtwof(&amp;amp;i, &amp;amp;j, &amp;amp;result);, is the other common syntax.&amp;nbsp; Information on what to use should be available in your C compilers documentation.&amp;nbsp; One technical difference between the C and FORTRAN languages is the use of pointers.&amp;nbsp; Basically C and FORTRAN have different data structures, a variable x, in C is a pointer.&amp;nbsp; It points to a location in the memory whereas in FORTRAN a variable is directly the location of that value in the memory.&amp;nbsp; As a result the ampersand (&amp;amp;) is used to tell C send FORTRAN the number itself, not just a memory location!&amp;nbsp; As a result you will see &amp;amp;i not just i etc.&amp;nbsp; There is a second important difference.&amp;nbsp; FORTRAN is not a case-sensitive language whilst C is.&amp;nbsp; Be very careful about using mixed case names, better yet, just don&#39;t do it and only use lower case letters for example.&amp;nbsp; The important lesson here is that calling a language from another requires to think carefully about the differences in the languages and carefully tread around them.&lt;br /&gt;
&lt;br /&gt;
Now it is possible to write a simple C program that is usable by MathLink.&amp;nbsp; The code I will use is,&lt;br /&gt;
&lt;br /&gt;
#include &quot;mathlink.h&quot;&lt;br /&gt;
&lt;br /&gt;
int f(int i, int j) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int result;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; addtwof_(&amp;amp;i, &amp;amp;j, &amp;amp;result);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return result;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return MLMain(argc, argv);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
From my previous blog you should recognise the mathlink header file and the MLMain call at the bottom.&amp;nbsp; In the function f one can see the FORTRAN call, complete with ampersands.&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
In general, if a language can be called by C, and you can make it all happen, then you will just need an analogous program to the one here and MathLink will work.&amp;nbsp; Whilst the example here is dealing with FORTRAN there are many other languages that C call call directly.&lt;br /&gt;
&lt;br /&gt;
There is one more file that is needed, a template file.&amp;nbsp; As the program adds two integers the template is the same as the C only implementation.&amp;nbsp; For completeness here it is again,&lt;br /&gt;
&lt;br /&gt;
:Begin:&lt;br /&gt;
:Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;br /&gt;
:Pattern:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f[x_Integer, y_Integer]&lt;br /&gt;
:Arguments:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {x, y}&lt;br /&gt;
:ArgumentTypes: {Integer, Integer}&lt;br /&gt;
:ReturnType:&amp;nbsp;&amp;nbsp;&amp;nbsp; Integer&lt;br /&gt;
:End:&lt;br /&gt;
&lt;br /&gt;
I have called this file f.tm.&amp;nbsp; (Again, the .tm extension is important.)&lt;br /&gt;
&lt;br /&gt;
At this point the three files, addtwo.f95, f. tm and the C program, f.c, must be compiled and linked.&amp;nbsp; Previously the program mcc was used.&amp;nbsp; It is not possible to use mcc here because we need to link the FORTRAN code to the C.&lt;br /&gt;
&lt;br /&gt;
Linking FORTRAN with C, or any language with C for that matter, is done through object files.&amp;nbsp; All the files in a project are converted to object files, with a compiler, and are usually given .o extensions.&amp;nbsp; I will follow this convention.&amp;nbsp; A C compiler then links all the individual object files together into one executable.&amp;nbsp; Firstly I will outline how to generate the object files, I will use the GNU compilers.&lt;br /&gt;
&lt;br /&gt;
In the case of the FORTRAN subroutine, type into a terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ gfortran -c addtwof.f95 -o addtwof.o&lt;br /&gt;
&lt;br /&gt;
This will compile the FORTRAN code in to an object file.&amp;nbsp; It is the flag -c which ensures this.&amp;nbsp; I have opted to name the output addtwof.o.&amp;nbsp; Next the C program must be converted to an object file.&amp;nbsp; Type in to a terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ gcc -c f.c -o f.o&lt;br /&gt;
&lt;br /&gt;
All of the same comments for the gfortran call apply.&amp;nbsp; The -c flag provides an object file, here named f.o.&amp;nbsp; Now the tricky object file, the one from the template file.&amp;nbsp; To do this requires the Mathematica program mprep.&amp;nbsp; This will convert the .tm template file into C code.&amp;nbsp; The C code is passed to gcc to give an object file and we have three object files.&lt;br /&gt;
&lt;br /&gt;
I found the mprep program in the SystemFiles directory of my Mathematica installation.&amp;nbsp; Specifically it is in .../SystemFiles/Links/MathLink/Developerkit/&lt;i&gt;architecture&lt;/i&gt;/CompilerAdditions directory, where &lt;i&gt;architecture&lt;/i&gt; is either the 32-bit of 64-bit sub-directory, depending on your installation and OS.&amp;nbsp; In the folder you will find several useful things.&amp;nbsp; Firstly the program mprep, the script mcc (I have referred to it as a program before but it actually is a script) and some libraries.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To use mprep you have several options.&amp;nbsp; One is to type in to a terminal the location of mprep and the location of the template file.&amp;nbsp; This would look something like,&lt;br /&gt;
&lt;br /&gt;
terminal $ /usr/local/Wolfram/.../CompilerAdditions/mprep /home/user/.../f.tm -o f.tm.c&lt;br /&gt;
&lt;br /&gt;
which would convert the .tm template file in to a f.tm.c C code file.&amp;nbsp; Or, if this is too unweildy, copy mprep and the libraries into the location where the template file is.&amp;nbsp; Then one can just type in to a terminal,&lt;br /&gt;
&lt;br /&gt;
terminal $ ./mprep f.tm -o f.tm.c&lt;br /&gt;
&lt;br /&gt;
Finally you could copy the template file to the mprep directory but that could get messy if many MathLink projects are undertaken.&amp;nbsp; Now we can use gcc to generate an object file from the template C code, f.tm.c.&amp;nbsp; This is done like before,&lt;br /&gt;
&lt;br /&gt;
terminal $ gcc -c f.tm.c -o f.tm.o&lt;br /&gt;
&lt;br /&gt;
Now we have three objects files, one from the FORTRAN code, one from the C code and one from the template file.&amp;nbsp; To link them all together use gcc again but with a specific set of libraries.&amp;nbsp; Into a terminal type,&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
terminal $ gcc f.tm.o f.o addtwof.o libML64i3.a -lm -lpthread -lrt -lstdc++ -o program&lt;br /&gt;
&lt;br /&gt;
This will output an executable, here called program.&amp;nbsp; The library calls are quite complicated.&amp;nbsp; Of the 5 calls, 4 are normal enough and all begin with -l.&amp;nbsp; The final one is a Mathematica one, there are two libraries.&amp;nbsp; The above code calls the 64-bit version, there is a 32-bit version called libML32o3.a.&amp;nbsp; Both of which are in the same location as the mprep file.&amp;nbsp; The code I have given assumes the libraries have been moved to the directory where all the object files are.&amp;nbsp; You will need to tweak the terminal commands or copy files around, as is appropriate.&lt;br /&gt;
&lt;br /&gt;
The above procedure is the details of the mcc script.&amp;nbsp; Passing the template file and C code to the mcc script causes several things.&amp;nbsp; Firstly mcc works out where the mprep program and all the libraries it needs are, which architecture it is running on and what C compile it has to use.&amp;nbsp; Then it starts mprep and the C compiler as we have done.&amp;nbsp; In fact, if you can not find certain files, you can open the mcc script (it is easier to find because it is also in a file with the other Mathematica executables) and read where files are and generally how mcc works.&lt;br /&gt;
&lt;br /&gt;
If you have got this far then it is easy to use your executable in Mathematica with the Install command as before.&lt;br /&gt;
&lt;br /&gt;
By studying this tutorial and changing the relevant code it should be simple to have Mathematica call a C program that calls a very large range of languages.&amp;nbsp; It should be possible to produce very powerful tools using this method.</content><link rel='replies' type='application/atom+xml' href='http://compotato.blogspot.com/feeds/1723666396686452552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://compotato.blogspot.com/2011/09/calling-fortran-code-with-mathlink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/1723666396686452552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/1723666396686452552'/><link rel='alternate' type='text/html' href='http://compotato.blogspot.com/2011/09/calling-fortran-code-with-mathlink.html' title='Calling FORTRAN Code with MathLink'/><author><name>Neutralino</name><uri>http://www.blogger.com/profile/04380605872780563173</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7090518505459435252.post-7754384162539259696</id><published>2011-09-15T16:32:00.000-07:00</published><updated>2011-09-16T01:53:44.894-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="Mathematica"/><category scheme="http://www.blogger.com/atom/ns#" term="MathLink"/><title type='text'>A Simple Example of MathLink</title><content type='html'>This post shows how to use MathLink in the simplest scenario.&amp;nbsp; This post will show how to make Mathematica run a C program that takes the two inputs and returns the sum, all using MathLink.&lt;br /&gt;
A MathLink project consists of a minimum of two files.&amp;nbsp; One is the C program itself and the second is a template file that tells Mathematica how to interact with the C program.&amp;nbsp; To join the files together there is a program called mprep, supplied by Mathematica, which converts the template to C code.&amp;nbsp; In this post I will not use mprep but instead use the program mcc which makes use of mprep and is simpler.&amp;nbsp; An explanation of how to use mprep will be given in a post about calling FORTRAN with MathLink.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now let us begin with the code.&amp;nbsp; Begin by writing a C function that solves the required problem.&amp;nbsp; Here I will tackle the challenge of adding two integers, x and y.&amp;nbsp; An appropriate C function is,&lt;br /&gt;
&lt;br /&gt;
int f(int x, int y) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return x*y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
To produce a complete C program there are two further components, both of which are standard for any MathLink exercise.&amp;nbsp; One is a header file &quot;mathlink.h&quot; and the second is the main body of the program which is supplied by Mathematica.&amp;nbsp; It is,&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return MLMain(argc, argv);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Putting all three components together gives the complete C program,&lt;br /&gt;
&lt;br /&gt;
#include &quot;mathlink.h&quot;&lt;br /&gt;
&lt;br /&gt;
int f(int x, int y) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return x+y;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return MLMain(argc, argv);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Next for the template file.&amp;nbsp; There is a guide to writing the template file in the Mathematica documentation.&amp;nbsp; &lt;a href=&quot;http://reference.wolfram.com/mathematica/tutorial/SettingUpExternalFunctionsToBeCalledFromMathematica.html&quot;&gt;Here&lt;/a&gt; is the template guide, the link also runs through the example solved here.&amp;nbsp; The template file is quite self-explanatory.&amp;nbsp; It explains that the function is called f, requires two integer inputs, x and y, and will return and integer.&amp;nbsp; Here it is,&lt;br /&gt;
&lt;br /&gt;
:Begin:&lt;br /&gt;
:Function:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;br /&gt;
:Pattern:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f[x_Integer, y_Integer]&lt;br /&gt;
:Arguments:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {x, y}&lt;br /&gt;
:ArgumentTypes: {Integer, Integer}&lt;br /&gt;
:ReturnType:&amp;nbsp;&amp;nbsp;&amp;nbsp; Integer&lt;br /&gt;
:End:&lt;br /&gt;
&lt;br /&gt;
Place this code in to a file which must have a .tm extension.&amp;nbsp; I will be using f.tm.&amp;nbsp; My C program will be called f.c.&amp;nbsp; To compile the code, open a terminal and type,&lt;br /&gt;
&lt;br /&gt;
terminal $ mcc f.c f.tm -o output&lt;br /&gt;
&lt;br /&gt;
The -o flag is optional and just controls the name of the resulting executable file as usual.&lt;br /&gt;
&lt;br /&gt;
It is now possible to use the function f in Mathematica.&amp;nbsp; After opening Mathematica, one needs the command Install[&lt;i&gt;argument&lt;/i&gt;] where the argument is the path to the executable, here called output, that has been generated.&amp;nbsp; After the command is executed Mathematica should be able to use f.&amp;nbsp; To try it one can type f[2,3] and Mathematica should reply 5!&lt;br /&gt;
&lt;br /&gt;
What MathLink does is to give a library that contains the C command MLMain(&lt;i&gt;argc, argv*&lt;/i&gt;).&amp;nbsp; The argument &lt;i&gt;argc&lt;/i&gt; refers to the name of the function.&amp;nbsp; Therefore, the name of the function in the C code and the name in the template file must match.&amp;nbsp; They will then match the name of the function in Mathematica.&amp;nbsp; (So one can name the C code file and the template file as one wishes.)&amp;nbsp; When the function is installed in Mathematica, and then called, Mathematica passes the string that names the function in the argument &lt;i&gt;argc&lt;/i&gt; and the arguments of the function in &lt;i&gt;argv*&lt;/i&gt;.&amp;nbsp; All of this is unraveled in the C program to call the corresponding function.</content><link rel='replies' type='application/atom+xml' href='http://compotato.blogspot.com/feeds/7754384162539259696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://compotato.blogspot.com/2011/09/simple-example-of-mathlink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/7754384162539259696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/7754384162539259696'/><link rel='alternate' type='text/html' href='http://compotato.blogspot.com/2011/09/simple-example-of-mathlink.html' title='A Simple Example of MathLink'/><author><name>Neutralino</name><uri>http://www.blogger.com/profile/04380605872780563173</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7090518505459435252.post-3370037232376110055</id><published>2011-09-15T15:28:00.000-07:00</published><updated>2011-09-15T15:28:04.843-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C"/><category scheme="http://www.blogger.com/atom/ns#" term="Lisp"/><category scheme="http://www.blogger.com/atom/ns#" term="Mathematica"/><category scheme="http://www.blogger.com/atom/ns#" term="MathLink"/><title type='text'>Adventures in MathLink</title><content type='html'>Recently I became interested in MathLink, a feature in Mathematica allowing for external calls to C programs.&amp;nbsp; There is also a J/Link project allowing Java to interface with Mathematica.&lt;br /&gt;
&lt;br /&gt;
In simple terms, MathLink allows Mathematica to run pass some arguments to a C program, run the program and return the results.&amp;nbsp; An example use of such a feature might be a numerical project.&amp;nbsp; Suppose one is interested in a numerical problem and would like to develop tools to perform simulations.&amp;nbsp; Typically Mathematica is slow at large scale numerical work, or, perhaps it is that a particular C library would be very useful.&amp;nbsp; Either way, one decides C code would be useful.&amp;nbsp; Using MathLink one could develop a toolkit of functions combining the efficiency of C code with the analytic tools and user interface of Mathematica.&amp;nbsp; Clearly this would be very useful to some people.&lt;br /&gt;
&lt;br /&gt;
It is also possible to use MathLink in reverse, that is to run Mathematica as a subroutine inside a C program.&amp;nbsp; I did not explore this option.&lt;br /&gt;
&lt;br /&gt;
To learn about MathLink I set myself three projects.&amp;nbsp; Firstly writing a simple proof-of-concept program in C that Mathematica could call.&amp;nbsp; Secondly writing a C program that itself calls a FORTRAN program.&amp;nbsp; Mathematica would then be able to call the C program, which would use a FORTRAN program to perform a computation, and return the results to Mathematica.&amp;nbsp; This may be useful to remaining FORTRAN users and it is a simple language for C code to call.&amp;nbsp; Thirdly, and most ambitiously, I wanted to produce a MathLink solution that allowed Mathematica to call Lisp code, again through a C program.&lt;br /&gt;
&lt;br /&gt;
Each of the three tasks will make up further blog entries.</content><link rel='replies' type='application/atom+xml' href='http://compotato.blogspot.com/feeds/3370037232376110055/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://compotato.blogspot.com/2011/09/adventures-in-mathlink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/3370037232376110055'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7090518505459435252/posts/default/3370037232376110055'/><link rel='alternate' type='text/html' href='http://compotato.blogspot.com/2011/09/adventures-in-mathlink.html' title='Adventures in MathLink'/><author><name>Neutralino</name><uri>http://www.blogger.com/profile/04380605872780563173</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>