diff options
author | 2008-03-08 19:02:24 +0000 | |
---|---|---|
committer | 2008-03-08 19:02:24 +0000 | |
commit | 721626dfc5cab61ded1a26357b6c3dbb33763dc0 (patch) | |
tree | b0ca850c64a5bfccdd300cb49baf2e469640d276 | |
parent | 138aad0ed03cb9045b1f74ce52f0e47ce02966c8 (diff) |
* Added support for a comma initializer: mat.block(i,j,2,2) << 1, 2, 3, 4;
If the number of coefficients does not match the matrix size, then an assertion is raised.
No support for xpr on the right side for the moment.
* Added support for assertion checking. This allows to test that an assertion is indeed raised
when it should be.
* Fixed a mistake in the CwiseUnary example.
-rw-r--r-- | Eigen/Core | 3 | ||||
-rw-r--r-- | Eigen/src/Core/CommaInitializer.h | 67 | ||||
-rw-r--r-- | Eigen/src/Core/CwiseBinaryOp.h | 26 | ||||
-rw-r--r-- | Eigen/src/Core/CwiseUnaryOp.h | 17 | ||||
-rw-r--r-- | Eigen/src/Core/MatrixBase.h | 6 | ||||
-rw-r--r-- | Eigen/src/Core/Util.h | 9 | ||||
-rw-r--r-- | doc/Doxyfile.in | 66 | ||||
-rw-r--r-- | doc/examples/class_CwiseUnaryOp.cpp | 7 | ||||
-rw-r--r-- | doc/snippets/MatrixBase_set.cpp | 8 | ||||
-rw-r--r-- | test/CMakeLists.txt | 17 | ||||
-rw-r--r-- | test/basicstuff.cpp | 18 | ||||
-rw-r--r-- | test/main.h | 79 | ||||
-rw-r--r-- | test/product.cpp | 3 |
13 files changed, 259 insertions, 67 deletions
diff --git a/Eigen/Core b/Eigen/Core index eda3fd82a..e52ee96e9 100644 --- a/Eigen/Core +++ b/Eigen/Core @@ -1,7 +1,9 @@ #include <cstdlib> #include <cmath> #include <complex> +#ifndef EIGEN_CUSTOM_ASSERT #include <cassert> +#endif #include <iostream> namespace Eigen { @@ -37,6 +39,7 @@ namespace Eigen { #include "src/Core/Map.h" #include "src/Core/IO.h" #include "src/Core/Swap.h" +#include "src/Core/CommaInitializer.h" } // namespace Eigen diff --git a/Eigen/src/Core/CommaInitializer.h b/Eigen/src/Core/CommaInitializer.h new file mode 100644 index 000000000..0e6674be1 --- /dev/null +++ b/Eigen/src/Core/CommaInitializer.h @@ -0,0 +1,67 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr> +// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr> +// +// Eigen 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 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen 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 or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see <http://www.gnu.org/licenses/>. + +#ifndef EIGEN_COMMA_INITIALIZER_H +#define EIGEN_COMMA_INITIALIZER_H + +template<typename Scalar, typename Derived> +struct MatrixBase<Scalar, Derived>::CommaInitializer +{ + CommaInitializer(Derived& mat) : m_matrix(mat), m_count(1) {} + + CommaInitializer& operator,(const Scalar& s) { + assert(m_count<m_matrix.size() && "Too many coefficients passed to Matrix::operator<<"); + m_matrix._coeffRef(m_count/m_matrix.cols(), m_count%m_matrix.cols()) = s; + m_count++; + return *this; + } + + ~CommaInitializer(void) + { + assert(m_count==m_matrix.size() && "Too few coefficients passed to Matrix::operator<<"); + } + + Derived& m_matrix; + int m_count; +}; + + +/** Convenient operator to set the coefficients of a matrix. + * + * The coefficients must be provided in a row major order and exactly match + * the size of the matrix. Otherwise an assertion is raised. + * + * Example: \include MatrixBase_set.cpp + * Output: \verbinclude MatrixBase_set.out + */ +template<typename Scalar, typename Derived> +typename MatrixBase<Scalar, Derived>::CommaInitializer MatrixBase<Scalar, Derived>::operator<< (const Scalar& s) +{ + coeffRef(0,0) = s; + return CommaInitializer(*static_cast<Derived *>(this)); +} + + +#endif // EIGEN_COMMA_INITIALIZER_H diff --git a/Eigen/src/Core/CwiseBinaryOp.h b/Eigen/src/Core/CwiseBinaryOp.h index cdcc329b5..9d3dd695c 100644 --- a/Eigen/src/Core/CwiseBinaryOp.h +++ b/Eigen/src/Core/CwiseBinaryOp.h @@ -89,23 +89,26 @@ class CwiseBinaryOp : NoOperatorEquals, const BinaryOp m_functor; }; -/** \brief Template functor to compute the sum of two scalars +/** \internal + * \brief Template functor to compute the sum of two scalars * * \sa class CwiseBinaryOp, MatrixBase::operator+ */ -struct CwiseSumOp EIGEN_EMPTY_STRUCT { +struct ScalarSumOp EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a, const Scalar& b) const { return a + b; } }; -/** \brief Template functor to compute the difference of two scalars +/** \internal + * \brief Template functor to compute the difference of two scalars * * \sa class CwiseBinaryOp, MatrixBase::operator- */ -struct CwiseDifferenceOp EIGEN_EMPTY_STRUCT { +struct ScalarDifferenceOp EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a, const Scalar& b) const { return a - b; } }; -/** \brief Template functor to compute the product of two scalars +/** \internal + * \brief Template functor to compute the product of two scalars * * \sa class CwiseBinaryOp, MatrixBase::cwiseProduct() */ @@ -113,7 +116,8 @@ struct ScalarProductOp EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a, const Scalar& b) const { return a * b; } }; -/** \brief Template functor to compute the quotient of two scalars +/** \internal + * \brief Template functor to compute the quotient of two scalars * * \sa class CwiseBinaryOp, MatrixBase::cwiseQuotient() */ @@ -128,10 +132,10 @@ struct ScalarQuotientOp EIGEN_EMPTY_STRUCT { * \sa class CwiseBinaryOp, MatrixBase::operator-=() */ template<typename Scalar, typename Derived1, typename Derived2> -const CwiseBinaryOp<CwiseDifferenceOp, Derived1, Derived2> +const CwiseBinaryOp<ScalarDifferenceOp, Derived1, Derived2> operator-(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2) { - return CwiseBinaryOp<CwiseDifferenceOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg()); + return CwiseBinaryOp<ScalarDifferenceOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg()); } /** replaces \c *this by \c *this - \a other. @@ -154,10 +158,10 @@ MatrixBase<Scalar, Derived>::operator-=(const MatrixBase<Scalar, OtherDerived> & * \sa class CwiseBinaryOp, MatrixBase::operator+=() */ template<typename Scalar, typename Derived1, typename Derived2> -const CwiseBinaryOp<CwiseSumOp, Derived1, Derived2> +const CwiseBinaryOp<ScalarSumOp, Derived1, Derived2> operator+(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2) { - return CwiseBinaryOp<CwiseSumOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg()); + return CwiseBinaryOp<ScalarSumOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg()); } /** replaces \c *this by \c *this + \a other. @@ -203,7 +207,7 @@ MatrixBase<Scalar, Derived>::cwiseQuotient(const MatrixBase<Scalar, OtherDerived * * The template parameter \a CustomBinaryOp is the type of the functor * of the custom operator (see class CwiseBinaryOp for an example) - * + * * \sa class CwiseBinaryOp, MatrixBase::operator+, MatrixBase::operator-, MatrixBase::cwiseProduct, MatrixBase::cwiseQuotient */ template<typename Scalar, typename Derived> diff --git a/Eigen/src/Core/CwiseUnaryOp.h b/Eigen/src/Core/CwiseUnaryOp.h index c51ad26bc..51a037f24 100644 --- a/Eigen/src/Core/CwiseUnaryOp.h +++ b/Eigen/src/Core/CwiseUnaryOp.h @@ -76,7 +76,8 @@ class CwiseUnaryOp : NoOperatorEquals, const UnaryOp m_functor; }; -/** \brief Template functor to compute the opposite of a scalar +/** \internal + * \brief Template functor to compute the opposite of a scalar * * \sa class CwiseUnaryOp, MatrixBase::operator- */ @@ -84,7 +85,8 @@ struct ScalarOppositeOp EIGEN_EMPTY_STRUCT { template<typename Scalar> Scalar operator() (const Scalar& a) const { return -a; } }; -/** \brief Template functor to compute the absolute value of a scalar +/** \internal + * \brief Template functor to compute the absolute value of a scalar * * \sa class CwiseUnaryOp, MatrixBase::cwiseAbs */ @@ -116,7 +118,7 @@ MatrixBase<Scalar, Derived>::cwiseAbs() const * * The template parameter \a CustomUnaryOp is the type of the functor * of the custom unary operator. - * + * * Here is an example: * \include class_CwiseUnaryOp.cpp * @@ -131,7 +133,8 @@ MatrixBase<Scalar, Derived>::cwise(const CustomUnaryOp& func) const } -/** \brief Template functor to compute the conjugate of a complex value +/** \internal + * \brief Template functor to compute the conjugate of a complex value * * \sa class CwiseUnaryOp, MatrixBase::conjugate() */ @@ -149,7 +152,8 @@ MatrixBase<Scalar, Derived>::conjugate() const return CwiseUnaryOp<ScalarConjugateOp, Derived>(asArg()); } -/** \brief Template functor to cast a scalar to another +/** \internal + * \brief Template functor to cast a scalar to another * * \sa class CwiseUnaryOp, MatrixBase::cast() */ @@ -178,7 +182,8 @@ MatrixBase<Scalar, Derived>::cast() const } -/** \brief Template functor to multiply a scalar by a fixed another one +/** \internal + * \brief Template functor to multiply a scalar by a fixed another one * * \sa class CwiseUnaryOp, MatrixBase::operator*, MatrixBase::operator/ */ diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index c89d3784c..e924fa329 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -54,6 +54,8 @@ */ template<typename Scalar, typename Derived> class MatrixBase { + struct CommaInitializer; + public: /** \brief Some traits provided by the Derived type. @@ -166,7 +168,6 @@ template<typename Scalar, typename Derived> class MatrixBase AsArg asArg() const { return static_cast<const Derived *>(this)->_asArg(); } - //@{ /** Copies \a other into *this. \returns a reference to *this. */ template<typename OtherDerived> Derived& operator=(const MatrixBase<Scalar, OtherDerived>& other); @@ -179,6 +180,8 @@ template<typename Scalar, typename Derived> class MatrixBase return this->operator=<Derived>(other); } + CommaInitializer operator<< (const Scalar& s); + /** swaps *this with the expression \a other. * * \note \a other is only marked const because I couln't find another way @@ -187,7 +190,6 @@ template<typename Scalar, typename Derived> class MatrixBase */ template<typename OtherDerived> void swap(const MatrixBase<Scalar, OtherDerived>& other); - //@} /// \name sub-matrices //@{ diff --git a/Eigen/src/Core/Util.h b/Eigen/src/Core/Util.h index 962a942d1..872df2cb5 100644 --- a/Eigen/src/Core/Util.h +++ b/Eigen/src/Core/Util.h @@ -151,8 +151,9 @@ struct ei_has_nothing {int a[1];}; struct ei_has_std_result_type {int a[2];}; struct ei_has_tr1_result {int a[3];}; -/** Convenient struct to get the result type of a unary or binary functor. - * +/** \internal + * Convenient struct to get the result type of a unary or binary functor. + * * It supports both the current STL mechanism (using the result_type member) as well as * upcoming next STL generation (using a templated result member). * If none of these member is provided, then the type of the first argument is returned. @@ -175,7 +176,7 @@ struct ei_result_of<Func(ArgType)> { template<typename T> static ei_has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType)>::type const * = 0); static ei_has_nothing testFunctor(...); - + typedef typename ei_unary_result_of_select<Func, ArgType, sizeof(testFunctor(static_cast<Func*>(0)))>::type type; }; @@ -197,7 +198,7 @@ struct ei_result_of<Func(ArgType0,ArgType1)> { template<typename T> static ei_has_tr1_result testFunctor(T const *, typename T::template result<T(ArgType0,ArgType1)>::type const * = 0); static ei_has_nothing testFunctor(...); - + typedef typename ei_binary_result_of_select<Func, ArgType0, ArgType1, sizeof(testFunctor(static_cast<Func*>(0)))>::type type; }; diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index 429215207..a8dc339c1 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -25,8 +25,8 @@ ABBREVIATE_BRIEF = "The $name class" \ ALWAYS_DETAILED_SEC = NO INLINE_INHERITED_MEMB = NO FULL_PATH_NAMES = NO -STRIP_FROM_PATH = -STRIP_FROM_INC_PATH = +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = SHORT_NAMES = NO JAVADOC_AUTOBRIEF = NO QT_AUTOBRIEF = NO @@ -69,11 +69,11 @@ GENERATE_TODOLIST = YES GENERATE_TESTLIST = YES GENERATE_BUGLIST = YES GENERATE_DEPRECATEDLIST= YES -ENABLED_SECTIONS = +ENABLED_SECTIONS = MAX_INITIALIZER_LINES = 30 SHOW_USED_FILES = YES SHOW_DIRECTORIES = NO -FILE_VERSION_FILTER = +FILE_VERSION_FILTER = #--------------------------------------------------------------------------- # configuration options related to warning and progress messages #--------------------------------------------------------------------------- @@ -83,7 +83,7 @@ WARN_IF_UNDOCUMENTED = NO WARN_IF_DOC_ERROR = YES WARN_NO_PARAMDOC = NO WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = +WARN_LOGFILE = #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- @@ -93,17 +93,17 @@ FILE_PATTERNS = * RECURSIVE = NO EXCLUDE = CMake* *.txt EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = -EXCLUDE_SYMBOLS = +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = EXAMPLE_PATH = ${CMAKE_SOURCE_DIR}/doc/snippets \ ${CMAKE_BINARY_DIR}/doc/snippets \ ${CMAKE_SOURCE_DIR}/doc/examples \ ${CMAKE_BINARY_DIR}/doc/examples EXAMPLE_PATTERNS = * EXAMPLE_RECURSIVE = NO -IMAGE_PATH = -INPUT_FILTER = -FILTER_PATTERNS = +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = FILTER_SOURCE_FILES = NO #--------------------------------------------------------------------------- # configuration options related to source browsing @@ -121,21 +121,21 @@ VERBATIM_HEADERS = YES #--------------------------------------------------------------------------- ALPHABETICAL_INDEX = NO COLS_IN_ALPHA_INDEX = 5 -IGNORE_PREFIX = +IGNORE_PREFIX = #--------------------------------------------------------------------------- # configuration options related to the HTML output #--------------------------------------------------------------------------- GENERATE_HTML = YES HTML_OUTPUT = html HTML_FILE_EXTENSION = .html -HTML_HEADER = -HTML_FOOTER = -HTML_STYLESHEET = +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = HTML_ALIGN_MEMBERS = YES GENERATE_HTMLHELP = NO HTML_DYNAMIC_SECTIONS = NO -CHM_FILE = -HHC_LOCATION = +CHM_FILE = +HHC_LOCATION = GENERATE_CHI = NO BINARY_TOC = NO TOC_EXPAND = NO @@ -153,7 +153,7 @@ MAKEINDEX_CMD_NAME = makeindex COMPACT_LATEX = NO PAPER_TYPE = a4wide EXTRA_PACKAGES = amssymb -LATEX_HEADER = +LATEX_HEADER = PDF_HYPERLINKS = NO USE_PDFLATEX = NO LATEX_BATCHMODE = NO @@ -165,8 +165,8 @@ GENERATE_RTF = NO RTF_OUTPUT = rtf COMPACT_RTF = NO RTF_HYPERLINKS = NO -RTF_STYLESHEET_FILE = -RTF_EXTENSIONS_FILE = +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = #--------------------------------------------------------------------------- # configuration options related to the man page output #--------------------------------------------------------------------------- @@ -179,8 +179,8 @@ MAN_LINKS = NO #--------------------------------------------------------------------------- GENERATE_XML = NO XML_OUTPUT = xml -XML_SCHEMA = -XML_DTD = +XML_SCHEMA = +XML_DTD = XML_PROGRAMLISTING = YES #--------------------------------------------------------------------------- # configuration options for the AutoGen Definitions output @@ -192,29 +192,29 @@ GENERATE_AUTOGEN_DEF = NO GENERATE_PERLMOD = NO PERLMOD_LATEX = NO PERLMOD_PRETTY = YES -PERLMOD_MAKEVAR_PREFIX = +PERLMOD_MAKEVAR_PREFIX = #--------------------------------------------------------------------------- -# Configuration options related to the preprocessor +# Configuration options related to the preprocessor #--------------------------------------------------------------------------- ENABLE_PREPROCESSING = YES MACRO_EXPANSION = YES EXPAND_ONLY_PREDEF = YES SEARCH_INCLUDES = YES -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = -PREDEFINED = +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = EIGEN_EMPTY_STRUCT EXPAND_AS_DEFINED = EIGEN_MAKE_SCALAR_OPS SKIP_FUNCTION_MACROS = YES #--------------------------------------------------------------------------- -# Configuration::additions related to external references +# Configuration::additions related to external references #--------------------------------------------------------------------------- -TAGFILES = -GENERATE_TAGFILE = +TAGFILES = +GENERATE_TAGFILE = ALLEXTERNALS = NO EXTERNAL_GROUPS = YES PERL_PATH = /usr/bin/perl #--------------------------------------------------------------------------- -# Configuration options related to the dot tool +# Configuration options related to the dot tool #--------------------------------------------------------------------------- CLASS_DIAGRAMS = NO MSCGEN_PATH = NO @@ -232,8 +232,8 @@ CALLER_GRAPH = NO GRAPHICAL_HIERARCHY = NO DIRECTORY_GRAPH = NO DOT_IMAGE_FORMAT = png -DOT_PATH = -DOTFILE_DIRS = +DOT_PATH = +DOTFILE_DIRS = DOT_GRAPH_MAX_NODES = 50 MAX_DOT_GRAPH_DEPTH = 1000 DOT_TRANSPARENT = NO @@ -241,6 +241,6 @@ DOT_MULTI_TARGETS = NO GENERATE_LEGEND = NO DOT_CLEANUP = NO #--------------------------------------------------------------------------- -# Configuration::additions related to the search engine +# Configuration::additions related to the search engine #--------------------------------------------------------------------------- SEARCHENGINE = NO diff --git a/doc/examples/class_CwiseUnaryOp.cpp b/doc/examples/class_CwiseUnaryOp.cpp index e10037352..4d4bfe734 100644 --- a/doc/examples/class_CwiseUnaryOp.cpp +++ b/doc/examples/class_CwiseUnaryOp.cpp @@ -6,12 +6,13 @@ using namespace std; template<typename Scalar> struct CwiseClampOp EIGEN_EMPTY_STRUCT { CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {} - Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup : m_sup : x); } + Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); } + Scalar m_inf, m_sup; }; int main(int, char**) { - Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random(); - cout << m1.cwise(m2, CwiseClampOp<Matrix4d::Scalar>(-0.5,0.5)) << endl; + Matrix4d m1 = Matrix4d::random(); + cout << m1.cwise(CwiseClampOp<Matrix4d::Scalar>(-0.5,0.5)) << endl; return 0; } diff --git a/doc/snippets/MatrixBase_set.cpp b/doc/snippets/MatrixBase_set.cpp new file mode 100644 index 000000000..376efbce5 --- /dev/null +++ b/doc/snippets/MatrixBase_set.cpp @@ -0,0 +1,8 @@ +Matrix3i m1; +m1 <<= 1, 2, 3, + 4, 5, 6, + 7, 8, 9; +cout << m1 << endl << endl; +Matrix3i m2 = Matrix3i::identity(); +m2.block(0,0, 2,2) <<= 10, 11, 12, 13; +cout << m2 << endl;
\ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 504b30611..22bc2d23c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,12 @@ IF(BUILD_TESTS) +OPTION(EIGEN_NO_ASSERTION_CHECKING "Disable checking of assertions" OFF) + ENABLE_TESTING() FIND_PACKAGE(Qt4 REQUIRED) INCLUDE_DIRECTORIES( ${QT_INCLUDE_DIR} ) SET(test_SRCS - cwiseop.cpp main.cpp basicstuff.cpp linearstructure.cpp @@ -15,11 +16,25 @@ SET(test_SRCS miscmatrices.cpp smallvectors.cpp map.cpp + cwiseop.cpp ) QT4_AUTOMOC(${test_SRCS}) ADD_EXECUTABLE(test ${test_SRCS}) TARGET_LINK_LIBRARIES(test ${QT_QTCORE_LIBRARY} ${QT_QTTEST_LIBRARY}) +IF(NOT EIGEN_NO_ASSERTION_CHECKING) + + SET_TARGET_PROPERTIES(test PROPERTIES COMPILE_FLAGS "-fexceptions") + OPTION(EIGEN_DEBUG_ASSERTS "Enable debuging of assertions" OFF) + IF(EIGEN_DEBUG_ASSERTS) + ADD_DEFINITIONS(-DEIGEN_DEBUG_ASSERTS=1) + ENDIF(EIGEN_DEBUG_ASSERTS) + +ELSE(NOT EIGEN_NO_ASSERTION_CHECKING) + + ADD_DEFINITIONS(-DEIGEN_NO_ASSERTION_CHECKING=1) + +ENDIF(NOT EIGEN_NO_ASSERTION_CHECKING) ADD_TEST(Eigen test) diff --git a/test/basicstuff.cpp b/test/basicstuff.cpp index a8fb8db33..56f41ed4a 100644 --- a/test/basicstuff.cpp +++ b/test/basicstuff.cpp @@ -79,6 +79,11 @@ template<typename MatrixType> void basicStuff(const MatrixType& m) rv = square.col(r); cv = square.row(r); VERIFY_IS_APPROX(rv, cv.transpose()); + + if(cols!=1 && rows!=1 && MatrixType::Traits::SizeAtCompileTime!=Dynamic) + { + VERIFY_RAISES_ASSERT(m1 = (m2.block(0,0, rows-1, cols-1))); + } } void EigenTest::testBasicStuff() @@ -91,6 +96,19 @@ void EigenTest::testBasicStuff() basicStuff(MatrixXcd(20, 20)); basicStuff(Matrix<float, 100, 100>()); } + + // some additional basic tests + { + Matrix3d m3; + Matrix4d m4; + VERIFY_RAISES_ASSERT(m4 = m3); + + VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8) ); + VERIFY_RAISES_ASSERT( (m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) ); + m3 << 1, 2, 3, 4, 5, 6, 7, 8, 9; + double data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + VERIFY_IS_APPROX(m3, (Matrix<double,3,3,RowMajor>::map(data)) ); + } } } // namespace Eigen diff --git a/test/main.h b/test/main.h index 54f6039cf..894f1164b 100644 --- a/test/main.h +++ b/test/main.h @@ -5,12 +5,12 @@ // // Eigen 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 +// License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as -// published by the Free Software Foundation; either version 2 of +// published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY @@ -18,7 +18,7 @@ // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // -// You should have received a copy of the GNU Lesser General Public +// You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see <http://www.gnu.org/licenses/>. @@ -27,15 +27,80 @@ #include <QtTest/QtTest> -//#define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER RowMajor -#define EIGEN_INTERNAL_DEBUGGING -#include <Eigen/Core> - #include <cstdlib> #include <ctime> #define DEFAULT_REPEAT 50 +#ifndef EIGEN_NO_ASSERTION_CHECKING + + namespace Eigen + { + struct ei_assert_exception + {}; + static const bool should_raise_an_assert = false; + } + + #define EI_PP_MAKE_STRING2(S) #S + #define EI_PP_MAKE_STRING(S) EI_PP_MAKE_STRING2(S) + + #ifdef assert + #undef assert + #endif + + // If EIGEN_DEBUG_ASSERTS is defined and if no assertion is raised while + // one should have been, then the list of excecuted assertions is printed out. + // + // EIGEN_DEBUG_ASSERTS is not enabled by default as it + // significantly increases the compilation time + // and might even introduce side effects that would hide + // some memory errors. + #ifdef EIGEN_DEBUG_ASSERTS + + namespace Eigen + { + static bool ei_push_assert = false; + static std::vector<std::string> ei_assert_list; + } + + #define assert(a) if (!(a)) { throw Eigen::ei_assert_exception();} \ + else if (Eigen::ei_push_assert) {ei_assert_list.push_back( std::string(EI_PP_MAKE_STRING(__FILE__)) + " (" + EI_PP_MAKE_STRING(__LINE__) + ") : " + #a );} + + #define VERIFY_RAISES_ASSERT(a) {\ + try { \ + Eigen::ei_assert_list.clear(); \ + Eigen::ei_push_assert = true; \ + a; \ + Eigen::ei_push_assert = false; \ + std::cout << "One of the following asserts should have been raised:\n"; \ + for (uint ai=0 ; ai<ei_assert_list.size() ; ++ai) \ + std::cout << " " << ei_assert_list[ai] << "\n"; \ + QVERIFY(Eigen::should_raise_an_assert && # a); \ + } catch (Eigen::ei_assert_exception e) { Eigen::ei_push_assert = false; QVERIFY(true);} } + + #else // EIGEN_DEBUG_ASSERTS + + #define assert(a) if (!(a)) { throw Eigen::ei_assert_exception();} + + #define VERIFY_RAISES_ASSERT(a) {\ + try {a; QVERIFY(Eigen::should_raise_an_assert && # a); } \ + catch (Eigen::ei_assert_exception e) { QVERIFY(true);} } + + #endif // EIGEN_DEBUG_ASSERTS + + #define EIGEN_CUSTOM_ASSERT + +#else // EIGEN_NO_ASSERTION_CHECKING + + #define VERIFY_RAISES_ASSERT(a) {} + +#endif // EIGEN_NO_ASSERTION_CHECKING + + +//#define EIGEN_DEFAULT_MATRIX_STORAGE_ORDER RowMajor +#define EIGEN_INTERNAL_DEBUGGING +#include <Eigen/Core> + #define VERIFY(a) QVERIFY(a) #define VERIFY_IS_APPROX(a, b) QVERIFY(test_ei_isApprox(a, b)) #define VERIFY_IS_NOT_APPROX(a, b) QVERIFY(!test_ei_isApprox(a, b)) diff --git a/test/product.cpp b/test/product.cpp index 4d5b5161a..8d802b576 100644 --- a/test/product.cpp +++ b/test/product.cpp @@ -83,6 +83,9 @@ template<typename MatrixType> void product(const MatrixType& m) VERIFY_IS_APPROX(v1, identity*v1); // again, test operator() to check const-qualification VERIFY_IS_APPROX(MatrixType::identity(rows, cols)(r,c), static_cast<Scalar>(r==c)); + + if (rows!=cols) + VERIFY_RAISES_ASSERT(m3 = m1*m1); } void EigenTest::testProduct() |