Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Complex Exponential

  1. Apr 19, 2015 #1
    Hi, so I need to write a fortran code with 2, 2x2 matrices.

    These matrices are in the form of B=(1 exp(i)(theta) 0 0) and D=(0 0 exp(i)(theta) 1) where i is sqrt of -1 and theta is an angle between 0 and 2pi.

    I've expanded the exponential so it reads cos(theta)+isin(theta) and let theta=pi/2

    I've delcared i as complex in the form of i=(0.0,1.0) however as the matrices are declared as real with real components in them (ie: 0.0, 1.0) an error appears stating that "You cannot mix types in an array constructor (Complex(Kind=1)) in a real(Kind=1) constructor."

    Is it possible to mix types in an array constructor or can I have the whole array as complex?

    Any help would be much appreciated.
  2. jcsd
  3. Apr 19, 2015 #2


    Staff: Mentor

    No, you can't mix types in an array. Make the base type of the array COMPLEX.
  4. Apr 19, 2015 #3


    User Avatar

    Staff: Mentor

    Also, Fortran's generic EXP function accommodates the COMPLEX data type, so you can do stuff like this:

    Code (Text):

          complex i, z
          real theta
          i = (0.0, 1.0)
          theta = 0.5
          z = exp (i*theta)
          print *, z
    No need to use Euler's identity to calculate the real and imaginary parts separately.
  5. Apr 20, 2015 #4
    Thanks for your help, I've declared each matrix as complex and used exp(i)(theta) instead of cos and sine however the problem still remains, here is my programme below:

    implicit none
    REAL, PARAMETER :: pi=3.14159
    real :: ph
    complex, dimension(2,2) :: B, D
    complex :: z, ic
    ph= 1.0
    ic = ( 0.0, 1.0 )

    z = exp(ic*ph)

    B = RESHAPE( (/ 1.0, z ,0.0,0.0 /), (/2.0,2.0/) )

    D = RESHAPE( (/ 0.0,0.0,z,1.0 /), (/2.0,2.0/) )

    write(6,'(2f4.1)') B, D

    end program

    Is it because I'm using 1.0 and 0.0 as real values inside a complex array?

    If so, can I convert them so they're complex numbers?
  6. Apr 20, 2015 #5


    User Avatar

    Staff: Mentor

    Have you tried using (0.0, 0.0) and (1.0, 0.0) instead of 0.0 and 1.0?
  7. Apr 20, 2015 #6
    Thank you, that works!
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Fortran Complex Exponential Date
Runtime error 112 Oct 17, 2015
[Fortran] Help Reading Complex 2D data Jul 14, 2015
Mesh 2d of a complex geometry with FORTRAN Jun 29, 2015
[Fortran] Quick question about complex exponentials Jun 11, 2015
Complex number in fortran 90 May 17, 2015