00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_SPARSE_CWISE_UNARY_OP_H
00026 #define EIGEN_SPARSE_CWISE_UNARY_OP_H
00027
00028 template<typename UnaryOp, typename MatrixType>
00029 struct ei_traits<SparseCwiseUnaryOp<UnaryOp, MatrixType> > : ei_traits<MatrixType>
00030 {
00031 typedef typename ei_result_of<
00032 UnaryOp(typename MatrixType::Scalar)
00033 >::type Scalar;
00034 typedef typename MatrixType::Nested MatrixTypeNested;
00035 typedef typename ei_unref<MatrixTypeNested>::type _MatrixTypeNested;
00036 enum {
00037 CoeffReadCost = _MatrixTypeNested::CoeffReadCost + ei_functor_traits<UnaryOp>::Cost
00038 };
00039 };
00040
00041 template<typename UnaryOp, typename MatrixType>
00042 class SparseCwiseUnaryOp : ei_no_assignment_operator,
00043 public SparseMatrixBase<SparseCwiseUnaryOp<UnaryOp, MatrixType> >
00044 {
00045 public:
00046
00047 class InnerIterator;
00048
00049
00050 EIGEN_SPARSE_GENERIC_PUBLIC_INTERFACE(SparseCwiseUnaryOp)
00051
00052 inline SparseCwiseUnaryOp(const MatrixType& mat, const UnaryOp& func = UnaryOp())
00053 : m_matrix(mat), m_functor(func) {}
00054
00055 EIGEN_STRONG_INLINE int rows() const { return m_matrix.rows(); }
00056 EIGEN_STRONG_INLINE int cols() const { return m_matrix.cols(); }
00057
00058
00059
00060
00061 protected:
00062 const typename MatrixType::Nested m_matrix;
00063 const UnaryOp m_functor;
00064 };
00065
00066
00067 template<typename UnaryOp, typename MatrixType>
00068 class SparseCwiseUnaryOp<UnaryOp,MatrixType>::InnerIterator
00069 {
00070 typedef typename SparseCwiseUnaryOp::Scalar Scalar;
00071 typedef typename ei_traits<SparseCwiseUnaryOp>::_MatrixTypeNested _MatrixTypeNested;
00072 typedef typename _MatrixTypeNested::InnerIterator MatrixTypeIterator;
00073 public:
00074
00075 EIGEN_STRONG_INLINE InnerIterator(const SparseCwiseUnaryOp& unaryOp, int outer)
00076 : m_iter(unaryOp.m_matrix,outer), m_functor(unaryOp.m_functor)
00077 {}
00078
00079 EIGEN_STRONG_INLINE InnerIterator& operator++()
00080 { ++m_iter; return *this; }
00081
00082 EIGEN_STRONG_INLINE Scalar value() const { return m_functor(m_iter.value()); }
00083
00084 EIGEN_STRONG_INLINE int index() const { return m_iter.index(); }
00085 EIGEN_STRONG_INLINE int row() const { return m_iter.row(); }
00086 EIGEN_STRONG_INLINE int col() const { return m_iter.col(); }
00087
00088 EIGEN_STRONG_INLINE operator bool() const { return m_iter; }
00089
00090 protected:
00091 MatrixTypeIterator m_iter;
00092 const UnaryOp m_functor;
00093
00094 private:
00095 InnerIterator& operator=(const InnerIterator&);
00096 };
00097
00098 template<typename Derived>
00099 template<typename CustomUnaryOp>
00100 EIGEN_STRONG_INLINE const SparseCwiseUnaryOp<CustomUnaryOp, Derived>
00101 SparseMatrixBase<Derived>::unaryExpr(const CustomUnaryOp& func) const
00102 {
00103 return SparseCwiseUnaryOp<CustomUnaryOp, Derived>(derived(), func);
00104 }
00105
00106 template<typename Derived>
00107 EIGEN_STRONG_INLINE const SparseCwiseUnaryOp<ei_scalar_opposite_op<typename ei_traits<Derived>::Scalar>,Derived>
00108 SparseMatrixBase<Derived>::operator-() const
00109 {
00110 return derived();
00111 }
00112
00113 template<typename ExpressionType>
00114 EIGEN_STRONG_INLINE const EIGEN_SPARSE_CWISE_UNOP_RETURN_TYPE(ei_scalar_abs_op)
00115 SparseCwise<ExpressionType>::abs() const
00116 {
00117 return _expression();
00118 }
00119
00120 template<typename ExpressionType>
00121 EIGEN_STRONG_INLINE const EIGEN_SPARSE_CWISE_UNOP_RETURN_TYPE(ei_scalar_abs2_op)
00122 SparseCwise<ExpressionType>::abs2() const
00123 {
00124 return _expression();
00125 }
00126
00127 template<typename Derived>
00128 EIGEN_STRONG_INLINE typename SparseMatrixBase<Derived>::ConjugateReturnType
00129 SparseMatrixBase<Derived>::conjugate() const
00130 {
00131 return ConjugateReturnType(derived());
00132 }
00133
00134 template<typename Derived>
00135 EIGEN_STRONG_INLINE const typename SparseMatrixBase<Derived>::RealReturnType
00136 SparseMatrixBase<Derived>::real() const { return derived(); }
00137
00138 template<typename Derived>
00139 EIGEN_STRONG_INLINE const typename SparseMatrixBase<Derived>::ImagReturnType
00140 SparseMatrixBase<Derived>::imag() const { return derived(); }
00141
00142 template<typename Derived>
00143 template<typename NewType>
00144 EIGEN_STRONG_INLINE const SparseCwiseUnaryOp<ei_scalar_cast_op<typename ei_traits<Derived>::Scalar, NewType>, Derived>
00145 SparseMatrixBase<Derived>::cast() const
00146 {
00147 return derived();
00148 }
00149
00150 template<typename Derived>
00151 EIGEN_STRONG_INLINE const SparseCwiseUnaryOp<ei_scalar_multiple_op<typename ei_traits<Derived>::Scalar>, Derived>
00152 SparseMatrixBase<Derived>::operator*(const Scalar& scalar) const
00153 {
00154 return SparseCwiseUnaryOp<ei_scalar_multiple_op<Scalar>, Derived>
00155 (derived(), ei_scalar_multiple_op<Scalar>(scalar));
00156 }
00157
00158 template<typename Derived>
00159 EIGEN_STRONG_INLINE const SparseCwiseUnaryOp<ei_scalar_quotient1_op<typename ei_traits<Derived>::Scalar>, Derived>
00160 SparseMatrixBase<Derived>::operator/(const Scalar& scalar) const
00161 {
00162 return SparseCwiseUnaryOp<ei_scalar_quotient1_op<Scalar>, Derived>
00163 (derived(), ei_scalar_quotient1_op<Scalar>(scalar));
00164 }
00165
00166 template<typename Derived>
00167 EIGEN_STRONG_INLINE Derived&
00168 SparseMatrixBase<Derived>::operator*=(const Scalar& other)
00169 {
00170 for (int j=0; j<outerSize(); ++j)
00171 for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00172 i.valueRef() *= other;
00173 return derived();
00174 }
00175
00176 template<typename Derived>
00177 EIGEN_STRONG_INLINE Derived&
00178 SparseMatrixBase<Derived>::operator/=(const Scalar& other)
00179 {
00180 for (int j=0; j<outerSize(); ++j)
00181 for (typename Derived::InnerIterator i(derived(),j); i; ++i)
00182 i.valueRef() /= other;
00183 return derived();
00184 }
00185
00186 #endif // EIGEN_SPARSE_CWISE_UNARY_OP_H