Medial Code Documentation
Loading...
Searching...
No Matches
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
13namespace Eigen {
14
33namespace internal {
34template<typename NullaryOp, typename PlainObjectType>
35struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
36{
37 enum {
39 };
40};
41}
42
43template<typename NullaryOp, typename PlainObjectType>
44class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
45{
46 public:
47
49 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
50
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
62 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
64 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
65
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
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
92 const NullaryOp& functor() const { return m_functor; }
93
94 protected:
97 const NullaryOp m_functor;
98};
99
100
114template<typename Derived>
115template<typename CustomNullaryOp>
116EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
117DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
118{
119 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
120}
121
140template<typename Derived>
141template<typename CustomNullaryOp>
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
159template<typename Derived>
160template<typename CustomNullaryOp>
163{
164 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
165}
166
180template<typename Derived>
181EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
182DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
183{
185}
186
202template<typename Derived>
203EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
208
218template<typename Derived>
219EIGEN_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
243template<typename Derived>
244EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
245DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
246{
247 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
249}
250
255template<typename Derived>
256EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
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));
277template<typename Derived>
278EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
279DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
280{
281 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
283}
284
289template<typename Derived>
290EIGEN_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
299template<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
314template<typename Derived>
316(const Scalar& val, const RealScalar& prec) const
317{
318 return isApproxToConstant(val, prec);
319}
320
325template<typename Derived>
326EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
327{
328 setConstant(val);
329}
330
335template<typename Derived>
336EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
338 return derived() = Constant(rows(), cols(), val);
339}
350template<typename Derived>
351EIGEN_STRONG_INLINE Derived&
352PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
353{
354 resize(size);
355 return setConstant(val);
356}
357
369template<typename Derived>
370EIGEN_STRONG_INLINE Derived&
371PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
372{
373 resize(rows, cols);
374 return setConstant(val);
375}
376
390template<typename Derived>
391EIGEN_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
407template<typename Derived>
408EIGEN_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
430template<typename Derived>
431EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
432DenseBase<Derived>::Zero(Index rows, Index cols)
433{
434 return Constant(rows, cols, Scalar(0));
435}
436
453template<typename Derived>
454EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
456{
457 return Constant(size, Scalar(0));
458}
459
470template<typename Derived>
471EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
473{
474 return Constant(Scalar(0));
475}
476
485template<typename Derived>
486bool 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
503template<typename Derived>
504EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
505{
506 return setConstant(Scalar(0));
507}
508
518template<typename Derived>
519EIGEN_STRONG_INLINE Derived&
521{
522 resize(newSize);
523 return setConstant(Scalar(0));
524}
525
536template<typename Derived>
537EIGEN_STRONG_INLINE Derived&
539{
540 resize(rows, cols);
541 return setConstant(Scalar(0));
542}
543
544// ones:
545
560template<typename Derived>
561EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
562DenseBase<Derived>::Ones(Index rows, Index cols)
563{
564 return Constant(rows, cols, Scalar(1));
565}
566
583template<typename Derived>
584EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
586{
587 return Constant(newSize, Scalar(1));
588}
589
600template<typename Derived>
601EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
603{
604 return Constant(Scalar(1));
605}
606
615template<typename Derived>
617(const RealScalar& prec) const
618{
619 return isApproxToConstant(Scalar(1), prec);
620}
629template<typename Derived>
630EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
631{
632 return setConstant(Scalar(1));
633}
634
644template<typename Derived>
645EIGEN_STRONG_INLINE Derived&
647{
648 resize(newSize);
649 return setConstant(Scalar(1));
650}
651
662template<typename Derived>
663EIGEN_STRONG_INLINE Derived&
665{
666 resize(rows, cols);
667 return setConstant(Scalar(1));
668}
669
670// Identity:
671
686template<typename Derived>
687EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
692
703template<typename Derived>
704EIGEN_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
720template<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
744namespace internal {
745
746template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
748{
750 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
751 {
752 return m = Derived::Identity(m.rows(), m.cols());
753 }
754};
755
756template<typename Derived>
757struct setIdentity_impl<Derived, true>
758{
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
778template<typename Derived>
779EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
780{
782}
783
794template<typename Derived>
795EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
796{
797 derived().resize(rows, cols);
798 return setIdentity();
799}
800
807template<typename Derived>
808EIGEN_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
822template<typename Derived>
823EIGEN_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
835template<typename Derived>
837{ return Derived::Unit(0); }
838
845template<typename Derived>
847{ return Derived::Unit(1); }
848
855template<typename Derived>
857{ return Derived::Unit(2); }
858
865template<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:45
EIGEN_DEVICE_FUNC const NullaryOp & functor() const
Definition CwiseNullaryOp.h:92
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:49
EIGEN_DEVICE_FUNC Derived & setZero()
Sets all coefficients in this expression to zero.
Definition CwiseNullaryOp.h:504
EIGEN_DEVICE_FUNC bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
This is just an alias for isApproxToConstant().
Definition CwiseNullaryOp.h:316
static EIGEN_DEVICE_FUNC const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition CwiseNullaryOp.h:245
EIGEN_DEVICE_FUNC void fill(const Scalar &value)
Alias for setConstant(): sets all coefficients in this expression to val.
Definition CwiseNullaryOp.h:326
EIGEN_DEVICE_FUNC Derived & setConstant(const Scalar &value)
Sets all coefficients in this expression to value.
Definition CwiseNullaryOp.h:336
internal::traits< Derived >::Scalar Scalar
The numeric type of the expression' coefficients, e.g.
Definition DenseBase.h:68
static EIGEN_DEVICE_FUNC const ConstantReturnType Ones()
Definition CwiseNullaryOp.h:602
EIGEN_DEVICE_FUNC Derived & setOnes()
Sets all coefficients in this expression to one.
Definition CwiseNullaryOp.h:630
EIGEN_DEVICE_FUNC bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:617
static EIGEN_DEVICE_FUNC const ConstantReturnType Zero()
Definition CwiseNullaryOp.h:472
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition CwiseNullaryOp.h:391
EIGEN_DEVICE_FUNC bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:486
EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:301
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
static EIGEN_DEVICE_FUNC const IdentityReturnType Identity()
Definition CwiseNullaryOp.h:705
static EIGEN_DEVICE_FUNC const BasisReturnType UnitW()
Definition CwiseNullaryOp.h:866
EIGEN_DEVICE_FUNC Derived & setIdentity()
Writes the identity expression (not necessarily square) into *this.
Definition CwiseNullaryOp.h:779
static EIGEN_DEVICE_FUNC const BasisReturnType UnitX()
Definition CwiseNullaryOp.h:836
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:722
static EIGEN_DEVICE_FUNC const BasisReturnType UnitZ()
Definition CwiseNullaryOp.h:856
static EIGEN_DEVICE_FUNC const BasisReturnType Unit(Index size, Index i)
Definition CwiseNullaryOp.h:808
static EIGEN_DEVICE_FUNC const BasisReturnType UnitY()
Definition CwiseNullaryOp.h:846
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &value)
Resizes to the given size, and sets all coefficients in this expression to the given value.
Definition CwiseNullaryOp.h:352
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition CwiseNullaryOp.h:520
EIGEN_DEVICE_FUNC Derived & setOnes(Index size)
Resizes to the given newSize, and sets all coefficients in this expression to one.
Definition CwiseNullaryOp.h:646
Pseudo expression representing a solving operation.
Definition Solve.h:63
Definition XprHelper.h:67
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition XprHelper.h:428
Definition NullaryFunctors.h:107
Definition NullaryFunctors.h:18
Definition NullaryFunctors.h:31
Definition CwiseNullaryOp.h:748
Definition ForwardDeclarations.h:17
Definition Meta.h:30