Fortran Fortran complex array assignment

AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
4
Views
2K
Replies
25
Views
3K
Replies
4
Views
2K
Replies
5
Views
9K
Replies
13
Views
2K
Replies
16
Views
2K
Replies
3
Views
2K
Back
Top