<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dani Lisle</title>
    <description>The latest articles on Forem by Dani Lisle (@xdddani).</description>
    <link>https://forem.com/xdddani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1284528%2Fa6f40b3d-9a3c-4844-bf6b-8d6329a38507.jpg</url>
      <title>Forem: Dani Lisle</title>
      <link>https://forem.com/xdddani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xdddani"/>
    <language>en</language>
    <item>
      <title>Visualizing Complex-Valued Functions Using Python (or Wolfram)</title>
      <dc:creator>Dani Lisle</dc:creator>
      <pubDate>Mon, 19 Feb 2024 08:31:46 +0000</pubDate>
      <link>https://forem.com/xdddani/visualizing-complex-valued-functions-using-python-or-wolfram-4721</link>
      <guid>https://forem.com/xdddani/visualizing-complex-valued-functions-using-python-or-wolfram-4721</guid>
      <description>&lt;p&gt;Today I was pouring through my "Complex Variables and Analytic Functions" book (written by the esteemed Bengt Fornberg and Cecile Piret), trying my best to wrap my mind around how complex function work and how to visualize them. The visualization part is extra difficult given that such a function takes a real and an imaginary input component, and outputs two such components as well. Therefore, a single 3D plot is not sufficient to "see" how the function behaves. Rather, we split the output into either an imaginary and real part, or into the magnitude and the argument/angle.&lt;/p&gt;

&lt;p&gt;The plots in the book were helpful, but I wanted to be able to play around some more. Wolfram Mathematica is an excellent tool for such visual explorations.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wolfram Language
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plotComplexFunction[f_]:=Module[{z,rePlot,imPlot,magPlot,phasePlot},z=x+I y;

rePlot = Plot3D[Re[f[z]],{x,-2,2},{y,-2,2},AxesLabel-&amp;gt;{"Re(z)","Im(z)","Re(f(z))"},Mesh-&amp;gt;None];

imPlot = Plot3D[Im[f[z]],{x,-2,2},{y,-2,2},
    AxesLabel-&amp;gt;{"Re(z)","Im(z)","Im(f(z))"},
    Mesh-&amp;gt;None];

magPlot = Plot3D[Abs[f[z]], {x, -2, 2}, {y, -2, 2}, 
    AxesLabel -&amp;gt; {"Re(z)", "Im(z)", "Abs(f(z))"}, 
    Mesh -&amp;gt; None, 
    ColorFunction -&amp;gt; Function[{x, y, z}, ColorData["Rainbow"][Rescale[Arg[x + I y], {-Pi, Pi}]]], 
    ColorFunctionScaling -&amp;gt; False];

phasePlot=DensityPlot[Arg[f[z]],{x,-2,2},{y,-2,2},
    ColorFunction-&amp;gt;"Rainbow",
    PlotLegends-&amp;gt;Automatic,
    AxesLabel-&amp;gt;{"Re(z)","Im(z)"},
    PlotLabel-&amp;gt;"Phase"];

GraphicsGrid[{{rePlot,imPlot},{magPlot,phasePlot}},ImageSize-&amp;gt;800]];

f[z_]:=(1/2)*(z+1/z);
plotComplexFunction[f]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/dreamchef/complex-functions-visualization"&gt;https://github.com/dreamchef/complex-functions-visualization&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wrote the above Mathematica code to produce a grid of plots showing the function in both ways just described. On the top, the imaginary and real parts of the function &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jvem791tkemxu40g4of.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jvem791tkemxu40g4of.png" alt="Image description" width="408" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;are shown, and on the bottom, and the magnitude, and the phase shown in color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4mzcyp7ze0ri1auv976.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4mzcyp7ze0ri1auv976.png" alt="Image description" width="800" height="703"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After playing around with a few functions using this code and convincing myself that it made sense why other functions looked the way they did, I wanted to do the same in Python as an exercise.&lt;/p&gt;

&lt;h1&gt;
  
  
  Python, PyPlot and complex-plotting-tools
&lt;/h1&gt;

&lt;p&gt;I found an excellent project on GitHub (&lt;a href="https://github.com/artmenlope/complex-plotting-tools"&gt;https://github.com/artmenlope/complex-plotting-tools&lt;/a&gt;) which I decided to use as a starting point, and potentially contribute to in the future. The repo provided a very easy interface for plotting complex-valued functions in a variety of ways. For example, after importing &lt;code&gt;numpy&lt;/code&gt;, &lt;code&gt;matplotlib&lt;/code&gt;, and the repo's &lt;code&gt;cplotting_tools module&lt;/code&gt;, defining the function and calling &lt;code&gt;cplt.complex_plot3D(x, y, f, log_mode=False)&lt;/code&gt; produces the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyfx2gjgehfsjhhazy9tu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyfx2gjgehfsjhhazy9tu.png" alt="Image description" width="800" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These are all for the same f(z) as above. To view the side-by-side imaginary and real parts of the function, use &lt;code&gt;cplt.plot_re_im(x, y, f, cmap="twilight", contour=False, alpha=0.9)&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdrfj132bx02rhckk4i7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdrfj132bx02rhckk4i7.png" alt="Image description" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Additionally, the library provides other cool ways to study functions, including a stream plot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3up50c941h7w31frmtzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3up50c941h7w31frmtzt.png" alt="Image description" width="800" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Future Direction
&lt;/h1&gt;

&lt;p&gt;The library shows a lot of promise and is relatively easy to use! It does require a &lt;code&gt;pts&lt;/code&gt; variable to be defined for a given function which encodes the poles and zeros of the function. Wolfram does not require this because it figures out this information under the hood, and it would be really nice if complex-plotting-tools had this functionality as well. Hopefully, I'll be able to figure out an implementation for this in the future and submit that change.&lt;/p&gt;

&lt;p&gt;In the meantime, have fun plotting with Wolfram and Python, and send any questions or ideas for development my way!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;br&gt;
Dani&lt;/p&gt;

</description>
      <category>python</category>
      <category>mathematics</category>
      <category>visualization</category>
      <category>github</category>
    </item>
  </channel>
</rss>
