/usr/include/shark/LinAlg/solveSystem.h is in libshark-dev 3.1.3+ds1-2.
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | //===========================================================================
/*!
*
*
* \brief Some operations for matrices.
*
*
*
*
* \author O. Krause
* \date 2011
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
//===========================================================================
#ifndef SHARK_LINALG_SOLVE_SYSTEM_H
#define SHARK_LINALG_SOLVE_SYSTEM_H
#include <shark/LinAlg/solveTriangular.h>
namespace shark{ namespace blas{
/**
* \ingroup shark_globals
*
* @{
*/
/// \brief In-Place System of linear equations solver.
///
///Solves a system of linear equations
///Ax=b
///for x, using LU decomposition and
///backward substitution sotring the results in b.
///This Method is in
///no way optimized for sparse matrices.
///Be aware, that the matrix must have full rank!
template<class MatT,class VecT>
void solveSystemInPlace(
matrix_expression<MatT> const& A,
vector_expression<VecT>& b
);
/// \brief System of linear equations solver.
///
/// Solves asystem of linear equations
/// Ax=b
/// for x, using LU decomposition and
/// backward substitution. This Method is in
/// no way optimized for sparse matrices.
/// Be aware, that the matrix must have full rank!
template<class MatT,class Vec1T,class Vec2T>
void solveSystem(
const matrix_expression<MatT> & A,
vector_expression<Vec1T>& x,
const vector_expression<Vec2T> & b
);
/// \brief In-Place system of linear equations solver.
///
///Solves multiple systems of linear equations
///Ax_1=b_1
///Ax_2=b_2
///...
///=>AX=B
///for X, using LU decomposition and
///backward substitution and stores the result in b
///Note, that B=(b_1,...,b_n), so the right hand sides are stored as columns
///This Method is in no way optimized for sparse matrices.
///Be aware, that the matrix must have full rank!
template<class MatT,class Mat2T>
void solveSystemInPlace(
matrix_expression<MatT> const& A,
matrix_expression<Mat2T>& B
);
/// \brief System of linear equations solver.
///
/// Solves multiple systems of linear equations
/// Ax_1=b_1
/// Ax_2=b_2
/// ...
/// =>AX=B
/// for X, using LU decomposition and
/// backward substitution.
/// Note, that B=(b_1,...,b_n), so the right hand sides are stored as columns
/// This Method is in no way optimized for sparse matrices.
/// Be aware that the matrix must have full rank!
template<class MatT,class Mat1T,class Mat2T>
void solveSystem(
const matrix_expression<MatT> & A,
matrix_expression<Mat1T>& X,
const matrix_expression<Mat2T> & B
);
/// \brief System of symmetric linear equations solver. The result is stored in b
///
/// Solves a system of linear equations
/// Ax=b
/// for x, using Cholesky decomposition and
/// backward substitution. and stores the result in b.
/// A must be symmetric.
/// This method is in no way optimized for sparse matrices.
/// Be aware, that the matrix must have full rank!
template<class System, class MatT,class VecT>
void solveSymmPosDefSystemInPlace(
matrix_expression<MatT> const& A,
vector_expression<VecT>& b
);
/// \brief System of symmetric linear equations solver.
///
/// Solves multiple systems of linear equations
/// Ax_1=b_1
/// Ax_1=b_2
/// ...
/// =>AX=B
/// or XA=B
/// for X, using cholesky decomposition and
/// backward substitution. The first template parameter is used
/// to decide which type of system is solved
/// Note, that B=(b_1,...,b_n), so the right hand sides are stored as columns.
/// A must be symmetric.
/// This Method is in no way optimized for sparse matrices.
/// Be aware, that the matrix must have full rank!
/// Also the result is stored in B directly so it"s contents are destroyed.
/// @param A the system matrix A
/// @param B the right hand side of the LGS, also stores the result
template<class System, class MatT,class Mat1T>
void solveSymmPosDefSystemInPlace(
matrix_expression<MatT> const& A,
matrix_expression<Mat1T>& B
);
/// \brief System of symmetric linear equations solver.
///
/// Solves a system of linear equations
/// Ax=b
/// for x, using Cholesky decomposition and
/// backward substitution. A must be symmetric.
/// This Method is in no way optimized for sparse matrices.
/// Be aware, that the matrix must have full rank!
template<class System,class MatT,class Vec1T,class Vec2T>
void solveSymmPosDefSystem(
matrix_expression<MatT> const& A,
vector_expression<Vec1T>& x,
vector_expression<Vec2T> const& b
);
/// \brief System of symmetric linear equations solver.
///
/// Solves multiple systems of linear equations
/// Ax_1=b_1
/// Ax_1=b_2
/// ...
/// =>AX=B
/// or XA = B
/// for X, using cholesky decomposition and
/// backward substitution. The first template parameter is used
/// to decide which type of system is solved
/// Note, that B=(b_1,...,b_n), so the right hand sides are stored as columns.
/// A must be symmetric.
/// This Method is in no way optimized for sparse matrices.
/// Be aware, that the matrix must have full rank!
/// @param A the system matrix A
/// @param X the stored result of the solution of LGS
/// @param B the right hand side of the LGS
template<class System,class MatT,class Mat1T,class Mat2T>
void solveSymmPosDefSystem(
matrix_expression<MatT> const& A,
matrix_expression<Mat1T>& X,
matrix_expression<Mat2T> const& B
);
/// \brief Solves a square system of linear equations without full rank.
///
/// Solves the system Ax= b or x^TA=b^T when A is
/// symmetric positive semi-definite.
/// If b is not in the span of Ax or xA, the least squares solution is used,
/// that is we minimize ||Ax-b||^2
///
/// The computation is carried out in-place.
/// The algorithm can be looked up in
/// "Fast Computation of Moore-Penrose Inverse Matrices"
/// Pierre Courrieu, 2005
///
/// \param A \f$ n \times n \f$ input matrix.
/// \param b right hand side vector.
template<class System,class MatT,class VecT>
void solveSymmSemiDefiniteSystemInPlace(
matrix_expression<MatT> const& A,
vector_expression<VecT>& b
);
/// \brief Solves multiple square system of linear equations without full rank.
///
/// Solves multiple systems of linear equations
/// Ax_1=b_1
/// Ax_1=b_2
/// ...
/// =>AX=B
/// or XA = B
/// A must be symmetric positive semi-definite - thus is not required to have full rank.
/// Note, that B=(b_1,...,b_n), so the right hand sides are stored as columns.
/// If the b_i are not in the span of Ax_i or x_i^TA, the least squares solution is used,
/// that is we minimize ||Ax_i-b_i||^2
///
/// The computation is carried out in-place.
/// The algorithm can be looked up in
/// "Fast Computation of Moore-Penrose Inverse Matrices"
/// Pierre Courrieu, 2005
///
/// \param A \f$ n \times n \f$ input matrix.
/// \param B \f$ n \times k \f$ right hand side matrix.
template<class System,class Mat1T,class Mat2T>
void solveSymmSemiDefiniteSystemInPlace(
matrix_expression<Mat1T> const& A,
matrix_expression<Mat2T>& B
);
/// \brief Solves a non-square system of linear equations.
///
/// Given a \f$ m \times n \f$ input matrix A this function uses
/// the generalized inverse of A to solve the system of linear equations.
/// If b is not in the span of Ax or xA, the least squares solution is used,
/// that is we minimize ||Ax-b||^2
///
/// The computation is carried out in-place.
///
/// \param A \f$ n \times m \f$ input matrix.
/// \param b right hand side of the problem.
template<class System,class MatT,class VecT>
void generalSolveSystemInPlace(
matrix_expression<MatT> const& A,
vector_expression<VecT>& b
);
/// \brief Solves multiple non-square systems of linear equations.
///
/// Given a \f$ m \times n \f$ input matrix A this function uses
/// the generalized inverse of A to solve the system of linear equations AX=B.
/// If b_i is not in the span of Ax_i or x_iA, the least squares solution is used,
/// that is we minimize ||Ax_i-b_i||^2 for all columns b_i of B.
///
/// The computation is carried out in-place.
///
/// \param A \f$ n \times m \f$ input matrix.
/// \param B \f$ n \times k \f$ right hand sied matrix.
template<class System,class MatA,class MatB>
void generalSolveSystemInPlace(
matrix_expression<MatA> const& A,
matrix_expression<MatB>& B
);
/// \brief Approximates the solution of a linear system of equation Ax=b.
///
/// Most often there is no need for the exact solution of a system of linear
/// equations. Instead only a good approximation needs to be found.
/// In this case an iterative method can be used which stops when
/// a suitable exact solution is found. For a lot of systems this already happens
/// after a very low number of iterations.
/// Every iteration has complexity O(n^2) and after n iterations the
/// exact solution is found. However if this solution is needed, the other
/// methods, as for example solveSymmPosDefSystem are more suitable.
///
/// This algorithm does not require A to have full rank, however it must be
/// positive semi-definite.
///
/// This algorithm stops after the maximum number of iterations is
/// exceeded or after the max-norm of the residual \f$ r_k= -Ax_k+b\f$ is
/// smaller than epsilon.
///
/// The initial solution argument governs whether x already stores a possible starting point.
/// If this is true, it is checked whether it is better than
/// starting from 0 (i.e. the max-norm of the initial residual is smaller than -b).
///
/// \param A the positive semi-definite n x n-Matrix
/// \param x the solution vector
/// \param b the right hand side
/// \param epsilon stopping criterium for the residual
/// \param maxIterations the maximum number of iterations
/// \param initialSolution if this is true, x stores an initial guess of the solution
template<class MatT, class VecT, class VecT2>
void approxsolveSymmPosDefSystem(
matrix_expression<MatT> const& A,
vector_expression<VecT>& x,
vector_expression<VecT2> const& b,
double epsilon = 1.e-10,
bool initialSolution = false,
unsigned int maxIterations = 0
){
SIZE_CHECK(A().size1()==A().size2());
SIZE_CHECK(A().size1()==b().size());
std::size_t dim = b().size();
std::size_t maxIt = (maxIterations == 0)? dim: maxIterations;
typedef typename VecT::value_type value_type;
vector<value_type> r = b;//current residual
if(initialSolution){
SIZE_CHECK(x().size() == dim);
noalias(r) -= prod(A,x);
if(norm_inf(r) > norm_inf(b)){
x().clear();
r = b;
}
}
else{
ensure_size(x,dim);
x().clear();
}
vector<value_type> rnext(dim); //the next residual
vector<value_type> p = r; //the search direction- initially it is the gradient direction
vector<value_type> Ap(dim); //stores prod(A,p)
for(std::size_t i = 0; i != maxIt; ++i){
noalias(Ap) = prod(A,p);
double rsqr=inner_prod(r,r);
double alpha = rsqr/inner_prod(p,Ap);
noalias(x())+=alpha*p;
noalias(rnext) = r - alpha * Ap;
if(norm_inf(rnext)<epsilon)
break;
double beta = inner_prod(rnext,rnext)/rsqr;
p*=beta;
noalias(p) +=rnext;
swap(r,rnext);
}
}
/// \brief Approximates the solution of a linear system of equation Ax=b, storing the solution in b.
///
/// Most often there is no need for the exact solution of a system of linear
/// equations. Instead only a good approximation needs to be found.
/// In this case an iterative method can be used which stops when
/// a suitable exact solution is found. For a lot of systems this already happens
/// after a very low number of iterations.
/// Every iteration has complexity O(n^2) and after n iterations the
/// exact solution is found. However if this solution is needed, the other
/// methods, as for xample solveSymmPosDefSystem are more suitable.
///
/// This algorithm stops after the maximum number of iterations is
/// exceeded or after the max-norm of the residual \f$ r_k= Ax_k-b\f$ is
/// smaller than epsilon. The reuslt is stored in b afterwars
///
/// \param A the positive semi-definite n x n-Matrix
/// \param b the right hand side which also stores the final solution
/// \param epsilon stopping criterium for the residual
/// \param maxIterations the maximum number of iterations
template<class MatT, class VecT>
void approxsolveSymmPosDefSystemInPlace(
matrix_expression<MatT> const& A,
vector_expression<VecT>& b,
double epsilon = 1.e-10,
unsigned int maxIterations = 0
){
SIZE_CHECK(A().size1()==A().size2());
SIZE_CHECK(A().size1()==b().size());
vector<typename VecT::value_type> x(b.size(),0.0);
approxsolveSymmPosDefSystem(A,x,b,epsilon,false,maxIterations);
swap(x,b);
}
/** @}*/
}}
#include "Impl/solveSystem.inl"
#endif
|