Eigen  3.4.90 (git rev a4098ac676528a83cfb73d4d26ce1b42ec05f47c)
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#include "./InternalHeaderCheck.h"
14
15namespace Eigen {
16
17namespace internal {
18template<typename NullaryOp, typename PlainObjectType>
19struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
20{
21 enum {
22 Flags = traits<PlainObjectType>::Flags & RowMajorBit
23 };
24};
25
26} // namespace internal
27
61template<typename NullaryOp, typename PlainObjectType>
62class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
63{
64 public:
65
66 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
67 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
68
69 EIGEN_DEVICE_FUNC
70 CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
71 : m_rows(rows), m_cols(cols), m_functor(func)
72 {
73 eigen_assert(rows >= 0
74 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
75 && cols >= 0
76 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
77 }
78
79 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
80 Index rows() const { return m_rows.value(); }
81 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
82 Index cols() const { return m_cols.value(); }
83
85 EIGEN_DEVICE_FUNC
86 const NullaryOp& functor() const { return m_functor; }
87
88 protected:
89 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
90 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
91 const NullaryOp m_functor;
92};
93
94
108template<typename Derived>
109template<typename CustomNullaryOp>
110EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
111#ifndef EIGEN_PARSED_BY_DOXYGEN
112const CwiseNullaryOp<CustomNullaryOp,typename DenseBase<Derived>::PlainObject>
113#else
114const CwiseNullaryOp<CustomNullaryOp,PlainObject>
115#endif
116DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
117{
118 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
119}
120
139template<typename Derived>
140template<typename CustomNullaryOp>
141EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
142#ifndef EIGEN_PARSED_BY_DOXYGEN
144#else
146#endif
147DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
148{
149 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
150 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
151 else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
152}
153
163template<typename Derived>
164template<typename CustomNullaryOp>
165EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
166#ifndef EIGEN_PARSED_BY_DOXYGEN
168#else
170#endif
171DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
172{
173 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
174}
175
189template<typename Derived>
190EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
192{
193 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
194}
195
211template<typename Derived>
212EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
214{
215 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
216}
217
227template<typename Derived>
228EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
230{
231 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
232 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
233}
234
244template<typename Derived>
245EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
246DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
247{
248 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
249 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
250}
251
256template<typename Derived>
257EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
258DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
260 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
261 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
262 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
264
288template<typename Derived>
289EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
290DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
291{
292 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
293 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low,high,size));
294}
295
300template<typename Derived>
301EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
303{
304 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
305 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
306 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar>(low,high,Derived::SizeAtCompileTime));
307}
308
310template<typename Derived>
312(const Scalar& val, const RealScalar& prec) const
313{
314 typename internal::nested_eval<Derived,1>::type self(derived());
315 for(Index j = 0; j < cols(); ++j)
316 for(Index i = 0; i < rows(); ++i)
317 if(!internal::isApprox(self.coeff(i, j), val, prec))
318 return false;
319 return true;
320}
321
325template<typename Derived>
326EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
327(const Scalar& val, const RealScalar& prec) const
329 return isApproxToConstant(val, prec);
330}
336template<typename Derived>
337EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
339 setConstant(val);
340}
341
346template<typename Derived>
347EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
349 return derived() = Constant(rows(), cols(), val);
361template<typename Derived>
362EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
364{
365 resize(size);
366 return setConstant(val);
367}
368
380template<typename Derived>
381EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
383{
384 resize(rows, cols);
385 return setConstant(val);
386}
387
394template<typename Derived>
395EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
396PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols, const Scalar& val)
397{
398 return setConstant(rows(), cols, val);
399}
400
407template<typename Derived>
408EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
409PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t, const Scalar& val)
410{
411 return setConstant(rows, cols(), val);
412}
413
414
431template<typename Derived>
432EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
433{
434 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
435 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low,high,newSize));
436}
437
451template<typename Derived>
452EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
453{
454 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
455 return setLinSpaced(size(), low, high);
456}
457
458// zero:
459
474template<typename Derived>
475EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
477{
478 return Constant(rows, cols, Scalar(0));
479}
480
497template<typename Derived>
498EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
500{
501 return Constant(size, Scalar(0));
502}
503
514template<typename Derived>
515EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
517{
518 return Constant(Scalar(0));
519}
520
529template<typename Derived>
530EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
531{
532 typename internal::nested_eval<Derived,1>::type self(derived());
533 for(Index j = 0; j < cols(); ++j)
534 for(Index i = 0; i < rows(); ++i)
535 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
536 return false;
537 return true;
538}
539
547template<typename Derived>
548EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
549{
550 return setConstant(Scalar(0));
551}
552
562template<typename Derived>
563EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
565{
566 resize(newSize);
567 return setConstant(Scalar(0));
568}
569
580template<typename Derived>
581EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
583{
584 resize(rows, cols);
585 return setConstant(Scalar(0));
586}
587
594template<typename Derived>
595EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
597{
598 return setZero(rows(), cols);
599}
600
607template<typename Derived>
608EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
610{
611 return setZero(rows, cols());
612}
613
614// ones:
615
630template<typename Derived>
631EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
633{
634 return Constant(rows, cols, Scalar(1));
635}
636
653template<typename Derived>
654EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
656{
657 return Constant(newSize, Scalar(1));
658}
659
670template<typename Derived>
671EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
673{
674 return Constant(Scalar(1));
675}
676
685template<typename Derived>
686EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
687(const RealScalar& prec) const
688{
689 return isApproxToConstant(Scalar(1), prec);
690}
691
699template<typename Derived>
700EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
701{
702 return setConstant(Scalar(1));
703}
704
714template<typename Derived>
715EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
718 resize(newSize);
719 return setConstant(Scalar(1));
720}
732template<typename Derived>
733EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
735{
736 resize(rows, cols);
737 return setConstant(Scalar(1));
738}
739
746template<typename Derived>
747EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
749{
750 return setOnes(rows, cols());
751}
752
759template<typename Derived>
760EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
762{
763 return setOnes(rows(), cols);
764}
765
766// Identity:
767
782template<typename Derived>
783EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
785{
786 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
787}
788
799template<typename Derived>
800EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
802{
803 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
804 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
805}
806
816template<typename Derived>
818(const RealScalar& prec) const
819{
820 typename internal::nested_eval<Derived,1>::type self(derived());
821 for(Index j = 0; j < cols(); ++j)
822 {
823 for(Index i = 0; i < rows(); ++i)
824 {
825 if(i == j)
826 {
827 if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
828 return false;
829 }
830 else
831 {
832 if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
833 return false;
834 }
835 }
836 }
837 return true;
838}
839
840namespace internal {
841
842template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
843struct setIdentity_impl
844{
845 EIGEN_DEVICE_FUNC
846 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
847 {
848 return m = Derived::Identity(m.rows(), m.cols());
849 }
850};
851
852template<typename Derived>
853struct setIdentity_impl<Derived, true>
854{
855 EIGEN_DEVICE_FUNC
856 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
857 {
858 m.setZero();
859 const Index size = numext::mini(m.rows(), m.cols());
860 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
861 return m;
862 }
863};
864
865} // end namespace internal
866
874template<typename Derived>
875EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
876{
877 return internal::setIdentity_impl<Derived>::run(derived());
878}
879
890template<typename Derived>
891EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
892{
893 derived().resize(rows, cols);
894 return setIdentity();
895}
896
903template<typename Derived>
904EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
905{
906 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
907 return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
908}
909
918template<typename Derived>
919EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
920{
921 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
922 return BasisReturnType(SquareMatrixType::Identity(),i);
923}
924
931template<typename Derived>
932EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
933{ return Derived::Unit(0); }
934
941template<typename Derived>
942EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
943{ return Derived::Unit(1); }
944
951template<typename Derived>
952EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
953{ return Derived::Unit(2); }
954
961template<typename Derived>
962EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
963{ return Derived::Unit(3); }
964
973template<typename Derived>
974EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
975{
976 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
977 eigen_assert(i<size());
978 derived().setZero();
979 derived().coeffRef(i) = Scalar(1);
980 return derived();
981}
982
992template<typename Derived>
993EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
994{
995 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
996 eigen_assert(i<newSize);
997 derived().resize(newSize);
998 return setUnit(i);
999}
1000
1001} // end namespace Eigen
1002
1003#endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:63
const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:86
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:42
static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:246
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:327
Derived & setOnes()
Definition: CwiseNullaryOp.h:700
static const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:672
static const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Definition: CwiseNullaryOp.h:116
static const ConstantReturnType Zero()
Definition: CwiseNullaryOp.h:516
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:432
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:191
void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:337
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:687
Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:347
Derived & setZero()
Definition: CwiseNullaryOp.h:548
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:530
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:312
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
static const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:942
Derived & setIdentity()
Definition: CwiseNullaryOp.h:875
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:818
static const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:932
static const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:801
static const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:952
static const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:904
Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:974
static const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:962
Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:716
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:363
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:564
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const int Dynamic
Definition: Constants.h:24
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41