You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB
C

#include <math.h>
typedef struct _mycomplex {
float re;
float im;
} Complex;
inline Complex complex_new(float re, float im) __attribute__ ((always_inline));
inline Complex complex_add(Complex c1, Complex c2) __attribute__ ((always_inline));
inline Complex complex_mult(Complex c1, Complex c2) __attribute__ ((always_inline));
inline double complex_abs(Complex c) __attribute__ ((always_inline));
inline Complex complex_new(float re, float im)
{
Complex c;
c.re = re;
c.im = im;
return c;
}
inline Complex complex_mult(Complex c1, Complex c2)
{
Complex ret;
ret.re = c1.re * c2.re - c1.im * c2.im;
ret.im = c1.re * c2.im + c1.im * c2.re;
return ret;
}
inline Complex complex_add(Complex c1, Complex c2)
{
c1.re += c2.re;
c1.im += c2.im;
return c1;
}
inline Complex complex_conj(Complex c) {
c.im = -c.im;
return c;
}
inline double complex_abs(Complex c)
{
double re, im;
re = c.re;
im = c.im;
return sqrt(re * re + im * im);
}
inline void complex_print(Complex c) {
printf("%.3f + i%.3f", c.re, c.im);
}