complex.h
Go to the documentation of this file.
00001 
00018 #ifndef __COMPLEX_H
00019 #define __COMPLEX_H
00020 
00021 #include <cmath>
00022 #include <cstdio>
00023 
00024 double epsilon= 0.0000001;
00025 
00026 struct complex { double r; double i;};
00027 
00028 complex czero = {0,0};
00029 
00030 complex cmul(complex a,complex b)
00031 { 
00032         complex r;
00033         r.r=a.r*b.r-a.i*b.i;
00034         r.i=a.i*b.r+a.r*b.i;
00035         return r;
00036 }
00037 
00038 complex cadd(complex a,complex b)
00039 {
00040         complex r;
00041         r.r=a.r+b.r;
00042         r.i=a.i+b.i;
00043         return r;
00044 }
00045 
00046 complex csub(complex a,complex b)
00047 {
00048         complex r;
00049         r.r=a.r-b.r;
00050         r.i=a.i-b.i;
00051         return r;
00052 }
00053 
00054 complex r2c(double x)
00055 {
00056         complex r;
00057         r.r=x;
00058         r.i=0;
00059         return r;
00060 }
00061 
00062 bool isEqual(complex a,complex b)
00063 {
00064         return ((a.r==b.r)&&(a.i==b.i));
00065 }
00066 
00067 bool isEqualE(complex a,complex b)
00068 {
00069         return ((abs(a.r-b.r)<epsilon)&&(abs(a.i-b.i)<epsilon));
00070 }
00071 
00072 complex cpow(complex a,int b)
00073 {
00074         complex r;
00075     r=a;
00076         for (int i=1;i<b;i++) r=cmul(r,a);
00077         return r;
00078 }
00079 
00080 complex cdiv(complex a,complex b)
00081 {
00082         complex r;
00083         double B=b.r*b.r+b.i*b.i;
00084         r.r=(a.r*b.r+a.i*b.i)/B;
00085         r.i=(a.i*b.r-a.r*b.i)/B;
00086         return r;
00087 }
00088 
00089 complex cexp(complex a){
00090         complex r;
00091         double length,angle;
00092         length=exp(a.r);
00093         angle=a.i;
00094         r.r=cos(angle)*length;
00095         r.i=sin(angle)*length;
00096         return r;
00097 }
00098 
00099 complex cln(complex a){
00100         complex r;
00101         double length,angle;
00102         length=sqrt(a.r*a.r+a.i*a.i);
00103         angle=atan2(a.i,a.r);
00104         r.r=log(length);
00105         r.i=angle;
00106         return r;
00107 }
00108 
00109 complex cneg(complex a){
00110         complex r;
00111         r.r=-a.r;
00112         r.i=-a.i;
00113         return r;
00114 }
00115 
00116 complex cpos(complex a){
00117         return a;
00118 }
00119 
00120 complex csqrt(complex a)
00121 {
00122         complex r;
00123         double length,angle;
00124         length=sqrt(sqrt(a.r*a.r+a.i*a.i));
00125         angle=atan2(a.i,a.r)/2;
00126         r.r=cos(angle)*length;
00127         r.i=sin(angle)*length;
00128         return r;
00129 }
00130 
00131 void cprint(complex c)
00132 {
00133         printf("%f",c.r);
00134         if (c.i>0) printf("+%fi",c.i);
00135         if (c.i<0) printf("%fi",c.i);
00136         printf("\n");
00137 }
00138 
00139 bool isReal(complex x)
00140 {
00141                 return (abs(x.i)<=epsilon);
00142 }
00143 #endif


asr_flock_of_birds
Author(s): Bernhardt Andre, Engelmann Stephan, Giesler Björn, Heller Florian, Jäkel Rainer, Nguyen Trung, Pardowitz Michael, Weckesser Peter, Yi Xie, Zöllner Raoul
autogenerated on Sat Jun 8 2019 19:36:21