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