source: src/unittests/MatrixUnittest.cpp@ 9cd807

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 9cd807 was 31fb1d, checked in by Frederik Heber <heber@…>, 15 years ago

(un)select-atoms-inside-cuboid: cuboid may be rotated.

  • Matrix::rotate() sets contents to arbitrary rotation matrix.
  • BUGFIX: Matrix::transpose() was working only for symmetric matrices.
  • TEST: unit test cases for rotation() and transpose().
  • new options "angle-x", "angle-y", "angle-z"
  • (un)select-atoms-inside-cuboid: have these as optional arguments.
  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * MatrixUnittest.cpp
3 *
4 * Created on: Jul 7, 2010
5 * Author: crueger
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <cppunit/CompilerOutputter.h>
14#include <cppunit/extensions/TestFactoryRegistry.h>
15#include <cppunit/ui/text/TestRunner.h>
16
17#include <cmath>
18
19#include "MatrixUnittest.hpp"
20#include "LinearAlgebra/Matrix.hpp"
21#include "LinearAlgebra/Vector.hpp"
22#include "Exceptions/NotInvertibleException.hpp"
23
24#ifdef HAVE_TESTRUNNER
25#include "UnitTestMain.hpp"
26#endif /*HAVE_TESTRUNNER*/
27
28// Registers the fixture into the 'registry'
29CPPUNIT_TEST_SUITE_REGISTRATION( MatrixUnittest );
30
31void MatrixUnittest::setUp(){
32 zero = new Matrix();
33 one = new Matrix();
34 for(int i =NDIM;i--;){
35 one->at(i,i)=1.;
36 }
37 full=new Matrix();
38 for(int i=NDIM;i--;){
39 for(int j=NDIM;j--;){
40 full->at(i,j)=1.;
41 }
42 }
43 diagonal = new Matrix();
44 for(int i=NDIM;i--;){
45 diagonal->at(i,i)=i+1.;
46 }
47 perm1 = new Matrix();
48 perm1->column(0) = e1;
49 perm1->column(1) = e3;
50 perm1->column(2) = e2;
51
52
53 perm2 = new Matrix();
54 perm2->column(0) = e2;
55 perm2->column(1) = e1;
56 perm2->column(2) = e3;
57
58 perm3 = new Matrix();
59 perm3->column(0) = e2;
60 perm3->column(1) = e3;
61 perm3->column(2) = e1;
62
63 perm4 = new Matrix();
64 perm4->column(0) = e3;
65 perm4->column(1) = e2;
66 perm4->column(2) = e1;
67
68 perm5 = new Matrix();
69 perm5->column(0) = e3;
70 perm5->column(1) = e1;
71 perm5->column(2) = e2;
72
73}
74void MatrixUnittest::tearDown(){
75 delete zero;
76 delete one;
77 delete full;
78 delete diagonal;
79 delete perm1;
80 delete perm2;
81 delete perm3;
82 delete perm4;
83 delete perm5;
84}
85
86void MatrixUnittest::AccessTest(){
87 Matrix mat;
88 for(int i=NDIM;i--;){
89 for(int j=NDIM;j--;){
90 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),0.);
91 }
92 }
93 int k=1;
94 for(int i=NDIM;i--;){
95 for(int j=NDIM;j--;){
96 mat.at(i,j)=k++;
97 }
98 }
99 k=1;
100 for(int i=NDIM;i--;){
101 for(int j=NDIM;j--;){
102 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),(double)k);
103 ++k;
104 }
105 }
106}
107
108void MatrixUnittest::VectorTest(){
109 Matrix mat;
110 for(int i=NDIM;i--;){
111 CPPUNIT_ASSERT_EQUAL(mat.row(i),zeroVec);
112 CPPUNIT_ASSERT_EQUAL(mat.column(i),zeroVec);
113 }
114 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),zeroVec);
115
116 mat.one();
117 CPPUNIT_ASSERT_EQUAL(mat.row(0),e1);
118 CPPUNIT_ASSERT_EQUAL(mat.row(1),e2);
119 CPPUNIT_ASSERT_EQUAL(mat.row(2),e3);
120 CPPUNIT_ASSERT_EQUAL(mat.column(0),e1);
121 CPPUNIT_ASSERT_EQUAL(mat.column(1),e2);
122 CPPUNIT_ASSERT_EQUAL(mat.column(2),e3);
123
124 Vector t1=Vector(1.,1.,1.);
125 Vector t2=Vector(2.,2.,2.);
126 Vector t3=Vector(3.,3.,3.);
127 Vector t4=Vector(1.,2.,3.);
128
129 mat.row(0)=t1;
130 mat.row(1)=t2;
131 mat.row(2)=t3;
132 CPPUNIT_ASSERT_EQUAL(mat.row(0),t1);
133 CPPUNIT_ASSERT_EQUAL(mat.row(1),t2);
134 CPPUNIT_ASSERT_EQUAL(mat.row(2),t3);
135 CPPUNIT_ASSERT_EQUAL(mat.column(0),t4);
136 CPPUNIT_ASSERT_EQUAL(mat.column(1),t4);
137 CPPUNIT_ASSERT_EQUAL(mat.column(2),t4);
138 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
139 for(int i=NDIM;i--;){
140 for(int j=NDIM;j--;){
141 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),i+1.);
142 }
143 }
144
145 mat.column(0)=t1;
146 mat.column(1)=t2;
147 mat.column(2)=t3;
148 CPPUNIT_ASSERT_EQUAL(mat.column(0),t1);
149 CPPUNIT_ASSERT_EQUAL(mat.column(1),t2);
150 CPPUNIT_ASSERT_EQUAL(mat.column(2),t3);
151 CPPUNIT_ASSERT_EQUAL(mat.row(0),t4);
152 CPPUNIT_ASSERT_EQUAL(mat.row(1),t4);
153 CPPUNIT_ASSERT_EQUAL(mat.row(2),t4);
154 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
155 for(int i=NDIM;i--;){
156 for(int j=NDIM;j--;){
157 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),j+1.);
158 }
159 }
160}
161
162void MatrixUnittest::TransposeTest(){
163 Matrix res;
164
165 // transpose of unit is unit
166 res.one();
167 (const Matrix)res.transpose();
168 CPPUNIT_ASSERT_EQUAL(res,*one);
169
170 // transpose of transpose is same matrix
171 res.zero();
172 res.set(2,2, 1.);
173 CPPUNIT_ASSERT_EQUAL(res.transpose().transpose(),res);
174}
175
176void MatrixUnittest::OperationTest(){
177 Matrix res;
178
179 res =(*zero) *(*zero);
180 CPPUNIT_ASSERT_EQUAL(res,*zero);
181 res =(*zero) *(*one);
182 CPPUNIT_ASSERT_EQUAL(res,*zero);
183 res =(*zero) *(*full);
184 CPPUNIT_ASSERT_EQUAL(res,*zero);
185 res =(*zero) *(*diagonal);
186 CPPUNIT_ASSERT_EQUAL(res,*zero);
187 res =(*zero) *(*perm1);
188 CPPUNIT_ASSERT_EQUAL(res,*zero);
189 res =(*zero) *(*perm2);
190 CPPUNIT_ASSERT_EQUAL(res,*zero);
191 res =(*zero) *(*perm3);
192 CPPUNIT_ASSERT_EQUAL(res,*zero);
193 res =(*zero) *(*perm4);
194 CPPUNIT_ASSERT_EQUAL(res,*zero);
195 res =(*zero) *(*perm5);
196 CPPUNIT_ASSERT_EQUAL(res,*zero);
197
198 res =(*one)*(*one);
199 CPPUNIT_ASSERT_EQUAL(res,*one);
200 res =(*one)*(*full);
201 CPPUNIT_ASSERT_EQUAL(res,*full);
202 res =(*one)*(*diagonal);
203 CPPUNIT_ASSERT_EQUAL(res,*diagonal);
204 res =(*one)*(*perm1);
205 CPPUNIT_ASSERT_EQUAL(res,*perm1);
206 res =(*one)*(*perm2);
207 CPPUNIT_ASSERT_EQUAL(res,*perm2);
208 res =(*one)*(*perm3);
209 CPPUNIT_ASSERT_EQUAL(res,*perm3);
210 res =(*one)*(*perm4);
211 CPPUNIT_ASSERT_EQUAL(res,*perm4);
212 res =(*one)*(*perm5);
213 CPPUNIT_ASSERT_EQUAL(res,*perm5);
214
215 res = (*full)*(*perm1);
216 CPPUNIT_ASSERT_EQUAL(res,*full);
217 res = (*full)*(*perm2);
218 CPPUNIT_ASSERT_EQUAL(res,*full);
219 res = (*full)*(*perm3);
220 CPPUNIT_ASSERT_EQUAL(res,*full);
221 res = (*full)*(*perm4);
222 CPPUNIT_ASSERT_EQUAL(res,*full);
223 res = (*full)*(*perm5);
224 CPPUNIT_ASSERT_EQUAL(res,*full);
225
226 res = (*diagonal)*(*perm1);
227 CPPUNIT_ASSERT_EQUAL(res.column(0),e1);
228 CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
229 CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
230 res = (*diagonal)*(*perm2);
231 CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
232 CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
233 CPPUNIT_ASSERT_EQUAL(res.column(2),3*e3);
234 res = (*diagonal)*(*perm3);
235 CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
236 CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
237 CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
238 res = (*diagonal)*(*perm4);
239 CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
240 CPPUNIT_ASSERT_EQUAL(res.column(1),2*e2);
241 CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
242 res = (*diagonal)*(*perm5);
243 CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
244 CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
245 CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
246}
247
248void MatrixUnittest::RotationTest(){
249 Matrix res;
250 Matrix inverse;
251
252 // zero rotation angles yields unity matrix
253 res.rotation(0,0,0);
254 CPPUNIT_ASSERT_EQUAL(*one, res);
255
256 // arbitrary rotation matrix has det = 1
257 res.rotation(M_PI/3.,1.,M_PI/7.);
258 CPPUNIT_ASSERT(fabs(fabs(res.determinant()) -1.) < MYEPSILON);
259
260 // inverse is rotation matrix with negative angles
261 res.rotation(M_PI/3.,0.,0.);
262 inverse.rotation(-M_PI/3.,0.,0.);
263 CPPUNIT_ASSERT_EQUAL(*one, res * inverse);
264
265 // ... or transposed
266 res.rotation(M_PI/3.,0.,0.);
267 CPPUNIT_ASSERT_EQUAL(inverse, ((const Matrix) res).transpose());
268}
269
270void MatrixUnittest::InvertTest(){
271 CPPUNIT_ASSERT_THROW(zero->invert(),NotInvertibleException);
272 CPPUNIT_ASSERT_THROW(full->invert(),NotInvertibleException);
273
274 Matrix res;
275 res = (*one)*one->invert();
276 CPPUNIT_ASSERT_EQUAL(res,*one);
277 res = (*diagonal)*diagonal->invert();
278 CPPUNIT_ASSERT_EQUAL(res,*one);
279 res = (*perm1)*perm1->invert();
280 CPPUNIT_ASSERT_EQUAL(res,*one);
281 res = (*perm2)*perm2->invert();
282 CPPUNIT_ASSERT_EQUAL(res,*one);
283 res = (*perm3)*perm3->invert();
284 CPPUNIT_ASSERT_EQUAL(res,*one);
285 res = (*perm4)*perm4->invert();
286 CPPUNIT_ASSERT_EQUAL(res,*one);
287 res = (*perm5)*perm5->invert();
288 CPPUNIT_ASSERT_EQUAL(res,*one);
289}
290
291
292void MatrixUnittest::DeterminantTest(){
293 CPPUNIT_ASSERT_EQUAL(zero->determinant(),0.);
294 CPPUNIT_ASSERT_EQUAL(one->determinant(),1.);
295 CPPUNIT_ASSERT_EQUAL(diagonal->determinant(),6.);
296 CPPUNIT_ASSERT_EQUAL(full->determinant(),0.);
297 CPPUNIT_ASSERT_EQUAL(perm1->determinant(),-1.);
298 CPPUNIT_ASSERT_EQUAL(perm2->determinant(),-1.);
299 CPPUNIT_ASSERT_EQUAL(perm3->determinant(),1.);
300 CPPUNIT_ASSERT_EQUAL(perm4->determinant(),-1.);
301 CPPUNIT_ASSERT_EQUAL(perm5->determinant(),1.);
302}
303
304void MatrixUnittest::VecMultTest(){
305 CPPUNIT_ASSERT_EQUAL((*zero)*e1,zeroVec);
306 CPPUNIT_ASSERT_EQUAL((*zero)*e2,zeroVec);
307 CPPUNIT_ASSERT_EQUAL((*zero)*e3,zeroVec);
308 CPPUNIT_ASSERT_EQUAL((*zero)*zeroVec,zeroVec);
309
310 CPPUNIT_ASSERT_EQUAL((*one)*e1,e1);
311 CPPUNIT_ASSERT_EQUAL((*one)*e2,e2);
312 CPPUNIT_ASSERT_EQUAL((*one)*e3,e3);
313 CPPUNIT_ASSERT_EQUAL((*one)*zeroVec,zeroVec);
314
315 CPPUNIT_ASSERT_EQUAL((*diagonal)*e1,e1);
316 CPPUNIT_ASSERT_EQUAL((*diagonal)*e2,2*e2);
317 CPPUNIT_ASSERT_EQUAL((*diagonal)*e3,3*e3);
318 CPPUNIT_ASSERT_EQUAL((*diagonal)*zeroVec,zeroVec);
319
320 CPPUNIT_ASSERT_EQUAL((*perm1)*e1,e1);
321 CPPUNIT_ASSERT_EQUAL((*perm1)*e2,e3);
322 CPPUNIT_ASSERT_EQUAL((*perm1)*e3,e2);
323 CPPUNIT_ASSERT_EQUAL((*perm1)*zeroVec,zeroVec);
324
325 CPPUNIT_ASSERT_EQUAL((*perm2)*e1,e2);
326 CPPUNIT_ASSERT_EQUAL((*perm2)*e2,e1);
327 CPPUNIT_ASSERT_EQUAL((*perm2)*e3,e3);
328 CPPUNIT_ASSERT_EQUAL((*perm2)*zeroVec,zeroVec);
329
330 CPPUNIT_ASSERT_EQUAL((*perm3)*e1,e2);
331 CPPUNIT_ASSERT_EQUAL((*perm3)*e2,e3);
332 CPPUNIT_ASSERT_EQUAL((*perm3)*e3,e1);
333 CPPUNIT_ASSERT_EQUAL((*perm3)*zeroVec,zeroVec);
334
335 CPPUNIT_ASSERT_EQUAL((*perm4)*e1,e3);
336 CPPUNIT_ASSERT_EQUAL((*perm4)*e2,e2);
337 CPPUNIT_ASSERT_EQUAL((*perm4)*e3,e1);
338 CPPUNIT_ASSERT_EQUAL((*perm4)*zeroVec,zeroVec);
339
340 CPPUNIT_ASSERT_EQUAL((*perm5)*e1,e3);
341 CPPUNIT_ASSERT_EQUAL((*perm5)*e2,e1);
342 CPPUNIT_ASSERT_EQUAL((*perm5)*e3,e2);
343 CPPUNIT_ASSERT_EQUAL((*perm5)*zeroVec,zeroVec);
344
345 Vector t = Vector(1.,2.,3.);
346 CPPUNIT_ASSERT_EQUAL((*perm1)*t,Vector(1,3,2));
347 CPPUNIT_ASSERT_EQUAL((*perm2)*t,Vector(2,1,3));
348 CPPUNIT_ASSERT_EQUAL((*perm3)*t,Vector(3,1,2));
349 CPPUNIT_ASSERT_EQUAL((*perm4)*t,Vector(3,2,1));
350 CPPUNIT_ASSERT_EQUAL((*perm5)*t,Vector(2,3,1));
351}
Note: See TracBrowser for help on using the repository browser.