Line data Source code
1 : /*
2 : * File: TestSLDL.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on Nov 5, 2015, 4:08:41 AM
6 : *
7 : * ForBES is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * ForBES is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU Lesser General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "TestSLDL.h"
22 :
23 :
24 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestSLDL);
25 :
26 3 : TestSLDL::TestSLDL() {
27 3 : }
28 :
29 6 : TestSLDL::~TestSLDL() {
30 6 : }
31 :
32 3 : void TestSLDL::setUp() {
33 3 : }
34 :
35 3 : void TestSLDL::tearDown() {
36 3 : }
37 :
38 1 : void TestSLDL::testFactorizeAndSolve() {
39 1 : const size_t n = 5;
40 1 : const size_t m = 2;
41 1 : Matrix X = MatrixFactory::MakeRandomSparse(n, m, 5, 0.0, 1.0);
42 :
43 2 : Matrix Xt(X);
44 1 : Xt.transpose();
45 :
46 2 : Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
47 2 : Matrix sol;
48 :
49 1 : double beta = 1.13453;
50 1 : FactoredSolver * solver = new S_LDLFactorization(X, beta);
51 1 : int status = solver -> factorize();
52 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
53 1 : status = solver -> solve(x, sol);
54 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
55 :
56 2 : Matrix g1 = X * Xt * sol;
57 2 : Matrix g2 = x;
58 1 : status = Matrix::add(g2, -beta, sol, 1.0);
59 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
60 :
61 1 : _ASSERT_EQ(g1, g2);
62 :
63 2 : delete solver;
64 1 : }
65 :
66 1 : void TestSLDL::testDenseShort() {
67 1 : const size_t n = 3;
68 1 : const size_t m = 7;
69 1 : Matrix A = MatrixFactory::MakeRandomMatrix(n, m, 0.0, 1.0); /* dense matrix*/
70 2 : Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
71 1 : double beta = 0.5234234;
72 2 : Matrix sol;
73 :
74 2 : Matrix At(A);
75 1 : At.transpose();
76 :
77 1 : FactoredSolver * solver = new S_LDLFactorization(A, beta);
78 1 : int status = solver->factorize();
79 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
80 1 : status = solver -> solve(x, sol);
81 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
82 :
83 :
84 2 : Matrix g1 = A * At * sol;
85 2 : Matrix g2 = x;
86 1 : Matrix::add(g2, -beta, sol, 1.0);
87 :
88 1 : delete solver;
89 :
90 2 : _ASSERT_EQ(g1, g2);
91 :
92 1 : }
93 :
94 1 : void TestSLDL::testDenseTall() {
95 1 : const size_t n = 10;
96 1 : const size_t m = 2;
97 : //Matrix X = MatrixFactory::MakeRandomSparse(n, m, 5, 0.0, 1.0);
98 : double X_data[20] = {
99 : 0.004634224134067,
100 : 0.774910464711502,
101 : 0.817303220653433,
102 : 0.868694705363510,
103 : 0.084435845510910,
104 : 0.399782649098896,
105 : 0.259870402850654,
106 : 0.800068480224308,
107 : 0.431413827463545,
108 : 0.910647594429523,
109 : 0.181847028302852,
110 : 0.263802916521990,
111 : 0.145538980384717,
112 : 0.136068558708664,
113 : 0.869292207640089,
114 : 0.579704587365570,
115 : 0.549860201836332,
116 : 0.144954798223727,
117 : 0.853031117721894,
118 : 0.622055131485066
119 1 : };
120 1 : Matrix X(n, m, X_data);
121 :
122 1 : double beta = 0.567;
123 1 : FactoredSolver * solver = new S_LDLFactorization(X, beta);
124 1 : int status = solver -> factorize();
125 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
126 :
127 2 : Matrix x(10, 1);
128 11 : for (size_t i = 0; i < 10; i++) {
129 10 : x[i] = 1.0;
130 : }
131 :
132 :
133 2 : Matrix sol;
134 1 : status = solver -> solve(x, sol);
135 1 : _ASSERT_EQ(ForBESUtils::STATUS_OK, status);
136 :
137 : double sol_expected_data[10] = {
138 : 1.485739599580427,
139 : 0.238313604427297,
140 : 0.352734275537945,
141 : 0.291783384355755,
142 : 0.344089316703959,
143 : 0.315246516232826,
144 : 0.564132239648385,
145 : 0.378781855470006,
146 : -0.138528061838890,
147 : -0.494174777885299,
148 1 : };
149 2 : Matrix sol_expected(10, 1, sol_expected_data);
150 :
151 1 : _ASSERT_EQ(sol_expected, sol);
152 2 : delete solver;
153 :
154 4 : }
155 :
|