KrisLibrary  1.0.0
linalgebra.h
1 #ifndef MATH_LINEAR_ALGEBRA_H
2 #define MATH_LINEAR_ALGEBRA_H
3 
4 #include "matrix.h"
5 
6 namespace Math {
7 
9 {
10  enum Type { Jacobi, GaussSeidel, SOR };
11  IterativeMethod(const Matrix& A,const Vector& b,Real omega=One);
12 
13  void InitialRandom(Vector& x0,Real xmin=-One,Real xmax=One) const;
14  void InitialOnes(Vector& x0) const;
15  bool Solve(Type type,Vector& x0,int& maxIters,Real& tol) const;
16 
17  bool IsValid_Jacobi() const; //must be diagonally dominant
18  bool IsValid_GaussSeidel() const; //must be diagonally dominant or pos. def
19  bool IsValid_SOR() const; //must be diagonally dominant or pos. def
20 
21  void Iterate_Jacobi(Vector& x) const;
22  void Iterate_GaussSeidel(Vector& x) const;
23  void Iterate_SOR(Vector& x) const;
24 
25  const Matrix& A;
26  const Vector& b;
27  Real omega; //for SOR
28 };
29 
30 //Ax=b
32 {
33  MatrixEquation(const Matrix& A, const Vector& b);
34 
35  // Exact solution of x (returns false if A is not invertible)
36  bool Solve(Vector& x) const;
37  bool Solve_Cholesky(Vector& x) const;
38  bool Solve_LU(Vector& x) const;
39  bool Solve_SVD(Vector& x) const;
40  bool Solve_Jacobi(Vector& x,int maxIters=100,Real tol=Epsilon) const;
41  bool Solve_GaussSeidel(Vector& x,int maxIters=100,Real tol=Epsilon) const;
42  bool Solve_SOR(Vector& x,Real omega=1.25,int maxIters=100,Real tol=Epsilon) const;
43 
44  // Least-squares solution of x (returns false if method fails)
45  bool LeastSquares(Vector& x) const;
46  bool LeastSquares_Cholesky(Vector& x) const;
47  bool LeastSquares_QR(Vector& x) const;
48  bool LeastSquares_SVD(Vector& x) const;
49  bool LeastSquares_GaussSeidel(Vector& x,int maxIters=100,Real tol=Epsilon) const;
50 
51 
52  // Back substitution, supposing A is upper/lower triangular (U/L)
53  bool LBackSubstitute(Vector& x) const;
54  bool UBackSubstitute(Vector& x) const;
55  bool LTBackSubstitute(Vector& x) const;
56 
57  // All solutions -- x=x0+N*y, where y is arbitrary
58  bool AllSolutions(Vector& x0,Matrix& N) const;
59  bool AllSolutions_RE(Vector& x0,Matrix& N) const;
60  bool AllSolutions_SVD(Vector& x0,Matrix& N) const;
61 
62  inline bool IsValid() const { return A.m == b.n; }
63  inline void Residual(const Vector& x, Vector& r) const { r.setNegative(b); A.madd(x,r); }
64 
65  const Matrix& A;
66  const Vector& b;
67 };
68 
69 }
70 
71 #endif
Definition: linalgebra.h:8
Definition: linalgebra.h:31
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12