Group Abstract Group Abstract

Message Boards Message Boards

How to speed up code with the new compiler?

Posted 4 years ago

enter image description here

Above is the painting by Parmigiano "Self-portrait in a Convex Mirror". Based upon my past contribution on reflection and anamorphism in spherical mirrors, I investigated if I could make a similar "painting"using Mathematica. Here is the geometrical setup:

enter image description here

C is the center of he mirror sphere, V is the view point and let S be a point to be reflected. The first problem is to find a point Q on the sphere such that the light ray VQ and the reflected ray QS form equal angles with the normal n on the sphere at Q. I wrote a function including an NSolvevalues stating the laws of reflection: V,C, S and Q in same plane and vector angles VQ to n and SQ to n must be equal:

findIntersection[ptS : {xs_, ys_, zs_}, xv_] := 
 Module[{ptC = {0, 0, 0}, plane, ptV = {xv, 0, 0}}, 
  plane = InfinitePlane[{ptC, ptV, ptS}]; 
  Quiet@First[
    NSolveValues[{x, y, z} \[Element] Sphere[] && 
      VectorAngle[ptV - {x, y, z}, {x, y, z}] == 
       VectorAngle[ptS - {x, y, z}, {x, y, z}] && {x, y, 
        z} \[Element] plane && x > 0, {x, y, z}]]]

Works all right but the problem is the time it takes to find just one point Q. And I need a Q for every pixel in an image!

RepeatedTiming[findIntersection[{3.5, -4.5, -1.5}, 7.55]]

enter image description here

I know about the new compiler of the Wolfram Language but I am not familiar enough with it to write myself a CompiledFunction version of my function. The old Compile does not work properly for this and takes even longer:

findIntersectionCF = Compile[{{ptS, _Real, 1}, {xv, _Real}},
  First@NSolveValues[{x, y, z} \[Element] Sphere[] && 
     VectorAngle[ptV - {x, y, z}, {x, y, z}] == 
      VectorAngle[ptS - {x, y, z}, {x, y, z}] && {x, y, z} \[Element] 
      InfinitePlane[{{0., 0., 0.}, {xv, 0., 0.}, ptS}] && x > 0, {x, 
     y, z}], CompilationTarget -> "C"]

enter image description here

My question is : Can somebody at Wolfram or this community convert my function to the new CompiledFunction code and speed my function up?

POSTED BY: Erik Mahieu
5 Replies
POSTED BY: Daniel Lichtblau

Not sure if the "new" compiler (FunctionCompile) can be of use here. The workhorse function (NSolveValues) is already a C level function in the Wolfram Language and thus as fast as can be.

POSTED BY: Arnoud Buzing
Posted 4 years ago

I made an error in the formulation of my question. It should read: "Can somebody at Wolfram or this community convert my function to the new CompiledCodeFunction (instead of CompileFunction code)and speed my function up?" Sorry about that...

POSTED BY: Erik Mahieu
Posted 4 years ago

Thanks for your reply Arnaud. As suggested by Daniel, it appears better in some cases to rephrase the function than to Compile it.

POSTED BY: Erik Mahieu
Posted 4 years ago

Thank you Daniel, excellent and useful suggestions. There is something for me to learn in every step of your solution!

POSTED BY: Erik Mahieu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard