Eigen  3.3.9
 
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
15namespace internal {
16template<typename NullaryOp, typename PlainObjectType>
17struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
18{
19 enum {
20 Flags = traits<PlainObjectType>::Flags & RowMajorBit
21 };
22};
23
24} // namespace internal
25
59template<typename NullaryOp, typename PlainObjectType>
60class 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
106template<typename Derived>
107template<typename CustomNullaryOp>
108EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
109DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
110{
111 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
112}
113
132template<typename Derived>
133template<typename CustomNullaryOp>
134EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
135DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
136{
137 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
139 else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
140}
141
151template<typename Derived>
152template<typename CustomNullaryOp>
153EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
154DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
155{
157}
158
172template<typename Derived>
173EIGEN_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
194template<typename Derived>
195EIGEN_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
210template<typename Derived>
211EIGEN_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
222template<typename Derived>
223EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
224DenseBase<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
234template<typename Derived>
235EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
236DenseBase<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
254 * For integer scalar types, an even spacing is possible if and only if the length of the range,
255 * i.e., \c high-low is a scalar multiple of \c size-1, or if \c size is a scalar multiple of the
256 * number of values \c high-low+1 (meaning each value can be repeated the same number of time).
257 * If one of these two considions is not satisfied, then \c high is lowered to the largest value
258 * satisfying one of this constraint.
259 * Here are some examples:
261 * Example: \include DenseBase_LinSpacedInt.cpp
262 * Output: \verbinclude DenseBase_LinSpacedInt.out
263 *
264 * \sa setLinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
265 */
266template<typename Derived>
267EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
268DenseBase<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));
273
278template<typename Derived>
279EIGEN_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
288template<typename Derived>
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
303template<typename Derived>
304EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
305(const Scalar& val, const RealScalar& prec) const
306{
307 return isApproxToConstant(val, prec);
308}
309
314template<typename Derived>
315EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
316{
317 setConstant(val);
318}
319
324template<typename Derived>
325EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
326{
327 return derived() = Constant(rows(), cols(), val);
328}
332 * \only_for_vectors
334 * Example: \include Matrix_setConstant_int.cpp
335 * Output: \verbinclude Matrix_setConstant_int.out
337 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
338 */
339template<typename Derived>
340EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
343 resize(size);
344 return setConstant(val);
345}
346
354 * Output: \verbinclude Matrix_setConstant_int_int.out
356 * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
357 */
358template<typename Derived>
359EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
362 resize(rows, cols);
363 return setConstant(val);
366/**
367 * \brief Sets a linearly spaced vector.
368 *
369 * The function generates 'size' equally spaced values in the closed interval [low,high].
370 * When size is set to 1, a vector of length 1 containing 'high' is returned.
371 *
372 * \only_for_vectors
373 *
374 * Example: \include DenseBase_setLinSpaced.cpp
375 * Output: \verbinclude DenseBase_setLinSpaced.out
376 *
377 * For integer scalar types, do not miss the explanations on the definition
378 * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
380 * \sa LinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
381 */
382template<typename Derived>
383EIGEN_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
402template<typename Derived>
403EIGEN_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
425template<typename Derived>
426EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
428{
429 return Constant(rows, cols, Scalar(0));
430}
431
448template<typename Derived>
449EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
451{
452 return Constant(size, Scalar(0));
453}
454
465template<typename Derived>
466EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
468{
469 return Constant(Scalar(0));
470}
471
480template<typename Derived>
481EIGEN_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
498template<typename Derived>
499EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
500{
501 return setConstant(Scalar(0));
502}
503
513template<typename Derived>
514EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
516{
517 resize(newSize);
518 return setConstant(Scalar(0));
519}
520
531template<typename Derived>
532EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
534{
535 resize(rows, cols);
536 return setConstant(Scalar(0));
537}
538
539// ones:
540
555template<typename Derived>
556EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
558{
559 return Constant(rows, cols, Scalar(1));
560}
561
578template<typename Derived>
579EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
581{
582 return Constant(newSize, Scalar(1));
583}
584
595template<typename Derived>
596EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
598{
599 return Constant(Scalar(1));
600}
601
610template<typename Derived>
611EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
612(const RealScalar& prec) const
613{
614 return isApproxToConstant(Scalar(1), prec);
615}
616
624template<typename Derived>
625EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
626{
627 return setConstant(Scalar(1));
628}
629
639template<typename Derived>
640EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
642{
643 resize(newSize);
644 return setConstant(Scalar(1));
645}
646
653 * Output: \verbinclude Matrix_setOnes_int_int.out
655 * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
656 */
657template<typename Derived>
658EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
660{
661 resize(rows, cols);
662 return setConstant(Scalar(1));
663}
664
665// Identity:
666
681template<typename Derived>
682EIGEN_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
698template<typename Derived>
699EIGEN_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
715template<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
739namespace internal {
740
741template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
742struct 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
751template<typename Derived>
752struct 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
773template<typename Derived>
774EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
775{
776 return internal::setIdentity_impl<Derived>::run(derived());
777}
778
789template<typename Derived>
790EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
791{
792 derived().resize(rows, cols);
793 return setIdentity();
794}
795
802template<typename Derived>
803EIGEN_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
817template<typename Derived>
818EIGEN_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
830template<typename Derived>
831EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
832{ return Derived::Unit(0); }
833
840template<typename Derived>
841EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
842{ return Derived::Unit(1); }
843
850template<typename Derived>
851EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
852{ return Derived::Unit(2); }
853
860template<typename Derived>
861EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
862{ return Derived::Unit(3); }
863
864} // end namespace Eigen
865
866#endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition CwiseNullaryOp.h:61
const NullaryOp & functor() const
Definition CwiseNullaryOp.h:84
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:47
bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:305
Derived & setOnes()
Definition CwiseNullaryOp.h:625
static const ConstantReturnType Ones()
Definition CwiseNullaryOp.h:597
void resize(Index newSize)
Definition DenseBase.h:241
static const ConstantReturnType Zero()
Definition CwiseNullaryOp.h:467
Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition CwiseNullaryOp.h:383
internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:66
static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition CwiseNullaryOp.h:174
CoeffReturnType value() const
Definition DenseBase.h:480
void fill(const Scalar &value)
Definition CwiseNullaryOp.h:315
DenseBase()
Definition DenseBase.h:592
bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:612
Derived & setConstant(const Scalar &value)
Definition CwiseNullaryOp.h:325
static const SequentialLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition CwiseNullaryOp.h:224
Derived & setZero()
Definition CwiseNullaryOp.h:499
bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:481
bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:290
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
static const BasisReturnType UnitY()
Definition CwiseNullaryOp.h:841
Derived & setIdentity()
Definition CwiseNullaryOp.h:774
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition CwiseNullaryOp.h:717
static const BasisReturnType UnitX()
Definition CwiseNullaryOp.h:831
static const IdentityReturnType Identity()
Definition CwiseNullaryOp.h:700
static const BasisReturnType UnitZ()
Definition CwiseNullaryOp.h:851
static const BasisReturnType Unit(Index size, Index i)
Definition CwiseNullaryOp.h:803
static const BasisReturnType UnitW()
Definition CwiseNullaryOp.h:861
Derived & setOnes(Index size)
Definition CwiseNullaryOp.h:641
void resize(Index rows, Index cols)
Definition PlainObjectBase.h:279
Derived & setConstant(Index size, const Scalar &val)
Definition CwiseNullaryOp.h:341
Derived & setZero(Index size)
Definition CwiseNullaryOp.h:515
const unsigned int RowMajorBit
Definition Constants.h:61
Namespace containing all symbols from the Eigen library.
Definition A05_PortingFrom2To3.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:65
const int Dynamic
Definition Constants.h:21