Peano 4
Loading...
Searching...
No Matches
MatrixTest.cpp
Go to the documentation of this file.
1// This file is part of the Peano project. For conditions of distribution and
2// use, please see the copyright notice at www.peano-framework.org
3#include "MatrixTest.h"
4#include "tarch/la/Matrix.h"
7#include <string>
8#include <sstream>
9
10
11
13 TestCase ("tarch::la::MatrixTest") {
14}
15
16
18 testMethod (testConstruction);
19 testMethod (testAssignment);
20 testMethod (testMatrixOperations);
21 testMethod (testMatrixMatrixOperations);
22 testMethod (testTransposedMatrix);
23}
24
25
28 validateEquals (matrix.size(), 2);
29 validateEquals (matrix.rows(), 1);
30 validateEquals (matrix.cols(), 2);
31 validateEquals (matrix(0,0), 1);
32 validateEquals (matrix(0,1), 1);
33
34 Matrix<1,2,int> matrix2(matrix);
35 validateEquals (matrix2(0,0), 1);
36 validateEquals (matrix2(0,1), 1);
37}
38
39
41 Matrix<2,2,int> matrix = {1, 2, 3, 4};
42 validateEquals (matrix(0,0), 1);
43 validateEquals (matrix(0,1), 2);
44 validateEquals (matrix(1,0), 3);
45 validateEquals (matrix(1,1), 4);
46
47 Matrix<1,2,int> matrix2 = {1, 2};
48 validateEquals (matrix2(0,0), 1);
49 validateEquals (matrix2(0,1), 2);
50}
51
52
54 // Test streaming
55 Matrix<2,2,int> matrix2 = {1, 2, 3, 4};
56 std::ostringstream stream;
57 stream << matrix2;
58 validateEquals (stream.str(), std::string("[[1,2],[3,4]]"));
59 // Test matrix multiply scalar
60 matrix2 =matrix2*2;
61 validateEquals(matrix2(0,0),2);
62 validateEquals(matrix2(0,1),4);
63 validateEquals(matrix2(1,0),6);
64 validateEquals(matrix2(1,1),8);
65}
66
67
69{
70 Matrix<2,3,int> lMatrix;
71 Matrix<3,2,int> rMatrix;
72 Matrix<2,2,int> result(0);
73 lMatrix = {1, 2, 3,
74 4, 5, 6};
75 rMatrix = {6, 5,
76 4, 3,
77 2, 1};
78
79 // Matrix matrix multiplication
80 result = lMatrix * rMatrix;
81 validateEquals (result(0,0), 20);
82 validateEquals (result(0,1), 14);
83 validateEquals (result(1,0), 56);
84 validateEquals (result(1,1), 41);
85
86 // Bitwise comparison
87 Matrix<2,3,int> matrixA(1);
88 Matrix<2,3,int> matrixB(2);
89 validate (matrixA == matrixA);
90 validate (! (matrixA == matrixB));
91
92 // Test equalsReturnIndex
93 Matrix<2,3,double> matrix1(1);
94 Matrix<2,3,double> matrix2(2);
95 std::pair<int,int> i=equalsReturnIndex(matrix1,matrix2);
96 validateEquals(i.first,0);
97 validateEquals(i.second,0);
98}
99
100
102{
103 Matrix<3,2,int> matrix = {1, 2,
104 3, 4,
105 5, 6};
106 Matrix<2,3,int> transposed = transpose(matrix);
107 validateEquals (transposed(0,0), 1);
108 validateEquals (transposed(0,1), 3);
109 validateEquals (transposed(0,2), 5);
110 validateEquals (transposed(1,0), 2);
111 validateEquals (transposed(1,1), 4);
112 validateEquals (transposed(1,2), 6);
113
114 validate (transpose(transposed) == matrix);
115}
And from this we can write down f$ nabla phi_i nabla phi_i dx but since we are constructing matrix let s investigate the f$ our matrix elements will nabla phi_i dx f By this will be a sparse matrix
#define validate(booleanExpr)
Definition TestMacros.h:37
#define testMethod(name)
Run a test method and check for errors.
Definition TestMacros.h:24
#define validateEquals(actualValue, validValue)
Definition TestMacros.h:299
Static (i.e.
Definition Matrix.h:31
void testTransposedMatrix()
Tests operations with TransposedMatrix.h.
void testMatrixOperations()
Tests methods from MatrixOperations.h.
void testMatrixMatrixOperations()
Tests methods from MatrixMatrixOperations.h.
void testConstruction()
Tests constructors.
void testAssignment()
Tests methods from MatrixAssign.h.
virtual void run()
This routine is triggered by the TestCaseCollection.
Matrix< Cols, Rows, Scalar > transpose(const Matrix< Rows, Cols, Scalar > &matrix)
std::pair< int, int > equalsReturnIndex(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Return Index of element which is not equals.