Fortran 95 & complex numbers question

Click For Summary

Discussion Overview

The discussion revolves around a coding issue in Fortran 95 related to the use of complex numbers in an array. Participants are exploring the compilation error encountered when attempting to assign values to a complex array using expressions involving double precision arrays.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • The original poster describes a compilation error when trying to assign a complex number to an array element using an expression that includes a double precision array.
  • Some participants suggest using an auxiliary variable to simplify the assignment, but the original poster indicates that this approach did not resolve the issue.
  • One participant references the Fortran 95 manual, specifically the CMPLX function, as a potential solution for creating complex numbers from real components.
  • There is a mix of supportive and critical responses regarding the original poster's approach to seeking help, with some participants expressing frustration over the perceived lack of effort in consulting available resources.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the compilation error or the best approach to resolve it. There are competing views on whether the solution lies in using auxiliary variables or leveraging the CMPLX function.

Contextual Notes

Some participants express uncertainty about the original poster's understanding of Fortran 95 and the available documentation, which may affect the clarity of the discussion.

Who May Find This Useful

Individuals learning Fortran 95, particularly those working with complex numbers and array manipulations, may find this discussion relevant.

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 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K