From 1af61c6ff078e6f9f9cd1298374811cfc73d4488 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Sun, 9 Sep 2007 09:41:15 +0000 Subject: reorganize header files, split MatrixBase into smaller files. expose only a few meta-headers to the user, the rest moves to a internal/ subdirectory --- src/internal/Block.h | 99 ++++++++++++++++++++ src/internal/CMakeLists.txt | 6 ++ src/internal/Matrix.h | 179 ++++++++++++++++++++++++++++++++++++ src/internal/MatrixAlias.h | 116 +++++++++++++++++++++++ src/internal/MatrixBase.h | 201 ++++++++++++++++++++++++++++++++++++++++ src/internal/MatrixOps.h | 218 ++++++++++++++++++++++++++++++++++++++++++++ src/internal/MatrixRef.h | 76 +++++++++++++++ src/internal/MatrixXpr.h | 108 ++++++++++++++++++++++ src/internal/Minor.h | 95 +++++++++++++++++++ src/internal/RowAndCol.h | 141 ++++++++++++++++++++++++++++ src/internal/ScalarOps.h | 145 +++++++++++++++++++++++++++++ src/internal/Util.h | 83 +++++++++++++++++ src/internal/Vector.h | 190 ++++++++++++++++++++++++++++++++++++++ 13 files changed, 1657 insertions(+) create mode 100644 src/internal/Block.h create mode 100644 src/internal/CMakeLists.txt create mode 100644 src/internal/Matrix.h create mode 100644 src/internal/MatrixAlias.h create mode 100644 src/internal/MatrixBase.h create mode 100644 src/internal/MatrixOps.h create mode 100644 src/internal/MatrixRef.h create mode 100644 src/internal/MatrixXpr.h create mode 100644 src/internal/Minor.h create mode 100644 src/internal/RowAndCol.h create mode 100644 src/internal/ScalarOps.h create mode 100644 src/internal/Util.h create mode 100644 src/internal/Vector.h (limited to 'src/internal') diff --git a/src/internal/Block.h b/src/internal/Block.h new file mode 100644 index 000000000..68c4439cd --- /dev/null +++ b/src/internal/Block.h @@ -0,0 +1,99 @@ +// 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 EIGEN_BLOCK_H +#define EIGEN_BLOCK_H + +namespace Eigen { + +template class MatrixBlock +{ + public: + typedef typename MatrixType::Scalar Scalar; + + MatrixBlock(const MatrixType& matrix, int startRow, int endRow, + int startCol = 0, int endCol = 0) + : m_matrix(matrix), m_startRow(startRow), m_endRow(endRow), + m_startCol(startCol), m_endCol(endCol) + { + assert(startRow >= 0 && startRow <= endRow && endRow < matrix.rows() + && startCol >= 0 && startCol <= endCol && endCol < matrix.cols()); + } + + MatrixBlock(const MatrixBlock& other) + : m_matrix(other.m_matrix), m_startRow(other.m_startRow), m_endRow(other.m_endRow), + m_startCol(other.m_startCol), m_endCol(other.m_endCol) {} + + int rows() const { return m_endRow - m_startRow + 1; } + int cols() const { return m_endCol - m_startCol + 1; } + + Scalar& write(int row, int col=0) + { + return m_matrix.write(row + m_startRow, col + m_startCol); + } + + Scalar read(int row, int col=0) const + { + return m_matrix.read(row + m_startRow, col + m_startCol); + } + + protected: + MatrixType m_matrix; + const int m_startRow, m_endRow, m_startCol, m_endCol; +}; + +template +MatrixXpr< + MatrixBlock< + MatrixRef< + MatrixBase + > + > +> +MatrixBase::block(int startRow, int endRow, int startCol, int endCol) +{ + typedef MatrixBlock ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(ref(), startRow, endRow, startCol, endCol)); +} + +template +MatrixXpr< + MatrixBlock< + MatrixXpr + > +> +MatrixXpr::block(int startRow, int endRow, int startCol, int endCol) +{ + typedef MatrixBlock< + MatrixXpr + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(*this, startRow, endRow, startCol, endCol)); +} + +} + +#endif // EIGEN_BLOCK_H diff --git a/src/internal/CMakeLists.txt b/src/internal/CMakeLists.txt new file mode 100644 index 000000000..c3643b916 --- /dev/null +++ b/src/internal/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB Eigen_internal_SRCS "*.h") + +INSTALL(FILES + ${Eigen_internal_SRCS} + DESTINATION ${INCLUDE_INSTALL_DIR}/internal + ) diff --git a/src/internal/Matrix.h b/src/internal/Matrix.h new file mode 100644 index 000000000..be7c22bc3 --- /dev/null +++ b/src/internal/Matrix.h @@ -0,0 +1,179 @@ +// 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. + +/** \file Matrix.h + * \brief Matrix and MatrixX class templates + */ + +#ifndef EIGEN_MATRIX_H +#define EIGEN_MATRIX_H + +#include "MatrixBase.h" + +namespace Eigen +{ + +template +class Matrix: public MatrixBase< Matrix > +{ + friend class MatrixBase >; + typedef class MatrixBase > Base; + + public: + typedef T Scalar; + + private: + + static bool _hasDynamicNumRows() + { return false; } + + static bool _hasDynamicNumCols() + { return false; } + + int _rows() const + { return Rows; } + + int _cols() const + { return Cols; } + + void _resize( int rows, int cols ) const + { + assert(rows == Rows && cols == Cols); + } + + public: + + Matrix() + { + assert(Rows > 0 && Cols > 0); + } + + Matrix(const Matrix& other) : Base() + { + *this = other; + } + + Matrix(int rows, int cols) + { + assert(Rows > 0 && Cols > 0 && rows == Rows && cols == Cols); + } + + void operator=(const Matrix & other) + { Base::operator=(other); } + + template + void operator=(const MatrixXpr &xpr) + { Base::operator=(xpr); } + + template + explicit Matrix(const MatrixXpr& xpr) + { + *this = xpr; + } + + protected: + + T m_array[ Rows * Cols ]; + +}; + +template +class MatrixX : public MatrixBase< MatrixX > +{ + friend class MatrixBase >; + typedef class MatrixBase > Base; + + public: + + typedef T Scalar; + + MatrixX(int rows, int cols) + { _init(rows, cols); } + + MatrixX(const MatrixX& other) : Base() + { + _init(other.rows(), other.cols()); + *this = other; + } + + ~MatrixX() + { delete[] m_array; } + + void operator=(const MatrixX& other) + { Base::operator=(other); } + + template + void operator=(const MatrixXpr &xpr) + { Base::operator=(xpr); } + + template + explicit MatrixX(const MatrixXpr& xpr) + { + _init(xpr.rows(), xpr.cols()); + *this = xpr; + } + + protected: + + int m_rows, m_cols; + + T *m_array; + + private: + + int _rows() const { return m_rows; } + int _cols() const { return m_cols; } + + static bool _hasDynamicNumRows() + { return true; } + + static bool _hasDynamicNumCols() + { return true; } + + void _resize( int rows, int cols ) + { + assert(rows > 0 && cols > 0); + if(rows * cols > m_rows * m_cols) + { + delete[] m_array; + m_array = new T[rows * cols]; + } + m_rows = rows; + m_cols = cols; + } + + void _init( int rows, int cols ) + { + assert(rows > 0 && cols > 0); + m_rows = rows; + m_cols = cols; + m_array = new T[m_rows * m_cols]; + } + +}; + +} // namespace Eigen + +#endif // EIGEN_MATRIX_H diff --git a/src/internal/MatrixAlias.h b/src/internal/MatrixAlias.h new file mode 100644 index 000000000..868011321 --- /dev/null +++ b/src/internal/MatrixAlias.h @@ -0,0 +1,116 @@ +// 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 EIGEN_MATRIXALIAS_H +#define EIGEN_MATRIXALIAS_H + +namespace Eigen +{ + +template class MatrixAlias +{ + public: + typedef typename Derived::Scalar Scalar; + typedef MatrixRef > Ref; + typedef MatrixXpr Xpr; + + MatrixAlias(Derived& matrix) : m_aliased(matrix), m_tmp(matrix) {} + MatrixAlias(const MatrixAlias& other) : m_aliased(other.m_aliased), m_tmp(other.m_tmp) {} + + ~MatrixAlias() + { + m_aliased.xpr() = m_tmp; + } + + Ref ref() + { + return Ref(*this); + } + + Xpr xpr() + { + return Xpr(ref()); + } + + static bool hasDynamicNumRows() + { + return MatrixBase::hasDynamicNumRows(); + } + + static bool hasDynamicNumCols() + { + return MatrixBase::hasDynamicNumCols(); + } + + int rows() const { return m_tmp.rows(); } + int cols() const { return m_tmp.cols(); } + + Scalar& write(int row, int col) + { + return m_tmp.write(row, col); + } + + MatrixXpr > row(int i) { return xpr().row(i); }; + MatrixXpr > col(int i) { return xpr().col(i); }; + MatrixXpr > minor(int row, int col) { return xpr().minor(row, col); }; + MatrixXpr > + block(int startRow, int endRow, int startCol = 0, int endCol = 0) + { + return xpr().block(startRow, endRow, startCol, endCol); + } + + template + void operator=(const MatrixXpr &other) + { + xpr() = other; + } + + template + void operator+=(const MatrixXpr &other) + { + xpr() += other; + } + + template + void operator-=(const MatrixXpr &other) + { + xpr() -= other; + } + + protected: + MatrixRef > m_aliased; + Derived m_tmp; +}; + +template +typename MatrixBase::Alias +MatrixBase::alias() +{ + return Alias(*static_cast(this)); +} + +} // namespace Eigen + +#endif // EIGEN_MATRIXALIAS_H diff --git a/src/internal/MatrixBase.h b/src/internal/MatrixBase.h new file mode 100644 index 000000000..910cc4ac0 --- /dev/null +++ b/src/internal/MatrixBase.h @@ -0,0 +1,201 @@ +// 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 EIGEN_MATRIXBASE_H +#define EIGEN_MATRIXBASE_H + +#include "Util.h" +#include "MatrixXpr.h" +#include "MatrixRef.h" + +namespace Eigen +{ + +template +class MatrixBase +{ + public: + + typedef typename ForwardDecl::Scalar Scalar; + typedef MatrixRef > Ref; + typedef MatrixXpr Xpr; + typedef MatrixAlias Alias; + + Ref ref() + { + return Ref(*this); + } + + Xpr xpr() + { + return Xpr(ref()); + } + + Alias alias(); + + static bool hasDynamicNumRows() + { + return Derived::_hasDynamicNumRows(); + } + + static bool hasDynamicNumCols() + { + return Derived::_hasDynamicNumCols(); + } + + int rows() const + { + return static_cast(this)->_rows(); + } + + int cols() const + { + return static_cast(this)->_cols(); + } + + void resize(int rows, int cols) + { + static_cast(this)->_resize(rows, cols); + } + + const Scalar* array() const + { + return static_cast(this)->m_array; + } + + Scalar* array() + { + return static_cast(this)->m_array; + } + + const Scalar& read(int row, int col = 0) const + { + EIGEN_CHECK_RANGES(*this, row, col); + return array()[row + col * rows()]; + } + + const Scalar& operator()(int row, int col = 0) const + { + return read(row, col); + } + + Scalar& write(int row, int col = 0) + { + EIGEN_CHECK_RANGES(*this, row, col); + return array()[row + col * rows()]; + } + + Scalar& operator()(int row, int col = 0) + { + return write(row, col); + } + + template + MatrixBase& operator=(const MatrixXpr &otherXpr) + { + resize(otherXpr.rows(), otherXpr.cols()); + xpr() = otherXpr; + return *this; + } + + MatrixBase& operator=(const MatrixBase &other) + { + resize(other.rows(), other.cols()); + for(int i = 0; i < rows(); i++) + for(int j = 0; j < cols(); j++) + this->operator()(i, j) = other(i, j); + return *this; + } + + MatrixXpr > row(int i); + MatrixXpr > col(int i); + MatrixXpr > minor(int row, int col); + MatrixXpr > + block(int startRow, int endRow, int startCol = 0, int endCol = 0); + + template + MatrixBase& operator+=(const MatrixXpr &xpr); + template + MatrixBase& operator-=(const MatrixXpr &xpr); + template + MatrixBase& operator+=(MatrixBase &other); + template + MatrixBase& operator-=(MatrixBase &other); + + protected: + + MatrixBase() {}; +}; + +template +template +MatrixXpr& MatrixXpr::operator=(const MatrixBase& matrix) +{ + assert(rows() == matrix.rows() && cols() == matrix.cols()); + for(int i = 0; i < rows(); i++) + for(int j = 0; j < cols(); j++) + write(i, j) = matrix(i, j); + return *this; +} + +template +std::ostream & operator << +( std::ostream & s, + const MatrixBase & m ) +{ + for( int i = 0; i < m.rows(); i++ ) + { + s << m( i, 0 ); + for (int j = 1; j < m.cols(); j++ ) + s << " " << m( i, j ); + if( i < m.rows() - 1) + s << std::endl; + } + return s; +} + +template +std::ostream & operator << (std::ostream & s, + const MatrixXpr& xpr) +{ + for( int i = 0; i < xpr.rows(); i++ ) + { + s << xpr.read(i, 0); + for (int j = 1; j < xpr.cols(); j++ ) + s << " " << xpr.read(i, j); + if( i < xpr.rows() - 1) + s << std::endl; + } + return s; +} + +} // namespace Eigen + +#include "MatrixAlias.h" +#include "MatrixOps.h" +#include "ScalarOps.h" +#include "RowAndCol.h" + +#endif // EIGEN_MATRIXBASE_H diff --git a/src/internal/MatrixOps.h b/src/internal/MatrixOps.h new file mode 100644 index 000000000..e76e64c85 --- /dev/null +++ b/src/internal/MatrixOps.h @@ -0,0 +1,218 @@ +// 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 EIGEN_MATRIXOPS_H +#define EIGEN_MATRIXOPS_H + +namespace Eigen { + +#define EIGEN_MAKE_MATRIX_OP_XPR(NAME, SYMBOL) \ +template class Matrix##NAME \ +{ \ + public: \ + typedef typename Lhs::Scalar Scalar; \ +\ + Matrix##NAME(const Lhs& lhs, const Rhs& rhs) \ + : m_lhs(lhs), m_rhs(rhs) \ + { \ + assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols()); \ + } \ +\ + Matrix##NAME(const Matrix##NAME& other) \ + : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} \ +\ + int rows() const { return m_lhs.rows(); } \ + int cols() const { return m_lhs.cols(); } \ +\ + Scalar read(int row, int col) const \ + { \ + return m_lhs.read(row, col) SYMBOL m_rhs.read(row, col); \ + } \ +\ + protected: \ + const Lhs m_lhs; \ + const Rhs m_rhs; \ +}; + +EIGEN_MAKE_MATRIX_OP_XPR(Sum, +) +EIGEN_MAKE_MATRIX_OP_XPR(Difference, -) + +#undef EIGEN_MAKE_MATRIX_OP_XPR + +template class MatrixProduct +{ + public: + typedef typename Lhs::Scalar Scalar; + + MatrixProduct(const Lhs& lhs, const Rhs& rhs) + : m_lhs(lhs), m_rhs(rhs) + { + assert(lhs.cols() == rhs.rows()); + } + + MatrixProduct(const MatrixProduct& other) + : m_lhs(other.m_lhs), m_rhs(other.m_rhs) {} + + int rows() const { return m_lhs.rows(); } + int cols() const { return m_rhs.cols(); } + + Scalar read(int row, int col) const + { + Scalar x = static_cast(0); + for(int i = 0; i < m_lhs.cols(); i++) + x += m_lhs.read(row, i) * m_rhs.read(i, col); + return x; + } + + protected: + const Lhs m_lhs; + const Rhs m_rhs; +}; + +#define EIGEN_MAKE_MATRIX_OP(NAME, SYMBOL) \ +template \ +MatrixXpr< \ + Matrix##NAME< \ + MatrixXpr, \ + MatrixXpr \ + > \ +> \ +operator SYMBOL(const MatrixXpr &xpr1, const MatrixXpr &xpr2) \ +{ \ + typedef Matrix##NAME< \ + MatrixXpr, \ + MatrixXpr \ + > ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(xpr1, xpr2)); \ +} \ +\ +template \ +MatrixXpr< \ + Matrix##NAME< \ + MatrixRef >, \ + MatrixXpr \ + > \ +> \ +operator SYMBOL(MatrixBase &mat, const MatrixXpr &xpr) \ +{ \ + typedef Matrix##NAME< \ + MatrixRef >, \ + MatrixXpr \ + > ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(mat.ref(), xpr)); \ +} \ +\ +template \ +MatrixXpr< \ + Matrix##NAME< \ + MatrixXpr, \ + MatrixRef > \ + > \ +> \ +operator SYMBOL(const MatrixXpr &xpr, MatrixBase &mat) \ +{ \ + typedef Matrix##NAME< \ + MatrixXpr, \ + MatrixRef > \ + > ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(xpr, mat.ref())); \ +} \ +\ +template \ +MatrixXpr< \ + Matrix##NAME< \ + MatrixRef >, \ + MatrixRef > \ + > \ +> \ +operator SYMBOL(MatrixBase &mat1, MatrixBase &mat2) \ +{ \ + typedef Matrix##NAME< \ + MatrixRef >, \ + MatrixRef > \ + > ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(mat1.ref(), \ + mat2.ref())); \ +} + +EIGEN_MAKE_MATRIX_OP(Sum, +) +EIGEN_MAKE_MATRIX_OP(Difference, -) +EIGEN_MAKE_MATRIX_OP(Product, *) + +#undef EIGEN_MAKE_MATRIX_OP + +#define EIGEN_MAKE_MATRIX_OP_EQ(SYMBOL) \ +template \ +template \ +MatrixBase & \ +MatrixBase::operator SYMBOL##=(MatrixBase &mat2) \ +{ \ + return *this = *this SYMBOL mat2; \ +} \ +\ +template \ +template \ +MatrixBase & \ +MatrixBase::operator SYMBOL##=(const MatrixXpr &xpr) \ +{ \ + return *this = *this SYMBOL xpr; \ +} \ +\ +template \ +template \ +MatrixXpr & \ +MatrixXpr::operator SYMBOL##=(MatrixBase &mat) \ +{ \ + assert(rows() == mat.rows() && cols() == mat.cols()); \ + for(int i = 0; i < rows(); i++) \ + for(int j = 0; j < cols(); j++) \ + write(i, j) SYMBOL##= mat.read(i, j); \ + return *this; \ +} \ +\ +template \ +template \ +MatrixXpr & \ +MatrixXpr::operator SYMBOL##=(const MatrixXpr &other) \ +{ \ + assert(rows() == other.rows() && cols() == other.cols()); \ + for(int i = 0; i < rows(); i++) \ + for(int j = 0; j < cols(); j++) \ + write(i, j) SYMBOL##= other.read(i, j); \ + return *this; \ +} + +EIGEN_MAKE_MATRIX_OP_EQ(+) +EIGEN_MAKE_MATRIX_OP_EQ(-) + +#undef EIGEN_MAKE_MATRIX_OP_EQ + +} // namespace Eigen + +#endif // EIGEN_MATRIXOPS_H diff --git a/src/internal/MatrixRef.h b/src/internal/MatrixRef.h new file mode 100644 index 000000000..0123c535c --- /dev/null +++ b/src/internal/MatrixRef.h @@ -0,0 +1,76 @@ +// 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 EIGEN_MATRIXREF_H +#define EIGEN_MATRIXREF_H + +namespace Eigen +{ + +template class MatrixRef +{ + public: + typedef typename ForwardDecl::Scalar Scalar; + typedef MatrixXpr > Xpr; + + MatrixRef(MatrixType& matrix) : m_matrix(matrix) {} + MatrixRef(const MatrixRef& other) : m_matrix(other.m_matrix) {} + ~MatrixRef() {} + + static bool hasDynamicNumRows() + { + return MatrixType::hasDynamicNumRows(); + } + + static bool hasDynamicNumCols() + { + return MatrixType::hasDynamicNumCols(); + } + + int rows() const { return m_matrix.rows(); } + int cols() const { return m_matrix.cols(); } + + const Scalar& read(int row, int col) const + { + return m_matrix.read(row, col); + } + + Scalar& write(int row, int col) + { + return m_matrix.write(row, col); + } + + Xpr xpr() + { + return Xpr(*this); + } + + protected: + MatrixType& m_matrix; +}; + +} // namespace Eigen + +#endif // EIGEN_MATRIXREF_H diff --git a/src/internal/MatrixXpr.h b/src/internal/MatrixXpr.h new file mode 100644 index 000000000..fe890d16c --- /dev/null +++ b/src/internal/MatrixXpr.h @@ -0,0 +1,108 @@ +// 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 EIGEN_MATRIXXPR_H +#define EIGEN_MATRIXXPR_H + +namespace Eigen { + +//forward declarations +template class MatrixRow; +template class MatrixCol; +template class MatrixMinor; +template class MatrixBlock; + +template class MatrixXpr +{ + public: + typedef typename Content::Scalar Scalar; + + MatrixXpr(const Content& content) + : m_content(content) {} + + MatrixXpr(const MatrixXpr& other) + : m_content(other.m_content) {} + + ~MatrixXpr() {} + + int rows() const { return m_content.rows(); } + int cols() const { return m_content.cols(); } + + Scalar& write(int row, int col) + { + return m_content.write(row, col); + } + + Scalar read(int row, int col) const + { + return m_content.read(row, col); + } + + template + MatrixXpr& operator=(const MatrixXpr& other) + { + assert(rows() == other.rows() && cols() == other.cols()); + for(int i = 0; i < rows(); i++) + for(int j = 0; j < cols(); j++) + write(i, j) = other.read(i, j); + return *this; + } + + //special case of the above template operator=. Strangely, g++ 4.1 failed to use + //that template when OtherContent == Content + MatrixXpr& operator=(const MatrixXpr& other) + { + assert(rows() == other.rows() && cols() == other.cols()); + for(int i = 0; i < rows(); i++) + for(int j = 0; j < cols(); j++) + write(i, j) = other.read(i, j); + return *this; + } + + template + MatrixXpr& operator=(const MatrixBase& matrix); + + MatrixXpr > > row(int i); + MatrixXpr > > col(int i); + MatrixXpr > > minor(int row, int col); + MatrixXpr > > + block(int startRow, int endRow, int startCol= 0, int endCol = 0); + + template + MatrixXpr& operator+=(const MatrixXpr &other); + template + MatrixXpr& operator-=(const MatrixXpr &other); + template + MatrixXpr& operator+=(MatrixBase &matrix); + template + MatrixXpr& operator-=(MatrixBase &matrix); + + protected: + Content m_content; +}; + +} // namespace Eigen + +#endif // EIGEN_MATRIXXPR_H diff --git a/src/internal/Minor.h b/src/internal/Minor.h new file mode 100644 index 000000000..da0d759de --- /dev/null +++ b/src/internal/Minor.h @@ -0,0 +1,95 @@ +// 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 EIGEN_MINOR_H +#define EIGEN_MINOR_H + +namespace Eigen { + +template class MatrixMinor +{ + public: + typedef typename MatrixType::Scalar Scalar; + + MatrixMinor(const MatrixType& matrix, int row, int col = 0) + : m_matrix(matrix), m_row(row), m_col(col) + { + EIGEN_CHECK_RANGES(matrix, row, col); + } + + MatrixMinor(const MatrixMinor& other) + : m_matrix(other.m_matrix), m_row(other.m_row), m_col(other.m_col) {} + + int rows() const { return m_matrix.rows() - 1; } + int cols() const { return m_matrix.cols() - 1; } + + Scalar& write(int row, int col=0) + { + return m_matrix.write(row + (row >= m_row), col + (col >= m_col)); + } + + Scalar read(int row, int col=0) const + { + return m_matrix.read(row + (row >= m_row), col + (col >= m_col)); + } + + protected: + MatrixType m_matrix; + const int m_row, m_col; +}; + +template +MatrixXpr< + MatrixMinor< + MatrixRef< + MatrixBase + > + > +> +MatrixBase::minor(int row, int col) +{ + typedef MatrixMinor ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(ref(), row, col)); +} + +template +MatrixXpr< + MatrixMinor< + MatrixXpr + > +> +MatrixXpr::minor(int row, int col) +{ + typedef MatrixMinor< + MatrixXpr + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(*this, row, col)); +} + +} + +#endif // EIGEN_MINOR_H diff --git a/src/internal/RowAndCol.h b/src/internal/RowAndCol.h new file mode 100644 index 000000000..5da3cfa23 --- /dev/null +++ b/src/internal/RowAndCol.h @@ -0,0 +1,141 @@ +// 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 EIGEN_ROWANDCOL_H +#define EIGEN_ROWANDCOL_H + +namespace Eigen { + +template class MatrixRow +{ + public: + typedef typename MatrixType::Scalar Scalar; + + MatrixRow(const MatrixType& matrix, int row) + : m_matrix(matrix), m_row(row) + { + EIGEN_CHECK_ROW_RANGE(matrix, row); + } + + MatrixRow(const MatrixRow& other) + : m_matrix(other.m_matrix), m_row(other.m_row) {} + + int rows() const { return m_matrix.cols(); } + int cols() const { return 1; } + + Scalar& write(int row, int col=0) + { + EIGEN_UNUSED(col); + EIGEN_CHECK_ROW_RANGE(*this, row); + return m_matrix.write(m_row, row); + } + + Scalar read(int row, int col=0) const + { + EIGEN_UNUSED(col); + EIGEN_CHECK_ROW_RANGE(*this, row); + return m_matrix.read(m_row, row); + } + + protected: + MatrixType m_matrix; + const int m_row; +}; + +template class MatrixCol +{ + public: + typedef typename MatrixType::Scalar Scalar; + + MatrixCol(const MatrixType& matrix, int col) + : m_matrix(matrix), m_col(col) + { + EIGEN_CHECK_COL_RANGE(matrix, col); + } + + MatrixCol(const MatrixCol& other) + : m_matrix(other.m_matrix), m_col(other.m_col) {} + + int rows() const { return m_matrix.rows(); } + int cols() const { return 1; } + + Scalar& write(int row, int col=0) + { + EIGEN_UNUSED(col); + EIGEN_CHECK_ROW_RANGE(*this, row); + return m_matrix.write(row, m_col); + } + + Scalar read(int row, int col=0) const + { + EIGEN_UNUSED(col); + EIGEN_CHECK_ROW_RANGE(*this, row); + return m_matrix.read(row, m_col); + } + + protected: + MatrixType m_matrix; + const int m_col; +}; + +#define EIGEN_MAKE_ROW_COL_FUNCTIONS(func, Func) \ +template \ +MatrixXpr< \ + Matrix##Func< \ + MatrixRef< \ + MatrixBase \ + > \ + > \ +> \ +MatrixBase::func(int i)\ +{ \ + typedef Matrix##Func ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(ref(), i)); \ +} \ +\ +template \ +MatrixXpr< \ + Matrix##Func< \ + MatrixXpr \ + > \ +> \ +MatrixXpr::func(int i)\ +{ \ + typedef Matrix##Func< \ + MatrixXpr \ + > ProductType; \ + typedef MatrixXpr XprType; \ + return XprType(ProductType(*this, i)); \ +} + +EIGEN_MAKE_ROW_COL_FUNCTIONS(row, Row) +EIGEN_MAKE_ROW_COL_FUNCTIONS(col, Col) + +#undef EIGEN_MAKE_ROW_COL_FUNCTIONS + +} + +#endif // EIGEN_ROWANDCOL_H diff --git a/src/internal/ScalarOps.h b/src/internal/ScalarOps.h new file mode 100644 index 000000000..01772569a --- /dev/null +++ b/src/internal/ScalarOps.h @@ -0,0 +1,145 @@ +// 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 EIGEN_SCALAROPS_H +#define EIGEN_SCALAROPS_H + +namespace Eigen { + +template class ScalarProduct +{ + public: + typedef typename MatrixType::Scalar Scalar; + + ScalarProduct(const MatrixType& matrix, Scalar scalar) + : m_matrix(matrix), m_scalar(scalar) {} + + ScalarProduct(const ScalarProduct& other) + : m_matrix(other.m_matrix), m_scalar(other.m_scalar) {} + + int rows() const { return m_matrix.rows(); } + int cols() const { return m_matrix.cols(); } + + Scalar read(int row, int col) const + { + return m_matrix.read(row, col) * m_scalar; + } + + protected: + const MatrixType m_matrix; + const Scalar m_scalar; +}; + +template +MatrixXpr< + ScalarProduct< + MatrixXpr + > +> +operator *(const MatrixXpr& xpr, + typename Content::Scalar scalar) +{ + typedef ScalarProduct< + MatrixXpr + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(xpr, scalar)); +} + +template +MatrixXpr< + ScalarProduct< + MatrixXpr + > +> +operator *(typename Content::Scalar scalar, + const MatrixXpr& xpr) +{ + typedef ScalarProduct< + MatrixXpr + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(xpr, scalar)); +} + +template +MatrixXpr< + ScalarProduct< + MatrixRef > + > +> +operator *(MatrixBase& matrix, + typename Derived::Scalar scalar) +{ + typedef ScalarProduct< + MatrixRef > + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(matrix.ref(), scalar)); +} + +template +MatrixXpr< + ScalarProduct< + MatrixRef > + > +> +operator *(typename Derived::Scalar scalar, + MatrixBase& matrix) +{ + typedef ScalarProduct< + MatrixRef > + > ProductType; + typedef MatrixXpr XprType; + return XprType(ProductType(matrix.ref(), scalar)); +} + +template +MatrixXpr< + ScalarProduct< + MatrixXpr + > +> +operator /(MatrixXpr& xpr, + typename Content::Scalar scalar) +{ + return xpr * (static_cast(1) / scalar); +} + +template +MatrixXpr< + ScalarProduct< + MatrixRef > + > +> +operator /(MatrixBase& matrix, + typename Derived::Scalar scalar) +{ + return matrix * (static_cast(1) / scalar); +} + +} // namespace Eigen + +#endif // EIGEN_SCALAROPS_H diff --git a/src/internal/Util.h b/src/internal/Util.h new file mode 100644 index 000000000..31b94b421 --- /dev/null +++ b/src/internal/Util.h @@ -0,0 +1,83 @@ +// 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 EIGEN_UTIL_H +#define EIGEN_UTIL_H + +#include +#include + +#undef minor + +#define EIGEN_UNUSED(x) (void)x +#define EIGEN_CHECK_RANGES(matrix, row, col) \ + assert(row >= 0 && row < (matrix).rows() && col >= 0 && col < (matrix).cols()) +#define EIGEN_CHECK_ROW_RANGE(matrix, row) \ + assert(row >= 0 && row < (matrix).rows()) +#define EIGEN_CHECK_COL_RANGE(matrix, col) \ + assert(col >= 0 && col < (matrix).cols()) + +namespace Eigen +{ + +//forward declarations +template class Matrix; +template class MatrixX; +template class Vector; +template class VectorX; +template class MatrixBase; +template class MatrixAlias; + +template struct ForwardDecl; +template struct ForwardDecl< Matrix > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixX > +{ typedef T Scalar; }; +template struct ForwardDecl< Vector > +{ typedef T Scalar; }; +template struct ForwardDecl< VectorX > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixBase > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixAlias > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixBase > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixAlias > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixBase > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixAlias > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixBase > > +{ typedef T Scalar; }; +template struct ForwardDecl< MatrixAlias > > +{ typedef T Scalar; }; + +template class MatrixRef; + +} // namespace Eigen + +#endif // EIGEN_UTIL_H diff --git a/src/internal/Vector.h b/src/internal/Vector.h new file mode 100644 index 000000000..0d1c719c5 --- /dev/null +++ b/src/internal/Vector.h @@ -0,0 +1,190 @@ +// 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. + +/** \file Matrix.h + * \brief Matrix and MatrixX class templates + */ + +#ifndef EIGEN_VECTOR_H +#define EIGEN_VECTOR_H + +#include "MatrixBase.h" + +namespace Eigen +{ + +template +class Vector: public MatrixBase > +{ + friend class MatrixBase >; + typedef class MatrixBase > Base; + + public: + typedef T Scalar; + + private: + + static bool _hasDynamicNumRows() + { return false; } + + static bool _hasDynamicNumCols() + { return false; } + + int _rows() const + { return Size; } + + int _cols() const + { return 1; } + + void _resize( int rows, int cols ) const + { + assert( rows == Size && cols == 1 ); + } + + public: + + Vector() + { + assert(Size > 0); + } + + explicit Vector(int rows, int cols = 1) + { + assert(Size > 0 && rows == Size && cols == 1); + } + + Vector(const Vector& other) : Base() + { + *this = other; + } + + void operator=(const Vector & other) + { Base::operator=(other); } + + template + void operator=(const MatrixXpr &xpr) + { + Base::operator=(xpr); + } + + template + explicit Vector(const MatrixXpr& xpr) + { + *this = xpr; + } + + int size() const { return _rows(); } + + protected: + + T m_array[Size]; + +}; + +template +class VectorX : public MatrixBase > +{ + friend class MatrixBase >; + typedef class MatrixBase > Base; + + public: + + typedef T Scalar; + + explicit VectorX(int rows, int cols = 1) + { + assert(cols == 1); + _init(rows); + } + + VectorX(const VectorX& other) : Base() + { + _init(other.size()); + *this = other; + } + + void operator=(const VectorX& other) + { + Base::operator=(other); + } + + template + void operator=(const MatrixXpr &xpr) + { + Base::operator=(xpr); + } + + template + explicit VectorX(const MatrixXpr& xpr) + { + _init(xpr.rows()); + *this = xpr; + } + + ~VectorX() + { + delete[] m_array; } + + int size() const { return _rows(); } + + protected: + + int m_size; + T *m_array; + + private: + + int _rows() const { return m_size; } + int _cols() const { return 1; } + + static bool _hasDynamicNumRows() + { return true; } + + static bool _hasDynamicNumCols() + { return false; } + + void _resize(int rows, int cols) + { + assert(rows > 0 && cols == 1); + if(rows > m_size) + { + delete[] m_array; + m_array = new T[rows]; + } + m_size = rows; + } + + void _init(int size) + { + assert(size > 0); + m_size = size; + m_array = new T[m_size]; + } + +}; + +} // namespace Eigen + +#endif // EIGEN_VECTOR_H -- cgit v1.2.3