📄 complex.c.sh
字号:
X CCheck( "CxAdd 2", *cp, 2.0, 3.0 );XX /* CxSub test */X cp = CxSub( CxCons( &a, 4.0, 7.0 ), bp );X CCheck( "CxSub 1", a, -2.0, 15.0 );X CCheck( "CxSub 2", *cp, -2.0, 15.0 );XX /* CxMul test */X cp = CxMul( CxCons( bp, -1.0, 3.0 ), CxCons( &a, 1.0, 2.0 ) );X CCheck( "CxMul 1", *bp, -7.0, 1.0 );X CCheck( "CxMul 2", *cp, -7.0, 1.0 );XX /* CxDiv test */X cp = CxDiv( bp, &a );X CCheck( "CxDiv 1", *bp, -1.0, 3.0 );X CCheck( "CxDiv 2", *cp, -1.0, 3.0 );XX /* CxSqrt and overlapping CxMul tests */X (void)CxCons( &a, -1.0, 2.0 );X cp = CxSqrt( CxMul( &a, &a ) );X CCheck( "CxSqrt 1", a, -1.0, 2.0 );X CCheck( "CxSqrt 2", *cp, -1.0, 2.0 );X (void)CxCons( &a, 3.0, 2.0 );X cp = CxSqrt( CxMul( &a, &a ) );X CCheck( "CxSqrt 3", a, 3.0, 2.0 );X CCheck( "CxSqrt 4", *cp, 3.0, 2.0 );XX /* CxFree "test" */X CxFree( bp );XX return errs;X }XXXstatic voidXCCheck( s, c, r, i ) /* check complex number */X char *s; /* message string for failure */X complex c; /* complex to be checked */X double r, i; /* expected real, imaginary parts */X {X if ( RelDif( CxReal( &c ), r ) > TOLX || RelDif( CxImag( &c ), i ) > TOLX ) {X ++errs;X Printf( "%s; s.b. (%f,%f), was (%g,%g)\n",X s, r, i, c.re, c.imX );X }X }XXXstatic voidXRCheck( s, d, r ) /* check real number */X char *s; /* message string for failure */X double d; /* real to be checked */X double r; /* expected value */X {X if ( RelDif( d, r ) > TOL )X {X ++errs;X Printf( "%s; s.b. %f, was %g\n", s, r, d );X }X }XXXstatic doubleXRelDif( a, b ) /* returns relative difference: */X double a, b; /* 0.0 if exactly the same,X otherwise ratio of differenceX to the larger of the two */X {X double c = Abs( a );X double d = Abs( b );XX d = Max( c, d );XX return d == 0.0 ? 0.0 : Abs( a - b ) / d;X }END_OF_cx_test.cif test 4250 -ne `wc -c <cx_test.c`; then echo shar: \"cx_test.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxadd.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxadd.c\"elseecho shar: Extracting \"cxadd.c\" \(304 characters\)sed "s/^X//" >cxadd.c <<'END_OF_cxadd.c'X/*X CxAdd -- add one complex to anotherXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxadd.c 1.1XX CxAdd( &a, &b ) adds b to a and returns &aX*/XX#include <complex.h>XXcomplex *XCxAdd( ap, bp )X register complex *ap, *bp; /* may coincide */X {X ap->re += bp->re;X ap->im += bp->im;XX return ap;X }END_OF_cxadd.cif test 304 -ne `wc -c <cxadd.c`; then echo shar: \"cxadd.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxampl.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxampl.c\"elseecho shar: Extracting \"cxampl.c\" \(278 characters\)sed "s/^X//" >cxampl.c <<'END_OF_cxampl.c'X/*X CxAmpl -- amplitude (magnitude, modulus, norm) of a complexXX CxAmpl( &c ) returns |c|XX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxampl.c 1.1X*/XX#include <math.h>XX#include <complex.h>XXdoubleXCxAmpl( cp )X register complex *cp;X {X return hypot( cp->re, cp->im );X }END_OF_cxampl.cif test 278 -ne `wc -c <cxampl.c`; then echo shar: \"cxampl.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxconj.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxconj.c\"elseecho shar: Extracting \"cxconj.c\" \(278 characters\)sed "s/^X//" >cxconj.c <<'END_OF_cxconj.c'X/*X CxConj -- conjugate a complexXX CxConj( &c ) conjugates c and returns &cXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxconj.c 1.1X*/XX#include <complex.h>XXcomplex *XCxConj( cp )X register complex *cp;X {X /* (real part unchanged) */X cp->im = -cp->im;XX return cp;X }END_OF_cxconj.cif test 278 -ne `wc -c <cxconj.c`; then echo shar: \"cxconj.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxcons.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxcons.c\"elseecho shar: Extracting \"cxcons.c\" \(329 characters\)sed "s/^X//" >cxcons.c <<'END_OF_cxcons.c'X/*X CxCons -- construct a complex from real and imaginary partsXX CxCons( &c, re, im ) makes c = re + i im and returns &cXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxcons.c 1.1X*/XX#include <complex.h>XXcomplex *XCxCons( cp, re, im )X register complex *cp;X double re, im;X {X cp->re = re;X cp->im = im;XX return cp;X }END_OF_cxcons.cif test 329 -ne `wc -c <cxcons.c`; then echo shar: \"cxcons.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxcopy.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxcopy.c\"elseecho shar: Extracting \"cxcopy.c\" \(264 characters\)sed "s/^X//" >cxcopy.c <<'END_OF_cxcopy.c'X/*X CxCopy -- copy a complexXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxcopy.c 1.1XX CxCopy( &a, &b ) copies b to a and returns &aX*/XX#include <complex.h>XXcomplex *XCxCopy( ap, bp )X complex *ap, *bp; /* may coincide */X {X *ap = *bp;XX return ap;X }END_OF_cxcopy.cif test 264 -ne `wc -c <cxcopy.c`; then echo shar: \"cxcopy.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxdiv.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxdiv.c\"elseecho shar: Extracting \"cxdiv.c\" \(820 characters\)sed "s/^X//" >cxdiv.c <<'END_OF_cxdiv.c'X/*X CxDiv -- divide one complex by anotherXX CxDiv( &a, &b ) divides a by b and returns &a;X zero divisor failsXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxdiv.c 1.1 (modified for public version)X*/XX#include <complex.h>XX#define Abs( x ) ((x) < 0 ? -(x) : (x))XXcomplex *XCxDiv( ap, bp )X register complex *ap, *bp; /* may coincide (?) */X {X double r, s;X double ap__re = ap->re;XX /* Note: classical formula may cause unnecessary overflow */X r = bp->re;X s = bp->im;X if ( Abs( r ) >= Abs( s ) )X {X r = s / r; /* <= 1 */X s = bp->re + r * s;X ap->re = (ap->re + ap->im * r) / s;X ap->im = (ap->im - ap__re * r) / s;X }X else /* Abs( s ) > Abs( r ) */X {X r = r / s; /* < 1 */X s = s + r * bp->re;X ap->re = (ap->re * r + ap->im) / s;X ap->im = (ap->im * r - ap__re) / s;X }XX return ap;X }END_OF_cxdiv.cif test 820 -ne `wc -c <cxdiv.c`; then echo shar: \"cxdiv.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxmul.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxmul.c\"elseecho shar: Extracting \"cxmul.c\" \(424 characters\)sed "s/^X//" >cxmul.c <<'END_OF_cxmul.c'X/*X CxMul -- multiply one complex by anotherXX CxMul( &a, &b ) multiplies a by b and returns &aXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxmul.c 1.1X*/XX#include <complex.h>XXcomplex *XCxMul( ap, bp )X register complex *ap, *bp; /* (may coincide) */X {X double ap__re = ap->re;X double bp__re = bp->re;XX ap->re = ap__re * bp__re - ap->im * bp->im;X ap->im = ap__re * bp->im + ap->im * bp__re;XX return ap;X }END_OF_cxmul.cif test 424 -ne `wc -c <cxmul.c`; then echo shar: \"cxmul.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxphas.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxphas.c\"elseecho shar: Extracting \"cxphas.c\" \(406 characters\)sed "s/^X//" >cxphas.c <<'END_OF_cxphas.c'X/*X CxPhas -- phase (angle, argument) of a complexXX CxPhas( &c ) returns arg(c) in radians (-Pi,Pi]XX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxphas.c 1.1 (modified for public version)X*/XX#include <math.h>XX#include <complex.h>XXdoubleXCxPhas( cp )X register complex *cp;X {X if ( cp->re == 0.0 && cp->im == 0.0 )X return 0.0; /* can't trust atan2() */X elseX return atan2( cp->im, cp->re );X }END_OF_cxphas.cif test 406 -ne `wc -c <cxphas.c`; then echo shar: \"cxphas.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxphsr.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxphsr.c\"elseecho shar: Extracting \"cxphsr.c\" \(395 characters\)sed "s/^X//" >cxphsr.c <<'END_OF_cxphsr.c'X/*X CxPhsr -- construct a complex "phasor" from amplitude and phaseXX CxPhsr( &c, amp, phs ) makes c = amp exp(i phs)X and returns &cXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxphsr.c 1.1X*/XX#include <math.h>XX#include <complex.h>XXcomplex *XCxPhsr( cp, amp, phs )X register complex *cp;X double amp, phs;X {X cp->re = amp * cos( phs );X cp->im = amp * sin( phs );XX return cp;X }END_OF_cxphsr.cif test 395 -ne `wc -c <cxphsr.c`; then echo shar: \"cxphsr.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxscal.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxscal.c\"elseecho shar: Extracting \"cxscal.c\" \(291 characters\)sed "s/^X//" >cxscal.c <<'END_OF_cxscal.c'X/*X CxScal -- multiply a complex by a scalarXX CxScal( &c, s ) scales c by s and returns &cXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxscal.c 1.1X*/XX#include <complex.h>XXcomplex *XCxScal( cp, s )X register complex *cp;X double s;X {X cp->re *= s;X cp->im *= s;XX return cp;X }END_OF_cxscal.cif test 291 -ne `wc -c <cxscal.c`; then echo shar: \"cxscal.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxsqrt.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxsqrt.c\"elseecho shar: Extracting \"cxsqrt.c\" \(1339 characters\)sed "s/^X//" >cxsqrt.c <<'END_OF_cxsqrt.c'X/*X CxSqrt -- compute square root of complex numberXX CxSqrt( &c ) replaces c by sqrt(c) and returns &cXX Note: This is a double-valued function; the result ofX CxSqrt() always has nonnegative imaginary part.XX inspired by Jeff Hanes' versionXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxsqrt.c 1.1 (modified for public version)X*/XX#include <math.h>XX#include <complex.h>XX#define Sgn( x ) ((x) == 0 ? 0 : (x) > 0 ? 1 : -1)XXcomplex *XCxSqrt( cp )X register complex *cp;X {X /* record signs of original real & imaginary parts */X int re_sign = Sgn( cp->re );X int im_sign = Sgn( cp->im );XX /* special cases are not necessary; they are here for speed */XX if ( re_sign == 0 )X if ( im_sign == 0 )X ; /* (0,0) already there */X else if ( im_sign > 0 )X cp->re = cp->im = sqrt( cp->im / 2.0 );X else /* im_sign < 0 */X cp->re = -(cp->im = sqrt( -cp->im / 2.0 ));X else if ( im_sign == 0 )X if ( re_sign > 0 )X cp->re = sqrt( cp->re );X/* cp->im = 0.0; /* 0 already there */X else { /* re_sign < 0 */X cp->im = sqrt( -cp->re );X cp->re = 0.0;X }X else { /* no shortcuts */X double ampl = CxAmpl( cp );XX cp->im = sqrt( (ampl - cp->re) /2.0 );XX if ( im_sign > 0 )X cp->re = sqrt( (ampl + cp->re) / 2.0 );X else /* im_sign < 0 */X cp->re = -sqrt( (ampl + cp->re) / 2.0 );X }XX return cp;X }END_OF_cxsqrt.cif test 1339 -ne `wc -c <cxsqrt.c`; then echo shar: \"cxsqrt.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cxsub.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"cxsub.c\"elseecho shar: Extracting \"cxsub.c\" \(320 characters\)sed "s/^X//" >cxsub.c <<'END_OF_cxsub.c'X/*X CxSub -- subtract one complex from anotherXX CxSub( &a, &b ) subtracts b from a and returns &aXX last edit: 86/01/04 D A GwynXX SCCS ID: @(#)cxsub.c 1.1X*/XX#include <complex.h>XXcomplex *XCxSub( ap, bp )X register complex *ap, *bp; /* (may coincide) */X {X ap->re -= bp->re;X ap->im -= bp->im;XX return ap;X }END_OF_cxsub.cif test 320 -ne `wc -c <cxsub.c`; then echo shar: \"cxsub.c\" unpacked with wrong size!fi# end of overwriting checkfiecho shar: End of shell archive.exit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -