Fortran complex array assignment

  • Fortran
  • Thread starter xaratustra
  • Start date
  • #1
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.
 

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
7,002
293
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
38
0
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
AlephZero
Science Advisor
Homework Helper
7,002
293
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
38
0
Yes. works perfectly now. :biggrin:

many thanks.
 

Related Threads on Fortran complex array assignment

Replies
7
Views
9K
  • Last Post
Replies
4
Views
776
Replies
4
Views
1K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
1
Views
8K
  • Last Post
Replies
20
Views
2K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
2
Views
736
Replies
13
Views
1K
  • Last Post
Replies
1
Views
4K
Top