integer_types.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #define EIGEN_NO_STATIC_ASSERT
00026 
00027 #include "main.h"
00028 
00029 #undef VERIFY_IS_APPROX
00030 #define VERIFY_IS_APPROX(a, b) VERIFY((a)==(b));
00031 #undef VERIFY_IS_NOT_APPROX
00032 #define VERIFY_IS_NOT_APPROX(a, b) VERIFY((a)!=(b));
00033 
00034 template<typename MatrixType> void signed_integer_type_tests(const MatrixType& m)
00035 {
00036   typedef typename MatrixType::Index Index;
00037   typedef typename MatrixType::Scalar Scalar;
00038 
00039   enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
00040   VERIFY(is_signed == 1);
00041 
00042   Index rows = m.rows();
00043   Index cols = m.cols();
00044 
00045   MatrixType m1(rows, cols),
00046              m2 = MatrixType::Random(rows, cols),
00047              mzero = MatrixType::Zero(rows, cols);
00048 
00049   do {
00050     m1 = MatrixType::Random(rows, cols);
00051   } while(m1 == mzero || m1 == m2);
00052 
00053   // check linear structure
00054 
00055   Scalar s1;
00056   do {
00057     s1 = internal::random<Scalar>();
00058   } while(s1 == 0);
00059 
00060   VERIFY_IS_EQUAL(-(-m1),                  m1);
00061   VERIFY_IS_EQUAL(-m2+m1+m2,               m1);
00062   VERIFY_IS_EQUAL((-m1+m2)*s1,             -s1*m1+s1*m2);
00063 }
00064 
00065 template<typename MatrixType> void integer_type_tests(const MatrixType& m)
00066 {
00067   typedef typename MatrixType::Index Index;
00068   typedef typename MatrixType::Scalar Scalar;
00069 
00070   VERIFY(NumTraits<Scalar>::IsInteger);
00071   enum { is_signed = (Scalar(-1) > Scalar(0)) ? 0 : 1 };
00072   VERIFY(int(NumTraits<Scalar>::IsSigned) == is_signed);
00073 
00074   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00075 
00076   Index rows = m.rows();
00077   Index cols = m.cols();
00078 
00079   // this test relies a lot on Random.h, and there's not much more that we can do
00080   // to test it, hence I consider that we will have tested Random.h
00081   MatrixType m1(rows, cols),
00082              m2 = MatrixType::Random(rows, cols),
00083              m3(rows, cols),
00084              mzero = MatrixType::Zero(rows, cols);
00085 
00086   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
00087   SquareMatrixType identity = SquareMatrixType::Identity(rows, rows),
00088                    square = SquareMatrixType::Random(rows, rows);
00089   VectorType v1(rows),
00090              v2 = VectorType::Random(rows),
00091              vzero = VectorType::Zero(rows);
00092 
00093   do {
00094     m1 = MatrixType::Random(rows, cols);
00095   } while(m1 == mzero || m1 == m2);
00096 
00097   do {
00098     v1 = VectorType::Random(rows);
00099   } while(v1 == vzero || v1 == v2);
00100 
00101   VERIFY_IS_APPROX(               v1,    v1);
00102   VERIFY_IS_NOT_APPROX(           v1,    2*v1);
00103   VERIFY_IS_APPROX(               vzero, v1-v1);
00104   VERIFY_IS_APPROX(               m1,    m1);
00105   VERIFY_IS_NOT_APPROX(           m1,    2*m1);
00106   VERIFY_IS_APPROX(               mzero, m1-m1);
00107 
00108   VERIFY_IS_APPROX(m3 = m1,m1);
00109   MatrixType m4;
00110   VERIFY_IS_APPROX(m4 = m1,m1);
00111 
00112   m3.real() = m1.real();
00113   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), static_cast<const MatrixType&>(m1).real());
00114   VERIFY_IS_APPROX(static_cast<const MatrixType&>(m3).real(), m1.real());
00115 
00116   // check == / != operators
00117   VERIFY(m1==m1);
00118   VERIFY(m1!=m2);
00119   VERIFY(!(m1==m2));
00120   VERIFY(!(m1!=m1));
00121   m1 = m2;
00122   VERIFY(m1==m2);
00123   VERIFY(!(m1!=m2));
00124 
00125   // check linear structure
00126 
00127   Scalar s1;
00128   do {
00129     s1 = internal::random<Scalar>();
00130   } while(s1 == 0);
00131 
00132   VERIFY_IS_EQUAL(m1+m1,                   2*m1);
00133   VERIFY_IS_EQUAL(m1+m2-m1,                m2);
00134   VERIFY_IS_EQUAL(m1*s1,                   s1*m1);
00135   VERIFY_IS_EQUAL((m1+m2)*s1,              s1*m1+s1*m2);
00136   m3 = m2; m3 += m1;
00137   VERIFY_IS_EQUAL(m3,                      m1+m2);
00138   m3 = m2; m3 -= m1;
00139   VERIFY_IS_EQUAL(m3,                      m2-m1);
00140   m3 = m2; m3 *= s1;
00141   VERIFY_IS_EQUAL(m3,                      s1*m2);
00142 
00143   // check matrix product.
00144 
00145   VERIFY_IS_APPROX(identity * m1, m1);
00146   VERIFY_IS_APPROX(square * (m1 + m2), square * m1 + square * m2);
00147   VERIFY_IS_APPROX((m1 + m2).transpose() * square, m1.transpose() * square + m2.transpose() * square);
00148   VERIFY_IS_APPROX((m1 * m2.transpose()) * m1, m1 * (m2.transpose() * m1));
00149 }
00150 
00151 void test_integer_types()
00152 {
00153   for(int i = 0; i < g_repeat; i++) {
00154     CALL_SUBTEST_1( integer_type_tests(Matrix<unsigned int, 1, 1>()) );
00155     CALL_SUBTEST_1( integer_type_tests(Matrix<unsigned long, 3, 4>()) );
00156 
00157     CALL_SUBTEST_2( integer_type_tests(Matrix<long, 2, 2>()) );
00158     CALL_SUBTEST_2( signed_integer_type_tests(Matrix<long, 2, 2>()) );
00159 
00160     CALL_SUBTEST_3( integer_type_tests(Matrix<char, 2, Dynamic>(2, 10)) );
00161     CALL_SUBTEST_3( signed_integer_type_tests(Matrix<signed char, 2, Dynamic>(2, 10)) );
00162 
00163     CALL_SUBTEST_4( integer_type_tests(Matrix<unsigned char, 3, 3>()) );
00164     CALL_SUBTEST_4( integer_type_tests(Matrix<unsigned char, Dynamic, Dynamic>(20, 20)) );
00165 
00166     CALL_SUBTEST_5( integer_type_tests(Matrix<short, Dynamic, 4>(7, 4)) );
00167     CALL_SUBTEST_5( signed_integer_type_tests(Matrix<short, Dynamic, 4>(7, 4)) );
00168 
00169     CALL_SUBTEST_6( integer_type_tests(Matrix<unsigned short, 4, 4>()) );
00170 
00171     CALL_SUBTEST_7( integer_type_tests(Matrix<long long, 11, 13>()) );
00172     CALL_SUBTEST_7( signed_integer_type_tests(Matrix<long long, 11, 13>()) );
00173 
00174     CALL_SUBTEST_8( integer_type_tests(Matrix<unsigned long long, Dynamic, 5>(1, 5)) );
00175   }
00176 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:25