a complex function

Feynman
08-11-2008, 06:34 AM
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

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

davekw7x
08-11-2008, 08:55 AM
...so where is the problrm?

Here's how you can get to the bottom of things. Try something like the following:

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:

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:

program h
complex expp
.
.
.

Regards,

Dave