[Fortran] Quick question about complex exponentials

  • Context: Fortran 
  • Thread starter Thread starter Matterwave
  • Start date Start date
  • Tags Tags
    Complex Fortran
Click For Summary

Discussion Overview

The discussion revolves around programming in Fortran, specifically regarding the correct way to assign complex variables in an array using complex exponentials. It touches on syntax issues, the use of functions versus constants, and the rationale for using Fortran in scientific computing.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant encounters a compiler error when trying to assign a complex variable using a tuple notation for functions, questioning the correct syntax.
  • Another participant clarifies that only literal constants can be assigned directly using tuple notation, while variables must use the cmplx function.
  • A participant confirms the distinction that even variables, not just functions, require the cmplx function for assignment.
  • There is a question about whether parameters would allow the simpler assignment method, which another participant doubts.
  • Discussion shifts to the reasons for continuing to use Fortran, highlighting its advantages in handling existing scientific code and parallel processing capabilities.
  • One participant reflects on their past experience with Fortran for complex number operations and compares it to MATLAB, suggesting potential alternatives in C/C++.

Areas of Agreement / Disagreement

Participants generally agree on the syntax requirements for assigning complex variables in Fortran, but there is uncertainty regarding the use of parameters in this context. The discussion also presents differing views on the continued relevance of Fortran in programming.

Contextual Notes

Limitations include the lack of clarity on the specific conditions under which the simpler assignment might work and the potential for varying interpretations of programming practices across different languages.

Who May Find This Useful

Readers interested in Fortran programming, complex number handling in scientific computing, or those comparing programming languages for engineering applications may find this discussion relevant.

Matterwave
Science Advisor
Homework Helper
Gold Member
Messages
3,971
Reaction score
329
Hey, so I just have a quick question. I am trying to set a complex variable (in an array) as ##e^{i\alpha_1}## and the line I used in my code looks like this:

Code:
  hmajphasemix(2,2)=(cos(alpha1),sin(alpha1))

But the compiler is telling me that it "expects a right parenthesis" at this line. I'm kind of confused cus I'm counting the right number of parenthesis. Is this the correct way to set this variable or have I messed something up?
 
Technology news on Phys.org
Only literal constants can be assigned that way:
Code:
cnumber = (1.2, 2.3)
When using variables, as you are doing, then you need:
Code:
hmajphasemix(2,2) = cmplx(cos(alpha1),sin(alpha1))
 
Ah, so I can only use (X,Y) when X and Y are just numbers, but if X and Y are some functions of some variable then I need to use cmplx(X,Y) is that right?
 
Yeap
 
Not even functions, even if they just variables... Without cmplx they need to be literal contacts
 
Contacts->constants
 
Ah, thanks! What if X and Y are parameters? Would the simpler one work then?
 
Don't think so...anyway, you can do your own testing now that you know what's going on.
 
  • Like
Likes   Reactions: jim mcnamara
gsal said:
anyway, you can do your own testing now that you know what's going on.
This^
 
  • #10
Alrighty. Thanks again. :)
 
  • #11
One question: why would you ever still program in fortran lol?
 
  • #12
serp777 said:
One question: why would you ever still program in fortran lol?
Apparently there are good reasons to do so. There is a ton of existing Fortran code for scientific and engineering applications. Also, Fortran code lends itself more easily to highly parallelized applications than do C and C++ programs, or so I understand. One of the drawbacks in C/C++ code is that data can be accessed indirectly (via pointers), which can be problematic where a block of data has to be accessed by multiple threads. The relatively new restrict keyword in C (C99) is an effort to restrict the access to data by only a single pointer.
 
  • #13
The last time I programmed in FORTRAN (an FM multipath simulation) it was because of the ease of working with complex numbers.
(and I happened to have a graphing package)
I'd probably do it in MATLAB now and it would take forever to run.

Maybe there are some good complex math packages for C or C++ now.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K