MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathematica to .NET compiler

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79314] Re: Mathematica to .NET compiler
  • From: Jon Harrop <jon at ffconsultancy.com>
  • Date: Tue, 24 Jul 2007 06:07:46 -0400 (EDT)
  • References: <f7pon7$prc$1@smc.vnet.net> <f7sgu2$ss8$1@smc.vnet.net>

David Bailey wrote:
> You would only be able to compile about as much as the Compile...

I think we could add some useful constructs like recursion.

> There is, of course, MathCode, which translates a subset of Mathematica
> to C++:
> 
> http://www.mathcore.com/products/mathcode/mathcodec++.php
> 
> The subset in question is described on a link, which was unfortunately
> dead when I pressed it. I would imagine their subset would be much the
> same as yours would need to be.

I have been unable to find out what exactly the MathCode compilers can
compile but their chosen target languages (C++ and Fortran) are not garbage
collected which, I assume, severely limits what they can compile.

As an example, I might like to be able to compile the following program (a
simple ray tracer):

scene =
  N@{Sphere[{0, 0, 4}, 1],
     Sphere[{-1, 1, 4}, 1],
     Sphere[{-1,-1, 4}, 1],
     Sphere[{1, 1, 4}, 1],
     Sphere[{1,-1, 4}, 1]};

\[Delta] = Sqrt[$MachineEpsilon];

Unitise[p_] := p / Sqrt[p.p]

RaySphere[o_, d_, c_, r_] :=
  Block[{v=c-o, b=v.d, disc=b^2 - v.v + r^2},
    If[disc <= 0., \[Infinity],
      disc = Sqrt[disc];
      Block[{t2 = b + disc},
        If[t2 <= 0., \[Infinity],
          Block[{t1 = b - disc},
            If[t1 > 0., t1, t2]]
        ]
      ]
    ]
  ]

Intersect[o_, d_][{lambda_, n_}, Sphere[c_, r_]] :=
  Block[{lambda2 = RaySphere[o, d, c, r]},
    If[lambda2 >= lambda, {lambda, n}, {lambda2, Unitise[o + lambda2 d -
c]}]
    ]
Intersect[o_, d_][hit_, list_List] := Fold[Intersect[o, d], hit, list]

nohit = N@{\[Infinity], {0, 0, 0}};

neglight = N@Unitise[{1, 3, -2}];

RayTrace[o_, d_, scene_] :=
  Block[{lambda, n, g, p},
    {lambda, n} = Intersect[o, d][nohit, scene];
    If[lambda === \[Infinity], 0.,
      g = n.neglight;
      If[g <= 0., 0.,
        p = o + lambda d + \[Delta] n;
        {lambda, n} = Intersect[p, neglight][nohit, scene];
        If[lambda < \[Infinity], 0., g]]]
    ]

Timing[image =
      Table[
        Table[
          RayTrace[{0., 0., -2.5}, Unitise[N@{x, y, 128}], scene],
            {y, -64, 64}],
          {x, -64, 64}];]

image;

Show[Graphics[Raster[image]], AspectRatio -> 1];

-- 
Dr Jon D Harrop, Flying Frog Consultancy
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?usenet


  • Prev by Date: Please help in creating/installing my package
  • Next by Date: save as (regular) html problem
  • Previous by thread: Re: Mathematica to .NET compiler
  • Next by thread: Re: Mathematica to .NET compiler