r3: Initial revision
[ctsim.git] / include / kmath.h
1 /******************************************************************************
2  *
3  * FILE IDENTIFICATION
4  *
5  *      File Name:      NUMR.H
6  *      Author:         Kevin Rosenberg
7  *      Purpose:        Header file containing definitions for numerical app's
8  *      Date Started:   Nov 84
9  *
10  * DESCRIPTION
11  *
12  * MODIFICATION LOG
13  *
14  *****************************************************************************/
15
16 #ifndef _H_kmath
17 #define _H_kmath
18
19 #include <stdio.h>
20 #include <math.h>
21
22 #define PI      3.14159265358979323846
23 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
24 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
25 #define I_PI    0.31830988618379067154  /* Inverse of PI */
26 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
27  
28 #define TWOPI   6.28318530717958647692
29 #define SQRT2   1.414213562373095049
30
31 #define F_EPSILON       1.0E-6
32 #define D_EPSILON       1.0E-10
33
34 #define DEG_TO_RAD(x)   (x*(PI/180.))
35 #define RAD_TO_DEG(x)   (x*(180./PI))
36
37
38 #define ASSUMEDZERO  1E-10
39
40 /* codes for C data types */
41
42 #define DT_CHAR         1
43 #define DT_INT          2
44 #define DT_LONG         3
45 #define DT_FLOAT        4
46 #define DT_DOUBLE       5
47 #define DT_STRING  6
48
49 typedef char     *CMTX_1D;
50 typedef CMTX_1D  *CMTX_2D;
51 typedef CMTX_2D  *CMTX_3D;
52
53 typedef int      *IMTX_1D;
54 typedef IMTX_1D  *IMTX_2D;
55 typedef IMTX_2D  *IMTX_3D;
56
57 typedef float    *FMTX_1D;
58 typedef FMTX_1D  *FMTX_2D;
59 typedef FMTX_2D  *FMTX_3D;
60
61 typedef double   *DMTX_1D;
62 typedef DMTX_1D  *DMTX_2D;
63 typedef DMTX_2D  *DMTX_3D;
64
65 union elem_val_un {     /* holds an element value */
66         char      c;
67         int       i;
68         long int  l;
69         float     f;
70         double    d;
71 };
72
73 typedef union elem_val_un  MTX_ELEM_VAL;
74
75
76 union elem_ptr_un {     /* holds a pointer to a 1D vector of any type */
77         char      *c;
78         int       *i;
79         long int  *l;
80         float     *f;
81         double    *d;
82 };
83
84 typedef union elem_ptr_un  MTX_1D;
85 typedef MTX_1D            *MTX_2D;
86 typedef MTX_2D            *MTX_3D;
87
88 union mtx_val_ptr_un {                  /* pointer to matrix values */
89     MTX_1D  m1;
90     MTX_2D  m2;
91     MTX_3D  m3;
92 };
93
94 typedef union mtx_val_ptr_un  MTX_PTR;
95
96 struct matrix_st {
97     unsigned int order;                 /* order, or dimension, of matrix */
98     unsigned int elemtype;              /* element type */
99     unsigned int elemsize;              /* size of element in bytes */
100     unsigned int nx, ny, nz;            /* size of matrix in each dimension */
101     MTX_PTR val;                        /* pointer to matrix values */
102 };
103
104 typedef struct matrix_st MTX;
105 typedef struct matrix_st *MTXP;
106
107 /* DEFINITION IDENTIFICATION
108  *
109  *      Definitions to access a matrix element from an matrix
110  *
111
112 #define me1(mtx,x)\
113   (mtx->elemtype == DT_FLOAT ? mtx->val.m1.f[x] :\
114     (mtx->elemtype == DT_DOUBLE ? mtx->val.m1.d[x] :\
115       (mtx->elemtype == DT_INT ? mtx->val.m1.i[x] :\
116         (mtx->elemtype == DT_LONG ? mtx->val.m1.l[x] :\
117           (mtx->elemtype == DT_CHAR ? mtx->val.m1.c[x] :\
118              0\
119               )))))
120
121 #define me2(mtx,x,y)\
122   (mtx->elemtype == DT_FLOAT ? mtx->val.m2[x].f[y] :\
123     (mtx->elemtype == DT_DOUBLE ? mtx->val.m2[x].d[y] :\
124       (mtx->elemtype == DT_INT ? mtx->val.m2[x].i[y] :\
125         (mtx->elemtype == DT_LONG ? mtx->val.m2[x].l[y] :\
126           (mtx->elemtype == DT_CHAR ? mtx->val.m2[x].c[y] :\
127             0\
128               )))))
129
130
131 #define me3(mtx,x,y,z)\
132   (mtx->elemtype == DT_FLOAT ? mtx->val.m3[x][y].f[z] :\
133     (mtx->elemtype == DT_DOUBLE ? mtx->val.m3[x][y].d[z] :\
134       (mtx->elemtype == DT_INT ? mtx->val.m3[x][y].i[z] :\
135         (mtx->elemtype == DT_LONG ? mtx->val.m3[x][y].l[z] :\
136           (mtx->elemtype == DT_CHAR ? mtx->val.m3[x][y].c[z] :\
137             0\
138                )))))
139 */
140
141
142 /* calc_x.c */
143 int calc_x(double *x, const double xmin, const double xmax, const int n);
144 /* calcpoly.c */
145 void calcpoly(const double *a, const int degree, const double *x, double *y, const int n);
146 /* gauss.c */
147 int gauss(double **a, double *b, double *x, const int num);
148 /* lnearest.c */
149 long int lnearest(double x);
150 /* lsfit.c */
151 int lsfit(double *x, double *y, int n, int degree, double *coeff, FILE *fp);
152 /* matprt.c */
153 void matprt(const double **m, const int row, const int col, FILE *fp);
154 /* mtx_disp.c */
155 void mtx_show(const MTX *mtx);
156 void mtx_prt(const MTX *mtx, FILE *fp);
157 int mtx_prt_elem(const MTX *mtx, FILE *fp, unsigned int x, unsigned int y, unsigned int z);
158 /* mtx_elem.c */
159 int mtx_get_elem(const MTX *mtx, MTX_ELEM_VAL *me, const int x, const int y, const int z);
160 int mtx_put_elem(MTX *mtx, const MTX_ELEM_VAL *me, unsigned int x, unsigned int y, unsigned int z);
161 /* mtx_inp.c */
162 int mtx_inp_elem(const char *prompt, MTX_ELEM_VAL *mev, const int dtype);
163 /* mtx_main.c */
164 MTX *mtx_init(const unsigned int order, const unsigned int elem_type, const unsigned int nx, const unsigned int ny, const unsigned int nz);
165 MTX *mtx_clr(MTX *mtx);
166 int mtx_free(MTX *mtx);
167 int mtx_elem_size(const int dt);
168 int mtx_check(const MTX *mtx, const char *func_name);
169 /* norm_ang.c */
170 double norm_ang(double theta);
171 /* pntprt.c */
172 void pntprt(const double *x, const double *y, const int n, FILE *fp);
173 /* pntread.c */
174 int pntread(double *x, double *y, int *num, const int maxn, FILE *fp);
175 /* poly.c */
176 double poly(const double *a, const int degree, const double x);
177 /* simpson.c */
178 double simpson(const double xmin, const double xmax, const double *y, const int np);
179 /* stddev.c */
180 double stddev(const double *y1, const double *y2, const int n, const int df_lost);
181 /* vectprt.c */
182 void vectprt(const double *v, const int n, FILE *fp);
183 /* vectread.c */
184 int vectread(double *v, int *n, const int maxn, FILE *fp);
185 /* vectsort.c */
186 int vectsort(int num_vec, const int dtype, const int num_pts, char *vec1, char *vec2, char *vec3);
187
188 #endif