![]() |
|
#1
|
|||
|
|||
|
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
thank you |
|
#2
|
|||
|
|||
|
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
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 )
Bottom line: In the main program, you must tell the compiler that the value from the function is complex: Code:
program h
complex expp
.
.
.
Dave |
![]() |
| Thread Tools | |
| Display Modes | |
|
|