Woolz Image Processing  Version 1.7.5
AlgType.h
Go to the documentation of this file.
1 #ifndef ALGTYPE_H
2 #define ALGTYPE_H
3 #if defined(__GNUC__)
4 #ident "University of Edinburgh $Id: a08b6cb07478909eef9d9f1671c6211bb8270c18 $"
5 #else
6 static char _AlgType_h[] = "University of Edinburgh $Id: a08b6cb07478909eef9d9f1671c6211bb8270c18 $";
7 #endif
8 /*!
9 * \file libAlg/AlgType.h
10 * \author Bill Hill
11 * \date March 1999
12 * \version $Id: a08b6cb07478909eef9d9f1671c6211bb8270c18 $
13 * \par
14 * Address:
15 * MRC Human Genetics Unit,
16 * MRC Institute of Genetics and Molecular Medicine,
17 * University of Edinburgh,
18 * Western General Hospital,
19 * Edinburgh, EH4 2XU, UK.
20 * \par
21 * Copyright (C), [2012],
22 * The University Court of the University of Edinburgh,
23 * Old College, Edinburgh, UK.
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 2
28 * of the License, or (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be
31 * useful but WITHOUT ANY WARRANTY; without even the implied
32 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
33 * PURPOSE. See the GNU General Public License for more
34 * details.
35 *
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
40 * \brief Type definitions for the Woolz numerical algorithm
41 * library.
42 * \ingroup Alg
43 */
44 
45 
46 #ifndef WLZ_EXT_BIND
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 #endif /* WLZ_EXT_BIND */
51 
52 /* Standard min, max, absolute value and nearest integer macros */
53 #define ALG_MAX(X,Y) (((X)>(Y))?(X):(Y))
54 #define ALG_MIN(X,Y) (((X)<(Y))?(X):(Y))
55 #define ALG_MAXIDX(X,Y) (((X)>(Y))?(0):(1))
56 #define ALG_MINIDX(X,Y) (((X)<(Y))?(0):(1))
57 #define ALG_ABS(X) (((X)>0)?(X):(-(X)))
58 #define ALG_NINT(X) ((int)(((X)<0)?((X)-(0.5)):((X)+(0.5))))
59 #define ALG_SQR(X) ((X)*(X))
60 #define ALG_MAX3(X,Y,Z) (((X)>(Y))?(((X)>(Z))?(X):(Z)):(((Y)>(Z))?(Y):(Z)))
61 #define ALG_MIN3(X,Y,Z) (((X)<(Y))?(((X)<(Z))?(X):(Z)):(((Y)<(Z))?(Y):(Z)))
62 #define ALG_MAXIDX3(X,Y,Z) \
63  (((X)>(Y))?(((X)>(Z))?(0):(3)):(((Y)>(Z))?(1):(3)))
64 #define ALG_MININD3(X,Y,Z) \
65  (((X)<(Y))?(((X)<(Z))?(0):(3)):(((Y)<(Z))?(1):(3)))
66 
67 /* Determinannts on 2x2 and 3x3 matrices as an ordered 1D array. */
68 #define ALG_DETERMINANT_4(M) \
69  ((((M)[0])*((M)[3]))-(((M)[1])*((M)[2])))
70 #define ALG_DETERMINANT_9(M) \
71  ((((M)[0])*((M)[4])*((M)[8]))-(((M)[0])*((M)[5])*((M)[7]))+ \
72  (((M)[1])*((M)[5])*((M)[6]))-(((M)[1])*((M)[3])*((M)[8]))+ \
73  (((M)[2])*((M)[3])*((M)[7]))-(((M)[2])*((M)[4])*((M)[6])))
74 
75 /* Standard math constants */
76 #define ALG_M_E (2.7182818284590452354)
77 #define ALG_M_LOG2E (1.4426950408889634074)
78 #define ALG_M_LOG10E (0.43429448190325182765)
79 #define ALG_M_LN2 (0.69314718055994530942)
80 #define ALG_M_LN10 (2.30258509299404568402)
81 #define ALG_M_PI (3.14159265358979323846)
82 #define ALG_M_PI_2 (1.57079632679489661923)
83 #define ALG_M_PI_4 (0.78539816339744830961)
84 #define ALG_M_1_PI (0.31830988618379067154)
85 #define ALG_M_2_PI (0.63661977236758134308)
86 #define ALG_M_2_SQRTPI (1.12837916709551257390)
87 #define ALG_M_SQRT2 (1.41421356237309504880)
88 #define ALG_M_SQRT3 (1.73205080756887729353)
89 #define ALG_M_SQRT1_2 (0.70710678118654752440)
90 
91 /* A tollerance value for double precission arithmetic. */
92 #define ALG_DBL_TOLLERANCE (1.0E-9)
93 
94 /*!
95 * \enum _AlgDistribution
96 * \brief Statistical distributions.
97 * Typedef: ::AlgDistribution.
98 */
99 typedef enum _AlgDistribution
100 {
106 
107 /*!
108 * \enum _AlgMatrixType
109 * \brief Matrix representations.
110 * Typedef: ::AlgMatrixType
111 */
112 typedef enum _AlgMatrixType
113 {
114  ALG_MATRIX_NULL = 0, /*!< A NULL matric with no elements. */
115  ALG_MATRIX_RECT, /*!< Rectangular matrix, with storage
116  for each element. These matrices should
117  be allocated using the libAlc array
118  allocation functions. */
119  ALG_MATRIX_SYM, /*!< Symmetric matrix, with storage
120  for the upper triangle only. These
121  matrices should be allocated using the
122  libAlc symmetric array allocation
123  functions. */
124  ALG_MATRIX_LLR /*!< Sparse matrix stored in linked list
125  row format. */
126 } AlgMatrixType;
127 
128 /*!
129 * \struct _AlgMatrix
130 * \brief A union of all valid matrix types.
131 * Typedef: ::AlgMatrix..
132 */
133 typedef union _AlgMatrix
134 {
139 } AlgMatrix;
140 
141 /*!
142 * \struct _AlgMatrixCore
143 * \brief A core matrix type with members common to all matrix types.
144 * Typedef: ::AlgMatrixCore.
145 */
146 typedef struct _AlgMatrixCore
147 {
148  AlgMatrixType type; /*!< Matrix type. */
149  size_t nR; /*!< Number of rows. */
150  size_t nC; /*!< Number of columns. */
151 } AlgMatrixCore;
152 
153 /*!
154 * \struct _AlgMatrixRect
155 * \brief Rectangular matrix.
156 * Typedef: ::AlgMatrixRect.
157 */
158 typedef struct _AlgMatrixRect
159 {
160  AlgMatrixType type; /*!< From AlgmatrixCore. */
161  size_t nR; /*!< From AlgmatrixCore. */
162  size_t nC; /*!< From AlgmatrixCore. */
163  size_t maxR; /*!< Rows space allocated for. */
164  size_t maxC; /*!< Columns space allocated for. */
165  double **array; /* Array of elements. */
166 } AlgMatrixRect;
167 
168 /*!
169 * \struct _AlgMatrixSym
170 * \brief Symmetric matrix.
171 * Typedef: ::AlgMatrixRect.
172 */
173 typedef struct _AlgMatrixSym
174 {
175  AlgMatrixType type; /*!< From AlgmatrixCore. */
176  size_t nR; /*!< From AlgmatrixCore. */
177  size_t nC; /*!< From AlgmatrixCore. */
178  size_t maxN; /*!< Max rows/columns space allocated for. */
179  double **array;
180 } AlgMatrixSym;
181 
182 /*!
183 * \struct _AlgMatrixLLRE
184 * \brief Entry in the linked list row matrix.
185 * Typedef: ::AlgMatrixLLRE.
186 */
187 typedef struct _AlgMatrixLLRE
188 {
189  size_t col; /*!< Column in matrix. */
190  double val; /*!< Value in the row, column. */
191  struct _AlgMatrixLLRE *nxt; /*!< Next entry either in the value list or
192  the free list. */
193 } AlgMatrixLLRE;
194 
195 /*!
196 * \struct _AlgMatrixLLRE
197 * \brief Linked list row matrix, in which the values are stored
198 * in linked lists, with a linked list for each row of
199 * the matrix. This can be very efficient if the matrix
200 * is very sparse, but is very ineffiecient if the matrix
201 * is dense. The cross over is around 5 percent values
202 * being non-zero. At 1 percent of values being non-zero
203 * the linked list matrix is faster than a rectangular matrix
204 * (eg for matrix multiplication) by about a factor of ten.
205 */
206 typedef struct _AlgMatrixLLR
207 {
208  AlgMatrixType type; /*!< From AlgmatrixCore. */
209  size_t nR; /*!< From AlgmatrixCore. */
210  size_t nC; /*!< From AlgmatrixCore. */
211  size_t numEnt; /*!< Number of (no-zero) entries. */
212  size_t maxEnt; /*!< Maximum number of entries. */
213  double tol; /*!< Lowest absolute non-zero value. */
214  void *blk; /*!< Stack of blocks of triples allocated
215  managed using AlcFreeStack. */
216  AlgMatrixLLRE *freeStk; /*!< Stack of free linked list entries. */
217  AlgMatrixLLRE **tbl; /*!< Table of matrix linked lists with a list
218  for each row of the matrix. */
219 } AlgMatrixLLR;
220 
221 typedef struct _AlgMatrixTriple
222 {
223  size_t row; /*!< Row in matrix. */
224  size_t col; /*!< Column in matrix. */
225  double val; /*!< Value in the row, column. */
227 
228 /*!
229 * \enum _AlgPadType
230 * \brief Types of daat padding.
231 * Typedef: ::AlgPadType.
232 */
233 typedef enum _AlgPadType
234 {
235  ALG_PAD_NONE, /*!< No padding, same as padding with zeros. */
236  ALG_PAD_ZERO, /*!< Pad data with zeros. */
237  ALG_PAD_END, /*!< Pad data with first/last data values. */
238  ALG_PAD_VALUE /*!< Pad data with given value. */
239 } AlgPadType;
240 
241 /*!
242 * \struct _ComplexD
243 * \brief Complex number data type.
244 * Typedef: ::ComplexD.
245 */
246 typedef struct _ComplexD
247 {
248  double re;
249  double im;
250 } ComplexD;
251 
252 
253 /*
254 * \enum _AlgError
255 * \brief Error codes.
256 * Typedef: ::AlgError.
257 */
258 typedef enum _AlgError
259 {
261  ALG_ERR_CONVERGENCE, /*!< Failure to converge. */
262  ALG_ERR_DIVZERO, /*!< Divide by zero. */
263  ALG_ERR_FUNC, /*!< Function parameters invalid. */
264  ALG_ERR_MALLOC, /*!< Memory allocation failure. */
265  ALG_ERR_MATRIX_CONDITION, /*!< Matrix condition number out of range. */
266  ALG_ERR_MATRIX_HOMOGENEOUS, /*!< Homogeneous matrix. */
267  ALG_ERR_MATRIX_SINGULAR, /*!< Singular matrix. */
268  ALG_ERR_MATRIX_TYPE, /*!< Invalid matrix type given. */
269  ALG_ERR_NONGLOBAL, /*!< Finds local solution, but fails to global
270  solution. */
271  ALG_ERR_READ, /*!< Read failure. */
272  ALG_ERR_WRITE, /*!< Write failure. */
274 } AlgError;
275 
276 /*
277 * \enum _AlgDbgMask
278 * \brief Debug mask values.
279 * Typedef: ::AlgDbgMask.
280 */
281 typedef enum _AlgDbgMask
282 {
285  ALG_DBG_LVL_2 = (1<<1),
286  ALG_DBG_LVL_3 = (1<<2),
287  ALG_DBG_LVL_FN = (1<<3)
288 } AlgDbgMask;
289 
290 #ifndef CTYPESGEN
291 typedef AlgError (*AlgDbgFn)(char *, ...);
292 
293 extern AlgDbgFn algDbgOutFn;
294 #define ALG_DBG_FN (*algDbgOutFn)
295 #define ALG_DBG(F,M) ((((F)&(algDbgMask))==(F))?ALG_DBG_FN M:ALG_ERR_NONE)
296 #endif
297 
298 #ifndef WLZ_EXT_BIND
299 #ifdef __cplusplus
300 }
301 #endif /* __cplusplus */
302 #endif /* WLZ_EXT_BIND */
303 
304 #endif /* ! ALGTYPE_H */
Definition: AlgType.h:238
double ** array
Definition: AlgType.h:165
Definition: AlgType.h:261
_AlgDbgMask
Definition: AlgType.h:281
struct _AlgMatrixCore * core
Definition: AlgType.h:135
AlgDbgFn algDbgOutFn
Definition: AlgDebug.c:49
size_t maxC
Definition: AlgType.h:164
size_t maxN
Definition: AlgType.h:178
AlgMatrixType type
Definition: AlgType.h:175
enum _AlgMatrixType AlgMatrixType
size_t col
Definition: AlgType.h:224
Definition: AlgType.h:263
Definition: AlgType.h:119
_AlgDistribution
Statistical distributions. Typedef: AlgDistribution.
Definition: AlgType.h:99
struct _AlgMatrixRect AlgMatrixRect
Definition: AlgType.h:236
Definition: AlgType.h:114
size_t nC
Definition: AlgType.h:177
size_t maxR
Definition: AlgType.h:163
Symmetric matrix. Typedef: AlgMatrixRect.
Definition: AlgType.h:173
AlgMatrixType type
Definition: AlgType.h:208
AlgError(* AlgDbgFn)(char *,...)
Definition: AlgType.h:291
double im
Definition: AlgType.h:249
enum _AlgDistribution AlgDistribution
Definition: AlgType.h:221
Definition: AlgType.h:115
size_t maxEnt
Definition: AlgType.h:212
Definition: AlgType.h:285
size_t nC
Definition: AlgType.h:150
Definition: AlgType.h:284
double ** array
Definition: AlgType.h:179
Definition: AlgType.h:264
size_t row
Definition: AlgType.h:223
struct _AlgMatrixSym * sym
Definition: AlgType.h:137
enum _AlgPadType AlgPadType
Definition: AlgType.h:237
size_t nR
Definition: AlgType.h:176
Definition: AlgType.h:267
Definition: AlgType.h:287
_AlgError
Definition: AlgType.h:258
Definition: AlgType.h:262
void * blk
Definition: AlgType.h:214
Definition: AlgType.h:266
Rectangular matrix. Typedef: AlgMatrixRect.
Definition: AlgType.h:158
Definition: AlgType.h:271
Definition: AlgType.h:124
union _AlgMatrix AlgMatrix
enum _AlgError AlgError
Definition: AlgType.h:273
struct _AlgMatrixTriple AlgMatrixTriple
AlgMatrixLLRE * freeStk
Definition: AlgType.h:216
Definition: AlgType.h:260
AlgMatrixType type
Definition: AlgType.h:160
Definition: AlgType.h:265
struct _AlgMatrixLLR * llr
Definition: AlgType.h:138
Definition: AlgType.h:286
Definition: AlgType.h:268
_AlgPadType
Types of daat padding. Typedef: AlgPadType.
Definition: AlgType.h:233
struct _AlgMatrixLLRE AlgMatrixLLRE
struct _AlgMatrixSym AlgMatrixSym
AlgMatrixType type
Definition: AlgType.h:148
Definition: AlgType.h:104
size_t nR
Definition: AlgType.h:209
Definition: AlgType.h:283
size_t nC
Definition: AlgType.h:210
double re
Definition: AlgType.h:248
double tol
Definition: AlgType.h:213
size_t nC
Definition: AlgType.h:162
struct _AlgMatrixLLRE * nxt
Definition: AlgType.h:191
A core matrix type with members common to all matrix types. Typedef: AlgMatrixCore.
Definition: AlgType.h:146
struct _AlgMatrixLLR AlgMatrixLLR
Definition: AlgType.h:102
Definition: AlgType.h:272
struct _AlgMatrixCore AlgMatrixCore
AlgMatrixLLRE ** tbl
Definition: AlgType.h:217
size_t nR
Definition: AlgType.h:149
_AlgMatrixType
Matrix representations. Typedef: AlgMatrixType.
Definition: AlgType.h:112
size_t col
Definition: AlgType.h:189
double val
Definition: AlgType.h:225
Entry in the linked list row matrix. Typedef: AlgMatrixLLRE.
Definition: AlgType.h:187
Definition: AlgType.h:269
Definition: AlgType.h:235
Complex number data type. Typedef: ComplexD.
Definition: AlgType.h:246
struct _AlgMatrixRect * rect
Definition: AlgType.h:136
size_t nR
Definition: AlgType.h:161
enum _AlgDbgMask AlgDbgMask
Definition: AlgType.h:103
size_t numEnt
Definition: AlgType.h:211
A union of all valid matrix types. Typedef: AlgMatrix..
Definition: AlgType.h:133
double val
Definition: AlgType.h:190
Definition: AlgType.h:206
struct _ComplexD ComplexD
Definition: AlgType.h:101