source: src/unittests/gslmatrixsymmetricunittest.cpp@ 2793ef

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

Wrapper class for gsl_matrix along with working Unit test.

  • new class GSLMatrix wraps all useful functions of the GSL library.
  • new unit tests GSLMatrixSymmetricUnitTest (symmetric case) and GSLMatrixUnitTest tests each part and is working.

Signed-off-by: Frederik Heber <heber@…>

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