sprint 1-alpha
|
00001 /****************************************************************************** 00002 * sprint::matrix_common 00003 * 00004 * Copyright (C) 2004-2011 Paolo Medici <www.pmx.it> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 * 00020 *******************************************************************************/ 00021 #ifndef _MATRIX_COMMON_H 00022 #define _MATRIX_COMMON_H 00023 00028 namespace sprint { 00029 00031 template<class T> 00032 void transpose(T & dst, const T & src) 00033 { 00034 for(unsigned int r =0; r <src.rows(); r++) 00035 for(unsigned int c =0; c <src.cols(); c++) 00036 dst(c,r) = src(r,c); 00037 } 00038 00040 template<typename A, typename B, typename C> 00041 bool multiply(C & out, const A & a, const B & b) 00042 { 00043 unsigned int N = a.cols(); 00044 typedef typename C::data_type T; 00045 if( 00046 (N!=b.rows()) || 00047 (out.rows()!=a.rows()) || 00048 (out.cols()!=b.cols()) 00049 ) 00050 return false; 00051 00052 for(unsigned int i=0;i<a.rows();i++) 00053 for(unsigned int j=0;j<b.cols();j++) 00054 { 00055 T sum = T(0); 00056 for(unsigned int k=0;k<N;k++) 00057 sum += a(i,k) * b(k,j); 00058 out(i,j) = sum; 00059 } 00060 00061 return true; 00062 } 00063 00064 } 00065 00066 #endif