restrict identity() to square matrices; small change helping g++ optimize.

This commit is contained in:
Benoit Jacob 2007-10-14 10:01:25 +00:00
parent 6c8f159635
commit 3f97918760
3 changed files with 10 additions and 9 deletions

View File

@ -36,30 +36,31 @@ template<typename MatrixType> class Identity
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime, static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ColsAtCompileTime = MatrixType::ColsAtCompileTime; ColsAtCompileTime = MatrixType::ColsAtCompileTime;
Identity(int rows, int cols) : m_rows(rows), m_cols(cols) Identity(int rows) : m_rows(rows)
{ {
assert(rows > 0 && cols > 0); assert(rows > 0);
assert(RowsAtCompileTime == ColsAtCompileTime);
} }
private: private:
Identity& _ref() { return *this; } Identity& _ref() { return *this; }
const Identity& _constRef() const { return *this; } const Identity& _constRef() const { return *this; }
int _rows() const { return m_rows; } int _rows() const { return m_rows; }
int _cols() const { return m_cols; } int _cols() const { return m_rows; }
Scalar _read(int row, int col) const Scalar _read(int row, int col) const
{ {
return static_cast<Scalar>(row == col); return row == col ? static_cast<Scalar>(1) : static_cast<Scalar>(0);
} }
protected: protected:
int m_rows, m_cols; int m_rows;
}; };
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
Identity<Derived> Object<Scalar, Derived>::identity(int rows, int cols) Identity<Derived> Object<Scalar, Derived>::identity(int rows)
{ {
return Identity<Derived>(rows, cols); return Identity<Derived>(rows);
} }
#endif // EI_IDENTITY_H #endif // EI_IDENTITY_H

View File

@ -116,7 +116,7 @@ template<typename Scalar, typename Derived> class Object
static Zero<Derived> static Zero<Derived>
zero(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime); zero(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime);
static Identity<Derived> static Identity<Derived>
identity(int rows = RowsAtCompileTime, int cols = ColsAtCompileTime); identity(int rows = RowsAtCompileTime);
template<typename OtherDerived> template<typename OtherDerived>
bool isApprox( bool isApprox(

View File

@ -37,7 +37,7 @@ template<typename MatrixType> void basicStuff(const MatrixType& m)
m3, m3,
mzero = MatrixType::zero(rows, cols), mzero = MatrixType::zero(rows, cols),
identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
::identity(rows, rows), ::identity(rows),
square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
::random(rows, rows); ::random(rows, rows);
VectorType v1 = VectorType::random(rows), VectorType v1 = VectorType::random(rows),