Search code examples
arraysfortrancomplex-numbersintel-fortran

Apply conjugation to real array (Fortran)


In Fortran, I have an 1D array of type real, real :: work(2*N), which represents N complex numbers. I don't have any impact of the declaration of the array.

Later I need to apply a complex conjugation on work. However, conjg(work(:)) does not work since it is of type real.

Is there a efficient way to convince the compiler to apply the conjg to my array?


Solution

  • The easiest approach is already in the comment by HighPerformanceMark, just multiply the elements representing the imaginary part by -1.

    You can also use equivalence between a real array and a complex array. It will be just one array but viewed as both real and complex. Maybe not strictly standard conforming (not sure) but working as long as N is constant.

    The equivalence is used as:

    real :: work(2*N)
    complex :: cwork(N)
    
    !both work and cwork point to the same data
    equivalence (work, cwork)
    
    work = some_initial_value
    
    !this conjugates work at the same time as cwork because they are just different names for the same array
    cwork = conjg(cwork)