Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] Quick question about complex exponentials

  1. Jun 11, 2015 #1

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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 (Text):

      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?
     
  2. jcsd
  3. Jun 11, 2015 #2
    Only literal constants can be assigned that way:
    Code (Text):

    cnumber = (1.2, 2.3)
     
    When using variables, as you are doing, then you need:
    Code (Text):

    hmajphasemix(2,2) = cmplx(cos(alpha1),sin(alpha1))
     
     
  4. Jun 11, 2015 #3

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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?
     
  5. Jun 11, 2015 #4
    Yeap
     
  6. Jun 11, 2015 #5
    Not even functions, even if they just variables... Without cmplx they need to be literal contacts
     
  7. Jun 11, 2015 #6
    Contacts->constants
     
  8. Jun 11, 2015 #7

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Ah, thanks! What if X and Y are parameters? Would the simpler one work then?
     
  9. Jun 12, 2015 #8
    Don't think so....anyway, you can do your own testing now that you know what's going on.
     
  10. Jun 12, 2015 #9

    Mark44

    Staff: Mentor

    This^
     
  11. Jun 12, 2015 #10

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Alrighty. Thanks again. :)
     
  12. Jun 17, 2015 #11
    One question: why would you ever still program in fortran lol?
     
  13. Jun 17, 2015 #12

    Mark44

    Staff: Mentor

    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.
     
  14. Jun 17, 2015 #13

    meBigGuy

    User Avatar
    Gold Member

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] Quick question about complex exponentials
Loading...