Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute value squared of a complex number in Fortran

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
like image 664
norio Avatar asked Nov 30 '25 14:11

norio


1 Answers

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.

like image 114
High Performance Mark Avatar answered Dec 02 '25 04:12

High Performance Mark