source: src/LinearAlgebra/unittests/MatrixUnitTest.cpp@ 9b410d

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 9b410d was 9b410d, checked in by Frederik Heber <heber@…>, 14 years ago

Replace MYEPSILON in LinearAlgebra/ by LINALG_MYEPSILON.

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