[Users] Example: Using complex numbers in C

Erik Schnetter schnetter at cct.lsu.edu
Tue Mar 12 12:46:29 CDT 2013


[I am posting this example in response to a request I received in private.]

This is how you can use complex numbers from C. This code assumes that you
have declared CCTK_COMPLEX grid functions u, v, w.

(I didn't actually try to compile this example, but the overall structure
should be fine.)

#include <cctk.h>
#include <cctk_Arguments.h>

#include <complex.h>
#include <math.h>

void do_some_complex_calculations(CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;

#pragma omp parallel for
  for (int k=0; k<cctk_lsh[2]; ++k) {
  for (int j=0; j<cctk_lsh[1]; ++j) {
  for (int i=0; i<cctk_lsh[0]; ++i) {
    const int idx = CCTK_GFINDEX3D(cctkGH, i,j,k);

    CCTK_COMPLEX tmp = 2.0 + 3.0 * I;
    u[idx] = conj(tmp) * v[idx] + 2.0 * cabs(w[idx]);
    u[idx] = cexp(u[idx]) - creal(tmp) + cimag(tmp);

  }
  }
  }
}

-erik

-- 
Erik Schnetter <schnetter at cct.lsu.edu>
http://www.perimeterinstitute.ca/personal/eschnetter/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.einsteintoolkit.org/pipermail/users/attachments/20130312/dc27d431/attachment.html 


More information about the Users mailing list