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
32
33namespace internal {
34template<typename NullaryOp, typename PlainObjectType>
35struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
36{
37 enum {
38 Flags = (traits<PlainObjectType>::Flags
39 & ( HereditaryBits
40 | (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
41 | (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
42 | (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
43 CoeffReadCost = functor_traits<NullaryOp>::Cost
44 };
45};
46}
47
48template<typename NullaryOp, typename PlainObjectType>
49class CwiseNullaryOp : internal::no_assignment_operator,
50 public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type
51{
52 public:
53
54 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
55 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
56
57 CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
58 : m_rows(rows), m_cols(cols), m_functor(func)
59 {
60 eigen_assert(rows >= 0
61 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
62 && cols >= 0
63 && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
64 }
65
66 EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
67 EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
68
69 EIGEN_STRONG_INLINE const Scalar coeff(Index rows, Index cols) const
70 {
71 return m_functor(rows, cols);
72 }
73
74 template<int LoadMode>
75 EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
76 {
77 return m_functor.packetOp(row, col);
78 }
79
80 EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
81 {
82 return m_functor(index);
83 }
84
85 template<int LoadMode>
86 EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
87 {
88 return m_functor.packetOp(index);
89 }
90
92 const NullaryOp& functor() const { return m_functor; }
93
94 protected:
95 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
96 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
97 const NullaryOp m_functor;
98};
99
100
114template<typename Derived>
115template<typename CustomNullaryOp>
116EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
117DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
118{
119 return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
120}
121
137template<typename Derived>
138template<typename CustomNullaryOp>
139EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
140DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
141{
142 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
143 if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func);
144 else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func);
145}
146
156template<typename Derived>
157template<typename CustomNullaryOp>
158EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
163
177template<typename Derived>
178EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
180{
181 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
182}
183
199template<typename Derived>
200EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
202{
203 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
204}
205
215template<typename Derived>
216EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
218{
219 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
220 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
221}
222
240template<typename Derived>
241EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
242DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
243{
244 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
245 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
246}
247
248/**
249 * \copydoc DenseBase::LinSpaced(Sequential_t, Index, const Scalar&, const Scalar&)
250 * Special version for fixed size types which does not require the size parameter.
251 */
252template<typename Derived>
254DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
256 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
257 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
258 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime));
259}
261/**
262 * \brief Sets a linearly space vector.
264 * The function generates 'size' equally spaced values in the closed interval [low,high].
265 * When size is set to 1, a vector of length 1 containing 'high' is returned.
266 *
267 * \only_for_vectors
268 *
269 * Example: \include DenseBase_LinSpaced.cpp
270 * Output: \verbinclude DenseBase_LinSpaced.out
271 *
272 * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp
273 */
274template<typename Derived>
275EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
276DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
277{
278 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
279 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size));
280}
281
286template<typename Derived>
287EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
288DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
289{
290 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
291 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
292 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime));
293}
294
296template<typename Derived>
298(const Scalar& value, RealScalar prec) const
299{
300 for(Index j = 0; j < cols(); ++j)
301 for(Index i = 0; i < rows(); ++i)
302 if(!internal::isApprox(this->coeff(i, j), value, prec))
303 return false;
304 return true;
305}
306
309 * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
310template<typename Derived>
312(const Scalar& value, RealScalar prec) const
314 return isApproxToConstant(value, prec);
315}
319 * \sa setConstant(), Constant(), class CwiseNullaryOp
320 */
321template<typename Derived>
322EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& value)
323{
325}
329 * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
330 */
331template<typename Derived>
332EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& value)
333{
334 return derived() = Constant(rows(), cols(), value);
337/** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value.
339 * \only_for_vectors
340 *
341 * Example: \include Matrix_setConstant_int.cpp
342 * Output: \verbinclude Matrix_setConstant_int.out
344 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
345 */
346template<typename Derived>
347EIGEN_STRONG_INLINE Derived&
348PlainObjectBase<Derived>::setConstant(Index size, const Scalar& value)
349{
350 resize(size);
351 return setConstant(value);
352}
353
358 * \param value the value to which all coefficients are set
360 * Example: \include Matrix_setConstant_int_int.cpp
361 * Output: \verbinclude Matrix_setConstant_int_int.out
362 *
363 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
364 */
365template<typename Derived>
366EIGEN_STRONG_INLINE Derived&
367PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& value)
368{
369 resize(rows, cols);
370 return setConstant(value);
371}
372
386template<typename Derived>
387EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index size, const Scalar& low, const Scalar& high)
388{
389 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
390 return derived() = Derived::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
391}
392
403template<typename Derived>
404EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
405{
406 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
407 return setLinSpaced(size(), low, high);
408}
409
410// zero:
411
426template<typename Derived>
427EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
429{
430 return Constant(rows, cols, Scalar(0));
431}
432
449template<typename Derived>
450EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
452{
453 return Constant(size, Scalar(0));
454}
455
466template<typename Derived>
467EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
469{
470 return Constant(Scalar(0));
471}
472
481template<typename Derived>
482bool DenseBase<Derived>::isZero(RealScalar prec) const
483{
484 for(Index j = 0; j < cols(); ++j)
485 for(Index i = 0; i < rows(); ++i)
486 if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<Scalar>(1), prec))
487 return false;
488 return true;
489}
490
498template<typename Derived>
499EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
500{
501 return setConstant(Scalar(0));
502}
503
513template<typename Derived>
514EIGEN_STRONG_INLINE Derived&
516{
517 resize(size);
518 return setConstant(Scalar(0));
519}
521/** Resizes to the given size, and sets all coefficients in this expression to zero.
522 *
523 * \param rows the new number of rows
524 * \param cols the new number of columns
526 * Example: \include Matrix_setZero_int_int.cpp
527 * Output: \verbinclude Matrix_setZero_int_int.out
529 * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
530 */
531template<typename Derived>
532EIGEN_STRONG_INLINE Derived&
534{
535 resize(rows, cols);
536 return setConstant(Scalar(0));
537}
538
539// ones:
540
555template<typename Derived>
556EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
558{
559 return Constant(rows, cols, Scalar(1));
560}
561
578template<typename Derived>
579EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
581{
582 return Constant(size, Scalar(1));
583}
584
595template<typename Derived>
596EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
598{
599 return Constant(Scalar(1));
600}
601
610template<typename Derived>
612(RealScalar prec) const
613{
614 return isApproxToConstant(Scalar(1), prec);
615}
616
624template<typename Derived>
625EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
626{
627 return setConstant(Scalar(1));
628}
629
639template<typename Derived>
640EIGEN_STRONG_INLINE Derived&
642{
643 resize(size);
644 return setConstant(Scalar(1));
645}
646
657template<typename Derived>
658EIGEN_STRONG_INLINE Derived&
660{
661 resize(rows, cols);
662 return setConstant(Scalar(1));
663}
664
665// Identity:
666
681template<typename Derived>
682EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
684{
685 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
686}
687
698template<typename Derived>
699EIGEN_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
715template<typename Derived>
717(RealScalar prec) const
718{
719 for(Index j = 0; j < cols(); ++j)
720 {
721 for(Index i = 0; i < rows(); ++i)
722 {
723 if(i == j)
724 {
725 if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec))
726 return false;
727 }
728 else
729 {
730 if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec))
731 return false;
732 }
733 }
734 }
735 return true;
736}
737
738namespace internal {
739
740template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
741struct setIdentity_impl
742{
743 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
744 {
745 return m = Derived::Identity(m.rows(), m.cols());
746 }
747};
748
749template<typename Derived>
750struct setIdentity_impl<Derived, true>
751{
752 typedef typename Derived::Index Index;
753 static EIGEN_STRONG_INLINE Derived& run(Derived& m)
754 {
755 m.setZero();
756 const Index size = (std::min)(m.rows(), m.cols());
757 for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
758 return m;
759 }
760};
761
762} // end namespace internal
763
771template<typename Derived>
772EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
773{
774 return internal::setIdentity_impl<Derived>::run(derived());
775}
776
787template<typename Derived>
788EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
789{
790 derived().resize(rows, cols);
791 return setIdentity();
792}
793
800template<typename Derived>
801EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index size, Index i)
802{
803 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
804 return BasisReturnType(SquareMatrixType::Identity(size,size), i);
805}
806
815template<typename Derived>
816EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
817{
818 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
819 return BasisReturnType(SquareMatrixType::Identity(),i);
820}
821
828template<typename Derived>
829EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
830{ return Derived::Unit(0); }
831
838template<typename Derived>
839EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
840{ return Derived::Unit(1); }
841
848template<typename Derived>
849EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
850{ return Derived::Unit(2); }
851
858template<typename Derived>
859EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
860{ return Derived::Unit(3); }
861
862} // end namespace Eigen
863
864#endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition CwiseNullaryOp.h:51
const NullaryOp & functor() const
Definition CwiseNullaryOp.h:92
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:38
static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition CwiseNullaryOp.h:242
static const CwiseNullaryOp< CustomNullaryOp, Derived > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Definition CwiseNullaryOp.h:117
Derived & setOnes()
Definition CwiseNullaryOp.h:625
static const ConstantReturnType Ones()
Definition CwiseNullaryOp.h:597
static const ConstantReturnType Zero()
Definition CwiseNullaryOp.h:468
Derived & setConstant(const Scalar &value)
Definition CwiseNullaryOp.h:332
DenseBase()
Definition DenseBase.h:513
internal::traits< Derived >::Index Index
The type of indices.
Definition DenseBase.h:51
bool isZero(RealScalar prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:482
bool isOnes(RealScalar prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:612
Derived & setZero()
Definition CwiseNullaryOp.h:499
CoeffReturnType value() const
Definition DenseBase.h:434
void fill(const Scalar &value)
Definition CwiseNullaryOp.h:322
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition CwiseNullaryOp.h:179
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly space vector.
Definition CwiseNullaryOp.h:387
bool isConstant(const Scalar &value, RealScalar prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:312
@ RowsAtCompileTime
Definition DenseBase.h:92
@ ColsAtCompileTime
Definition DenseBase.h:98
bool isApproxToConstant(const Scalar &value, RealScalar prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:298
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
static const IdentityReturnType Identity()
Definition CwiseNullaryOp.h:700
static const BasisReturnType UnitZ()
Definition CwiseNullaryOp.h:849
static const BasisReturnType UnitW()
Definition CwiseNullaryOp.h:859
static const BasisReturnType UnitX()
Definition CwiseNullaryOp.h:829
bool isIdentity(RealScalar prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:717
static const BasisReturnType UnitY()
Definition CwiseNullaryOp.h:839
static const BasisReturnType Unit(Index size, Index i)
Definition CwiseNullaryOp.h:801
Derived & setIdentity()
Definition CwiseNullaryOp.h:772
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:515
Derived & setOnes(Index size)
Definition CwiseNullaryOp.h:641
Derived & setConstant(Index size, const Scalar &value)
Definition CwiseNullaryOp.h:348
void resize(Index rows, Index cols)
Definition PlainObjectBase.h:219
const unsigned int PacketAccessBit
Definition Constants.h:76
const unsigned int LinearAccessBit
Definition Constants.h:112
const unsigned int EvalBeforeNestingBit
Definition Constants.h:53
Definition LDLT.h:18