ngspice/src/include/complex.h

221 lines
5.3 KiB
C
Raw Normal View History

2000-04-27 22:03:57 +02:00
/*
* Copyright (c) 1985 Thomas L. Quarles
* src/Makefile.am src/main.c src/sconvert.c src/analysis/cktdisto.c src/analysis/cktnoise.c src/analysis/noisean.c: Updates for the new header files. * src/maths/cmaths/cmath1.c src/maths/cmaths/cmath2.c src/maths/cmaths/cmath3.c src/maths/cmaths/cmath4.c: Updates for the new header files. * src/frontend/.cvsignore src/frontend/Makefile.am: Updates for the new files. * src/frontend/agraf.c src/frontend/aspice.c src/frontend/breakp.c src/frontend/breakp2.c src/frontend/circuits.c src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/define.c src/frontend/diff.c src/frontend/dimens.c src/frontend/display.c src/frontend/doplot.c src/frontend/dotcards.c src/frontend/evaluate.c src/frontend/fourier.c src/frontend/graf.c src/frontend/grid.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/interp.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/misccoms.h src/frontend/miscvars.c src/frontend/mw_coms.c src/frontend/newcoms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/plotcurv.c src/frontend/points.c src/frontend/postcoms.c src/frontend/rawfile.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/typesdef.c src/frontend/vectors.c src/frontend/where.c src/frontend/postcoms.c: Updates for the new header files. Some commands have moved into the new files below. * src/frontend/README src/frontend/com_compose.c src/frontend/com_compose.h src/frontend/com_display.c src/frontend/com_display.h src/frontend/com_let.c src/frontend/com_let.h src/frontend/com_setscale.c src/frontend/com_setscale.h src/frontend/commands.c src/frontend/commands.h src/frontend/completion.h src/frontend/streams.h src/frontend/testcommands.c: Separation into different com_* commands. This is a start. The rest of the subdirectory needs doing. * src/include/complex.h src/include/cpdefs.h src/include/cpextern.h src/include/cpstd.h src/include/fteconst.h src/include/ftedata.h src/include/ftedev.h src/include/fteext.h src/include/ftegraph.h src/include/fteparse.h src/include/dvec.h src/include/grid.h src/include/plot.h src/include/pnode.h src/include/sim.h src/include/variable.h src/include/wordlist.h src/include/bool.h: Separation of header files into smaller pieces. This limits recompilation to only the affected source files. The original header files have a warning message embedded to flag obsoleted use. * src/frontend/compose.c src/frontend/compose.h src/frontend/nutctab.c src/frontend/nutctab.h src/frontend/plot5.c src/frontend/plot5.h src/frontend/spcmdtab.c src/frontend/x11.c src/frontend/x11.h src/frontend/xgraph.c src/frontend/xgraph.h: Moved these files into src/frontend/plotting subdirectory. * src/frontend/plotting/.cvsignore src/frontend/plotting/Makefile.am src/frontend/plotting/plot5.c src/frontend/plotting/plot5.h src/frontend/plotting/plotting.c src/frontend/plotting/plotting.h src/frontend/plotting/pvec.c src/frontend/plotting/pvec.h src/frontend/plotting/x11.c src/frontend/plotting/x11.h src/frontend/plotting/xgraph.c src/frontend/plotting/xgraph.h: The new libplotting library with automake and CVS infrastructure.
2000-05-06 16:12:51 +02:00
* Modified: Paolo Nenzi 1999, Arno W. Peters 2000
2000-04-27 22:03:57 +02:00
*/
* src/Makefile.am src/main.c src/sconvert.c src/analysis/cktdisto.c src/analysis/cktnoise.c src/analysis/noisean.c: Updates for the new header files. * src/maths/cmaths/cmath1.c src/maths/cmaths/cmath2.c src/maths/cmaths/cmath3.c src/maths/cmaths/cmath4.c: Updates for the new header files. * src/frontend/.cvsignore src/frontend/Makefile.am: Updates for the new files. * src/frontend/agraf.c src/frontend/aspice.c src/frontend/breakp.c src/frontend/breakp2.c src/frontend/circuits.c src/frontend/cpitf.c src/frontend/debugcom.c src/frontend/define.c src/frontend/diff.c src/frontend/dimens.c src/frontend/display.c src/frontend/doplot.c src/frontend/dotcards.c src/frontend/evaluate.c src/frontend/fourier.c src/frontend/graf.c src/frontend/grid.c src/frontend/inp.c src/frontend/inpcom.c src/frontend/interp.c src/frontend/linear.c src/frontend/misccoms.c src/frontend/misccoms.h src/frontend/miscvars.c src/frontend/mw_coms.c src/frontend/newcoms.c src/frontend/nutinp.c src/frontend/options.c src/frontend/outitf.c src/frontend/parse.c src/frontend/plotcurv.c src/frontend/points.c src/frontend/postcoms.c src/frontend/rawfile.c src/frontend/runcoms.c src/frontend/runcoms2.c src/frontend/shyu.c src/frontend/spec.c src/frontend/spiceif.c src/frontend/typesdef.c src/frontend/vectors.c src/frontend/where.c src/frontend/postcoms.c: Updates for the new header files. Some commands have moved into the new files below. * src/frontend/README src/frontend/com_compose.c src/frontend/com_compose.h src/frontend/com_display.c src/frontend/com_display.h src/frontend/com_let.c src/frontend/com_let.h src/frontend/com_setscale.c src/frontend/com_setscale.h src/frontend/commands.c src/frontend/commands.h src/frontend/completion.h src/frontend/streams.h src/frontend/testcommands.c: Separation into different com_* commands. This is a start. The rest of the subdirectory needs doing. * src/include/complex.h src/include/cpdefs.h src/include/cpextern.h src/include/cpstd.h src/include/fteconst.h src/include/ftedata.h src/include/ftedev.h src/include/fteext.h src/include/ftegraph.h src/include/fteparse.h src/include/dvec.h src/include/grid.h src/include/plot.h src/include/pnode.h src/include/sim.h src/include/variable.h src/include/wordlist.h src/include/bool.h: Separation of header files into smaller pieces. This limits recompilation to only the affected source files. The original header files have a warning message embedded to flag obsoleted use. * src/frontend/compose.c src/frontend/compose.h src/frontend/nutctab.c src/frontend/nutctab.h src/frontend/plot5.c src/frontend/plot5.h src/frontend/spcmdtab.c src/frontend/x11.c src/frontend/x11.h src/frontend/xgraph.c src/frontend/xgraph.h: Moved these files into src/frontend/plotting subdirectory. * src/frontend/plotting/.cvsignore src/frontend/plotting/Makefile.am src/frontend/plotting/plot5.c src/frontend/plotting/plot5.h src/frontend/plotting/plotting.c src/frontend/plotting/plotting.h src/frontend/plotting/pvec.c src/frontend/plotting/pvec.h src/frontend/plotting/x11.c src/frontend/plotting/x11.h src/frontend/plotting/xgraph.c src/frontend/plotting/xgraph.h: The new libplotting library with automake and CVS infrastructure.
2000-05-06 16:12:51 +02:00
#ifndef _COMPLEX_H
#define _COMPLEX_H
/* Complex numbers. */
struct _complex { /* IBM portability... */
double cx_real;
double cx_imag;
} ;
typedef struct _complex complex;
#define realpart(cval) ((struct _complex *) (cval))->cx_real
#define imagpart(cval) ((struct _complex *) (cval))->cx_imag
2000-04-27 22:03:57 +02:00
/* header file containing definitions for complex functions
*
* Each expects two arguments for each complex number - a real and an
* imaginary part.
*/
typedef struct {
double real;
double imag;
} SPcomplex;
#define DC_ABS(a,b) (fabs(a) + fabs(b))
/*
* Division among complex numbers
*/
#define DC_DIVEQ(a,b,c,d) { \
double r,s,x,y;\
if(fabs(c)>fabs(d)) { \
r=(d)/(c);\
s=(c)+r*(d);\
x=((*(a))+(*(b))*r)/s;\
y=((*(b))-(*(a))*r)/s;\
} else { \
r=(c)/(d);\
s=(d)+r*(c);\
x=((*(a))*r+(*(b)))/s;\
y=((*(b))*r-(*(a)))/s;\
}\
(*(a)) = x; \
(*(b)) = y; \
}
/*
* This is the standard multiplication among complex numbers:
* (x+jy)=(a+jb)*(c+jd)
* x = ac - bd and y = ad + bc
*/
#define DC_MULT(a,b,c,d,x,y) { \
*(x) = (a) * (c) - (b) * (d) ;\
*(y) = (a) * (d) + (b) * (c) ;\
}
/*
* Difference among complex numbers a+jb and c+jd
* a = a - c amd b = b - d
*/
#define DC_MINUSEQ(a,b,c,d) { \
*(a) -= (c) ;\
*(b) -= (d) ;\
}
/*
* Square root among complex numbers
* We need to treat all the cases because the sqrt() function
* works only on real numbers.
*/
#define C_SQRT(A) { \
double _mag, _a; \
if ((A).imag == 0.0) { \
if ((A).real < 0.0) { \
(A).imag = sqrt(-(A).real); \
(A).real = 0.0; \
} else { \
(A).real = sqrt((A).real); \
(A).imag = 0.0; \
} \
} else { \
_mag = sqrt((A).real * (A).real + (A).imag * (A).imag); \
_a = (_mag - (A).real) / 2.0; \
if (_a <= 0.0) { \
(A).real = sqrt(_mag); \
(A).imag /= (2.0 * (A).real); /*XXX*/ \
} else { \
_a = sqrt(_a); \
(A).real = (A).imag / (2.0 * _a); \
(A).imag = _a; \
} \
} \
}
/*
* This macro calculates the squared modulus of the complex number
* and return it as the real part of the same number:
* a+jb -> a = (a*a) + (b*b)
*/
#define C_MAG2(A) (((A).real = (A).real * (A).real + (A).imag * (A).imag), \
(A).imag = 0.0)
/*
* Two macros to obtain the colpex conjugate of a number,
* The first one replace the given complex with the conjugate,
* the second sets A as the conjugate of B.
*/
#define C_CONJ(A) ((A).imag *= -1.0)
#define C_CONJEQ(A,B) { \
(A).real = (B.real); \
(A).imag = - (B.imag); \
}
/*
* Simple assignement
*/
#define C_EQ(A,B) { \
(A).real = (B.real); \
(A).imag = (B.imag); \
}
/*
* Normalization ???
*
*/
#define C_NORM(A,B) { \
if ((A).real == 0.0 && (A).imag == 0.0) { \
(B) = 0; \
} else { \
while (fabs((A).real) > 1.0 || fabs((A).imag) > 1.0) { \
(B) += 1; \
(A).real /= 2.0; \
(A).imag /= 2.0; \
} \
while (fabs((A).real) <= 0.5 && fabs((A).imag) <= 0.5) { \
(B) -= 1; \
(A).real *= 2.0; \
(A).imag *= 2.0; \
} \
} \
}
/*
* The magnitude of the complex number
*/
#define C_ABS(A) (sqrt((A).real * (A.real) + (A.imag * A.imag)))
/*
* Standard arithmetic between complex numbers
*
*/
#define C_MUL(A,B) { \
double TMP1, TMP2; \
TMP1 = (A.real); \
TMP2 = (B.real); \
(A).real = TMP1 * TMP2 - (A.imag) * (B.imag); \
(A).imag = TMP1 * (B.imag) + (A.imag) * TMP2; \
}
#define C_MULEQ(A,B,C) { \
(A).real = (B.real) * (C.real) - (B.imag) * (C.imag); \
(A).imag = (B.real) * (C.imag) + (B.imag) * (C.real); \
}
#define C_DIV(A,B) { \
double _tmp, _mag; \
_tmp = (A.real); \
(A).real = _tmp * (B.real) + (A).imag * (B.imag); \
(A).imag = - _tmp * (B.imag) + (A.imag) * (B.real); \
_mag = (B.real) * (B.real) + (B.imag) * (B.imag); \
(A).real /= _mag; \
(A).imag /= _mag; \
}
#define C_DIVEQ(A,B,C) { \
double _mag; \
(A).real = (B.real) * (C.real) + (B.imag) * (C.imag); \
(A).imag = (B.imag) * (C.real) - (B.real) * (C.imag) ; \
_mag = (C.real) * (C.real) + (C.imag) * (C.imag); \
(A).real /= _mag; \
(A).imag /= _mag; \
}
#define C_ADD(A,B) { \
(A).real += (B.real); \
(A).imag += (B.imag); \
}
#define C_ADDEQ(A,B,C) { \
(A).real = (B.real) + (C.real); \
(A).imag = (B.imag) + (C.imag); \
}
#define C_SUB(A,B) { \
(A).real -= (B.real); \
(A).imag -= (B.imag); \
}
#define C_SUBEQ(A,B,C) { \
(A).real = (B.real) - (C.real); \
(A).imag = (B.imag) - (C.imag); \
}
#endif /*CMPLX*/