User | Revision | Line number | New contents of line |
ykuroda |
0:13a5d365ba16
|
1
|
// This file is part of Eigen, a lightweight C++ template library
|
ykuroda |
0:13a5d365ba16
|
2
|
// for linear algebra.
|
ykuroda |
0:13a5d365ba16
|
3
|
//
|
ykuroda |
0:13a5d365ba16
|
4
|
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
|
ykuroda |
0:13a5d365ba16
|
5
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
|
ykuroda |
0:13a5d365ba16
|
6
|
//
|
ykuroda |
0:13a5d365ba16
|
7
|
// This Source Code Form is subject to the terms of the Mozilla
|
ykuroda |
0:13a5d365ba16
|
8
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
ykuroda |
0:13a5d365ba16
|
9
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
ykuroda |
0:13a5d365ba16
|
10
|
|
ykuroda |
0:13a5d365ba16
|
11
|
#ifndef EIGEN_CWISE_UNARY_OP_H
|
ykuroda |
0:13a5d365ba16
|
12
|
#define EIGEN_CWISE_UNARY_OP_H
|
ykuroda |
0:13a5d365ba16
|
13
|
|
ykuroda |
0:13a5d365ba16
|
14
|
namespace Eigen {
|
ykuroda |
0:13a5d365ba16
|
15
|
|
ykuroda |
0:13a5d365ba16
|
16
|
/** \class CwiseUnaryOp
|
ykuroda |
0:13a5d365ba16
|
17
|
* \ingroup Core_Module
|
ykuroda |
0:13a5d365ba16
|
18
|
*
|
ykuroda |
0:13a5d365ba16
|
19
|
* \brief Generic expression where a coefficient-wise unary operator is applied to an expression
|
ykuroda |
0:13a5d365ba16
|
20
|
*
|
ykuroda |
0:13a5d365ba16
|
21
|
* \param UnaryOp template functor implementing the operator
|
ykuroda |
0:13a5d365ba16
|
22
|
* \param XprType the type of the expression to which we are applying the unary operator
|
ykuroda |
0:13a5d365ba16
|
23
|
*
|
ykuroda |
0:13a5d365ba16
|
24
|
* This class represents an expression where a unary operator is applied to an expression.
|
ykuroda |
0:13a5d365ba16
|
25
|
* It is the return type of all operations taking exactly 1 input expression, regardless of the
|
ykuroda |
0:13a5d365ba16
|
26
|
* presence of other inputs such as scalars. For example, the operator* in the expression 3*matrix
|
ykuroda |
0:13a5d365ba16
|
27
|
* is considered unary, because only the right-hand side is an expression, and its
|
ykuroda |
0:13a5d365ba16
|
28
|
* return type is a specialization of CwiseUnaryOp.
|
ykuroda |
0:13a5d365ba16
|
29
|
*
|
ykuroda |
0:13a5d365ba16
|
30
|
* Most of the time, this is the only way that it is used, so you typically don't have to name
|
ykuroda |
0:13a5d365ba16
|
31
|
* CwiseUnaryOp types explicitly.
|
ykuroda |
0:13a5d365ba16
|
32
|
*
|
ykuroda |
0:13a5d365ba16
|
33
|
* \sa MatrixBase::unaryExpr(const CustomUnaryOp &) const, class CwiseBinaryOp, class CwiseNullaryOp
|
ykuroda |
0:13a5d365ba16
|
34
|
*/
|
ykuroda |
0:13a5d365ba16
|
35
|
|
ykuroda |
0:13a5d365ba16
|
36
|
namespace internal {
|
ykuroda |
0:13a5d365ba16
|
37
|
template<typename UnaryOp, typename XprType>
|
ykuroda |
0:13a5d365ba16
|
38
|
struct traits<CwiseUnaryOp<UnaryOp, XprType> >
|
ykuroda |
0:13a5d365ba16
|
39
|
: traits<XprType>
|
ykuroda |
0:13a5d365ba16
|
40
|
{
|
ykuroda |
0:13a5d365ba16
|
41
|
typedef typename result_of<
|
ykuroda |
0:13a5d365ba16
|
42
|
UnaryOp(typename XprType::Scalar)
|
ykuroda |
0:13a5d365ba16
|
43
|
>::type Scalar;
|
ykuroda |
0:13a5d365ba16
|
44
|
typedef typename XprType::Nested XprTypeNested;
|
ykuroda |
0:13a5d365ba16
|
45
|
typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
|
ykuroda |
0:13a5d365ba16
|
46
|
enum {
|
ykuroda |
0:13a5d365ba16
|
47
|
Flags = _XprTypeNested::Flags & (
|
ykuroda |
0:13a5d365ba16
|
48
|
HereditaryBits | LinearAccessBit | AlignedBit
|
ykuroda |
0:13a5d365ba16
|
49
|
| (functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)),
|
ykuroda |
0:13a5d365ba16
|
50
|
CoeffReadCost = EIGEN_ADD_COST(_XprTypeNested::CoeffReadCost, functor_traits<UnaryOp>::Cost)
|
ykuroda |
0:13a5d365ba16
|
51
|
};
|
ykuroda |
0:13a5d365ba16
|
52
|
};
|
ykuroda |
0:13a5d365ba16
|
53
|
}
|
ykuroda |
0:13a5d365ba16
|
54
|
|
ykuroda |
0:13a5d365ba16
|
55
|
template<typename UnaryOp, typename XprType, typename StorageKind>
|
ykuroda |
0:13a5d365ba16
|
56
|
class CwiseUnaryOpImpl;
|
ykuroda |
0:13a5d365ba16
|
57
|
|
ykuroda |
0:13a5d365ba16
|
58
|
template<typename UnaryOp, typename XprType>
|
ykuroda |
0:13a5d365ba16
|
59
|
class CwiseUnaryOp : internal::no_assignment_operator,
|
ykuroda |
0:13a5d365ba16
|
60
|
public CwiseUnaryOpImpl<UnaryOp, XprType, typename internal::traits<XprType>::StorageKind>
|
ykuroda |
0:13a5d365ba16
|
61
|
{
|
ykuroda |
0:13a5d365ba16
|
62
|
public:
|
ykuroda |
0:13a5d365ba16
|
63
|
|
ykuroda |
0:13a5d365ba16
|
64
|
typedef typename CwiseUnaryOpImpl<UnaryOp, XprType,typename internal::traits<XprType>::StorageKind>::Base Base;
|
ykuroda |
0:13a5d365ba16
|
65
|
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryOp)
|
ykuroda |
0:13a5d365ba16
|
66
|
|
ykuroda |
0:13a5d365ba16
|
67
|
inline CwiseUnaryOp(const XprType& xpr, const UnaryOp& func = UnaryOp())
|
ykuroda |
0:13a5d365ba16
|
68
|
: m_xpr(xpr), m_functor(func) {}
|
ykuroda |
0:13a5d365ba16
|
69
|
|
ykuroda |
0:13a5d365ba16
|
70
|
EIGEN_STRONG_INLINE Index rows() const { return m_xpr.rows(); }
|
ykuroda |
0:13a5d365ba16
|
71
|
EIGEN_STRONG_INLINE Index cols() const { return m_xpr.cols(); }
|
ykuroda |
0:13a5d365ba16
|
72
|
|
ykuroda |
0:13a5d365ba16
|
73
|
/** \returns the functor representing the unary operation */
|
ykuroda |
0:13a5d365ba16
|
74
|
const UnaryOp& functor() const { return m_functor; }
|
ykuroda |
0:13a5d365ba16
|
75
|
|
ykuroda |
0:13a5d365ba16
|
76
|
/** \returns the nested expression */
|
ykuroda |
0:13a5d365ba16
|
77
|
const typename internal::remove_all<typename XprType::Nested>::type&
|
ykuroda |
0:13a5d365ba16
|
78
|
nestedExpression() const { return m_xpr; }
|
ykuroda |
0:13a5d365ba16
|
79
|
|
ykuroda |
0:13a5d365ba16
|
80
|
/** \returns the nested expression */
|
ykuroda |
0:13a5d365ba16
|
81
|
typename internal::remove_all<typename XprType::Nested>::type&
|
ykuroda |
0:13a5d365ba16
|
82
|
nestedExpression() { return m_xpr.const_cast_derived(); }
|
ykuroda |
0:13a5d365ba16
|
83
|
|
ykuroda |
0:13a5d365ba16
|
84
|
protected:
|
ykuroda |
0:13a5d365ba16
|
85
|
typename XprType::Nested m_xpr;
|
ykuroda |
0:13a5d365ba16
|
86
|
const UnaryOp m_functor;
|
ykuroda |
0:13a5d365ba16
|
87
|
};
|
ykuroda |
0:13a5d365ba16
|
88
|
|
ykuroda |
0:13a5d365ba16
|
89
|
// This is the generic implementation for dense storage.
|
ykuroda |
0:13a5d365ba16
|
90
|
// It can be used for any expression types implementing the dense concept.
|
ykuroda |
0:13a5d365ba16
|
91
|
template<typename UnaryOp, typename XprType>
|
ykuroda |
0:13a5d365ba16
|
92
|
class CwiseUnaryOpImpl<UnaryOp,XprType,Dense>
|
ykuroda |
0:13a5d365ba16
|
93
|
: public internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type
|
ykuroda |
0:13a5d365ba16
|
94
|
{
|
ykuroda |
0:13a5d365ba16
|
95
|
public:
|
ykuroda |
0:13a5d365ba16
|
96
|
|
ykuroda |
0:13a5d365ba16
|
97
|
typedef CwiseUnaryOp<UnaryOp, XprType> Derived;
|
ykuroda |
0:13a5d365ba16
|
98
|
typedef typename internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type Base;
|
ykuroda |
0:13a5d365ba16
|
99
|
EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
|
ykuroda |
0:13a5d365ba16
|
100
|
|
ykuroda |
0:13a5d365ba16
|
101
|
EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
|
ykuroda |
0:13a5d365ba16
|
102
|
{
|
ykuroda |
0:13a5d365ba16
|
103
|
return derived().functor()(derived().nestedExpression().coeff(rowId, colId));
|
ykuroda |
0:13a5d365ba16
|
104
|
}
|
ykuroda |
0:13a5d365ba16
|
105
|
|
ykuroda |
0:13a5d365ba16
|
106
|
template<int LoadMode>
|
ykuroda |
0:13a5d365ba16
|
107
|
EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
|
ykuroda |
0:13a5d365ba16
|
108
|
{
|
ykuroda |
0:13a5d365ba16
|
109
|
return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(rowId, colId));
|
ykuroda |
0:13a5d365ba16
|
110
|
}
|
ykuroda |
0:13a5d365ba16
|
111
|
|
ykuroda |
0:13a5d365ba16
|
112
|
EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
|
ykuroda |
0:13a5d365ba16
|
113
|
{
|
ykuroda |
0:13a5d365ba16
|
114
|
return derived().functor()(derived().nestedExpression().coeff(index));
|
ykuroda |
0:13a5d365ba16
|
115
|
}
|
ykuroda |
0:13a5d365ba16
|
116
|
|
ykuroda |
0:13a5d365ba16
|
117
|
template<int LoadMode>
|
ykuroda |
0:13a5d365ba16
|
118
|
EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
|
ykuroda |
0:13a5d365ba16
|
119
|
{
|
ykuroda |
0:13a5d365ba16
|
120
|
return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(index));
|
ykuroda |
0:13a5d365ba16
|
121
|
}
|
ykuroda |
0:13a5d365ba16
|
122
|
};
|
ykuroda |
0:13a5d365ba16
|
123
|
|
ykuroda |
0:13a5d365ba16
|
124
|
} // end namespace Eigen
|
ykuroda |
0:13a5d365ba16
|
125
|
|
ykuroda |
0:13a5d365ba16
|
126
|
#endif // EIGEN_CWISE_UNARY_OP_H |