Numerical Recipes Forum a complex function
 Register FAQ Calendar Search Today's Posts Mark Forums Read

#1
08-11-2008, 06:34 AM
 Feynman 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
#2
08-11-2008, 08:55 AM
 davekw7x Registered User Join Date: Jan 2008 Posts: 453
Quote:
 Originally Posted by Feynman ...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

 Thread Tools Display Modes Hybrid Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Numerical Recipes Official Announcements     About the Numerical Recipes Forum     General Information on Numerical Recipes     Official Bug Reports (NR3, the Third Edition)     Old Bug Reports (Obsolete Editions of NR) Numerical Recipes Third Edition Forum     General Hints, Tips, and Tricks for Using NR3     General Problems in Using NR3     Rollover and Empanel     Plugin Problems     Methods: All Chapters in NR3     NR3 in Java     Using NR3 with MATLAB     Using NR3 with Python Obsolete Editions Forum     General Hints, Tips, and Tricks for Using NR     General Problems in Using NR     C++ Programming with NR     Fortran 90 Programming with NR     Alternatives to Numerical Recipes     NR in Other Computer Languages     General Computing and Open Discussions     Methods: Chapters 2, 11, and 18     Methods: Chapters 3, 4, 5, and 6     Methods: Chapters 7, 8, and 20     Methods: Chapters 9 and 10     Methods: Chapters 12 and 13     Methods: Chapters 14 and 15     Methods: Chapters 16 and 17     Methods: Chapter 19

All times are GMT -5. The time now is 11:59 PM.

 Numerical Recipes Software - Archive - Top