source: src/unittests/gslmatrixsymmetricunittest.cpp@ c66537

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 c66537 was bcf653, checked in by Frederik Heber <heber@…>, 15 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 6.8 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 * gslmatrixunittest.cpp
10 *
11 * Created on: Jan 8, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20using namespace std;
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
26#include "gslmatrixsymmetricunittest.hpp"
27
28#ifdef HAVE_TESTRUNNER
29#include "UnitTestMain.hpp"
30#endif /*HAVE_TESTRUNNER*/
31
32/********************************************** Test classes **************************************/
33
34// Registers the fixture into the 'registry'
35CPPUNIT_TEST_SUITE_REGISTRATION( GSLMatrixSymmetricTest );
36
37
38void GSLMatrixSymmetricTest::setUp()
39{
40 m = new GSLMatrix(3,3);
41};
42
43void GSLMatrixSymmetricTest::tearDown()
44{
45 delete(m);
46};
47
48/** Unit Test for accessing matrix elements.
49 *
50 */
51void GSLMatrixSymmetricTest::AccessTest()
52{
53 // check whether all elements are initially zero
54 for (int i=0;i<3;i++)
55 for (int j=0;j<3;j++)
56 CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
57
58 // set
59 for (int i=0;i<3;i++)
60 for (int j=0;j<3;j++)
61 m->Set(i,j, i*3+j );
62
63 // and check
64 double *ptr = NULL;
65 for (int i=0;i<3;i++)
66 for (int j=0;j<3;j++) {
67 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->Get(i,j) );
68 ptr = m->Pointer(i,j);
69 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
70 }
71
72 // assignment
73 for (int i=0;i<3;i++)
74 for (int j=0;j<3;j++)
75 m->Set(i,j, i*3+j );
76 GSLMatrix *dest = new GSLMatrix(3,3);
77 *dest = *m;
78 for (int i=0;i<3;i++)
79 for (int j=0;j<3;j++)
80 CPPUNIT_ASSERT_EQUAL( dest->Get(i,j), m->Get(i,j) );
81 delete(dest);
82
83 // out of bounds
84 //CPPUNIT_ASSERT_EQUAL(0., v->Get(4,2) );
85 //CPPUNIT_ASSERT_EQUAL(0., v->Get(2,17) );
86 //CPPUNIT_ASSERT_EQUAL(0., v->Get(1024,140040) );
87 //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,0) );
88 //CPPUNIT_ASSERT_EQUAL(0., v->Get(0,-1) );
89 //CPPUNIT_ASSERT_EQUAL(0., v->Get(-1,-1) );
90};
91
92/** Unit Test for initializating matrices.
93 *
94 */
95void GSLMatrixSymmetricTest::InitializationTest()
96{
97 // set zero
98 m->SetZero();
99 for (int i=0;i<3;i++)
100 for (int j=0;j<3;j++)
101 CPPUNIT_ASSERT_EQUAL( 0., m->Get(i,j) );
102
103 // set all
104 m->SetAll(1.5);
105 for (int i=0;i<3;i++)
106 for (int j=0;j<3;j++)
107 CPPUNIT_ASSERT_EQUAL( 1.5, m->Get(i,j) );
108
109 // set basis
110 m->SetIdentity();
111 for (int i=0;i<3;i++)
112 for (int j=0;j<3;j++)
113 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
114
115 // set from array
116 double array[] = { 1., 0., 0.,
117 0., 1., 0.,
118 0., 0., 1. };
119 m->SetFromDoubleArray(array);
120 for (int i=0;i<3;i++)
121 for (int j=0;j<3;j++)
122 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
123
124};
125
126/** Unit Test for copying matrices.
127 *
128 */
129void GSLMatrixSymmetricTest::CopyTest()
130{
131 // set basis
132 GSLMatrix *dest = NULL;
133 for (int i=0;i<3;i++) {
134 m->SetAll(i);
135 dest = new GSLMatrix(m);
136 for (int j=0;j<3;j++)
137 CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
138
139 delete(dest);
140 }
141};
142
143/** Unit Test for exchanging rows and columns.
144 *
145 */
146void GSLMatrixSymmetricTest::ExchangeTest()
147{
148 // set to 1,1,1,2, ...
149 for (int i=0;i<3;i++)
150 for (int j=0;j<3;j++)
151 m->Set(i,j, i+1 );
152
153 // swap such that nothing happens
154 m->SwapColumns(1,2);
155 for (int i=0;i<3;i++)
156 for (int j=0;j<3;j++)
157 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
158
159 // swap two rows
160 m->SwapRows(1,2);
161 for (int i=0;i<3;i++)
162 for (int j=0;j<3;j++)
163 switch (j) {
164 case 0:
165 CPPUNIT_ASSERT_EQUAL( 1., m->Get(j,i) );
166 break;
167 case 1:
168 CPPUNIT_ASSERT_EQUAL( 3., m->Get(j,i) );
169 break;
170 case 2:
171 CPPUNIT_ASSERT_EQUAL( 2., m->Get(j,i) );
172 break;
173 default:
174 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
175 }
176 // check that op is reversable
177 m->SwapRows(1,2);
178 for (int i=0;i<3;i++)
179 for (int j=0;j<3;j++)
180 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
181
182 // set to 1,2,3,1, ...
183 for (int i=0;i<3;i++)
184 for (int j=0;j<3;j++)
185 m->Set(i,j, j+1. );
186
187 // swap such that nothing happens
188 m->SwapRows(0,2);
189 for (int i=0;i<3;i++)
190 for (int j=0;j<3;j++)
191 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
192
193 // swap two columns
194 m->SwapColumns(0,2);
195 for (int i=0;i<3;i++)
196 for (int j=0;j<3;j++)
197 switch (j) {
198 case 0:
199 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
200 break;
201 case 1:
202 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
203 break;
204 case 2:
205 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
206 break;
207 default:
208 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
209 }
210 // check that op is reversable
211 m->SwapColumns(0,2);
212 for (int i=0;i<3;i++)
213 for (int j=0;j<3;j++)
214 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
215
216
217 // set to 1,2,3,4, ...
218 for (int i=0;i<3;i++)
219 for (int j=0;j<3;j++)
220 m->Set(i,j, 3*i+j+1 );
221 // transpose
222 m->Transpose();
223 for (int i=0;i<3;i++)
224 for (int j=0;j<3;j++)
225 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
226 // second transpose
227 m->Transpose();
228 for (int i=0;i<3;i++)
229 for (int j=0;j<3;j++)
230 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
231};
232
233/** Unit Test for matrix properties.
234 *
235 */
236void GSLMatrixSymmetricTest::PropertiesTest()
237{
238 // is zero
239 m->SetZero();
240 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
241 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
242 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
243 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
244
245 // is positive
246 m->SetAll(0.5);
247 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
248 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
249 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
250 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
251
252 // is negative
253 m->SetAll(-0.1);
254 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
255 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
256 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
257 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
258
259 // is positive definite
260 double array[] = { 1., 0., 0.,
261 0., 1., 1.,
262 0., 0., 1. };
263 m->SetFromDoubleArray(array);
264 CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() );
265
266 //determinant
267 m->SetIdentity();
268 CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() );
269
270 m->SetZero();
271 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
272
273 m->Set( 0, 0, 1.);
274 m->Set( 1, 1, 1.);
275 m->Set( 2, 1, 1.);
276 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
277
278 double array2[] = { 2., 0., 1.,
279 -3., 1., 1.,
280 1., 5.5, 1. };
281 m->SetFromDoubleArray(array2);
282 CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() );
283};
Note: See TracBrowser for help on using the repository browser.