Newer
Older
#include <cxxtest/TestSuite.h>
#include <cmath>
#include <ostream>
#include <vector>
Gigg, Martyn Anthony
committed
#include "MantidKernel/Matrix.h"
#include "MantidKernel/V3D.h"
Gigg, Martyn Anthony
committed
#include <boost/lexical_cast.hpp>
Gigg, Martyn Anthony
committed
using Mantid::Kernel::Matrix;
using Mantid::Kernel::DblMatrix;
using Mantid::Kernel::V3D;
class MatrixTest: public CxxTest::TestSuite
A[0][1]=4.0;
A[0][2]=6.0;
A[2][0]=5.0;
A[1][1]=3.0;
A[2][1]=1.0;
A[1][2]=6.0;
A[2][2]=-7.0;
return;
}
Janik Zikovsky
committed
/**
{
Matrix<double> A(3,3);
A[0][0]=1.0;
A[1][0]=3.0;
A[0][1]=4.0;
A[0][2]=6.0;
A[2][0]=5.0;
A[1][1]=3.0;
A[2][1]=1.0;
A[1][2]=6.0;
A[2][2]=-7.0;
TS_ASSERT_DELTA(A.Invert(),105.0,1e-5);
}
void testIdent()
{
Matrix<double> A(3,3);
A[0][0]=1.0;
A[1][0]=0.0;
A[0][1]=0.0;
A[0][2]=0.0;
A[2][0]=0.0;
A[1][1]=1.0;
A[2][1]=0.0;
A[1][2]=0.0;
A[2][2]=1.0;
Gigg, Martyn Anthony
committed
/** Test of equals with a user-specified tolerance */
void test_equals()
{
Matrix<double> A(3,3, true);
Matrix<double> B(3,3, true);
B[1][1] = 1.1;
TS_ASSERT( !A.equals(B, 0.05) );
TS_ASSERT( A.equals(B, 0.15) );
}
Janik Zikovsky
committed
/**
{
Matrix<double> A(3,3);
makeMatrix(A);
Matrix<double> B(A);
A.swapRows(1,2);
A.swapCols(1,2);
TS_ASSERT_EQUALS(A[0][0],B[0][0]);
TS_ASSERT_EQUALS(A[2][2],B[1][1]);
// Plus all the others..
}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
{
Matrix<double> Eval;
Matrix<double> Diag;
Matrix<double> A(3,3); // NOTE: A must be symmetric
A[0][0]=1.0;
A[1][0]=A[0][1]=4.0;
A[0][2]=A[2][0]=5.0;
A[1][1]=3.0;
A[2][1]=A[1][2]=6.0;
A[2][2]=-7.0;
TS_ASSERT(A.Diagonalise(Eval,Diag));
Matrix<double> MA=A*Eval;
Matrix<double> MV=Eval*Diag;
Eval.sortEigen(Diag);
TS_ASSERT(Diag[0][0]<Diag[1][1]);
TS_ASSERT(Diag[1][1]<Diag[2][2]);
TS_ASSERT(MA==MV);
std::vector<double> X(3);
X[0]=Eval[0][1];
X[1]=Eval[1][1];
X[2]=Eval[2][1];
std::vector<double> out=A*X;
transform(X.begin(),X.end(),X.begin(),std::bind2nd(std::multiplies<double>(),Diag[1][1]));
TS_ASSERT_DELTA(X[0],out[0],0.0001);
TS_ASSERT_DELTA(X[1],out[1],0.0001);
TS_ASSERT_DELTA(X[2],out[2],0.0001);
}
Janik Zikovsky
committed
/**
{
Matrix<double> Eval;
Matrix<double> Diag;
Matrix<double> A(2,2); // symmetric only
A[0][0]=1.0;
A[1][0]=3.0;
A[0][1]=3.0;
A[1][1]=4.0;
TS_ASSERT(A.Diagonalise(Eval,Diag)); // returns 1 or 2
Matrix<double> EvalT(Eval);
EvalT.Transpose();
Eval*=Diag;
Eval*=EvalT;
TS_ASSERT(Eval==A);
}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
void testFromVectorThrows()
{
std::vector<double> data(5,0);
TSM_ASSERT_THROWS("building matrix by this construcor and data with wrong number of elements should throw",(Matrix<double>(data)),std::invalid_argument);
}
void testFromVectorBuildCorrect()
{
std::vector<int> data(9,0);
for(int i=0;i<9;i++){
data[i]=i;
}
Matrix<int> myMat;
TSM_ASSERT_THROWS_NOTHING("building matrix by this construcor and data with correct number of elements should not throw",myMat=Matrix<int>(data));
// and the range of the elements in the matrix is correct;
V3D rez1 = myMat*V3D(1,0,0);
V3D rez2 = myMat*V3D(0,1,0);
V3D rez3 = myMat*V3D(0,0,1);
TSM_ASSERT_EQUALS("The data in a matrix have to be located row-wise, so multiplication by (1,0,0)^T selects 1-st column ",true,V3D(0,3,6)==rez1);
TSM_ASSERT_EQUALS("The data in a matrix have to be located row-wise, so multiplication by (0,1,0)^T selects 2-nd column ",true,V3D(1,4,7)==rez2);
TSM_ASSERT_EQUALS("The data in a matrix have to be located row-wise, so multiplication by (0,0,1)^T selects 3-rd column ",true,V3D(2,5,8)==rez3);
}
Savici, Andrei T.
committed
void testIsRotation()
{
Matrix<double> d(3,3,true);
TS_ASSERT(d.isRotation());
d[0][0]=-1;
TS_ASSERT(!d.isRotation());
}
Savici, Andrei T.
committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
void testToRotation()
{
/*
|1 0 0|
|1 2 0|
|0 0 -3|
transforms to
|-s-s 0|
|-s s 0|
|0 0 -1|
with s=sqrt(0.5) and scaling (-sqrt(2),sqrt(2),3)
*/
Matrix<double> d(3,3,true);
d[1][0]=1.0;
d[1][1]=2.;
d[2][2]=-3.;
std::vector<double> v=d.toRotation();
TS_ASSERT_DELTA(d[0][0],-sqrt(0.5),1e-7);
TS_ASSERT_DELTA(d[0][1],-sqrt(0.5),1e-7);
TS_ASSERT_DELTA(d[1][0],-sqrt(0.5),1e-7);
TS_ASSERT_DELTA(d[1][1],sqrt(0.5),1e-7);
TS_ASSERT_DELTA(d[2][2],-1.,1e-7);
TS_ASSERT_DELTA(v[0],-sqrt(2.),1e-7);
TS_ASSERT_DELTA(v[1],sqrt(2.),1e-7);
TS_ASSERT_DELTA(v[2],3.,1e-7);
}
Gigg, Martyn Anthony
committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
void test_Input_Stream_Throws_On_Bad_Input()
{
DblMatrix rot;
std::istringstream is;
is.str("Matr(3,3)1,2,3,4,5,6,7,8,9");
TS_ASSERT_THROWS(is >> rot, std::invalid_argument);
is.str("Matrix3,3)1,2,3,4,5,6,7,8,9");
TS_ASSERT_THROWS(is >> rot, std::invalid_argument);
is.str("Matrix(3,31,2,3,4,5,6,7,8,9");
TS_ASSERT_THROWS(is >> rot, std::invalid_argument);
}
void test_Input_Stream_On_Square_Matrix()
{
DblMatrix rot;
std::istringstream is;
is.str("Matrix(3,3)1,2,3,4,5,6,7,8,9");
TS_ASSERT_THROWS_NOTHING(is >> rot);
TS_ASSERT_EQUALS(rot.numRows(), 3);
TS_ASSERT_EQUALS(rot.numCols(), 3);
for( size_t i = 0; i < 3; ++i )
{
for( size_t j = 0; j < 3; ++j )
{
TS_ASSERT_EQUALS(rot[i][j], static_cast<double>(i*rot.numRows() + j + 1));
}
}
}
void test_Input_Stream_On_Non_Square_Matrix()
{
DblMatrix rot;
std::istringstream is;
is.str("Matrix(2,4)0,1,2,3,10,11,12,13");
TS_ASSERT_THROWS_NOTHING(is >> rot);
TS_ASSERT_EQUALS(rot.numRows(), 2);
TS_ASSERT_EQUALS(rot.numCols(), 4);
for( size_t i = 0; i < 2; ++i )
{
for( size_t j = 0; j < 4; ++j )
{
if( i < 1 )
Gigg, Martyn Anthony
committed
{
TS_ASSERT_EQUALS(rot[i][j], static_cast<double>(i+j));
}
else
{
TS_ASSERT_EQUALS(rot[i][j], static_cast<double>(9+i+j));
}
}
}
}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
void test_fillMatrix_With_Good_Input_Gives_Expected_Matrix()
{
DblMatrix rot;
std::istringstream is;
is.str("Matrix(3|3)1|2|3|4|5|6|7|8|9");
TS_ASSERT_THROWS_NOTHING(Mantid::Kernel::fillFromStream(is, rot, '|'));
checkMatrixHasExpectedValuesForSquareMatrixTest(rot);
}
void test_fillMatrix_Accepts_Any_Delimiter_Between_Number_Rows_And_Columns()
{
DblMatrix rot;
std::istringstream is;
is.str("Matrix(3@3)1|2|3|4|5|6|7|8|9");
TS_ASSERT_THROWS_NOTHING(Mantid::Kernel::fillFromStream(is, rot, '|'));
checkMatrixHasExpectedValuesForSquareMatrixTest(rot);
}
void test_fillMatrix_With_Mixed_Delimiters_In_Input_Values_Throws()
{
DblMatrix rot;
std::istringstream is;
is.str("Matrix(3|3)1|2,3|4|5|6|7|8|9");
TS_ASSERT_THROWS(Mantid::Kernel::fillFromStream(is, rot, '|'), std::invalid_argument);
}
void test_Construction_Non_Square_Matrix_From_Output_Stream()
Gigg, Martyn Anthony
committed
{
DblMatrix ref(2,3);
ref[0][0] = 5;
ref[0][1] = 10;
ref[0][2] = 15;
ref[1][0] = 105;
ref[1][1] = 110;
ref[1][2] = 115;
std::ostringstream os;
os << ref;
TS_ASSERT_EQUALS(os.str(), "Matrix(2,3)5,10,15,105,110,115");
}
Gigg, Martyn Anthony
committed
void test_Construction_Square_Matrix_From_Output_Stream()
{
Gigg, Martyn Anthony
committed
DblMatrix square(2,2);
square[0][0] = 2;
square[0][1] = 4;
square[1][0] = 6;
square[1][1] = 8;
std::ostringstream os;
Gigg, Martyn Anthony
committed
os << square;
TS_ASSERT_EQUALS(os.str(), "Matrix(2,2)2,4,6,8");
}
void test_Dump_Matrix_To_Output_Stream_With_Custom_Delimiter()
{
DblMatrix square(2,2);
square[0][0] = 2;
square[0][1] = 4;
square[1][0] = 6;
square[1][1] = 8;
std::ostringstream os;
Mantid::Kernel::dumpToStream(os, square, '|');
TS_ASSERT_EQUALS(os.str(), "Matrix(2|2)2|4|6|8");
Gigg, Martyn Anthony
committed
}
void test_lexical_cast()
{
try
{
DblMatrix R = boost::lexical_cast<DblMatrix>("Matrix(2,2)2,4,6,8");
TS_ASSERT_EQUALS(R.numRows(), 2);
TS_ASSERT_EQUALS(R.numCols(), 2);
TS_ASSERT_EQUALS(R[0][0], 2.0);
TS_ASSERT_EQUALS(R[0][1], 4.0);
TS_ASSERT_EQUALS(R[1][0], 6.0);
TS_ASSERT_EQUALS(R[1][1], 8.0);
}
catch(boost::bad_lexical_cast & e)
{
TS_FAIL(e.what());
}
}
Gigg, Martyn Anthony
committed
private:
void checkMatrixHasExpectedValuesForSquareMatrixTest(const DblMatrix & mat)
{
TS_ASSERT_EQUALS(mat.numRows(), 3);
TS_ASSERT_EQUALS(mat.numCols(), 3);
for( size_t i = 0; i < 3; ++i )
{
for( size_t j = 0; j < 3; ++j )
{
TS_ASSERT_EQUALS(mat[i][j], static_cast<double>(i*mat.numRows() + j + 1));
}
}
Gigg, Martyn Anthony
committed
}