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