1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra. Eigen itself is part of the KDE project.
4
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
26
#ifndef EIGEN_CWISE_BINARY_OP_H
27
#define EIGEN_CWISE_BINARY_OP_H
29
/** \class CwiseBinaryOp
31
* \brief Generic expression of a coefficient-wise operator between two matrices or vectors
33
* \param BinaryOp template functor implementing the operator
34
* \param Lhs the type of the left-hand side
35
* \param Rhs the type of the right-hand side
37
* This class represents an expression of a generic binary operator of two matrices or vectors.
38
* It is the return type of the operator+, operator-, and the Cwise methods, and most
39
* of the time this is the only way it is used.
41
* However, if you want to write a function returning such an expression, you
42
* will need to use this class.
44
* \sa MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const, class CwiseUnaryOp, class CwiseNullaryOp
46
template<typename BinaryOp, typename Lhs, typename Rhs>
47
struct ei_traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
49
// even though we require Lhs and Rhs to have the same scalar type (see CwiseBinaryOp constructor),
50
// we still want to handle the case when the result type is different.
51
typedef typename ei_result_of<
57
typedef typename Lhs::Nested LhsNested;
58
typedef typename Rhs::Nested RhsNested;
59
typedef typename ei_unref<LhsNested>::type _LhsNested;
60
typedef typename ei_unref<RhsNested>::type _RhsNested;
62
LhsCoeffReadCost = _LhsNested::CoeffReadCost,
63
RhsCoeffReadCost = _RhsNested::CoeffReadCost,
64
LhsFlags = _LhsNested::Flags,
65
RhsFlags = _RhsNested::Flags,
66
RowsAtCompileTime = Lhs::RowsAtCompileTime,
67
ColsAtCompileTime = Lhs::ColsAtCompileTime,
68
MaxRowsAtCompileTime = Lhs::MaxRowsAtCompileTime,
69
MaxColsAtCompileTime = Lhs::MaxColsAtCompileTime,
70
Flags = (int(LhsFlags) | int(RhsFlags)) & (
72
| (int(LhsFlags) & int(RhsFlags) & (LinearAccessBit | AlignedBit))
73
| (ei_functor_traits<BinaryOp>::PacketAccess && ((int(LhsFlags) & RowMajorBit)==(int(RhsFlags) & RowMajorBit))
74
? (int(LhsFlags) & int(RhsFlags) & PacketAccessBit) : 0)),
75
CoeffReadCost = LhsCoeffReadCost + RhsCoeffReadCost + ei_functor_traits<BinaryOp>::Cost
79
template<typename BinaryOp, typename Lhs, typename Rhs>
80
class CwiseBinaryOp : ei_no_assignment_operator,
81
public MatrixBase<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
85
EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
86
typedef typename ei_traits<CwiseBinaryOp>::LhsNested LhsNested;
87
typedef typename ei_traits<CwiseBinaryOp>::RhsNested RhsNested;
89
EIGEN_STRONG_INLINE CwiseBinaryOp(const Lhs& lhs, const Rhs& rhs, const BinaryOp& func = BinaryOp())
90
: m_lhs(lhs), m_rhs(rhs), m_functor(func)
92
// we require Lhs and Rhs to have the same scalar type. Currently there is no example of a binary functor
93
// that would take two operands of different types. If there were such an example, then this check should be
94
// moved to the BinaryOp functors, on a per-case basis. This would however require a change in the BinaryOp functors, as
95
// currently they take only one typename Scalar template parameter.
96
// It is tempting to always allow mixing different types but remember that this is often impossible in the vectorized paths.
97
// So allowing mixing different types gives very unexpected errors when enabling vectorization, when the user tries to
98
// add together a float matrix and a double matrix.
99
EIGEN_STATIC_ASSERT((ei_functor_allows_mixing_real_and_complex<BinaryOp>::ret
100
? int(ei_is_same_type<typename Lhs::RealScalar, typename Rhs::RealScalar>::ret)
101
: int(ei_is_same_type<typename Lhs::Scalar, typename Rhs::Scalar>::ret)),
102
YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
103
// require the sizes to match
104
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Lhs, Rhs)
105
ei_assert(lhs.rows() == rhs.rows() && lhs.cols() == rhs.cols());
108
EIGEN_STRONG_INLINE int rows() const { return m_lhs.rows(); }
109
EIGEN_STRONG_INLINE int cols() const { return m_lhs.cols(); }
111
EIGEN_STRONG_INLINE const Scalar coeff(int row, int col) const
113
return m_functor(m_lhs.coeff(row, col), m_rhs.coeff(row, col));
116
template<int LoadMode>
117
EIGEN_STRONG_INLINE PacketScalar packet(int row, int col) const
119
return m_functor.packetOp(m_lhs.template packet<LoadMode>(row, col), m_rhs.template packet<LoadMode>(row, col));
122
EIGEN_STRONG_INLINE const Scalar coeff(int index) const
124
return m_functor(m_lhs.coeff(index), m_rhs.coeff(index));
127
template<int LoadMode>
128
EIGEN_STRONG_INLINE PacketScalar packet(int index) const
130
return m_functor.packetOp(m_lhs.template packet<LoadMode>(index), m_rhs.template packet<LoadMode>(index));
134
const LhsNested m_lhs;
135
const RhsNested m_rhs;
136
const BinaryOp m_functor;
139
/**\returns an expression of the difference of \c *this and \a other
141
* \note If you want to substract a given scalar from all coefficients, see Cwise::operator-().
143
* \sa class CwiseBinaryOp, MatrixBase::operator-=(), Cwise::operator-()
145
template<typename Derived>
146
template<typename OtherDerived>
147
EIGEN_STRONG_INLINE const CwiseBinaryOp<ei_scalar_difference_op<typename ei_traits<Derived>::Scalar>,
148
Derived, OtherDerived>
149
MatrixBase<Derived>::operator-(const MatrixBase<OtherDerived> &other) const
151
return CwiseBinaryOp<ei_scalar_difference_op<Scalar>,
152
Derived, OtherDerived>(derived(), other.derived());
155
/** replaces \c *this by \c *this - \a other.
157
* \returns a reference to \c *this
159
template<typename Derived>
160
template<typename OtherDerived>
161
EIGEN_STRONG_INLINE Derived &
162
MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other)
164
return *this = *this - other;
167
/** \relates MatrixBase
169
* \returns an expression of the sum of \c *this and \a other
171
* \note If you want to add a given scalar to all coefficients, see Cwise::operator+().
173
* \sa class CwiseBinaryOp, MatrixBase::operator+=(), Cwise::operator+()
175
template<typename Derived>
176
template<typename OtherDerived>
177
EIGEN_STRONG_INLINE const CwiseBinaryOp<ei_scalar_sum_op<typename ei_traits<Derived>::Scalar>, Derived, OtherDerived>
178
MatrixBase<Derived>::operator+(const MatrixBase<OtherDerived> &other) const
180
return CwiseBinaryOp<ei_scalar_sum_op<Scalar>, Derived, OtherDerived>(derived(), other.derived());
183
/** replaces \c *this by \c *this + \a other.
185
* \returns a reference to \c *this
187
template<typename Derived>
188
template<typename OtherDerived>
189
EIGEN_STRONG_INLINE Derived &
190
MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other)
192
return *this = *this + other;
195
/** \returns an expression of the Schur product (coefficient wise product) of *this and \a other
197
* Example: \include Cwise_product.cpp
198
* Output: \verbinclude Cwise_product.out
200
* \sa class CwiseBinaryOp, operator/(), square()
202
template<typename ExpressionType>
203
template<typename OtherDerived>
204
EIGEN_STRONG_INLINE const EIGEN_CWISE_PRODUCT_RETURN_TYPE
205
Cwise<ExpressionType>::operator*(const MatrixBase<OtherDerived> &other) const
207
return EIGEN_CWISE_PRODUCT_RETURN_TYPE(_expression(), other.derived());
210
/** \returns an expression of the coefficient-wise quotient of *this and \a other
212
* Example: \include Cwise_quotient.cpp
213
* Output: \verbinclude Cwise_quotient.out
215
* \sa class CwiseBinaryOp, operator*(), inverse()
217
template<typename ExpressionType>
218
template<typename OtherDerived>
219
EIGEN_STRONG_INLINE const EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_quotient_op)
220
Cwise<ExpressionType>::operator/(const MatrixBase<OtherDerived> &other) const
222
return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_quotient_op)(_expression(), other.derived());
225
/** Replaces this expression by its coefficient-wise product with \a other.
227
* Example: \include Cwise_times_equal.cpp
228
* Output: \verbinclude Cwise_times_equal.out
230
* \sa operator*(), operator/=()
232
template<typename ExpressionType>
233
template<typename OtherDerived>
234
inline ExpressionType& Cwise<ExpressionType>::operator*=(const MatrixBase<OtherDerived> &other)
236
return m_matrix.const_cast_derived() = *this * other;
239
/** Replaces this expression by its coefficient-wise quotient by \a other.
241
* Example: \include Cwise_slash_equal.cpp
242
* Output: \verbinclude Cwise_slash_equal.out
244
* \sa operator/(), operator*=()
246
template<typename ExpressionType>
247
template<typename OtherDerived>
248
inline ExpressionType& Cwise<ExpressionType>::operator/=(const MatrixBase<OtherDerived> &other)
250
return m_matrix.const_cast_derived() = *this / other;
253
/** \returns an expression of the coefficient-wise min of *this and \a other
255
* Example: \include Cwise_min.cpp
256
* Output: \verbinclude Cwise_min.out
258
* \sa class CwiseBinaryOp
260
template<typename ExpressionType>
261
template<typename OtherDerived>
262
EIGEN_STRONG_INLINE const EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_min_op)
263
Cwise<ExpressionType>::min(const MatrixBase<OtherDerived> &other) const
265
return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_min_op)(_expression(), other.derived());
268
/** \returns an expression of the coefficient-wise max of *this and \a other
270
* Example: \include Cwise_max.cpp
271
* Output: \verbinclude Cwise_max.out
273
* \sa class CwiseBinaryOp
275
template<typename ExpressionType>
276
template<typename OtherDerived>
277
EIGEN_STRONG_INLINE const EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_max_op)
278
Cwise<ExpressionType>::max(const MatrixBase<OtherDerived> &other) const
280
return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_max_op)(_expression(), other.derived());
283
/** \returns an expression of a custom coefficient-wise operator \a func of *this and \a other
285
* The template parameter \a CustomBinaryOp is the type of the functor
286
* of the custom operator (see class CwiseBinaryOp for an example)
288
* \addexample CustomCwiseBinaryFunctors \label How to use custom coeff wise binary functors
290
* Here is an example illustrating the use of custom functors:
291
* \include class_CwiseBinaryOp.cpp
292
* Output: \verbinclude class_CwiseBinaryOp.out
294
* \sa class CwiseBinaryOp, MatrixBase::operator+, MatrixBase::operator-, Cwise::operator*, Cwise::operator/
296
template<typename Derived>
297
template<typename CustomBinaryOp, typename OtherDerived>
298
EIGEN_STRONG_INLINE const CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>
299
MatrixBase<Derived>::binaryExpr(const MatrixBase<OtherDerived> &other, const CustomBinaryOp& func) const
301
return CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>(derived(), other.derived(), func);
304
#endif // EIGEN_CWISE_BINARY_OP_H