diff --git a/matrix.hpp b/matrix.hpp deleted file mode 100644 index 89b77d85..00000000 --- a/matrix.hpp +++ /dev/null @@ -1,560 +0,0 @@ -/*************************************************************************** - * $Id: matrix.hpp,v 1.11 2006/05/13 10:05:53 nojhan Exp $ - * Copyright : Free Software Foundation - * Author : Johann Dréo - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#ifndef MATRIX -#define MATRIX - -#include -#include - -#include "Exception_oMetah.hpp" - -using namespace std; - -namespace ometah { - -//! Test if a vector is comprised in bounds -template -bool isInBounds( T aVector, T mins, T maxs) -{ - unsigned int i; - for(i=0; i maxs[i] ){ - return false; - } - } - return true; -} - - -//! Force a vector to be in bounds -template -T forceBounds( T aVector, T mins, T maxs) -{ - T CastedVector=aVector; - - unsigned int i; - for(i=0; i maxs[i] ){ - CastedVector[i]=maxs[i]; - } - } - return CastedVector; -} - -//! Create a 2D matrix filled with values -/* - if we want a vector > : - T stand for double - V stand for vector > -*/ -template -U matrixFilled( unsigned int dimL, unsigned int dimC, T fillValue ) -{ - unsigned int i; - - // make the vector possible at this step - typename U::value_type vec(dimC, fillValue); - - U mat; - for(i=0; i -vector > matrixFilled( unsigned int dimL, unsigned int dimC, T fillValue ) -{ - unsigned int i; - - // make the vector possible at this step - vector< T > vec(dimC, fillValue); - - vector > mat; - for(i=0; i -T multiply( T matA, T matB) -{ - - T newMat; - - unsigned int Al=matA.size(); - unsigned int Ac=matA[0].size(); - unsigned int Bl=matB.size(); - unsigned int Bc=matB[0].size(); - - newMat=matrixFilled( Al,Bc,0.0); - - if(Ac!=Bl) { - throw Exception_Size_Match("Cannot multiply matrices, sizes does not match", EXCEPTION_INFOS ); - } - - for( unsigned int i=0; i -U multiply(U aVector, T aNb) -{ - U res; - - res.reserve( aVector.size() ); - - unsigned int i; - for(i=0; i -T cholesky( T A) -{ - - // FIXME : vérifier que A est symétrique définie positive - - T B; - unsigned int Al=A.size(); - unsigned int Ac=A[0].size(); - B = matrixFilled(Al, Ac, 0.0); - - unsigned int i,j,k; - - // first column - i=0; - - // diagonal - j=0; - B[0][0]=sqrt(A[0][0]); - - // end of the column - for(j=1;j -T transpose( T &mat) -{ - unsigned int iSize=mat.size(); - unsigned int jSize=mat[0].size(); - - if ( iSize == 0 || jSize == 0 ) { - ostringstream msg; - msg << "ErrorSize: matrix not defined " - << "(iSize:" << iSize << ", jSize:" << jSize << ")"; - throw Exception_Size( msg.str(), EXCEPTION_INFOS ); - } - - typename T::value_type aVector; - T newMat; - - unsigned int i, j; - - for (j=0; j -vector mean( vector > mat) -{ - vector moyDim; - moyDim.reserve(mat.size()); - - unsigned int i,a; - a=mat.size(); - - for(i=0;i -T mean( vector aVector, unsigned int begin=0, unsigned int during=0) -{ - if (during==0) { - during = aVector.size() - begin; // if no end : take all - } - - T aSum, aMean; - - aSum = sum(aVector, begin, during); // Sum - aMean = aSum / (during - begin); // Mean - - return aMean; -} - -//! Calculate a variance-covariance matrix from a list of vector -/*! - For a population of p points on n dimensions : - if onRow==true, the matrix should have p rows and n columns. - if onRow==false, the matrix should have n rows and p columns. -*/ -template -U varianceCovariance( U pop, bool onRow = true) -{ -/* - // vector of means - typename U::value_type vecMeanCentered; - if(onRow) { - vecMeanCentered = mean( transpose(pop) ); // p rows and n columns => means of p - } else { - vecMeanCentered = mean( pop ); // n rows and p columns => means of n - } - - // centered population - // same size as the initial matrix - U popMeanCentered = matrixFilled(pop.size(),pop[0].size(), 0.0); - - // centering - // rows - for(unsigned int i=0;i covariance of p - } else { - popVar = multiply( popMeanCentered, popMeanCenteredT ); // if n rows and p columns => covariance of n - } - - // multiplication by 1/n : - for(unsigned int i=0;i -T sum(vector aVector, unsigned int begin=0, unsigned int during=0) -{ - if ( begin > aVector.size() || during > aVector.size() ) { - ostringstream msg; - msg << "ErrorSize: parameters are out of vector bounds " - << "(begin:" << begin << ", during:" << during - << ", size:" << aVector.size() << ")"; - throw Exception_Size_Index( msg.str(), EXCEPTION_INFOS ); - } - - if (during==0) { - during = aVector.size() - begin; - } - - T aSum=0; - - for (unsigned int j=begin; j -T stdev(vector aVector, unsigned int begin=0, unsigned int during=0) -{ - if ( begin > aVector.size() || during > aVector.size() ) { - ostringstream msg; - msg << "ErrorSize: parameters are out of vector bounds " - << "(begin:" << begin << ", during:" << during - << ", size:" << aVector.size() << ")"; - throw Exception_Size_Index( msg.str(), EXCEPTION_INFOS ); - } - - if (during==0) { - during = aVector.size() - begin; - } - - vector deviation; - T aMean, aDev, aStd; - - aMean = mean(aVector, begin, during); // mean - - for (unsigned int j=begin; j -typename T::value_type min(T aVector, unsigned int begin=0, unsigned int during=0) -{ - if ( begin > aVector.size() || during > aVector.size() ) { - ostringstream msg; - msg << "ErrorSize: parameters are out of vector bounds " - << "(begin:" << begin << ", during:" << during - << ", size:" << aVector.size() << ")"; - throw Exception_Size_Index( msg.str(), EXCEPTION_INFOS ); - } - - if (during==0) { - during = aVector.size() - begin; - } - - typename T::value_type aMin = aVector[begin]; - - for (unsigned int i=begin+1; i -vector mins(vector > aMatrix) -{ - vector mins; - - for( unsigned int i=0; i < aMatrix.size(); i++ ) { - mins.push_back( min(aMatrix[i]) ); - } - - return mins; -} - -//! Find the maximums values of a matrix, for each row -template -vector maxs(vector > aMatrix) -{ - vector maxs; - - for( unsigned int i=0; i < aMatrix.size(); i++ ) { - maxs.push_back( max(aMatrix[i]) ); - } - - return maxs; -} - -//! Find the maximum value of a vector -template -typename T::value_type max(T aVector, unsigned int begin=0, unsigned int during=0) -{ - if ( begin > aVector.size() || during > aVector.size() ) { - ostringstream msg; - msg << "ErrorSize: parameters are out of vector bounds " - << "(begin:" << begin << ", during:" << during - << ", size:" << aVector.size() << ")"; - throw Exception_Size_Index( msg.str(), EXCEPTION_INFOS ); - } - - if (during==0) { - during = aVector.size() - begin; - } - - typename T::value_type aMax = aVector[begin]; - - for (unsigned int i=begin+1; i aMax ) { - aMax = aVector[i]; - } - } - - return aMax; -} - -//! Substraction of two vectors, terms by terms -template -T substraction(T from, T that) -{ - T res; - - res.reserve(from.size()); - - for(unsigned int i=0; i -T addition(T from, T that) -{ - T res; - - res.reserve( from.size() ); - - for(unsigned int i=0; i -T absolute(T aVector) -{ - for(unsigned int i=0; i -vector gravityCenter( vector > points, vector weights ) -{ - - // if we have only one weight, we use it for all items - if ( weights.size() == 1 ) { - for ( unsigned int i=1; i < points.size(); i++ ) { - weights.push_back( weights[0] ); - } - } - - // if sizes does not match : error - if ( points.size() != weights.size() ) { - ostringstream msg; - msg << "ErrorSize: " - << "points size (" << points.size() << ")" - << " does not match weights size (" << weights.size() << ")"; - throw Exception_Size_Match( msg.str(), EXCEPTION_INFOS ); - } - - T weightsSum = sum(weights); - - vector > pointsT = transpose( points ); - - vector gravity; - - for ( unsigned int i=0; i < pointsT.size(); i++ ) { // dimensions - T g = 0; - for ( unsigned int j=0; j < pointsT[i].size(); j++ ) { // points - g += ( pointsT[i][j] * weights[j] ) / weightsSum; - } - gravity.push_back( g ); - } - - return gravity; -} - -} // ometah - -#endif // MATRIX