Can complex numbers be used in Fortran array constructors?

In summary, the conversation discusses the need to write a Fortran code with 2, 2x2 matrices 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. The speaker expands the exponential and declares i as complex, but encounters an error due to mixing types in an array constructor. The expert suggests making the base type of the array complex and using Fortran's generic EXP function. The speaker also asks about converting real values into complex numbers, to which the expert suggests using (0.0, 0.0) and (1
  • #1
Lukejambo
13
0
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.
 
Technology news on Phys.org
  • #2
Lukejambo said:
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?
No, you can't mix types in an array. Make the base type of the array COMPLEX.
Lukejambo said:
Any help would be much appreciated.
 
  • #3
Also, Fortran's generic EXP function accommodates the COMPLEX data type, so you can do stuff like this:

Code:
      complex i, z
      real theta
      i = (0.0, 1.0)
      theta = 0.5
      z = exp (i*theta)
      print *, z
      end

No need to use Euler's identity to calculate the real and imaginary parts separately.
 
  • #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:

program
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?
 
  • #5
Lukejambo said:
If so, can I convert them so they're complex numbers?

Have you tried using (0.0, 0.0) and (1.0, 0.0) instead of 0.0 and 1.0?
 
  • #6
Thank you, that works!
 

FAQ: Can complex numbers be used in Fortran array constructors?

What is a Fortran Complex Exponential?

A Fortran Complex Exponential is a mathematical function that calculates the exponential value of a complex number. It is used in scientific computing and engineering applications, particularly in the fields of physics and mathematics.

How is a Fortran Complex Exponential different from a regular exponential function?

A regular exponential function only accepts real numbers as input, whereas a Fortran Complex Exponential can accept complex numbers as input. It also outputs a complex number as its result.

What is the syntax for using a Fortran Complex Exponential?

The syntax for using a Fortran Complex Exponential is: exp(z), where z is a complex number expressed in the form (a, b) where a is the real part and b is the imaginary part.

What are some common applications of Fortran Complex Exponential?

Fortran Complex Exponential is commonly used in solving differential equations, Fourier analysis, and signal processing. It is also used in computing the electromagnetic field in physics simulations and in calculating heat transfer in engineering applications.

Are there any limitations to using Fortran Complex Exponential?

One limitation of using Fortran Complex Exponential is that it can only calculate the exponential value of a complex number, but cannot handle other mathematical operations such as addition or multiplication. It also requires some knowledge of complex numbers and their properties to use effectively.

Similar threads

Replies
4
Views
2K
Replies
4
Views
2K
Replies
8
Views
1K
Replies
5
Views
1K
Replies
4
Views
2K
Replies
21
Views
2K
Replies
8
Views
3K
Replies
12
Views
3K
Replies
9
Views
4K
Replies
6
Views
3K
Back
Top