37 #include <Base/Math/Random.hh>
38 #include <Base/Math/SparseMatrix.hh>
39 #include <MathAlgo/SVD.hh>
46 double epsilon = 1e-15)
49 const unsigned int n = A.GetRows();
50 const unsigned int m = A.GetCols();
51 for (
unsigned int i = 0; i < n; i++) {
52 for (
unsigned int j = 0; j < m; j++) {
53 if (fabs(A[i][j]) < epsilon) A[i][j] = 0.0;
62 cout <<
"-- Testing base operations -- " << endl;
70 cout <<
"- Dense is A = " << A << endl;
71 cout <<
"- Sparse is S = " << endl;
74 cout <<
"- Modifying S (consistency check)... " << flush;
76 cout << (S.CheckConsistency() ?
"OK, " :
"Not OK!, ") << flush;
77 S.MultiplyDiagonalBy(0.5);
78 cout << (S.CheckConsistency() ?
"OK, " :
"Not OK!, ") << flush;
79 S.SetElement(1, 1, 0.0);
80 cout << (S.CheckConsistency() ?
"OK, " :
"Not OK!, ") << flush;
81 S.SetElement(1, 4, 1.0);
82 cout << (S.CheckConsistency() ?
"OK" :
"Not OK!")
90 cout <<
"sparse S after setting single elements is " << S << endl << flush;
93 SAddTest.SetElement(0,0,1.0);
94 SAddTest.SetElement(1,1,1.0);
95 cout <<
"s before adding " << SAddTest << endl;
103 cout <<
"adding to bottom " << addBottomSparse << addBottomSparse.GetRows() <<
" " << addBottomSparse.GetCols() << endl;
104 cout <<
"adding to right " << addRightSparse << addRightSparse.GetRows() <<
" " << addRightSparse.GetCols() <<endl;
105 SAddTest.AppendMatrixBottom(addBottomSparse);
106 cout <<
"s after adding to bottom " << SAddTest <<
" size " << SAddTest.GetRows() <<
" " << SAddTest.GetCols() << endl;
107 SAddTest.AppendMatrixRight(addRightSparse);
108 cout <<
"s after adding to right " << SAddTest <<
" size " << SAddTest.GetRows() <<
" " << SAddTest.GetCols() << endl;
110 cout << (SAddTest.CheckConsistency() ?
"OK" :
"Not OK!")
116 cout <<
"- Modified sparse is S = " << endl;
118 cout <<
"- Conversion to dense is B = " << B << endl;
123 BIAS::SparseMatrix ST(S.GetColsNum(), S.GetRowsNum()), STS(ST.GetRowsNum(), S.GetColsNum());
127 cout <<
"- Sparse multiplication S'S = " << endl;
128 STS.Print(
"S'S", cout);
129 cout <<
"- Dense multiplication B'B = " << BTB << endl;
133 m = STS.GetMaxDiagonalElement(i);
134 cout <<
"- Maximal diagonal element of S'S is [" << i <<
"][" << i
135 <<
"] = " << m << endl;
137 m = S.GetMaxRowElement(j, i);
138 cout <<
"- Maximal element of row " << j <<
" in S is [" << j <<
"][" << i
139 <<
"] = " << m << endl;
140 m = ST.GetMaxColumnElement(j, i);
141 cout <<
"- Maximal element of column " << j <<
" of S' is [" << i
142 <<
"][" << j <<
"] = " << m << endl;
144 cout <<
"- Checking consistency of S... ";
145 cout << (S.CheckConsistency() ?
"OK!" :
"failed!") << endl;
146 cout <<
"- Checking consistency of S'... ";
147 cout << (ST.CheckConsistency() ?
"OK!" :
"failed!") << endl;
148 cout <<
"- Checking consistency of S'S... ";
149 cout << (STS.CheckConsistency() ?
"OK!" :
"failed!") << endl;
152 cout << endl <<
"-- Testing matrix inversion -- " << endl << endl;
166 N.SetElement(5, 5, 1.0);
167 cout <<
"- Dense is M = " << M << endl;
168 cout <<
"- Sparse is N = " << endl;
170 cout <<
"- Checking consistency of N... ";
171 cout << (N.CheckConsistency() ?
"OK!" :
"failed!") << endl;
175 cout <<
"SVD result of inverse is M^-1 = " << CropMatrix(Minv) << endl;
180 cout <<
"- Sparse after inversion by Gauss-Jordan is N = " << endl;
182 cout <<
"- Inverse is N^-1 = " << endl;
183 Ninv.Print(
"N^-1", cout);
184 cout <<
"- Checking consistency of N... ";
185 cout << (N.CheckConsistency() ?
"OK!" :
"failed!") << endl;
186 cout <<
"- Checking consistency of N^-1... ";
187 cout << (Ninv.CheckConsistency() ?
"OK!" :
"failed!") << endl;
189 cout <<
"- M * M^-1 = " << CropMatrix(M*Minv) << endl;
193 cout <<
"- N * N^-1 = " << endl;
194 NNinv.Print(
"N*N^-1", cout);
195 cout <<
"- Checking consistency of N*Ninv... ";
196 cout << (NNinv.CheckConsistency() ?
"OK!" :
"failed!") << endl;
199 cout << endl <<
"-- Testing linear equation systems -- " << endl << endl;
211 cout <<
"- Solving linear equation system L*x = b via dense SVD and sparse "
212 <<
"matrix Gauss-Jordan approach..." << endl;
215 cout <<
"- Sparse LES matrix after Gauss-Jordan is L = " << endl;
217 cout <<
"- Checking consistency of L... ";
218 cout << (L.CheckConsistency() ?
"OK!" :
"failed!") << endl;
220 cout <<
"- Result of SVD is x = " << x << endl;
221 cout <<
"- Result of sparse Gauss-Jordan is x = " << x2 << endl;
222 Lold.Multiply(x2, b2);
223 cout <<
"- Evaluation of |L*x - b| yields "
225 <<
" (compare with dense SVD: "
227 <<
")" << endl << endl;
int Invert(BIAS::Matrix< double > &inverse)
Returns inverse matrix as dense matrix.
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Matrix< T > Transpose() const
transpose function, storing data destination matrix
double NormL2() const
Return the L2 norm: sqrt(a^1 + a^2 + ...)
void Multiply(SparseMatrix &A, SparseMatrix &result)
Multiplies with A and returns result M*A.
Implementation of sparse matrix operations.
std::ostream & Print(std::ostream &s, const int width, const int precision, bool scientific=true) const
void Transpose(SparseMatrix &result)
Returns transposed matrix in result (as sparse matrix).