Path Tracer
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 {
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 
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:
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 {
192 }
193 
209 template<typename Derived>
210 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
212 {
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)
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)
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 
402 template<typename Derived>
403 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
404 {
405  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
406  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
407 }
408 
422 template<typename Derived>
423 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
424 {
425  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
426  return setLinSpaced(size(), low, high);
427 }
428 
429 // zero:
430 
445 template<typename Derived>
446 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
448 {
449  return Constant(rows, cols, Scalar(0));
450 }
451 
468 template<typename Derived>
469 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
471 {
472  return Constant(size, Scalar(0));
473 }
474 
485 template<typename Derived>
486 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
488 {
489  return Constant(Scalar(0));
490 }
491 
500 template<typename Derived>
501 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
502 {
503  typename internal::nested_eval<Derived,1>::type self(derived());
504  for(Index j = 0; j < cols(); ++j)
505  for(Index i = 0; i < rows(); ++i)
506  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
507  return false;
508  return true;
509 }
510 
518 template<typename Derived>
519 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
520 {
521  return setConstant(Scalar(0));
522 }
523 
533 template<typename Derived>
534 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
536 {
537  resize(newSize);
538  return setConstant(Scalar(0));
539 }
540 
551 template<typename Derived>
552 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
554 {
555  resize(rows, cols);
556  return setConstant(Scalar(0));
557 }
558 
559 // ones:
560 
575 template<typename Derived>
576 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
578 {
579  return Constant(rows, cols, Scalar(1));
580 }
581 
598 template<typename Derived>
599 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
601 {
602  return Constant(newSize, Scalar(1));
603 }
604 
615 template<typename Derived>
616 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
618 {
619  return Constant(Scalar(1));
620 }
621 
630 template<typename Derived>
631 EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
632 (const RealScalar& prec) const
633 {
634  return isApproxToConstant(Scalar(1), prec);
635 }
636 
644 template<typename Derived>
645 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
646 {
647  return setConstant(Scalar(1));
648 }
649 
659 template<typename Derived>
660 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
662 {
663  resize(newSize);
664  return setConstant(Scalar(1));
665 }
666 
677 template<typename Derived>
678 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
680 {
681  resize(rows, cols);
682  return setConstant(Scalar(1));
683 }
684 
685 // Identity:
686 
701 template<typename Derived>
702 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
704 {
706 }
707 
718 template<typename Derived>
719 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
721 {
722  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
723  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
724 }
725 
735 template<typename Derived>
736 bool MatrixBase<Derived>::isIdentity
737 (const RealScalar& prec) const
738 {
739  typename internal::nested_eval<Derived,1>::type self(derived());
740  for(Index j = 0; j < cols(); ++j)
741  {
742  for(Index i = 0; i < rows(); ++i)
743  {
744  if(i == j)
745  {
746  if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
747  return false;
748  }
749  else
750  {
751  if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
752  return false;
753  }
754  }
755  }
756  return true;
757 }
758 
759 namespace internal {
760 
761 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
763 {
764  EIGEN_DEVICE_FUNC
765  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
766  {
767  return m = Derived::Identity(m.rows(), m.cols());
768  }
769 };
770 
771 template<typename Derived>
772 struct setIdentity_impl<Derived, true>
773 {
774  EIGEN_DEVICE_FUNC
775  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
776  {
777  m.setZero();
778  const Index size = numext::mini(m.rows(), m.cols());
779  for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
780  return m;
781  }
782 };
783 
784 } // end namespace internal
785 
793 template<typename Derived>
794 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
795 {
797 }
798 
809 template<typename Derived>
810 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
811 {
812  derived().resize(rows, cols);
813  return setIdentity();
814 }
815 
822 template<typename Derived>
823 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
824 {
825  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
826  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
827 }
828 
837 template<typename Derived>
838 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
839 {
840  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
841  return BasisReturnType(SquareMatrixType::Identity(),i);
842 }
843 
850 template<typename Derived>
851 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
852 { return Derived::Unit(0); }
853 
860 template<typename Derived>
861 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
862 { return Derived::Unit(1); }
863 
870 template<typename Derived>
871 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
872 { return Derived::Unit(2); }
873 
880 template<typename Derived>
881 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
882 { return Derived::Unit(3); }
883 
892 template<typename Derived>
893 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
894 {
895  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
896  eigen_assert(i<size());
897  derived().setZero();
898  derived().coeffRef(i) = Scalar(1);
899  return derived();
900 }
901 
911 template<typename Derived>
912 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
913 {
914  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
915  eigen_assert(i<newSize);
916  derived().resize(newSize);
917  return setUnit(i);
918 }
919 
920 } // end namespace Eigen
921 
922 #endif // EIGEN_CWISE_NULLARY_OP_H
Eigen::internal::variable_if_dynamic< Index, RowsAtCompileTime >
Eigen::DenseBase::Constant
static EIGEN_DEVICE_FUNC const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:189
Eigen::MatrixBase::UnitX
static EIGEN_DEVICE_FUNC const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:851
Eigen::internal::scalar_identity_op
Definition: NullaryFunctors.h:31
Eigen
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Eigen::DenseBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero()
Definition: CwiseNullaryOp.h:519
Eigen::DenseBase::isZero
EIGEN_DEVICE_FUNC bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:501
Eigen::Block
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:105
Eigen::internal::linspaced_op
Definition: NullaryFunctors.h:131
Eigen::DenseBase::LinSpaced
EIGEN_DEPRECATED static EIGEN_DEVICE_FUNC const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:244
Eigen::DenseBase::Ones
static EIGEN_DEVICE_FUNC const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:617
Eigen::DenseBase::Scalar
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:66
Eigen::internal::dense_xpr_base
Definition: XprHelper.h:492
Eigen::DenseBase::isApproxToConstant
EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:310
Eigen::DenseBase::setConstant
EIGEN_DEVICE_FUNC Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:345
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:65
Eigen::MatrixBase::Unit
static EIGEN_DEVICE_FUNC const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:823
Eigen::MatrixBase::UnitY
static EIGEN_DEVICE_FUNC const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:861
Eigen::DenseBase::setLinSpaced
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:403
Eigen::CwiseNullaryOp
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:61
Eigen::DenseBase::isOnes
EIGEN_DEVICE_FUNC bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:632
Eigen::PlainObjectBase::setConstant
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:361
Eigen::CwiseNullaryOp::functor
EIGEN_DEVICE_FUNC const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:84
Eigen::internal::true_type
Definition: Meta.h:63
Eigen::internal::scalar_constant_op
Definition: NullaryFunctors.h:18
Eigen::internal::setIdentity_impl
Definition: CwiseNullaryOp.h:763
Eigen::PlainObjectBase::setOnes
EIGEN_DEVICE_FUNC Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:661
Eigen::MatrixBase::UnitZ
static EIGEN_DEVICE_FUNC const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:871
Eigen::MatrixBase::Identity
static EIGEN_DEVICE_FUNC const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:720
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:21
Eigen::DenseBase::isConstant
EIGEN_DEVICE_FUNC bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:325
Eigen::DenseBase::fill
EIGEN_DEVICE_FUNC void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:335
Eigen::MatrixBase::setUnit
EIGEN_DEVICE_FUNC Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:893
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::DenseBase
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
Eigen::MatrixBase::UnitW
static EIGEN_DEVICE_FUNC const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:881
Eigen::DenseBase::setOnes
EIGEN_DEVICE_FUNC Derived & setOnes()
Definition: CwiseNullaryOp.h:645
Eigen::internal::no_assignment_operator
Definition: XprHelper.h:110
Eigen::PlainObjectBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:535
Eigen::MatrixBase
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
Eigen::MatrixBase::setIdentity
EIGEN_DEVICE_FUNC Derived & setIdentity()
Definition: CwiseNullaryOp.h:794
Eigen::DenseBase::Zero
static EIGEN_DEVICE_FUNC const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:487
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42