Split Row and Column into separate files.

Introduce a notion of RowVector (typedef for Matriw with 1 row)
Make row() return a row vector instead of a "column vector"
Introduce operator[] to access elements of row/column vectors uniformly
Remove default arguments in operator(), these were for vectors, now use operator[] instead
This commit is contained in:
Benoit Jacob 2007-10-01 07:45:30 +00:00
parent 3cf6caba1a
commit 96524fc573
6 changed files with 45 additions and 84 deletions

View File

@ -26,8 +26,10 @@ int main(int, char **)
cout << "Now the 4x4 matrix m2 is:" << endl << m2 << endl; cout << "Now the 4x4 matrix m2 is:" << endl << m2 << endl;
cout << "The central 2x2 block of m2 is:" << endl << m2.block(1,2,1,2) << endl; cout << "The central 2x2 block of m2 is:" << endl << m2.block(1,2,1,2) << endl;
cout << "Row 0 of m2, written as a column vector, is:" << endl << m2.row(0) << endl; cout << "Row 0 of m2 is:" << endl << m2.row(0) << endl;
cout << "The third element in that row is " << m2.row(0)[2] << endl;
cout << "Column 1 of m2 is:" << endl << m2.col(1) << endl; cout << "Column 1 of m2 is:" << endl << m2.col(1) << endl;
cout << "The matrix m2 with row 0 and column 1 removed is:" << endl << m2.minor(0,1) << endl; cout << "The matrix m2 with row 0 and column 1 removed is:" << endl << m2.minor(0,1) << endl;
return 0; return 0;
} }

View File

@ -26,7 +26,8 @@
#ifndef EI_MANIP_H #ifndef EI_MANIP_H
#define EI_MANIP_H #define EI_MANIP_H
#include "internal/RowAndCol.h" #include "internal/Row.h"
#include "internal/Column.h"
#include "internal/Block.h" #include "internal/Block.h"
#include "internal/Minor.h" #include "internal/Minor.h"

View File

@ -33,7 +33,7 @@
template<typename _Scalar, int _Rows, int _Cols> template<typename _Scalar, int _Rows, int _Cols>
class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >, class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >,
public EiMatrixStorage<_Scalar, _Rows, _Cols> public EiMatrixStorage<_Scalar, _Rows, _Cols>
{ {
public: public:
friend class EiObject<_Scalar, EiMatrix>; friend class EiObject<_Scalar, EiMatrix>;
@ -57,13 +57,13 @@ class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >,
Ref _ref() { return Ref(*this); } Ref _ref() { return Ref(*this); }
ConstRef _constRef() const { return ConstRef(*this); } ConstRef _constRef() const { return ConstRef(*this); }
const Scalar& _read(int row, int col = 0) const const Scalar& _read(int row, int col) const
{ {
EI_CHECK_RANGES(*this, row, col); EI_CHECK_RANGES(*this, row, col);
return array()[row + col * Storage::_rows()]; return array()[row + col * Storage::_rows()];
} }
Scalar& _write(int row, int col = 0) Scalar& _write(int row, int col)
{ {
EI_CHECK_RANGES(*this, row, col); EI_CHECK_RANGES(*this, row, col);
return array()[row + col * Storage::_rows()]; return array()[row + col * Storage::_rows()];
@ -103,7 +103,8 @@ class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >,
#define EI_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \ #define EI_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
typedef EiMatrix<Type, Size, Size> EiMatrix##SizeSuffix##TypeSuffix; \ typedef EiMatrix<Type, Size, Size> EiMatrix##SizeSuffix##TypeSuffix; \
typedef EiMatrix<Type, Size, 1> EiVector##SizeSuffix##TypeSuffix; typedef EiMatrix<Type, Size, 1> EiVector##SizeSuffix##TypeSuffix; \
typedef EiMatrix<Type, 1, Size> EiRowVector##SizeSuffix##TypeSuffix;
#define EI_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ #define EI_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
EI_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \ EI_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
@ -124,6 +125,5 @@ EI_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
#include "Eval.h" #include "Eval.h"
#include "MatrixOps.h" #include "MatrixOps.h"
#include "ScalarOps.h" #include "ScalarOps.h"
#include "RowAndCol.h"
#endif // EI_MATRIX_H #endif // EI_MATRIX_H

View File

@ -24,8 +24,8 @@
// License. This exception does not invalidate any other reasons why a work // 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. // based on this file might be covered by the GNU General Public License.
#ifndef EI_EIGENBASE_H #ifndef EI_OBJECT_H
#define EI_EIGENBASE_H #define EI_OBJECT_H
#include "Util.h" #include "Util.h"
@ -118,7 +118,7 @@ template<typename Scalar, typename Derived> class EiObject
EiRow<Derived> row(int i); EiRow<Derived> row(int i);
EiColumn<Derived> col(int i); EiColumn<Derived> col(int i);
EiMinor<Derived> minor(int row, int col); EiMinor<Derived> minor(int row, int col);
EiBlock<Derived> block(int startRow, int endRow, int startCol= 0, int endCol = 0); EiBlock<Derived> block(int startRow, int endRow, int startCol, int endCol);
template<typename OtherDerived> template<typename OtherDerived>
EiMatrixProduct<Derived, OtherDerived> EiMatrixProduct<Derived, OtherDerived>
@ -145,12 +145,26 @@ template<typename Scalar, typename Derived> class EiObject
Derived& operator/=(const std::complex<float>& other); Derived& operator/=(const std::complex<float>& other);
Derived& operator/=(const std::complex<double>& other); Derived& operator/=(const std::complex<double>& other);
Scalar operator()(int row, int col = 0) const Scalar operator()(int row, int col) const
{ return read(row, col); } { return read(row, col); }
Scalar& operator()(int row, int col = 0) Scalar& operator()(int row, int col)
{ return write(row, col); } { return write(row, col); }
Scalar operator[](int index) const
{
assert(RowsAtCompileTime == 1 || ColsAtCompileTime == 1);
if(RowsAtCompileTime == 1) return read(0, index);
else return read(index, 0);
}
Scalar& operator[](int index)
{
assert(RowsAtCompileTime == 1 || ColsAtCompileTime == 1);
if(RowsAtCompileTime == 1) return write(0, index);
else return write(index, 0);
}
EiEval<Derived> eval() const EI_ALWAYS_INLINE; EiEval<Derived> eval() const EI_ALWAYS_INLINE;
}; };
@ -170,4 +184,4 @@ std::ostream & operator <<
return s; return s;
} }
#endif // EI_EIGENBASE_H #endif // EI_OBJECT_H

View File

@ -23,8 +23,8 @@
// License. This exception does not invalidate any other reasons why a work // 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. // based on this file might be covered by the GNU General Public License.
#ifndef EI_ROWANDCOL_H #ifndef EI_ROW_H
#define EI_ROWANDCOL_H #define EI_ROW_H
template<typename MatrixType> class EiRow template<typename MatrixType> class EiRow
: public EiObject<typename MatrixType::Scalar, EiRow<MatrixType> > : public EiObject<typename MatrixType::Scalar, EiRow<MatrixType> >
@ -34,8 +34,8 @@ template<typename MatrixType> class EiRow
typedef typename MatrixType::Ref MatRef; typedef typename MatrixType::Ref MatRef;
friend class EiObject<Scalar, EiRow<MatrixType> >; friend class EiObject<Scalar, EiRow<MatrixType> >;
static const int RowsAtCompileTime = MatrixType::ColsAtCompileTime, static const int RowsAtCompileTime = 1,
ColsAtCompileTime = 1; ColsAtCompileTime = MatrixType::ColsAtCompileTime;
EiRow(const MatRef& matrix, int row) EiRow(const MatRef& matrix, int row)
: m_matrix(matrix), m_row(row) : m_matrix(matrix), m_row(row)
@ -58,21 +58,19 @@ template<typename MatrixType> class EiRow
EiRow& _ref() { return *this; } EiRow& _ref() { return *this; }
const EiRow& _constRef() const { return *this; } const EiRow& _constRef() const { return *this; }
int _rows() const { return m_matrix.cols(); } int _rows() const { return 1; }
int _cols() const { return 1; } int _cols() const { return m_matrix.cols(); }
Scalar& _write(int row, int col=0) Scalar& _write(int row, int col)
{ {
EI_UNUSED(col); EI_UNUSED(row);
EI_CHECK_ROW_RANGE(*this, row); return m_matrix.write(m_row, col);
return m_matrix.write(m_row, row);
} }
Scalar _read(int row, int col=0) const Scalar _read(int row, int col) const
{ {
EI_UNUSED(col); EI_UNUSED(row);
EI_CHECK_ROW_RANGE(*this, row); return m_matrix.read(m_row, col);
return m_matrix.read(m_row, row);
} }
protected: protected:
@ -80,53 +78,6 @@ template<typename MatrixType> class EiRow
const int m_row; const int m_row;
}; };
template<typename MatrixType> class EiColumn
: public EiObject<typename MatrixType::Scalar, EiColumn<MatrixType> >
{
public:
typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::Ref MatRef;
friend class EiObject<Scalar, EiColumn<MatrixType> >;
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ColsAtCompileTime = 1;
EiColumn(const MatRef& matrix, int col)
: m_matrix(matrix), m_col(col)
{
EI_CHECK_COL_RANGE(matrix, col);
}
EiColumn(const EiColumn& other)
: m_matrix(other.m_matrix), m_col(other.m_col) {}
EI_INHERIT_ASSIGNMENT_OPERATORS(EiColumn)
private:
EiColumn& _ref() { return *this; }
const EiColumn& _constRef() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return 1; }
Scalar& _write(int row, int col=0)
{
EI_UNUSED(col);
EI_CHECK_ROW_RANGE(*this, row);
return m_matrix.write(row, m_col);
}
Scalar _read(int row, int col=0) const
{
EI_UNUSED(col);
EI_CHECK_ROW_RANGE(*this, row);
return m_matrix.read(row, m_col);
}
protected:
MatRef m_matrix;
const int m_col;
};
template<typename Scalar, typename Derived> template<typename Scalar, typename Derived>
EiRow<Derived> EiRow<Derived>
EiObject<Scalar, Derived>::row(int i) EiObject<Scalar, Derived>::row(int i)
@ -134,11 +85,4 @@ EiObject<Scalar, Derived>::row(int i)
return EiRow<Derived>(static_cast<Derived*>(this)->ref(), i); return EiRow<Derived>(static_cast<Derived*>(this)->ref(), i);
} }
template<typename Scalar, typename Derived> #endif // EI_ROW_H
EiColumn<Derived>
EiObject<Scalar, Derived>::col(int i)
{
return EiColumn<Derived>(static_cast<Derived*>(this)->ref(), i);
}
#endif // EI_ROWANDCOL_H

View File

@ -90,14 +90,14 @@ template<typename Scalar, typename Derived> \
Derived & \ Derived & \
EiObject<Scalar, Derived>::operator*=(const OtherScalar &other) \ EiObject<Scalar, Derived>::operator*=(const OtherScalar &other) \
{ \ { \
return *this = *this * other; \ return *this = *this * other; \
} \ } \
\ \
template<typename Scalar, typename Derived> \ template<typename Scalar, typename Derived> \
Derived & \ Derived & \
EiObject<Scalar, Derived>::operator/=(const OtherScalar &other) \ EiObject<Scalar, Derived>::operator/=(const OtherScalar &other) \
{ \ { \
return *this = *this / other; \ return *this = *this / other; \
} }
EI_MAKE_SCALAR_OPS(int) EI_MAKE_SCALAR_OPS(int)