xref: /aosp_15_r20/external/eigen/Eigen/src/plugins/MatrixCwiseBinaryOps.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008-2009 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2006-2008 Benoit Jacob <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li // This file is a base class plugin containing matrix specifics coefficient wise functions.
12*bf2c3715SXin Li 
13*bf2c3715SXin Li /** \returns an expression of the Schur product (coefficient wise product) of *this and \a other
14*bf2c3715SXin Li   *
15*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseProduct.cpp
16*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseProduct.out
17*bf2c3715SXin Li   *
18*bf2c3715SXin Li   * \sa class CwiseBinaryOp, cwiseAbs2
19*bf2c3715SXin Li   */
20*bf2c3715SXin Li template<typename OtherDerived>
21*bf2c3715SXin Li EIGEN_DEVICE_FUNC
EIGEN_CWISE_BINARY_RETURN_TYPE(Derived,OtherDerived,product)22*bf2c3715SXin Li EIGEN_STRONG_INLINE const EIGEN_CWISE_BINARY_RETURN_TYPE(Derived,OtherDerived,product)
23*bf2c3715SXin Li cwiseProduct(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
24*bf2c3715SXin Li {
25*bf2c3715SXin Li   return EIGEN_CWISE_BINARY_RETURN_TYPE(Derived,OtherDerived,product)(derived(), other.derived());
26*bf2c3715SXin Li }
27*bf2c3715SXin Li 
28*bf2c3715SXin Li /** \returns an expression of the coefficient-wise == operator of *this and \a other
29*bf2c3715SXin Li   *
30*bf2c3715SXin Li   * \warning this performs an exact comparison, which is generally a bad idea with floating-point types.
31*bf2c3715SXin Li   * In order to check for equality between two vectors or matrices with floating-point coefficients, it is
32*bf2c3715SXin Li   * generally a far better idea to use a fuzzy comparison as provided by isApprox() and
33*bf2c3715SXin Li   * isMuchSmallerThan().
34*bf2c3715SXin Li   *
35*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseEqual.cpp
36*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseEqual.out
37*bf2c3715SXin Li   *
38*bf2c3715SXin Li   * \sa cwiseNotEqual(), isApprox(), isMuchSmallerThan()
39*bf2c3715SXin Li   */
40*bf2c3715SXin Li template<typename OtherDerived>
41*bf2c3715SXin Li EIGEN_DEVICE_FUNC
42*bf2c3715SXin Li inline const CwiseBinaryOp<numext::equal_to<Scalar>, const Derived, const OtherDerived>
cwiseEqual(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> & other)43*bf2c3715SXin Li cwiseEqual(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
44*bf2c3715SXin Li {
45*bf2c3715SXin Li   return CwiseBinaryOp<numext::equal_to<Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
46*bf2c3715SXin Li }
47*bf2c3715SXin Li 
48*bf2c3715SXin Li /** \returns an expression of the coefficient-wise != operator of *this and \a other
49*bf2c3715SXin Li   *
50*bf2c3715SXin Li   * \warning this performs an exact comparison, which is generally a bad idea with floating-point types.
51*bf2c3715SXin Li   * In order to check for equality between two vectors or matrices with floating-point coefficients, it is
52*bf2c3715SXin Li   * generally a far better idea to use a fuzzy comparison as provided by isApprox() and
53*bf2c3715SXin Li   * isMuchSmallerThan().
54*bf2c3715SXin Li   *
55*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseNotEqual.cpp
56*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseNotEqual.out
57*bf2c3715SXin Li   *
58*bf2c3715SXin Li   * \sa cwiseEqual(), isApprox(), isMuchSmallerThan()
59*bf2c3715SXin Li   */
60*bf2c3715SXin Li template<typename OtherDerived>
61*bf2c3715SXin Li EIGEN_DEVICE_FUNC
62*bf2c3715SXin Li inline const CwiseBinaryOp<numext::not_equal_to<Scalar>, const Derived, const OtherDerived>
cwiseNotEqual(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> & other)63*bf2c3715SXin Li cwiseNotEqual(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
64*bf2c3715SXin Li {
65*bf2c3715SXin Li   return CwiseBinaryOp<numext::not_equal_to<Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
66*bf2c3715SXin Li }
67*bf2c3715SXin Li 
68*bf2c3715SXin Li /** \returns an expression of the coefficient-wise min of *this and \a other
69*bf2c3715SXin Li   *
70*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseMin.cpp
71*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseMin.out
72*bf2c3715SXin Li   *
73*bf2c3715SXin Li   * \sa class CwiseBinaryOp, max()
74*bf2c3715SXin Li   */
75*bf2c3715SXin Li template<typename OtherDerived>
76*bf2c3715SXin Li EIGEN_DEVICE_FUNC
77*bf2c3715SXin Li EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_min_op<Scalar,Scalar>, const Derived, const OtherDerived>
cwiseMin(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> & other)78*bf2c3715SXin Li cwiseMin(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
79*bf2c3715SXin Li {
80*bf2c3715SXin Li   return CwiseBinaryOp<internal::scalar_min_op<Scalar,Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
81*bf2c3715SXin Li }
82*bf2c3715SXin Li 
83*bf2c3715SXin Li /** \returns an expression of the coefficient-wise min of *this and scalar \a other
84*bf2c3715SXin Li   *
85*bf2c3715SXin Li   * \sa class CwiseBinaryOp, min()
86*bf2c3715SXin Li   */
87*bf2c3715SXin Li EIGEN_DEVICE_FUNC
88*bf2c3715SXin Li EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_min_op<Scalar,Scalar>, const Derived, const ConstantReturnType>
cwiseMin(const Scalar & other)89*bf2c3715SXin Li cwiseMin(const Scalar &other) const
90*bf2c3715SXin Li {
91*bf2c3715SXin Li   return cwiseMin(Derived::Constant(rows(), cols(), other));
92*bf2c3715SXin Li }
93*bf2c3715SXin Li 
94*bf2c3715SXin Li /** \returns an expression of the coefficient-wise max of *this and \a other
95*bf2c3715SXin Li   *
96*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseMax.cpp
97*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseMax.out
98*bf2c3715SXin Li   *
99*bf2c3715SXin Li   * \sa class CwiseBinaryOp, min()
100*bf2c3715SXin Li   */
101*bf2c3715SXin Li template<typename OtherDerived>
102*bf2c3715SXin Li EIGEN_DEVICE_FUNC
103*bf2c3715SXin Li EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_max_op<Scalar,Scalar>, const Derived, const OtherDerived>
cwiseMax(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> & other)104*bf2c3715SXin Li cwiseMax(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
105*bf2c3715SXin Li {
106*bf2c3715SXin Li   return CwiseBinaryOp<internal::scalar_max_op<Scalar,Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
107*bf2c3715SXin Li }
108*bf2c3715SXin Li 
109*bf2c3715SXin Li /** \returns an expression of the coefficient-wise max of *this and scalar \a other
110*bf2c3715SXin Li   *
111*bf2c3715SXin Li   * \sa class CwiseBinaryOp, min()
112*bf2c3715SXin Li   */
113*bf2c3715SXin Li EIGEN_DEVICE_FUNC
114*bf2c3715SXin Li EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_max_op<Scalar,Scalar>, const Derived, const ConstantReturnType>
cwiseMax(const Scalar & other)115*bf2c3715SXin Li cwiseMax(const Scalar &other) const
116*bf2c3715SXin Li {
117*bf2c3715SXin Li   return cwiseMax(Derived::Constant(rows(), cols(), other));
118*bf2c3715SXin Li }
119*bf2c3715SXin Li 
120*bf2c3715SXin Li 
121*bf2c3715SXin Li /** \returns an expression of the coefficient-wise quotient of *this and \a other
122*bf2c3715SXin Li   *
123*bf2c3715SXin Li   * Example: \include MatrixBase_cwiseQuotient.cpp
124*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_cwiseQuotient.out
125*bf2c3715SXin Li   *
126*bf2c3715SXin Li   * \sa class CwiseBinaryOp, cwiseProduct(), cwiseInverse()
127*bf2c3715SXin Li   */
128*bf2c3715SXin Li template<typename OtherDerived>
129*bf2c3715SXin Li EIGEN_DEVICE_FUNC
130*bf2c3715SXin Li EIGEN_STRONG_INLINE const CwiseBinaryOp<internal::scalar_quotient_op<Scalar>, const Derived, const OtherDerived>
cwiseQuotient(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> & other)131*bf2c3715SXin Li cwiseQuotient(const EIGEN_CURRENT_STORAGE_BASE_CLASS<OtherDerived> &other) const
132*bf2c3715SXin Li {
133*bf2c3715SXin Li   return CwiseBinaryOp<internal::scalar_quotient_op<Scalar>, const Derived, const OtherDerived>(derived(), other.derived());
134*bf2c3715SXin Li }
135*bf2c3715SXin Li 
136*bf2c3715SXin Li typedef CwiseBinaryOp<internal::scalar_cmp_op<Scalar,Scalar,internal::cmp_EQ>, const Derived, const ConstantReturnType> CwiseScalarEqualReturnType;
137*bf2c3715SXin Li 
138*bf2c3715SXin Li /** \returns an expression of the coefficient-wise == operator of \c *this and a scalar \a s
139*bf2c3715SXin Li   *
140*bf2c3715SXin Li   * \warning this performs an exact comparison, which is generally a bad idea with floating-point types.
141*bf2c3715SXin Li   * In order to check for equality between two vectors or matrices with floating-point coefficients, it is
142*bf2c3715SXin Li   * generally a far better idea to use a fuzzy comparison as provided by isApprox() and
143*bf2c3715SXin Li   * isMuchSmallerThan().
144*bf2c3715SXin Li   *
145*bf2c3715SXin Li   * \sa cwiseEqual(const MatrixBase<OtherDerived> &) const
146*bf2c3715SXin Li   */
147*bf2c3715SXin Li EIGEN_DEVICE_FUNC
148*bf2c3715SXin Li inline const CwiseScalarEqualReturnType
cwiseEqual(const Scalar & s)149*bf2c3715SXin Li cwiseEqual(const Scalar& s) const
150*bf2c3715SXin Li {
151*bf2c3715SXin Li   return CwiseScalarEqualReturnType(derived(), Derived::Constant(rows(), cols(), s), internal::scalar_cmp_op<Scalar,Scalar,internal::cmp_EQ>());
152*bf2c3715SXin Li }
153