1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gauss.hxx,v $
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.
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
41 Number of rows in matrix
44 Number of columns in matrix
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
,
56 const BaseType
& minPivot
)
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
)
66 for(j
=i
+1; j
<rows
; ++j
)
67 if( fabs(matrix
[ j
*cols
+ i
]) > fabs(matrix
[ max
*cols
+ i
]) )
70 /* check pivot value */
71 if( fabs(matrix
[ max
*cols
+ i
]) < minPivot
)
72 return false; /* pivot too small! */
74 /* interchange rows 'max' and 'i' */
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 */
94 /** Retrieve solution vector of linear system by substituting backwards.
96 This operation _relies_ on the previous successful
97 application of eliminate()!
100 Matrix in upper diagonal form, as e.g. generated by eliminate()
103 Number of rows in matrix
106 Number of columns in matrix
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
114 template <class Matrix
, class Vector
, typename BaseType
>
115 bool substitute( const Matrix
& matrix
,
121 int j
,k
; /* *must* be signed, when looping like: j>=0 ! */
123 /* substitute backwards */
124 for(j
=rows
-1; j
>=0; --j
)
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 */
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.
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
153 Number of rows in matrix
156 Number of columns in matrix
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
,
171 if( eliminate
<Matrix
,BaseType
>(matrix
, rows
, cols
, minPivot
) )
172 return substitute
<Matrix
,Vector
,BaseType
>(matrix
, rows
, cols
, result
);