android: Update app-specific/MIME type icons
[LibreOffice.git] / basegfx / source / workbench / gauss.hxx
blob4ef050ccbc5250905d43531404727ad9925d5ec7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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.
23 @param matrix
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
27 row major order.
29 @param rows
30 Number of rows in matrix
32 @param cols
33 Number of columns in matrix
35 @param minPivot
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 #pragma once
44 template <class Matrix, typename BaseType>
45 bool eliminate( Matrix& matrix,
46 int rows,
47 int cols,
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)
54 /* find best pivot */
55 int max = i;
56 for(int j=i+1; j<rows; ++j)
57 if( fabs(matrix[ j*cols + i ]) > fabs(matrix[ max*cols + i ]) )
58 max = j;
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 */
78 return true;
81 /** Retrieve solution vector of linear system by substituting backwards.
83 This operation _relies_ on the previous successful
84 application of eliminate()!
86 @param matrix
87 Matrix in upper diagonal form, as e.g. generated by eliminate()
89 @param rows
90 Number of rows in matrix
92 @param cols
93 Number of columns in matrix
95 @param result
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
99 by zero occurred).
101 template <class Matrix, class Vector, typename BaseType>
102 bool substitute( const Matrix& matrix,
103 int rows,
104 int cols,
105 Vector& result )
107 BaseType temp;
109 /* j, k *must* be signed, when looping like: j>=0 ! */
110 /* substitute backwards */
111 for(int j=rows-1; j>=0; --j)
113 temp = 0.0;
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 */
124 return true;
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.
132 @param matrix
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
136 row major order.
138 @param rows
139 Number of rows in matrix
141 @param cols
142 Number of columns in matrix
144 @param minPivot
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,
152 int rows,
153 int cols,
154 Vector& result,
155 BaseType minPivot )
157 if( eliminate<Matrix,BaseType>(matrix, rows, cols, minPivot) )
158 return substitute<Matrix,Vector,BaseType>(matrix, rows, cols, result);
160 return false;
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */