Eigen  3.3.90 (mercurial changeset af41d217c085)
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
109 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
110 {
111  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
112 }
113 
132 template<typename Derived>
133 template<typename CustomNullaryOp>
134 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
135 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
136 {
137  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
138  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
139  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
140 }
141 
151 template<typename Derived>
152 template<typename CustomNullaryOp>
153 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
154 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
155 {
156  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
157 }
158 
172 template<typename Derived>
173 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
175 {
176  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
177 }
178 
194 template<typename Derived>
195 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
197 {
198  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
199 }
200 
210 template<typename Derived>
211 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
213 {
214  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
215  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
216 }
217 
222 template<typename Derived>
223 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
224 DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
225 {
226  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
227  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
228 }
229 
234 template<typename Derived>
235 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
236 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
237 {
238  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
239  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
240  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
241 }
242 
266 template<typename Derived>
267 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
268 DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
269 {
270  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
271  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
272 }
273 
278 template<typename Derived>
279 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
281 {
282  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
283  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
284  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
285 }
286 
288 template<typename Derived>
289 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApproxToConstant
290 (const Scalar& val, const RealScalar& prec) const
291 {
292  typename internal::nested_eval<Derived,1>::type self(derived());
293  for(Index j = 0; j < cols(); ++j)
294  for(Index i = 0; i < rows(); ++i)
295  if(!internal::isApprox(self.coeff(i, j), val, prec))
296  return false;
297  return true;
298 }
299 
303 template<typename Derived>
304 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
305 (const Scalar& val, const RealScalar& prec) const
306 {
307  return isApproxToConstant(val, prec);
308 }
309 
314 template<typename Derived>
315 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
316 {
317  setConstant(val);
318 }
319 
324 template<typename Derived>
325 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
326 {
327  return derived() = Constant(rows(), cols(), val);
328 }
329 
339 template<typename Derived>
340 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
342 {
343  resize(size);
344  return setConstant(val);
345 }
346 
358 template<typename Derived>
359 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
360 PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
361 {
362  resize(rows, cols);
363  return setConstant(val);
364 }
365 
382 template<typename Derived>
383 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
384 {
385  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
386  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar>(low,high,newSize));
387 }
388 
402 template<typename Derived>
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
404 {
405  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
406  return setLinSpaced(size(), low, high);
407 }
408 
409 // zero:
410 
425 template<typename Derived>
426 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
428 {
429  return Constant(rows, cols, Scalar(0));
430 }
431 
448 template<typename Derived>
449 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
451 {
452  return Constant(size, Scalar(0));
453 }
454 
465 template<typename Derived>
466 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
468 {
469  return Constant(Scalar(0));
470 }
471 
480 template<typename Derived>
481 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
482 {
483  typename internal::nested_eval<Derived,1>::type self(derived());
484  for(Index j = 0; j < cols(); ++j)
485  for(Index i = 0; i < rows(); ++i)
486  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
487  return false;
488  return true;
489 }
490 
498 template<typename Derived>
499 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
500 {
501  return setConstant(Scalar(0));
502 }
503 
513 template<typename Derived>
514 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
516 {
517  resize(newSize);
518  return setConstant(Scalar(0));
519 }
520 
531 template<typename Derived>
532 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
534 {
535  resize(rows, cols);
536  return setConstant(Scalar(0));
537 }
538 
539 // ones:
540 
555 template<typename Derived>
556 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
558 {
559  return Constant(rows, cols, Scalar(1));
560 }
561 
578 template<typename Derived>
579 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
581 {
582  return Constant(newSize, Scalar(1));
583 }
584 
595 template<typename Derived>
596 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
598 {
599  return Constant(Scalar(1));
600 }
601 
610 template<typename Derived>
611 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
612 (const RealScalar& prec) const
613 {
614  return isApproxToConstant(Scalar(1), prec);
615 }
616 
624 template<typename Derived>
625 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
626 {
627  return setConstant(Scalar(1));
628 }
629 
639 template<typename Derived>
640 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
642 {
643  resize(newSize);
644  return setConstant(Scalar(1));
645 }
646 
657 template<typename Derived>
658 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
660 {
661  resize(rows, cols);
662  return setConstant(Scalar(1));
663 }
664 
665 // Identity:
666 
681 template<typename Derived>
682 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
684 {
685  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
686 }
687 
698 template<typename Derived>
699 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
701 {
702  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
703  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
704 }
705 
715 template<typename Derived>
717 (const RealScalar& prec) const
718 {
719  typename internal::nested_eval<Derived,1>::type self(derived());
720  for(Index j = 0; j < cols(); ++j)
721  {
722  for(Index i = 0; i < rows(); ++i)
723  {
724  if(i == j)
725  {
726  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
727  return false;
728  }
729  else
730  {
731  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
732  return false;
733  }
734  }
735  }
736  return true;
737 }
738 
739 namespace internal {
740 
741 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
742 struct setIdentity_impl
743 {
744  EIGEN_DEVICE_FUNC
745  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
746  {
747  return m = Derived::Identity(m.rows(), m.cols());
748  }
749 };
750 
751 template<typename Derived>
752 struct setIdentity_impl<Derived, true>
753 {
754  EIGEN_DEVICE_FUNC
755  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
756  {
757  m.setZero();
758  const Index size = numext::mini(m.rows(), m.cols());
759  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
760  return m;
761  }
762 };
763 
764 } // end namespace internal
765 
773 template<typename Derived>
774 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
775 {
776  return internal::setIdentity_impl<Derived>::run(derived());
777 }
778 
789 template<typename Derived>
790 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
791 {
792  derived().resize(rows, cols);
793  return setIdentity();
794 }
795 
802 template<typename Derived>
803 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
804 {
805  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
806  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
807 }
808 
817 template<typename Derived>
818 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
819 {
820  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
821  return BasisReturnType(SquareMatrixType::Identity(),i);
822 }
823 
830 template<typename Derived>
831 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
832 { return Derived::Unit(0); }
833 
840 template<typename Derived>
841 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
842 { return Derived::Unit(1); }
843 
850 template<typename Derived>
851 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
852 { return Derived::Unit(2); }
853 
860 template<typename Derived>
861 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
862 { return Derived::Unit(3); }
863 
872 template<typename Derived>
873 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
874 {
875  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
876  eigen_assert(i<size());
877  derived().setZero();
878  derived().coeffRef(i) = Scalar(1);
879  return derived();
880 }
881 
891 template<typename Derived>
892 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
893 {
894  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
895  eigen_assert(i<newSize);
896  derived().resize(newSize);
897  return setUnit(i);
898 }
899 
900 } // end namespace Eigen
901 
902 #endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:60
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:861
static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:224
Derived & setIdentity()
Definition: CwiseNullaryOp.h:774
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:38
const unsigned int RowMajorBit
Definition: Constants.h:65
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:174
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:641
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:515
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:831
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:481
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:467
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:315
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:612
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:841
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:341
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:803
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:717
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:597
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:383
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:305
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:700
Derived & setZero()
Definition: CwiseNullaryOp.h:499
Derived & setOnes()
Definition: CwiseNullaryOp.h:625
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:290
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:325
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:851
Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:873