Eigen  3.2.92
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 
33 namespace internal {
34 template<typename NullaryOp, typename PlainObjectType>
35 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
36 {
37  enum {
38  Flags = traits<PlainObjectType>::Flags & RowMajorBit
39  };
40 };
41 }
42 
43 template<typename NullaryOp, typename PlainObjectType>
44 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
45 {
46  public:
47 
48  typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
49  EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
50 
51  EIGEN_DEVICE_FUNC
52  CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
53  : m_rows(rows), m_cols(cols), m_functor(func)
54  {
55  eigen_assert(rows >= 0
56  && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
57  && cols >= 0
58  && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
59  }
60 
61  EIGEN_DEVICE_FUNC
62  EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
63  EIGEN_DEVICE_FUNC
64  EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
65 
66  EIGEN_DEVICE_FUNC
67  EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
68  {
69  return m_functor(rowId, colId);
70  }
71 
72  template<int LoadMode>
73  EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
74  {
75  return m_functor.packetOp(rowId, colId);
76  }
77 
78  EIGEN_DEVICE_FUNC
79  EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
80  {
81  return m_functor(index);
82  }
83 
84  template<int LoadMode>
85  EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
86  {
87  return m_functor.packetOp(index);
88  }
89 
91  EIGEN_DEVICE_FUNC
92  const NullaryOp& functor() const { return m_functor; }
93 
94  protected:
95  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
96  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
97  const NullaryOp m_functor;
98 };
99 
100 
114 template<typename Derived>
115 template<typename CustomNullaryOp>
116 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
117 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
118 {
119  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
120 }
121 
140 template<typename Derived>
141 template<typename CustomNullaryOp>
143 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
144 {
145  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
146  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
147  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
148 }
149 
159 template<typename Derived>
160 template<typename CustomNullaryOp>
162 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
163 {
164  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
165 }
166 
180 template<typename Derived>
181 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
182 DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
183 {
184  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
185 }
186 
202 template<typename Derived>
203 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
204 DenseBase<Derived>::Constant(Index size, const Scalar& value)
205 {
206  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
207 }
208 
218 template<typename Derived>
219 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
221 {
222  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
223  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
224 }
225 
243 template<typename Derived>
244 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
245 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
246 {
247  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
248  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,size));
249 }
250 
255 template<typename Derived>
256 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
257 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
258 {
259  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
260  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
261  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,Derived::SizeAtCompileTime));
262 }
263 
277 template<typename Derived>
278 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
279 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
280 {
281  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
282  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,size));
283 }
284 
289 template<typename Derived>
290 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
292 {
293  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
294  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
295  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,Derived::SizeAtCompileTime));
296 }
297 
299 template<typename Derived>
301 (const Scalar& val, const RealScalar& prec) const
302 {
303  typename internal::nested_eval<Derived,1>::type self(derived());
304  for(Index j = 0; j < cols(); ++j)
305  for(Index i = 0; i < rows(); ++i)
306  if(!internal::isApprox(self.coeff(i, j), val, prec))
307  return false;
308  return true;
309 }
310 
314 template<typename Derived>
316 (const Scalar& val, const RealScalar& prec) const
317 {
318  return isApproxToConstant(val, prec);
319 }
320 
325 template<typename Derived>
326 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
327 {
328  setConstant(val);
329 }
330 
335 template<typename Derived>
336 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
337 {
338  return derived() = Constant(rows(), cols(), val);
339 }
340 
350 template<typename Derived>
351 EIGEN_STRONG_INLINE Derived&
352 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
353 {
354  resize(size);
355  return setConstant(val);
356 }
357 
369 template<typename Derived>
370 EIGEN_STRONG_INLINE Derived&
371 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
372 {
373  resize(rows, cols);
374  return setConstant(val);
375 }
376 
390 template<typename Derived>
391 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
392 {
393  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
394  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,newSize));
395 }
396 
407 template<typename Derived>
408 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
409 {
410  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
411  return setLinSpaced(size(), low, high);
412 }
413 
414 // zero:
415 
430 template<typename Derived>
431 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
432 DenseBase<Derived>::Zero(Index rows, Index cols)
433 {
434  return Constant(rows, cols, Scalar(0));
435 }
436 
453 template<typename Derived>
454 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
456 {
457  return Constant(size, Scalar(0));
458 }
459 
470 template<typename Derived>
471 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
473 {
474  return Constant(Scalar(0));
475 }
476 
485 template<typename Derived>
486 bool DenseBase<Derived>::isZero(const RealScalar& prec) const
487 {
488  typename internal::nested_eval<Derived,1>::type self(derived());
489  for(Index j = 0; j < cols(); ++j)
490  for(Index i = 0; i < rows(); ++i)
491  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
492  return false;
493  return true;
494 }
495 
503 template<typename Derived>
504 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
505 {
506  return setConstant(Scalar(0));
507 }
508 
518 template<typename Derived>
519 EIGEN_STRONG_INLINE Derived&
521 {
522  resize(newSize);
523  return setConstant(Scalar(0));
524 }
525 
536 template<typename Derived>
537 EIGEN_STRONG_INLINE Derived&
538 PlainObjectBase<Derived>::setZero(Index rows, Index cols)
539 {
540  resize(rows, cols);
541  return setConstant(Scalar(0));
542 }
543 
544 // ones:
545 
560 template<typename Derived>
561 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
562 DenseBase<Derived>::Ones(Index rows, Index cols)
563 {
564  return Constant(rows, cols, Scalar(1));
565 }
566 
583 template<typename Derived>
584 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
586 {
587  return Constant(newSize, Scalar(1));
588 }
589 
600 template<typename Derived>
601 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
603 {
604  return Constant(Scalar(1));
605 }
606 
615 template<typename Derived>
617 (const RealScalar& prec) const
618 {
619  return isApproxToConstant(Scalar(1), prec);
620 }
621 
629 template<typename Derived>
630 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
631 {
632  return setConstant(Scalar(1));
633 }
634 
644 template<typename Derived>
645 EIGEN_STRONG_INLINE Derived&
647 {
648  resize(newSize);
649  return setConstant(Scalar(1));
650 }
651 
662 template<typename Derived>
663 EIGEN_STRONG_INLINE Derived&
664 PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
665 {
666  resize(rows, cols);
667  return setConstant(Scalar(1));
668 }
669 
670 // Identity:
671 
686 template<typename Derived>
687 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
688 MatrixBase<Derived>::Identity(Index rows, Index cols)
689 {
690  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
691 }
692 
703 template<typename Derived>
704 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
706 {
707  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
708  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
709 }
710 
720 template<typename Derived>
722 (const RealScalar& prec) const
723 {
724  typename internal::nested_eval<Derived,1>::type self(derived());
725  for(Index j = 0; j < cols(); ++j)
726  {
727  for(Index i = 0; i < rows(); ++i)
728  {
729  if(i == j)
730  {
731  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
732  return false;
733  }
734  else
735  {
736  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
737  return false;
738  }
739  }
740  }
741  return true;
742 }
743 
744 namespace internal {
745 
746 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
747 struct setIdentity_impl
748 {
749  EIGEN_DEVICE_FUNC
750  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
751  {
752  return m = Derived::Identity(m.rows(), m.cols());
753  }
754 };
755 
756 template<typename Derived>
757 struct setIdentity_impl<Derived, true>
758 {
759  EIGEN_DEVICE_FUNC
760  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
761  {
762  m.setZero();
763  const Index size = (std::min)(m.rows(), m.cols());
764  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
765  return m;
766  }
767 };
768 
769 } // end namespace internal
770 
778 template<typename Derived>
779 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
780 {
781  return internal::setIdentity_impl<Derived>::run(derived());
782 }
783 
794 template<typename Derived>
795 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
796 {
797  derived().resize(rows, cols);
798  return setIdentity();
799 }
800 
807 template<typename Derived>
808 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
809 {
810  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
811  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
812 }
813 
822 template<typename Derived>
823 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
824 {
825  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
826  return BasisReturnType(SquareMatrixType::Identity(),i);
827 }
828 
835 template<typename Derived>
837 { return Derived::Unit(0); }
838 
845 template<typename Derived>
847 { return Derived::Unit(1); }
848 
855 template<typename Derived>
857 { return Derived::Unit(2); }
858 
865 template<typename Derived>
867 { return Derived::Unit(3); }
868 
869 } // end namespace Eigen
870 
871 #endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:44
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:68
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:866
static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition: CwiseNullaryOp.h:245
Definition: LDLT.h:16
Derived & setIdentity()
Definition: CwiseNullaryOp.h:779
const unsigned int RowMajorBit
Definition: Constants.h:61
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:41
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:182
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:646
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:520
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:836
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:486
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:472
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:326
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:617
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:846
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:808
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:722
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:602
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition: CwiseNullaryOp.h:391
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:92
Definition: Eigen_Colamd.h:54
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:316
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:705
Derived & setZero()
Definition: CwiseNullaryOp.h:504
Derived & setOnes()
Definition: CwiseNullaryOp.h:630
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:301
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:336
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
static const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:856
Derived & setConstant(Index size, const Scalar &value)
Definition: CwiseNullaryOp.h:352