Line data Source code
1 : /*
2 : * File: TestMatrixFactory.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on Jul 13, 2015, 1:03:11 PM
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 "TestMatrixFactory.h"
22 : #include "MatrixFactory.h"
23 : #include <iostream>
24 :
25 :
26 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestMatrixFactory);
27 :
28 11 : TestMatrixFactory::TestMatrixFactory() {
29 11 : }
30 :
31 22 : TestMatrixFactory::~TestMatrixFactory() {
32 22 : }
33 :
34 11 : void TestMatrixFactory::setUp() {
35 11 : }
36 :
37 11 : void TestMatrixFactory::tearDown() {
38 11 : Matrix::destroy_handle();
39 11 : }
40 :
41 1 : void TestMatrixFactory::testMakeIdentity() {
42 1 : int n = 10;
43 1 : double alpha = 2.5;
44 1 : Matrix result = MatrixFactory::MakeIdentity(n, alpha);
45 1 : _ASSERT_EQ(Matrix::MATRIX_DIAGONAL, result.getType());
46 11 : for (int i = 0; i < n; i++) {
47 110 : for (int j = 0; j < n; j++) {
48 100 : if (i == j) {
49 10 : _ASSERT_EQ(alpha, result.get(i, j));
50 : } else {
51 90 : _ASSERT_EQ(0.0, result.get(i, j));
52 : }
53 : }
54 1 : }
55 :
56 1 : }
57 :
58 1 : void TestMatrixFactory::testMakeRandomMatrix() {
59 1 : size_t n = 8;
60 1 : Matrix result = MatrixFactory::MakeRandomMatrix(n, n, 0.01, 1.5, Matrix::MATRIX_LOWERTR);
61 1 : _ASSERT_EQ(Matrix::MATRIX_LOWERTR, result.getType());
62 9 : for (size_t i = 0; i < n; i++) {
63 72 : for (size_t j = 0; j < n; j++) {
64 64 : if (i < j) {
65 28 : _ASSERT_EQ(0.0, result.get(i, j));
66 : } else {
67 36 : _ASSERT(result.get(i, j) > 0);
68 : }
69 : }
70 : }
71 :
72 1 : result = MatrixFactory::MakeRandomMatrix(n, n, 0.01, 1.5, Matrix::MATRIX_SYMMETRIC);
73 1 : _ASSERT_EQ(Matrix::MATRIX_SYMMETRIC, result.getType());
74 :
75 1 : result = MatrixFactory::MakeRandomMatrix(n, n, 0.01, 1.5, Matrix::MATRIX_DIAGONAL);
76 1 : _ASSERT_EQ(Matrix::MATRIX_DIAGONAL, result.getType());
77 :
78 1 : result = MatrixFactory::MakeRandomMatrix(n, n, 0.01, 1.5, Matrix::MATRIX_DENSE);
79 1 : _ASSERT_EQ(Matrix::MATRIX_DENSE, result.getType());
80 :
81 1 : }
82 :
83 1 : void TestMatrixFactory::testMakeSparse() {
84 1 : size_t n = 5;
85 1 : size_t m = 8;
86 1 : size_t nnz = 10;
87 1 : Matrix *SP = new Matrix();
88 21 : for (size_t k = 0; k < 20; k++) {
89 20 : _ASSERT_OK(*SP = MatrixFactory::MakeRandomSparse(n, m, nnz, 0.0, 1.0));
90 20 : _ASSERT_EQ(n, SP->getNrows());
91 20 : _ASSERT_EQ(m, SP->getNcols());
92 20 : _ASSERT_EQ(Matrix::MATRIX_SPARSE, SP->getType());
93 20 : size_t actual_nnz = 0;
94 120 : for (size_t i = 0; i < n; i++) {
95 900 : for (size_t j = 0; j < m; j++) {
96 800 : if (SP->get(i, j) != 0) {
97 200 : actual_nnz++;
98 : }
99 : }
100 : }
101 20 : _ASSERT_EQ(nnz, actual_nnz);
102 : }
103 1 : delete SP;
104 :
105 1 : Matrix A;
106 1 : _ASSERT_OK(A = MatrixFactory::MakeRandomMatrix(n, m, 0.0, 1.0, Matrix::MATRIX_SPARSE));
107 1 : _ASSERT_EQ(Matrix::MATRIX_SPARSE, A.getType());
108 1 : _ASSERT_EQ(n, A.getNrows());
109 1 : _ASSERT_EQ(m, A.getNcols());
110 1 : }
111 :
112 1 : void TestMatrixFactory::testReadSparseFromFile() {
113 : FILE *fp;
114 1 : fp = fopen("matrices/sparse1.mx", "r");
115 :
116 1 : CPPUNIT_ASSERT_MESSAGE("File not found or cannot open", fp != NULL);
117 :
118 1 : Matrix A;
119 1 : _ASSERT_OK(A = MatrixFactory::ReadSparse(fp));
120 :
121 1 : _ASSERT_EQ(0, fclose(fp));
122 :
123 1 : size_t n = 6;
124 1 : size_t m = 9;
125 1 : size_t nnz = 7;
126 2 : Matrix A_correct = MatrixFactory::MakeSparse(n, m, nnz, Matrix::SPARSE_UNSYMMETRIC);
127 1 : A_correct.set(0, 0, 3.8);
128 1 : A_correct.set(0, 1, 2.20);
129 1 : A_correct.set(1, 2, -1.18);
130 1 : A_correct.set(3, 3, 5.5);
131 1 : A_correct.set(3, 5, 1.23);
132 1 : A_correct.set(5, 1, 0.95);
133 1 : A_correct.set(5, 5, 2.68);
134 :
135 2 : _ASSERT_EQ(A_correct, A);
136 1 : }
137 :
138 1 : void TestMatrixFactory::testSparse() {
139 1 : size_t n = 5;
140 1 : size_t m = 10;
141 1 : size_t max_nnz = 5;
142 1 : Matrix M = MatrixFactory::MakeSparse(n, m, max_nnz, Matrix::SPARSE_UNSYMMETRIC);
143 1 : _ASSERT_EQ(Matrix::MATRIX_SPARSE, M.getType());
144 1 : }
145 :
146 1 : void TestMatrixFactory::testSparse2() {
147 1 : const size_t n = 20;
148 1 : const size_t m = 40;
149 1 : const size_t nnz = 6;
150 1 : Matrix N = MatrixFactory::MakeSparse(n, m, nnz, Matrix::SPARSE_UNSYMMETRIC);
151 : Matrix *M;
152 1 : M = new Matrix(N);
153 1 : CPPUNIT_ASSERT_NO_THROW(delete M);
154 :
155 1 : _ASSERT_EQ(n, N.getNrows());
156 1 : _ASSERT_EQ(m, N.getNcols());
157 1 : _ASSERT_EQ(0, Matrix::cholmod_handle()->status);
158 1 : _ASSERT(Matrix::cholmod_handle()->memory_usage > 0);
159 1 : }
160 :
161 1 : void TestMatrixFactory::testShallow1() {
162 1 : const size_t n = 10;
163 1 : Matrix X = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 10.0);
164 :
165 : /*
166 : * shallow copies (with various offsets)...
167 : */
168 2 : Matrix X_shallow_copy = MatrixFactory::ShallowVector(X, 1);
169 1 : _ASSERT_EQ(n - 1, X_shallow_copy.getNrows());
170 1 : _ASSERT_EQ(n - 1, X_shallow_copy.length());
171 10 : for (size_t i = 0; i < n - 1; i++) {
172 9 : _ASSERT_EQ(X_shallow_copy[i], X[i + 1]);
173 : }
174 :
175 1 : X_shallow_copy = MatrixFactory::ShallowVector(X, 2);
176 1 : _ASSERT_EQ(n - 2, X_shallow_copy.getNrows());
177 1 : _ASSERT_EQ(n - 2, X_shallow_copy.length());
178 9 : for (size_t i = 0; i < n - 2; i++) {
179 8 : _ASSERT_EQ(X_shallow_copy[i], X[i + 2]);
180 : }
181 :
182 :
183 11 : for (size_t i = 0; i < n; i++) {
184 10 : X.set(i, 0, i + 1.0);
185 : }
186 :
187 1 : X_shallow_copy = MatrixFactory::ShallowVector(X, 1);
188 1 : _ASSERT_EQ(n - 1, X_shallow_copy.getNrows());
189 1 : _ASSERT_EQ(n - 1, X_shallow_copy.length());
190 10 : for (size_t i = 0; i < n - 1; i++) {
191 9 : _ASSERT_EQ(i + 2.0, X_shallow_copy.get(i, 0));
192 : }
193 :
194 2 : Matrix Y = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 10.0);
195 1 : Y.transpose();
196 :
197 1 : X_shallow_copy = MatrixFactory::ShallowVector(Y, 1);
198 1 : _ASSERT_EQ(Y.getNcols() - 1, X_shallow_copy.getNcols());
199 2 : _ASSERT_EQ(Y.getNrows(), X_shallow_copy.getNrows());
200 :
201 :
202 1 : }
203 :
204 1 : void TestMatrixFactory::testShallow2() {
205 : /* test exceptions */
206 1 : const size_t n = 20;
207 1 : Matrix X_matrix = MatrixFactory::MakeRandomMatrix(n, 2, 0.0, 10.0);
208 2 : Matrix X_sparse = MatrixFactory::MakeRandomSparse(n, 1, static_cast<size_t> (n / 2), 0.0, 10.0);
209 2 : Matrix X_shallow_copy;
210 1 : _ASSERT_EXCEPTION(X_shallow_copy = MatrixFactory::ShallowVector(X_matrix, 0), std::invalid_argument);
211 2 : _ASSERT_EXCEPTION(X_shallow_copy = MatrixFactory::ShallowVector(X_sparse, 0), std::invalid_argument);
212 1 : }
213 :
214 1 : void TestMatrixFactory::testShallow3() {
215 : /* test method ShallowVector(const Matrix& orig, size_t offset); */
216 1 : const size_t n = 20;
217 1 : Matrix X = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 10.0);
218 :
219 1 : const size_t n1 = 4;
220 1 : const size_t p = 2;
221 2 : Matrix X_shallow = MatrixFactory::ShallowVector(X, n1, p);
222 1 : _ASSERT_EQ(n1, X_shallow.length());
223 :
224 5 : for (size_t i = 0; i < n1; i++) {
225 4 : _ASSERT_EQ(X[p + i], X_shallow[i]);
226 : }
227 :
228 2 : Matrix Xsub = X.submatrixCopy(p, p + n1 - 1, 0, 0);
229 1 : _ASSERT_EQ(Xsub, X_shallow);
230 :
231 2 : Matrix A = MatrixFactory::MakeRandomMatrix(n1, n1, 10.0, 2.0);
232 2 : Matrix y1 = A*Xsub;
233 2 : Matrix y2 = A*X_shallow;
234 :
235 2 : _ASSERT_EQ(y1, y2);
236 :
237 1 : }
238 :
239 1 : void TestMatrixFactory::testShallow4() {
240 : /* Shallow matrix from pointer-to-double */
241 1 : const size_t n = 20;
242 1 : const size_t m = 10;
243 1 : double * data = new double[n]();
244 21 : for (size_t i = 0; i < n; i++) {
245 20 : data[i] = i + 1.0;
246 : }
247 :
248 1 : Matrix shallow = MatrixFactory::ShallowVector(data, m, 1);
249 1 : _ASSERT_EQ(m, shallow.length());
250 1 : _ASSERT_EQ(m, shallow.getNrows());
251 11 : for (size_t i = 0; i < m; i++) {
252 10 : _ASSERT_EQ(static_cast<double> (i + 2.0), shallow[i]);
253 1 : }
254 :
255 1 : }
256 :
257 1 : void TestMatrixFactory::testFailSafe() {
258 1 : Matrix A;
259 1 : _ASSERT_EXCEPTION(A = MatrixFactory::MakeRandomSparse(10, 10, 101, 0.0, 1.0), std::invalid_argument);
260 4 : }
261 :
262 :
263 :
264 :
|