source: src/unittests/gslmatrixsymmetricunittest.cpp@ 13e3c3

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

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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