//Functions used by program ucomplex.cpp #include #include "complex1.h" //y=x*x double SQR(double x) { return x*x; } //rectangular to polar conversion void RectPol(COMPLEX *n) { double PI=3.1415926535; n->r=sqrt(n->x*n->x+n->y*n->y); if (n->x==0) if (n->y>0) n->t=PI/2; else if (n->y<0) n->t=-PI/2; else n->t=0; else { n->t=atan(n->y/n->x); if (n->x<0) if (n->y>=0) n->t+=PI; else n->t-=PI; } } //polar to rectangular conversion void PolRect(COMPLEX *n) { double PI=3.1415926535; if (n->t>PI) n->t-=2*PI; if (n->t<-PI) n->t+=2*PI; n->x=n->r*cos(n->t); n->y=n->r*sin(n->t); } //add two complex numbers bool ZSum(COMPLEX z1, COMPLEX z2, COMPLEX *z) { z->x=z1.x+z2.x; z->y=z1.y+z2.y; RectPol(z); return TRUE; } //subtract two complex numbers bool ZMinus(COMPLEX z1, COMPLEX z2, COMPLEX *z) { z->x=z1.x-z2.x; z->y=z1.y-z2.y; RectPol(z); return TRUE; } //multiply two complex numbers bool ZMult(COMPLEX z1, COMPLEX z2, COMPLEX *z) { z->r=z1.r*z2.r; z->t=z1.t+z2.t; PolRect(z); return TRUE; } //divide two complex numbers bool ZDiv(COMPLEX z1, COMPLEX z2, COMPLEX *z) { if (z2.r==0) return FALSE; else { z->r=z1.r/z2.r; z->t=z1.t-z2.t; PolRect(z); return TRUE; } } //compute exp(z) bool Zexp(COMPLEX z1, COMPLEX *z) { z->x=exp(z1.x)*cos(z1.y); z->y=exp(z1.x)*sin(z1.y); RectPol(z); return TRUE; } //compute Ln(z) bool ZLn(COMPLEX z1, COMPLEX *z) { if (z1.r<=0) return FALSE; else { z->x=log(z1.r); z->y=z1.t; RectPol(z); return TRUE; } } //compute Z^2 bool ZSqr(COMPLEX z1, COMPLEX *z) { z->x=SQR(z1.x)-SQR(z1.y); z->y=2*z1.x*z1.y; RectPol(z); return TRUE; } //compute SQRT(Z) bool ZSqrt(COMPLEX z1, COMPLEX *z) { z->x=sqrt(z1.r)*cos(z1.t/2); z->y=sqrt(z1.r)*sin(z1.t/2); RectPol(z); return TRUE; } //compute 1/Z bool ZInv(COMPLEX z1, COMPLEX *z) { if (z1.r==0) return FALSE; else { z->r=1 / z1.r; z->t= -z1.t; PolRect(z); return TRUE; } } //compute Z1^Z2 bool ZPower(COMPLEX z1, COMPLEX z2, COMPLEX *z) { COMPLEX z3; bool result; result=FALSE; if (ZLn(z1,&z3)) if (ZMult(z3,z2,&z3)) if (Zexp(z3,z)) result=TRUE; return result; } //compute Sh(Z) bool ZSh(COMPLEX z1, COMPLEX *z) { z->x=(exp(z1.x)*cos(z1.y) - exp(-z1.x)*cos(-z1.y))/2; z->y=(exp(z1.x)*sin(z1.y) - exp(-z1.x)*sin(-z1.y))/2; RectPol(z); return TRUE; } //compute Ch(Z) bool ZCh(COMPLEX z1, COMPLEX *z) { z->x=(exp(z1.x)*cos(z1.y) + exp(-z1.x)*cos(-z1.y))/2; z->y=(exp(z1.x)*sin(z1.y) + exp(-z1.x)*sin(-z1.y))/2; RectPol(z); return TRUE; } //compute sin(Z) bool ZSin(COMPLEX z1, COMPLEX *z) { z->x= (exp(-z1.y)*sin(z1.x) - exp(z1.y)*sin(-z1.x))/2; z->y=-(exp(-z1.y)*cos(z1.x) - exp(z1.y)*cos(-z1.x))/2; RectPol(z); return TRUE; } //compute cos(Z) bool ZCos(COMPLEX z1, COMPLEX *z) { z->x=(exp(-z1.y)*cos(z1.x) + exp(z1.y)*cos(-z1.x))/2; z->y=(exp(-z1.y)*sin(z1.x) + exp(z1.y)*sin(-z1.x))/2; RectPol(z); return TRUE; } //compute Tan(Z) bool ZTan(COMPLEX z1, COMPLEX *z) { COMPLEX z3,z4; bool result; result=FALSE; if (ZSin(z1,&z3)) if (ZCos(z1,&z4)) if (ZDiv(z3,z4,z)) result=TRUE; else result=FALSE; return result; } //compute Th(Z) bool ZTh(COMPLEX z1, COMPLEX *z) { COMPLEX z3,z4; bool result; result=FALSE; if (ZSh(z1,&z3)) if (ZCh(z1,&z4)) if (ZDiv(z3,z4,z)) result=TRUE; else result=FALSE; return result; } bool ZArcsin(COMPLEX z1, COMPLEX *z) { COMPLEX z3; z3.x=1-SQR(z1.x)+SQR(z1.y); z3.y=-2*z1.x*z1.y; RectPol(&z3); if (!ZSqrt(z3,&z3)) return FALSE; z3.x=z3.x-z1.y; z3.y=z3.y+z1.x; RectPol(&z3); if (!ZLn(z3,&z3)) return FALSE; z->x=z3.y; z->y=-z3.x; RectPol(z); return TRUE; } bool ZArccos(COMPLEX z1, COMPLEX *z) { COMPLEX z3; z3.x=1-SQR(z1.x)+SQR(z1.y); z3.y=-2*z1.x*z1.y; RectPol(&z3); if (!ZSqrt(z3,&z3)) return FALSE; z3.x=z1.x-z3.y; z3.y=z1.y+z3.x; RectPol(&z3); if (!ZLn(z3,&z3)) return FALSE; z->x=z3.y; z->y=-z3.x; RectPol(z); return TRUE; } bool ZArctan(COMPLEX z1, COMPLEX *z) { COMPLEX z2,z3; z2.x=-z1.x; z2.y=1-z1.y; z3.x=z1.x; z3.y=1+z1.y; RectPol(&z2); RectPol(&z3); if (!ZDiv(z2,z3,&z3)) return FALSE; if (!ZLn(z3,&z3)) return FALSE; z->x=z3.y/2; z->y=-z3.x/2; RectPol(z); return TRUE; } bool ZArgsh(COMPLEX z1, COMPLEX *z) { COMPLEX z3; z3.x=1+SQR(z1.x)-SQR(z1.y); z3.y=2*z1.x*z1.y; RectPol(&z3); if (!ZSqrt(z3,&z3)) return FALSE; z3.x=z3.x+z1.x; z3.y=z3.y+z1.y; RectPol(&z3); if (!ZLn(z3,z)) return FALSE; return TRUE; } bool ZArgch(COMPLEX z1, COMPLEX *z) { COMPLEX z3; z3.x=-1+SQR(z1.x)-SQR(z1.y); z3.y=2*z1.x*z1.y; RectPol(&z3); if (!ZSqrt(z3,&z3)) return FALSE; z3.x=z3.x+z1.x; z3.y=z3.y+z1.y; RectPol(&z3); if (!ZLn(z3,z)) return FALSE; return TRUE; } bool ZArgth(COMPLEX z1, COMPLEX *z) { COMPLEX z2,z3; z2.x=1+z1.x; z2.y=z1.y; z3.x=1-z1.x; z3.y=-z1.y; RectPol(&z2); RectPol(&z3); if (!ZDiv(z2,z3,&z3)) return FALSE; if (!ZLn(z3,&z3)) return FALSE; z->x=z3.x/2; z->y=z3.y/2; RectPol(z); return TRUE; } //end of file complex1.cpp