source: src/unittests/MatrixContentSymmetricUnittest.cpp@ 4c1230

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 4c1230 was bbf1bd, checked in by Frederik Heber <heber@…>, 14 years ago

Extended VectorContent class.

  • VectorContent formerly has been just a structure to contain the gsl_vector due to forward declaration reasons.
  • now VectorContent is a true wrapper to gsl_vector, i.e. all functionality that is now specific to 3 dimensions has been shifted from GSLVector over to VectorContent.
  • VectorContentView is preserved to allow for VectorContent as a view on a row or column of a matrix.
  • changed and renamed unit test gslvectorunittest -> VectorContentUnitTest
  • GSLVector is not used anymore anywhere
  • one long-sough error was a missing assignment operator filled-in in a wrong manner by the compiler for VectorContent.

Note that:

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