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

Linux Cross Reference
Tina5/tina-libs/tina/math/mathMatr_sub.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_sub.c,v $
 37  * Date    :  $Date: 2005/01/09 17:49:25 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: mathMatr_sub.c,v 1.5 2005/01/09 17:49:25 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Matrix subtraction
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief  Subtract the elements in Matrix2 from the corresponding elements in Matrix1.        
 50  *
 51  *  Functions are of two main types;
 52  *
 53  *  - Those which return the difference as a new matrix.
 54  *  - Those which return the difference "inplace" of Matrix1.
 55 */
 56 
 57 #include "mathMatr_sub.h"
 58 
 59 #if HAVE_CONFIG_H
 60 #include <config.h>
 61 #endif
 62 
 63 #include <tina/sys/sysDef.h>
 64 #include <tina/sys/sysPro.h>
 65 #include <tina/math/math_MatrDef.h>
 66 #include <tina/math/mathMatr_get.h>
 67 #include <tina/math/mathMatr_put.h>
 68 #include <tina/math/mathMatr_alloc.h>
 69 #include <tina/math/mathMatr_copy.h>
 70 #include <tina/math/mathMatr_fill.h>
 71 #include <tina/math/mathMatr_minus.h>
 72 #include <tina/math/mathMatr_util.h>
 73 #include <tina/math/mathMatr_free.h>
 74 
 75 Matrix         *matrix_diff(Matrix * mat1, Matrix * mat2)
 76 {
 77         return (matrix_sub(mat1, mat2));
 78 }
 79 
 80 Matrix         *matrix_sub(Matrix * mat1, Matrix * mat2)
 81 {
 82         Matrix         *diff=NULL;
 83         Matrix         *m1=NULL;
 84         Matrix         *m2=NULL;
 85         Vartype         vtype;
 86 
 87         if (mat1 == NULL)
 88                 return (matrix_minus(mat2));
 89         if (mat2 == NULL)
 90                 return (matrix_copy(mat1));
 91 
 92         /** choose type of diff: e.g. int-float = float, etc. **/
 93 
 94         vtype = matrix_sup_vtype(mat1->vtype, mat2->vtype);
 95 
 96         m1 = matrix_cast_fill(mat1, vtype);
 97         m2 = matrix_cast_fill(mat2, vtype);
 98 
 99         switch (vtype)
100         {
101         case int_v:
102                 diff = imatrix_sub(m1, m2);
103                 break;
104         case float_v:
105                 diff = fmatrix_sub(m1, m2);
106                 break;
107         case double_v:
108                 diff = dmatrix_sub(m1, m2);
109                 break;
110         default:
111                 error("matrix_sub: unsupported type", non_fatal);
112                 break;
113         }
114 
115         /** free copies  **/
116 
117         matrix_free((Matrix *) m1);
118         matrix_free((Matrix *) m2);
119 
120         return (diff);
121 }
122 
123 Matrix         *imatrix_sub(Matrix * mat1, Matrix * mat2)
124 {
125         Matrix         *diff;
126         int           **el1, **el2, **el3;
127         int             m, n;
128         int             i, j;
129 
130         if (mat1 == NULL)
131                 return (matrix_minus(mat2));
132         if (mat2 == NULL)
133                 return (matrix_copy(mat1));
134 
135         if (mat1->m != mat2->m || mat1->n != mat2->n)
136         {
137                 error("imatrix_add: sizes not matched", warning);
138                 return (NULL);
139         }
140         m = mat1->m;
141         n = mat1->n;
142         diff = matrix_alloc(m, n, matrix_full, int_v);
143 
144         el1 = mat1->el.int_v;
145         el2 = mat2->el.int_v;
146         el3 = diff->el.int_v;
147 
148         for (i = 0; i < m; i++)
149                 for (j = 0; j < n; j++)
150                         el3[i][j] = el1[i][j] - el2[i][j];
151 
152         return (diff);
153 }
154 
155 Matrix         *fmatrix_sub(Matrix * mat1, Matrix * mat2)
156 {
157         Matrix         *diff;
158         float         **el1, **el2, **el3;
159         int             m, n;
160         int             i, j;
161 
162         if (mat1 == NULL)
163                 return (matrix_minus(mat2));
164         if (mat2 == NULL)
165                 return (matrix_copy(mat1));
166 
167         if (mat1->m != mat2->m || mat1->n != mat2->n)
168         {
169                 error("imatrix_add: sizes not matched", warning);
170                 return (NULL);
171         }
172         m = mat1->m;
173         n = mat1->n;
174         diff = matrix_alloc(m, n, matrix_full, float_v);
175 
176         el1 = mat1->el.float_v;
177         el2 = mat2->el.float_v;
178         el3 = diff->el.float_v;
179 
180         for (i = 0; i < m; i++)
181                 for (j = 0; j < n; j++)
182                         el3[i][j] = el1[i][j] - el2[i][j];
183 
184         return (diff);
185 }
186 
187 Matrix         *dmatrix_sub(Matrix * mat1, Matrix * mat2)
188 {
189         Matrix         *diff;
190         double        **el1, **el2, **el3;
191         int             m, n;
192         int             i, j;
193 
194         if (mat1 == NULL)
195                 return (matrix_minus(mat2));
196         if (mat2 == NULL)
197                 return (matrix_copy(mat1));
198 
199         if (mat1->m != mat2->m || mat1->n != mat2->n)
200         {
201                 error("imatrix_add: sizes not matched", warning);
202                 return (NULL);
203         }
204         m = mat1->m;
205         n = mat1->n;
206         diff = matrix_alloc(m, n, matrix_full, double_v);
207 
208         el1 = mat1->el.double_v;
209         el2 = mat2->el.double_v;
210         el3 = diff->el.double_v;
211 
212         for (i = 0; i < m; i++)
213                 for (j = 0; j < n; j++)
214                         el3[i][j] = el1[i][j] - el2[i][j];
215 
216         return (diff);
217 }
218 
219 Matrix         *imatrix_sub_inplace(Matrix * mat1, Matrix * mat2)
220 {
221         int           **el1, **el2;
222         int             m, n;
223         int             i, j;
224 
225         if (mat1 == NULL || mat2 == NULL)
226                 return (mat1);
227 
228         if (mat1->m != mat2->m || mat1->n != mat2->n)
229         {
230                 error("imatrix_add: sizes not matched", warning);
231                 return (mat1);
232         }
233         m = mat1->m;
234         n = mat1->n;
235 
236         el1 = mat1->el.int_v;
237         el2 = mat2->el.int_v;
238 
239         for (i = 0; i < m; i++)
240                 for (j = 0; j < n; j++)
241                         el1[i][j] = el1[i][j] - el2[i][j];
242 
243         return (mat1);
244 }
245 
246 Matrix         *fmatrix_sub_inplace(Matrix * mat1, Matrix * mat2)
247 {
248         float         **el1, **el2;
249         int             m, n;
250         int             i, j;
251 
252         if (mat1 == NULL || mat2 == NULL)
253                 return (mat1);
254 
255         if (mat1->m != mat2->m || mat1->n != mat2->n)
256         {
257                 error("imatrix_add: sizes not matched", warning);
258                 return (mat1);
259         }
260         m = mat1->m;
261         n = mat1->n;
262 
263         el1 = mat1->el.float_v;
264         el2 = mat2->el.float_v;
265 
266         for (i = 0; i < m; i++)
267                 for (j = 0; j < n; j++)
268                         el1[i][j] = el1[i][j] - el2[i][j];
269 
270         return (mat1);
271 }
272 
273 Matrix         *dmatrix_sub_inplace(Matrix * mat1, Matrix * mat2)
274 {
275         double        **el1, **el2;
276         int             m, n;
277         int             i, j;
278 
279         if (mat1 == NULL || mat2 == NULL)
280                 return (mat1);
281 
282         if (mat1->m != mat2->m || mat1->n != mat2->n)
283         {
284                 error("imatrix_add: sizes not matched", warning);
285                 return (mat1);
286         }
287         m = mat1->m;
288         n = mat1->n;
289 
290         el1 = mat1->el.double_v;
291         el2 = mat2->el.double_v;
292 
293         for (i = 0; i < m; i++)
294                 for (j = 0; j < n; j++)
295                         el1[i][j] = el1[i][j] - el2[i][j];
296 
297         return (mat1);
298 }
299 

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