/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/utils/complex.c

Go to the documentation of this file.
00001 //---
00002 //
00003 // This file is part of sedflux.
00004 //
00005 // sedflux is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 2 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // sedflux is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with sedflux; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //
00019 //---
00020 
00021 #include <math.h>
00022 
00023 #include "complex.h"
00024 
00025 Complex c_add(Complex a, Complex b)
00026 {
00027         Complex c;
00028         c.r=a.r+b.r;
00029         c.i=a.i+b.i;
00030         return c;
00031 }
00032 
00033 Complex c_sub(Complex a, Complex b)
00034 {
00035         Complex c;
00036         c.r=a.r-b.r;
00037         c.i=a.i-b.i;
00038         return c;
00039 }
00040 
00041 
00042 Complex c_mul(Complex a, Complex b)
00043 {
00044         Complex c;
00045         c.r=a.r*b.r-a.i*b.i;
00046         c.i=a.i*b.r+a.r*b.i;
00047         return c;
00048 }
00049 
00050 Complex c_complex(double re, double im)
00051 {
00052         Complex c;
00053         c.r=re;
00054         c.i=im;
00055         return c;
00056 }
00057 
00058 Complex c_conj(Complex z)
00059 {
00060         Complex c;
00061         c.r=z.r;
00062         c.i = -z.i;
00063         return c;
00064 }
00065 
00066 Complex c_div(Complex a, Complex b)
00067 {
00068         Complex c;
00069         double r,den;
00070         if (fabs(b.r) >= fabs(b.i)) {
00071                 r=b.i/b.r;
00072                 den=b.r+r*b.i;
00073                 c.r=(a.r+r*a.i)/den;
00074                 c.i=(a.i-r*a.r)/den;
00075         } else {
00076                 r=b.r/b.i;
00077                 den=b.i+r*b.r;
00078                 c.r=(a.r*r+a.i)/den;
00079                 c.i=(a.i*r-a.r)/den;
00080         }
00081         return c;
00082 }
00083 
00084 double c_abs(Complex z)
00085 {
00086         double x,y,ans,temp;
00087         x=fabs(z.r);
00088         y=fabs(z.i);
00089         if (x == 0.0)
00090                 ans=y;
00091         else if (y == 0.0)
00092                 ans=x;
00093         else if (x > y) {
00094                 temp=y/x;
00095                 ans=x*sqrt(1.0+temp*temp);
00096         } else {
00097                 temp=x/y;
00098                 ans=y*sqrt(1.0+temp*temp);
00099         }
00100         return ans;
00101 }
00102 
00103 Complex c_sqrt(Complex z)
00104 {
00105         Complex c;
00106         double x,y,w,r;
00107         if ((z.r == 0.0) && (z.i == 0.0)) {
00108                 c.r=0.0;
00109                 c.i=0.0;
00110                 return c;
00111         } else {
00112                 x=fabs(z.r);
00113                 y=fabs(z.i);
00114                 if (x >= y) {
00115                         r=y/x;
00116                         w=sqrt(x)*sqrt(0.5*(1.0+sqrt(1.0+r*r)));
00117                 } else {
00118                         r=x/y;
00119                         w=sqrt(y)*sqrt(0.5*(r+sqrt(1.0+r*r)));
00120                 }
00121                 if (z.r >= 0.0) {
00122                         c.r=w;
00123                         c.i=z.i/(2.0*w);
00124                 } else {
00125                         c.i=(z.i >= 0) ? w : -w;
00126                         c.r=z.i/(2.0*c.i);
00127                 }
00128                 return c;
00129         }
00130 }
00131 
00132 Complex c_rcmul(double x, Complex a)
00133 {
00134         Complex c;
00135         c.r=x*a.r;
00136         c.i=x*a.i;
00137         return c;
00138 }
00139 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2