1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir /** This method eliminates elements below main diagonal in the given 29*cdf0e10cSrcweir matrix by gaussian elimination. 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir @param matrix 32*cdf0e10cSrcweir The matrix to operate on. Last column is the result vector (right 33*cdf0e10cSrcweir hand side of the linear equation). After successful termination, 34*cdf0e10cSrcweir the matrix is upper triangular. The matrix is expected to be in 35*cdf0e10cSrcweir row major order. 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir @param rows 38*cdf0e10cSrcweir Number of rows in matrix 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir @param cols 41*cdf0e10cSrcweir Number of columns in matrix 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir @param minPivot 44*cdf0e10cSrcweir If the pivot element gets lesser than minPivot, this method fails, 45*cdf0e10cSrcweir otherwise, elimination succeeds and true is returned. 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir @return true, if elimination succeeded. 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir template <class Matrix, typename BaseType> 50*cdf0e10cSrcweir bool eliminate( Matrix& matrix, 51*cdf0e10cSrcweir int rows, 52*cdf0e10cSrcweir int cols, 53*cdf0e10cSrcweir const BaseType& minPivot ) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir BaseType temp; 56*cdf0e10cSrcweir int max, i, j, k; /* *must* be signed, when looping like: j>=0 ! */ 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /* eliminate below main diagonal */ 59*cdf0e10cSrcweir for(i=0; i<cols-1; ++i) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir /* find best pivot */ 62*cdf0e10cSrcweir max = i; 63*cdf0e10cSrcweir for(j=i+1; j<rows; ++j) 64*cdf0e10cSrcweir if( fabs(matrix[ j*cols + i ]) > fabs(matrix[ max*cols + i ]) ) 65*cdf0e10cSrcweir max = j; 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir /* check pivot value */ 68*cdf0e10cSrcweir if( fabs(matrix[ max*cols + i ]) < minPivot ) 69*cdf0e10cSrcweir return false; /* pivot too small! */ 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir /* interchange rows 'max' and 'i' */ 72*cdf0e10cSrcweir for(k=0; k<cols; ++k) 73*cdf0e10cSrcweir { 74*cdf0e10cSrcweir temp = matrix[ i*cols + k ]; 75*cdf0e10cSrcweir matrix[ i*cols + k ] = matrix[ max*cols + k ]; 76*cdf0e10cSrcweir matrix[ max*cols + k ] = temp; 77*cdf0e10cSrcweir } 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /* eliminate column */ 80*cdf0e10cSrcweir for(j=i+1; j<rows; ++j) 81*cdf0e10cSrcweir for(k=cols-1; k>=i; --k) 82*cdf0e10cSrcweir matrix[ j*cols + k ] -= matrix[ i*cols + k ] * 83*cdf0e10cSrcweir matrix[ j*cols + i ] / matrix[ i*cols + i ]; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir /* everything went well */ 87*cdf0e10cSrcweir return true; 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /** Retrieve solution vector of linear system by substituting backwards. 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir This operation _relies_ on the previous successful 94*cdf0e10cSrcweir application of eliminate()! 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir @param matrix 97*cdf0e10cSrcweir Matrix in upper diagonal form, as e.g. generated by eliminate() 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir @param rows 100*cdf0e10cSrcweir Number of rows in matrix 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir @param cols 103*cdf0e10cSrcweir Number of columns in matrix 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir @param result 106*cdf0e10cSrcweir Result vector. Given matrix must have space for one column (rows entries). 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir @return true, if back substitution was possible (i.e. no division 109*cdf0e10cSrcweir by zero occured). 110*cdf0e10cSrcweir */ 111*cdf0e10cSrcweir template <class Matrix, class Vector, typename BaseType> 112*cdf0e10cSrcweir bool substitute( const Matrix& matrix, 113*cdf0e10cSrcweir int rows, 114*cdf0e10cSrcweir int cols, 115*cdf0e10cSrcweir Vector& result ) 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir BaseType temp; 118*cdf0e10cSrcweir int j,k; /* *must* be signed, when looping like: j>=0 ! */ 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir /* substitute backwards */ 121*cdf0e10cSrcweir for(j=rows-1; j>=0; --j) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir temp = 0.0; 124*cdf0e10cSrcweir for(k=j+1; k<cols-1; ++k) 125*cdf0e10cSrcweir temp += matrix[ j*cols + k ] * result[k]; 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir if( matrix[ j*cols + j ] == 0.0 ) 128*cdf0e10cSrcweir return false; /* imminent division by zero! */ 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir result[j] = (matrix[ j*cols + cols-1 ] - temp) / matrix[ j*cols + j ]; 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir /* everything went well */ 134*cdf0e10cSrcweir return true; 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir /** This method determines solution of given linear system, if any 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir This is a wrapper for eliminate and substitute, given matrix must 141*cdf0e10cSrcweir contain right side of equation as the last column. 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir @param matrix 144*cdf0e10cSrcweir The matrix to operate on. Last column is the result vector (right 145*cdf0e10cSrcweir hand side of the linear equation). After successful termination, 146*cdf0e10cSrcweir the matrix is upper triangular. The matrix is expected to be in 147*cdf0e10cSrcweir row major order. 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir @param rows 150*cdf0e10cSrcweir Number of rows in matrix 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir @param cols 153*cdf0e10cSrcweir Number of columns in matrix 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir @param minPivot 156*cdf0e10cSrcweir If the pivot element gets lesser than minPivot, this method fails, 157*cdf0e10cSrcweir otherwise, elimination succeeds and true is returned. 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir @return true, if elimination succeeded. 160*cdf0e10cSrcweir */ 161*cdf0e10cSrcweir template <class Matrix, class Vector, typename BaseType> 162*cdf0e10cSrcweir bool solve( Matrix& matrix, 163*cdf0e10cSrcweir int rows, 164*cdf0e10cSrcweir int cols, 165*cdf0e10cSrcweir Vector& result, 166*cdf0e10cSrcweir BaseType minPivot ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir if( eliminate<Matrix,BaseType>(matrix, rows, cols, minPivot) ) 169*cdf0e10cSrcweir return substitute<Matrix,Vector,BaseType>(matrix, rows, cols, result); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir return false; 172*cdf0e10cSrcweir } 173