Fortran Fortran complex array assignment

Click For Summary
The discussion revolves around an error encountered while trying to assign values to a complex array in Fortran. The user initially attempts to assign values to the real and imaginary parts of a complex number using the `real()` and `aimag()` functions, which is incorrect as these functions cannot modify the components of a complex number. The correct approach is to assign a complex number directly using the syntax `in(t) = (cos(2 * pi * f0 * t), sin(2 * pi * f0 * t))`. However, when testing with a simple program, the user encounters another error related to the syntax for assigning values to the complex array. The solution provided is to use the `cmplx()` function for assignments involving expressions, which resolves the issue. The final confirmation indicates that using `cmplx(2*t, 3*3)` works correctly, demonstrating the importance of using the appropriate functions for complex number assignments in Fortran.
xaratustra
Messages
38
Reaction score
0
I have a an array of type complex. I am trying to assign a value to it in a loop, but I get a strange error "Unclassifiable statement at (1)". I really don't understand why.

Code:
do t = 1, count
   real(in(t)) = cos(2 * pi * f0 * t) 
   aimag(in(t))=sin(2 * pi * f0 * t) 
end do

thanks.
 
Technology news on Phys.org
The real() and imag() just return values, like all other Fortran functions. You can't use them to change the real or imaginary part of a complex number.

This should work:
Code:
do t = 1, count
  in(t) = ( cos(2 * pi * f0 * t) , sin(2 * pi * f0 * t) )
enddo
 
AlephZero said:
This should work:
Code:
do t = 1, count
  in(t) = ( cos(2 * pi * f0 * t) , sin(2 * pi * f0 * t) )
enddo

sorry for late reply.
But unfortunately it doesn't work. Even in the following simple code:
Code:
program test
  implicit none
  integer::t
  complex::in(4)
  do t = 1, 4
     in(t) = ( 2*t, 3*3 )
  end do
end program test

gfortran gives the following result:

Code:
test.f95:6.18:

     in(t) = ( 2*t, 3*3 )
                     1
Error: Expected a right parenthesis in expression at (1)

any ideas?
 
Try in(t) = cmplx( 2*t, 3*3 )

in(t) = (1.0, 2.0) should work for a constant value. Maybe it doesn't work for expressions.
 
Yes. works perfectly now. :biggrin:

many thanks.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K