2
$\begingroup$

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)
{
  double x, y, dist, xadd, yadd, temp;
  int xco, yco, counts;

  MLPutFunction(stdlink,"List",divs);

  for(yco = 0; yco <divs; yco++){
  yadd = ymin + (ymax-ymin)*yco/(divs-1.0);
  MLPutFunction(stdlink,"List",divs);

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

  x = x0;
  y = y0;
  xadd = xmin + (xmax-xmin)*xco/(divs-1.0);

  dist = x0*x0+y0*y0;
  for(counts = 0; counts<100 && dist < 10000; counts++){
  temp = x*x-y*y+xadd;
  y = 2*x*y+yadd;
  x = temp;
  dist = x*x+y*y;
  }
  MLPutInteger(stdlink,counts);
  }
  }
}

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.

$\endgroup$
  • 2
    $\begingroup$ 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". $\endgroup$ – Daniel Lichtblau Jun 27 '17 at 15:45
  • 1
    $\begingroup$ Here's a Mandelbrot implementation with Compile. It may very well be faster than the MathLink program above. $\endgroup$ – Szabolcs Aug 30 '17 at 10:26
2
$\begingroup$

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:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.