Fortran complex numbers and do loops

In summary, you can use a do loop to calculate a complex number in fortran 90. However, it is not recommended to use a complex number as a do loop parameter, and it is more appropriate to use a while-loop for this purpose. Additionally, the code example provided may not be completely accurate as the variable ydum appears in both REAL and COMPLEX declarations.
  • #1
freja
7
0
I have a very simple question, but have been unable to find an answer for it. When using fortran 90 can you use a DO loop to calculate a complex number? For example:

Code:
COMPLEX FUNCTION forwards (tincdum,tstartdum,tenddum,fdum,ydum,idum,wdum&
                                      &,lamdadum)
  IMPLICIT NONE

  REAL, INTENT(IN):: tincdum,tstartdum,tenddum,wdum,lamdadum,ydum,idum
  COMPLEX, INTENT(IN):: ydum,idum
  COMPLEX:: fn

  DO ydum = tstartdum,tenddum,tincdum
   fn = fofy(idum,wdum,lamdadum,ydum,fn)
   ydum = ydum + tincdum*fn
  END DO

  forwards = ydum
 END FUNCTION forwards
[code/]

Thank you
 
Technology news on Phys.org
  • #2
You can use a do loop to calculate a complex number. I am not sure if you can use a complex number for a do loop.


Code:
DO ydum = tstartdum,tenddum,tincdum
fn = fofy(idum,wdum,lamdadum,ydum,fn)
ydum = ydum + tincdum*fn
END DO

In the above example, I doubt if ydum can be used as a do loop parameter, nor would it be desirable (under traditional fortran rules) to modify the parameter inside the loop.
On the other hand, it is perfectly legitimate to do what you want to do using a while-loop.
See
http://www.tat.physik.uni-tuebingen.de/~kley/lehre/ftn77/tutorial/loops.html

As a side note, ydum appears in both REAL and COMPLEX declarations. I am not sure if this was intended.
 
  • #3
for your question. Yes, you can use a DO loop to calculate a complex number in Fortran 90. In the code provided, the DO loop iterates over the variable ydum, which is declared as a COMPLEX type. This means that ydum can hold both real and imaginary values, allowing for the calculation of a complex number. The function forwards also returns a COMPLEX type, indicating that the calculated value will also be a complex number. Therefore, the DO loop in this code is a valid way to calculate a complex number in Fortran 90.
 

1. What are complex numbers in Fortran?

Complex numbers in Fortran are data types that consist of two floating-point numbers, one for the real part and one for the imaginary part. They are denoted by the format (a,b) where a is the real part and b is the imaginary part.

2. How do I declare and initialize complex numbers in Fortran?

To declare and initialize a complex number in Fortran, you can use the complex keyword followed by the real and imaginary parts in parentheses. For example, complex :: z = (1.0, 2.5) would declare a complex number z with a real part of 1.0 and an imaginary part of 2.5.

3. What is the purpose of the DO loop in Fortran?

The DO loop in Fortran is a control structure that allows you to repeat a set of statements multiple times. It is commonly used for performing repetitive calculations and processing large amounts of data.

4. How do I use complex numbers in a DO loop in Fortran?

To use complex numbers in a DO loop in Fortran, you can declare a complex variable and use it as the loop variable. For example, do i = 1, 5 would loop through the values of i from 1 to 5, and you can use a complex variable as the index, such as do z = (1.0, 2.5), (5.0, 3.2).

5. Can I perform arithmetic operations on complex numbers in Fortran?

Yes, Fortran supports a wide range of arithmetic operations on complex numbers, including addition, subtraction, multiplication, and division. These operations follow the same rules as for real numbers, with the addition of the imaginary component. You can also use built-in functions, such as abs() and conjg(), to perform calculations on complex numbers.

Similar threads

  • Atomic and Condensed Matter
Replies
3
Views
868
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
4
Views
615
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top