NDDEM
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())
69  : m_rows(rows), m_cols(cols), m_functor(func)
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
114 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
115 {
116  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
117 }
118 
137 template<typename Derived>
138 template<typename CustomNullaryOp>
140 #ifndef EIGEN_PARSED_BY_DOXYGEN
142 #else
144 #endif
145 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
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>
288 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
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
#define EIGEN_DEPRECATED
Definition: Macros.h:1058
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1283
#define eigen_assert(x)
Definition: Macros.h:1037
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:147
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:142
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:105
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:61
EIGEN_DEVICE_FUNC CwiseNullaryOp(Index rows, Index cols, const NullaryOp &func=NullaryOp())
Definition: CwiseNullaryOp.h:68
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
Definition: CwiseNullaryOp.h:88
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const
Definition: CwiseNullaryOp.h:78
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
Definition: CwiseNullaryOp.h:87
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const
Definition: CwiseNullaryOp.h:80
EIGEN_DEVICE_FUNC const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
const NullaryOp m_functor
Definition: CwiseNullaryOp.h:89
internal::dense_xpr_base< CwiseNullaryOp >::type Base
Definition: CwiseNullaryOp.h:64
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
static EIGEN_DEVICE_FUNC const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:189
static EIGEN_DEVICE_FUNC const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:670
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:430
EIGEN_DEVICE_FUNC Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:345
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:73
EIGEN_DEVICE_FUNC void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:335
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
EIGEN_DEPRECATED static EIGEN_DEVICE_FUNC const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:244
static EIGEN_DEVICE_FUNC const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:514
EIGEN_DEVICE_FUNC bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:685
static EIGEN_DEVICE_FUNC const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
EIGEN_DEVICE_FUNC bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:528
EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:310
EIGEN_DEVICE_FUNC bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:325
EIGEN_DEVICE_FUNC Derived & setZero()
Definition: CwiseNullaryOp.h:546
EIGEN_DEVICE_FUNC Derived & setOnes()
Definition: CwiseNullaryOp.h:698
static EIGEN_DEVICE_FUNC const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:950
static EIGEN_DEVICE_FUNC const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:902
static EIGEN_DEVICE_FUNC const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:799
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:816
static EIGEN_DEVICE_FUNC const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:940
static EIGEN_DEVICE_FUNC const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:930
static EIGEN_DEVICE_FUNC const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:960
EIGEN_DEVICE_FUNC Derived & setIdentity()
Definition: CwiseNullaryOp.h:873
EIGEN_DEVICE_FUNC Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:972
EIGEN_DEVICE_FUNC Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:714
internal::traits< Derived >::Scalar Scalar
Definition: PlainObjectBase.h:106
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:361
Definition: XprHelper.h:110
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:135
const unsigned int RowMajorBit
Definition: Constants.h:66
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1947
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1940
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:1083
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
NoChange_t
Definition: Constants.h:360
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Sequential_t
Definition: Constants.h:361
const int Dynamic
Definition: Constants.h:22
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
Flags
Special flags for archives.
Definition: cereal.hpp:185
Definition: document.h:416
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
Definition: XprHelper.h:484
Definition: NullaryFunctors.h:131
Definition: NullaryFunctors.h:18
Definition: NullaryFunctors.h:31
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:854
Definition: CwiseNullaryOp.h:842
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:844
Definition: ForwardDeclarations.h:17
Definition: Meta.h:96
#define const
Definition: zconf.h:233