#include #include #include #include "complex.h" struct complex { double re; double im; }; complex complex_new(double re, double im) { complex z = malloc(sizeof(struct complex)); z->re = re; z->im = im; return z; } complex inject(double x) { return complex_new(x,0.0); } complex cclone(complex z) { return complex_new(z->re,z->im); } void cdelete(complex z) { free(z); } double real_part(complex z) { return z->re; } double imag_part(complex z) { return z->im; } complex cadd(complex z1,complex z2) { return complex_new(z1->re + z2->re, z1->im + z2->im); } complex cmul(complex z1,complex z2) { return complex_new(z1->re * z2->re - z1->im * z2->im, z1->re * z2->im + z1->im * z2->re); } double cmodule(complex z) { return sqrt(z-> re * z -> re + z-> im * z -> im); } static void zprint(complex z) { fprintf(stderr,"(%f +i*%f)\n",z->re,z->im); }