📄 clog.c
字号:
/* clog.c * * Complex natural logarithm * * * * SYNOPSIS: * * void clog(); * cmplx z, w; * * clog( &z, &w ); * * * * DESCRIPTION: * * Returns complex logarithm to the base e (2.718...) of * the complex argument x. * * If z = x + iy, r = sqrt( x**2 + y**2 ), * then * w = log(r) + i arctan(y/x). * * The arctangent ranges from -PI to +PI. * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 7000 8.5e-17 1.9e-17 * IEEE -10,+10 30000 5.0e-15 1.1e-16 * * Larger relative error can be observed for z near 1 +i0. * In IEEE arithmetic the peak absolute error is 5.2e-16, rms * absolute error 1.0e-16. *//*Cephes Math Library Release 2.8: June, 2000Copyright 1984, 1995, 2000 by Stephen L. Moshier*/#include "mconf.h"#ifdef ANSIPROTstatic void cchsh ( double x, double *c, double *s );static double redupi ( double x );static double ctans ( cmplx *z );/* These are supposed to be in some standard place. */double fabs (double);double sqrt (double);double pow (double, double);double log (double);double exp (double);double atan2 (double, double);double cosh (double);double sinh (double);double asin (double);double sin (double);double cos (double);double cabs (cmplx *);void cadd ( cmplx *, cmplx *, cmplx * );void cmul ( cmplx *, cmplx *, cmplx * );void csqrt ( cmplx *, cmplx * );static void cchsh ( double, double *, double * );static double redupi ( double );static double ctans ( cmplx * );void clog ( cmplx *, cmplx * );void casin ( cmplx *, cmplx * );void cacos ( cmplx *, cmplx * );void catan ( cmplx *, cmplx * );#elsestatic void cchsh();static double redupi();static double ctans();double cabs(), fabs(), sqrt(), pow();double log(), exp(), atan2(), cosh(), sinh();double asin(), sin(), cos();void cadd(), cmul(), csqrt();void clog(), casin(), cacos(), catan();#endifextern double MAXNUM, MACHEP, PI, PIO2;void clog( z, w )register cmplx *z, *w;{double p, rr;/*rr = sqrt( z->r * z->r + z->i * z->i );*/rr = cabs(z);p = log(rr);#if ANSICrr = atan2( z->i, z->r );#elserr = atan2( z->r, z->i );if( rr > PI ) rr -= PI + PI;#endifw->i = rr;w->r = p;}/* cexp() * * Complex exponential function * * * * SYNOPSIS: * * void cexp(); * cmplx z, w; * * cexp( &z, &w ); * * * * DESCRIPTION: * * Returns the exponential of the complex argument z * into the complex result w. * * If * z = x + iy, * r = exp(x), * * then * * w = r cos y + i r sin y. * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 8700 3.7e-17 1.1e-17 * IEEE -10,+10 30000 3.0e-16 8.7e-17 * */void cexp( z, w )register cmplx *z, *w;{double r;r = exp( z->r );w->r = r * cos( z->i );w->i = r * sin( z->i );}/* csin() * * Complex circular sine * * * * SYNOPSIS: * * void csin(); * cmplx z, w; * * csin( &z, &w ); * * * * DESCRIPTION: * * If * z = x + iy, * * then * * w = sin x cosh y + i cos x sinh y. * * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 8400 5.3e-17 1.3e-17 * IEEE -10,+10 30000 3.8e-16 1.0e-16 * Also tested by csin(casin(z)) = z. * */void csin( z, w )register cmplx *z, *w;{double ch, sh;cchsh( z->i, &ch, &sh );w->r = sin( z->r ) * ch;w->i = cos( z->r ) * sh;}/* calculate cosh and sinh */static void cchsh( x, c, s )double x, *c, *s;{double e, ei;if( fabs(x) <= 0.5 ) { *c = cosh(x); *s = sinh(x); }else { e = exp(x); ei = 0.5/e; e = 0.5 * e; *s = e - ei; *c = e + ei; }}/* ccos() * * Complex circular cosine * * * * SYNOPSIS: * * void ccos(); * cmplx z, w; * * ccos( &z, &w ); * * * * DESCRIPTION: * * If * z = x + iy, * * then * * w = cos x cosh y - i sin x sinh y. * * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 8400 4.5e-17 1.3e-17 * IEEE -10,+10 30000 3.8e-16 1.0e-16 */void ccos( z, w )register cmplx *z, *w;{double ch, sh;cchsh( z->i, &ch, &sh );w->r = cos( z->r ) * ch;w->i = -sin( z->r ) * sh;}/* ctan() * * Complex circular tangent * * * * SYNOPSIS: * * void ctan(); * cmplx z, w; * * ctan( &z, &w ); * * * * DESCRIPTION: * * If * z = x + iy, * * then * * sin 2x + i sinh 2y * w = --------------------. * cos 2x + cosh 2y * * On the real axis the denominator is zero at odd multiples * of PI/2. The denominator is evaluated by its Taylor * series near these points. * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 5200 7.1e-17 1.6e-17 * IEEE -10,+10 30000 7.2e-16 1.2e-16 * Also tested by ctan * ccot = 1 and catan(ctan(z)) = z. */void ctan( z, w )register cmplx *z, *w;{double d;d = cos( 2.0 * z->r ) + cosh( 2.0 * z->i );if( fabs(d) < 0.25 ) d = ctans(z);if( d == 0.0 ) { mtherr( "ctan", OVERFLOW ); w->r = MAXNUM; w->i = MAXNUM; return; }w->r = sin( 2.0 * z->r ) / d;w->i = sinh( 2.0 * z->i ) / d;}/* ccot() * * Complex circular cotangent * * * * SYNOPSIS: * * void ccot(); * cmplx z, w; * * ccot( &z, &w ); * * * * DESCRIPTION: * * If * z = x + iy, * * then * * sin 2x - i sinh 2y * w = --------------------. * cosh 2y - cos 2x * * On the real axis, the denominator has zeros at even * multiples of PI/2. Near these points it is evaluated * by a Taylor series. * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 3000 6.5e-17 1.6e-17 * IEEE -10,+10 30000 9.2e-16 1.2e-16 * Also tested by ctan * ccot = 1 + i0. */void ccot( z, w )register cmplx *z, *w;{double d;d = cosh(2.0 * z->i) - cos(2.0 * z->r);if( fabs(d) < 0.25 ) d = ctans(z);if( d == 0.0 ) { mtherr( "ccot", OVERFLOW ); w->r = MAXNUM; w->i = MAXNUM; return; }w->r = sin( 2.0 * z->r ) / d;w->i = -sinh( 2.0 * z->i ) / d;}/* Program to subtract nearest integer multiple of PI *//* extended precision value of PI: */#ifdef UNKstatic double DP1 = 3.14159265160560607910E0;static double DP2 = 1.98418714791870343106E-9;static double DP3 = 1.14423774522196636802E-17;#endif#ifdef DECstatic unsigned short P1[] = {0040511,0007732,0120000,0000000,};static unsigned short P2[] = {0031010,0055060,0100000,0000000,};static unsigned short P3[] = {0022123,0011431,0105056,0001560,};#define DP1 *(double *)P1#define DP2 *(double *)P2#define DP3 *(double *)P3#endif#ifdef IBMPCstatic unsigned short P1[] = {0x0000,0x5400,0x21fb,0x4009};static unsigned short P2[] = {0x0000,0x1000,0x0b46,0x3e21};static unsigned short P3[] = {0xc06e,0x3145,0x6263,0x3c6a};#define DP1 *(double *)P1#define DP2 *(double *)P2#define DP3 *(double *)P3#endif#ifdef MIEEEstatic unsigned short P1[] = {0x4009,0x21fb,0x5400,0x0000};static unsigned short P2[] = {0x3e21,0x0b46,0x1000,0x0000};static unsigned short P3[] = {0x3c6a,0x6263,0x3145,0xc06e};#define DP1 *(double *)P1#define DP2 *(double *)P2#define DP3 *(double *)P3#endifstatic double redupi(x)double x;{double t;long i;t = x/PI;if( t >= 0.0 ) t += 0.5;else t -= 0.5;i = t; /* the multiple */t = i;t = ((x - t * DP1) - t * DP2) - t * DP3;return(t);}/* Taylor series expansion for cosh(2y) - cos(2x) */static double ctans(z)cmplx *z;{double f, x, x2, y, y2, rn, t;double d;x = fabs( 2.0 * z->r );y = fabs( 2.0 * z->i );x = redupi(x);x = x * x;y = y * y;x2 = 1.0;y2 = 1.0;f = 1.0;rn = 0.0;d = 0.0;do { rn += 1.0; f *= rn; rn += 1.0; f *= rn; x2 *= x; y2 *= y; t = y2 + x2; t /= f; d += t; rn += 1.0; f *= rn; rn += 1.0; f *= rn; x2 *= x; y2 *= y; t = y2 - x2; t /= f; d += t; }while( fabs(t/d) > MACHEP );return(d);}/* casin() * * Complex circular arc sine * * * * SYNOPSIS: * * void casin(); * cmplx z, w; * * casin( &z, &w ); * * * * DESCRIPTION: * * Inverse complex sine: * * 2 * w = -i clog( iz + csqrt( 1 - z ) ). * * * ACCURACY: * * Relative error: * arithmetic domain # trials peak rms * DEC -10,+10 10100 2.1e-15 3.4e-16 * IEEE -10,+10 30000 2.2e-14 2.7e-15 * Larger relative error can be observed for z near zero. * Also tested by csin(casin(z)) = z. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -