~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to extern/Eigen2/Eigen/src/Core/CwiseBinaryOp.h

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// This file is part of Eigen, a lightweight C++ template library
2
 
// for linear algebra. Eigen itself is part of the KDE project.
3
 
//
4
 
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5
 
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6
 
//
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.
11
 
//
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.
16
 
//
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.
21
 
//
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/>.
25
 
 
26
 
#ifndef EIGEN_CWISE_BINARY_OP_H
27
 
#define EIGEN_CWISE_BINARY_OP_H
28
 
 
29
 
/** \class CwiseBinaryOp
30
 
  *
31
 
  * \brief Generic expression of a coefficient-wise operator between two matrices or vectors
32
 
  *
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
36
 
  *
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.
40
 
  *
41
 
  * However, if you want to write a function returning such an expression, you
42
 
  * will need to use this class.
43
 
  *
44
 
  * \sa MatrixBase::binaryExpr(const MatrixBase<OtherDerived> &,const CustomBinaryOp &) const, class CwiseUnaryOp, class CwiseNullaryOp
45
 
  */
46
 
template<typename BinaryOp, typename Lhs, typename Rhs>
47
 
struct ei_traits<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
48
 
{
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<
52
 
                     BinaryOp(
53
 
                       typename Lhs::Scalar,
54
 
                       typename Rhs::Scalar
55
 
                     )
56
 
                   >::type Scalar;
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;
61
 
  enum {
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)) & (
71
 
        HereditaryBits
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
76
 
  };
77
 
};
78
 
 
79
 
template<typename BinaryOp, typename Lhs, typename Rhs>
80
 
class CwiseBinaryOp : ei_no_assignment_operator,
81
 
  public MatrixBase<CwiseBinaryOp<BinaryOp, Lhs, Rhs> >
82
 
{
83
 
  public:
84
 
 
85
 
    EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseBinaryOp)
86
 
    typedef typename ei_traits<CwiseBinaryOp>::LhsNested LhsNested;
87
 
    typedef typename ei_traits<CwiseBinaryOp>::RhsNested RhsNested;
88
 
 
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)
91
 
    {
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());
106
 
    }
107
 
 
108
 
    EIGEN_STRONG_INLINE int rows() const { return m_lhs.rows(); }
109
 
    EIGEN_STRONG_INLINE int cols() const { return m_lhs.cols(); }
110
 
 
111
 
    EIGEN_STRONG_INLINE const Scalar coeff(int row, int col) const
112
 
    {
113
 
      return m_functor(m_lhs.coeff(row, col), m_rhs.coeff(row, col));
114
 
    }
115
 
 
116
 
    template<int LoadMode>
117
 
    EIGEN_STRONG_INLINE PacketScalar packet(int row, int col) const
118
 
    {
119
 
      return m_functor.packetOp(m_lhs.template packet<LoadMode>(row, col), m_rhs.template packet<LoadMode>(row, col));
120
 
    }
121
 
 
122
 
    EIGEN_STRONG_INLINE const Scalar coeff(int index) const
123
 
    {
124
 
      return m_functor(m_lhs.coeff(index), m_rhs.coeff(index));
125
 
    }
126
 
 
127
 
    template<int LoadMode>
128
 
    EIGEN_STRONG_INLINE PacketScalar packet(int index) const
129
 
    {
130
 
      return m_functor.packetOp(m_lhs.template packet<LoadMode>(index), m_rhs.template packet<LoadMode>(index));
131
 
    }
132
 
 
133
 
  protected:
134
 
    const LhsNested m_lhs;
135
 
    const RhsNested m_rhs;
136
 
    const BinaryOp m_functor;
137
 
};
138
 
 
139
 
/**\returns an expression of the difference of \c *this and \a other
140
 
  *
141
 
  * \note If you want to substract a given scalar from all coefficients, see Cwise::operator-().
142
 
  *
143
 
  * \sa class CwiseBinaryOp, MatrixBase::operator-=(), Cwise::operator-()
144
 
  */
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
150
 
{
151
 
  return CwiseBinaryOp<ei_scalar_difference_op<Scalar>,
152
 
                       Derived, OtherDerived>(derived(), other.derived());
153
 
}
154
 
 
155
 
/** replaces \c *this by \c *this - \a other.
156
 
  *
157
 
  * \returns a reference to \c *this
158
 
  */
159
 
template<typename Derived>
160
 
template<typename OtherDerived>
161
 
EIGEN_STRONG_INLINE Derived &
162
 
MatrixBase<Derived>::operator-=(const MatrixBase<OtherDerived> &other)
163
 
{
164
 
  return *this = *this - other;
165
 
}
166
 
 
167
 
/** \relates MatrixBase
168
 
  *
169
 
  * \returns an expression of the sum of \c *this and \a other
170
 
  *
171
 
  * \note If you want to add a given scalar to all coefficients, see Cwise::operator+().
172
 
  *
173
 
  * \sa class CwiseBinaryOp, MatrixBase::operator+=(), Cwise::operator+()
174
 
  */
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
179
 
{
180
 
  return CwiseBinaryOp<ei_scalar_sum_op<Scalar>, Derived, OtherDerived>(derived(), other.derived());
181
 
}
182
 
 
183
 
/** replaces \c *this by \c *this + \a other.
184
 
  *
185
 
  * \returns a reference to \c *this
186
 
  */
187
 
template<typename Derived>
188
 
template<typename OtherDerived>
189
 
EIGEN_STRONG_INLINE Derived &
190
 
MatrixBase<Derived>::operator+=(const MatrixBase<OtherDerived>& other)
191
 
{
192
 
  return *this = *this + other;
193
 
}
194
 
 
195
 
/** \returns an expression of the Schur product (coefficient wise product) of *this and \a other
196
 
  *
197
 
  * Example: \include Cwise_product.cpp
198
 
  * Output: \verbinclude Cwise_product.out
199
 
  *
200
 
  * \sa class CwiseBinaryOp, operator/(), square()
201
 
  */
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
206
 
{
207
 
  return EIGEN_CWISE_PRODUCT_RETURN_TYPE(_expression(), other.derived());
208
 
}
209
 
 
210
 
/** \returns an expression of the coefficient-wise quotient of *this and \a other
211
 
  *
212
 
  * Example: \include Cwise_quotient.cpp
213
 
  * Output: \verbinclude Cwise_quotient.out
214
 
  *
215
 
  * \sa class CwiseBinaryOp, operator*(), inverse()
216
 
  */
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
221
 
{
222
 
  return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_quotient_op)(_expression(), other.derived());
223
 
}
224
 
 
225
 
/** Replaces this expression by its coefficient-wise product with \a other.
226
 
  *
227
 
  * Example: \include Cwise_times_equal.cpp
228
 
  * Output: \verbinclude Cwise_times_equal.out
229
 
  *
230
 
  * \sa operator*(), operator/=()
231
 
  */
232
 
template<typename ExpressionType>
233
 
template<typename OtherDerived>
234
 
inline ExpressionType& Cwise<ExpressionType>::operator*=(const MatrixBase<OtherDerived> &other)
235
 
{
236
 
  return m_matrix.const_cast_derived() = *this * other;
237
 
}
238
 
 
239
 
/** Replaces this expression by its coefficient-wise quotient by \a other.
240
 
  *
241
 
  * Example: \include Cwise_slash_equal.cpp
242
 
  * Output: \verbinclude Cwise_slash_equal.out
243
 
  *
244
 
  * \sa operator/(), operator*=()
245
 
  */
246
 
template<typename ExpressionType>
247
 
template<typename OtherDerived>
248
 
inline ExpressionType& Cwise<ExpressionType>::operator/=(const MatrixBase<OtherDerived> &other)
249
 
{
250
 
  return m_matrix.const_cast_derived() = *this / other;
251
 
}
252
 
 
253
 
/** \returns an expression of the coefficient-wise min of *this and \a other
254
 
  *
255
 
  * Example: \include Cwise_min.cpp
256
 
  * Output: \verbinclude Cwise_min.out
257
 
  *
258
 
  * \sa class CwiseBinaryOp
259
 
  */
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
264
 
{
265
 
  return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_min_op)(_expression(), other.derived());
266
 
}
267
 
 
268
 
/** \returns an expression of the coefficient-wise max of *this and \a other
269
 
  *
270
 
  * Example: \include Cwise_max.cpp
271
 
  * Output: \verbinclude Cwise_max.out
272
 
  *
273
 
  * \sa class CwiseBinaryOp
274
 
  */
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
279
 
{
280
 
  return EIGEN_CWISE_BINOP_RETURN_TYPE(ei_scalar_max_op)(_expression(), other.derived());
281
 
}
282
 
 
283
 
/** \returns an expression of a custom coefficient-wise operator \a func of *this and \a other
284
 
  *
285
 
  * The template parameter \a CustomBinaryOp is the type of the functor
286
 
  * of the custom operator (see class CwiseBinaryOp for an example)
287
 
  *
288
 
  * \addexample CustomCwiseBinaryFunctors \label How to use custom coeff wise binary functors
289
 
  *
290
 
  * Here is an example illustrating the use of custom functors:
291
 
  * \include class_CwiseBinaryOp.cpp
292
 
  * Output: \verbinclude class_CwiseBinaryOp.out
293
 
  *
294
 
  * \sa class CwiseBinaryOp, MatrixBase::operator+, MatrixBase::operator-, Cwise::operator*, Cwise::operator/
295
 
  */
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
300
 
{
301
 
  return CwiseBinaryOp<CustomBinaryOp, Derived, OtherDerived>(derived(), other.derived(), func);
302
 
}
303
 
 
304
 
#endif // EIGEN_CWISE_BINARY_OP_H