⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 complex.c.sh

📁 复数运算库
💻 SH
📖 第 1 页 / 共 2 页
字号:
#! /bin/sh# This is a shell archive.  Remove anything before this line, then unpack# it by saving it into a file and typing "sh file".  To overwrite existing# files, type "sh file -c".  You can also feed this as standard input via# unshar, or by typing "sh <file", e.g..  If this archive is complete, you# will see the following message at the end:#		"End of shell archive."# Contents:  Makefile complex.3 complex.h cx_test.c cxadd.c cxampl.c#   cxconj.c cxcons.c cxcopy.c cxdiv.c cxmul.c cxphas.c cxphsr.c#   cxscal.c cxsqrt.c cxsub.cPATH=/bin:/usr/bin:/usr/ucb ; export PATHif test -f Makefile -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"Makefile\"elseecho shar: Extracting \"Makefile\" \(395 characters\)sed "s/^X//" >Makefile <<'END_OF_Makefile'XALL=complex.3 complex.h libcomplex.aXOBJS=\X    cxadd.o cxampl.o cxconj.o cxcons.o cxcopy.o cxdiv.o cxmul.o \X    cxphas.o cxphsr.o cxscal.o cxsqrt.o cxsub.oXXall:	$(ALL)XXinstall:	$(ALL)X	@echo install $(ALL) according to local convention.XXcx_test:	cx_test.c libcomplex.aX	$(CC) $(CFLAGS) -o cx_test cx_test.c libcomplex.aXXlibcomplex.a:	$(OBJS)X	ar r libcomplex.a $(OBJS)XX$(OBJS):	complex.hEND_OF_Makefileif test 395 -ne `wc -c <Makefile`; then    echo shar: \"Makefile\" unpacked with wrong size!fi# end of overwriting checkfiif test -f complex.3 -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"complex.3\"elseecho shar: Extracting \"complex.3\" \(5830 characters\)sed "s/^X//" >complex.3 <<'END_OF_complex.3'X'\" eX.TH COMPLEX 3V LOCALX'\"	last edit:	86/02/03	D A GwynX'\"	SCCS ID:	@(#)complex.3	1.2 (modified for public version)X.EQXdelim @@X.ENX.SH NAMEXcomplex \- complex arithmetic operationsX.SH SYNOPSISX.BX#include <complex.h>	/* assuming appropriate cc \-I option */X.brX/* All the following functions are declared in this header file. */X.PX.B complex *CxAdd(ap,bp);X.brX.B complex *ap, *bp;X.PX.B complex *CxSub(ap,bp);X.brX.B complex *ap, *bp;X.PX.B complex *CxMul(ap,bp);X.brX.B complex *ap, *bp;X.PX.B complex *CxDiv(ap,bp);X.brX.B complex *ap, *bp;X.PX.B complex *CxSqrt(cp);X.brX.B complex *cp;X.PX.B complex *CxScal(cp,\^s);X.brX.B complex *cp;X.brX.B double s;X.PX.B complex *CxNeg(cp);X.brX.B complex *cp;X.PX.B complex *CxConj(cp);X.brX.B complex *cp;X.PX.B complex *CxCopy(ap,bp);X.brX.B complex *ap, *bp;X.PX.B complex *CxCons(cp,\^r,\^i);X.brX.B complex *cp;X.brX.B double r, i;X.PX.B complex *CxPhsr(cp,m,p);X.brX.B complex *cp;X.brX.B double m, p;X.PX.B double CxReal(cp);X.brX.B complex *cp;X.PX.B double CxImag(cp);X.brX.B complex *cp;X.PX.B double CxAmpl(cp);X.brX.B complex *cp;X.PX.B double CxPhas(cp);X.brX.B complex *cp;X.PX.B complex *CxAllo(\ );X.PX.B void CxFree(cp);X.brX.B complex *cp;X.SH DESCRIPTIONXThese routines perform arithmeticXand other useful operations on complex numbers.XAn appropriate data structureX.B complexXis defined in the header file;Xall access toX.B complexXdata should beX.I viaXthese predefined functions.X(SeeX.SM HINTSXfor further information.)X.PXIn the following descriptions,Xthe namesX.IR a ,X.IR b ,XandX.I cXrepresent theX.B complexXdata addressed by the corresponding pointersX.IR ap ,X.IR bp ,XandX.IR cp .X.PX.I CxAdd\^XaddsX.I bXtoX.I aXand returns a pointer to the result.X.PX.I CxSubXsubtractsX.I bXfromX.I aXand returns a pointer to the result.X.PX.I CxMul\^XmultipliesX.I aXbyX.I bXand returns a pointer to the result.X.PX.I CxDivXdividesX.I aXbyX.I bXand returns a pointer to the result.XThe divisor must not be precisely zero.X.PX.I CxSqrtXreplacesX.I cXby the ``principal value'' of its square rootX(one having a non-negative imaginary part)Xand returns a pointer to the result.X.PX.I CxScal\^XmultipliesX.I cXby the scalarX.I sXand returns a pointer to the result.X.PX.I CxNegXnegatesX.I cXand returns a pointer to the result.X.PX.I CxConjXconjugatesX.I cXand returns a pointer to the result.X.PX.I CxCopyXassigns the value ofX.I bXtoX.I aXand returns a pointer to the result.X.PX.I CxConsXconstructs the complex numberX.I cXfrom its real and imaginary partsX.I rXandX.IR i ,Xrespectively,Xand returns a pointer to the result.X.PX.I CxPhsrXconstructs the complex numberX.I cXfrom its ``phasor'' amplitude and phase (given in radians)X.I mXandX.IR p ,Xrespectively,Xand returns a pointer to the result.X.PX.I CxReal\^Xreturns the real part of the complex numberX.IR c .X.PX.I CxImagXreturns the imaginary part of the complex numberX.IR c .X.PX.I CxAmpl\^Xreturns the amplitude of the complex numberX.IR c .X.PX.I CxPhasXreturns the phase of the complex numberX.IR c ,Xas radians in the range @(- pi , pi ]@.X.PX.I CxAlloXallocates storage for aX.B complexXdatum; it returnsX.SMX.B NULLX(defined as 0 inX.BR <stdio.h> )Xif not enough storage is available.X.PX.I CxFreeXreleases storage previously allocated byX.IR CxAllo .XThe contents of such storage must not be used afterward.X.SH HINTSXTheX.B complexXdata type consists of real and imaginary components;X.I CxReal\^XandX.I CxImagXare actually macros that access these components directly.XThis allows addresses of the components to be taken,Xas in the following \s-1EXAMPLE\s0.X.PXThe complex functions are designed to be nested;Xsee the following \s-1EXAMPLE\s0.XFor this reason,Xmany of them modify the contents of their first parameter.X.I CxCopyXcan be used to create a ``working copy'' ofX.B complexXdata that would otherwise be modified.X.PXThe square-root function is inherently double-valued;Xin most applications, both roots should receive equal consideration.XThe second root is the negative of the ``principal value''.X.bpX.SH EXAMPLEXThe following program is compiled by the commandX.brX	$ \fIcc \|\-I/usr/local/include \|example.c \|/usr/local/lib/libcomplex.a \|\-lm\fPX.brXIt reads in two complex vectors,Xthen computes and prints their inner product.X.spX.PX	#include	<stdio.h>X.brX	#include	<complex.h>X.spX	main( argc, argv )X.brX		int		argc;X.brX		char		*argv[\|];X.brX		{X.brX		int		n;		/* # elements in each array */X.brX		int		i;		/* indexes arrays */X.brX		complex		a[10], b[10];	/* input vectors */X.brX		complex		s;		/* accumulates scalar product */X.brX		complex		*c = CxAllo(\|);	/* holds cross-term */X.spX		if ( c == NULL )X.brX			{X.brX			(void)fprintf( stderr, ``not enough memory\en'' );X.brX			return 1;X.brX			}X.brX		(void)printf( ``\enenter number of elements: '' );X.brX		(void)scanf( `` %d'', &n );X.brX		/* (There really should be some input validation here.) */X.brX		(void) printf( ``\enenter real, imaginary pairs for first array:\en'' );X.brX		for ( i = 0; i < n; ++i )X.brX			(void)scanf( `` %lg %lg'', &CxReal( &a[i] ), &CxImag( &a[i] ) );X.brX		(void)printf( ``\enenter real, imaginary pairs for second array:\en'' );X.brX		for ( i = 0; i < n; ++i )X.brX			(void)scanf( `` %lg %lg'', &CxReal( &b[i] ), &CxImag( &b[i] ) );X.brX		(void)CxCons( &s, 0.0, 0.0 );	/* initialize accumulator */X.brX		for ( i = 0; i < n; ++i )X.brX			(void)CxAdd( &s, CxMul( &a[i], CxConj( CxCopy( c, &b[i] ) ) ) );X.brX		(void)printf( ``\enproduct is (%g,%g)\en'', CxReal( &s ), CxImag( &s ) );X.brX		CxFree( c );X.brX		return 0;X.brX		}X.SH FILESX/usr/local/include/complex.h		header file containing definitionsX.brX/usr/local/lib/libcomplex.a		complex run-time support libraryX.SH AUTHORSXDouglas A. Gwyn, BRL/VLD-VMBX.brXJeff Hanes, BRL/VLD-VMB (original version ofX.IR CxSqrt\^ )END_OF_complex.3if test 5830 -ne `wc -c <complex.3`; then    echo shar: \"complex.3\" unpacked with wrong size!fi# end of overwriting checkfiif test -f complex.h -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"complex.h\"elseecho shar: Extracting \"complex.h\" \(966 characters\)sed "s/^X//" >complex.h <<'END_OF_complex.h'X/*X	<complex.h> -- definitions for complex arithmetic routinesXX	last edit:	86/01/04	D A GwynXX	SCCS ID:	@(#)complex.h	1.1 (modified for public version)X*/XX/* "complex number" data type: */XXtypedef structX	{X	double		re;		/* real part */X	double		im;		/* imaginary part */X	}	complex;XX/* "The future is now": */XX#ifdef __STDC__	/* X3J11 */X#define	_CxGenPtr	void *		/* generic pointer type */X#else		/* K&R */X#define	_CxGenPtr	char *		/* generic pointer type */X#endifXX/* functions that are correctly done as macros: */XX#define	CxAllo()		((complex *)malloc( sizeof (complex) ))X#define	CxFree( cp )		free( (_CxGenPtr)(cp) )X#define	CxNeg( cp )		CxScal( cp, -1.0 )X#define	CxReal( cp )		(cp)->reX#define	CxImag( cp )		(cp)->imXXextern void		free();Xextern _CxGenPtr	malloc();XX/* library functions: */XXextern double	CxAmpl(), CxPhas();Xextern complex	*CxAdd(), *CxConj(), *CxCons(), *CxCopy(), *CxDiv(),X		*CxMul(), *CxPhsr(), *CxScal(), *CxSqrt(), *CxSub();END_OF_complex.hif test 966 -ne `wc -c <complex.h`; then    echo shar: \"complex.h\" unpacked with wrong size!fi# end of overwriting checkfiif test -f cx_test.c -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"cx_test.c\"elseecho shar: Extracting \"cx_test.c\" \(4250 characters\)sed "s/^X//" >cx_test.c <<'END_OF_cx_test.c'X/*X	ctest -- complex arithmetic testXX	last edit:	86/01/04	D A GwynXX	SCCS ID:	@(#)cx_test.c	1.1 (modified for public version)X*/XX#include	<stdio.h>X#include	<math.h>XX#include	<complex.h>XX#define DEGRAD	57.2957795130823208767981548141051703324054724665642X					/* degrees per radian */X#define Abs( x )	((x) < 0 ? -(x) : (x))X#define Max( a, b )	((a) > (b) ? (a) : (b))XXextern void	exit();XX#define	Printf	(void)printfXX#define	TOL	1.0e-10			/* tolerance for checks */XXstatic int	errs = 0;		/* tally errors */XXstatic void	CCheck(), RCheck();Xstatic double	RelDif();XXX/*ARGSUSED*/Xmain( argc, argv )X	int	argc;X	char	*argv[];X	{X	complex a, *bp, *cp;XX	/* CxAllo test */X	bp = CxAllo();X	if ( bp == NULL )X		{X		Printf( "CxAllo failed\n" );X		exit( 1 );X		}XX	/* CxReal, CxImag test */X	CxReal( bp ) = 1.0;X	CxImag( bp ) = 2.0;X	RCheck( "CxReal", CxReal( bp ), 1.0 );X	RCheck( "CxImag", CxImag( bp ), 2.0 );XX	/* CxCons test */X	cp = CxCons( &a, -3.0, -4.0);X	CCheck( "CxCons 1", a, -3.0, -4.0 );X	CCheck( "CxCons 2", *cp, -3.0, -4.0 );XX	/* CxNeg test */X	cp = CxNeg( &a );X	CCheck( "CxNeg 1", a, 3.0, 4.0 );X	CCheck( "CxNeg 2", *cp, 3.0, 4.0 );XX	/* CxCopy test */X	cp = CxCopy( bp, &a );X	(void)CxCons( &a, 1.0, sqrt( 3.0 ) );X	CCheck( "CxCopy 1", *bp, 3.0, 4.0 );X	CCheck( "CxCopy 2", *cp, 3.0, 4.0 );XX	/* CxAmpl, CxPhas test */X	RCheck( "CxAmpl 1", CxAmpl( &a ), 2.0 );X	RCheck( "CxPhas 1", CxPhas( &a ) * DEGRAD, 60.0 );X	/* try other quadrants */X	a.re = -a.re;X	RCheck( "CxAmpl 2", CxAmpl( &a ), 2.0 );X	RCheck( "CxPhas 2", CxPhas( &a ) * DEGRAD, 120.0 );X	a.im = -a.im;X	RCheck( "CxAmpl 3", CxAmpl( &a ), 2.0 );X	RCheck( "CxPhas 3", CxPhas( &a ) * DEGRAD, -120.0 );X	a.re = -a.re;X	RCheck( "CxAmpl 4", CxAmpl( &a ), 2.0 );X	RCheck( "CxPhas 4", CxPhas( &a ) * DEGRAD, -60.0 );X	/* one more for good measure */X	RCheck( "CxAmpl 5", CxAmpl( bp ), 5.0 );XX	/* CxPhsr test */X	cp = CxPhsr( &a, 100.0, -20.0 / DEGRAD );X	RCheck( "CxPhsr 1", CxAmpl( &a ), 100.0 );X	RCheck( "CxPhsr 2", CxPhas( &a ) * DEGRAD, -20.0 );X	RCheck( "CxPhsr 3", CxAmpl( cp ), 100.0 );X	RCheck( "CxPhsr 4", CxPhas( cp ) * DEGRAD, -20.0 );XX	/* CxConj test */X	cp = CxConj( bp );X	CCheck( "CxConj 1", *bp, 3.0, -4.0 );X	CCheck( "CxConj 2", *cp, 3.0, -4.0 );XX	/* CxScal test */X	cp = CxScal( bp, 2.0 );X	CCheck( "CxScal 1", *bp, 6.0, -8.0 );X	CCheck( "CxScal 2", *cp, 6.0, -8.0 );XX	/* CxAdd test */X	cp = CxAdd( CxCons( &a, -4.0, 11.0 ), bp );X	CCheck( "CxAdd 1", a, 2.0, 3.0 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -