Fortran 95 & complex numbers question

In summary, the user is trying to set the values in a complex array using a 'do' loop, but gets an error message when trying to compile. The user is new to f95 and is struggling to figure out what the real problem might be. The user has tried the manual and found that it wasn't helpful.
  • #1
Sdakouls
8
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
  • #2
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)
 
  • #3
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
 
  • #4
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
 
  • #5
I love you
 
  • #6
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.
 
  • #7
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.
 

1. What is Fortran 95?

Fortran 95 is a computer programming language used for scientific and numerical calculations. It is an updated version of the Fortran programming language and was released in 1995.

2. What are complex numbers in Fortran 95?

Complex numbers in Fortran 95 are a data type used to represent numbers with both a real and imaginary component. They are denoted by the suffix "i" and can be used in mathematical calculations.

3. How are complex numbers declared in Fortran 95?

Complex numbers can be declared in Fortran 95 using the "COMPLEX" keyword, followed by the desired variable name and an optional specification of the precision of the real and imaginary components.

4. What operations can be performed on complex numbers in Fortran 95?

In Fortran 95, complex numbers can be added, subtracted, multiplied, and divided using the standard arithmetic operators. They can also be compared using logical operators and manipulated using built-in functions such as "CONJG" and "ABS".

5. How are complex numbers stored in memory in Fortran 95?

In Fortran 95, complex numbers are stored in memory as two consecutive real numbers, with the real component first followed by the imaginary component. This allows for efficient storage and manipulation of complex numbers in the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
609
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
1
Views
749
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
Replies
8
Views
2K
Back
Top