title: How to compute a complex function in a cleverer way with Fortran?
body: <p>I want to solve this equation by using Fortran:</p>
  <p><img src="https://latex.codecogs.com/svg.image?%5Cpsi&amp;space;=&amp;space;-i(-%5Cbeta&amp;space;%5Cfrac%7B%5Cpartial%5E2&amp;space;%5Cpsi%7D%7B%5Cpartial&amp;space;x%5E2%7D+%5Calpha&amp;space;%5Cleft%7C&amp;space;%5Cpsi%5Cright%7C%5E2%5Cpsi)" alt="eq1" /></p>
  <p>Where ψ (<code>psi</code>) is a complex Fortran variable.
  Now, I am solving this by defining two new complex variables: <br />
  <code>ir=(1.0,0.0)</code> and <code>ii=(0.0,1.0)</code>.<br />
  I use these to select only the real or imaginary part of the equation. In this way I solve my equation separately for the real and imaginary part. The code is here:</p>
  <pre><code>do i = 1,nn    
    mod2 = (abs(psi(i)))**2
    psi(i) = ir*(-beta*imag(der2(i)) + alpha*mod2*imag(psi(i))) + ii*(beta*real(der2(i)) - alpha*mod2*real(psi(i)))
  end do
  </code></pre>
  <p>Where <code>psi</code> and <code>der2</code> are complex arrays with <code>nn</code> elements.<br />
  I want to solve this equation in a better way without splitting it in two equations. I tried to solve it in this way:</p>
  <pre><code>mod2 = abs(psi)**2
  psi = -ii*(-beta*der2+alpha*mod2*psi)
  </code></pre>
  <p>but it doesn't work because I obtain completely different values with respect to the first method I used. For me it makes sense that it doesn't work because in the second method I am not evaluating the real part. Is this right?<br />
  As an example, the 10° element of my psi array becomes:\</p>
  <ol>
  <li>(-6.39094355774850412E-003,-6.04041029332164168E-003) (with 1° method)\</li>
  <li>(-1.75266632213431602E-004,-6.21567692553507290E-003) (2° method)\</li>
  </ol>
  <p>Any suggestion?
  Thank you!</p>