source: src/unittests/MatrixUnittest.cpp@ b5c53d

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

Created libMolecuilderLinearAlgebra.

  • is shared.
  • contains the following classes: Line, Plane, Space, Vector, Matrix, gslvector, gslmatrix, linearsystemofequations
  • had to change include path almost everywhere
  • change linkin in src/ and src/unittests/Makefile.am
  • Property mode set to 100644
File size: 8.3 KB
Line 
1/*
2 * MatrixUnittest.cpp
3 *
4 * Created on: Jul 7, 2010
5 * Author: crueger
6 */
7
8#include <cppunit/CompilerOutputter.h>
9#include <cppunit/extensions/TestFactoryRegistry.h>
10#include <cppunit/ui/text/TestRunner.h>
11
12#include "MatrixUnittest.hpp"
13#include "LinearAlgebra/Matrix.hpp"
14#include "LinearAlgebra/Vector.hpp"
15#include "Exceptions/NotInvertibleException.hpp"
16
17#ifdef HAVE_TESTRUNNER
18#include "UnitTestMain.hpp"
19#endif /*HAVE_TESTRUNNER*/
20
21// Registers the fixture into the 'registry'
22CPPUNIT_TEST_SUITE_REGISTRATION( MatrixUnittest );
23
24void MatrixUnittest::setUp(){
25 zero = new Matrix();
26 one = new Matrix();
27 for(int i =NDIM;i--;){
28 one->at(i,i)=1.;
29 }
30 full=new Matrix();
31 for(int i=NDIM;i--;){
32 for(int j=NDIM;j--;){
33 full->at(i,j)=1.;
34 }
35 }
36 diagonal = new Matrix();
37 for(int i=NDIM;i--;){
38 diagonal->at(i,i)=i+1.;
39 }
40 perm1 = new Matrix();
41 perm1->column(0) = e1;
42 perm1->column(1) = e3;
43 perm1->column(2) = e2;
44
45
46 perm2 = new Matrix();
47 perm2->column(0) = e2;
48 perm2->column(1) = e1;
49 perm2->column(2) = e3;
50
51 perm3 = new Matrix();
52 perm3->column(0) = e2;
53 perm3->column(1) = e3;
54 perm3->column(2) = e1;
55
56 perm4 = new Matrix();
57 perm4->column(0) = e3;
58 perm4->column(1) = e2;
59 perm4->column(2) = e1;
60
61 perm5 = new Matrix();
62 perm5->column(0) = e3;
63 perm5->column(1) = e1;
64 perm5->column(2) = e2;
65
66}
67void MatrixUnittest::tearDown(){
68 delete zero;
69 delete one;
70 delete full;
71 delete diagonal;
72 delete perm1;
73 delete perm2;
74 delete perm3;
75 delete perm4;
76 delete perm5;
77}
78
79void MatrixUnittest::AccessTest(){
80 Matrix mat;
81 for(int i=NDIM;i--;){
82 for(int j=NDIM;j--;){
83 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),0.);
84 }
85 }
86 int k=1;
87 for(int i=NDIM;i--;){
88 for(int j=NDIM;j--;){
89 mat.at(i,j)=k++;
90 }
91 }
92 k=1;
93 for(int i=NDIM;i--;){
94 for(int j=NDIM;j--;){
95 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),(double)k);
96 ++k;
97 }
98 }
99}
100
101void MatrixUnittest::VectorTest(){
102 Matrix mat;
103 for(int i=NDIM;i--;){
104 CPPUNIT_ASSERT_EQUAL(mat.row(i),zeroVec);
105 CPPUNIT_ASSERT_EQUAL(mat.column(i),zeroVec);
106 }
107 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),zeroVec);
108
109 mat.one();
110 CPPUNIT_ASSERT_EQUAL(mat.row(0),e1);
111 CPPUNIT_ASSERT_EQUAL(mat.row(1),e2);
112 CPPUNIT_ASSERT_EQUAL(mat.row(2),e3);
113 CPPUNIT_ASSERT_EQUAL(mat.column(0),e1);
114 CPPUNIT_ASSERT_EQUAL(mat.column(1),e2);
115 CPPUNIT_ASSERT_EQUAL(mat.column(2),e3);
116
117 Vector t1=Vector(1.,1.,1.);
118 Vector t2=Vector(2.,2.,2.);
119 Vector t3=Vector(3.,3.,3.);
120 Vector t4=Vector(1.,2.,3.);
121
122 mat.row(0)=t1;
123 mat.row(1)=t2;
124 mat.row(2)=t3;
125 CPPUNIT_ASSERT_EQUAL(mat.row(0),t1);
126 CPPUNIT_ASSERT_EQUAL(mat.row(1),t2);
127 CPPUNIT_ASSERT_EQUAL(mat.row(2),t3);
128 CPPUNIT_ASSERT_EQUAL(mat.column(0),t4);
129 CPPUNIT_ASSERT_EQUAL(mat.column(1),t4);
130 CPPUNIT_ASSERT_EQUAL(mat.column(2),t4);
131 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
132 for(int i=NDIM;i--;){
133 for(int j=NDIM;j--;){
134 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),i+1.);
135 }
136 }
137
138 mat.column(0)=t1;
139 mat.column(1)=t2;
140 mat.column(2)=t3;
141 CPPUNIT_ASSERT_EQUAL(mat.column(0),t1);
142 CPPUNIT_ASSERT_EQUAL(mat.column(1),t2);
143 CPPUNIT_ASSERT_EQUAL(mat.column(2),t3);
144 CPPUNIT_ASSERT_EQUAL(mat.row(0),t4);
145 CPPUNIT_ASSERT_EQUAL(mat.row(1),t4);
146 CPPUNIT_ASSERT_EQUAL(mat.row(2),t4);
147 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
148 for(int i=NDIM;i--;){
149 for(int j=NDIM;j--;){
150 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),j+1.);
151 }
152 }
153}
154
155void MatrixUnittest::OperationTest(){
156 Matrix res;
157
158 res =(*zero) *(*zero);
159 CPPUNIT_ASSERT_EQUAL(res,*zero);
160 res =(*zero) *(*one);
161 CPPUNIT_ASSERT_EQUAL(res,*zero);
162 res =(*zero) *(*full);
163 CPPUNIT_ASSERT_EQUAL(res,*zero);
164 res =(*zero) *(*diagonal);
165 CPPUNIT_ASSERT_EQUAL(res,*zero);
166 res =(*zero) *(*perm1);
167 CPPUNIT_ASSERT_EQUAL(res,*zero);
168 res =(*zero) *(*perm2);
169 CPPUNIT_ASSERT_EQUAL(res,*zero);
170 res =(*zero) *(*perm3);
171 CPPUNIT_ASSERT_EQUAL(res,*zero);
172 res =(*zero) *(*perm4);
173 CPPUNIT_ASSERT_EQUAL(res,*zero);
174 res =(*zero) *(*perm5);
175 CPPUNIT_ASSERT_EQUAL(res,*zero);
176
177 res =(*one)*(*one);
178 CPPUNIT_ASSERT_EQUAL(res,*one);
179 res =(*one)*(*full);
180 CPPUNIT_ASSERT_EQUAL(res,*full);
181 res =(*one)*(*diagonal);
182 CPPUNIT_ASSERT_EQUAL(res,*diagonal);
183 res =(*one)*(*perm1);
184 CPPUNIT_ASSERT_EQUAL(res,*perm1);
185 res =(*one)*(*perm2);
186 CPPUNIT_ASSERT_EQUAL(res,*perm2);
187 res =(*one)*(*perm3);
188 CPPUNIT_ASSERT_EQUAL(res,*perm3);
189 res =(*one)*(*perm4);
190 CPPUNIT_ASSERT_EQUAL(res,*perm4);
191 res =(*one)*(*perm5);
192 CPPUNIT_ASSERT_EQUAL(res,*perm5);
193
194 res = (*full)*(*perm1);
195 CPPUNIT_ASSERT_EQUAL(res,*full);
196 res = (*full)*(*perm2);
197 CPPUNIT_ASSERT_EQUAL(res,*full);
198 res = (*full)*(*perm3);
199 CPPUNIT_ASSERT_EQUAL(res,*full);
200 res = (*full)*(*perm4);
201 CPPUNIT_ASSERT_EQUAL(res,*full);
202 res = (*full)*(*perm5);
203 CPPUNIT_ASSERT_EQUAL(res,*full);
204
205 res = (*diagonal)*(*perm1);
206 CPPUNIT_ASSERT_EQUAL(res.column(0),e1);
207 CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
208 CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
209 res = (*diagonal)*(*perm2);
210 CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
211 CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
212 CPPUNIT_ASSERT_EQUAL(res.column(2),3*e3);
213 res = (*diagonal)*(*perm3);
214 CPPUNIT_ASSERT_EQUAL(res.column(0),2*e2);
215 CPPUNIT_ASSERT_EQUAL(res.column(1),3*e3);
216 CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
217 res = (*diagonal)*(*perm4);
218 CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
219 CPPUNIT_ASSERT_EQUAL(res.column(1),2*e2);
220 CPPUNIT_ASSERT_EQUAL(res.column(2),e1);
221 res = (*diagonal)*(*perm5);
222 CPPUNIT_ASSERT_EQUAL(res.column(0),3*e3);
223 CPPUNIT_ASSERT_EQUAL(res.column(1),e1);
224 CPPUNIT_ASSERT_EQUAL(res.column(2),2*e2);
225}
226
227
228void MatrixUnittest::InvertTest(){
229 CPPUNIT_ASSERT_THROW(zero->invert(),NotInvertibleException);
230 CPPUNIT_ASSERT_THROW(full->invert(),NotInvertibleException);
231
232 Matrix res;
233 res = (*one)*one->invert();
234 CPPUNIT_ASSERT_EQUAL(res,*one);
235 res = (*diagonal)*diagonal->invert();
236 CPPUNIT_ASSERT_EQUAL(res,*one);
237 res = (*perm1)*perm1->invert();
238 CPPUNIT_ASSERT_EQUAL(res,*one);
239 res = (*perm2)*perm2->invert();
240 CPPUNIT_ASSERT_EQUAL(res,*one);
241 res = (*perm3)*perm3->invert();
242 CPPUNIT_ASSERT_EQUAL(res,*one);
243 res = (*perm4)*perm4->invert();
244 CPPUNIT_ASSERT_EQUAL(res,*one);
245 res = (*perm5)*perm5->invert();
246 CPPUNIT_ASSERT_EQUAL(res,*one);
247}
248
249
250void MatrixUnittest::DeterminantTest(){
251 CPPUNIT_ASSERT_EQUAL(zero->determinant(),0.);
252 CPPUNIT_ASSERT_EQUAL(one->determinant(),1.);
253 CPPUNIT_ASSERT_EQUAL(diagonal->determinant(),6.);
254 CPPUNIT_ASSERT_EQUAL(full->determinant(),0.);
255 CPPUNIT_ASSERT_EQUAL(perm1->determinant(),-1.);
256 CPPUNIT_ASSERT_EQUAL(perm2->determinant(),-1.);
257 CPPUNIT_ASSERT_EQUAL(perm3->determinant(),1.);
258 CPPUNIT_ASSERT_EQUAL(perm4->determinant(),-1.);
259 CPPUNIT_ASSERT_EQUAL(perm5->determinant(),1.);
260}
261
262void MatrixUnittest::VecMultTest(){
263 CPPUNIT_ASSERT_EQUAL((*zero)*e1,zeroVec);
264 CPPUNIT_ASSERT_EQUAL((*zero)*e2,zeroVec);
265 CPPUNIT_ASSERT_EQUAL((*zero)*e3,zeroVec);
266 CPPUNIT_ASSERT_EQUAL((*zero)*zeroVec,zeroVec);
267
268 CPPUNIT_ASSERT_EQUAL((*one)*e1,e1);
269 CPPUNIT_ASSERT_EQUAL((*one)*e2,e2);
270 CPPUNIT_ASSERT_EQUAL((*one)*e3,e3);
271 CPPUNIT_ASSERT_EQUAL((*one)*zeroVec,zeroVec);
272
273 CPPUNIT_ASSERT_EQUAL((*diagonal)*e1,e1);
274 CPPUNIT_ASSERT_EQUAL((*diagonal)*e2,2*e2);
275 CPPUNIT_ASSERT_EQUAL((*diagonal)*e3,3*e3);
276 CPPUNIT_ASSERT_EQUAL((*diagonal)*zeroVec,zeroVec);
277
278 CPPUNIT_ASSERT_EQUAL((*perm1)*e1,e1);
279 CPPUNIT_ASSERT_EQUAL((*perm1)*e2,e3);
280 CPPUNIT_ASSERT_EQUAL((*perm1)*e3,e2);
281 CPPUNIT_ASSERT_EQUAL((*perm1)*zeroVec,zeroVec);
282
283 CPPUNIT_ASSERT_EQUAL((*perm2)*e1,e2);
284 CPPUNIT_ASSERT_EQUAL((*perm2)*e2,e1);
285 CPPUNIT_ASSERT_EQUAL((*perm2)*e3,e3);
286 CPPUNIT_ASSERT_EQUAL((*perm2)*zeroVec,zeroVec);
287
288 CPPUNIT_ASSERT_EQUAL((*perm3)*e1,e2);
289 CPPUNIT_ASSERT_EQUAL((*perm3)*e2,e3);
290 CPPUNIT_ASSERT_EQUAL((*perm3)*e3,e1);
291 CPPUNIT_ASSERT_EQUAL((*perm3)*zeroVec,zeroVec);
292
293 CPPUNIT_ASSERT_EQUAL((*perm4)*e1,e3);
294 CPPUNIT_ASSERT_EQUAL((*perm4)*e2,e2);
295 CPPUNIT_ASSERT_EQUAL((*perm4)*e3,e1);
296 CPPUNIT_ASSERT_EQUAL((*perm4)*zeroVec,zeroVec);
297
298 CPPUNIT_ASSERT_EQUAL((*perm5)*e1,e3);
299 CPPUNIT_ASSERT_EQUAL((*perm5)*e2,e1);
300 CPPUNIT_ASSERT_EQUAL((*perm5)*e3,e2);
301 CPPUNIT_ASSERT_EQUAL((*perm5)*zeroVec,zeroVec);
302
303 Vector t = Vector(1.,2.,3.);
304 CPPUNIT_ASSERT_EQUAL((*perm1)*t,Vector(1,3,2));
305 CPPUNIT_ASSERT_EQUAL((*perm2)*t,Vector(2,1,3));
306 CPPUNIT_ASSERT_EQUAL((*perm3)*t,Vector(3,1,2));
307 CPPUNIT_ASSERT_EQUAL((*perm4)*t,Vector(3,2,1));
308 CPPUNIT_ASSERT_EQUAL((*perm5)*t,Vector(2,3,1));
309}
Note: See TracBrowser for help on using the repository browser.