LibreOffice Module basegfx (master) 1
gauss.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
42#pragma once
43
44template <class Matrix, typename BaseType>
45bool eliminate( Matrix& matrix,
46 int rows,
47 int cols,
48 const BaseType& minPivot )
49{
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)
53 {
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;
59
60 /* check pivot value */
61 if( fabs(matrix[ max*cols + i ]) < minPivot )
62 return false; /* pivot too small! */
63
64 /* interchange rows 'max' and 'i' */
65 for(int k=0; k<cols; ++k)
66 {
67 std::swap(matrix[ i*cols + k ], matrix[ max*cols + k ]);
68 }
69
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 ];
75 }
76
77 /* everything went well */
78 return true;
79}
80
101template <class Matrix, class Vector, typename BaseType>
102bool substitute( const Matrix& matrix,
103 int rows,
104 int cols,
105 Vector& result )
106{
107 BaseType temp;
108
109 /* j, k *must* be signed, when looping like: j>=0 ! */
110 /* substitute backwards */
111 for(int j=rows-1; j>=0; --j)
112 {
113 temp = 0.0;
114 for(int k=j+1; k<cols-1; ++k)
115 temp += matrix[ j*cols + k ] * result[k];
116
117 if( matrix[ j*cols + j ] == 0.0 )
118 return false; /* imminent division by zero! */
119
120 result[j] = (matrix[ j*cols + cols-1 ] - temp) / matrix[ j*cols + j ];
121 }
122
123 /* everything went well */
124 return true;
125}
126
150template <class Matrix, class Vector, typename BaseType>
151bool solve( Matrix& matrix,
152 int rows,
153 int cols,
154 Vector& result,
155 BaseType minPivot )
156{
157 if( eliminate<Matrix,BaseType>(matrix, rows, cols, minPivot) )
158 return substitute<Matrix,Vector,BaseType>(matrix, rows, cols, result);
159
160 return false;
161}
162
163/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define max(a, b)
bool substitute(const Matrix &matrix, int rows, int cols, Vector &result)
Retrieve solution vector of linear system by substituting backwards.
Definition: gauss.hxx:102
bool eliminate(Matrix &matrix, int rows, int cols, const BaseType &minPivot)
This method eliminates elements below main diagonal in the given matrix by gaussian elimination.
Definition: gauss.hxx:45
bool solve(Matrix &matrix, int rows, int cols, Vector &result, BaseType minPivot)
This method determines solution of given linear system, if any.
Definition: gauss.hxx:151
double matrix[4][4]
int i
Any result