From 96524fc5730b2e791d2406ced7e78f3bd9fb79ab Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Mon, 1 Oct 2007 07:45:30 +0000 Subject: 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 --- doc/tutorial.cpp | 4 +- src/Manip | 3 +- src/internal/Matrix.h | 10 ++-- src/internal/Object.h | 26 +++++++-- src/internal/Row.h | 88 +++++++++++++++++++++++++++++ src/internal/RowAndCol.h | 144 ----------------------------------------------- src/internal/ScalarOps.h | 4 +- 7 files changed, 120 insertions(+), 159 deletions(-) create mode 100644 src/internal/Row.h delete mode 100644 src/internal/RowAndCol.h diff --git a/doc/tutorial.cpp b/doc/tutorial.cpp index 03495dbcb..a5b019531 100644 --- a/doc/tutorial.cpp +++ b/doc/tutorial.cpp @@ -26,8 +26,10 @@ int main(int, char **) 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 << "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 << "The matrix m2 with row 0 and column 1 removed is:" << endl << m2.minor(0,1) << endl; + return 0; } diff --git a/src/Manip b/src/Manip index ca450ddf7..db4607091 100644 --- a/src/Manip +++ b/src/Manip @@ -26,7 +26,8 @@ #ifndef 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/Minor.h" diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h index e5481df2f..19e3b313d 100644 --- a/src/internal/Matrix.h +++ b/src/internal/Matrix.h @@ -33,7 +33,7 @@ template class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >, - public EiMatrixStorage<_Scalar, _Rows, _Cols> + public EiMatrixStorage<_Scalar, _Rows, _Cols> { public: friend class EiObject<_Scalar, EiMatrix>; @@ -57,13 +57,13 @@ class EiMatrix : public EiObject<_Scalar, EiMatrix<_Scalar, _Rows, _Cols> >, Ref _ref() { return Ref(*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); 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); 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) \ typedef EiMatrix EiMatrix##SizeSuffix##TypeSuffix; \ -typedef EiMatrix EiVector##SizeSuffix##TypeSuffix; +typedef EiMatrix EiVector##SizeSuffix##TypeSuffix; \ +typedef EiMatrix EiRowVector##SizeSuffix##TypeSuffix; #define EI_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \ EI_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \ @@ -124,6 +125,5 @@ EI_MAKE_TYPEDEFS_ALL_SIZES(std::complex, cd) #include "Eval.h" #include "MatrixOps.h" #include "ScalarOps.h" -#include "RowAndCol.h" #endif // EI_MATRIX_H diff --git a/src/internal/Object.h b/src/internal/Object.h index 3f27e6c0e..de33c84c0 100644 --- a/src/internal/Object.h +++ b/src/internal/Object.h @@ -24,8 +24,8 @@ // 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_EIGENBASE_H -#define EI_EIGENBASE_H +#ifndef EI_OBJECT_H +#define EI_OBJECT_H #include "Util.h" @@ -118,7 +118,7 @@ template class EiObject EiRow row(int i); EiColumn col(int i); EiMinor minor(int row, int col); - EiBlock block(int startRow, int endRow, int startCol= 0, int endCol = 0); + EiBlock block(int startRow, int endRow, int startCol, int endCol); template EiMatrixProduct @@ -145,12 +145,26 @@ template class EiObject Derived& operator/=(const std::complex& other); Derived& operator/=(const std::complex& other); - Scalar operator()(int row, int col = 0) const + Scalar operator()(int row, int col) const { return read(row, col); } - Scalar& operator()(int row, int col = 0) + Scalar& operator()(int row, int 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 eval() const EI_ALWAYS_INLINE; }; @@ -170,4 +184,4 @@ std::ostream & operator << return s; } -#endif // EI_EIGENBASE_H +#endif // EI_OBJECT_H diff --git a/src/internal/Row.h b/src/internal/Row.h new file mode 100644 index 000000000..39efc0b2b --- /dev/null +++ b/src/internal/Row.h @@ -0,0 +1,88 @@ +// 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 +// +// 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_ROW_H +#define EI_ROW_H + +template class EiRow + : public EiObject > +{ + public: + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::Ref MatRef; + friend class EiObject >; + + static const int RowsAtCompileTime = 1, + ColsAtCompileTime = MatrixType::ColsAtCompileTime; + + EiRow(const MatRef& matrix, int row) + : m_matrix(matrix), m_row(row) + { + EI_CHECK_ROW_RANGE(matrix, row); + } + + EiRow(const EiRow& other) + : m_matrix(other.m_matrix), m_row(other.m_row) {} + + template + EiRow& operator=(const EiObject& other) + { + return EiObject >::operator=(other); + } + + EI_INHERIT_ASSIGNMENT_OPERATORS(EiRow) + + private: + EiRow& _ref() { return *this; } + const EiRow& _constRef() const { return *this; } + + int _rows() const { return 1; } + int _cols() const { return m_matrix.cols(); } + + Scalar& _write(int row, int col) + { + EI_UNUSED(row); + return m_matrix.write(m_row, col); + } + + Scalar _read(int row, int col) const + { + EI_UNUSED(row); + return m_matrix.read(m_row, col); + } + + protected: + MatRef m_matrix; + const int m_row; +}; + +template +EiRow +EiObject::row(int i) +{ + return EiRow(static_cast(this)->ref(), i); +} + +#endif // EI_ROW_H diff --git a/src/internal/RowAndCol.h b/src/internal/RowAndCol.h deleted file mode 100644 index 62a9d56da..000000000 --- a/src/internal/RowAndCol.h +++ /dev/null @@ -1,144 +0,0 @@ -// 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 -// -// 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_ROWANDCOL_H -#define EI_ROWANDCOL_H - -template class EiRow - : public EiObject > -{ - public: - typedef typename MatrixType::Scalar Scalar; - typedef typename MatrixType::Ref MatRef; - friend class EiObject >; - - static const int RowsAtCompileTime = MatrixType::ColsAtCompileTime, - ColsAtCompileTime = 1; - - EiRow(const MatRef& matrix, int row) - : m_matrix(matrix), m_row(row) - { - EI_CHECK_ROW_RANGE(matrix, row); - } - - EiRow(const EiRow& other) - : m_matrix(other.m_matrix), m_row(other.m_row) {} - - template - EiRow& operator=(const EiObject& other) - { - return EiObject >::operator=(other); - } - - EI_INHERIT_ASSIGNMENT_OPERATORS(EiRow) - - private: - EiRow& _ref() { return *this; } - const EiRow& _constRef() const { return *this; } - - int _rows() const { return m_matrix.cols(); } - 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(m_row, row); - } - - Scalar _read(int row, int col=0) const - { - EI_UNUSED(col); - EI_CHECK_ROW_RANGE(*this, row); - return m_matrix.read(m_row, row); - } - - protected: - MatRef m_matrix; - const int m_row; -}; - -template class EiColumn - : public EiObject > -{ - public: - typedef typename MatrixType::Scalar Scalar; - typedef typename MatrixType::Ref MatRef; - friend class EiObject >; - - 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 -EiRow -EiObject::row(int i) -{ - return EiRow(static_cast(this)->ref(), i); -} - -template -EiColumn -EiObject::col(int i) -{ - return EiColumn(static_cast(this)->ref(), i); -} - -#endif // EI_ROWANDCOL_H diff --git a/src/internal/ScalarOps.h b/src/internal/ScalarOps.h index e2acfb5d9..903f2751b 100644 --- a/src/internal/ScalarOps.h +++ b/src/internal/ScalarOps.h @@ -90,14 +90,14 @@ template \ Derived & \ EiObject::operator*=(const OtherScalar &other) \ { \ - return *this = *this * other; \ + return *this = *this * other; \ } \ \ template \ Derived & \ EiObject::operator/=(const OtherScalar &other) \ { \ - return *this = *this / other; \ + return *this = *this / other; \ } EI_MAKE_SCALAR_OPS(int) -- cgit v1.2.3