Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > Fortran 90 Programming with NR

Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2008, 06:34 AM
Feynman Feynman is offline
Registered User
 
Join Date: Jun 2007
Posts: 9
a complex function

Hi,
I wrote a function wich calculate the math function : exp(i(kx+\bar(k)\bar(x))) (\bar it is the conjugate)
But it give me only the real part of the ansewr
Code:
 
     program h
      complex ii
      ii=cmplx(0.,1.)
      print*,'M=',expp(II,ii)
      end

      function expp(k,x)
      complex k,x,ii,expp
      ii=cmplx(0.,1.)
      expp=cexp(-ii*k*x-ii*conjg(k)*conjg(x))
      return
      end
so where is the problrm?
thank you
Reply With Quote
  #2  
Old 08-11-2008, 08:55 AM
davekw7x davekw7x is offline
Registered User
 
Join Date: Jan 2008
Posts: 453
Quote:
Originally Posted by Feynman View Post
...so where is the problrm?
Here's how you can get to the bottom of things. Try something like the following:

Code:
      program h
      complex M

      complex ii
      ii=cmplx(0.,1.)
      M = expp(II,ii)
      print*,'M = ', M
      end

      function expp(k,x)
      complex k,x,ii,expp
      print *, '  In expp: k = ', k
      print *, '           x = ', x
      ii=cmplx(0.,1.)
      expp=cexp(-ii*k*x-ii*conjg(k)*conjg(x))
      print*,'expp returning ', expp
      return
      end
Get the idea? Make the program tell you what it is seeing.


Now, depending on your compiler you may find that an argument is not being handled correctly (GNU g77) or you may find that the calculations within the function are carried out with the proper values but the result is not passed back to the main program (GNU gfortran). Or, maybe something else with other compilers.

Here's the output I get when I compile with GNU gfortran:
Code:
   In expp: k =  (  0.000000    ,  1.000000    )
            x =  (  0.000000    ,  1.000000    )
 expp returning  (-0.4161468    , 0.9092974    )
 M =  (           NaN,  0.000000    )
How could this happen? Well, in the main program, the compiler didn't know what kind of variable "expp" was, so the assignment went horribly wrong.

Bottom line: In the main program, you must tell the compiler that the value from the function is complex:

Code:
      program h
      complex expp
.
.
.
Regards,

Dave
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:41 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.