source: src/unittests/gslmatrixunittest.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.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 * 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 "gslmatrixunittest.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( GSLMatrixTest );
36
37
38void GSLMatrixTest::setUp()
39{
40 m = new GSLMatrix(4,3);
41};
42
43void GSLMatrixTest::tearDown()
44{
45 delete(m);
46};
47
48/** Unit Test for accessing matrix elements.
49 *
50 */
51void GSLMatrixTest::AccessTest()
52{
53 // check whether all elements are initially zero
54 for (int i=0;i<4;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<4;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<4;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<4;i++)
74 for (int j=0;j<3;j++)
75 m->Set(i,j, i*3+j );
76 GSLMatrix *dest = new GSLMatrix(4,3);
77 *dest = *m;
78 for (int i=0;i<4;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(5,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 GSLMatrixTest::InitializationTest()
96{
97 // set zero
98 m->SetZero();
99 for (int i=0;i<4;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<4;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<4;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 0., 0., 0. };
120 m->SetFromDoubleArray(array);
121 for (int i=0;i<4;i++)
122 for (int j=0;j<3;j++)
123 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->Get(i,j) );
124
125};
126
127/** Unit Test for copying matrices.
128 *
129 */
130void GSLMatrixTest::CopyTest()
131{
132 // set basis
133 GSLMatrix *dest = NULL;
134 for (int i=0;i<4;i++) {
135 m->SetAll(i);
136 dest = new GSLMatrix(m);
137 for (int j=0;j<3;j++)
138 CPPUNIT_ASSERT_EQUAL( m->Get(i,j) , dest->Get(i,j) );
139
140 delete(dest);
141 }
142};
143
144/** Unit Test for exchanging rows and columns.
145 *
146 */
147void GSLMatrixTest::ExchangeTest()
148{
149 // set to 1,1,1,2, ...
150 for (int i=0;i<4;i++)
151 for (int j=0;j<3;j++)
152 m->Set(i,j, i+1 );
153
154 // swap such that nothing happens
155 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
156 for (int i=0;i<4;i++)
157 for (int j=0;j<3;j++)
158 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
159
160 // swap two rows
161 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
162 for (int i=0;i<4;i++)
163 for (int j=0;j<3;j++)
164 switch (i) {
165 case 0:
166 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
167 break;
168 case 1:
169 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
170 break;
171 case 2:
172 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
173 break;
174 case 3:
175 CPPUNIT_ASSERT_EQUAL( 4., m->Get(i,j) );
176 break;
177 default:
178 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
179 }
180 // check that op is reversable
181 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
182 for (int i=0;i<4;i++)
183 for (int j=0;j<3;j++)
184 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->Get(i,j) );
185
186 // set to 1,2,3,1, ...
187 for (int i=0;i<4;i++)
188 for (int j=0;j<3;j++)
189 m->Set(i,j, j+1. );
190
191 // swap such that nothing happens
192 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
193 for (int i=0;i<4;i++)
194 for (int j=0;j<3;j++)
195 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
196
197 // swap two columns
198 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
199 for (int i=0;i<4;i++)
200 for (int j=0;j<3;j++)
201 switch (j) {
202 case 0:
203 CPPUNIT_ASSERT_EQUAL( 3., m->Get(i,j) );
204 break;
205 case 1:
206 CPPUNIT_ASSERT_EQUAL( 2., m->Get(i,j) );
207 break;
208 case 2:
209 CPPUNIT_ASSERT_EQUAL( 1., m->Get(i,j) );
210 break;
211 default:
212 CPPUNIT_ASSERT_EQUAL( -1., m->Get(i,j) );
213 }
214 // check that op is reversable
215 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
216 for (int i=0;i<4;i++)
217 for (int j=0;j<3;j++)
218 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->Get(i,j) );
219
220
221 // set to 1,2,3,4, ...
222 for (int i=0;i<4;i++)
223 for (int j=0;j<3;j++)
224 m->Set(i,j, 3*i+j+1 );
225 // transpose
226 CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
227 for (int i=0;i<4;i++)
228 for (int j=0;j<3;j++)
229 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(j,i) );
230 // second transpose
231 CPPUNIT_ASSERT_EQUAL( true, m->Transpose() );
232 for (int i=0;i<4;i++)
233 for (int j=0;j<3;j++)
234 CPPUNIT_ASSERT_EQUAL( (double)(3*i+j+1), m->Get(i,j) );
235};
236
237/** Unit Test for matrix properties.
238 *
239 */
240void GSLMatrixTest::PropertiesTest()
241{
242 // is zero
243 m->SetZero();
244 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
245 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
246 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
247 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
248
249 // is positive
250 m->SetAll(0.5);
251 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
252 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
253 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
254 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
255
256 // is negative
257 m->SetAll(-0.1);
258 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
259 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
260 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
261 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
262
263 // is positive definite
264 CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
265};
Note: See TracBrowser for help on using the repository browser.