CommonCwiseUnaryOps.h File Reference

Go to the source code of this file.

Classes

struct  CastXpr< NewType >
 

Functions

template<typename NewType >
CastXpr< NewType >::Type cast () const
 
ConjugateReturnType conjugate () const
 
template<bool Cond>
std::conditional_t< Cond, ConjugateReturnType, const Derived & > conjugateIf () const
 
const ImagReturnType imag () const
 
const NegativeReturnType operator- () const
 
RealReturnType real () const
 
template<typename CustomUnaryOp >
const CwiseUnaryOp< CustomUnaryOp, const Derived > unaryExpr (const CustomUnaryOp &func=CustomUnaryOp()) const
 Apply a unary operator coefficient-wise. More...
 
template<typename CustomViewOp >
const CwiseUnaryView< CustomViewOp, const Derived > unaryViewExpr (const CustomViewOp &func=CustomViewOp()) const
 

Function Documentation

◆ cast()

template<typename NewType >
CastXpr<NewType>::Type cast ( ) const
Returns
an expression of *this with the Scalar type casted to NewScalar.

The template parameter NewScalar is the type we are casting the scalars to.

See also
class CwiseUnaryOp

Definition at line 62 of file CommonCwiseUnaryOps.h.

63 {
64  return typename CastXpr<NewType>::Type(derived());
65 }
internal::cast_return_type< Derived, const CwiseUnaryOp< internal::core_cast_op< Scalar, NewType >, const Derived > >::type Type

◆ conjugate()

ConjugateReturnType conjugate ( ) const
inline
Returns
an expression of the complex conjugate of *this.
See also
Math functions, MatrixBase::adjoint()

Definition at line 74 of file CommonCwiseUnaryOps.h.

75 {
76  return ConjugateReturnType(derived());
77 }

◆ conjugateIf()

template<bool Cond>
std::conditional_t<Cond,ConjugateReturnType,const Derived&> conjugateIf ( ) const
inline
Returns
an expression of the complex conjugate of *this if Cond==true, returns derived() otherwise.
See also
conjugate()

Definition at line 87 of file CommonCwiseUnaryOps.h.

88 {
89  typedef std::conditional_t<Cond,ConjugateReturnType,const Derived&> ReturnType;
90  return ReturnType(derived());
91 }

◆ imag()

NonConstImagReturnType imag ( ) const
inline
Returns
an read-only expression of the imaginary part of *this.
See also
real()
Returns
a non const expression of the imaginary part of *this.
See also
real()

Definition at line 109 of file CommonCwiseUnaryOps.h.

109 { return ImagReturnType(derived()); }

◆ operator-()

const NegativeReturnType operator- ( ) const
inline
Returns
an expression of the opposite of *this

Definition at line 45 of file CommonCwiseUnaryOps.h.

45 { return NegativeReturnType(derived()); }

◆ real()

NonConstRealReturnType real ( ) const
inline
Returns
a read-only expression of the real part of *this.
See also
imag()
Returns
a non const expression of the real part of *this.
See also
imag()

Definition at line 100 of file CommonCwiseUnaryOps.h.

100 { return RealReturnType(derived()); }

◆ unaryExpr()

template<typename CustomUnaryOp >
const CwiseUnaryOp<CustomUnaryOp, const Derived> unaryExpr ( const CustomUnaryOp &  func = CustomUnaryOp()) const
inline

Apply a unary operator coefficient-wise.

Parameters
[in]funcFunctor implementing the unary operator
Template Parameters
CustomUnaryOpType of func
Returns
An expression of a custom coefficient-wise unary operator func of *this

The function ptr_fun() from the C++ standard library can be used to make functors out of normal functions.

Example:

#include <Eigen/Core>
#include <iostream>
// define function to be applied coefficient-wise
double ramp(double x)
{
if (x > 0)
return x;
else
return 0;
}
int main(int, char**)
{
std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(std::ptr_fun(ramp)) << std::endl;
return 0;
}
Matrix3d m1
Definition: IOFormat.cpp:2
static const RandomReturnType Random()
Definition: Random.h:114
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
int main(int, char **)
Definition: class_Block.cpp:18
double ramp(double x)

Output:

   0.68   0.823  -0.444   -0.27
 -0.211  -0.605   0.108  0.0268
  0.566   -0.33 -0.0452   0.904
  0.597   0.536   0.258   0.832
becomes: 
  0.68  0.823      0      0
     0      0  0.108 0.0268
 0.566      0      0  0.904
 0.597  0.536  0.258  0.832

Genuine functors allow for more possibilities, for instance it may contain a state.

Example:

#include <Eigen/Core>
#include <iostream>
// define a custom template unary functor
template<typename Scalar>
struct CwiseClampOp {
CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {}
const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); }
Scalar m_inf, m_sup;
};
int main(int, char**)
{
std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << std::endl;
return 0;
}
IndexedView_or_Block operator()(const RowIndices &rowIndices, const ColIndices &colIndices)

Output:

   0.68   0.823  -0.444   -0.27
 -0.211  -0.605   0.108  0.0268
  0.566   -0.33 -0.0452   0.904
  0.597   0.536   0.258   0.832
becomes: 
    0.5     0.5  -0.444   -0.27
 -0.211    -0.5   0.108  0.0268
    0.5   -0.33 -0.0452     0.5
    0.5     0.5   0.258     0.5
See also
unaryViewExpr, binaryExpr, class CwiseUnaryOp

Definition at line 135 of file CommonCwiseUnaryOps.h.

136 {
137  return CwiseUnaryOp<CustomUnaryOp, const Derived>(derived(), func);
138 }

◆ unaryViewExpr()

template<typename CustomViewOp >
const CwiseUnaryView<CustomViewOp, const Derived> unaryViewExpr ( const CustomViewOp &  func = CustomViewOp()) const
inline
Returns
an expression of a custom coefficient-wise unary operator func of *this

The template parameter CustomUnaryOp is the type of the functor of the custom unary operator.

Example:

#include <Eigen/Core>
#include <iostream>
// define a custom template unary functor
template<typename Scalar>
struct CwiseClampOp {
CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {}
const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); }
Scalar m_inf, m_sup;
};
int main(int, char**)
{
std::cout << m1 << std::endl << "becomes: " << std::endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << std::endl;
return 0;
}

Output:

   0.68   0.823  -0.444   -0.27
 -0.211  -0.605   0.108  0.0268
  0.566   -0.33 -0.0452   0.904
  0.597   0.536   0.258   0.832
becomes: 
    0.5     0.5  -0.444   -0.27
 -0.211    -0.5   0.108  0.0268
    0.5   -0.33 -0.0452     0.5
    0.5     0.5   0.258     0.5
See also
unaryExpr, binaryExpr class CwiseUnaryOp

Definition at line 156 of file CommonCwiseUnaryOps.h.

157 {
158  return CwiseUnaryView<CustomViewOp, const Derived>(derived(), func);
159 }