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