123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /* cmplxf.c
- *
- * Complex number arithmetic
- *
- *
- *
- * SYNOPSIS:
- *
- * typedef struct {
- * float r; real part
- * float i; imaginary part
- * }cmplxf;
- *
- * cmplxf *a, *b, *c;
- *
- * caddf( a, b, c ); c = b + a
- * csubf( a, b, c ); c = b - a
- * cmulf( a, b, c ); c = b * a
- * cdivf( a, b, c ); c = b / a
- * cnegf( c ); c = -c
- * cmovf( b, c ); c = b
- *
- *
- *
- * DESCRIPTION:
- *
- * Addition:
- * c.r = b.r + a.r
- * c.i = b.i + a.i
- *
- * Subtraction:
- * c.r = b.r - a.r
- * c.i = b.i - a.i
- *
- * Multiplication:
- * c.r = b.r * a.r - b.i * a.i
- * c.i = b.r * a.i + b.i * a.r
- *
- * Division:
- * d = a.r * a.r + a.i * a.i
- * c.r = (b.r * a.r + b.i * a.i)/d
- * c.i = (b.i * a.r - b.r * a.i)/d
- * ACCURACY:
- *
- * In DEC arithmetic, the test (1/z) * z = 1 had peak relative
- * error 3.1e-17, rms 1.2e-17. The test (y/z) * (z/y) = 1 had
- * peak relative error 8.3e-17, rms 2.1e-17.
- *
- * Tests in the rectangle {-10,+10}:
- * Relative error:
- * arithmetic function # trials peak rms
- * IEEE cadd 30000 5.9e-8 2.6e-8
- * IEEE csub 30000 6.0e-8 2.6e-8
- * IEEE cmul 30000 1.1e-7 3.7e-8
- * IEEE cdiv 30000 2.1e-7 5.7e-8
- */
- /* cmplx.c
- * complex number arithmetic
- */
- /*
- Cephes Math Library Release 2.1: December, 1988
- Copyright 1984, 1987, 1988 by Stephen L. Moshier
- Direct inquiries to 30 Frost Street, Cambridge, MA 02140
- */
- #include <math.h>
- extern float MAXNUMF, MACHEPF, PIF, PIO2F;
- #define fabsf(x) ( (x) < 0 ? -(x) : (x) )
- #ifdef ANSIC
- float sqrtf(float), frexpf(float, int *);
- float ldexpf(float, int);
- float cabsf(cmplxf *), atan2f(float, float), cosf(float), sinf(float);
- #else
- float sqrtf(), frexpf(), ldexpf();
- float cabsf(), atan2f(), cosf(), sinf();
- #endif
- /*
- typedef struct
- {
- float r;
- float i;
- }cmplxf;
- */
- cmplxf czerof = {0.0, 0.0};
- extern cmplxf czerof;
- cmplxf conef = {1.0, 0.0};
- extern cmplxf conef;
- /* c = b + a */
- void caddf( a, b, c )
- register cmplxf *a, *b;
- cmplxf *c;
- {
- c->r = b->r + a->r;
- c->i = b->i + a->i;
- }
- /* c = b - a */
- void csubf( a, b, c )
- register cmplxf *a, *b;
- cmplxf *c;
- {
- c->r = b->r - a->r;
- c->i = b->i - a->i;
- }
- /* c = b * a */
- void cmulf( a, b, c )
- register cmplxf *a, *b;
- cmplxf *c;
- {
- register float y;
- y = b->r * a->r - b->i * a->i;
- c->i = b->r * a->i + b->i * a->r;
- c->r = y;
- }
- /* c = b / a */
- void cdivf( a, b, c )
- register cmplxf *a, *b;
- cmplxf *c;
- {
- float y, p, q, w;
- y = a->r * a->r + a->i * a->i;
- p = b->r * a->r + b->i * a->i;
- q = b->i * a->r - b->r * a->i;
- if( y < 1.0f )
- {
- w = MAXNUMF * y;
- if( (fabsf(p) > w) || (fabsf(q) > w) || (y == 0.0f) )
- {
- c->r = MAXNUMF;
- c->i = MAXNUMF;
- mtherr( "cdivf", OVERFLOW );
- return;
- }
- }
- c->r = p/y;
- c->i = q/y;
- }
- /* b = a */
- void cmovf( a, b )
- register short *a, *b;
- {
- int i;
- i = 8;
- do
- *b++ = *a++;
- while( --i );
- }
- void cnegf( a )
- register cmplxf *a;
- {
- a->r = -a->r;
- a->i = -a->i;
- }
- /* cabsf()
- *
- * Complex absolute value
- *
- *
- *
- * SYNOPSIS:
- *
- * float cabsf();
- * cmplxf z;
- * float a;
- *
- * a = cabsf( &z );
- *
- *
- *
- * DESCRIPTION:
- *
- *
- * If z = x + iy
- *
- * then
- *
- * a = sqrt( x**2 + y**2 ).
- *
- * Overflow and underflow are avoided by testing the magnitudes
- * of x and y before squaring. If either is outside half of
- * the floating point full scale range, both are rescaled.
- *
- *
- * ACCURACY:
- *
- * Relative error:
- * arithmetic domain # trials peak rms
- * IEEE -10,+10 30000 1.2e-7 3.4e-8
- */
- /*
- Cephes Math Library Release 2.1: January, 1989
- Copyright 1984, 1987, 1989 by Stephen L. Moshier
- Direct inquiries to 30 Frost Street, Cambridge, MA 02140
- */
- /*
- typedef struct
- {
- float r;
- float i;
- }cmplxf;
- */
- /* square root of max and min numbers */
- #define SMAX 1.3043817825332782216E+19
- #define SMIN 7.6664670834168704053E-20
- #define PREC 12
- #define MAXEXPF 128
- #define SMAXT (2.0f * SMAX)
- #define SMINT (0.5f * SMIN)
- float cabsf( z )
- register cmplxf *z;
- {
- float x, y, b, re, im;
- int ex, ey, e;
- re = fabsf( z->r );
- im = fabsf( z->i );
- if( re == 0.0f )
- {
- return( im );
- }
- if( im == 0.0f )
- {
- return( re );
- }
- /* Get the exponents of the numbers */
- x = frexpf( re, &ex );
- y = frexpf( im, &ey );
- /* Check if one number is tiny compared to the other */
- e = ex - ey;
- if( e > PREC )
- return( re );
- if( e < -PREC )
- return( im );
- /* Find approximate exponent e of the geometric mean. */
- e = (ex + ey) >> 1;
- /* Rescale so mean is about 1 */
- x = ldexpf( re, -e );
- y = ldexpf( im, -e );
-
- /* Hypotenuse of the right triangle */
- b = sqrtf( x * x + y * y );
- /* Compute the exponent of the answer. */
- y = frexpf( b, &ey );
- ey = e + ey;
- /* Check it for overflow and underflow. */
- if( ey > MAXEXPF )
- {
- mtherr( "cabsf", OVERFLOW );
- return( MAXNUMF );
- }
- if( ey < -MAXEXPF )
- return(0.0f);
- /* Undo the scaling */
- b = ldexpf( b, e );
- return( b );
- }
- /* csqrtf()
- *
- * Complex square root
- *
- *
- *
- * SYNOPSIS:
- *
- * void csqrtf();
- * cmplxf z, w;
- *
- * csqrtf( &z, &w );
- *
- *
- *
- * DESCRIPTION:
- *
- *
- * If z = x + iy, r = |z|, then
- *
- * 1/2
- * Im w = [ (r - x)/2 ] ,
- *
- * Re w = y / 2 Im w.
- *
- *
- * Note that -w is also a square root of z. The solution
- * reported is always in the upper half plane.
- *
- * Because of the potential for cancellation error in r - x,
- * the result is sharpened by doing a Heron iteration
- * (see sqrt.c) in complex arithmetic.
- *
- *
- *
- * ACCURACY:
- *
- * Relative error:
- * arithmetic domain # trials peak rms
- * IEEE -10,+10 100000 1.8e-7 4.2e-8
- *
- */
- void csqrtf( z, w )
- cmplxf *z, *w;
- {
- cmplxf q, s;
- float x, y, r, t;
- x = z->r;
- y = z->i;
- if( y == 0.0f )
- {
- if( x < 0.0f )
- {
- w->r = 0.0f;
- w->i = sqrtf(-x);
- return;
- }
- else
- {
- w->r = sqrtf(x);
- w->i = 0.0f;
- return;
- }
- }
- if( x == 0.0f )
- {
- r = fabsf(y);
- r = sqrtf(0.5f*r);
- if( y > 0 )
- w->r = r;
- else
- w->r = -r;
- w->i = r;
- return;
- }
- /* Approximate sqrt(x^2+y^2) - x = y^2/2x - y^4/24x^3 + ... .
- * The relative error in the first term is approximately y^2/12x^2 .
- */
- if( (fabsf(y) < fabsf(0.015f*x))
- && (x > 0) )
- {
- t = 0.25f*y*(y/x);
- }
- else
- {
- r = cabsf(z);
- t = 0.5f*(r - x);
- }
- r = sqrtf(t);
- q.i = r;
- q.r = 0.5f*y/r;
- /* Heron iteration in complex arithmetic:
- * q = (q + z/q)/2
- */
- cdivf( &q, z, &s );
- caddf( &q, &s, w );
- w->r *= 0.5f;
- w->i *= 0.5f;
- }
|