Fortran complex array assignment

Click For Summary

Discussion Overview

The discussion revolves around assigning values to a complex array in Fortran, specifically addressing errors encountered during the assignment process within a loop. Participants explore different methods for correctly initializing the real and imaginary parts of complex numbers.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error "Unclassifiable statement at (1)" when attempting to assign values to a complex array using the real() and aimag() functions.
  • Another participant clarifies that real() and aimag() are not suitable for modifying the components of a complex number and suggests using a tuple assignment instead.
  • A later reply confirms that the tuple assignment method does not work in a specific example involving expressions, leading to another error message.
  • Another participant proposes using the cmplx() function for assignments, indicating that it may resolve the issue with expressions.
  • One participant confirms success with the cmplx() method after initial difficulties.

Areas of Agreement / Disagreement

Participants generally agree on the limitations of using real() and aimag() for assignments, but there is some disagreement regarding the effectiveness of tuple assignments with expressions, which remains unresolved until the suggestion of using cmplx() is introduced.

Contextual Notes

Participants note that the tuple assignment method may not work for expressions, indicating a potential limitation in the language's handling of complex number assignments.

Who May Find This Useful

This discussion may be useful for programmers working with Fortran, particularly those dealing with complex numbers and array assignments.

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.
 

Similar threads

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