SparseCwiseUnaryOp.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.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_SPARSE_CWISE_UNARY_OP_H
26 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
27 
28 namespace Eigen {
29 
30 template<typename UnaryOp, typename MatrixType>
31 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>
32  : public SparseMatrixBase<CwiseUnaryOp<UnaryOp, MatrixType> >
33 {
34  public:
35 
36  class InnerIterator;
37  class ReverseInnerIterator;
38 
41 
42  protected:
43  typedef typename internal::traits<Derived>::_XprTypeNested _MatrixTypeNested;
44  typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
45  typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
46 };
47 
48 template<typename UnaryOp, typename MatrixType>
49 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::InnerIterator
50  : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeIterator
51 {
52  typedef typename CwiseUnaryOpImpl::Scalar Scalar;
54  public:
55 
56  EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
57  : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
58  {}
59 
60  EIGEN_STRONG_INLINE InnerIterator& operator++()
61  { Base::operator++(); return *this; }
62 
63  EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
64 
65  protected:
66  const UnaryOp m_functor;
67  private:
68  typename CwiseUnaryOpImpl::Scalar& valueRef();
69 };
70 
71 template<typename UnaryOp, typename MatrixType>
72 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::ReverseInnerIterator
73  : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeReverseIterator
74 {
75  typedef typename CwiseUnaryOpImpl::Scalar Scalar;
77  public:
78 
79  EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
80  : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
81  {}
82 
83  EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
84  { Base::operator--(); return *this; }
85 
86  EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
87 
88  protected:
89  const UnaryOp m_functor;
90  private:
91  typename CwiseUnaryOpImpl::Scalar& valueRef();
92 };
93 
94 template<typename ViewOp, typename MatrixType>
95 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>
96  : public SparseMatrixBase<CwiseUnaryView<ViewOp, MatrixType> >
97 {
98  public:
99 
100  class InnerIterator;
101  class ReverseInnerIterator;
102 
105 
106  protected:
107  typedef typename internal::traits<Derived>::_MatrixTypeNested _MatrixTypeNested;
108  typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
109  typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
110 };
111 
112 template<typename ViewOp, typename MatrixType>
113 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::InnerIterator
114  : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeIterator
115 {
116  typedef typename CwiseUnaryViewImpl::Scalar Scalar;
118  public:
119 
120  EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
121  : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
122  {}
123 
124  EIGEN_STRONG_INLINE InnerIterator& operator++()
125  { Base::operator++(); return *this; }
126 
127  EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
128  EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
129 
130  protected:
131  const ViewOp m_functor;
132 };
133 
134 template<typename ViewOp, typename MatrixType>
135 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::ReverseInnerIterator
136  : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeReverseIterator
137 {
138  typedef typename CwiseUnaryViewImpl::Scalar Scalar;
140  public:
141 
142  EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
143  : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
144  {}
145 
146  EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
147  { Base::operator--(); return *this; }
148 
149  EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
150  EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
151 
152  protected:
153  const ViewOp m_functor;
154 };
155 
156 template<typename Derived>
157 EIGEN_STRONG_INLINE Derived&
159 {
160  for (Index j=0; j<outerSize(); ++j)
161  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
162  i.valueRef() *= other;
163  return derived();
164 }
165 
166 template<typename Derived>
167 EIGEN_STRONG_INLINE Derived&
169 {
170  for (Index j=0; j<outerSize(); ++j)
171  for (typename Derived::InnerIterator i(derived(),j); i; ++i)
172  i.valueRef() /= other;
173  return derived();
174 }
175 
176 } // end namespace Eigen
177 
178 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H