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

Linux Cross Reference
Tina4/src/math/matrix/mat_get.c

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

  1 /**@(#)Matrix element access (Default values are returned for locations
  2  * @(#)outside matrix, accessing these locations is not an error.
  3  */
  4 
  5 #include <math.h>
  6 #include <stdio.h>
  7 #include <tina/sys.h>
  8 #include <tina/math.h>
  9 #include <tina/mathfuncs.h>
 10 
 11 int     matrix_get_full(Matrix * mat, int i, int j);
 12 double  matrix_getf_full(Matrix * mat, int i, int j);
 13 Complex matrix_getz_full(Matrix * mat, int i, int j);
 14 void   *matrix_getp_full(Matrix * mat, int i, int j);
 15 Complex cmplx();
 16 Complex cmplx_zero();
 17 Complex cmplx_unit();
 18 
 19 /** default values for locations outside matrix **/
 20 
 21 static int mat_default_val = 0;
 22 static double mat_default_fval = 0.0;
 23 static Complex mat_default_zval = {Complex_id, 0.0, 0.0};
 24 static void *mat_default_pval = NULL;
 25 
 26 /** functions to set default values **/
 27 
 28 void    matrix_set_default_val(int ival)
 29 {
 30     mat_default_val = ival;
 31 }
 32 
 33 void    matrix_set_default_fval(double fval)
 34 {
 35     mat_default_fval = fval;
 36 }
 37 
 38 void    matrix_set_default_zval(Complex zval)
 39 {
 40     mat_default_zval = zval;
 41 }
 42 
 43 void    matrix_set_default_pval(void *pval)
 44 {
 45     mat_default_pval = pval;
 46 }
 47 
 48 double  mat_getf(Matrix * mat, int i, int j)
 49 {
 50     if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
 51         return (mat_default_fval);
 52     return (mat->el.float_v[i][j]);
 53 }
 54 
 55 /** gets integer value from matrix, casting if necessary **/
 56 
 57 int     matrix_get(Matrix * mat, int i, int j)
 58 {
 59     if (mat == NULL)
 60         return (mat_default_val);
 61 
 62     if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
 63         return (mat_default_val);
 64 
 65     switch (mat->shape)
 66     {
 67     case matrix_full:
 68         return (matrix_get_full(mat, i, j));
 69     case matrix_lower:
 70     case matrix_symmetric:
 71         if (i < j)
 72             SWAP(int, i, j);
 73         return (matrix_get_full(mat, i, j));
 74     case matrix_upper:
 75         if (j < i)
 76             SWAP(int, i, j);
 77         return (matrix_get_full(mat, i, j));
 78     }
 79     return (mat_default_val);
 80 }
 81 
 82 int     matrix_get_full(Matrix * mat, int i, int j)
 83 {
 84     int     val;
 85 
 86     switch (mat->vtype)
 87     {
 88     case char_v:
 89         val = (int) mat->el.char_v[i][j];
 90         break;
 91     case uchar_v:
 92         val = (int) mat->el.uchar_v[i][j];
 93         break;
 94     case short_v:
 95         val = (int) mat->el.short_v[i][j];
 96         break;
 97     case ushort_v:
 98         val = (int) mat->el.ushort_v[i][j];
 99         break;
100     case int_v:
101         val = mat->el.int_v[i][j];
102         break;
103     case uint_v:
104         val = (int) mat->el.uint_v[i][j];
105         break;
106     case float_v:
107         val = (int) mat->el.float_v[i][j];
108         break;
109     case double_v:
110         val = (int) mat->el.double_v[i][j];
111         break;
112     case complex_v:
113         val = (int) mat->el.complex_v[i][j].x;
114         break;
115     case ptr_v:
116         if (mat->el.ptr_v[i][j] == NULL)
117             val = 0;
118         else
119             val = 1;
120         break;
121     }
122     return (val);
123 }
124 
125 /** gets float value from matrix, casting if necessary **/
126 
127 double  matrix_getf(Matrix * mat, int i, int j)
128 {
129     if (mat == NULL)
130         return (mat_default_fval);
131 
132     if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
133         return (mat_default_fval);
134 
135     switch (mat->shape)
136     {
137     case matrix_full:
138         return (matrix_getf_full(mat, i, j));
139     case matrix_lower:
140     case matrix_symmetric:
141         if (i < j)
142             SWAP(int, i, j);
143         return (matrix_getf_full(mat, i, j));
144     case matrix_upper:
145         if (j < i)
146             SWAP(int, i, j);
147         return (matrix_getf_full(mat, i, j));
148     }
149     return (mat_default_fval);
150 }
151 
152 double  matrix_getf_full(Matrix * mat, int i, int j)
153 {
154     double  val;
155 
156     switch (mat->vtype)
157     {
158     case char_v:
159         val = (double) mat->el.char_v[i][j];
160         break;
161     case uchar_v:
162         val = (double) mat->el.uchar_v[i][j];
163         break;
164     case short_v:
165         val = (double) mat->el.short_v[i][j];
166         break;
167     case ushort_v:
168         val = (double) mat->el.ushort_v[i][j];
169         break;
170     case int_v:
171         val = (double) mat->el.int_v[i][j];
172         break;
173     case uint_v:
174         val = (double) mat->el.uint_v[i][j];
175         break;
176     case float_v:
177         val = (double) mat->el.float_v[i][j];
178         break;
179     case double_v:
180         val = (double) mat->el.double_v[i][j];
181         break;
182     case complex_v:
183         val = (double) mat->el.complex_v[i][j].x;
184         break;
185     case ptr_v:
186         if (mat->el.ptr_v[i][j] == NULL)
187             val = 0;
188         else
189             val = 1;
190         break;
191     }
192     return (val);
193 }
194 
195 /** gets complex value from matrix, casting if necessary **/
196 
197 Complex matrix_getz(Matrix * mat, int i, int j)
198 {
199     if (mat == NULL)
200         return (mat_default_zval);
201 
202     if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
203         return (mat_default_zval);
204 
205     switch (mat->shape)
206     {
207     case matrix_full:
208         return (matrix_getz_full(mat, i, j));
209     case matrix_lower:
210     case matrix_symmetric:
211         if (i < j)
212             SWAP(int, i, j);
213         return (matrix_getz_full(mat, i, j));
214     case matrix_upper:
215         if (j < i)
216             SWAP(int, i, j);
217         return (matrix_getz_full(mat, i, j));
218     }
219     return (mat_default_zval);
220 }
221 
222 Complex matrix_getz_full(Matrix * mat, int i, int j)
223 {
224     Complex val = {Complex_id};
225 
226     switch (mat->vtype)
227     {
228     case char_v:
229         val = cmplx((double) mat->el.char_v[i][j], 0.0);
230         break;
231     case uchar_v:
232         val = cmplx((double) mat->el.uchar_v[i][j], 0.0);
233         break;
234     case short_v:
235         val = cmplx((double) mat->el.short_v[i][j], 0.0);
236         break;
237     case ushort_v:
238         val = cmplx((double) mat->el.ushort_v[i][j], 0.0);
239         break;
240     case int_v:
241         val = cmplx((double) mat->el.int_v[i][j], 0.0);
242         break;
243     case uint_v:
244         val = cmplx((double) mat->el.uint_v[i][j], 0.0);
245         break;
246     case float_v:
247         val = cmplx((double) mat->el.float_v[i][j], 0.0);
248         break;
249     case double_v:
250         val = cmplx((double) mat->el.double_v[i][j], 0.0);
251         break;
252     case complex_v:
253         val = mat->el.complex_v[i][j];
254         break;
255     case ptr_v:
256         if (mat->el.ptr_v[i][j] == NULL)
257             val = cmplx_zero();
258         else
259             val = cmplx_unit();
260         break;
261     }
262     return (val);
263 }
264 
265 /** gets pointer value from matrix, casting if necessary **/
266 
267 void   *matrix_getp(Matrix * mat, int i, int j)
268 {
269     if (mat == NULL)
270         return (mat_default_pval);
271 
272     if (i < 0 || i >= mat->m || j < 0 || j >= mat->n)
273         return (mat_default_pval);
274 
275     switch (mat->shape)
276     {
277     case matrix_full:
278         return (matrix_getp_full(mat, i, j));
279     case matrix_lower:
280     case matrix_symmetric:
281         if (i < j)
282             SWAP(int, i, j);
283         return (matrix_getp_full(mat, i, j));
284     case matrix_upper:
285         if (j < i)
286             SWAP(int, i, j);
287         return (matrix_getp_full(mat, i, j));
288     }
289     return (mat_default_pval);
290 }
291 
292 void   *matrix_getp_full(Matrix * mat, int i, int j)
293 {
294     void   *val;
295 
296     switch (mat->vtype)
297     {
298         /* cast twice to avoid compiler warning: cast to pointer from
299          * integer of different size */
300     case char_v:
301         val = (void *) (int) mat->el.char_v[i][j];
302         break;
303     case uchar_v:
304         val = (void *) (int) mat->el.uchar_v[i][j];
305         break;
306     case short_v:
307         val = (void *) (int) mat->el.short_v[i][j];
308         break;
309     case ushort_v:
310         val = (void *) (int) mat->el.ushort_v[i][j];
311         break;
312     case int_v:
313         val = (void *) mat->el.int_v[i][j];
314         break;
315     case uint_v:
316         val = (void *) mat->el.uint_v[i][j];
317         break;
318     case ptr_v:
319         val = mat->el.ptr_v[i][j];
320         break;
321     default:
322         val = mat_default_pval;
323         break;
324     }
325     return (val);
326 }
327 

~ [ 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.