mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
* 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.
This commit is contained in:
67
Eigen/src/Core/CommaInitializer.h
Normal file
67
Eigen/src/Core/CommaInitializer.h
Normal file
@@ -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
|
||||
@@ -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>
|
||||
|
||||
@@ -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/
|
||||
*/
|
||||
|
||||
@@ -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
|
||||
//@{
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user