aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2009-07-14 23:27:37 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2009-07-14 23:27:37 +0200
commit587029a612df2a98a545702e90dc40feb7a62d3e (patch)
tree742dd8419c31d6bb665dd7a7efd95df5ddbf98bf
parent8120a5cecd982ed182d6e18b072fb9a95dc925dc (diff)
started an implementation of BandMatrix: at least the read/write access
to the main/sub/super diagonals seems to work well.
-rw-r--r--Eigen/Core1
-rw-r--r--Eigen/src/Core/BandMatrix.h154
-rw-r--r--Eigen/src/Core/util/ForwardDeclarations.h2
-rw-r--r--test/CMakeLists.txt1
-rw-r--r--test/bandmatrix.cpp61
5 files changed, 219 insertions, 0 deletions
diff --git a/Eigen/Core b/Eigen/Core
index 2bec68f9c..ee9bfe325 100644
--- a/Eigen/Core
+++ b/Eigen/Core
@@ -185,6 +185,7 @@ namespace Eigen {
#include "src/Core/SolveTriangular.h"
#include "src/Core/products/SelfadjointRank2Update.h"
#include "src/Core/products/TriangularMatrixVector.h"
+#include "src/Core/BandMatrix.h"
} // namespace Eigen
diff --git a/Eigen/src/Core/BandMatrix.h b/Eigen/src/Core/BandMatrix.h
new file mode 100644
index 000000000..03d17dac2
--- /dev/null
+++ b/Eigen/src/Core/BandMatrix.h
@@ -0,0 +1,154 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2009 Gael Guennebaud <g.gael@free.fr>
+//
+// Eigen is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 3 of the License, or (at your option) any later version.
+//
+// Alternatively, 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 of
+// the License, 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 Lesser General Public License or the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License and a copy of the GNU General Public License along with
+// Eigen. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef EIGEN_BANDMATRIX_H
+#define EIGEN_BANDMATRIX_H
+
+/** \nonstableyet
+ * \class BandMatrix
+ *
+ * \brief
+ *
+ * \param
+ *
+ * \sa
+ */
+template<typename _Scalar, int Size, int Supers, int Subs, int Options>
+struct ei_traits<BandMatrix<_Scalar,Size,Supers,Subs,Options> >
+{
+ typedef _Scalar Scalar;
+ enum {
+ CoeffReadCost = NumTraits<Scalar>::ReadCost,
+ RowsAtCompileTime = Size,
+ ColsAtCompileTime = Size,
+ MaxRowsAtCompileTime = Size,
+ MaxColsAtCompileTime = Size,
+ Flags = 0
+ };
+};
+
+template<typename _Scalar, int Size, int Supers, int Subs, int Options>
+class BandMatrix : public MultiplierBase<BandMatrix<_Scalar,Supers,Subs,Options> >
+{
+ public:
+
+ enum {
+ Flags = ei_traits<BandMatrix>::Flags,
+ CoeffReadCost = ei_traits<BandMatrix>::CoeffReadCost,
+ RowsAtCompileTime = ei_traits<BandMatrix>::RowsAtCompileTime,
+ ColsAtCompileTime = ei_traits<BandMatrix>::ColsAtCompileTime,
+ MaxRowsAtCompileTime = ei_traits<BandMatrix>::MaxRowsAtCompileTime,
+ MaxColsAtCompileTime = ei_traits<BandMatrix>::MaxColsAtCompileTime
+ };
+ typedef typename ei_traits<BandMatrix>::Scalar Scalar;
+ typedef Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime> PlainMatrixType;
+
+ protected:
+ enum {
+ DataSizeAtCompileTime = ((Size!=Dynamic) && (Supers!=Dynamic) && (Subs!=Dynamic))
+ ? Size*(Supers+Subs+1) - (Supers*Supers+Subs*Subs)/2
+ : Dynamic
+ };
+ typedef Matrix<Scalar,DataSizeAtCompileTime,1> DataType;
+
+ public:
+
+// inline BandMatrix() { }
+
+ inline BandMatrix(int size=Size, int supers=Supers, int subs=Subs)
+ : m_data(size*(supers+subs+1) - (supers*supers+subs*subs)/2),
+ m_size(size), m_supers(supers), m_subs(subs)
+ { }
+
+ inline int rows() const { return m_size.value(); }
+ inline int cols() const { return m_size.value(); }
+
+ inline int supers() const { return m_supers.value(); }
+ inline int subs() const { return m_subs.value(); }
+
+ inline VectorBlock<DataType,Size> diagonal()
+ { return VectorBlock<DataType,Size>(m_data,0,m_size.value()); }
+
+ inline const VectorBlock<DataType,Size> diagonal() const
+ { return VectorBlock<DataType,Size>(m_data,0,m_size.value()); }
+
+ template<int Index>
+ VectorBlock<DataType,Size==Dynamic?Dynamic:Size-(Index<0?-Index:Index)>
+ diagonal()
+ {
+ return VectorBlock<DataType,Size==Dynamic?Dynamic:Size-(Index<0?-Index:Index)>
+ (m_data,Index<0 ? subDiagIndex(-Index) : superDiagIndex(Index), m_size.value()-ei_abs(Index));
+ }
+
+ template<int Index>
+ const VectorBlock<DataType,Size==Dynamic?Dynamic:Size-(Index<0?-Index:Index)>
+ diagonal() const
+ {
+ return VectorBlock<DataType,Size==Dynamic?Dynamic:Size-(Index<0?-Index:Index)>
+ (m_data,Index<0 ? subDiagIndex(-Index) : superDiagIndex(Index), m_size.value()-ei_abs(Index));
+ }
+
+ inline VectorBlock<DataType,Dynamic> diagonal(int index)
+ {
+ ei_assert((index<0 && -index<=subs()) || (index>=0 && index<=supers()));
+ return VectorBlock<DataType,Dynamic>(m_data,
+ index<0 ? subDiagIndex(-index) : superDiagIndex(index), m_size.value()-ei_abs(index));
+ }
+ const VectorBlock<DataType,Dynamic> diagonal(int index) const
+ {
+ ei_assert((index<0 && -index<=subs()) || (index>=0 && index<=supers()));
+ return VectorBlock<DataType,Dynamic>(m_data,
+ index<0 ? subDiagIndex(-index) : superDiagIndex(index), m_size.value()-ei_abs(index));
+ }
+
+// inline VectorBlock<DataType,Size> subDiagonal()
+// { return VectorBlock<DataType,Size>(m_data,0,m_size.value()); }
+
+ PlainMatrixType toDense() const
+ {
+ PlainMatrixType res(rows(),cols());
+ res.setZero();
+ res.diagonal() = diagonal();
+ for (int i=1; i<=supers();++i)
+ res.diagonal(i) = diagonal(i);
+ for (int i=1; i<=subs();++i)
+ res.diagonal(-i) = diagonal(-i);
+ return res;
+ }
+
+ protected:
+
+ inline int subDiagIndex(int i) const
+ { return m_size.value()*(m_supers.value()+i)-(ei_abs2(i-1) + ei_abs2(m_supers.value()))/2; }
+
+ inline int superDiagIndex(int i) const
+ { return m_size.value()*i-ei_abs2(i-1)/2; }
+
+ DataType m_data;
+ ei_int_if_dynamic<Size> m_size;
+ ei_int_if_dynamic<Supers> m_supers;
+ ei_int_if_dynamic<Subs> m_subs;
+};
+
+#endif // EIGEN_BANDMATRIX_H
diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h
index 363972b60..9433a04cb 100644
--- a/Eigen/src/Core/util/ForwardDeclarations.h
+++ b/Eigen/src/Core/util/ForwardDeclarations.h
@@ -66,6 +66,8 @@ template<typename ExpressionType> class WithFormat;
template<typename MatrixType> struct CommaInitializer;
template<typename Functor, typename EvalType> class ReturnByValue;
+template<typename _Scalar, int Size=Dynamic, int Supers=Dynamic, int Subs=Dynamic, int Options=0> class BandMatrix;
+
template<typename Lhs, typename Rhs> struct ei_product_mode;
template<typename Lhs, typename Rhs, int ProductMode = ei_product_mode<Lhs,Rhs>::value> struct ProductReturnType;
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 886731d45..5e1adcbf4 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -111,6 +111,7 @@ ei_add_test(array_replicate)
ei_add_test(array_reverse)
ei_add_test(triangular)
ei_add_test(product_triangular)
+ei_add_test(bandmatrix)
ei_add_test(cholesky " " "${GSL_LIBRARIES}")
ei_add_test(lu ${EI_OFLAG})
ei_add_test(determinant)
diff --git a/test/bandmatrix.cpp b/test/bandmatrix.cpp
new file mode 100644
index 000000000..96ccda2cf
--- /dev/null
+++ b/test/bandmatrix.cpp
@@ -0,0 +1,61 @@
+// This file is triangularView of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@gmail.com>
+//
+// Eigen is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 3 of the License, or (at your option) any later version.
+//
+// Alternatively, 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 of
+// the License, 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 Lesser General Public License or the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License and a copy of the GNU General Public License along with
+// Eigen. If not, see <http://www.gnu.org/licenses/>.
+
+#include "main.h"
+
+template<typename MatrixType> void bandmatrix(MatrixType& m)
+{
+ typedef typename MatrixType::Scalar Scalar;
+ typedef typename NumTraits<Scalar>::Real RealScalar;
+ typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrixType;
+
+ int size = m.rows();
+
+ DenseMatrixType dm1(size,size);
+ dm1.setZero();
+
+ m.diagonal().setConstant(123);
+ dm1.diagonal().setConstant(123);
+ for (int i=1; i<=m.supers();++i)
+ {
+ m.diagonal(i).setConstant(i);
+ dm1.diagonal(i).setConstant(i);
+ }
+ for (int i=1; i<=m.subs();++i)
+ {
+ m.diagonal(-i).setConstant(-i);
+ dm1.diagonal(-i).setConstant(-i);
+ }
+ std::cerr << m.toDense() << "\n\n" << dm1 << "\n\n";
+ VERIFY_IS_APPROX(dm1,m.toDense());
+
+}
+
+void test_bandmatrix()
+{
+ for(int i = 0; i < g_repeat ; i++) {
+ BandMatrix<float,Dynamic,Dynamic,Dynamic> m(6,3,2);
+ CALL_SUBTEST( bandmatrix(m) );
+ }
+}