mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
add zero() and identity() static methods, update unit-tests
This commit is contained in:
@@ -27,6 +27,8 @@ namespace Eigen {
|
||||
#include "Core/Trace.h"
|
||||
#include "Core/Dot.h"
|
||||
#include "Core/Random.h"
|
||||
#include "Core/Zero.h"
|
||||
#include "Core/Identity.h"
|
||||
#include "Core/Fuzzy.h"
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
@@ -33,6 +33,7 @@ bool Object<Scalar, Derived>::isApprox(
|
||||
const typename NumTraits<Scalar>::Real& prec
|
||||
) const
|
||||
{
|
||||
assert(rows() == other.rows() && cols() == other.cols());
|
||||
if(IsVector)
|
||||
{
|
||||
return((*this - other).norm2() <= std::min(norm2(), other.norm2()) * prec * prec);
|
||||
@@ -73,6 +74,7 @@ bool Object<Scalar, Derived>::isMuchSmallerThan(
|
||||
const typename NumTraits<Scalar>::Real& prec
|
||||
) const
|
||||
{
|
||||
assert(rows() == other.rows() && cols() == other.cols());
|
||||
if(IsVector)
|
||||
{
|
||||
return(norm2() <= other.norm2() * prec * prec);
|
||||
|
||||
65
src/Core/Identity.h
Normal file
65
src/Core/Identity.h
Normal file
@@ -0,0 +1,65 @@
|
||||
// This file is part of Eigen, a lightweight C++ template library
|
||||
// for linear algebra. Eigen itself is part of the KDE project.
|
||||
//
|
||||
// Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr>
|
||||
//
|
||||
// Eigen is free software; 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 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 General Public License for more
|
||||
// details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with Eigen; if not, write to the Free Software Foundation, Inc., 51
|
||||
// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
//
|
||||
// As a special exception, if other files instantiate templates or use macros
|
||||
// or functions from this file, or you compile this file and link it
|
||||
// with other works to produce a work based on this file, this file does not
|
||||
// by itself cause the resulting work to be covered by the GNU General Public
|
||||
// License. This exception does not invalidate any other reasons why a work
|
||||
// based on this file might be covered by the GNU General Public License.
|
||||
|
||||
#ifndef EI_IDENTITY_H
|
||||
#define EI_IDENTITY_H
|
||||
|
||||
template<typename MatrixType> class Identity
|
||||
: public Object<typename MatrixType::Scalar, Identity<MatrixType> >
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
friend class Object<Scalar, Identity<MatrixType> >;
|
||||
|
||||
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime;
|
||||
|
||||
Identity(int rows, int cols) : m_rows(rows), m_cols(cols)
|
||||
{
|
||||
assert(rows > 0 && cols > 0);
|
||||
}
|
||||
|
||||
private:
|
||||
Identity& _ref() { return *this; }
|
||||
const Identity& _constRef() const { return *this; }
|
||||
int _rows() const { return m_rows; }
|
||||
int _cols() const { return m_cols; }
|
||||
|
||||
Scalar _read(int row, int col) const
|
||||
{
|
||||
return static_cast<Scalar>(row == col);
|
||||
}
|
||||
|
||||
protected:
|
||||
int m_rows, m_cols;
|
||||
};
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
Identity<Derived> Object<Scalar, Derived>::identity(int rows, int cols)
|
||||
{
|
||||
return Identity<Derived>(rows, cols);
|
||||
}
|
||||
|
||||
#endif // EI_IDENTITY_H
|
||||
@@ -113,6 +113,10 @@ template<typename Scalar, typename Derived> class Object
|
||||
|
||||
static Eval<Random<Derived> >
|
||||
random(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime);
|
||||
static Zero<Derived>
|
||||
zero(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime);
|
||||
static Identity<Derived>
|
||||
identity(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime);
|
||||
|
||||
template<typename OtherDerived>
|
||||
bool isApprox(
|
||||
|
||||
@@ -56,6 +56,8 @@ template<typename Lhs, typename Rhs> class Difference;
|
||||
template<typename Lhs, typename Rhs> class Product;
|
||||
template<typename MatrixType> class ScalarMultiple;
|
||||
template<typename MatrixType> class Random;
|
||||
template<typename MatrixType> class Zero;
|
||||
template<typename MatrixType> class Identity;
|
||||
template<typename ExpressionType> class Eval;
|
||||
|
||||
template<typename T> struct ForwardDecl
|
||||
|
||||
67
src/Core/Zero.h
Normal file
67
src/Core/Zero.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) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr>
|
||||
//
|
||||
// Eigen is free software; 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 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 General Public License for more
|
||||
// details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with Eigen; if not, write to the Free Software Foundation, Inc., 51
|
||||
// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
//
|
||||
// As a special exception, if other files instantiate templates or use macros
|
||||
// or functions from this file, or you compile this file and link it
|
||||
// with other works to produce a work based on this file, this file does not
|
||||
// by itself cause the resulting work to be covered by the GNU General Public
|
||||
// License. This exception does not invalidate any other reasons why a work
|
||||
// based on this file might be covered by the GNU General Public License.
|
||||
|
||||
#ifndef EI_ZERO_H
|
||||
#define EI_ZERO_H
|
||||
|
||||
template<typename MatrixType> class Zero
|
||||
: public Object<typename MatrixType::Scalar, Zero<MatrixType> >
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
friend class Object<Scalar, Zero<MatrixType> >;
|
||||
|
||||
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime;
|
||||
|
||||
Zero(int rows, int cols) : m_rows(rows), m_cols(cols)
|
||||
{
|
||||
assert(rows > 0 && cols > 0);
|
||||
}
|
||||
|
||||
private:
|
||||
Zero& _ref() { return *this; }
|
||||
const Zero& _constRef() const { return *this; }
|
||||
int _rows() const { return m_rows; }
|
||||
int _cols() const { return m_cols; }
|
||||
|
||||
Scalar _read(int row, int col) const
|
||||
{
|
||||
EI_UNUSED(row);
|
||||
EI_UNUSED(col);
|
||||
return static_cast<Scalar>(0);
|
||||
}
|
||||
|
||||
protected:
|
||||
int m_rows, m_cols;
|
||||
};
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
Zero<Derived> Object<Scalar, Derived>::zero(int rows, int cols)
|
||||
{
|
||||
return Zero<Derived>(rows, cols);
|
||||
}
|
||||
|
||||
#endif // EI_ZERO_H
|
||||
Reference in New Issue
Block a user