~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-libs/tina/math/mathMatr_get.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathMatr_get.c,v $
 37  * Date    :  $Date: 2008/12/07 02:27:36 $
 38  * Version :  $Revision: 1.7 $
 39  * CVS Id  :  $Id: mathMatr_get.c,v 1.7 2008/12/07 02:27:36 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Matrix element access (Default values are returned for locations
 44  *         outside matrix, accessing these locations is not an error.
 45  *
 46  *********
 47 */
 48 /** 
 49  *  @file
 50  *  @brief  Matrix element access.      
 51  *
 52  *  Get value at a given location in a matrix. Default values are returned for locations
 53  *  outside matrix, accessing these locations is not an error.
 54  * 
 55 */
 56 
 57 #include "mathMatr_get.h"
 58 
 59 #if HAVE_CONFIG_H
 60 #include <config.h>
 61 #endif
 62 
 63 #include <math.h>
 64 #include <stdio.h>
 65 #include <tina/sys/sysDef.h>
 66 #include <tina/sys/sysGen_error.h>
 67 #include <tina/math/math_MatrDef.h>
 68 #include <tina/math/math_TypPro.h>
 69 
 70 
 71 /** default values for locations outside matrix **/
 72 
 73 static int      mat_default_val = 0;
 74 static double   mat_default_fval = 0.0;
 75 static Complex  mat_default_zval = {Complex_id, 0.0, 0.0};
 76 static void    *mat_default_pval = NULL;
 77 
 78 /** functions to set default values **/
 79 
 80 void            matrix_set_default_val(int ival)
 81 {
 82         mat_default_val = ival;
 83 }
 84 
 85 void            matrix_set_default_fval(double fval)
 86 {
 87         mat_default_fval = fval;
 88 }
 89 
 90 void            matrix_set_default_zval(Complex zval)
 91 {
 92         mat_default_zval = zval;
 93 }
 94 
 95 void            matrix_set_default_pval(void *pval)
 96 {
 97         mat_default_pval = pval;
 98 }
 99 
100 double          mat_getf(Matrix * mat, int i, int j)
101 {
102         if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
103                 return (mat_default_fval);
104         return (mat->el.float_v[i][j]);
105 }
106 
107 /** gets integer value from matrix, casting if necessary **/
108 
109 int             matrix_get(Matrix * mat, int i, int j)
110 {
111         if (mat == NULL)
112                 return (mat_default_val);
113 
114         if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
115                 return (mat_default_val);
116 
117         switch (mat->shape)
118         {
119         case matrix_full:
120                 return (matrix_get_full(mat, i, j));
121         case matrix_lower:
122         case matrix_symmetric:
123                 if (i < j)
124                         SWAP(int, i, j);
125                 return (matrix_get_full(mat, i, j));
126         case matrix_upper:
127                 if (j < i)
128                         SWAP(int, i, j);
129                 return (matrix_get_full(mat, i, j));
130         }
131         return (mat_default_val);
132 }
133 
134 int             matrix_get_full(Matrix * mat, int i, int j)
135 {
136         int             val = 0;
137 
138         switch (mat->vtype)
139         {
140         case char_v:
141                 val = (int) mat->el.char_v[i][j];
142                 break;
143         case uchar_v:
144                 val = (int) mat->el.uchar_v[i][j];
145                 break;
146         case short_v:
147                 val = (int) mat->el.short_v[i][j];
148                 break;
149         case ushort_v:
150                 val = (int) mat->el.ushort_v[i][j];
151                 break;
152         case int_v:
153                 val = mat->el.int_v[i][j];
154                 break;
155         case uint_v:
156                 val = (int) mat->el.uint_v[i][j];
157                 break;
158         case float_v:
159                 val = (int) mat->el.float_v[i][j];
160                 break;
161         case double_v:
162                 val = (int) mat->el.double_v[i][j];
163                 break;
164         case complex_v:
165                 val = (int) mat->el.complex_v[i][j].x;
166                 break;
167         case ptr_v:
168                 if (mat->el.ptr_v[i][j] == NULL)
169                         val = 0;
170                 else
171                         val = 1;
172                 break;
173         default:
174                 error("matrix_get_full: unsupported type", non_fatal);
175                 break;
176         }
177         return (val);
178 }
179 
180 /** gets float value from matrix, casting if necessary **/
181 
182 double          matrix_getf(Matrix * mat, int i, int j)
183 {
184         if (mat == NULL)
185                 return (mat_default_fval);
186 
187         if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
188                 return (mat_default_fval);
189 
190         switch (mat->shape)
191         {
192         case matrix_full:
193                 return (matrix_getf_full(mat, i, j));
194         case matrix_lower:
195         case matrix_symmetric:
196                 if (i < j)
197                         SWAP(int, i, j);
198                 return (matrix_getf_full(mat, i, j));
199         case matrix_upper:
200                 if (j < i)
201                         SWAP(int, i, j);
202                 return (matrix_getf_full(mat, i, j));
203         }
204         return (mat_default_fval);
205 }
206 
207 double          matrix_getf_full(Matrix * mat, int i, int j)
208 {
209         double          val = 0.0;
210 
211         switch (mat->vtype)
212         {
213         case char_v:
214                 val = (double) mat->el.char_v[i][j];
215                 break;
216         case uchar_v:
217                 val = (double) mat->el.uchar_v[i][j];
218                 break;
219         case short_v:
220                 val = (double) mat->el.short_v[i][j];
221                 break;
222         case ushort_v:
223                 val = (double) mat->el.ushort_v[i][j];
224                 break;
225         case int_v:
226                 val = (double) mat->el.int_v[i][j];
227                 break;
228         case uint_v:
229                 val = (double) mat->el.uint_v[i][j];
230                 break;
231         case float_v:
232                 val = (double) mat->el.float_v[i][j];
233                 break;
234         case double_v:
235                 val = (double) mat->el.double_v[i][j];
236                 break;
237         case complex_v:
238                 val = (double) mat->el.complex_v[i][j].x;
239                 break;
240         case ptr_v:
241                 if (mat->el.ptr_v[i][j] == NULL)
242                         val = 0;
243                 else
244                         val = 1;
245                 break;
246         default:
247                 error("matrix_get_full: unsupported type", non_fatal);
248                 break;
249         }
250         return (val);
251 }
252 
253 /** gets complex value from matrix, casting if necessary **/
254 
255 Complex         matrix_getz(Matrix * mat, int i, int j)
256 {
257         if (mat == NULL)
258                 return (mat_default_zval);
259 
260         if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
261                 return (mat_default_zval);
262 
263         switch (mat->shape)
264         {
265         case matrix_full:
266                 return (matrix_getz_full(mat, i, j));
267         case matrix_lower:
268         case matrix_symmetric:
269                 if (i < j)
270                         SWAP(int, i, j);
271                 return (matrix_getz_full(mat, i, j));
272         case matrix_upper:
273                 if (j < i)
274                         SWAP(int, i, j);
275                 return (matrix_getz_full(mat, i, j));
276         }
277         return (mat_default_zval);
278 }
279 
280 Complex         matrix_getz_full(Matrix * mat, int i, int j)
281 {
282         Complex         val = {Complex_id};
283 
284         switch (mat->vtype)
285         {
286         case char_v:
287                 val = cmplx((double) mat->el.char_v[i][j], 0.0);
288                 break;
289         case uchar_v:
290                 val = cmplx((double) mat->el.uchar_v[i][j], 0.0);
291                 break;
292         case short_v:
293                 val = cmplx((double) mat->el.short_v[i][j], 0.0);
294                 break;
295         case ushort_v:
296                 val = cmplx((double) mat->el.ushort_v[i][j], 0.0);
297                 break;
298         case int_v:
299                 val = cmplx((double) mat->el.int_v[i][j], 0.0);
300                 break;
301         case uint_v:
302                 val = cmplx((double) mat->el.uint_v[i][j], 0.0);
303                 break;
304         case float_v:
305                 val = cmplx((double) mat->el.float_v[i][j], 0.0);
306                 break;
307         case double_v:
308                 val = cmplx((double) mat->el.double_v[i][j], 0.0);
309                 break;
310         case complex_v:
311                 val = mat->el.complex_v[i][j];
312                 break;
313         case ptr_v:
314                 if (mat->el.ptr_v[i][j] == NULL)
315                         val = cmplx_zero();
316                 else
317                         val = cmplx_unit();
318                 break;
319         default:
320                 error("matrix_get_full: unsupported type", non_fatal);
321                 break;
322         }
323         return (val);
324 }
325 
326 /** gets pointer value from matrix, casting if necessary **/
327 
328 void           *matrix_getp(Matrix * mat, int i, int j)
329 {
330         if (mat == NULL)
331                 return (mat_default_pval);
332 
333         if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
334                 return (mat_default_pval);
335 
336         switch (mat->shape)
337         {
338         case matrix_full:
339                 return (matrix_getp_full(mat, i, j));
340         case matrix_lower:
341         case matrix_symmetric:
342                 if (i < j)
343                         SWAP(int, i, j);
344                 return (matrix_getp_full(mat, i, j));
345         case matrix_upper:
346                 if (j < i)
347                         SWAP(int, i, j);
348                 return (matrix_getp_full(mat, i, j));
349         }
350         return (mat_default_pval);
351 }
352 
353 void           *matrix_getp_full(Matrix * mat, int i, int j)
354 {
355         void           *val;
356 
357         switch (mat->vtype)
358         {
359         case char_v:
360                 val = (void *) &(mat->el.char_v[i][j]);
361                 break;
362         case uchar_v:
363                 val = (void *) &(mat->el.uchar_v[i][j]);
364                 break;
365         case short_v:
366                 val = (void *) &(mat->el.short_v[i][j]);
367                 break;
368         case ushort_v:
369                 val = (void *) &(mat->el.ushort_v[i][j]);
370                 break;
371         case int_v:
372                 val = (void *) &(mat->el.int_v[i][j]);
373                 break;
374         case uint_v:
375                 val = (void *) &(mat->el.uint_v[i][j]);
376                 break;
377         case float_v:
378                 val = (void *) &(mat->el.float_v[i][j]);
379                 break;
380         case ptr_v:
381                 val = mat->el.ptr_v[i][j];
382                 break;
383         default:
384                 val = mat_default_pval;
385                 break;
386         }
387         return (val);
388 }
389 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.