• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
11 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
12 
13 namespace Eigen {
14 
15 template<typename UnaryOp, typename MatrixType>
16 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>
17   : public SparseMatrixBase<CwiseUnaryOp<UnaryOp, MatrixType> >
18 {
19   public:
20 
21     class InnerIterator;
22     class ReverseInnerIterator;
23 
24     typedef CwiseUnaryOp<UnaryOp, MatrixType> Derived;
25     EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
26 
27   protected:
28     typedef typename internal::traits<Derived>::_XprTypeNested _MatrixTypeNested;
29     typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
30     typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
31 };
32 
33 template<typename UnaryOp, typename MatrixType>
34 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::InnerIterator
35     : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeIterator
36 {
37     typedef typename CwiseUnaryOpImpl::Scalar Scalar;
38     typedef typename CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeIterator Base;
39   public:
40 
InnerIterator(const CwiseUnaryOpImpl & unaryOp,typename CwiseUnaryOpImpl::Index outer)41     EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
42       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
43     {}
44 
45     EIGEN_STRONG_INLINE InnerIterator& operator++()
46     { Base::operator++(); return *this; }
47 
value()48     EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
49 
50   protected:
51     const UnaryOp m_functor;
52   private:
53     typename CwiseUnaryOpImpl::Scalar& valueRef();
54 };
55 
56 template<typename UnaryOp, typename MatrixType>
57 class CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::ReverseInnerIterator
58     : public CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeReverseIterator
59 {
60     typedef typename CwiseUnaryOpImpl::Scalar Scalar;
61     typedef typename CwiseUnaryOpImpl<UnaryOp,MatrixType,Sparse>::MatrixTypeReverseIterator Base;
62   public:
63 
ReverseInnerIterator(const CwiseUnaryOpImpl & unaryOp,typename CwiseUnaryOpImpl::Index outer)64     EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryOpImpl& unaryOp, typename CwiseUnaryOpImpl::Index outer)
65       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
66     {}
67 
68     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
69     { Base::operator--(); return *this; }
70 
value()71     EIGEN_STRONG_INLINE typename CwiseUnaryOpImpl::Scalar value() const { return m_functor(Base::value()); }
72 
73   protected:
74     const UnaryOp m_functor;
75   private:
76     typename CwiseUnaryOpImpl::Scalar& valueRef();
77 };
78 
79 template<typename ViewOp, typename MatrixType>
80 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>
81   : public SparseMatrixBase<CwiseUnaryView<ViewOp, MatrixType> >
82 {
83   public:
84 
85     class InnerIterator;
86     class ReverseInnerIterator;
87 
88     typedef CwiseUnaryView<ViewOp, MatrixType> Derived;
89     EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
90 
91   protected:
92     typedef typename internal::traits<Derived>::_MatrixTypeNested _MatrixTypeNested;
93     typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
94     typedef typename _MatrixTypeNested::ReverseInnerIterator MatrixTypeReverseIterator;
95 };
96 
97 template<typename ViewOp, typename MatrixType>
98 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::InnerIterator
99     : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeIterator
100 {
101     typedef typename CwiseUnaryViewImpl::Scalar Scalar;
102     typedef typename CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeIterator Base;
103   public:
104 
InnerIterator(const CwiseUnaryViewImpl & unaryOp,typename CwiseUnaryViewImpl::Index outer)105     EIGEN_STRONG_INLINE InnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
106       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
107     {}
108 
109     EIGEN_STRONG_INLINE InnerIterator& operator++()
110     { Base::operator++(); return *this; }
111 
value()112     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
valueRef()113     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
114 
115   protected:
116     const ViewOp m_functor;
117 };
118 
119 template<typename ViewOp, typename MatrixType>
120 class CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::ReverseInnerIterator
121     : public CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeReverseIterator
122 {
123     typedef typename CwiseUnaryViewImpl::Scalar Scalar;
124     typedef typename CwiseUnaryViewImpl<ViewOp,MatrixType,Sparse>::MatrixTypeReverseIterator Base;
125   public:
126 
ReverseInnerIterator(const CwiseUnaryViewImpl & unaryOp,typename CwiseUnaryViewImpl::Index outer)127     EIGEN_STRONG_INLINE ReverseInnerIterator(const CwiseUnaryViewImpl& unaryOp, typename CwiseUnaryViewImpl::Index outer)
128       : Base(unaryOp.derived().nestedExpression(),outer), m_functor(unaryOp.derived().functor())
129     {}
130 
131     EIGEN_STRONG_INLINE ReverseInnerIterator& operator--()
132     { Base::operator--(); return *this; }
133 
value()134     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar value() const { return m_functor(Base::value()); }
valueRef()135     EIGEN_STRONG_INLINE typename CwiseUnaryViewImpl::Scalar& valueRef() { return m_functor(Base::valueRef()); }
136 
137   protected:
138     const ViewOp m_functor;
139 };
140 
141 template<typename Derived>
142 EIGEN_STRONG_INLINE Derived&
143 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
144 {
145   for (Index j=0; j<outerSize(); ++j)
146     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
147       i.valueRef() *= other;
148   return derived();
149 }
150 
151 template<typename Derived>
152 EIGEN_STRONG_INLINE Derived&
153 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
154 {
155   for (Index j=0; j<outerSize(); ++j)
156     for (typename Derived::InnerIterator i(derived(),j); i; ++i)
157       i.valueRef() /= other;
158   return derived();
159 }
160 
161 } // end namespace Eigen
162 
163 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H
164