Eigen  3.2.93
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 
42 template<typename NullaryOp, typename PlainObjectType>
43 class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
44 {
45  public:
46 
47  typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
48  EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
49 
50  EIGEN_DEVICE_FUNC
51  CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
52  : m_rows(rows), m_cols(cols), m_functor(func)
53  {
54  eigen_assert(rows >= 0
55  && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
56  && cols >= 0
57  && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
58  }
59 
60  EIGEN_DEVICE_FUNC
61  EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
62  EIGEN_DEVICE_FUNC
63  EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
64 
65  EIGEN_DEVICE_FUNC
66  EIGEN_STRONG_INLINE const Scalar coeff(Index rowId, Index colId) const
67  {
68  return m_functor(rowId, colId);
69  }
70 
71  template<int LoadMode>
72  EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
73  {
74  return m_functor.packetOp(rowId, colId);
75  }
76 
77  EIGEN_DEVICE_FUNC
78  EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
79  {
80  return m_functor(index);
81  }
82 
83  template<int LoadMode>
84  EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
85  {
86  return m_functor.packetOp(index);
87  }
88 
90  EIGEN_DEVICE_FUNC
91  const NullaryOp& functor() const { return m_functor; }
92 
93  protected:
94  const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
95  const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
96  const NullaryOp m_functor;
97 };
98 
99 
113 template<typename Derived>
114 template<typename CustomNullaryOp>
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>
142 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
143 {
144  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
145  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
146  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
147 }
148 
158 template<typename Derived>
159 template<typename CustomNullaryOp>
161 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
162 {
163  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
164 }
165 
179 template<typename Derived>
180 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
182 {
183  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
184 }
185 
201 template<typename Derived>
202 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
204 {
205  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
206 }
207 
217 template<typename Derived>
218 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
220 {
221  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
222  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
223 }
224 
242 template<typename Derived>
243 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
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,PacketScalar,false>(low,high,size));
248 }
249 
254 template<typename Derived>
255 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
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,PacketScalar,false>(low,high,Derived::SizeAtCompileTime));
261 }
262 
276 template<typename Derived>
277 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
278 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
279 {
280  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
281  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,size));
282 }
283 
288 template<typename Derived>
289 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
291 {
292  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
293  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
294  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar,true>(low,high,Derived::SizeAtCompileTime));
295 }
296 
298 template<typename Derived>
300 (const Scalar& val, const RealScalar& prec) const
301 {
302  typename internal::nested_eval<Derived,1>::type self(derived());
303  for(Index j = 0; j < cols(); ++j)
304  for(Index i = 0; i < rows(); ++i)
305  if(!internal::isApprox(self.coeff(i, j), val, prec))
306  return false;
307  return true;
308 }
309 
313 template<typename Derived>
315 (const Scalar& val, const RealScalar& prec) const
316 {
317  return isApproxToConstant(val, prec);
318 }
319 
324 template<typename Derived>
325 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
326 {
327  setConstant(val);
328 }
329 
334 template<typename Derived>
335 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
336 {
337  return derived() = Constant(rows(), cols(), val);
338 }
339 
349 template<typename Derived>
350 EIGEN_STRONG_INLINE Derived&
352 {
353  resize(size);
354  return setConstant(val);
355 }
356 
368 template<typename Derived>
369 EIGEN_STRONG_INLINE Derived&
370 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
371 {
372  resize(rows, cols);
373  return setConstant(val);
374 }
375 
389 template<typename Derived>
390 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
391 {
392  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
393  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar,false>(low,high,newSize));
394 }
395 
406 template<typename Derived>
407 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
408 {
409  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
410  return setLinSpaced(size(), low, high);
411 }
412 
413 // zero:
414 
429 template<typename Derived>
430 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
432 {
433  return Constant(rows, cols, Scalar(0));
434 }
435 
452 template<typename Derived>
453 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
455 {
456  return Constant(size, Scalar(0));
457 }
458 
469 template<typename Derived>
470 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
472 {
473  return Constant(Scalar(0));
474 }
475 
484 template<typename Derived>
485 bool DenseBase<Derived>::isZero(const RealScalar& prec) const
486 {
487  typename internal::nested_eval<Derived,1>::type self(derived());
488  for(Index j = 0; j < cols(); ++j)
489  for(Index i = 0; i < rows(); ++i)
490  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
491  return false;
492  return true;
493 }
494 
502 template<typename Derived>
503 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
504 {
505  return setConstant(Scalar(0));
506 }
507 
517 template<typename Derived>
518 EIGEN_STRONG_INLINE Derived&
520 {
521  resize(newSize);
522  return setConstant(Scalar(0));
523 }
524 
535 template<typename Derived>
536 EIGEN_STRONG_INLINE Derived&
538 {
539  resize(rows, cols);
540  return setConstant(Scalar(0));
541 }
542 
543 // ones:
544 
559 template<typename Derived>
560 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
562 {
563  return Constant(rows, cols, Scalar(1));
564 }
565 
582 template<typename Derived>
583 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
585 {
586  return Constant(newSize, Scalar(1));
587 }
588 
599 template<typename Derived>
600 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
602 {
603  return Constant(Scalar(1));
604 }
605 
614 template<typename Derived>
616 (const RealScalar& prec) const
617 {
618  return isApproxToConstant(Scalar(1), prec);
619 }
620 
628 template<typename Derived>
629 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
630 {
631  return setConstant(Scalar(1));
632 }
633 
643 template<typename Derived>
644 EIGEN_STRONG_INLINE Derived&
646 {
647  resize(newSize);
648  return setConstant(Scalar(1));
649 }
650 
661 template<typename Derived>
662 EIGEN_STRONG_INLINE Derived&
664 {
665  resize(rows, cols);
666  return setConstant(Scalar(1));
667 }
668 
669 // Identity:
670 
685 template<typename Derived>
686 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
688 {
689  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
690 }
691 
702 template<typename Derived>
703 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
705 {
706  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
707  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
708 }
709 
719 template<typename Derived>
721 (const RealScalar& prec) const
722 {
723  typename internal::nested_eval<Derived,1>::type self(derived());
724  for(Index j = 0; j < cols(); ++j)
725  {
726  for(Index i = 0; i < rows(); ++i)
727  {
728  if(i == j)
729  {
730  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
731  return false;
732  }
733  else
734  {
735  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
736  return false;
737  }
738  }
739  }
740  return true;
741 }
742 
743 namespace internal {
744 
745 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
746 struct setIdentity_impl
747 {
748  EIGEN_DEVICE_FUNC
749  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
750  {
751  return m = Derived::Identity(m.rows(), m.cols());
752  }
753 };
754 
755 template<typename Derived>
756 struct setIdentity_impl<Derived, true>
757 {
758  EIGEN_DEVICE_FUNC
759  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
760  {
761  m.setZero();
762  const Index size = (std::min)(m.rows(), m.cols());
763  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
764  return m;
765  }
766 };
767 
768 } // end namespace internal
769 
777 template<typename Derived>
778 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
779 {
780  return internal::setIdentity_impl<Derived>::run(derived());
781 }
782 
793 template<typename Derived>
794 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
795 {
796  derived().resize(rows, cols);
797  return setIdentity();
798 }
799 
806 template<typename Derived>
807 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
808 {
809  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
810  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
811 }
812 
821 template<typename Derived>
823 {
824  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
825  return BasisReturnType(SquareMatrixType::Identity(),i);
826 }
827 
834 template<typename Derived>
836 { return Derived::Unit(0); }
837 
844 template<typename Derived>
846 { return Derived::Unit(1); }
847 
854 template<typename Derived>
856 { return Derived::Unit(2); }
857 
864 template<typename Derived>
866 { return Derived::Unit(3); }
867 
868 } // end namespace Eigen
869 
870 #endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:43
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:865
static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:244
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Derived & setIdentity()
Definition: CwiseNullaryOp.h:778
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
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:181
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:645
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:519
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:835
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:485
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:471
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:325
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:616
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:845
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:351
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:807
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:721
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:601
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:390
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:91
Definition: Eigen_Colamd.h:50
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:315
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:704
Derived & setZero()
Definition: CwiseNullaryOp.h:503
Derived & setOnes()
Definition: CwiseNullaryOp.h:629
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:300
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:335
const int Dynamic
Definition: Constants.h:21
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
static const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:855