Skip to content

Instantly share code, notes, and snippets.

@edubart
Created November 24, 2010 19:11
Show Gist options
  • Save edubart/714204 to your computer and use it in GitHub Desktop.
Save edubart/714204 to your computer and use it in GitHub Desktop.
#include "complex.h"
#include <math.h>
typedef struct COMPLEX {
float r, i;
} complex;
void comPrint(complex a);
complex comMul(complex a, complex b);
complex comInv(complex a);
complex comDiv(complex a, complex b);
complex comScale(complex a, float b);
complex comConjugate(complex a);
float comNorm(complex a);
void comSqrt(complex a, complex *b, complex *c);
complex comMul(complex a, complex b) {
complex c;
c.r = (a.r * b.r) - (a.i * b.i);
c.i = (a.i * b.r) + (a.r * b.i);
return c;
}
complex comInv(complex a) {
complex c;
c.r = a.r/(a.r*a.r + a.i*a.i);
c.i = a.i/(a.r*a.r + a.i*a.i);
return c;
}
complex comDiv(complex a, complex b) {
return comMul(a, comInv(b));
}
complex comScale(complex a, float b) {
complex c;
c.r = a.r * b;
c.i = a.i * b;
return c;
}
complex comConjugate(complex a) {
complex c;
c.r = a.r;
c.i = -a.i;
return c;
}
float comNorm(complex a) {
return sqrt((a.r*a.r) + (a.i*a.i));
}
void comSqrt(complex a, complex *b, complex *c) {
float norm = comNorm(a);
(*b).r = sqrt((norm + a.r)/2.0f);
(*b).i = sqrt((norm - a.r)/2.0f);
(*c).r = sqrt((norm + a.r)/2.0f);
(*c).i =-sqrt((norm - a.r)/2.0f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment