/usr/include/rheolef/tiny_lu.h is in librheolef-dev 6.6-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #ifndef _RHEO_TINY_LU_H
#define _RHEO_TINY_LU_H
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///
/// =========================================================================
#include "rheolef/tiny_matvec.h"
// first step: LU factorization
// ----------------------------
// with partial pivoting
//
// references :
// P. Lascaux, R. Theodor
// "Analyse numerique matricielle
// appliquee a l'art de l'ingenieur",
// page 242,
// Masson, 1986
//
namespace rheolef {
template <class T>
void
lu (tiny_matrix<T>& a, tiny_vector<size_t>& piv)
{
typedef size_t size_type;
const size_type n = a.nrow();
if (n == 0) return;
// initialize permutation table
for (size_type i = 0; i < n; i++)
piv [i] = i;
// factorize in 'n' steps
for (size_type k = 0; k < n-1; k++) {
// we search the largest element of th k-th
// line, that has not yet been pivot-line
T amax = abs(a(piv[k],k));
size_type jmax = k;
for (size_type i = k+1; i < n; i++) {
if (abs(a(piv[i],k)) > amax) {
amax = abs(a(piv[i],k));
jmax = i;
}
}
// largest element is in piv[jmax] line
// we permut indexes
size_type i = piv [k];
piv [k] = piv [jmax];
piv [jmax] = i;
// and invert the pivot
if (1 + a(piv[k],k) == 1) { // a (piv[k],k) < zero machine
error_macro ("lu: unisolvence failed on pivot " << k);
}
T pivinv = 1./a(piv[k],k);
// modify lines that has not yet been
// pivot-lines
for (size_type i = k+1; i < n; i++) {
T c = a(piv[i],k) * pivinv;
a(piv[i],k) = c;
for (size_type j = k+1; j < n; j++)
a(piv [i],j) -= c * a(piv[k],j);
}
}
}
// second step: one-column resolution
// ----------------------------------
template <class T>
void
solve (tiny_matrix<T>& a, tiny_vector<size_t>& piv,
const tiny_vector<T>& b, tiny_vector<T>& x)
{
typedef size_t size_type;
const size_type n = a.nrow();
if (n == 0) return;
// solve Ly = piv(b); y is stored in x
for (size_type i = 0; i < n; i++) {
T c = 0;
for (size_type j = 0; j < i; j++)
c += a(piv[i],j) * x [j];
x [i] = b [piv[i]] - c;
}
// solve Ux = y; x contains y as input and x as output
for (int i = n-1; i >= 0; i--) {
T c = 0;
for (size_type j = i+1; j < n; j++)
c += a(piv[i],j) * x [j];
x [i] = (x [i] - c) / a(piv[i],i);
}
}
// ---------------------------------
// third step : matrix inversion
// NOTE: the a matrix is destroyed !
// ---------------------------------
template <class T>
void
invert (tiny_matrix<T>& a, tiny_matrix<T>& inv_a)
{
typedef size_t size_type;
const size_type n = a.nrow();
// performs the gauss factorization: M = L.U
tiny_vector<size_t> piv (n);
lu (a, piv);
// invert M in B, column by colomn
tiny_vector<T> column (n);
tiny_vector<T> x (n);
inv_a.resize (n,n);
for (size_type j = 0; j < n; j++) {
for (size_type i = 0; i < n; i++)
column [i] = 0;
column [j] = 1;
solve (a, piv, column, x);
for (size_type i = 0; i < n; i++)
inv_a (i,j) = x [i];
}
}
template <class T>
void
put (std::ostream& out, std::string name, const tiny_matrix<T>& a)
{
typedef size_t size_type;
out << name << "(" << a.nrow() << "," << a.ncol() << ")" << std::endl;
for (size_type i = 0; i < a.nrow(); i++) {
for (size_type j = 0; j < a.ncol(); j++) {
out << name << "(" << i << "," << j << ") = " << a(i,j) << std::endl;
}
}
}
}// namespace rheolef
#endif // _RHEO_TINY_LU_H
|