Is there an intrinsic function in Fortran to compute the square modulus |z|^2 of a complex number z?
If not, is there a simpler or better way to compute it than the following?
REAL(z, precision_specifier)**2 + AIMAG(z)**2
No, the Fortran standard defines no such intrinsic routine. Nor, as far as I am aware, does any of the current crop of widely-used compilers provide such a routine.
If OP really wants to avoid an expensive sqrt, and doesn't like her existing solution, OP could try:
real :: rslt
real, dimension(2) :: parts
complex :: z
...
parts = transfer(z, parts)
rslt = dot_product(parts, parts)
or, given the same declarations, this might be preferred
rslt = dot_product(transfer(z, parts), transfer(z, parts))
As always, if performance matters to you, measure it.
Simpler or better ? You decide.
As always, transfer is not to be operated by persons under the age of 18, or persons under the influence of alcohol or other performance-impairing drugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With