1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 /** This method eliminates elements below main diagonal in the given 25 matrix by gaussian elimination. 26 27 @param matrix 28 The matrix to operate on. Last column is the result vector (right 29 hand side of the linear equation). After successful termination, 30 the matrix is upper triangular. The matrix is expected to be in 31 row major order. 32 33 @param rows 34 Number of rows in matrix 35 36 @param cols 37 Number of columns in matrix 38 39 @param minPivot 40 If the pivot element gets lesser than minPivot, this method fails, 41 otherwise, elimination succeeds and true is returned. 42 43 @return true, if elimination succeeded. 44 */ 45 template <class Matrix, typename BaseType> 46 bool eliminate( Matrix& matrix, 47 int rows, 48 int cols, 49 const BaseType& minPivot ) 50 { 51 BaseType temp; 52 int max, i, j, k; /* *must* be signed, when looping like: j>=0 ! */ 53 54 /* eliminate below main diagonal */ 55 for(i=0; i<cols-1; ++i) 56 { 57 /* find best pivot */ 58 max = i; 59 for(j=i+1; j<rows; ++j) 60 if( fabs(matrix[ j*cols + i ]) > fabs(matrix[ max*cols + i ]) ) 61 max = j; 62 63 /* check pivot value */ 64 if( fabs(matrix[ max*cols + i ]) < minPivot ) 65 return false; /* pivot too small! */ 66 67 /* interchange rows 'max' and 'i' */ 68 for(k=0; k<cols; ++k) 69 { 70 temp = matrix[ i*cols + k ]; 71 matrix[ i*cols + k ] = matrix[ max*cols + k ]; 72 matrix[ max*cols + k ] = temp; 73 } 74 75 /* eliminate column */ 76 for(j=i+1; j<rows; ++j) 77 for(k=cols-1; k>=i; --k) 78 matrix[ j*cols + k ] -= matrix[ i*cols + k ] * 79 matrix[ j*cols + i ] / matrix[ i*cols + i ]; 80 } 81 82 /* everything went well */ 83 return true; 84 } 85 86 87 /** Retrieve solution vector of linear system by substituting backwards. 88 89 This operation _relies_ on the previous successful 90 application of eliminate()! 91 92 @param matrix 93 Matrix in upper diagonal form, as e.g. generated by eliminate() 94 95 @param rows 96 Number of rows in matrix 97 98 @param cols 99 Number of columns in matrix 100 101 @param result 102 Result vector. Given matrix must have space for one column (rows entries). 103 104 @return true, if back substitution was possible (i.e. no division 105 by zero occured). 106 */ 107 template <class Matrix, class Vector, typename BaseType> 108 bool substitute( const Matrix& matrix, 109 int rows, 110 int cols, 111 Vector& result ) 112 { 113 BaseType temp; 114 int j,k; /* *must* be signed, when looping like: j>=0 ! */ 115 116 /* substitute backwards */ 117 for(j=rows-1; j>=0; --j) 118 { 119 temp = 0.0; 120 for(k=j+1; k<cols-1; ++k) 121 temp += matrix[ j*cols + k ] * result[k]; 122 123 if( matrix[ j*cols + j ] == 0.0 ) 124 return false; /* imminent division by zero! */ 125 126 result[j] = (matrix[ j*cols + cols-1 ] - temp) / matrix[ j*cols + j ]; 127 } 128 129 /* everything went well */ 130 return true; 131 } 132 133 134 /** This method determines solution of given linear system, if any 135 136 This is a wrapper for eliminate and substitute, given matrix must 137 contain right side of equation as the last column. 138 139 @param matrix 140 The matrix to operate on. Last column is the result vector (right 141 hand side of the linear equation). After successful termination, 142 the matrix is upper triangular. The matrix is expected to be in 143 row major order. 144 145 @param rows 146 Number of rows in matrix 147 148 @param cols 149 Number of columns in matrix 150 151 @param minPivot 152 If the pivot element gets lesser than minPivot, this method fails, 153 otherwise, elimination succeeds and true is returned. 154 155 @return true, if elimination succeeded. 156 */ 157 template <class Matrix, class Vector, typename BaseType> 158 bool solve( Matrix& matrix, 159 int rows, 160 int cols, 161 Vector& result, 162 BaseType minPivot ) 163 { 164 if( eliminate<Matrix,BaseType>(matrix, rows, cols, minPivot) ) 165 return substitute<Matrix,Vector,BaseType>(matrix, rows, cols, result); 166 167 return false; 168 } 169