source: ThirdParty/LinearAlgebra/src/unittests/MatrixContentSymmetricUnitTest.cpp@ 6a5921

AutomationFragmentation_failures Candidate_v1.6.1 ChemicalSpaceEvaluator Exclude_Hydrogens_annealWithBondGraph ForceAnnealing_with_BondGraph_contraction-expansion StoppableMakroAction
Last change on this file since 6a5921 was 4ecb2d, checked in by Frederik Heber <heber@…>, 8 years ago

Moved LinearAlgebra sub-package into ThirdParty folder.

  • needed to adapt location of libLinearAlgebra.la in all Makefile.am's.
  • relinked m4 subfolder, relinked am_doxygen_include.am. Both point to those present in molecuilder parent folder.
  • adapted configure.ac's:
  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[6d608d]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]21 */
22
[fc3b67]23/*
[fff54f]24 * MatrixContentSymmetricUnitTest.cpp
[fc3b67]25 *
26 * Created on: Jan 8, 2010
27 * Author: heber
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[fc3b67]35using namespace std;
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
[fff54f]41#include "MatrixContentSymmetricUnitTest.hpp"
[0d4424]42
[bf4b9f]43#include "MatrixContent.hpp"
[fc3b67]44
[9b6b2f]45#ifdef HAVE_TESTRUNNER
46#include "UnitTestMain.hpp"
47#endif /*HAVE_TESTRUNNER*/
48
[fc3b67]49/********************************************** Test classes **************************************/
50
51// Registers the fixture into the 'registry'
[0d4424]52CPPUNIT_TEST_SUITE_REGISTRATION( MatrixContentSymmetricTest );
[fc3b67]53
54
[0d4424]55void MatrixContentSymmetricTest::setUp()
[fc3b67]56{
[0d4424]57 m = new MatrixContent(3,3);
[fc3b67]58};
59
[0d4424]60void MatrixContentSymmetricTest::tearDown()
[fc3b67]61{
62 delete(m);
63};
64
65/** Unit Test for accessing matrix elements.
66 *
67 */
[0d4424]68void MatrixContentSymmetricTest::AccessTest()
[fc3b67]69{
70 // check whether all elements are initially zero
71 for (int i=0;i<3;i++)
72 for (int j=0;j<3;j++)
[0d4424]73 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
[fc3b67]74
75 // set
76 for (int i=0;i<3;i++)
77 for (int j=0;j<3;j++)
[0d4424]78 m->set(i,j, i*3+j );
[fc3b67]79
80 // and check
81 double *ptr = NULL;
82 for (int i=0;i<3;i++)
83 for (int j=0;j<3;j++) {
[0d4424]84 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), m->at(i,j) );
[fc3b67]85 ptr = m->Pointer(i,j);
86 CPPUNIT_ASSERT_EQUAL( (double)(i*3+j), *ptr );
87 }
88
89 // assignment
90 for (int i=0;i<3;i++)
91 for (int j=0;j<3;j++)
[0d4424]92 m->set(i,j, i*3+j );
93 MatrixContent *dest = new MatrixContent(3,3);
[fc3b67]94 *dest = *m;
95 for (int i=0;i<3;i++)
96 for (int j=0;j<3;j++)
[0d4424]97 CPPUNIT_ASSERT_EQUAL( dest->at(i,j), m->at(i,j) );
[fc3b67]98 delete(dest);
99
100 // out of bounds
[0d4424]101 //CPPUNIT_ASSERT_EQUAL(0., v->at(4,2) );
102 //CPPUNIT_ASSERT_EQUAL(0., v->at(2,17) );
103 //CPPUNIT_ASSERT_EQUAL(0., v->at(1024,140040) );
104 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,0) );
105 //CPPUNIT_ASSERT_EQUAL(0., v->at(0,-1) );
106 //CPPUNIT_ASSERT_EQUAL(0., v->at(-1,-1) );
[fc3b67]107};
108
109/** Unit Test for initializating matrices.
110 *
111 */
[0d4424]112void MatrixContentSymmetricTest::InitializationTest()
[fc3b67]113{
114 // set zero
[0d4424]115 m->setZero();
[fc3b67]116 for (int i=0;i<3;i++)
117 for (int j=0;j<3;j++)
[0d4424]118 CPPUNIT_ASSERT_EQUAL( 0., m->at(i,j) );
[fc3b67]119
120 // set all
[0d4424]121 m->setValue(1.5);
[fc3b67]122 for (int i=0;i<3;i++)
123 for (int j=0;j<3;j++)
[0d4424]124 CPPUNIT_ASSERT_EQUAL( 1.5, m->at(i,j) );
[fc3b67]125
126 // set basis
[0d4424]127 m->setIdentity();
[fc3b67]128 for (int i=0;i<3;i++)
129 for (int j=0;j<3;j++)
[0d4424]130 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
[fc3b67]131
132 // set from array
133 double array[] = { 1., 0., 0.,
134 0., 1., 0.,
135 0., 0., 1. };
[0d4424]136 m->setFromDoubleArray(array);
[fc3b67]137 for (int i=0;i<3;i++)
138 for (int j=0;j<3;j++)
[0d4424]139 CPPUNIT_ASSERT_EQUAL( i == j ? 1. : 0. , m->at(i,j) );
[fc3b67]140
141};
142
143/** Unit Test for copying matrices.
144 *
145 */
[0d4424]146void MatrixContentSymmetricTest::CopyTest()
[fc3b67]147{
148 // set basis
[0d4424]149 MatrixContent *dest = NULL;
[fc3b67]150 for (int i=0;i<3;i++) {
[0d4424]151 m->setValue(i);
152 dest = new MatrixContent(m);
[fc3b67]153 for (int j=0;j<3;j++)
[0d4424]154 CPPUNIT_ASSERT_EQUAL( m->at(i,j) , dest->at(i,j) );
[fc3b67]155
156 delete(dest);
157 }
158};
159
160/** Unit Test for exchanging rows and columns.
161 *
162 */
[0d4424]163void MatrixContentSymmetricTest::ExchangeTest()
[fc3b67]164{
165 // set to 1,1,1,2, ...
166 for (int i=0;i<3;i++)
167 for (int j=0;j<3;j++)
[0d4424]168 m->set(i,j, i+1 );
[fc3b67]169
170 // swap such that nothing happens
171 m->SwapColumns(1,2);
172 for (int i=0;i<3;i++)
173 for (int j=0;j<3;j++)
[0d4424]174 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
[fc3b67]175
176 // swap two rows
177 m->SwapRows(1,2);
178 for (int i=0;i<3;i++)
179 for (int j=0;j<3;j++)
180 switch (j) {
181 case 0:
[0d4424]182 CPPUNIT_ASSERT_EQUAL( 1., m->at(j,i) );
[fc3b67]183 break;
184 case 1:
[0d4424]185 CPPUNIT_ASSERT_EQUAL( 3., m->at(j,i) );
[fc3b67]186 break;
187 case 2:
[0d4424]188 CPPUNIT_ASSERT_EQUAL( 2., m->at(j,i) );
[fc3b67]189 break;
190 default:
[0d4424]191 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
[fc3b67]192 }
193 // check that op is reversable
194 m->SwapRows(1,2);
195 for (int i=0;i<3;i++)
196 for (int j=0;j<3;j++)
[0d4424]197 CPPUNIT_ASSERT_EQUAL( (double)i+1., m->at(i,j) );
[fc3b67]198
199 // set to 1,2,3,1, ...
200 for (int i=0;i<3;i++)
201 for (int j=0;j<3;j++)
[0d4424]202 m->set(i,j, j+1. );
[fc3b67]203
204 // swap such that nothing happens
205 m->SwapRows(0,2);
206 for (int i=0;i<3;i++)
207 for (int j=0;j<3;j++)
[0d4424]208 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
[fc3b67]209
210 // swap two columns
211 m->SwapColumns(0,2);
212 for (int i=0;i<3;i++)
213 for (int j=0;j<3;j++)
214 switch (j) {
215 case 0:
[0d4424]216 CPPUNIT_ASSERT_EQUAL( 3., m->at(i,j) );
[fc3b67]217 break;
218 case 1:
[0d4424]219 CPPUNIT_ASSERT_EQUAL( 2., m->at(i,j) );
[fc3b67]220 break;
221 case 2:
[0d4424]222 CPPUNIT_ASSERT_EQUAL( 1., m->at(i,j) );
[fc3b67]223 break;
224 default:
[0d4424]225 CPPUNIT_ASSERT_EQUAL( -1., m->at(i,j) );
[fc3b67]226 }
227 // check that op is reversable
228 m->SwapColumns(0,2);
229 for (int i=0;i<3;i++)
230 for (int j=0;j<3;j++)
[0d4424]231 CPPUNIT_ASSERT_EQUAL( (double)j+1., m->at(i,j) );
[fc3b67]232
233
[0d4424]234 // set to 1,2,3, ...
235 MatrixContent *n = new MatrixContent(3,3);
[fc3b67]236 for (int i=0;i<3;i++)
[0d4424]237 for (int j=0;j<3;j++) {
238 m->set(i,j, 3*i+j+1 );
239 n->set(i,j, 3*j+i+1 );
240 }
[fc3b67]241 // transpose
[26108c]242 MatrixContent res = (*m).transposed();
[0d4424]243 CPPUNIT_ASSERT( *n == res );
[fc3b67]244 // second transpose
[0d4424]245 res.transpose();
246 CPPUNIT_ASSERT( *m == res );
[bbf1bd]247 delete n;
[fc3b67]248};
249
250/** Unit Test for matrix properties.
251 *
252 */
[0d4424]253void MatrixContentSymmetricTest::PropertiesTest()
[fc3b67]254{
255 // is zero
[0d4424]256 m->setZero();
[fc3b67]257 CPPUNIT_ASSERT_EQUAL( true, m->IsNull() );
258 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
259 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
260 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
261
262 // is positive
[0d4424]263 m->setValue(0.5);
[fc3b67]264 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
265 CPPUNIT_ASSERT_EQUAL( true, m->IsPositive() );
266 CPPUNIT_ASSERT_EQUAL( false, m->IsNegative() );
267 CPPUNIT_ASSERT_EQUAL( true, m->IsNonNegative() );
268
269 // is negative
[0d4424]270 m->setValue(-0.1);
[fc3b67]271 CPPUNIT_ASSERT_EQUAL( false, m->IsNull() );
272 CPPUNIT_ASSERT_EQUAL( false, m->IsPositive() );
273 CPPUNIT_ASSERT_EQUAL( true, m->IsNegative() );
274 CPPUNIT_ASSERT_EQUAL( false, m->IsNonNegative() );
275
276 // is positive definite
277 double array[] = { 1., 0., 0.,
278 0., 1., 1.,
279 0., 0., 1. };
[0d4424]280 m->setFromDoubleArray(array);
[fc3b67]281 CPPUNIT_ASSERT_EQUAL( true, m->IsPositiveDefinite() );
[865272f]282
283 //determinant
[0d4424]284 m->setIdentity();
[865272f]285 CPPUNIT_ASSERT_EQUAL( 1., m->Determinant() );
286
[0d4424]287 m->setZero();
[865272f]288 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
289
[0d4424]290 m->set( 0, 0, 1.);
291 m->set( 1, 1, 1.);
292 m->set( 2, 1, 1.);
[865272f]293 CPPUNIT_ASSERT_EQUAL( 0., m->Determinant() );
294
295 double array2[] = { 2., 0., 1.,
296 -3., 1., 1.,
297 1., 5.5, 1. };
[0d4424]298 m->setFromDoubleArray(array2);
[865272f]299 CPPUNIT_ASSERT_EQUAL( -26.5, m->Determinant() );
[fc3b67]300};
Note: See TracBrowser for help on using the repository browser.