/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */ /* Author(s): Ofri Sadowsky Created on: 2004-04-20 (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights Reserved. --- begin cisst license - do not edit --- This software is provided "as is" under an open source license, with no warranty. The complete license can be found in license.txt and http://www.cisst.org/cisst/license.txt. --- end cisst license --- */ #include "vctDeterminantTest.h" #include #include vctDeterminantTest::vctDeterminantTest() : Vec2D(0) , Mat2x2(0) , Vec3D( vctFixedSizeVector(0) ) , RandomSequence( cmnRandomSequence::GetInstance() ) {} void vctDeterminantTest::setUp() { const ElementType tolerance = cmnTypeTraits::Tolerance(); const ElementType minRandom(-10); const ElementType maxRandom(10); while (Vec2D.Norm() < tolerance) { RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec2D.Pointer(), 2); } while (Mat2x2.Norm() < tolerance) { RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Mat2x2.Pointer(), 4); } while (Vec3D[0].Norm() < tolerance) { RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec3D[0].Pointer(), 3); } while (Vec3D[1].Norm() < tolerance) { RandomSequence.ExtractRandomValueArray(minRandom, maxRandom, Vec3D[1].Pointer(), 3); } } void vctDeterminantTest::TestDeterminant2x2ByInverse(const vctFixedSizeMatrix & inputMatrix) { const ElementType determinant = vctDeterminant<2>::Compute(inputMatrix); const ElementType tolerance = cmnTypeTraits::Tolerance(); if (fabs(determinant) < tolerance) { vctFixedSizeVector rowRatio; rowRatio.ElementwiseRatioOf( inputMatrix.Row(0), inputMatrix.Row(1) ); CPPUNIT_ASSERT_DOUBLES_EQUAL( rowRatio[0], rowRatio[1], tolerance ); vctFixedSizeVector columnRatio; columnRatio.ElementwiseRatioOf( inputMatrix.Column(0), inputMatrix.Column(1) ); CPPUNIT_ASSERT_DOUBLES_EQUAL( columnRatio[0], columnRatio[1], tolerance ); return; } const vctFixedSizeMatrix inverse( inputMatrix[1][1] / determinant, -inputMatrix[0][1] / determinant, -inputMatrix[1][0] / determinant, inputMatrix[0][0] / determinant ); /* gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 crashes with internal error on ubuntu 64 - Ubuntu 10.04.1 LTS with const */ /* const */ vctFixedSizeMatrix product( inputMatrix * inverse ); const vctFixedSizeMatrix identity( ElementType(1), ElementType(0), ElementType(0), ElementType(1) ); const vctFixedSizeMatrix difference = product - identity; ElementType diffNorm = difference.Norm(); CPPUNIT_ASSERT_DOUBLES_EQUAL( 0, diffNorm, tolerance ); } void vctDeterminantTest::TestDeterminant2x2ByOrthogonal(const vctFixedSizeVector & inputVector) { const ElementType tolerance = cmnTypeTraits::Tolerance(); const vctFixedSizeVector normalizedInput = inputVector / inputVector.Norm(); const vctFixedSizeMatrix orthogonalMatrix( normalizedInput[0], normalizedInput[1], -normalizedInput[1], normalizedInput[0]); const ElementType determinant = vctDeterminant<2>::Compute(orthogonalMatrix); CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, determinant, tolerance ); TestDeterminant2x2ByInverse( orthogonalMatrix ); } void vctDeterminantTest::TestDeterminant2x2ByLinearCombination(const vctFixedSizeVector & row0) { const ElementType tolerance = cmnTypeTraits::Tolerance(); const vctFixedSizeVector row1 = ElementType(2) * row0; const vctFixedSizeMatrix matrix2x2( row0[0], row0[1], row1[0], row1[1] ); const ElementType determinant = vctDeterminant<2>::Compute(matrix2x2); CPPUNIT_ASSERT_DOUBLES_EQUAL(0, determinant, tolerance); TestDeterminant2x2ByInverse( matrix2x2 ); } void vctDeterminantTest::TestDeterminant3x3ByCrossProduct(const vctFixedSizeVector & row0, const vctFixedSizeVector & row1) { const ElementType tolerance = cmnTypeTraits::Tolerance(); vctFixedSizeVector row2; row2.CrossProductOf( row0, row1 ); const vctFixedSizeMatrix matrix3x3( row0[0], row0[1], row0[2], row1[0], row1[1], row1[2], row2[0], row2[1], row2[2] ); const ElementType determinant = vctDeterminant<3>::Compute(matrix3x3); if (row2.Norm() < tolerance) { CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, determinant, tolerance); } else { CPPUNIT_ASSERT( fabs(determinant) > tolerance ); } } void vctDeterminantTest::TestDeterminant3x3ByLinearCombination(const vctFixedSizeVector & row0, const vctFixedSizeVector & row1) { const ElementType tolerance = cmnTypeTraits::Tolerance(); const vctFixedSizeVector row2 = row0 + row1; const vctFixedSizeMatrix matrix3x3( row0[0], row0[1], row0[2], row1[0], row1[1], row1[2], row2[0], row2[1], row2[2] ); const ElementType determinant = vctDeterminant<3>::Compute(matrix3x3); CPPUNIT_ASSERT_DOUBLES_EQUAL( 0.0, determinant, tolerance ); } CPPUNIT_TEST_SUITE_REGISTRATION(vctDeterminantTest);