Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.99 (git rev 199c5f2b47eb1f8e5a2d20e60f07e97cd95a6ba6)
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 namespace Eigen {
14 
15 namespace internal {
16 template<typename NullaryOp, typename PlainObjectType>
17 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
18 {
19  enum {
20  Flags = traits<PlainObjectType>::Flags & RowMajorBit
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 
64  typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
65  EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
66 
67  EIGEN_DEVICE_FUNC
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 
77  EIGEN_DEVICE_FUNC
78  EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
79  EIGEN_DEVICE_FUNC
80  EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
81 
83  EIGEN_DEVICE_FUNC
84  const NullaryOp& functor() const { return m_functor; }
85 
86  protected:
87  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
88  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
89  const NullaryOp m_functor;
90 };
91 
92 
106 template<typename Derived>
107 template<typename CustomNullaryOp>
108 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
109 #ifndef EIGEN_PARSED_BY_DOXYGEN
110 const CwiseNullaryOp<CustomNullaryOp,typename DenseBase<Derived>::PlainObject>
111 #else
112 const CwiseNullaryOp<CustomNullaryOp,PlainObject>
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>
139 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
140 #ifndef EIGEN_PARSED_BY_DOXYGEN
142 #else
144 #endif
145 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
146 {
147  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
148  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
149  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
150 }
151 
161 template<typename Derived>
162 template<typename CustomNullaryOp>
163 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
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>
188 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
190 {
191  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
192 }
193 
209 template<typename Derived>
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
212 {
213  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
214 }
215 
225 template<typename Derived>
226 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
228 {
229  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
230  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
231 }
232 
242 template<typename Derived>
243 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
244 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
245 {
246  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
247  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
248 }
249 
254 template<typename Derived>
255 EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
256 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
257 {
258  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
259  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
260  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
261 }
262 
286 template<typename Derived>
287 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
288 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
289 {
290  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
291  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
292 }
293 
298 template<typename Derived>
299 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
301 {
302  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
303  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
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>
324 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
325 (const Scalar& val, const RealScalar& prec) const
326 {
327  return isApproxToConstant(val, prec);
328 }
329 
334 template<typename Derived>
335 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
336 {
337  setConstant(val);
338 }
339 
344 template<typename Derived>
345 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
346 {
347  return derived() = Constant(rows(), cols(), val);
348 }
349 
359 template<typename Derived>
360 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
362 {
363  resize(size);
364  return setConstant(val);
365 }
366 
378 template<typename Derived>
379 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
380 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
381 {
382  resize(rows, cols);
383  return setConstant(val);
384 }
385 
392 template<typename Derived>
393 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
394 PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols, const Scalar& val)
395 {
396  return setConstant(rows(), cols, val);
397 }
398 
405 template<typename Derived>
406 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
407 PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t, const Scalar& val)
408 {
409  return setConstant(rows, cols(), val);
410 }
411 
412 
429 template<typename Derived>
430 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
431 {
432  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
433  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
434 }
435 
449 template<typename Derived>
450 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
451 {
452  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
453  return setLinSpaced(size(), low, high);
454 }
455 
456 // zero:
457 
472 template<typename Derived>
473 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
475 {
476  return Constant(rows, cols, Scalar(0));
477 }
478 
495 template<typename Derived>
496 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
498 {
499  return Constant(size, Scalar(0));
500 }
501 
512 template<typename Derived>
513 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
515 {
516  return Constant(Scalar(0));
517 }
518 
527 template<typename Derived>
528 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
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>
546 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
547 {
548  return setConstant(Scalar(0));
549 }
550 
560 template<typename Derived>
561 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
563 {
564  resize(newSize);
565  return setConstant(Scalar(0));
566 }
567 
578 template<typename Derived>
579 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
581 {
582  resize(rows, cols);
583  return setConstant(Scalar(0));
584 }
585 
592 template<typename Derived>
593 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
595 {
596  return setZero(rows(), cols);
597 }
598 
605 template<typename Derived>
606 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
608 {
609  return setZero(rows, cols());
610 }
611 
612 // ones:
613 
628 template<typename Derived>
629 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
631 {
632  return Constant(rows, cols, Scalar(1));
633 }
634 
651 template<typename Derived>
652 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
654 {
655  return Constant(newSize, Scalar(1));
656 }
657 
668 template<typename Derived>
669 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
671 {
672  return Constant(Scalar(1));
673 }
674 
683 template<typename Derived>
684 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
685 (const RealScalar& prec) const
686 {
687  return isApproxToConstant(Scalar(1), prec);
688 }
689 
697 template<typename Derived>
698 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
699 {
700  return setConstant(Scalar(1));
701 }
702 
712 template<typename Derived>
713 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
715 {
716  resize(newSize);
717  return setConstant(Scalar(1));
718 }
719 
730 template<typename Derived>
731 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
733 {
734  resize(rows, cols);
735  return setConstant(Scalar(1));
736 }
737 
744 template<typename Derived>
745 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
747 {
748  return setOnes(rows, cols());
749 }
750 
757 template<typename Derived>
758 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
760 {
761  return setOnes(rows(), cols);
762 }
763 
764 // Identity:
765 
780 template<typename Derived>
781 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
783 {
784  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
785 }
786 
797 template<typename Derived>
798 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
800 {
801  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
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)>
841 struct setIdentity_impl
842 {
843  EIGEN_DEVICE_FUNC
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 {
853  EIGEN_DEVICE_FUNC
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>
873 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
874 {
875  return internal::setIdentity_impl<Derived>::run(derived());
876 }
877 
888 template<typename Derived>
889 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
890 {
891  derived().resize(rows, cols);
892  return setIdentity();
893 }
894 
901 template<typename Derived>
902 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
903 {
904  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
905  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
906 }
907 
916 template<typename Derived>
917 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
918 {
919  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
920  return BasisReturnType(SquareMatrixType::Identity(),i);
921 }
922 
929 template<typename Derived>
930 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
931 { return Derived::Unit(0); }
932 
939 template<typename Derived>
940 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
941 { return Derived::Unit(1); }
942 
949 template<typename Derived>
950 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
951 { return Derived::Unit(2); }
952 
959 template<typename Derived>
960 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
961 { return Derived::Unit(3); }
962 
971 template<typename Derived>
972 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
973 {
974  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
975  eigen_assert(i<size());
976  derived().setZero();
977  derived().coeffRef(i) = Scalar(1);
978  return derived();
979 }
980 
990 template<typename Derived>
991 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
992 {
993  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
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
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:61
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:244
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:325
Derived & setOnes()
Definition: CwiseNullaryOp.h:698
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:670
static const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Definition: CwiseNullaryOp.h:114
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:514
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:430
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:189
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:335
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:685
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:345
Derived & setZero()
Definition: CwiseNullaryOp.h:546
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:528
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:310
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:940
Derived & setIdentity()
Definition: CwiseNullaryOp.h:873
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:816
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:930
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:799
static const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:950
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:902
Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:972
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:960
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:714
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:361
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
const unsigned int RowMajorBit
Definition: Constants.h:66
Namespace containing all symbols from the Eigen library.
Definition: Core:134
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:39