r27: code cleaning
[ctsim.git] / include / kmath.h
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (C) 1983-2000 Kevin Rosenberg
4 **
5 **  $Id: kmath.h,v 1.5 2000/05/02 15:31:39 kevin Exp $
6 **  $Log: kmath.h,v $
7 **  Revision 1.5  2000/05/02 15:31:39  kevin
8 **  code cleaning
9 **
10 **  Revision 1.4  2000/04/30 19:17:35  kevin
11 **  Set up include files for conditional INTERACTIVE_GRAPHICS
12 **
13 **  Revision 1.3  2000/04/28 14:14:16  kevin
14 **  *** empty log message ***
15 **
16 **
17 **  This program is free software; you can redistribute it and/or modify
18 **  it under the terms of the GNU General Public License (version 2) as
19 **  published by the Free Software Foundation.
20 **
21 **  This program is distributed in the hope that it will be useful,
22 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
23 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 **  GNU General Public License for more details.
25 **
26 **  You should have received a copy of the GNU General Public License
27 **  along with this program; if not, write to the Free Software
28 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 ******************************************************************************/
30 /******************************************************************************
31  *
32  * PURPOSE
33  *      Header file containing definitions for numerical app's
34  *      Date Started:   Nov 84
35  *
36  *****************************************************************************/
37
38 #ifndef _H_kmath
39 #define _H_kmath
40
41 #include <stdio.h>
42 #include <math.h>
43
44 #define PI      3.14159265358979323846
45 #define HALFPI  1.57079632679489661923  /* PI divided by 2 */
46 #define QUARTPI 0.78539816339744830962  /* PI divided by 4 */
47 #define I_PI    0.31830988618379067154  /* Inverse of PI */
48 #define I_PID2  0.63661977236758134308  /* Inverse of PID2 */
49  
50 #define TWOPI   6.28318530717958647692
51 #define SQRT2   1.414213562373095049
52
53 #define F_EPSILON       1.0E-6
54 #define D_EPSILON       1.0E-10
55
56 #define DEG_TO_RAD(x)   (x*(PI/180.))
57 #define RAD_TO_DEG(x)   (x*(180./PI))
58
59
60 #define ASSUMEDZERO  1E-10
61
62 /* codes for C data types */
63
64 #define DT_CHAR         1
65 #define DT_INT          2
66 #define DT_LONG         3
67 #define DT_FLOAT        4
68 #define DT_DOUBLE       5
69 #define DT_STRING  6
70
71 typedef char     *CMTX_1D;
72 typedef CMTX_1D  *CMTX_2D;
73 typedef CMTX_2D  *CMTX_3D;
74
75 typedef int      *IMTX_1D;
76 typedef IMTX_1D  *IMTX_2D;
77 typedef IMTX_2D  *IMTX_3D;
78
79 typedef float    *FMTX_1D;
80 typedef FMTX_1D  *FMTX_2D;
81 typedef FMTX_2D  *FMTX_3D;
82
83 typedef double   *DMTX_1D;
84 typedef DMTX_1D  *DMTX_2D;
85 typedef DMTX_2D  *DMTX_3D;
86
87 typedef double GRFMTX_2D[3][3];
88 typedef double GRFMTX_3D[4][4];
89
90 union elem_val_un {     /* holds an element value */
91         char      c;
92         int       i;
93         long int  l;
94         float     f;
95         double    d;
96 };
97
98 typedef union elem_val_un  MTX_ELEM_VAL;
99
100
101 union elem_ptr_un {     /* holds a pointer to a 1D vector of any type */
102         char      *c;
103         int       *i;
104         long int  *l;
105         float     *f;
106         double    *d;
107 };
108
109 typedef union elem_ptr_un  MTX_1D;
110 typedef MTX_1D            *MTX_2D;
111 typedef MTX_2D            *MTX_3D;
112
113 union mtx_val_ptr_un {                  /* pointer to matrix values */
114     MTX_1D  m1;
115     MTX_2D  m2;
116     MTX_3D  m3;
117 };
118
119 typedef union mtx_val_ptr_un  MTX_PTR;
120
121 struct matrix_st {
122     unsigned int order;                 /* order, or dimension, of matrix */
123     unsigned int elemtype;              /* element type */
124     unsigned int elemsize;              /* size of element in bytes */
125     unsigned int nx, ny, nz;            /* size of matrix in each dimension */
126     MTX_PTR val;                        /* pointer to matrix values */
127 };
128
129 typedef struct matrix_st MTX;
130 typedef struct matrix_st *MTXP;
131
132 /* DEFINITION IDENTIFICATION
133  *
134  *      Definitions to access a matrix element from an matrix
135  *
136
137 #define me1(mtx,x)\
138   (mtx->elemtype == DT_FLOAT ? mtx->val.m1.f[x] :\
139     (mtx->elemtype == DT_DOUBLE ? mtx->val.m1.d[x] :\
140       (mtx->elemtype == DT_INT ? mtx->val.m1.i[x] :\
141         (mtx->elemtype == DT_LONG ? mtx->val.m1.l[x] :\
142           (mtx->elemtype == DT_CHAR ? mtx->val.m1.c[x] :\
143              0\
144               )))))
145
146 #define me2(mtx,x,y)\
147   (mtx->elemtype == DT_FLOAT ? mtx->val.m2[x].f[y] :\
148     (mtx->elemtype == DT_DOUBLE ? mtx->val.m2[x].d[y] :\
149       (mtx->elemtype == DT_INT ? mtx->val.m2[x].i[y] :\
150         (mtx->elemtype == DT_LONG ? mtx->val.m2[x].l[y] :\
151           (mtx->elemtype == DT_CHAR ? mtx->val.m2[x].c[y] :\
152             0\
153               )))))
154
155
156 #define me3(mtx,x,y,z)\
157   (mtx->elemtype == DT_FLOAT ? mtx->val.m3[x][y].f[z] :\
158     (mtx->elemtype == DT_DOUBLE ? mtx->val.m3[x][y].d[z] :\
159       (mtx->elemtype == DT_INT ? mtx->val.m3[x][y].i[z] :\
160         (mtx->elemtype == DT_LONG ? mtx->val.m3[x][y].l[z] :\
161           (mtx->elemtype == DT_CHAR ? mtx->val.m3[x][y].c[z] :\
162             0\
163                )))))
164 */
165
166
167 /* cliprect.c */
168 int cliprect(double *x1, double *y1, double *x2, double *y2, const double rect[4]);
169 /* lnearest.c */
170 long int lnearest(double x);
171 /* mtx_disp.c */
172 void mtx_show(const MTX *mtx);
173 void mtx_prt(const MTX *mtx, FILE *fp);
174 int mtx_prt_elem(const MTX *mtx, FILE *fp, unsigned int x, unsigned int y, unsigned int z);
175 /* mtx_elem.c */
176 int mtx_get_elem(const MTX *mtx, MTX_ELEM_VAL *me, const int x, const int y, const int z);
177 int mtx_put_elem(MTX *mtx, const MTX_ELEM_VAL *me, unsigned int x, unsigned int y, unsigned int z);
178 /* mtx_inp.c */
179 int mtx_inp_elem(const char *prompt, MTX_ELEM_VAL *mev, const int dtype);
180 /* mtx_main.c */
181 MTX *mtx_init(const unsigned int order, const unsigned int elem_type, const unsigned int nx, const unsigned int ny, const unsigned int nz);
182 MTX *mtx_clr(MTX *mtx);
183 int mtx_free(MTX *mtx);
184 int mtx_elem_size(const int dt);
185 int mtx_check(const MTX *mtx, const char *func_name);
186 /* norm_ang.c */
187 double norm_ang(double theta);
188 /* simpson.c */
189 double simpson(const double xmin, const double xmax, const double *y, const int np);
190
191 #endif