📄 cmath2.c
字号:
/**********Copyright 1990 Regents of the University of California. All rights reserved.Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group**********//* * Routines to do complex mathematical functions. These routines require * the -lm libraries. We sacrifice a lot of space to be able * to avoid having to do a seperate call for every vector element, * but it pays off in time savings. These routines should never * allow FPE's to happen. * * Complex functions are called as follows: * cx_something(data, type, length, &newlength, &newtype), * and return a char * that is cast to complex or double. */#include <ngspice.h>#include <cpdefs.h>#include <ftedefs.h>#include <dvec.h>#include "cmath.h"#include "cmath2.h"static double *d_tan(double *dd, int length){ double *d; int i; d = alloc_d(length); for (i = 0; i < length; i++) { rcheck(cos(degtorad(dd[i])) != 0, "tan"); d[i] = sin(degtorad(dd[i])) / cos(degtorad(dd[i])); } return d;}static complex *c_tan(complex *cc, int length){ complex *c; int i; c = alloc_c(length); for (i = 0; i < length; i++) { double u, v; rcheck(cos(degtorad(realpart(&cc[i]))) * cosh(degtorad(imagpart(&cc[i]))), "tan"); rcheck(sin(degtorad(realpart(&cc[i]))) * sinh(degtorad(imagpart(&cc[i]))), "tan"); u = degtorad(realpart(&cc[i])); v = degtorad(imagpart(&cc[i])); /* The Lattice C compiler won't take multi-line macros, and * CMS won't take >80 column lines.... */#define xx1 sin(u) * cosh(v)#define xx2 cos(u) * sinh(v)#define xx3 cos(u) * cosh(v)#define xx4 sin(u) * sinh(v) cdiv(xx1, xx2, xx3, xx4, realpart(&c[i]), imagpart(&c[i])); } return c;}void *cx_tan(void *data, short int type, int length, int *newlength, short int *newtype, ...){ *newlength = length; if (type == VF_REAL) { *newtype = VF_REAL; return (void *) d_tan((double *) data, length); } else { *newtype = VF_COMPLEX; return (void *) c_tan((complex *) data, length); }}void *cx_atan(void *data, short int type, int length, int *newlength, short int *newtype, ...){ double *d; d = alloc_d(length); *newtype = VF_REAL; *newlength = length; if (type == VF_COMPLEX) { complex *cc = (complex *) data; int i; for (i = 0; i < length; i++) d[i] = radtodeg(atan(realpart(&cc[i]))); } else { double *dd = (double *) data; int i; for (i = 0; i < length; i++) d[i] = radtodeg(atan(dd[i])); } return ((void *) d);}doublecx_max_local(void *data, short int type, int length){ double largest = 0.0; if (type == VF_COMPLEX) { complex *cc = (complex *) data; int i; for (i = 0; i < length; i++) if (cmag(&cc[i]) > largest) largest = cmag(&cc[i]); } else { double *dd = (double *) data; int i; for (i = 0; i < length; i++) if (FTEcabs(dd[i]) > largest) largest = FTEcabs(dd[i]); } return largest;}/* Normalize the data so that the magnitude of the greatest value is 1. */void *cx_norm(void *data, short int type, int length, int *newlength, short int *newtype, ...){ double largest = 0.0; largest = cx_max_local(data, type, length); if (largest == 0.0) { fprintf(cp_err, "Error: can't normalize a 0 vector\n"); return (NULL); } *newlength = length; if (type == VF_COMPLEX) { complex *c; complex *cc = (complex *) data; int i; c = alloc_c(length); *newtype = VF_COMPLEX; for (i = 0; i < length; i++) { realpart(&c[i]) = realpart(&cc[i]) / largest; imagpart(&c[i]) = imagpart(&cc[i]) / largest; } return ((void *) c); } else { double *d; double *dd = (double *) data; int i; d = alloc_d(length); *newtype = VF_REAL; for (i = 0; i < length; i++) d[i] = dd[i] / largest; return ((void *) d); }}void *cx_uminus(void *data, short int type, int length, int *newlength, short int *newtype, ...){ *newlength = length; if (type == VF_COMPLEX) { complex *c; complex *cc = (complex *) data; int i; c = alloc_c(length); *newtype = VF_COMPLEX; for (i = 0; i < length; i++) { realpart(&c[i]) = - realpart(&cc[i]); imagpart(&c[i]) = - imagpart(&cc[i]); } return ((void *) c); } else { double *d; double *dd = (double *) data; int i; d = alloc_d(length); *newtype = VF_REAL; for (i = 0; i < length; i++) d[i] = - dd[i]; return ((void *) d); }}void *cx_rnd(void *data, short int type, int length, int *newlength, short int *newtype, ...){ *newlength = length; if (type == VF_COMPLEX) { complex *c; complex *cc = (complex *) data; int i; c = alloc_c(length); *newtype = VF_COMPLEX; for (i = 0; i < length; i++) { int j, k; j = floor(realpart(&cc[i])); k = floor(imagpart(&cc[i])); realpart(&c[i]) = j ? random() % j : 0; imagpart(&c[i]) = k ? random() % k : 0; } return ((void *) c); } else { double *d; double *dd = (double *) data; int i; d = alloc_d(length); *newtype = VF_REAL; for (i = 0; i < length; i++) { int j; j = floor(dd[i]); d[i] = j ? random() % j : 0; } return ((void *) d); }}/* Compute the mean of a vector. */void *cx_mean(void *data, short int type, int length, int *newlength, short int *newtype, ...){ *newlength = 1; rcheck(length > 0, "mean"); if (type == VF_REAL) { double *d; double *dd = (double *) data; int i; d = alloc_d(1); *newtype = VF_REAL; for (i = 0; i < length; i++) *d += dd[i]; *d /= length; return ((void *) d); } else { complex *c; complex *cc = (complex *) data; int i; c = alloc_c(1); *newtype = VF_COMPLEX; for (i = 0; i < length; i++) { realpart(c) += realpart(cc + i); imagpart(c) += imagpart(cc + i); } realpart(c) /= length; imagpart(c) /= length; return ((void *) c); }}void *cx_length(void *data, short int type, int length, int *newlength, short int *newtype, ...){ double *d; *newlength = 1; *newtype = VF_REAL; d = alloc_d(1); *d = length; return ((void *) d);}/* Return a vector from 0 to the magnitude of the argument. Length of the * argument is irrelevent. */void *cx_vector(void *data, short int type, int length, int *newlength, short int *newtype, ...){ complex *cc = (complex *) data; double *dd = (double *) data; int i, len; double *d; if (type == VF_REAL) len = FTEcabs(*dd); else len = cmag(cc); if (len == 0) len = 1; d = alloc_d(len); *newlength = len; *newtype = VF_REAL; for (i = 0; i < len; i++) d[i] = i; return ((void *) d);}/* Create a vector of the given length composed of all ones. */void *cx_unitvec(void *data, short int type, int length, int *newlength, short int *newtype, ...){ complex *cc = (complex *) data; double *dd = (double *) data; int i, len; double *d; if (type == VF_REAL) len = FTEcabs(*dd); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -