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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathMatr_times.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_times.c,v $
 37  * Date    :  $Date: 2005/01/09 17:49:25 $
 38  * Version :  $Revision: 1.6 $
 39  * CVS Id  :  $Id: mathMatr_times.c,v 1.6 2005/01/09 17:49:25 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Matrix multiplied by a scalar.
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief   Matrix multiplication by a scalar (double).        
 50  *
 51  *  NB, If the matrix is not already of type float or double, it is promoted/cast to 
 52  *  a float.
 53  * 
 54 */
 55 
 56 #include "mathMatr_times.h"
 57 
 58 #if HAVE_CONFIG_H
 59 #include <config.h>
 60 #endif
 61 
 62 #include <stdio.h>
 63 #include <tina/sys/sysDef.h>
 64 #include <tina/sys/sysGen_error.h>
 65 #include <tina/math/math_MatrDef.h>
 66 #include <tina/math/mathMatr_util.h>
 67 #include <tina/math/mathMatr_get.h>
 68 #include <tina/math/mathMatr_put.h>
 69 #include <tina/math/mathMatr_free.h>
 70 #include <tina/math/mathMatr_alloc.h>
 71 #include <tina/math/mathMatr_cast.h>
 72 
 73 
 74 Matrix         *matrix_times(double k, Matrix * mat)
 75 {
 76         Matrix         *prod=NULL;
 77         Matrix         *m=NULL;
 78         Vartype         vtype;
 79 
 80         if (mat == NULL)
 81                 return (NULL);
 82 
 83         vtype = matrix_sup_vtype(mat->vtype, float_v);
 84 
 85         if (mat->vtype != vtype)
 86                 m = matrix_cast(mat, vtype);
 87         else
 88                 m = mat;
 89 
 90         switch (vtype)
 91         {
 92         case float_v:
 93                 prod = fmatrix_times(k, m);
 94                 break;
 95         case double_v:
 96                 prod = dmatrix_times(k, m);
 97                 break;
 98         default:
 99                 error("matrix_times: unsupported type", non_fatal);
100                 break;
101         }
102         if (mat->vtype != vtype)
103                 matrix_free(m);
104 
105         return (prod);
106 }
107 
108 Matrix         *fmatrix_times(double k, Matrix * mat)
109 {
110         Matrix         *prod;
111         int             m = mat->m;
112         int             n = mat->n;
113         float         **el1 = mat->el.float_v;
114         float         **el2;
115         int             i, j;
116 
117         switch (mat->shape)
118         {
119         case matrix_full:
120                 prod = matrix_alloc(m, n, matrix_full, float_v);
121                 el2 = prod->el.float_v;
122                 for (i = 0; i < m; ++i)
123                         for (j = 0; j < n; ++j)
124                                 el2[i][j] = k * el1[i][j];
125                 break;
126         case matrix_lower:
127                 prod = matrix_alloc(m, n, matrix_lower, float_v);
128                 el2 = prod->el.float_v;
129                 for (i = 0; i < m; ++i)
130                         for (j = 0; j <= i; ++j)
131                                 el2[i][j] = k * el1[i][j];
132                 break;
133         case matrix_upper:
134                 prod = matrix_alloc(m, n, matrix_upper, float_v);
135                 el2 = prod->el.float_v;
136                 for (i = 0; i < m; ++i)
137                         for (j = i; j < n; ++j)
138                                 el2[i][j] = k * el1[i][j];
139                 break;
140         case matrix_symmetric:
141                 prod = matrix_alloc(m, n, matrix_symmetric, float_v);
142                 el2 = prod->el.float_v;
143                 for (i = 0; i < m; ++i)
144                         for (j = 0; j <= i; ++j)
145                                 el2[i][j] = k * el1[i][j];
146                 break;
147         default:
148                 prod = matrix_alloc(m, n, matrix_full, float_v);
149                 for (i = 0; i < m; ++i)
150                         for (j = 0; j < n; ++j)
151                                 matrix_putf(k * matrix_getf(mat, i, j), prod, i, j);
152                 break;
153         }
154 
155         return (prod);
156 }
157 
158 Matrix         *dmatrix_times(double k, Matrix * mat)
159 {
160         Matrix         *prod;
161         int             m = mat->m;
162         int             n = mat->n;
163         double        **el1 = mat->el.double_v;
164         double        **el2;
165         int             i, j;
166 
167         switch (mat->shape)
168         {
169         case matrix_full:
170                 prod = matrix_alloc(m, n, matrix_full, double_v);
171                 el2 = prod->el.double_v;
172                 for (i = 0; i < m; ++i)
173                         for (j = 0; j < n; ++j)
174                                 el2[i][j] = k * el1[i][j];
175                 break;
176         case matrix_lower:
177                 prod = matrix_alloc(m, n, matrix_lower, double_v);
178                 el2 = prod->el.double_v;
179                 for (i = 0; i < m; ++i)
180                         for (j = 0; j <= i; ++j)
181                                 el2[i][j] = k * el1[i][j];
182                 break;
183         case matrix_upper:
184                 prod = matrix_alloc(m, n, matrix_upper, double_v);
185                 el2 = prod->el.double_v;
186                 for (i = 0; i < m; ++i)
187                         for (j = i; j < n; ++j)
188                                 el2[i][j] = k * el1[i][j];
189                 break;
190         case matrix_symmetric:
191                 prod = matrix_alloc(m, n, matrix_symmetric, double_v);
192                 el2 = prod->el.double_v;
193                 for (i = 0; i < m; ++i)
194                         for (j = 0; j <= i; ++j)
195                                 el2[i][j] = k * el1[i][j];
196                 break;
197         default:
198                 prod = matrix_alloc(m, n, matrix_full, double_v);
199                 for (i = 0; i < m; ++i)
200                         for (j = 0; j < n; ++j)
201                                 matrix_putf(k * matrix_getf(mat, i, j), prod, i, j);
202                 break;
203         }
204 
205         return (prod);
206 }
207 

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