nullary.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-2011 Jitse Niesen <jitse@maths.leeds.ac.uk>
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 #include "main.h"
00026 
00027 template<typename MatrixType>
00028 bool equalsIdentity(const MatrixType& A)
00029 {
00030   typedef typename MatrixType::Index Index;
00031   typedef typename MatrixType::Scalar Scalar;
00032   Scalar zero = static_cast<Scalar>(0);
00033 
00034   bool offDiagOK = true;
00035   for (Index i = 0; i < A.rows(); ++i) {
00036     for (Index j = i+1; j < A.cols(); ++j) {
00037       offDiagOK = offDiagOK && (A(i,j) == zero);
00038     }
00039   }
00040   for (Index i = 0; i < A.rows(); ++i) {
00041     for (Index j = 0; j < std::min(i, A.cols()); ++j) {
00042       offDiagOK = offDiagOK && (A(i,j) == zero);
00043     }
00044   }
00045 
00046   bool diagOK = (A.diagonal().array() == 1).all();
00047   return offDiagOK && diagOK;
00048 }
00049 
00050 template<typename VectorType>
00051 void testVectorType(const VectorType& base)
00052 {
00053   typedef typename internal::traits<VectorType>::Index Index;
00054   typedef typename internal::traits<VectorType>::Scalar Scalar;
00055   Scalar low = internal::random<Scalar>(-500,500);
00056   Scalar high = internal::random<Scalar>(-500,500);
00057   if (low>high) std::swap(low,high);
00058   const Index size = base.size();
00059   const Scalar step = (high-low)/(size-1);
00060 
00061   // check whether the result yields what we expect it to do
00062   VectorType m(base);
00063   m.setLinSpaced(size,low,high);
00064 
00065   VectorType n(size);
00066   for (int i=0; i<size; ++i)
00067     n(i) = low+i*step;
00068 
00069   VERIFY_IS_APPROX(m,n);
00070 
00071   // random access version
00072   m = VectorType::LinSpaced(size,low,high);
00073   VERIFY_IS_APPROX(m,n);
00074 
00075   // Assignment of a RowVectorXd to a MatrixXd (regression test for bug #79).
00076   VERIFY( (MatrixXd(RowVectorXd::LinSpaced(3, 0, 1)) - RowVector3d(0, 0.5, 1)).norm() < std::numeric_limits<Scalar>::epsilon() );
00077 
00078   // These guys sometimes fail! This is not good. Any ideas how to fix them!?
00079 //   VERIFY( m(m.size()-1) == high );
00080 //   VERIFY( m(0) == low );
00081 
00082   // sequential access version
00083   m = VectorType::LinSpaced(Sequential,size,low,high);
00084   VERIFY_IS_APPROX(m,n);
00085 
00086   // These guys sometimes fail! This is not good. Any ideas how to fix them!?
00087   //VERIFY( m(m.size()-1) == high );
00088   //VERIFY( m(0) == low );
00089 
00090   // check whether everything works with row and col major vectors
00091   Matrix<Scalar,Dynamic,1> row_vector(size);
00092   Matrix<Scalar,1,Dynamic> col_vector(size);
00093   row_vector.setLinSpaced(size,low,high);
00094   col_vector.setLinSpaced(size,low,high);
00095   VERIFY( row_vector.isApprox(col_vector.transpose(), NumTraits<Scalar>::epsilon()));
00096 
00097   Matrix<Scalar,Dynamic,1> size_changer(size+50);
00098   size_changer.setLinSpaced(size,low,high);
00099   VERIFY( size_changer.size() == size );
00100 }
00101 
00102 template<typename MatrixType>
00103 void testMatrixType(const MatrixType& m)
00104 {
00105   typedef typename MatrixType::Index Index;
00106   const Index rows = m.rows();
00107   const Index cols = m.cols();
00108 
00109   MatrixType A;
00110   A.setIdentity(rows, cols);
00111   VERIFY(equalsIdentity(A));
00112   VERIFY(equalsIdentity(MatrixType::Identity(rows, cols)));
00113 }
00114 
00115 void test_nullary()
00116 {
00117   CALL_SUBTEST_1( testMatrixType(Matrix2d()) );
00118   CALL_SUBTEST_2( testMatrixType(MatrixXcf(internal::random<int>(1,300),internal::random<int>(1,300))) );
00119   CALL_SUBTEST_3( testMatrixType(MatrixXf(internal::random<int>(1,300),internal::random<int>(1,300))) );
00120   
00121   for(int i = 0; i < g_repeat; i++) {
00122     CALL_SUBTEST_4( testVectorType(VectorXd(internal::random<int>(1,300))) );
00123     CALL_SUBTEST_5( testVectorType(Vector4d()) );  // regression test for bug 232
00124     CALL_SUBTEST_6( testVectorType(Vector3d()) );
00125     CALL_SUBTEST_7( testVectorType(VectorXf(internal::random<int>(1,300))) );
00126     CALL_SUBTEST_8( testVectorType(Vector3f()) );
00127   }
00128 }


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