Line data Source code
1 : /*
2 : * File: TestOpReverseVector.cpp
3 : * Author: chung
4 : *
5 : * Created on Sep 16, 2015, 4:00:46 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 "TestOpReverseVector.h"
22 :
23 :
24 1 : CPPUNIT_TEST_SUITE_REGISTRATION(TestOpReverseVector);
25 :
26 3 : TestOpReverseVector::TestOpReverseVector() {
27 3 : }
28 :
29 6 : TestOpReverseVector::~TestOpReverseVector() {
30 6 : }
31 :
32 3 : void TestOpReverseVector::setUp() {
33 3 : }
34 :
35 3 : void TestOpReverseVector::tearDown() {
36 3 : }
37 :
38 1 : void TestOpReverseVector::testCall() {
39 11 : for (size_t n = 5; n < 15; n++) {
40 10 : Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
41 20 : Matrix y(x);
42 20 : OpReverseVector op(n);
43 10 : _ASSERT_EQ(n, op.dimensionIn().first);
44 10 : _ASSERT_EQ(n, op.dimensionOut().first);
45 20 : Matrix xrev = op.call(x);
46 105 : for (size_t i = 0; i < n; i++) {
47 95 : _ASSERT_EQ(y.get(i, 0), xrev.get(n - i - 1, 0));
48 : }
49 10 : }
50 1 : }
51 :
52 1 : void TestOpReverseVector::testCallNotFixedSize() {
53 1 : size_t n1 = 50;
54 1 : size_t n2 = 50;
55 1 : Matrix x1 = MatrixFactory::MakeRandomMatrix(n1, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
56 2 : Matrix x2 = MatrixFactory::MakeRandomMatrix(n2, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
57 2 : Matrix y1(x1);
58 2 : Matrix y2(x2);
59 1 : LinearOperator *op = new OpReverseVector();
60 1 : size_t zero = 0;
61 1 : _ASSERT_EQ(zero, op->dimensionIn().first);
62 1 : _ASSERT_EQ(zero, op->dimensionOut().first);
63 2 : Matrix xrev1 = op -> call(x1);
64 2 : Matrix xrev2 = op -> call(x2);
65 51 : for (size_t i = 0; i < n1; i++) {
66 50 : _ASSERT_EQ(y1.get(i, 0), xrev1.get(n1 - i - 1, 0));
67 : }
68 51 : for (size_t i = 0; i < n2; i++) {
69 50 : _ASSERT_EQ(y2.get(i, 0), xrev2.get(n2 - i - 1, 0));
70 : }
71 2 : delete op;
72 1 : }
73 :
74 1 : void TestOpReverseVector::testCallAdjoint() {
75 1 : size_t n = 80;
76 1 : Matrix x = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
77 2 : Matrix y = MatrixFactory::MakeRandomMatrix(n, 1, 0.0, 1.0, Matrix::MATRIX_DENSE);
78 1 : LinearOperator *op = new OpReverseVector(n);
79 1 : _ASSERT(op->isSelfAdjoint());
80 1 : _ASSERT_EQ(n, op->dimensionIn().first);
81 1 : _ASSERT_EQ(n, op->dimensionOut().first);
82 2 : Matrix Tx = op->call(x);
83 2 : Matrix Tstar_y = op->callAdjoint(y);
84 :
85 2 : Matrix err = y * Tx;
86 2 : Matrix temp = x*Tstar_y;
87 :
88 1 : _ASSERT_EQ(err, temp);
89 2 : delete op;
90 4 : }
91 :
92 :
|