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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathMatr_add.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_add.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathMatr_add.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :Matrix addition
 44  *
 45  *********
 46 */
 47 
 48 /** 
 49  *  @file
 50  *  @brief  Matrix addition of 2 matrices.      
 51  *
 52  *  File includes a top level matrix_sum function which calls lower level 
 53  *  functions to perform the calculations according to the type of the
 54  *  numbers in the matrices.
 55  * 
 56 */
 57 
 58 
 59 #include "mathMatr_add.h"
 60 
 61 #if HAVE_CONFIG_H
 62 #include <config.h>
 63 #endif
 64 
 65 
 66 #include <tina/sys/sysDef.h>
 67 #include <tina/sys/sysPro.h>
 68 #include <tina/math/math_MatrDef.h>
 69 #include <tina/math/mathMatr_alloc.h>
 70 #include <tina/math/mathMatr_copy.h>
 71 #include <tina/math/mathMatr_fill.h>
 72 #include <tina/math/mathMatr_util.h>
 73 #include <tina/math/mathMatr_free.h>
 74 
 75 Matrix         *matrix_sum(Matrix * mat1, Matrix * mat2)
 76 {
 77         Matrix         *sum = NULL;
 78         Matrix         *m1;
 79         Matrix         *m2;
 80         Vartype         vtype;
 81 
 82         if (mat1 == NULL)
 83                 return (matrix_copy(mat2));
 84         if (mat2 == NULL)
 85                 return (matrix_copy(mat1));
 86 
 87         /** choose type of sum: e.g. int+float = float, etc. **/
 88 
 89         vtype = matrix_sup_vtype(mat1->vtype, mat2->vtype);
 90 
 91         m1 = matrix_cast_fill(mat1, vtype);
 92         m2 = matrix_cast_fill(mat2, vtype);
 93 
 94         switch (vtype)
 95         {
 96         case int_v:
 97                 sum = imatrix_add(m1, m2);
 98                 break;
 99         case float_v:
100                 sum = fmatrix_add(m1, m2);
101                 break;
102         case double_v:
103                 sum = dmatrix_add(m1, m2);
104                 break;
105         default:
106                 break;
107         }
108 
109         /** free copies **/
110 
111         matrix_free(m1);
112         matrix_free(m2);
113 
114         return (sum);
115 }
116 
117 
118 Matrix         *matrix_add(Matrix * m1, Matrix * m2)
119 {
120         return (matrix_sum(m1, m2));
121 }
122 
123 Matrix         *imatrix_add(Matrix * mat1, Matrix * mat2)
124 {
125         Matrix         *sum = NULL;
126         int           **el1, **el2, **el3;
127         int             m, n;
128         int             i, j;
129 
130         if (mat1 == NULL)
131                 return (matrix_copy(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         sum = matrix_alloc(m, n, matrix_full, int_v);
143 
144         el1 = mat1->el.int_v;
145         el2 = mat2->el.int_v;
146         el3 = sum->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 (sum);
153 }
154 
155 Matrix         *fmatrix_add(Matrix * mat1, Matrix * mat2)
156 {
157         Matrix         *sum = NULL;
158         float         **el1, **el2, **el3;
159         int             m, n;
160         int             i, j;
161 
162         if (mat1 == NULL)
163                 return (matrix_copy(mat2));
164         if (mat2 == NULL)
165                 return (matrix_copy(mat1));
166 
167         if (mat1->m != mat2->m || mat1->n != mat2->n)
168         {
169                 error("fmatrix_add: sizes not matched", warning);
170                 return (NULL);
171         }
172         m = mat1->m;
173         n = mat1->n;
174         sum = matrix_alloc(m, n, matrix_full, float_v);
175 
176         el1 = mat1->el.float_v;
177         el2 = mat2->el.float_v;
178         el3 = sum->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 (sum);
185 }
186 
187 
188 Matrix         *dmatrix_add(Matrix * mat1, Matrix * mat2)
189 {
190         Matrix         *sum = NULL;
191         double        **el1, **el2, **el3;
192         int             m, n;
193         int             i, j;
194 
195         if (mat1 == NULL)
196                 return (matrix_copy(mat2));
197         if (mat2 == NULL)
198                 return (matrix_copy(mat1));
199 
200         if (mat1->m != mat2->m || mat1->n != mat2->n)
201         {
202                 error("imatrix_add: sizes not matched", warning);
203                 return (NULL);
204         }
205         m = mat1->m;
206         n = mat1->n;
207         sum = matrix_alloc(m, n, matrix_full, double_v);
208 
209         el1 = mat1->el.double_v;
210         el2 = mat2->el.double_v;
211         el3 = sum->el.double_v;
212 
213         for (i = 0; i < m; i++)
214                 for (j = 0; j < n; j++)
215                         el3[i][j] = el1[i][j] + el2[i][j];
216 
217         return (sum);
218 }
219 
220 
221 
222 Matrix         *imatrix_add_inplace(Matrix * mat1, Matrix * mat2)
223 {
224         int           **el1, **el2;
225         int             m, n;
226         int             i, j;
227 
228         if (mat1 == NULL || mat2 == NULL)
229                 return (mat1);
230 
231         if (mat1->m != mat2->m || mat1->n != mat2->n)
232         {
233                 error("imatrix_add: sizes not matched", warning);
234                 return (mat1);
235         }
236         m = mat1->m;
237         n = mat1->n;
238 
239         el1 = mat1->el.int_v;
240         el2 = mat2->el.int_v;
241 
242         for (i = 0; i < m; i++)
243                 for (j = 0; j < n; j++)
244                         el1[i][j] = el1[i][j] + el2[i][j];
245 
246         return (mat1);
247 }
248 
249 
250 Matrix         *fmatrix_add_inplace(Matrix * mat1, Matrix * mat2)
251 {
252         float         **el1, **el2;
253         int             m, n;
254         int             i, j;
255 
256         if (mat1 == NULL || mat2 == NULL)
257                 return (mat1);
258 
259         if (mat1->m != mat2->m || mat1->n != mat2->n)
260         {
261                 error("imatrix_add: sizes not matched", warning);
262                 return (mat1);
263         }
264         m = mat1->m;
265         n = mat1->n;
266 
267         el1 = mat1->el.float_v;
268         el2 = mat2->el.float_v;
269 
270         for (i = 0; i < m; i++)
271                 for (j = 0; j < n; j++)
272                         el1[i][j] = el1[i][j] + el2[i][j];
273 
274         return (mat1);
275 }
276 
277 Matrix         *dmatrix_add_inplace(Matrix * mat1, Matrix * mat2)
278 {
279         double        **el1, **el2;
280         int             m, n;
281         int             i, j;
282 
283         if (mat1 == NULL || mat2 == NULL)
284                 return (mat1);
285 
286         if (mat1->m != mat2->m || mat1->n != mat2->n)
287         {
288                 error("imatrix_add: sizes not matched", warning);
289                 return (mat1);
290         }
291         m = mat1->m;
292         n = mat1->n;
293 
294         el1 = mat1->el.double_v;
295         el2 = mat2->el.double_v;
296 
297         for (i = 0; i < m; i++)
298                 for (j = 0; j < n; j++)
299                         el1[i][j] = el1[i][j] + el2[i][j];
300 
301         return (mat1);
302 }
303 

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