/usr/include/deal.II/lac/householder.h is in libdeal.ii-dev 6.3.1-1.1.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | //---------------------------------------------------------------------------
// $Id: householder.h 20287 2010-01-05 10:16:05Z janssen $
// Version: $Name$
//
// Copyright (C) 2005, 2006, 2007, 2008 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__householder_h
#define __deal2__householder_h
#include <cmath>
#include <base/config.h>
#include <lac/full_matrix.h>
#include <lac/vector_memory.h>
#include <vector>
DEAL_II_NAMESPACE_OPEN
// forward declarations
template<typename number> class Vector;
/*! @addtogroup Matrix2
*@{
*/
/**
* QR-decomposition of a full matrix.
*
* Whenever an object of this class is created, it copies the matrix
* given and computes its QR-decomposition by Householder
* algorithm. Then, the function least_squares() can be used to
* compute the vector <i>x</i> minimizing <i>||Ax-b||</i> for a given
* vector <i>b</i>.
*
* @note Instantiations for this template are provided for <tt>@<float@> and
* @<double@></tt>; others can be generated in application programs (see the
* section on @ref Instantiations in the manual).
*
* @author Guido Kanschat, 2005
*/
template<typename number>
class Householder : private FullMatrix<number>
{
public:
/**
* Create an empty object.
*/
Householder ();
/**
* Create an object holding the
* QR-decomposition of a matrix.
*/
template<typename number2>
Householder (const FullMatrix<number2>&);
/**
* Compute the QR-decomposition
* of another matrix.
*/
template<typename number2>
void
initialize (const FullMatrix<number2>&);
/**
* Solve the least-squares
* problem for the right hand
* side <tt>src</tt>. The return
* value is the Euclidean norm of
* the approximation error.
*
* @arg @c dst contains the
* solution of the least squares
* problem on return.
*
* @arg @c src contains the right
* hand side <i>b</i> of the
* least squares problem. It will
* be changed during the algorithm
* and is unusable on return.
*/
template<typename number2>
double least_squares (Vector<number2> &dst,
const Vector<number2> &src) const;
/**
* This function does the same as
* the one for BlockVectors.
*/
template<typename number2>
double least_squares (BlockVector<number2> &dst,
const BlockVector<number2> &src) const;
private:
/**
* Storage for the diagonal
* elements of the orthogonal
* transformation.
*/
std::vector<number> diagonal;
};
/*@}*/
#ifndef DOXYGEN
/*-------------------------Inline functions -------------------------------*/
// QR-transformation cf. Stoer 1 4.8.2 (p. 191)
template <typename number>
Householder<number>::Householder()
{}
template <typename number>
template <typename number2>
void
Householder<number>::initialize(const FullMatrix<number2>& M)
{
const unsigned int m = M.n_rows(), n = M.n_cols();
this->reinit(m, n);
this->fill(M);
Assert (!this->empty(), typename FullMatrix<number2>::ExcEmptyMatrix());
diagonal.resize(m);
// m > n, src.n() = m
Assert (this->n_cols() <= this->n_rows(),
ExcDimensionMismatch(this->n_cols(), this->n_rows()));
for (unsigned int j=0 ; j<n ; ++j)
{
number2 sigma = 0;
unsigned int i;
// sigma = ||v||^2
for (i=j ; i<m ; ++i)
sigma += this->el(i,j)*this->el(i,j);
// We are ready if the column is
// empty. Are we?
if (std::fabs(sigma) < 1.e-15) return;
number2 s = (this->el(j,j) < 0) ? std::sqrt(sigma) : -std::sqrt(sigma);
//
number2 beta = std::sqrt(1./(sigma-s*this->el(j,j)));
// Make column j the Householder
// vector, store first entry in
// diagonal
diagonal[j] = beta*(this->el(j,j) - s);
this->el(j,j) = s;
for (i=j+1 ; i<m ; ++i)
this->el(i,j) *= beta;
// For all subsequent columns do
// the Householder reflexion
for (unsigned int k=j+1 ; k<n ; ++k)
{
number2 sum = diagonal[j]*this->el(j,k);
for (i=j+1 ; i<m ; ++i)
sum += this->el(i,j)*this->el(i,k);
this->el(j,k) -= sum*this->diagonal[j];
for (i=j+1 ; i<m ; ++i)
this->el(i,k) -= sum*this->el(i,j);
}
}
}
template <typename number>
template <typename number2>
Householder<number>::Householder(const FullMatrix<number2>& M)
{
initialize(M);
}
template <typename number>
template <typename number2>
double
Householder<number>::least_squares (Vector<number2>& dst,
const Vector<number2>& src) const
{
Assert (!this->empty(), typename FullMatrix<number2>::ExcEmptyMatrix());
AssertDimension(dst.size(), this->n());
AssertDimension(src.size(), this->m());
const unsigned int m = this->m(), n = this->n();
GrowingVectorMemory<Vector<number2> > mem;
Vector<number2>* aux = mem.alloc();
aux->reinit(src, true);
*aux = src;
// m > n, m = src.n, n = dst.n
// Multiply Q_n ... Q_2 Q_1 src
// Where Q_i = I-v_i v_i^T
for (unsigned int j=0;j<n;++j)
{
// sum = v_i^T dst
number2 sum = diagonal[j]* (*aux)(j);
for (unsigned int i=j+1 ; i<m ; ++i)
sum += this->el(i,j)*(*aux)(i);
// dst -= v * sum
(*aux)(j) -= sum*diagonal[j];
for (unsigned int i=j+1 ; i<m ; ++i)
(*aux)(i) -= sum*this->el(i,j);
}
// Compute norm of residual
number2 sum = 0.;
for (unsigned int i=n ; i<m ; ++i)
sum += (*aux)(i) * (*aux)(i);
// Compute solution
this->backward(dst, *aux);
mem.free(aux);
return std::sqrt(sum);
}
template <typename number>
template <typename number2>
double
Householder<number>::least_squares (BlockVector<number2>& dst,
const BlockVector<number2>& src) const
{
Assert (!this->empty(), typename FullMatrix<number2>::ExcEmptyMatrix());
AssertDimension(dst.size(), this->n());
AssertDimension(src.size(), this->m());
const unsigned int m = this->m(), n = this->n();
GrowingVectorMemory<BlockVector<number2> > mem;
BlockVector<number2>* aux = mem.alloc();
aux->reinit(src, true);
*aux = src;
// m > n, m = src.n, n = dst.n
// Multiply Q_n ... Q_2 Q_1 src
// Where Q_i = I-v_i v_i^T
for (unsigned int j=0;j<n;++j)
{
// sum = v_i^T dst
number2 sum = diagonal[j]* (*aux)(j);
for (unsigned int i=j+1 ; i<m ; ++i)
sum += this->el(i,j)*(*aux)(i);
// dst -= v * sum
(*aux)(j) -= sum*diagonal[j];
for (unsigned int i=j+1 ; i<m ; ++i)
(*aux)(i) -= sum*this->el(i,j);
}
// Compute norm of residual
number2 sum = 0.;
for (unsigned int i=n ; i<m ; ++i)
sum += (*aux)(i) * (*aux)(i);
//backward works for
//Vectors only, so copy
//them before
Vector<number2> v_dst, v_aux;
v_dst = dst;
v_aux = *aux;
// Compute solution
this->backward(v_dst, v_aux);
mem.free(aux);
return std::sqrt(sum);
}
#endif // DOXYGEN
DEAL_II_NAMESPACE_CLOSE
#endif
|