home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************/
- /* */
- /* COMPLEX.C */
- /* INCLUDE-Datei zum Rechnen mit komplexen Zahlen. */
- /* */
- /* Sprache: Turbo C 2.0 */
- /* */
- /* (C) 1991 Ralf Baethke-Franke & toolbox */
- /* */
- /**********************************************************/
-
-
- /**********************************************************/
- /* */
- /* Die Funktionen und ihre Berechnung: */
- /* ----------------------------------- */
- /* */
- /* Es gilt grundsätzlich: z = x + iy */
- /* mit x = z.re und y = z.im */
- /* Absolutbetrag: r = abs_c(z) */
- /* r = |z| = √(x² + y²) */
- /* Argument: w = atan(y/x) */
- /* */
- /* 1. Grundrechenarten */
- /* 1.1. Addition z = add_c(z1,z2) */
- /* z1 + z2 = (x1 + x2) + i(y1 + y2) */
- /* 1.2. Subtraktion z = sub_c(z1,z2) */
- /* z1 - z2 = (x1 - x2) - i(y1 - y2) */
- /* 1.3. Multiplikation z = mul_c(z1,z2) */
- /* z1 * z2 = x1x2 - y1y2 + i(x1y2 + y1x2) */
- /* 1.4. Division z = div_c(z1,z2) */
- /* z1 x1x2 + y1y2 x1y2 - y1x2 */
- /* -- = ----------- + i ----------- */
- /* z2 |z|² |z|² */
- /* */
- /* 2. Potenzen und Wurzeln */
- /* 2.1. Potenzen und Exponentialfunktion */
- /* 2.1.1. Quadrat z = sqr_c(z1) */
- /* z² = (x1² - y1²) + 2*i*x1y1 */
- /* 2.1.2. reelle Potenz z = pot_c(z1,n) */
- /* z1ⁿ = r1ⁿ (cos(nw1) + isin(nw1)) */
- /* 2.1.3. komplexe Potenz z = pot_cc(z1,z2) */
- /* z1^z2 = exp(z2*ln(z1)) */
- /* 2.1.4. Exponentialfkt. z = exp_c(z1) */
- /* exp(z1) = exp(x1)*(cos(y1) + isin(y1)) */
- /* 2.2. Wurzeln und natürl. Logarithmus */
- /* 2.2.1. Quadratwurzel z = sqrt_c(z1) */
- /* √z1 = ±√ ((r1 + x1)/2) ± i * √((r1 - x1)/2) */
- /* Es wird nur die erste der 4 mögl. Wurzeln berechnet */
- /* 2.2.2. reelle Wurzel z = rad_c(z1,n) */
- /* ⁿ√z1 = z1^(1/n) */
- /* 2.2.3. komplexe Wurzel z = rad_cc(z1,z2) */
- /* (z2)√z1 = z1^(1/z2) */
- /* 2.2.4. Logarithmus z = ln_c(z1) */
- /* ln(z1) = ln(r1) + i*w1 */
- /* Es wird der Hauptzweig des nat. Logarithmus berechnet */
- /* */
- /* 3. Trigonometrische Funktionen */
- /* 3.1. Sinus z = sin_c(z1) */
- /* sin(z1) = sin(x1)cosh(y1) + i * cos(x1)sinh(y1) */
- /* 3.2. Cosinus z = cos_c(z1) */
- /* cos(z1) = cos(x1)cosh(y1) - i * sin(x1)sinh(y1) */
- /* 3.3. Tangens z = tan_c(z1) */
- /* tan(z1) = sin(z1)/cos(z1) */
- /* 3.4. Cotangens z = cot_c(z1) */
- /* cot(z1) = cos(z1)/sin(z1) */
- /* */
- /* 4. Trigonometrische Umkehrfunktionen */
- /* 4.1. Arcussinus z = asin_c(z1) */
- /* asin(z1) = -i*ln(i*z1 + √(1 - z²)) */
- /* 4.2. Arcuscosinus z = acos_c(z1) */
- /* acos(z1) = -i*ln(z1 + √(z1² -1)) */
- /* 4.3. Arcustangens z = atan_c(z1) */
- /* 1 1 + i*z1² */
- /* atan(z1) = --- * ln(---------) */
- /* 2*i 1 - i*z1² */
- /* 4.4. Arcuscotangens z = acot_c(z1) */
- /* -1 i*z1² + 1 */
- /* acot(z1) = --- * ln(---------) */
- /* 2*i i*z1² - 1 */
- /* */
- /* 5. Hyperbelfunktionen */
- /* 5.1. Hyperbelsinus z = sinh_c(z1) */
- /* sinh(z1) = sinh(x1)cos(y1) + i*cosh(x1)sin(y1) */
- /* 5.2. Hyperbelcosinus z = cosh_c(z1) */
- /* cosh(z1) = cosh(x1)cos(y1) + i*sinh(x1)sin(y1) */
- /* 5.3. Hyperbeltangens z = tanh_c(z1) */
- /* tanh(z1) = sinh(z1) / cosh(z1) */
- /* 5.4. Hyperbelcotangens z = coth_c(z1) */
- /* coth(z1) = cosh(z1) / sinh(z1) */
- /* */
- /* 6. Umkehrungen der Hyperbelfunktionen */
- /* 6.1. Arcus-Hyperbelsinus z = asinh_c(z1) */
- /* asinh(z1) = ln(z1 + √(z1² + 1)) */
- /* 6.2. Arcus-Hyperbelcosinus z = acosh_c(z1) */
- /* acosh(z1) = ln(z1 + √(z1² - 1)) */
- /* 6.3. Arcus-Hyperbeltangens z = atanh_c(z1) */
- /* 1 1 + z */
- /* atanh(z1) = - * ln(-----) */
- /* 2 1 - z */
- /* 6.4. Arcus-Hyperbelcotangens z = atanh_c(z1) */
- /* 1 z + 1 */
- /* atanh(z1) = - * ln(-----) */
- /* 2 z - 1 */
- /* */
- /**********************************************************/
-
- #include <math.h>
-
- #define PI M_PI
- #define PI_HALBE PI/2
- #define ZWEI_PI 2 * PI
- #define INF 9e307
-
- typedef struct
- {
- double re, im;
- } compl;
-
- /******************* Funktionsprototypen *****************/
-
- double arg_opt ( double arg );
- double sqr ( double x );
- int sgn ( double i );
- int sign ( double i );
- double abs_c ( compl z );
- double arg_c ( compl z );
- compl konj_kompl ( compl z );
- compl add_c ( compl z1, compl z2 );
- compl sub_c ( compl z1, compl z2 );
- compl mul_c ( compl z1, compl z2 );
- compl div_c ( compl z1, compl z2 );
- compl sqr_c ( compl z1 );
- compl sqrt_c ( compl z1 );
- compl pot_c ( compl z1, double p);
- compl rad_c ( compl z1, double r );
- compl pot_cc ( compl z1, compl z2 );
- compl rad_cc ( compl z1, compl z2 );
- compl exp_c ( compl z1 );
- compl ln_c ( compl z1 );
- compl sin_c ( compl z1 );
- compl cos_c(compl z1);
- compl tan_c ( compl z1 );
- compl cot_c ( compl z1 );
- compl asin_c ( compl z1 );
- compl acos_c ( compl z1 );
- compl atan_c ( compl z1 );
- compl acot_c ( compl z1 );
- compl sinh_c ( compl z1 );
- compl cosh_c ( compl z1 );
- compl tanh_c ( compl z1 );
- compl coth_c ( compl z1 );
- compl asinh_c ( compl z1 );
- compl acosh_c ( compl z1 );
- compl atanh_c ( compl z1 );
- compl acoth_c ( compl z1 );
-
- /********************* Hilfsfunktionen ********************/
-
- double arg_opt ( double arg )
- /* dient zur Vermeidung von zu großen / kleinen Argu- */
- /* menten bei Winkelfunktionen */
- {
- if ( arg > ZWEI_PI )
- arg -= floor ( arg / ZWEI_PI ) * ZWEI_PI;
- if ( arg < - ZWEI_PI )
- arg += floor ( arg / ZWEI_PI ) * ZWEI_PI;
- return ( arg );
- }
-
- double sqr ( double x ) /* Quadrat einer reellen Zahl */
- {
- return( x * x );
- }
-
- int sgn ( double i ) /* Vorzeichen einer reellen Zahl */
- { /* sgn(0) = +1 */
- int sgn;
- sgn = ( i < 0 ) ? -1 : 1;
- return ( sgn );
- }
-
- int sign ( double i ) /* Vorzeichen einer reellen Zahl */
- { /* sign(0) = 0 */
- int sign;
- sign = ( !i ) ? 0 : sgn ( i );
- return ( sign );
- }
-
- double abs_c ( compl z ) /* Betrag einer komplexen Zahl */
- {
- return ( hypot ( z.re, z.im ) );
- }
-
- double arg_c ( compl z ) /* Argument einer kompl. Zahl */
- {
- double arg;
- if ( !z.re ) arg = sign ( z.im ) * PI_HALBE;
- else arg = ( z.re > 0 ) ? atan ( z.im / z.re ) :
- atan ( z.im / z.re ) + sgn ( z.im ) * PI;
- return ( arg );
- }
-
- compl konj_kompl ( compl z ) /* konjugiert komplexe Zahl */
- { /* zu z -> z' */
- z.im = -z.im;
- return ( z );
- }
-
- /************** Grundrechenarten: +, -, *, / **************/
-
- compl add_c ( compl z1, compl z2 )
- {
- compl z;
- z.re = z1.re + z2.re;
- z.im = z1.im + z2.im;
- return ( z );
- }
-
- compl sub_c ( compl z1, compl z2 )
- {
- compl z;
- z.re = z1.re - z2.re;
- z.im = z1.im - z2.im;
- return ( z );
- }
-
- compl mul_c ( compl z1, compl z2 )
- {
- compl z;
- z.re = z1.re * z2.re - z1.im * z2.im;
- z.im = z1.re * z2.im + z1.im * z2.re;
- return ( z );
- }
-
- compl div_c ( compl z1, compl z2 )
- {
- compl z;
- if ( abs_c ( z2 ) == 0 )
- {
- z.re = INF * sign ( z2.re );
- z.im = -INF * sign ( z2.im );
- }
- else
- {
- z.re = ( z1.re * z2.re + z1.im * z2.im ) /
- sqr ( abs_c ( z2 ) );
- z.im = ( z1.im * z2.re - z1.re * z2.im ) /
- sqr ( abs_c ( z2 ) );
- }
- return ( z );
- }
-
- /***************** Potenzen und Wurzeln *******************/
-
- compl sqr_c ( compl z1 )
- {
- compl z;
- z.re = sqr ( z1.re ) - sqr ( z1.im );
- z.im = 2 * z1.re * z1.im;
- return ( z );
- }
-
- compl sqrt_c ( compl z1 )
- {
- compl z;
- z.re = sqrt ( ( abs_c ( z1 ) + z1.re ) / 2 );
- z.im = sqrt ( ( abs_c ( z1 ) - z1.re ) / 2 );
- return(z);
- }
-
- compl pot_c ( compl z1, double p)
- {
- compl z;
- double betr, arg;
- if ( p == 1 ) z = z1;
- else if ( p == -1 )
- {
- z.re = 1;
- z.im = 0;
- z = div_c ( z, z1 );
- }
- else
- {
- betr = pow ( abs_c ( z1 ), p );
- arg = arg_opt ( p * arg_c ( z1 ) );
- z.re = betr * cos ( arg );
- z.im = betr * sin ( arg );
- }
- return ( z );
- }
-
- compl rad_c ( compl z1, double r )
- {
- compl z;
- z = pot_c ( z1, 1 / r );
- return ( z );
- }
-
- compl pot_cc ( compl z1, compl z2 )
- {
- compl z;
- z = exp_c ( mul_c ( z2, ln_c ( z1 ) ) );
- return ( z );
- }
-
- compl rad_cc ( compl z1, compl z2 )
- {
- compl z;
- z.re = 1; z.im = 0;
- z = pot_cc ( z1, div_c ( z, z2 ) );
- return ( z );
- }
-
- compl exp_c ( compl z1 )
- {
- compl z;
- z1.im = arg_opt ( z1.im );
- z1.re = exp ( z1.re );
- z.re = z1.re * cos ( z1.im );
- z.im = z1.re * sin ( z1.im );
- return ( z );
- }
-
- compl ln_c ( compl z1 )
- {
- compl z;
- z.re = log ( abs_c ( z1 ) );
- z.im = arg_c ( z1 );
- return(z);
- }
-
- /************** Trigonometrische Funktionen ***************/
-
- compl sin_c ( compl z1 )
- {
- compl z;
- z1.re = arg_opt ( z1.re );
- z.re = sin ( z1.re ) * cosh ( z1.im );
- z.im = cos ( z1.re ) * sinh ( z1.im );
- return(z);
- }
-
- compl cos_c(compl z1)
- {
- compl z;
- z1.re = arg_opt ( z1.re );
- z.re = cos ( z1.re ) * cosh ( z1.im );
- z.im = -sin ( z1.re ) * sinh ( z1.im );
- return(z);
- }
-
- compl tan_c ( compl z1 )
- {
- compl z;
- z = div_c ( sin_c ( z1 ), cos_c ( z1 ) );
- return ( z );
- }
-
- compl cot_c ( compl z1 )
- {
- compl z;
- z = div_c ( cos_c ( z1 ), sin_c ( z1 ) );
- return ( z );
- }
-
- /****** Umkehrungen der trigonometrischen Funktionen ******/
-
- compl asin_c ( compl z1 )
- {
- compl z, c1, c2, c3;
- c1.re = 1; c1.im = 0;
- c2.re = 0; c2.im = 1;
- c3.re = 0; c3.im = -1;
- z = sqr_c ( z1 );
- z = sub_c ( c1, z);
- z = sqrt_c ( z );
- z = add_c ( mul_c ( c2, z1 ), z );
- z = ln_c ( z );
- z = mul_c ( c3, z );
- return ( z );
- }
-
- compl acos_c ( compl z1 )
- {
- compl z, c1, c2;
- c1.re = 1; c1.im = 0;
- c2.re = 0; c2.im = -1;
- z = sqr_c ( z1 );
- z = sub_c ( z, c1);
- z = sqrt_c ( z );
- z = add_c ( z1, z );
- z = ln_c ( z );
- z = mul_c ( c2, z );
- return ( z );
- }
-
- compl atan_c ( compl z1 )
- {
- compl z, c1, c2, c3, a1, a2;
- c1.re = 0; c1.im = 1;
- c2.re = 1; c2.im = 0;
- c3.re = 0; c3.im = 2;
- z = mul_c ( c1, z1 );
- a1 = add_c ( c2, z );
- a2 = sub_c ( c2, z );
- z = div_c ( a1, a2 );
- z = ln_c ( z );
- a1 = div_c ( c2, c3 );
- z = mul_c ( a1, z );
- return ( z );
- }
-
- compl acot_c ( compl z1 )
- {
- compl z, c1, c2, c3, a1, a2;
- c1.re = 0; c1.im = 1;
- c2.re = 1; c2.im = 0;
- c3.re = 0; c3.im = -2;
- z = mul_c ( c1, z1 );
- a1 = add_c ( z, c2 );
- a2 = sub_c ( z, c2 );
- z = div_c ( a1, a2 );
- z = ln_c ( z );
- a1 = div_c ( c2, c3 );
- z = mul_c ( a1, z );
- return ( z );
- }
-
- /******************* Hyperbel-Funktionen ******************/
-
- compl sinh_c ( compl z1 )
- {
- compl z;
- z1.im = arg_opt ( z1.im );
- z.re = sinh ( z1.re ) * cos ( z1.im );
- z.im = cosh ( z1.re ) * sin ( z1.im );
- return ( z );
- }
-
- compl cosh_c ( compl z1 )
-
- {
- compl z;
- z1.im = arg_opt ( z1.im );
- z.re = cosh ( z1.re ) * cos ( z1.im );
- z.im = sinh ( z1.re ) * sin ( z1.im );
- return ( z );
- }
-
- compl tanh_c ( compl z1 )
- {
- compl z;
- z = div_c ( sinh_c ( z1 ), cosh_c ( z1 ) );
- return ( z );
- }
-
- compl coth_c ( compl z1 )
- {
- compl z;
- z = div_c ( cosh_c ( z1 ), sinh_c ( z1 ) );
- return ( z );
- }
-
- /*********** Umkehrungen der Hyperbel-Funktionen **********/
-
- compl asinh_c ( compl z1 )
- {
- compl z, c;
- c.re = 1; c.im = 0;
- z = sqr_c ( z1 );
- z = add_c ( z, c );
- z = sqrt_c ( z );
- z = add_c ( z1, z );
- z = ln_c ( z );
- return ( z );
- }
-
- compl acosh_c ( compl z1 )
- {
- compl z, c;
- c.re = -1; c.im = 0;
- z = sqr_c ( z1 );
- z = add_c ( z, c );
- z = sqrt_c ( z );
- z = add_c ( z1, z );
- z = ln_c ( z );
- return ( z );
- }
-
- compl atanh_c ( compl z1 )
- {
- compl z, c1, c2, a1, a2;
- c1.re = 1; c1.im = 0;
- c2.re = 2; c2.im = 0;
- a1 = add_c ( c1, z1 );
- a2 = sub_c ( c1, z1 );
- z = div_c ( a1, a2 );
- z = ln_c ( z );
- z = div_c ( z, c2 );
- return ( z );
- }
-
- compl acoth_c ( compl z1 )
- {
- compl z, c1, c2, a1, a2;
- c1.re = 1; c1.im = 0;
- c2.re = 2; c2.im = 0;
- a1 = add_c ( z1, c1 );
- a2 = sub_c ( z1, c1 );
- z = div_c ( a1, a2 );
- z = ln_c ( z );
- z = div_c ( z, c2 );
- return ( z );
- }
-
- /******************* Ende von COMPLEX.C ******************/