Fortran complex array assignment

In summary, the conversation discusses an issue with assigning values to a complex array in a loop and the error "Unclassifiable statement at (1)" that occurs. The expert suggests using the cmplx() function to assign values to the array and provides an example code that should work. The issue is resolved with this solution.
  • #1
xaratustra
38
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
  • #2
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
 
  • #3
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?
 
  • #4
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.
 
  • #5
Yes. works perfectly now. :biggrin:

many thanks.
 

1. What is "Fortran complex array assignment"?

"Fortran complex array assignment" refers to a feature in the Fortran programming language that allows for the assignment of values to arrays containing complex numbers. This allows for efficient manipulation and processing of complex data in scientific and engineering applications.

2. How is complex array assignment different from regular array assignment in Fortran?

In regular array assignment, only real numbers can be assigned to the array elements. In complex array assignment, both real and imaginary numbers can be assigned to the array elements, allowing for the representation of complex data.

3. Can complex arrays be assigned values from other complex arrays?

Yes, complex arrays can be assigned values from other complex arrays using the usual assignment operator (=). However, the arrays must have the same shape and type.

4. What is the syntax for complex array assignment in Fortran?

The syntax for complex array assignment in Fortran is array_name(index) = (real_part, imaginary_part). The values within the parentheses represent the real and imaginary parts of the complex number being assigned to the array element at the given index.

5. Are there any built-in functions for complex array assignment in Fortran?

Yes, Fortran has built-in functions for complex array assignment, such as CMPLX and DCMPLX, which can be used to assign values to complex arrays in a more concise manner. These functions also allow for the conversion of real numbers to complex numbers for assignment.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
178
  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
994
Back
Top