-add set...() methods and their documentation; remove Generic

-use row-major traversal when the number of columns is fixed
 and the number of rows is dynamic
-other minor changes
This commit is contained in:
Benoit Jacob 2008-01-11 15:08:04 +00:00
parent aae0667e1e
commit e092cbc75c
20 changed files with 112 additions and 26 deletions

View File

@ -71,7 +71,7 @@ template<typename NewScalar, typename MatrixType> class Cast : NoOperatorEquals,
} }
protected: protected:
MatRef m_matrix; const MatRef m_matrix;
}; };
/** \returns an expression of *this with the \a Scalar type casted to /** \returns an expression of *this with the \a Scalar type casted to

View File

@ -64,7 +64,7 @@ template<typename MatrixType> class Conjugate : NoOperatorEquals,
} }
protected: protected:
MatRef m_matrix; const MatRef m_matrix;
}; };
/** \returns an expression of the complex conjugate of *this. /** \returns an expression of the complex conjugate of *this.

View File

@ -52,7 +52,7 @@ class DiagonalMatrix : NoOperatorEquals,
DiagonalMatrix(const CoeffsVecRef& coeffs) : m_coeffs(coeffs) DiagonalMatrix(const CoeffsVecRef& coeffs) : m_coeffs(coeffs)
{ {
assert(CoeffsVectorType::Traits::IsVectorAtCompileTime assert(CoeffsVectorType::Traits::IsVectorAtCompileTime
&& coeffs.coeffs() > 0); && coeffs.size() > 0);
} }
private: private:
@ -62,8 +62,8 @@ class DiagonalMatrix : NoOperatorEquals,
}; };
const DiagonalMatrix& _ref() const { return *this; } const DiagonalMatrix& _ref() const { return *this; }
int _rows() const { return m_coeffs.coeffs(); } int _rows() const { return m_coeffs.size(); }
int _cols() const { return m_coeffs.coeffs(); } int _cols() const { return m_coeffs.size(); }
Scalar _coeff(int row, int col) const Scalar _coeff(int row, int col) const
{ {
@ -71,7 +71,7 @@ class DiagonalMatrix : NoOperatorEquals,
} }
protected: protected:
CoeffsVecRef m_coeffs; const CoeffsVecRef m_coeffs;
}; };
/** \returns an expression of a diagonal matrix with *this as vector of diagonal coefficients /** \returns an expression of a diagonal matrix with *this as vector of diagonal coefficients

View File

@ -74,7 +74,7 @@ Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
{ {
assert(Traits::IsVectorAtCompileTime assert(Traits::IsVectorAtCompileTime
&& OtherDerived::Traits::IsVectorAtCompileTime && OtherDerived::Traits::IsVectorAtCompileTime
&& coeffs() == other.coeffs()); && size() == other.size());
Scalar res; Scalar res;
if(EIGEN_UNROLLED_LOOPS if(EIGEN_UNROLLED_LOOPS
&& Traits::SizeAtCompileTime != Dynamic && Traits::SizeAtCompileTime != Dynamic
@ -85,7 +85,7 @@ Scalar MatrixBase<Scalar, Derived>::dot(const OtherDerived& other) const
else else
{ {
res = (*this).coeff(0) * conj(other.coeff(0)); res = (*this).coeff(0) * conj(other.coeff(0));
for(int i = 1; i < coeffs(); i++) for(int i = 1; i < size(); i++)
res += (*this).coeff(i)* conj(other.coeff(i)); res += (*this).coeff(i)* conj(other.coeff(i));
} }
return res; return res;

View File

@ -60,7 +60,7 @@ template<typename MatrixType> class Identity : NoOperatorEquals,
} }
protected: protected:
int m_rows; const int m_rows;
}; };
/** \returns an expression of the identity matrix of given type and size. /** \returns an expression of the identity matrix of given type and size.
@ -108,5 +108,18 @@ bool MatrixBase<Scalar, Derived>::isIdentity
return true; return true;
} }
/** Writes the identity expression into *this.
*
* Example: \include MatrixBase_setIdentity.cpp
* Output: \verbinclude MatrixBase_setIdentity.out
*
* \sa class Identity, identity()
*/
template<typename Scalar, typename Derived>
Derived& MatrixBase<Scalar, Derived>::setIdentity()
{
return *this = Identity<Derived>(rows());
}
#endif // EIGEN_IDENTITY_H #endif // EIGEN_IDENTITY_H

View File

@ -86,7 +86,7 @@ template<typename MatrixType> class Map
protected: protected:
const Scalar* m_data; const Scalar* m_data;
int m_rows, m_cols; const int m_rows, m_cols;
}; };
/** This is the const version of map(Scalar*,int,int). */ /** This is the const version of map(Scalar*,int,int). */

View File

@ -70,7 +70,7 @@
* \c Matrix<double,3,5>. * \c Matrix<double,3,5>.
* *
* Note that most of the API is in the base class MatrixBase, and that the base class * Note that most of the API is in the base class MatrixBase, and that the base class
* MatrixStorage also provides the MatrixStorage::recoeffs() public method. * MatrixStorage also provides the MatrixStorage::resize() public method.
*/ */
template<typename _Scalar, int _Rows, int _Cols, template<typename _Scalar, int _Rows, int _Cols,
int _StorageOrder = EIGEN_DEFAULT_MATRIX_STORAGE_ORDER> int _StorageOrder = EIGEN_DEFAULT_MATRIX_STORAGE_ORDER>
@ -135,12 +135,12 @@ class Matrix : public MatrixBase<_Scalar, Matrix<_Scalar, _Rows, _Cols, _Storage
if(RowsAtCompileTime == 1) if(RowsAtCompileTime == 1)
{ {
assert(other.isVector()); assert(other.isVector());
resize(1, other.coeffs()); resize(1, other.size());
} }
else if(ColsAtCompileTime == 1) else if(ColsAtCompileTime == 1)
{ {
assert(other.isVector()); assert(other.isVector());
resize(other.coeffs(), 1); resize(other.size(), 1);
} }
else resize(other.rows(), other.cols()); else resize(other.rows(), other.cols());
return Base::operator=(other); return Base::operator=(other);

View File

@ -115,7 +115,7 @@ template<typename Scalar, typename Derived> class MatrixBase
int cols() const { return static_cast<const Derived *>(this)->_cols(); } int cols() const { return static_cast<const Derived *>(this)->_cols(); }
/** \returns the number of coefficients, which is \a rows()*cols(). /** \returns the number of coefficients, which is \a rows()*cols().
* \sa rows(), cols(), Traits::SizeAtCompileTime. */ * \sa rows(), cols(), Traits::SizeAtCompileTime. */
int coeffs() const { return rows() * cols(); } int size() const { return rows() * cols(); }
/** \returns true if either the number of rows or the number of columns is equal to 1. /** \returns true if either the number of rows or the number of columns is equal to 1.
* In other words, this function returns * In other words, this function returns
* \code rows()==1 || cols()==1 \endcode * \code rows()==1 || cols()==1 \endcode
@ -189,6 +189,11 @@ template<typename Scalar, typename Derived> class MatrixBase
bool isIdentity(RealScalar prec = precision<Scalar>()) const; bool isIdentity(RealScalar prec = precision<Scalar>()) const;
bool isDiagonal(RealScalar prec = precision<Scalar>()) const; bool isDiagonal(RealScalar prec = precision<Scalar>()) const;
Derived& setZero();
Derived& setOnes();
Derived& setRandom();
Derived& setIdentity();
const DiagonalMatrix<Derived> asDiagonal() const; const DiagonalMatrix<Derived> asDiagonal() const;
DiagonalCoeffs<Derived> diagonal(); DiagonalCoeffs<Derived> diagonal();

View File

@ -64,7 +64,7 @@ template<typename MatrixType> class Ones : NoOperatorEquals,
} }
protected: protected:
int m_rows, m_cols; const int m_rows, m_cols;
}; };
/** \returns an expression of a matrix where all coefficients equal one. /** \returns an expression of a matrix where all coefficients equal one.
@ -146,4 +146,17 @@ bool MatrixBase<Scalar, Derived>::isOnes
return true; return true;
} }
/** Sets all coefficients in this expression to one.
*
* Example: \include MatrixBase_setOnes.cpp
* Output: \verbinclude MatrixBase_setOnes.out
*
* \sa class Ones, ones()
*/
template<typename Scalar, typename Derived>
Derived& MatrixBase<Scalar, Derived>::setOnes()
{
return *this = Ones<Derived>(rows(), cols());
}
#endif // EIGEN_ONES_H #endif // EIGEN_ONES_H

View File

@ -106,27 +106,45 @@ Derived& MatrixBase<Scalar, Derived>
if(Traits::IsVectorAtCompileTime && OtherDerived::Traits::IsVectorAtCompileTime) if(Traits::IsVectorAtCompileTime && OtherDerived::Traits::IsVectorAtCompileTime)
// copying a vector expression into a vector // copying a vector expression into a vector
{ {
assert(coeffs() == other.coeffs()); assert(size() == other.size());
if(EIGEN_UNROLLED_LOOPS && Traits::SizeAtCompileTime != Dynamic && Traits::SizeAtCompileTime <= 25) if(EIGEN_UNROLLED_LOOPS && Traits::SizeAtCompileTime != Dynamic && Traits::SizeAtCompileTime <= 25)
VectorOperatorEqualsUnroller VectorOperatorEqualsUnroller
<Derived, OtherDerived, Traits::SizeAtCompileTime>::run <Derived, OtherDerived, Traits::SizeAtCompileTime>::run
(*static_cast<Derived*>(this), *static_cast<const OtherDerived*>(&other)); (*static_cast<Derived*>(this), *static_cast<const OtherDerived*>(&other));
else else
for(int i = 0; i < coeffs(); i++) for(int i = 0; i < size(); i++)
coeffRef(i) = other.coeff(i); coeffRef(i) = other.coeff(i);
return *static_cast<Derived*>(this); return *static_cast<Derived*>(this);
} }
else // copying a matrix expression into a matrix else // copying a matrix expression into a matrix
{ {
assert(rows() == other.rows() && cols() == other.cols()); assert(rows() == other.rows() && cols() == other.cols());
if(EIGEN_UNROLLED_LOOPS && Traits::SizeAtCompileTime != Dynamic && Traits::SizeAtCompileTime <= 25) if(EIGEN_UNROLLED_LOOPS
&& Traits::SizeAtCompileTime != Dynamic
&& Traits::SizeAtCompileTime <= 25)
{
MatrixOperatorEqualsUnroller MatrixOperatorEqualsUnroller
<Derived, OtherDerived, Traits::SizeAtCompileTime>::run <Derived, OtherDerived, Traits::SizeAtCompileTime>::run
(*static_cast<Derived*>(this), *static_cast<const OtherDerived*>(&other)); (*static_cast<Derived*>(this), *static_cast<const OtherDerived*>(&other));
}
else else
for(int j = 0; j < cols(); j++) {
if(Traits::ColsAtCompileTime == Dynamic || Traits::RowsAtCompileTime != Dynamic)
{
// traverse in column-major order
for(int j = 0; j < cols(); j++)
for(int i = 0; i < rows(); i++)
coeffRef(i, j) = other.coeff(i, j);
}
else
{
// traverse in row-major order
// in order to allow the compiler to unroll the inner loop
for(int i = 0; i < rows(); i++) for(int i = 0; i < rows(); i++)
coeffRef(i, j) = other.coeff(i, j); for(int j = 0; j < cols(); j++)
coeffRef(i, j) = other.coeff(i, j);
}
}
return *static_cast<Derived*>(this); return *static_cast<Derived*>(this);
} }
} }

View File

@ -64,7 +64,7 @@ template<typename MatrixType> class Opposite : NoOperatorEquals,
} }
protected: protected:
MatRef m_matrix; const MatRef m_matrix;
}; };
/** \returns an expression of the opposite of \c *this /** \returns an expression of the opposite of \c *this

View File

@ -64,7 +64,7 @@ template<typename MatrixType> class Random : NoOperatorEquals,
} }
protected: protected:
int m_rows, m_cols; const int m_rows, m_cols;
}; };
/** \returns a random matrix (not an expression, the matrix is immediately evaluated). /** \returns a random matrix (not an expression, the matrix is immediately evaluated).
@ -128,4 +128,17 @@ const Eval<Random<Derived> > MatrixBase<Scalar, Derived>::random()
return Random<Derived>(Traits::RowsAtCompileTime, Traits::ColsAtCompileTime).eval(); return Random<Derived>(Traits::RowsAtCompileTime, Traits::ColsAtCompileTime).eval();
} }
/** Sets all coefficients in this expression to random values.
*
* Example: \include MatrixBase_setRandom.cpp
* Output: \verbinclude MatrixBase_setRandom.out
*
* \sa class Random, random()
*/
template<typename Scalar, typename Derived>
Derived& MatrixBase<Scalar, Derived>::setRandom()
{
return *this = Random<Derived>(rows(), cols());
}
#endif // EIGEN_RANDOM_H #endif // EIGEN_RANDOM_H

View File

@ -89,7 +89,6 @@ EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, *=) \
EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=) EIGEN_INHERIT_SCALAR_ASSIGNMENT_OPERATOR(Derived, /=)
const int Dynamic = -10; const int Dynamic = -10;
const int Generic = -20;
const int ColumnMajor = 0; const int ColumnMajor = 0;
const int RowMajor = 1; const int RowMajor = 1;

View File

@ -64,7 +64,7 @@ template<typename MatrixType> class Zero : NoOperatorEquals,
} }
protected: protected:
int m_rows, m_cols; const int m_rows, m_cols;
}; };
/** \returns an expression of a zero matrix. /** \returns an expression of a zero matrix.
@ -146,4 +146,17 @@ bool MatrixBase<Scalar, Derived>::isZero
return true; return true;
} }
/** Sets all coefficients in this expression to zero.
*
* Example: \include MatrixBase_setZero.cpp
* Output: \verbinclude MatrixBase_setZero.out
*
* \sa class Zero, zero()
*/
template<typename Scalar, typename Derived>
Derived& MatrixBase<Scalar, Derived>::setZero()
{
return *this = Zero<Derived>(rows(), cols());
}
#endif // EIGEN_ZERO_H #endif // EIGEN_ZERO_H

View File

@ -16,7 +16,7 @@ int main(int argc, char *argv[])
} }
for(int a = 0; a < 400000000; a++) for(int a = 0; a < 400000000; a++)
{ {
m = I + 0.00005 * (m + m*m); m = Matrix3d::identity() + 0.00005 * (m + m*m);
} }
cout << m << endl; cout << m << endl;
return 0; return 0;

View File

@ -0,0 +1,3 @@
Matrix4i m = Matrix4i::zero();
m.block<3,3>(1,0).setIdentity();
cout << m << endl;

View File

@ -0,0 +1,3 @@
Matrix4i m = Matrix4i::random();
m.row(1).setOnes();
cout << m << endl;

View File

@ -0,0 +1,3 @@
Matrix4i m = Matrix4i::zero();
m.col(1).setRandom();
cout << m << endl;

View File

@ -0,0 +1,3 @@
Matrix4i m = Matrix4i::random();
m.row(1).setZero();
cout << m << endl;

View File

@ -31,7 +31,7 @@ template<typename VectorType> void tmap(const VectorType& m)
{ {
typedef typename VectorType::Scalar Scalar; typedef typename VectorType::Scalar Scalar;
int size = m.coeffs(); int size = m.size();
// test Map.h // test Map.h
Scalar* array1 = new Scalar[size]; Scalar* array1 = new Scalar[size];