Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

Retrieving imaginary part of a complex number in a portable way

OP1
New Contributor III
1,263 Views

For years I have used DIMAG to extract the imaginary part of a double precision complex number and store it into a double precision real.

I am trying nowtocode with keeping portability in mind (using parameterized real kinds, for instance).

For the real part, one can write the following:

INTEGER,PARAMETER :: DP = SELECTED_REAL_KIND(15,100) ! (This definition can go into a module...)
REAL(DP) R
COMPLEX(DP) C
R = REAL(C,KIND=DP)

But what about the same kind of construct for the imaginary part? There is no IMAG generic function that can be used with the same syntax, only type specific functions such as DIMAG and so on...

Is there another way to do this, without resorting to type specific functions such as AIMAG, DIMAG ... a solution which defeats the portability purpose?

Thanks!!

0 Kudos
2 Replies
Steven_L_Intel1
Employee
1,263 Views

AIMAG is the generic name.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,263 Views


Consider creating a GENERIC INTERFACE which returns the imaginary part of the complex number in the same precision of the complex number

FUNCTION IMAGINARY_SP(C)
REAL(SP) :: IMAGINARY_SP
COMPLEX(SP) :: C
IMAGINARY_SP = REAL(C,KIND=SP)
END FUNCTION IMAGINARY_SP


FUNCTION IMAGINARY_DP(C)
REAL(DP) :: IMAGINARY_DP
COMPLEX(DP) :: C
IMAGINARY_DP = REAL(C,KIND=DP)
END FUNCTION IMAGINARY_DP

...


INTERFACE IMAGINARY
FUNCTION IMAGINARY_SP(C)
REAL(SP) :: IMAGINARY_SP
COMPLEX(SP) :: C
END FUNCTION IMAGINARY_SP
FUNCTION IMAGINARY_DP(C)
REAL(DP) :: IMAGINARY_DP
COMPLEX(DP) :: C
END FUNCTION IMAGINARY_DP
END INTERFACE IMAGINARY

Jim Dempsey

0 Kudos
Reply