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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 template<typename NullaryOp, typename PlainObjectType>
19 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
20 {
21  enum {
22  Flags = traits<PlainObjectType>::Flags & RowMajorBit
23  };
24 };
25 
26 } // namespace internal
27 
61 template<typename NullaryOp, typename PlainObjectType>
62 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
63 {
64  public:
65 
66  typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
68 
70  CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
71  : m_rows(rows), m_cols(cols), m_functor(func)
72  {
73  eigen_assert(rows >= 0
74  && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
75  && cols >= 0
76  && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
77  }
78 
79  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
80  Index rows() const { return m_rows.value(); }
81  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
82  Index cols() const { return m_cols.value(); }
83 
86  const NullaryOp& functor() const { return m_functor; }
87 
88  protected:
89  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
90  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
91  const NullaryOp m_functor;
92 };
93 
94 
108 template<typename Derived>
109 template<typename CustomNullaryOp>
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
111 #ifndef EIGEN_PARSED_BY_DOXYGEN
113 #else
115 #endif
116 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
117 {
119 }
120 
139 template<typename Derived>
140 template<typename CustomNullaryOp>
141 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
142 #ifndef EIGEN_PARSED_BY_DOXYGEN
144 #else
146 #endif
147 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
148 {
150  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
152 }
153 
163 template<typename Derived>
164 template<typename CustomNullaryOp>
165 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
166 #ifndef EIGEN_PARSED_BY_DOXYGEN
168 #else
170 #endif
171 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
172 {
173  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
174 }
175 
189 template<typename Derived>
190 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
192 {
193  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
194 }
195 
211 template<typename Derived>
212 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
214 {
215  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
216 }
217 
227 template<typename Derived>
228 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
230 {
232  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
233 }
234 
244 template<typename Derived>
247 {
249  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
250 }
251 
256 template<typename Derived>
259 {
262  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
263 }
264 
288 template<typename Derived>
289 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
290 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
291 {
293  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
294 }
295 
300 template<typename Derived>
303 {
306  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
307 }
308 
309 template <typename Derived>
313  return DenseBase<Derived>::NullaryExpr(size, internal::equalspaced_op<Scalar>(low, step));
314 }
315 
316 template <typename Derived>
320  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::equalspaced_op<Scalar>(low, step));
321 }
322 
324 template<typename Derived>
326 (const Scalar& val, const RealScalar& prec) const
327 {
328  typename internal::nested_eval<Derived,1>::type self(derived());
329  for(Index j = 0; j < cols(); ++j)
330  for(Index i = 0; i < rows(); ++i)
331  if(!internal::isApprox(self.coeff(i, j), val, prec))
332  return false;
333  return true;
334 }
335 
339 template<typename Derived>
341 (const Scalar& val, const RealScalar& prec) const
342 {
343  return isApproxToConstant(val, prec);
344 }
345 
350 template<typename Derived>
351 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
352 {
353  setConstant(val);
354 }
355 
360 template<typename Derived>
361 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
362 {
363  return derived() = Constant(rows(), cols(), val);
364 }
365 
375 template<typename Derived>
376 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
378 {
379  resize(size);
380  return setConstant(val);
381 }
382 
394 template<typename Derived>
395 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
397 {
398  resize(rows, cols);
399  return setConstant(val);
400 }
401 
408 template<typename Derived>
409 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
411 {
412  return setConstant(rows(), cols, val);
413 }
414 
421 template<typename Derived>
422 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
424 {
425  return setConstant(rows, cols(), val);
426 }
427 
428 
445 template<typename Derived>
446 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
447 {
449  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
450 }
451 
465 template<typename Derived>
466 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
467 {
469  return setLinSpaced(size(), low, high);
470 }
471 
472 template <typename Derived>
473 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setEqualSpaced(Index newSize, const Scalar& low,
474  const Scalar& step) {
476  return derived() = Derived::NullaryExpr(newSize, internal::equalspaced_op<Scalar>(low, step));
477 }
478 template <typename Derived>
479 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setEqualSpaced(const Scalar& low,
480  const Scalar& step) {
482  return setEqualSpaced(size(), low, step);
483 }
484 
485 // zero:
486 
501 template<typename Derived>
502 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
504 {
505  return Constant(rows, cols, Scalar(0));
506 }
507 
524 template<typename Derived>
525 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
527 {
528  return Constant(size, Scalar(0));
529 }
530 
541 template<typename Derived>
542 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
544 {
545  return Constant(Scalar(0));
546 }
547 
556 template<typename Derived>
558 {
559  typename internal::nested_eval<Derived,1>::type self(derived());
560  for(Index j = 0; j < cols(); ++j)
561  for(Index i = 0; i < rows(); ++i)
562  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
563  return false;
564  return true;
565 }
566 
574 template<typename Derived>
575 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
576 {
577  return setConstant(Scalar(0));
578 }
579 
589 template<typename Derived>
590 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
592 {
593  resize(newSize);
594  return setConstant(Scalar(0));
595 }
596 
607 template<typename Derived>
608 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
610 {
611  resize(rows, cols);
612  return setConstant(Scalar(0));
613 }
614 
621 template<typename Derived>
622 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
624 {
625  return setZero(rows(), cols);
626 }
627 
634 template<typename Derived>
635 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
637 {
638  return setZero(rows, cols());
639 }
640 
641 // ones:
642 
657 template<typename Derived>
658 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
660 {
661  return Constant(rows, cols, Scalar(1));
662 }
663 
680 template<typename Derived>
681 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
683 {
684  return Constant(newSize, Scalar(1));
685 }
686 
697 template<typename Derived>
698 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
700 {
701  return Constant(Scalar(1));
702 }
703 
712 template<typename Derived>
714 (const RealScalar& prec) const
715 {
716  return isApproxToConstant(Scalar(1), prec);
717 }
718 
726 template<typename Derived>
727 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
728 {
729  return setConstant(Scalar(1));
730 }
731 
741 template<typename Derived>
742 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
744 {
745  resize(newSize);
746  return setConstant(Scalar(1));
747 }
748 
759 template<typename Derived>
760 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
762 {
763  resize(rows, cols);
764  return setConstant(Scalar(1));
765 }
766 
773 template<typename Derived>
774 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
776 {
777  return setOnes(rows, cols());
778 }
779 
786 template<typename Derived>
787 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
789 {
790  return setOnes(rows(), cols);
791 }
792 
793 // Identity:
794 
809 template<typename Derived>
810 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
812 {
813  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
814 }
815 
826 template<typename Derived>
827 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
829 {
831  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
832 }
833 
843 template<typename Derived>
845 (const RealScalar& prec) const
846 {
847  typename internal::nested_eval<Derived,1>::type self(derived());
848  for(Index j = 0; j < cols(); ++j)
849  {
850  for(Index i = 0; i < rows(); ++i)
851  {
852  if(i == j)
853  {
854  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
855  return false;
856  }
857  else
858  {
859  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
860  return false;
861  }
862  }
863  }
864  return true;
865 }
866 
867 namespace internal {
868 
869 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
870 struct setIdentity_impl
871 {
873  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
874  {
875  return m = Derived::Identity(m.rows(), m.cols());
876  }
877 };
878 
879 template<typename Derived>
880 struct setIdentity_impl<Derived, true>
881 {
883  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
884  {
885  m.setZero();
886  const Index size = numext::mini(m.rows(), m.cols());
887  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
888  return m;
889  }
890 };
891 
892 } // end namespace internal
893 
901 template<typename Derived>
903 {
904  return internal::setIdentity_impl<Derived>::run(derived());
905 }
906 
917 template<typename Derived>
919 {
920  derived().resize(rows, cols);
921  return setIdentity();
922 }
923 
930 template<typename Derived>
932 {
934  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
935 }
936 
945 template<typename Derived>
947 {
949  return BasisReturnType(SquareMatrixType::Identity(),i);
950 }
951 
958 template<typename Derived>
960 { return Derived::Unit(0); }
961 
968 template<typename Derived>
970 { return Derived::Unit(1); }
971 
978 template<typename Derived>
980 { return Derived::Unit(2); }
981 
988 template<typename Derived>
990 { return Derived::Unit(3); }
991 
1000 template<typename Derived>
1002 {
1004  eigen_assert(i<size());
1005  derived().setZero();
1006  derived().coeffRef(i) = Scalar(1);
1007  return derived();
1008 }
1009 
1019 template<typename Derived>
1020 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
1021 {
1023  eigen_assert(i<newSize);
1024  derived().resize(newSize);
1025  return setUnit(i);
1026 }
1027 
1028 } // end namespace Eigen
1029 
1030 #endif // EIGEN_CWISE_NULLARY_OP_H
Matrix3f m
v setLinSpaced(5, 0.5f, 1.5f)
#define EIGEN_DEPRECATED
Definition: Macros.h:923
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
v resize(3)
v setConstant(3, 5)
m setIdentity(3, 3)
v setOnes(3)
v setZero(3)
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:41
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
Generic expression of a matrix where all coefficients are defined by a functor.
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
EIGEN_CONSTEXPR Index rows() const
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
CwiseNullaryOp(Index rows, Index cols, const NullaryOp &func=NullaryOp())
const NullaryOp m_functor
internal::dense_xpr_base< CwiseNullaryOp >::type Base
EIGEN_CONSTEXPR Index cols() const
const NullaryOp & functor() const
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Derived & setOnes()
static const ConstantReturnType Ones()
static const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
static const ConstantReturnType Zero()
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
void fill(const Scalar &value)
Derived & setEqualSpaced(Index size, const Scalar &low, const Scalar &step)
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
static const RandomAccessEqualSpacedReturnType EqualSpaced(Index size, const Scalar &low, const Scalar &step)
Derived & setConstant(const Scalar &value)
Derived & setZero()
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
static const BasisReturnType UnitY()
Derived & setIdentity()
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
static const BasisReturnType UnitX()
static const IdentityReturnType Identity()
static const BasisReturnType UnitZ()
static const BasisReturnType Unit(Index size, Index i)
Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
static const BasisReturnType UnitW()
internal::traits< Derived >::Scalar Scalar
Derived & setOnes(Index size)
Derived & setConstant(Index size, const Scalar &val)
Derived & setZero(Index size)
const unsigned int RowMajorBit
Definition: Constants.h:68
bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
: InteropHeaders
Definition: Core:139
NoChange_t
Definition: Constants.h:362
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Sequential_t
Definition: Constants.h:363
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41
std::ptrdiff_t j