1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* *****************************************************************
    MESQUITE -- The Mesh Quality Improvement Toolkit

    Copyright 2004 Sandia Corporation and Argonne National
    Laboratory.  Under the terms of Contract DE-AC04-94AL85000
    with Sandia Corporation, the U.S. Government retains certain
    rights in this software.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    (lgpl.txt) along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    [email protected], [email protected], [email protected],
    [email protected], [email protected], [email protected]

  ***************************************************************** */
//
//    AUTHOR: Thomas Leurent <[email protected]>
//       ORG: Argonne National Laboratory
//    E-MAIL: [email protected]
//
// ORIG-DATE: 12-Nov-02 at 18:05:56
//  LAST-MOD: 15-Apr-04 at 14:57:05 by Thomas Leurent
//
// DESCRIPTION:
// ============
/*! \file Matrix3DTest.cpp

Unit testing of various functions in the Matrix3D class.

*/
// DESCRIP-END.
//

#include "Vector3D.hpp"
#include "Matrix3D.hpp"

#include <cmath>

#include "cppunit/extensions/HelperMacros.h"

using namespace MBMesquite;
using std::cerr;
using std::cout;
using std::endl;

class Matrix3DTest : public CppUnit::TestFixture
{

  private:
    CPPUNIT_TEST_SUITE( Matrix3DTest );
    CPPUNIT_TEST( test_equal );
    CPPUNIT_TEST( test_plus );
    CPPUNIT_TEST( test_minus );
    CPPUNIT_TEST( test_Frobenius_2 );
    CPPUNIT_TEST( test_transpose );
    CPPUNIT_TEST( test_plus_equal );
    CPPUNIT_TEST( test_times_equal_scalar );
    CPPUNIT_TEST( test_times_scalar );
    CPPUNIT_TEST( test_plus_transpose );
    CPPUNIT_TEST( test_plus_transpose_equal );
    CPPUNIT_TEST( test_outer_product );
    CPPUNIT_TEST( test_fill_lower_triangle );
    CPPUNIT_TEST( test_times );
    CPPUNIT_TEST( test_mult_element );
    CPPUNIT_TEST( test_times_vector );
    CPPUNIT_TEST( test_vector_times );
    CPPUNIT_TEST( test_det );
    CPPUNIT_TEST( test_B_times_invA );
    CPPUNIT_TEST_SUITE_END();

  private:
    Vector3D e1, e2, e3;
    Matrix3D mIdentity;
    Matrix3D mMat1;
    Matrix3D mMat1sym;
    Matrix3D mMat2;
    Matrix3D mMat2trans;
    Matrix3D mMat1plus2;
    Matrix3D mMat1plus2trans;
    Matrix3D mMat1times2;
    Matrix3D mMat1times2inv;
    double tolEps;

  public:
    void setUp()
    {
        // sets up the unit vectors
        e1.set( 1, 0, 0 );
        e2.set( 0, 1, 0 );
        e3.set( 0, 0, 1 );

        mIdentity = " 1    0    0 "
                    " 0    1    0 "
                    " 0    0    1";

        mMat1 = " 1    4.2  2 "
                " 5.2  3    4 "
                " 1    7    0.4";

        mMat1sym = " 1    4.2  2 "
                   " 4.2  3    4 "
                   " 2    4    0.4";

        mMat2 = " 2    4    5 "
                " 2    1    3 "
                " 0    7    8 ";

        mMat2trans = " 2    2    0 "
                     " 4    1    7 "
                     " 5    3    8 ";

        mMat1plus2 = " 3    8.2   7 "
                     " 7.2  4     7 "
                     " 1    14    8.4";

        mMat1plus2trans = " 3    6.2   2 "
                          " 9.2  4     11 "
                          " 6    10    8.4";

        mMat1times2 = " 10.4 22.2  33.6 "
                      " 16.4 51.8  67.0 "
                      " 16.0 13.8  29.2 ";

        mMat1times2inv = " 2.519141   0.088216  -0.977863 "
                         " 1.009487   0.304594  -0.593375 "
                         " 5.838881  -0.699068  -2.203728 ";

        tolEps = 1e-12;
    }

    void tearDown() {}

  public:
    Matrix3DTest() {}<--- Member variable 'Matrix3DTest::tolEps' is not initialized in the constructor.

    void test_equal()
    {
        CPPUNIT_ASSERT( mMat1 == mMat1 );
        CPPUNIT_ASSERT( mMat1 != mMat2 );
    }

    void test_plus()
    {
        Matrix3D sum;
        sum = mMat1 + mMat2;
        CPPUNIT_ASSERT( sum == mMat1plus2 );
    }

    void test_minus()
    {
        Matrix3D res;
        res = mMat1 - mIdentity;
        Matrix3D correct( " 0    4.2  2 "
                          " 5.2  2    4 "
                          " 1    7    -0.6 " );
        CPPUNIT_ASSERT( res == correct );
    }

    void test_Frobenius_2()
    {
        double fro = Frobenius_2( mMat1 );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 124.84, fro, tolEps );
    }

    void test_transpose()
    {
        Matrix3D trans = transpose( mMat2 );
        CPPUNIT_ASSERT( trans == mMat2trans );
    }

    void test_plus_equal()
    {
        mMat1 += mMat2;
        CPPUNIT_ASSERT( mMat1 == mMat1plus2 );
    }

    void test_times_equal_scalar()
    {
        mMat2 *= 3;
        Matrix3D correct( " 6   12   15 "
                          " 6    3    9 "
                          " 0   21   24 " );
        CPPUNIT_ASSERT( mMat2 == correct );
    }
    void test_times_scalar()
    {
        Matrix3D tmp = mMat2 * 3;
        Matrix3D correct( " 6   12   15 "
                          " 6    3    9 "
                          " 0   21   24 " );
        CPPUNIT_ASSERT( tmp == correct );
        tmp[0][0] = 0;
        tmp       = 3 * mMat2;
        CPPUNIT_ASSERT( tmp == correct );
    }
    void test_plus_transpose()
    {
        Matrix3D plus_trans = plus_transpose( mMat1, mMat2 );
        CPPUNIT_ASSERT( plus_trans == mMat1plus2trans );
    }

    void test_plus_transpose_equal()
    {
        mMat1.plus_transpose_equal( mMat2 );
        CPPUNIT_ASSERT( mMat1 == mMat1plus2trans );
    }

    void test_outer_product()
    {
        Matrix3D mat;
        Vector3D vec1( 2, 7, 3 );
        Vector3D vec2( 5, 8, 9 );
        mat.outer_product( vec1, vec2 );
        Matrix3D correct( " 10    16    18 "
                          " 35    56    63 "
                          " 15    24    27 " );

        CPPUNIT_ASSERT( mat == correct );
    }

    void test_fill_lower_triangle()
    {
        mMat1.fill_lower_triangle();
        CPPUNIT_ASSERT( mMat1 == mMat1sym );
    }

    void test_times()
    {
        Matrix3D mult = mMat1 * mMat2;
        CPPUNIT_ASSERT( mult == mMat1times2 );

        mult = mMat1 * mIdentity;
        CPPUNIT_ASSERT( mult == mMat1 );
    }

    void test_mult_element()
    {
        Matrix3D mat = mult_element( mMat1, mIdentity );
        Matrix3D correct( " 1 0 0 "
                          " 0 3 0 "
                          " 0 0 0.4" );
        CPPUNIT_ASSERT( mat == correct );
    }

    void test_times_vector()
    {
        Vector3D vec = mMat1 * e1;
        Vector3D correct( 1, 5.2, 1 );
        CPPUNIT_ASSERT( vec == correct );

        Vector3D vec_2( 3., 2., 5. );
        Vector3D vec_12 = mMat1 * vec_2;
        correct.set( 21.4, 41.6, 19. );
        CPPUNIT_ASSERT( vec_12 == correct );
    }

    void test_vector_times()
    {
        Vector3D vec = e1 * mMat1;
        Vector3D correct( 1, 4.2, 2 );
        CPPUNIT_ASSERT( vec == correct );

        Vector3D vec2( 2.1, 3, 8 );
        vec = vec2 * mMat1;
        correct.set( 25.7, 73.82, 19.4 );
        int loop_i = 0;
        for( loop_i = 0; loop_i < 3; ++loop_i )
        {
            CPPUNIT_ASSERT_DOUBLES_EQUAL( vec[loop_i], correct[loop_i], tolEps );
        }
    }

    void test_det()
    {
        double d = det( mMat1 );
        CPPUNIT_ASSERT_DOUBLES_EQUAL( 48.064, d, tolEps );
    }

    void test_B_times_invA()
    {
        Matrix3D orig1( mMat1 );
        timesInvA( mMat2, mMat1 );

        // Checks mMat1 is unchanged
        CPPUNIT_ASSERT( mMat1 == orig1 );

        // Checks mMat2 now contains the correct result
        for( int i = 0; i < 3; ++i )
            for( int j = 0; j < 3; ++j )
                CPPUNIT_ASSERT_DOUBLES_EQUAL( mMat2[i][j], mMat1times2inv[i][j], 0.0001 );
    }
};

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Matrix3DTest, "Matrix3DTest" );
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Matrix3DTest, "Unit" );