How to achieve faster performance on plotting complex valued functions - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-22T04:46:32Z https://mathematica.stackexchange.com/feeds/question/184351 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/184351 5 How to achieve faster performance on plotting complex valued functions Arjihad https://mathematica.stackexchange.com/users/42739 2018-10-21T16:43:55Z 2019-09-20T07:34:36Z <p>I'm very interested in visualizing complex valued functions <span class="math-container">$f:\mathbb{C} \rightarrow \mathbb{C}$</span>. Especially interesting to me are Möbius transformations. </p> <p>I found a nice way of visualizing these kind of functions at</p> <p><a href="https://mathematica.stackexchange.com/questions/21314/how-do-i-put-an-image-on-the-complex-plane">How do I put an image on the complex plane</a></p> <p>I do use the code provided by <a href="https://mathematica.stackexchange.com/users/50/j-m">J. M.</a> very often.</p> <p>Here is an example:</p> <pre><code>c=1/2; image= </code></pre> <p><a href="https://i.stack.imgur.com/GZcUT.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GZcUT.jpg" alt="input_image"></a></p> <p>Using the Möbius transformation <span class="math-container">$\dfrac{z-c}{\overline{c}z-1}$</span> which maps the unit circle onto the unit circle itself, we get:</p> <pre><code>Image[ImageForwardTransformation[image, Through[{Re, Im}[((#[] + I #[])- c)/(Conjugate[c]*(#[] + I #[]) - 1)]] &amp;, Background -&gt; 1, DataRange -&gt; {{-1, 1}, {-1, 1}}, PlotRange -&gt; {{-1, 1}, {-1, 1}}]] </code></pre> <p><a href="https://i.stack.imgur.com/rIsev.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rIsev.jpg" alt="output_image"></a></p> <p>As you can see, the image quality is a little bit on the lower side at some spots, which is totally expected using this plot. I do get better results using input images with a higher pixel count. I generally like to use 4096×4096 px, which takes some time to plot. I'm asking if there is any way of speeding up this plotting method using bigger input images?</p> <p>The plot takes about 2-3 minutes. Also Mathematica does not make use of the 12 logical CPU cores my machine has. Is there maybe a way of splitting this up for multicore performance?</p> https://mathematica.stackexchange.com/questions/184351/-/184357#184357 4 Answer by Michael E2 for How to achieve faster performance on plotting complex valued functions Michael E2 https://mathematica.stackexchange.com/users/4999 2018-10-21T18:37:53Z 2018-10-22T00:35:06Z <p>Here's a quick idea of <a href="https://mathematica.stackexchange.com/questions/184351/how-to-achieve-faster-performance-on-plotting-complex-valued-functions/184357#comment480811_184351">what I had in mind in my comment</a>:</p> <pre><code>plot = ParametricPlot[r {Cos[t], Sin[t]}, {r, 0, 1}, {t, 0, 2 Pi}, PlotPoints -&gt; {35, 201}, Mesh -&gt; 11, Axes -&gt; False, Frame -&gt; False, ColorFunction -&gt; (Hue[#4] &amp;)]; {gc} = Cases[plot, _GraphicsComplex]; xf[c_] := ReIm@((# - c)/(Conjugate[c] # - 1) &amp;)@(#.{1, I}) &amp;; Manipulate[ Graphics@MapAt[xf[c.{1, I}], gc, 1], {{c, {0, 0}}, {-1, -1}, {1, 1}, TrackingFunction -&gt; ((c = #/Max[1, 1.1 Norm[#]]) &amp;)} ] </code></pre> <p><img src="https://i.stack.imgur.com/zqid4.png" alt="Mathematica graphics"></p> <p>It could be made even quicker with a more efficient initial image <code>plot</code>.</p> <p><em>Update:</em> Here's a more efficient <code>gc</code>, using single polygons for each block:</p> <pre><code>ClearAll[polarRect]; polarRect[{r1_, r2_}, {t1_, t2_}, n_: 60] := Polygon@Join[ Table[ r2 {Cos[t], Sin[t]}, {t, Subdivide[t1, t2, 2 + Round[n*r2/(t2 - t1)]]}], Rest@Table[r {Cos[t2], Sin[t2]}, {r, r2, r1, (r1 - r2)/Round[n/4]}], {r1 {Cos[t2], Sin[t2]}}, If[r1 == 0, {}, Table[ r1 {Cos[t], Sin[t]}, {t, Subdivide[t2, t1, 2 + Round[n*r2/(t2 - t1)]]}]], Rest@Table[r {Cos[t1], Sin[t1]}, {r, r1, r2, (r2 - r1)/Round[n/4]}] ]; With[{dr = 1/10, dt = Pi/4}, base = {EdgeForm[Directive[Thin, Black]], Table[ {Hue[t/2/Pi], N@polarRect[{r, r + dr}, {t, t + dt}]}, {r, 0, 1 - dr, dr}, {t, 0, 2 Pi - dt, dt} ] } ]; coords = DeleteDuplicates@ Developer`ToPackedArray[ Flatten[Cases[base, Polygon[p_] :&gt; p, Infinity], 1], Real]; nf = Nearest[coords -&gt; "Index"]; gc = GraphicsComplex[coords, base /. Polygon -&gt; Polygon@*Flatten@*nf]; </code></pre> https://mathematica.stackexchange.com/questions/184351/-/206538#206538 2 Answer by chyanog for How to achieve faster performance on plotting complex valued functions chyanog https://mathematica.stackexchange.com/users/2090 2019-09-20T07:14:29Z 2019-09-20T07:34:36Z <pre><code>image=Import["https://i.stack.imgur.com/GZcUT.jpg"] c=1/2; ImageForwardTransformation[image, Through[{Re,Im}[((#[]+I #[])-c)/(Conjugate[c]*(#[]+I #[])-1)]]&amp;, Background-&gt;1,DataRange-&gt;{{-1,1},{-1,1}},PlotRange-&gt;{{-1,1},{-1,1}}]//AbsoluteTiming </code></pre> <p><a href="https://i.stack.imgur.com/dgZuB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dgZuB.png" alt="enter image description here"></a></p> <p>Compile transformation function </p> <pre><code>expr=ReIm[((#[]+I #[])-c)/(Conjugate[c]*(#[]+I #[])-1)]&amp;[{x,y}]// ComplexExpand//Simplify cf=With[{expr=expr},Compile[{{v,_Real,1}},Block[{x=v[],y=v[]},expr]]]; ImageForwardTransformation[image,cf, Background-&gt;1,DataRange-&gt;{{-1,1},{-1,1}},PlotRange-&gt;{{-1,1},{-1,1}}]//AbsoluteTiming </code></pre> <p><a href="https://i.stack.imgur.com/K19CQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K19CQ.png" alt="enter image description here"></a></p> <p>Using <code>ParametricPlot</code> with Texture</p> <pre><code>ParametricPlot[expr,{x,-1,1},{y,-1,1}, PlotStyle-&gt;{Opacity,Texture[image]},ImageSize-&gt;ImageDimensions[image], BoundaryStyle-&gt;None,Axes-&gt;False,Frame-&gt;False,PlotRange-&gt;1]//AbsoluteTiming </code></pre> <p><a href="https://i.stack.imgur.com/yzsNn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yzsNn.png" alt="enter image description here"></a></p>