Fortran Fortran complex numbers and do loops

AI Thread Summary
In Fortran 90, a DO loop can be used to calculate a complex number, but using a complex number as a loop variable is not advisable. The example provided demonstrates a DO loop iterating over a real variable, while the complex variable `ydum` is modified within the loop, which is not recommended according to traditional Fortran practices. Instead, using a WHILE loop could be a better approach for such calculations. Additionally, there is a concern regarding the dual declaration of `ydum` as both REAL and COMPLEX, which may lead to confusion or errors in the code.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
4
Views
2K
Replies
8
Views
4K
Replies
13
Views
3K
Replies
4
Views
2K
Replies
8
Views
2K
Back
Top