source: src/unittests/gslmatrixunittest.cpp@ 701ad6

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