SkylineMatrixBase.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Guillaume Saupin <guillaume.saupin@cea.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_SKYLINEMATRIXBASE_H
26 #define EIGEN_SKYLINEMATRIXBASE_H
27 
28 #include "SkylineUtil.h"
29 
30 namespace Eigen {
31 
41 template<typename Derived> class SkylineMatrixBase : public EigenBase<Derived> {
42 public:
43 
44  typedef typename internal::traits<Derived>::Scalar Scalar;
45  typedef typename internal::traits<Derived>::StorageKind StorageKind;
46  typedef typename internal::index<StorageKind>::type Index;
47 
48  enum {
49  RowsAtCompileTime = internal::traits<Derived>::RowsAtCompileTime,
55  ColsAtCompileTime = internal::traits<Derived>::ColsAtCompileTime,
62  SizeAtCompileTime = (internal::size_at_compile_time<internal::traits<Derived>::RowsAtCompileTime,
63  internal::traits<Derived>::ColsAtCompileTime>::ret),
68  MaxRowsAtCompileTime = RowsAtCompileTime,
69  MaxColsAtCompileTime = ColsAtCompileTime,
70 
71  MaxSizeAtCompileTime = (internal::size_at_compile_time<MaxRowsAtCompileTime,
72  MaxColsAtCompileTime>::ret),
73 
80  Flags = internal::traits<Derived>::Flags,
85  CoeffReadCost = internal::traits<Derived>::CoeffReadCost,
90  IsRowMajor = Flags & RowMajorBit ? 1 : 0
91  };
92 
93 #ifndef EIGEN_PARSED_BY_DOXYGEN
94 
100  typedef typename NumTraits<Scalar>::Real RealScalar;
101 
103  typedef Matrix<Scalar, EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime),
104  EIGEN_SIZE_MAX(RowsAtCompileTime, ColsAtCompileTime) > SquareMatrixType;
105 
106  inline const Derived& derived() const {
107  return *static_cast<const Derived*> (this);
108  }
109 
110  inline Derived& derived() {
111  return *static_cast<Derived*> (this);
112  }
113 
114  inline Derived& const_cast_derived() const {
115  return *static_cast<Derived*> (const_cast<SkylineMatrixBase*> (this));
116  }
117 #endif // not EIGEN_PARSED_BY_DOXYGEN
118 
120  inline Index rows() const {
121  return derived().rows();
122  }
123 
125  inline Index cols() const {
126  return derived().cols();
127  }
128 
131  inline Index size() const {
132  return rows() * cols();
133  }
134 
137  inline Index nonZeros() const {
138  return derived().nonZeros();
139  }
140 
143  Index outerSize() const {
144  return (int(Flags) & RowMajorBit) ? this->rows() : this->cols();
145  }
146 
149  Index innerSize() const {
150  return (int(Flags) & RowMajorBit) ? this->cols() : this->rows();
151  }
152 
153  bool isRValue() const {
154  return m_isRValue;
155  }
156 
157  Derived& markAsRValue() {
158  m_isRValue = true;
159  return derived();
160  }
161 
162  SkylineMatrixBase() : m_isRValue(false) {
163  /* TODO check flags */
164  }
165 
166  inline Derived & operator=(const Derived& other) {
167  this->operator=<Derived > (other);
168  return derived();
169  }
170 
171  template<typename OtherDerived>
172  inline void assignGeneric(const OtherDerived& other) {
173  derived().resize(other.rows(), other.cols());
174  for (Index row = 0; row < rows(); row++)
175  for (Index col = 0; col < cols(); col++) {
176  if (other.coeff(row, col) != Scalar(0))
177  derived().insert(row, col) = other.coeff(row, col);
178  }
179  derived().finalize();
180  }
181 
182  template<typename OtherDerived>
183  inline Derived & operator=(const SkylineMatrixBase<OtherDerived>& other) {
184  //TODO
185  }
186 
187  template<typename Lhs, typename Rhs>
188  inline Derived & operator=(const SkylineProduct<Lhs, Rhs, SkylineTimeSkylineProduct>& product);
189 
190  friend std::ostream & operator <<(std::ostream & s, const SkylineMatrixBase& m) {
191  s << m.derived();
192  return s;
193  }
194 
195  template<typename OtherDerived>
196  const typename SkylineProductReturnType<Derived, OtherDerived>::Type
197  operator*(const MatrixBase<OtherDerived> &other) const;
198 
200  template<typename DenseDerived>
201  void evalTo(MatrixBase<DenseDerived>& dst) const {
202  dst.setZero();
203  for (Index i = 0; i < rows(); i++)
204  for (Index j = 0; j < rows(); j++)
205  dst(i, j) = derived().coeff(i, j);
206  }
207 
208  Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime> toDense() const {
209  return derived();
210  }
211 
217  EIGEN_STRONG_INLINE const typename internal::eval<Derived, IsSkyline>::type eval() const {
218  return typename internal::eval<Derived>::type(derived());
219  }
220 
221 protected:
222  bool m_isRValue;
223 };
224 
225 } // end namespace Eigen
226 
227 #endif // EIGEN_SkylineMatrixBase_H