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

Linux Cross Reference
Tina5/tina-libs/tina/math/mathMatr_mult.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_mult.c,v $
 37  * Date    :  $Date: 2005/01/09 17:49:25 $
 38  * Version :  $Revision: 1.5 $
 39  * CVS Id  :  $Id: mathMatr_mult.c,v 1.5 2005/01/09 17:49:25 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Matrix multiplication (various types and shapes)
 44  *
 45  *********
 46 */
 47 
 48 /** 
 49  *  @file
 50  *  @brief  Matrix multiplication of one Matrix by another.     
 51  *
 52  *  Seems to be identical to mathMatr_prod.c
 53  * 
 54 */
 55 
 56 
 57 #include "mathMatr_mult.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_util.h>
 72 #include <tina/math/mathMatr_free.h>
 73 
 74 Matrix         *matrix_mult(Matrix * mat1, Matrix * mat2)
 75 {
 76         Matrix         *prod=NULL;
 77         Matrix         *m1=NULL;
 78         Matrix         *m2=NULL;
 79         Vartype         vtype;
 80 
 81         if (mat1 == NULL)
 82                 return (NULL);
 83         if (mat2 == NULL)
 84                 return (NULL);
 85 
 86         /** choose type of product: e.g. int*float = float, etc. **/
 87 
 88         vtype = matrix_sup_vtype(mat1->vtype, mat2->vtype);
 89 
 90         /** make copy of required type if necessary **/
 91 
 92         m1 = matrix_cast_fill(mat1, vtype);
 93         m2 = matrix_cast_fill(mat2, vtype);
 94 
 95         switch (vtype)
 96         {
 97         case int_v:
 98                 prod = imatrix_mult(m1, m2);
 99                 break;
100         case float_v:
101                 prod = fmatrix_mult(m1, m2);
102                 break;
103         case double_v:
104                 prod = dmatrix_mult(m1, m2);
105                 break;
106         default:
107                 error("matrix_mult: unsupported type", non_fatal);
108                 break;
109         }
110 
111         /** free copies **/
112 
113         matrix_free((Matrix *) m1);
114         matrix_free((Matrix *) m2);
115 
116         return (prod);
117 }
118 
119 Matrix         *imatrix_mult(Matrix * mat1, Matrix * mat2)
120 {
121         Matrix         *prod;
122         int           **el1, **el2, **el3;
123         int             sum;
124         int             m, n1, n;
125         int             i, j, k;
126 
127         if (mat1 == NULL || mat2 == NULL)
128                 return (NULL);
129 
130         if ((n1 = mat1->n) != mat2->m)
131         {
132                 error("imatrix_mult: sizes not matched", warning);
133                 return (NULL);
134         }
135         m = mat1->m;
136         n = mat2->n;
137         prod = matrix_alloc(m, n, matrix_full, int_v);
138 
139         el1 = mat1->el.int_v;
140         el2 = mat2->el.int_v;
141         el3 = prod->el.int_v;
142 
143         for (i = 0; i < m; i++)
144                 for (j = 0; j < n; j++)
145                 {
146                         sum = 0;
147                         for (k = 0; k < n1; k++)
148                                 sum += el1[i][k] * el2[k][j];
149                         el3[i][j] = sum;
150                 }
151 
152         return (prod);
153 }
154 
155 Matrix         *fmatrix_mult(Matrix * mat1, Matrix * mat2)
156 {
157         Matrix         *prod;
158         float         **el1, **el2, **el3;
159         double          sum;
160         int             m, n1, n;
161         int             i, j, k;
162 
163         if (mat1 == NULL || mat2 == NULL)
164                 return (NULL);
165 
166         if ((n1 = mat1->n) != mat2->m)
167         {
168                 error("imatrix_mult: sizes not matched", warning);
169                 return (NULL);
170         }
171         m = mat1->m;
172         n = mat2->n;
173         prod = matrix_alloc(m, n, matrix_full, float_v);
174 
175         el1 = mat1->el.float_v;
176         el2 = mat2->el.float_v;
177         el3 = prod->el.float_v;
178 
179         for (i = 0; i < m; i++)
180                 for (j = 0; j < n; j++)
181                 {
182                         sum = 0.0;
183                         for (k = 0; k < n1; k++)
184                                 sum += el1[i][k] * el2[k][j];
185                         el3[i][j] = (float) sum;
186                 }
187 
188         return (prod);
189 }
190 
191 Matrix         *dmatrix_mult(Matrix * mat1, Matrix * mat2)
192 {
193         Matrix         *prod;
194         double        **el1, **el2, **el3;
195         double          sum;
196         int             m, n1, n;
197         int             i, j, k;
198 
199         if (mat1 == NULL || mat2 == NULL)
200                 return (NULL);
201 
202         if ((n1 = mat1->n) != mat2->m)
203         {
204                 error("imatrix_mult: sizes not matched", warning);
205                 return (NULL);
206         }
207         m = mat1->m;
208         n = mat2->n;
209         prod = matrix_alloc(m, n, matrix_full, double_v);
210 
211         el1 = mat1->el.double_v;
212         el2 = mat2->el.double_v;
213         el3 = prod->el.double_v;
214 
215         for (i = 0; i < m; i++)
216                 for (j = 0; j < n; j++)
217                 {
218                         sum = 0;
219                         for (k = 0; k < n1; k++)
220                                 sum += el1[i][k] * el2[k][j];
221                         el3[i][j] = sum;
222                 }
223 
224         return (prod);
225 }
226 

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