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

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

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