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.
42 #ifndef INCLUDED_BASEGFX_SOURCE_WORKBENCH_GAUSS_HXX
43 #define INCLUDED_BASEGFX_SOURCE_WORKBENCH_GAUSS_HXX
45 template <class Matrix
, typename BaseType
>
46 bool eliminate( Matrix
& matrix
,
49 const BaseType
& minPivot
)
53 /* i, j, k *must* be signed, when looping like: j>=0 ! */
54 /* eliminate below main diagonal */
55 for(int i
=0; i
<cols
-1; ++i
)
59 for(int j
=i
+1; j
<rows
; ++j
)
60 if( fabs(matrix
[ j
*cols
+ i
]) > fabs(matrix
[ max
*cols
+ i
]) )
63 /* check pivot value */
64 if( fabs(matrix
[ max
*cols
+ i
]) < minPivot
)
65 return false; /* pivot too small! */
67 /* interchange rows 'max' and 'i' */
68 for(int k
=0; k
<cols
; ++k
)
70 temp
= matrix
[ i
*cols
+ k
];
71 matrix
[ i
*cols
+ k
] = matrix
[ max
*cols
+ k
];
72 matrix
[ max
*cols
+ k
] = temp
;
75 /* eliminate column */
76 for(int j
=i
+1; j
<rows
; ++j
)
77 for(int k
=cols
-1; k
>=i
; --k
)
78 matrix
[ j
*cols
+ k
] -= matrix
[ i
*cols
+ k
] *
79 matrix
[ j
*cols
+ i
] / matrix
[ i
*cols
+ i
];
82 /* everything went well */
86 /** Retrieve solution vector of linear system by substituting backwards.
88 This operation _relies_ on the previous successful
89 application of eliminate()!
92 Matrix in upper diagonal form, as e.g. generated by eliminate()
95 Number of rows in matrix
98 Number of columns in matrix
101 Result vector. Given matrix must have space for one column (rows entries).
103 @return true, if back substitution was possible (i.e. no division
106 template <class Matrix
, class Vector
, typename BaseType
>
107 bool substitute( const Matrix
& matrix
,
114 /* j, k *must* be signed, when looping like: j>=0 ! */
115 /* substitute backwards */
116 for(int j
=rows
-1; j
>=0; --j
)
119 for(int k
=j
+1; k
<cols
-1; ++k
)
120 temp
+= matrix
[ j
*cols
+ k
] * result
[k
];
122 if( matrix
[ j
*cols
+ j
] == 0.0 )
123 return false; /* imminent division by zero! */
125 result
[j
] = (matrix
[ j
*cols
+ cols
-1 ] - temp
) / matrix
[ j
*cols
+ j
];
128 /* everything went well */
132 /** This method determines solution of given linear system, if any
134 This is a wrapper for eliminate and substitute, given matrix must
135 contain right side of equation as the last column.
138 The matrix to operate on. Last column is the result vector (right
139 hand side of the linear equation). After successful termination,
140 the matrix is upper triangular. The matrix is expected to be in
144 Number of rows in matrix
147 Number of columns in matrix
150 If the pivot element gets lesser than minPivot, this method fails,
151 otherwise, elimination succeeds and true is returned.
153 @return true, if elimination succeeded.
155 template <class Matrix
, class Vector
, typename BaseType
>
156 bool solve( Matrix
& matrix
,
162 if( eliminate
<Matrix
,BaseType
>(matrix
, rows
, cols
, minPivot
) )
163 return substitute
<Matrix
,Vector
,BaseType
>(matrix
, rows
, cols
, result
);
168 #endif // INCLUDED_BASEGFX_SOURCE_WORKBENCH_GAUSS_HXX
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */