# MathLink with Shaw's Complex Analysis with Mathematica

I purchased (some time ago) a copy of William Shaw's Complex Analysis with Mathematica, published in 2006. He uses MathLink (something I've never done before) to speed up a Mandelbrot application. Here is his template file, named mandel.tm.

:Begin:
:Function:   fractalml
:Pattern: FractalML[a_?NumberQ,b_?NumberQ,{{c_?NumberQ,d_?NumberQ},{e_?NumberQ,f_?NumberQ}},g_Integer]
:Arguments:  { N[a],N[b],N[c],N[d],N[e],N[f],g }
:ArgumentTypes: { Real,Real,Real,Real,Real,Real,Integer}
:ReturnType:   Manual
:End:


And here is his c-code, named mandel.c.

#include "mathlink.h"

void fractalml(double x0,double y0, double xmin, double xmax, double ymin, double ymax, int divs)
{
int xco, yco, counts;

for(yco = 0; yco <divs; yco++){

for(xco = 0; xco <divs ; xco++){

x = x0;
y = y0;

dist = x0*x0+y0*y0;
for(counts = 0; counts<100 && dist < 10000; counts++){
x = temp;
dist = x*x+y*y;
}
}
}
}

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


There is another file (I think it is the compiled file of mandel.c?) named mandel in his folder binMacOSX. All of the files that I have mentioned are included on a CD with his book. I copied his cd files to my MacBook pro (Sierra OS X and Mathematica 11.1.1), then I followed Shaw's first instruction as follows:

SetDirectory["/Users/davidarnold/Documents/Mathematica/books/WilliamTShaw/ComplexAnalysis/MathLink/binMacOSX"]


Then I tried his second instruction:

Install["mandel"]


After about three minutes I had to abort. It did not install.

Again, I've never tried to do something like this before, so are there suggestions that might work with his code?

Thanks.

• This does not directly answer the question, but if the book has equivalent WL code you might try redefining it to use Compile with the target language set to "C". – Daniel Lichtblau Jun 27 '17 at 15:45
• Here's a Mandelbrot implementation with Compile. It may very well be faster than the MathLink program above. – Szabolcs Aug 30 '17 at 10:26

The C code needs to be compiled before it can be used. If the code came with a pre-compiled binary, it may still not work. Macs used a different architecture back in 2006.

To compile the code, first you need a supported C compiler. On a Mac:

• Install XCode from the Mac App Store
• From a terminal, run xcode-select --install

Test that the compiler works in Mathematica, like this:

<<CCompilerDriver
CCompilers[]


It should list Clang.

Now SetDirectory to where mandel.tm and mandel.c are and use

<<CCompilerDriver
CreateExecutable[{"mandel.tm", "mandel.c"}, "mandel", "TargetDirectory" -> "."]


This will create an executable named mandel in the same directory. You can use it with Install:

Install["mandel"]


Above I showed the automated method to compile. A step by step guide for manual compilation is here: