CwiseNullaryOp.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_CWISE_NULLARY_OP_H
11 #define EIGEN_CWISE_NULLARY_OP_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 template<typename NullaryOp, typename PlainObjectType>
17 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
18 {
19  enum {
21  };
22 };
23 
24 } // namespace internal
25 
59 template<typename NullaryOp, typename PlainObjectType>
60 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
61 {
62  public:
63 
66 
68  CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
70  {
71  eigen_assert(rows >= 0
72  && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
73  && cols >= 0
74  && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
75  }
76 
78  Index rows() const { return m_rows.value(); }
80  Index cols() const { return m_cols.value(); }
81 
84  const NullaryOp& functor() const { return m_functor; }
85 
86  protected:
89  const NullaryOp m_functor;
90 };
91 
92 
106 template<typename Derived>
107 template<typename CustomNullaryOp>
109 #ifndef EIGEN_PARSED_BY_DOXYGEN
111 #else
113 #endif
115 {
117 }
118 
137 template<typename Derived>
138 template<typename CustomNullaryOp>
140 #ifndef EIGEN_PARSED_BY_DOXYGEN
142 #else
144 #endif
146 {
148  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
150 }
151 
161 template<typename Derived>
162 template<typename CustomNullaryOp>
164 #ifndef EIGEN_PARSED_BY_DOXYGEN
166 #else
168 #endif
169 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
170 {
171  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
172 }
173 
187 template<typename Derived>
190 {
192 }
193 
209 template<typename Derived>
212 {
214 }
215 
225 template<typename Derived>
228 {
230  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
231 }
232 
242 template<typename Derived>
245 {
248 }
249 
254 template<typename Derived>
257 {
260  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
261 }
262 
286 template<typename Derived>
289 {
292 }
293 
298 template<typename Derived>
301 {
304  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
305 }
306 
308 template<typename Derived>
310 (const Scalar& val, const RealScalar& prec) const
311 {
312  typename internal::nested_eval<Derived,1>::type self(derived());
313  for(Index j = 0; j < cols(); ++j)
314  for(Index i = 0; i < rows(); ++i)
315  if(!internal::isApprox(self.coeff(i, j), val, prec))
316  return false;
317  return true;
318 }
319 
323 template<typename Derived>
325 (const Scalar& val, const RealScalar& prec) const
326 {
327  return isApproxToConstant(val, prec);
328 }
329 
334 template<typename Derived>
336 {
337  setConstant(val);
338 }
339 
344 template<typename Derived>
346 {
347  return derived() = Constant(rows(), cols(), val);
348 }
349 
359 template<typename Derived>
362 {
363  resize(size);
364  return setConstant(val);
365 }
366 
378 template<typename Derived>
381 {
382  resize(rows, cols);
383  return setConstant(val);
384 }
385 
392 template<typename Derived>
395 {
396  return setConstant(rows(), cols, val);
397 }
398 
405 template<typename Derived>
408 {
409  return setConstant(rows, cols(), val);
410 }
411 
412 
429 template<typename Derived>
431 {
433  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
434 }
435 
449 template<typename Derived>
451 {
453  return setLinSpaced(size(), low, high);
454 }
455 
456 // zero:
457 
472 template<typename Derived>
475 {
476  return Constant(rows, cols, Scalar(0));
477 }
478 
495 template<typename Derived>
498 {
499  return Constant(size, Scalar(0));
500 }
501 
512 template<typename Derived>
515 {
516  return Constant(Scalar(0));
517 }
518 
527 template<typename Derived>
529 {
530  typename internal::nested_eval<Derived,1>::type self(derived());
531  for(Index j = 0; j < cols(); ++j)
532  for(Index i = 0; i < rows(); ++i)
533  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
534  return false;
535  return true;
536 }
537 
545 template<typename Derived>
547 {
548  return setConstant(Scalar(0));
549 }
550 
560 template<typename Derived>
563 {
564  resize(newSize);
565  return setConstant(Scalar(0));
566 }
567 
578 template<typename Derived>
581 {
582  resize(rows, cols);
583  return setConstant(Scalar(0));
584 }
585 
592 template<typename Derived>
595 {
596  return setZero(rows(), cols);
597 }
598 
605 template<typename Derived>
608 {
609  return setZero(rows, cols());
610 }
611 
612 // ones:
613 
628 template<typename Derived>
631 {
632  return Constant(rows, cols, Scalar(1));
633 }
634 
651 template<typename Derived>
654 {
655  return Constant(newSize, Scalar(1));
656 }
657 
668 template<typename Derived>
671 {
672  return Constant(Scalar(1));
673 }
674 
683 template<typename Derived>
685 (const RealScalar& prec) const
686 {
687  return isApproxToConstant(Scalar(1), prec);
688 }
689 
697 template<typename Derived>
699 {
700  return setConstant(Scalar(1));
701 }
702 
712 template<typename Derived>
715 {
716  resize(newSize);
717  return setConstant(Scalar(1));
718 }
719 
730 template<typename Derived>
733 {
734  resize(rows, cols);
735  return setConstant(Scalar(1));
736 }
737 
744 template<typename Derived>
747 {
748  return setOnes(rows, cols());
749 }
750 
757 template<typename Derived>
760 {
761  return setOnes(rows(), cols);
762 }
763 
764 // Identity:
765 
780 template<typename Derived>
783 {
785 }
786 
797 template<typename Derived>
800 {
802  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
803 }
804 
814 template<typename Derived>
816 (const RealScalar& prec) const
817 {
818  typename internal::nested_eval<Derived,1>::type self(derived());
819  for(Index j = 0; j < cols(); ++j)
820  {
821  for(Index i = 0; i < rows(); ++i)
822  {
823  if(i == j)
824  {
825  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
826  return false;
827  }
828  else
829  {
830  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
831  return false;
832  }
833  }
834  }
835  return true;
836 }
837 
838 namespace internal {
839 
840 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
842 {
844  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
845  {
846  return m = Derived::Identity(m.rows(), m.cols());
847  }
848 };
849 
850 template<typename Derived>
851 struct setIdentity_impl<Derived, true>
852 {
854  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
855  {
856  m.setZero();
857  const Index size = numext::mini(m.rows(), m.cols());
858  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
859  return m;
860  }
861 };
862 
863 } // end namespace internal
864 
872 template<typename Derived>
874 {
876 }
877 
888 template<typename Derived>
890 {
891  derived().resize(rows, cols);
892  return setIdentity();
893 }
894 
901 template<typename Derived>
903 {
905  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
906 }
907 
916 template<typename Derived>
918 {
920  return BasisReturnType(SquareMatrixType::Identity(),i);
921 }
922 
929 template<typename Derived>
931 { return Derived::Unit(0); }
932 
939 template<typename Derived>
941 { return Derived::Unit(1); }
942 
949 template<typename Derived>
951 { return Derived::Unit(2); }
952 
959 template<typename Derived>
961 { return Derived::Unit(3); }
962 
971 template<typename Derived>
973 {
975  eigen_assert(i<size());
976  derived().setZero();
977  derived().coeffRef(i) = Scalar(1);
978  return derived();
979 }
980 
990 template<typename Derived>
992 {
994  eigen_assert(i<newSize);
995  derived().resize(newSize);
996  return setUnit(i);
997 }
998 
999 } // end namespace Eigen
1000 
1001 #endif // EIGEN_CWISE_NULLARY_OP_H
Eigen::CwiseNullaryOp::Base
internal::dense_xpr_base< CwiseNullaryOp >::type Base
Definition: CwiseNullaryOp.h:64
Eigen::MatrixBase::UnitX
static const EIGEN_DEVICE_FUNC BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:930
Eigen::internal::variable_if_dynamic< Index, RowsAtCompileTime >
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::internal::scalar_identity_op
Definition: NullaryFunctors.h:31
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::DenseBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero()
Definition: CwiseNullaryOp.h:546
Eigen::DenseBase::isZero
EIGEN_DEVICE_FUNC bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:528
Eigen::Block
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Eigen::internal::linspaced_op
Definition: NullaryFunctors.h:119
resize
v resize(3)
Eigen::DenseBase::Zero
static const EIGEN_DEVICE_FUNC ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:514
Eigen::DenseBase< Solve< Decomposition, RhsType > >::Scalar
internal::traits< Solve< Decomposition, RhsType > >::Scalar Scalar
Definition: DenseBase.h:66
Eigen::DenseBase::Constant
static const EIGEN_DEVICE_FUNC ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:189
Eigen::internal::dense_xpr_base
Definition: XprHelper.h:483
Eigen::MatrixBase::UnitW
static const EIGEN_DEVICE_FUNC BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:960
Eigen::DenseBase::isApproxToConstant
EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:310
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::DenseBase::setConstant
EIGEN_DEVICE_FUNC Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:345
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::MatrixBase::UnitZ
static const EIGEN_DEVICE_FUNC BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:950
EIGEN_CONSTEXPR
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
EIGEN_DEPRECATED
#define EIGEN_DEPRECATED
Definition: Macros.h:1058
Eigen::DenseBase::Ones
static const EIGEN_DEVICE_FUNC ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:670
type
Definition: pytypes.h:1525
Eigen::internal::isApprox
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1947
EIGEN_DENSE_PUBLIC_INTERFACE
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1283
EIGEN_STATIC_ASSERT_VECTOR_ONLY
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:142
Eigen::NoChange_t
NoChange_t
Definition: Constants.h:360
Eigen::internal::isMuchSmallerThan
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: Eigen/src/Core/MathFunctions.h:1940
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
setIdentity
m setIdentity(3, 3)
Eigen::DenseBase::setLinSpaced
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:430
Eigen::CwiseNullaryOp
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:60
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::CwiseNullaryOp::m_rows
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
Definition: CwiseNullaryOp.h:87
Eigen::DenseBase::isOnes
EIGEN_DEVICE_FUNC bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:685
Eigen::MatrixBase::UnitY
static const EIGEN_DEVICE_FUNC BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:940
Eigen::PlainObjectBase::setConstant
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:361
Eigen::internal::true_type
Definition: Meta.h:96
Eigen::DenseBase< Solve< Decomposition, RhsType > >::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:73
Eigen::CwiseNullaryOp::rows
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const
Definition: CwiseNullaryOp.h:78
Eigen::CwiseNullaryOp::m_functor
const NullaryOp m_functor
Definition: CwiseNullaryOp.h:89
Eigen::DenseBase::LinSpaced
EIGEN_DEPRECATED static const EIGEN_DEVICE_FUNC RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:244
Eigen::MatrixBase::Identity
static const EIGEN_DEVICE_FUNC IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:799
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
setLinSpaced
v setLinSpaced(5, 0.5f, 1.5f)
Eigen::internal::scalar_constant_op
Definition: NullaryFunctors.h:18
Eigen::internal::setIdentity_impl
Definition: CwiseNullaryOp.h:841
Eigen::PlainObjectBase::setOnes
EIGEN_DEVICE_FUNC Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:714
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::numext::mini
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: Eigen/src/Core/MathFunctions.h:1085
Eigen::internal::variable_if_dynamic::value
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:135
Eigen::CwiseNullaryOp::CwiseNullaryOp
EIGEN_DEVICE_FUNC CwiseNullaryOp(Index rows, Index cols, const NullaryOp &func=NullaryOp())
Definition: CwiseNullaryOp.h:68
setConstant
v setConstant(3, 5)
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::CwiseNullaryOp::cols
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const
Definition: CwiseNullaryOp.h:80
Eigen::DenseBase::isConstant
EIGEN_DEVICE_FUNC bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:325
Eigen::internal::setIdentity_impl::run
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:844
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::MatrixBase::isIdentity
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:816
Eigen::Sequential_t
Sequential_t
Definition: Constants.h:361
Eigen::DenseBase::fill
EIGEN_DEVICE_FUNC void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:335
Eigen::CwiseNullaryOp::functor
const EIGEN_DEVICE_FUNC NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
Eigen::MatrixBase::setUnit
EIGEN_DEVICE_FUNC Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:972
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::MatrixBase::Unit
static const EIGEN_DEVICE_FUNC BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:902
Eigen::DenseBase::setOnes
EIGEN_DEVICE_FUNC Derived & setOnes()
Definition: CwiseNullaryOp.h:698
Eigen::internal::no_assignment_operator
Definition: XprHelper.h:109
setZero
v setZero(3)
internal
Definition: BandTriangularSolver.h:13
Eigen::PlainObjectBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
Eigen::DenseBase::NullaryExpr
static const EIGEN_DEVICE_FUNC CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Eigen::internal::size
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::PlainObjectBase< Matrix< int, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Scalar
internal::traits< Matrix< int, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Scalar Scalar
Definition: PlainObjectBase.h:106
func
Definition: benchGeometry.cpp:23
EIGEN_STATIC_ASSERT_FIXED_SIZE
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:147
test_callbacks.value
value
Definition: test_callbacks.py:160
setOnes
v setOnes(3)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::internal::setIdentity_impl< Derived, true >::run
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:854
Eigen::MatrixBase::setIdentity
EIGEN_DEVICE_FUNC Derived & setIdentity()
Definition: CwiseNullaryOp.h:873
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::CwiseNullaryOp::m_cols
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
Definition: CwiseNullaryOp.h:88


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:07