source: LinearAlgebra/src/unittests/MatrixContentUnitTest.cpp@ 68c923

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

Lots of small compiler warning fixes.

Issues:

  • non-virtual destructor despite virtual functions.
  • unused variables.
  • no break at end of switch case.
  • no value returned.
  • Property mode set to 100644
File size: 9.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 * MatrixContentUnitTest.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 <cmath>
27#include <limits>
28
29// include headers that implement a archive in simple text format
30#include <boost/archive/text_oarchive.hpp>
31#include <boost/archive/text_iarchive.hpp>
32
33#include "MatrixContentUnitTest.hpp"
34
35#include "MatrixContent.hpp"
36#include "VectorContent.hpp"
37
38#ifdef HAVE_TESTRUNNER
39#include "UnitTestMain.hpp"
40#endif /*HAVE_TESTRUNNER*/
41
42/********************************************** Test classes **************************************/
43
44const std::string matrixA = " 9.962848439806617e-01 5.950413028139907e-01 3.172650036465889e-01\n\
45 2.070939109924951e-01 8.365712117773689e-01 3.980567886073226e-01\n\
46 1.021600573071414e-01 8.061359922326598e-02 8.493537275043391e-01\n\
47 6.955361514014282e-01 6.873206387346102e-02 2.649165458481005e-01";
48const std::string vectorS = "1.630290761001098e+00 7.624807744374841e-01 6.374093742147465e-01";
49
50// Registers the fixture into the 'registry'
51CPPUNIT_TEST_SUITE_REGISTRATION( MatrixContentTest );
52
53
54void MatrixContentTest::setUp()
55{
56 m = new MatrixContent(4,3);
57};
58
59void MatrixContentTest::tearDown()
60{
61 delete(m);
62};
63
64/** Unit Test for accessing matrix elements.
65 *
66 */
67void MatrixContentTest::AccessTest()
68{
69 // check whether all elements are initially zero
70 for (int i=0;i<4;i++)
71 for (int j=0;j<3;j++)
72 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
73
74 // set
75 for (int i=0;i<4;i++)
76 for (int j=0;j<3;j++)
77 m->set(i,j, i*3+j );
78
79 // and check
80 double *ptr = NULL;
81 for (int i=0;i<4;i++)
82 for (int j=0;j<3;j++) {
83 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->at(i,j) );
84 ptr = m->Pointer(i,j);
85 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
86 }
87
88 // assignment
89 for (int i=0;i<4;i++)
90 for (int j=0;j<3;j++)
91 m->set(i,j, i*3+j );
92 MatrixContent *dest = new MatrixContent(4,3);
93 *dest = *m;
94 for (int i=0;i<4;i++)
95 for (int j=0;j<3;j++)
96 CPPUNIT_ASSERT_EQUAL( dest->at(i,j), m->at(i,j) );
97 delete(dest);
98
99 // out of bounds
100 //CPPUNIT_ASSERT_EQUAL(0., v->at(5,2) );
101 //CPPUNIT_ASSERT_EQUAL(0., v->at(2,17) );
102 //CPPUNIT_ASSERT_EQUAL(0., v->at(1024,140040) );
103 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,0) );
104 //CPPUNIT_ASSERT_EQUAL(0., v->at(0,-1) );
105 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,-1) );
106};
107
108/** Unit Test for initializating matrices.
109 *
110 */
111void MatrixContentTest::InitializationTest()
112{
113 // set zero
114 m->setZero();
115 for (int i=0;i<4;i++)
116 for (int j=0;j<3;j++)
117 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
118
119 // set all
120 m->setValue(1.5);
121 for (int i=0;i<4;i++)
122 for (int j=0;j<3;j++)
123 CPPUNIT_ASSERT_EQUAL( 1.5, m->at(i,j) );
124
125 // set basis
126 m->setIdentity();
127 for (int i=0;i<4;i++)
128 for (int j=0;j<3;j++)
129 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
130
131 // set from array
132 double array[] = { 1., 0., 0.,
133 0., 1., 0.,
134 0., 0., 1.,
135 0., 0., 0. };
136 m->setFromDoubleArray(array);
137 for (int i=0;i<4;i++)
138 for (int j=0;j<3;j++)
139 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
140
141};
142
143/** Unit Test for copying matrices.
144 *
145 */
146void MatrixContentTest::CopyTest()
147{
148 // set basis
149 MatrixContent *dest = NULL;
150 for (int i=0;i<4;i++) {
151 m->setValue(i);
152 dest = new MatrixContent(m);
153 for (int j=0;j<3;j++)
154 CPPUNIT_ASSERT_EQUAL( m->at(i,j) , dest->at(i,j) );
155
156 delete(dest);
157 }
158};
159
160/** Unit Test for exchanging rows and columns.
161 *
162 */
163void MatrixContentTest::ExchangeTest()
164{
165 // set to 1,1,1,2, ...
166 for (int i=0;i<4;i++)
167 for (int j=0;j<3;j++)
168 m->set(i,j, i+1 );
169
170 // swap such that nothing happens
171 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(1,2) );
172 for (int i=0;i<4;i++)
173 for (int j=0;j<3;j++)
174 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
175
176 // swap two rows
177 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
178 for (int i=0;i<4;i++)
179 for (int j=0;j<3;j++)
180 switch (i) {
181 case 0:
182 CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
183 break;
184 case 1:
185 CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
186 break;
187 case 2:
188 CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
189 break;
190 case 3:
191 CPPUNIT_ASSERT_EQUAL( 4., m->at(i,j) );
192 break;
193 default:
194 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
195 break;
196 }
197 // check that op is reversable
198 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(1,2) );
199 for (int i=0;i<4;i++)
200 for (int j=0;j<3;j++)
201 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
202
203 // set to 1,2,3,1, ...
204 for (int i=0;i<4;i++)
205 for (int j=0;j<3;j++)
206 m->set(i,j, j+1. );
207
208 // swap such that nothing happens
209 CPPUNIT_ASSERT_EQUAL( true, m->SwapRows(0,2) );
210 for (int i=0;i<4;i++)
211 for (int j=0;j<3;j++)
212 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
213
214 // swap two columns
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 switch (j) {
219 case 0:
220 CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
221 break;
222 case 1:
223 CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
224 break;
225 case 2:
226 CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
227 break;
228 default:
229 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
230 break;
231 }
232 // check that op is reversable
233 CPPUNIT_ASSERT_EQUAL( true, m->SwapColumns(0,2) );
234 for (int i=0;i<4;i++)
235 for (int j=0;j<3;j++)
236 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
237
238
239 // set to 1,2,3,4, ...
240 MatrixContent *n = new MatrixContent(3,4);
241 for (int i=0;i<4;i++)
242 for (int j=0;j<3;j++) {
243 m->set(i,j, 3*i+j+1 );
244 n->set(j,i, 3*i+j+1 );
245 }
246 // transpose
247 MatrixContent res = (*m).transposed();
248 CPPUNIT_ASSERT( *n == res );
249 // second transpose
250 MatrixContent res2 = res.transposed();
251 CPPUNIT_ASSERT( *m == res2 );
252 delete n;
253};
254
255/** Unit Test for matrix properties.
256 *
257 */
258void MatrixContentTest::PropertiesTest()
259{
260 // is zero
261 m->setZero();
262 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
263 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
264 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
265 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
266
267 // is positive
268 m->setValue(0.5);
269 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
270 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
271 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
272 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
273
274 // is negative
275 m->setValue(-0.1);
276 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
277 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
278 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
279 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
280
281 // is positive definite
282 CPPUNIT_ASSERT_EQUAL( false, m->IsPositiveDefinite() );
283};
284
285
286/** Unit test for reading from and writing matrix to stream
287 *
288 */
289void MatrixContentTest::ReadWriteTest()
290{
291 // set up matrix
292 for (int i=0;i<4;i++)
293 for (int j=0;j<3;j++)
294 m->set(i,j, i*3+j );
295
296 // write to stream
297 std::stringstream matrixstream;
298 m->write(matrixstream);
299
300 // parse in dimensions and check
301 std::pair<size_t, size_t> matrixdimensions = MatrixContent::preparseMatrixDimensions(matrixstream);
302 CPPUNIT_ASSERT_EQUAL( (size_t)4, matrixdimensions.first );
303 CPPUNIT_ASSERT_EQUAL( (size_t)3, matrixdimensions.second );
304 // parse in matrix and check
305 MatrixContent* n = new MatrixContent(4,3, matrixstream);
306 CPPUNIT_ASSERT_EQUAL( *m, *n );
307
308 // free matrix
309 delete n;
310}
311
312/** Unit test for MatrixContent::performSingularValueDecomposition().
313 *
314 */
315void MatrixContentTest::SVDTest()
316{
317 // setup "A", U,S,V
318 std::stringstream Astream(matrixA);
319 std::stringstream Sstream(vectorS);
320 MatrixContent A(m->getRows(), m->getColumns(), Astream);
321 VectorContent S_expected(m->getColumns(), Sstream);
322 *m = A;
323
324 // check SVD
325 VectorContent S(m->getColumns());
326 MatrixContent V(m->getColumns(), m->getColumns());
327 A.performSingularValueDecomposition(V,S);
328 MatrixContent S_diag(m->getColumns(), m->getColumns());
329 for (size_t index = 0; index < m->getColumns(); ++index)
330 S_diag.set(index, index, S[index]);
331 CPPUNIT_ASSERT_EQUAL(*m,A*S_diag*(V.transpose()));
332 for (size_t index = 0; index < m->getColumns(); ++index) {
333 //CPPUNIT_ASSERT_EQUAL(S_expected[index], S[index]);
334 CPPUNIT_ASSERT(fabs(S_expected[index] - S[index]) < numeric_limits<double>::epsilon()*10.);
335 }
336
337 // set up right-hand side
338 VectorContent b(4);
339 b[0] = 1.;
340 b[1] = 0.;
341 b[2] = 0.;
342 b[3] = 0.;
343
344 // SVD
345 VectorContent x_expected(3);
346 x_expected[0] = -0.00209169;
347 x_expected[1] = -0.325399;
348 x_expected[2] = 0.628004;
349 const VectorContent x_result(A.solveOverdeterminedLinearEquation(V,S,b));
350
351 // check result
352 for (size_t index = 0; index < m->getColumns(); ++index) {
353 CPPUNIT_ASSERT(fabs(x_expected[index] - x_result[index]) < 1.e-6);
354 }
355}
356
357/** Unit test for serialization
358 *
359 */
360void MatrixContentTest::SerializationTest()
361{
362 m->setZero();
363 int count=0;
364 for (size_t x = 0; x < 4 ; ++x)
365 for (size_t y = 0; y < 3 ; ++y)
366 m->at(x,y) = count++;
367 // write element to stream
368 std::stringstream stream;
369 boost::archive::text_oarchive oa(stream);
370 oa << m;
371
372 //std::cout << "Contents of archive is " << stream.str() << std::endl;
373
374 // create and open an archive for input
375 boost::archive::text_iarchive ia(stream);
376 // read class state from archive
377 MatrixContent *newm;
378
379 ia >> newm;
380
381 CPPUNIT_ASSERT (*m == *newm);
382}
Note: See TracBrowser for help on using the repository browser.