Fortran complex numbers and do loops

Click For Summary
SUMMARY

In Fortran 90, it is confirmed that a DO loop can be used to calculate complex numbers, as demonstrated in the provided code snippet. However, using a complex number as a DO loop parameter is not advisable, and modifying the loop variable within the loop contradicts traditional Fortran practices. Instead, utilizing a while-loop is recommended for such operations. Additionally, the variable 'ydum' is declared as both REAL and COMPLEX, which raises questions about its intended use.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with complex number operations in Fortran
  • Knowledge of DO loops and their limitations in Fortran
  • Experience with alternative looping constructs, such as while-loops
NEXT STEPS
  • Explore Fortran 90 complex number functionalities and best practices
  • Learn about the implications of variable scope and type in Fortran
  • Investigate the use of while-loops in Fortran for iterative calculations
  • Review Fortran 90 tutorials focusing on loops and control structures
USEFUL FOR

This discussion is beneficial for Fortran developers, computational scientists, and programmers working with numerical methods involving complex numbers and iterative processes.

freja
Messages
7
Reaction score
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
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K