1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /** This method eliminates elements below main diagonal in the given
21 matrix by gaussian elimination.
24 The matrix to operate on. Last column is the result vector (right
25 hand side of the linear equation). After successful termination,
26 the matrix is upper triangular. The matrix is expected to be in
30 Number of rows in matrix
33 Number of columns in matrix
36 If the pivot element gets lesser than minPivot, this method fails,
37 otherwise, elimination succeeds and true is returned.
39 @return true, if elimination succeeded.
44 template <class Matrix
, typename BaseType
>
45 bool eliminate( Matrix
& matrix
,
48 const BaseType
& minPivot
)
50 /* i, j, k *must* be signed, when looping like: j>=0 ! */
51 /* eliminate below main diagonal */
52 for(int i
=0; i
<cols
-1; ++i
)
56 for(int j
=i
+1; j
<rows
; ++j
)
57 if( fabs(matrix
[ j
*cols
+ i
]) > fabs(matrix
[ max
*cols
+ i
]) )
60 /* check pivot value */
61 if( fabs(matrix
[ max
*cols
+ i
]) < minPivot
)
62 return false; /* pivot too small! */
64 /* interchange rows 'max' and 'i' */
65 for(int k
=0; k
<cols
; ++k
)
67 std::swap(matrix
[ i
*cols
+ k
], matrix
[ max
*cols
+ k
]);
70 /* eliminate column */
71 for(int j
=i
+1; j
<rows
; ++j
)
72 for(int k
=cols
-1; k
>=i
; --k
)
73 matrix
[ j
*cols
+ k
] -= matrix
[ i
*cols
+ k
] *
74 matrix
[ j
*cols
+ i
] / matrix
[ i
*cols
+ i
];
77 /* everything went well */
81 /** Retrieve solution vector of linear system by substituting backwards.
83 This operation _relies_ on the previous successful
84 application of eliminate()!
87 Matrix in upper diagonal form, as e.g. generated by eliminate()
90 Number of rows in matrix
93 Number of columns in matrix
96 Result vector. Given matrix must have space for one column (rows entries).
98 @return true, if back substitution was possible (i.e. no division
101 template <class Matrix
, class Vector
, typename BaseType
>
102 bool substitute( const Matrix
& matrix
,
109 /* j, k *must* be signed, when looping like: j>=0 ! */
110 /* substitute backwards */
111 for(int j
=rows
-1; j
>=0; --j
)
114 for(int k
=j
+1; k
<cols
-1; ++k
)
115 temp
+= matrix
[ j
*cols
+ k
] * result
[k
];
117 if( matrix
[ j
*cols
+ j
] == 0.0 )
118 return false; /* imminent division by zero! */
120 result
[j
] = (matrix
[ j
*cols
+ cols
-1 ] - temp
) / matrix
[ j
*cols
+ j
];
123 /* everything went well */
127 /** This method determines solution of given linear system, if any
129 This is a wrapper for eliminate and substitute, given matrix must
130 contain right side of equation as the last column.
133 The matrix to operate on. Last column is the result vector (right
134 hand side of the linear equation). After successful termination,
135 the matrix is upper triangular. The matrix is expected to be in
139 Number of rows in matrix
142 Number of columns in matrix
145 If the pivot element gets lesser than minPivot, this method fails,
146 otherwise, elimination succeeds and true is returned.
148 @return true, if elimination succeeded.
150 template <class Matrix
, class Vector
, typename BaseType
>
151 bool solve( Matrix
& matrix
,
157 if( eliminate
<Matrix
,BaseType
>(matrix
, rows
, cols
, minPivot
) )
158 return substitute
<Matrix
,Vector
,BaseType
>(matrix
, rows
, cols
, result
);
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */