Fortran Fortran 95 & complex numbers question

AI Thread Summary
The user is encountering a compilation error in Fortran 95 when attempting to assign a complex number to an array element using an expression that includes a variable from a double precision array. The error arises from the syntax used to define the complex number, specifically the expression for the real part involving the variable x(i). The user has tried using an auxiliary variable to resolve the issue, but it did not work, leading to frustration. A suggestion was made to use the CMPLX function for creating complex numbers, which could simplify the assignment. The user is seeking further assistance and clarification on the problem.
Sdakouls
Messages
8
Reaction score
0
Hi,

I'm trying to compile a code (.f95, compiling using gfortran) in which I'm using a 'do' loop to set the values in a complex array; the following little piece is giving me trouble:

Code:
double precision, dimension(n) :: x
complex, dimension(n) :: y

do i=1, n
          x(i) = ...
end do

do i=1, n
          y(i) = (a + b*x(i)*x(i), 0.0)
end do

Where x(i) is an array of 'double precision' numbers (which have already been set (and they are correct) in another 'do' loop, the details of which I have omitted for brevity), of the same dimension as y(i). When I try to compile I get the error message:

y(i) = (a + b*x(i)*x(i), 0.0)
_______________________1
Error: Expected a right parenthesis in expression at (1)(Note: I inserted the underscores in the second line of the error message myself to get the '1' in the right place.)

It was my understanding that to define a complex number in f95, one writes it in the form (c, d) where c & d can be integers or reals. If I replace the real part of the expression for y(i) by some explicitly real number such as (for example) 2.0, the program compiles and the problem goes away. This leads me to believe that my problem lies in the use of the array element(s) x(i) in the expression for y(i).

Also, I know that the number shown is real and the 0.0 in the imaginary slot is redundant, but I am actually going to need to do things like y(i) = (x(i), z(i)), which I can't progress to if I can't get the above to compile.

I'm new to f95 and I'm willing to bet that I've made some completely novice mistake. However, I'm at a loss here and any help would be very much appreciated.

Thank you.
 
Last edited:
Technology news on Phys.org
No idea what's the real problem, but easy way of going around would be to use auxiliary variable:

v = a + b*x(i)*x(i)
y(i) = (v, 0.0)
 
I tried that before I posted and it didn't work. And I have no idea why not.

Also, I actually have a bunch of arrays like y(i) that I want to set up in this manner, so using auxiliary variables for the real and imaginary parts of each of these arrays would get very messy very quickly.

Thank you anyway.

Anyone else think they know what the issue might be? I tried googling the problem and found some similar issues, but no clear cut solution to the problem
 
Have you tried the manual?

7.28 CMPLX — Complex conversion function
Description:
CMPLX(X,[Y,KIND]) returns a complex number where X is converted to the
real component. If Y is present it is converted to the imaginary component. If
Y is not present then the imaginary component is set to 0.0. If X is complex
then Y must not be present.
Option: f95, gnu
Class: elemental function
Syntax: C = CMPLX(X[,Y,KIND])
Arguments:
X The type may be INTEGER(*), REAL(*), or COMPLEX(*).
Y Optional, allowed if X is not COMPLEX(*). May be INTEGER(*) or
REAL(*).
KIND Optional scaler integer initialization expression.
Return value:
The return value is of type COMPLEX(*)
Example:
program test_cmplx
integer :: i = 42
real :: x = 3.14
complex :: z
z = cmplx(i, x)
print *, z, cmplx(x)
end program test_cmplx
 
I love you
 
Can't say I love you too. I hate giving answers that I googled in five minutes to the people who asked the question on the forum not even trying to read the manual first.
 
Yesterday was my first time ever writing something in f95. And besides, I didn't even know that manual existed until you pointed me to it. I googled the question and came up with a bunch of similar errors but no solution. So be nice.

Also, if it peeves you so much, let me remind you that you're perfectly at liberty to just ignore these requests for help.
 

Similar threads

Replies
4
Views
2K
Replies
8
Views
4K
Replies
5
Views
9K
Replies
20
Views
3K
Replies
3
Views
2K
Back
Top