Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
CwiseNullaryOp.h
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;
67  EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
68 
69  EIGEN_DEVICE_FUNC
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 
85  EIGEN_DEVICE_FUNC
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
112 const CwiseNullaryOp<CustomNullaryOp,typename DenseBase<Derived>::PlainObject>
113 #else
114 const CwiseNullaryOp<CustomNullaryOp,PlainObject>
115 #endif
116 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
117 {
118  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
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 {
149  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
150  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
151  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, 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 {
231  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
232  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
233 }
234 
244 template<typename Derived>
245 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
246 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
247 {
248  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
249  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
250 }
251 
256 template<typename Derived>
257 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
258 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
259 {
260  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
261  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
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 {
292  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
293  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
294 }
295 
300 template<typename Derived>
301 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
303 {
304  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
305  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
306  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
307 }
308 
310 template<typename Derived>
312 (const Scalar& val, const RealScalar& prec) const
313 {
314  typename internal::nested_eval<Derived,1>::type self(derived());
315  for(Index j = 0; j < cols(); ++j)
316  for(Index i = 0; i < rows(); ++i)
317  if(!internal::isApprox(self.coeff(i, j), val, prec))
318  return false;
319  return true;
320 }
321 
325 template<typename Derived>
326 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
327 (const Scalar& val, const RealScalar& prec) const
328 {
329  return isApproxToConstant(val, prec);
330 }
331 
336 template<typename Derived>
337 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
338 {
339  setConstant(val);
340 }
341 
346 template<typename Derived>
347 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
348 {
349  return derived() = Constant(rows(), cols(), val);
350 }
351 
361 template<typename Derived>
362 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
364 {
365  resize(size);
366  return setConstant(val);
367 }
368 
380 template<typename Derived>
381 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
382 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
383 {
384  resize(rows, cols);
385  return setConstant(val);
386 }
387 
394 template<typename Derived>
395 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
396 PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols, const Scalar& val)
397 {
398  return setConstant(rows(), cols, val);
399 }
400 
407 template<typename Derived>
408 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
409 PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t, const Scalar& val)
410 {
411  return setConstant(rows, cols(), val);
412 }
413 
414 
431 template<typename Derived>
432 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
433 {
434  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
435  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
436 }
437 
451 template<typename Derived>
452 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
453 {
454  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
455  return setLinSpaced(size(), low, high);
456 }
457 
458 // zero:
459 
474 template<typename Derived>
475 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
477 {
478  return Constant(rows, cols, Scalar(0));
479 }
480 
497 template<typename Derived>
498 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
500 {
501  return Constant(size, Scalar(0));
502 }
503 
514 template<typename Derived>
515 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
517 {
518  return Constant(Scalar(0));
519 }
520 
529 template<typename Derived>
530 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
531 {
532  typename internal::nested_eval<Derived,1>::type self(derived());
533  for(Index j = 0; j < cols(); ++j)
534  for(Index i = 0; i < rows(); ++i)
535  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
536  return false;
537  return true;
538 }
539 
547 template<typename Derived>
548 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
549 {
550  return setConstant(Scalar(0));
551 }
552 
562 template<typename Derived>
563 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
565 {
566  resize(newSize);
567  return setConstant(Scalar(0));
568 }
569 
580 template<typename Derived>
581 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
583 {
584  resize(rows, cols);
585  return setConstant(Scalar(0));
586 }
587 
594 template<typename Derived>
595 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
597 {
598  return setZero(rows(), cols);
599 }
600 
607 template<typename Derived>
608 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
610 {
611  return setZero(rows, cols());
612 }
613 
614 // ones:
615 
630 template<typename Derived>
631 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
633 {
634  return Constant(rows, cols, Scalar(1));
635 }
636 
653 template<typename Derived>
654 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
656 {
657  return Constant(newSize, Scalar(1));
658 }
659 
670 template<typename Derived>
671 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
673 {
674  return Constant(Scalar(1));
675 }
676 
685 template<typename Derived>
686 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
687 (const RealScalar& prec) const
688 {
689  return isApproxToConstant(Scalar(1), prec);
690 }
691 
699 template<typename Derived>
700 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
701 {
702  return setConstant(Scalar(1));
703 }
704 
714 template<typename Derived>
715 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
717 {
718  resize(newSize);
719  return setConstant(Scalar(1));
720 }
721 
732 template<typename Derived>
733 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
735 {
736  resize(rows, cols);
737  return setConstant(Scalar(1));
738 }
739 
746 template<typename Derived>
747 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
749 {
750  return setOnes(rows, cols());
751 }
752 
759 template<typename Derived>
760 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
762 {
763  return setOnes(rows(), cols);
764 }
765 
766 // Identity:
767 
782 template<typename Derived>
783 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
785 {
786  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
787 }
788 
799 template<typename Derived>
800 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
802 {
803  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
804  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
805 }
806 
816 template<typename Derived>
818 (const RealScalar& prec) const
819 {
820  typename internal::nested_eval<Derived,1>::type self(derived());
821  for(Index j = 0; j < cols(); ++j)
822  {
823  for(Index i = 0; i < rows(); ++i)
824  {
825  if(i == j)
826  {
827  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
828  return false;
829  }
830  else
831  {
832  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
833  return false;
834  }
835  }
836  }
837  return true;
838 }
839 
840 namespace internal {
841 
842 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
843 struct setIdentity_impl
844 {
845  EIGEN_DEVICE_FUNC
846  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
847  {
848  return m = Derived::Identity(m.rows(), m.cols());
849  }
850 };
851 
852 template<typename Derived>
853 struct setIdentity_impl<Derived, true>
854 {
855  EIGEN_DEVICE_FUNC
856  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
857  {
858  m.setZero();
859  const Index size = numext::mini(m.rows(), m.cols());
860  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
861  return m;
862  }
863 };
864 
865 } // end namespace internal
866 
874 template<typename Derived>
875 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
876 {
877  return internal::setIdentity_impl<Derived>::run(derived());
878 }
879 
890 template<typename Derived>
891 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
892 {
893  derived().resize(rows, cols);
894  return setIdentity();
895 }
896 
903 template<typename Derived>
904 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
905 {
906  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
907  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
908 }
909 
918 template<typename Derived>
919 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
920 {
921  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
922  return BasisReturnType(SquareMatrixType::Identity(),i);
923 }
924 
931 template<typename Derived>
932 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
933 { return Derived::Unit(0); }
934 
941 template<typename Derived>
942 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
943 { return Derived::Unit(1); }
944 
951 template<typename Derived>
952 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
953 { return Derived::Unit(2); }
954 
961 template<typename Derived>
962 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
963 { return Derived::Unit(3); }
964 
973 template<typename Derived>
974 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
975 {
976  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
977  eigen_assert(i<size());
978  derived().setZero();
979  derived().coeffRef(i) = Scalar(1);
980  return derived();
981 }
982 
992 template<typename Derived>
993 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
994 {
995  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
996  eigen_assert(i<newSize);
997  derived().resize(newSize);
998  return setUnit(i);
999 }
1000 
1001 } // end namespace Eigen
1002 
1003 #endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:63
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:86
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)
Definition: CwiseNullaryOp.h:246
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:327
Derived & setOnes()
Definition: CwiseNullaryOp.h:700
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:672
static const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Definition: CwiseNullaryOp.h:116
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:516
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:432
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:191
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:337
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:687
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:347
Derived & setZero()
Definition: CwiseNullaryOp.h:548
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:530
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:312
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:942
Derived & setIdentity()
Definition: CwiseNullaryOp.h:875
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:818
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:932
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:801
static const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:952
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:904
Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:974
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:962
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:716
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:363
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:564
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41