Search code examples
ccomplex-numbersgsl

GSL Polar Complex Numbers Appear to be extremely different


Using minunit.h to test built-in gsl structs.

I have written the following test:

static
char * test_gsl_polar_complex_number_struct()
{
  double r = 0.325784;
  double theta = 0.421329;

  gsl_complex test_polr_complex_number = gsl_complex_polar ( r, theta ); 

  printf("expected r: %f, actual r: %f\n", r, GSL_REAL(test_polr_complex_number));
  mu_assert("real part of polar complex number does not match expected", 
  GSL_REAL(test_polr_complex_number) == r);

  return 0;
}

I am getting a failing test with the following output:

expected r: 0.325784, actual r: 0.297293
expected theta: 0.421329, actual theta: 0.133237
real part of polar complex number does not match expected

It is of note that the exact same test executes without errors on the rectangular complex struct.


Solution

  • You have the wrong expectations. The function gsl_complex_polar() initializes a complex number from components given in polar complex form:

    This function returns the complex number z = r \exp(i \theta) = r (\cos(\theta) + i \sin(\theta)) from the polar representation (r,theta)

    (GSL documentation)

    That's fine, but the GSL_REAL() macro returns the real part of a complex number. That's not the same thing as the r component of the polar representation. In fact, the docs I already quoted tell you exactly what it is: r cos(theta).