[Fortran] Quick question about complex exponentials

In summary: I haven't used them in a while.In summary, code that sets a complex variable as ##e^{i\alpha_1}## needs a right parenthesis.
  • #1
Matterwave
Science Advisor
Gold Member
3,971
328
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
  • #2
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))
 
  • #3
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?
 
  • #4
Yeap
 
  • #5
Not even functions, even if they just variables... Without cmplx they need to be literal contacts
 
  • #6
Contacts->constants
 
  • #7
Ah, thanks! What if X and Y are parameters? Would the simpler one work then?
 
  • #8
Don't think so...anyway, you can do your own testing now that you know what's going on.
 
  • Like
Likes jim mcnamara
  • #9
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.
 

1. What is a complex exponential in Fortran?

A complex exponential in Fortran is a mathematical function that involves raising a complex number to a power. It can be written in the form z = a + bi, where a and b are real numbers and i is the imaginary unit. This function is commonly used in scientific and engineering applications to represent sinusoidal functions and other complex phenomena.

2. How do I write a complex exponential in Fortran?

To write a complex exponential in Fortran, you can use the EXP function. The syntax is EXP(z), where z is a complex number. This will return the value of e to the power of z, where e is the base of the natural logarithm.

3. Can I use complex exponentials in Fortran to solve differential equations?

Yes, complex exponentials can be used in Fortran to solve differential equations, particularly in the field of signal processing. They are also useful for solving problems involving oscillations and resonance.

4. Are there any special considerations when working with complex exponentials in Fortran?

Yes, when working with complex exponentials in Fortran, it is important to be aware of the precision of the data type being used. Complex numbers in Fortran can be represented using single precision (REAL) or double precision (DOUBLE PRECISION). The choice of data type can affect the accuracy of the results.

5. Can I plot complex exponentials in Fortran?

Yes, it is possible to plot complex exponentials in Fortran using a variety of plotting libraries, such as gnuplot or matplotlib. However, it is important to note that the resulting plot will be a complex plane, as opposed to a traditional x-y plot. This can be useful for visualizing complex numbers and their relationships.

Similar threads

  • Programming and Computer Science
Replies
4
Views
614
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
5
Views
8K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
17
Views
4K
Back
Top