Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
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 <[email protected]>
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// IWYU pragma: private
14#include "./InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19template <typename NullaryOp, typename PlainObjectType>
20struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType> {
21 enum { Flags = traits<PlainObjectType>::Flags & RowMajorBit };
22};
23
24} // namespace internal
25
62template <typename NullaryOp, typename PlainObjectType>
63class CwiseNullaryOp : public internal::dense_xpr_base<CwiseNullaryOp<NullaryOp, PlainObjectType> >::type,
64 internal::no_assignment_operator {
65 public:
66 typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
67 EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
68
69 EIGEN_DEVICE_FUNC CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
70 : m_rows(rows), m_cols(cols), m_functor(func) {
71 eigen_assert(rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) && cols >= 0 &&
72 (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
73 }
74
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const { return m_rows.value(); }
76 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const { return m_cols.value(); }
77
79 EIGEN_DEVICE_FUNC const NullaryOp& functor() const { return m_functor; }
80
81 protected:
82 const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
83 const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
84 const NullaryOp m_functor;
85};
86
100template <typename Derived>
101template <typename CustomNullaryOp>
102EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
103#ifndef EIGEN_PARSED_BY_DOXYGEN
104 const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
105#else
106 const CwiseNullaryOp<CustomNullaryOp, PlainObject>
107#endif
108 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func) {
109 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
110}
111
130template <typename Derived>
131template <typename CustomNullaryOp>
132EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
133#ifndef EIGEN_PARSED_BY_DOXYGEN
135#else
137#endif
138 DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func) {
139 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
140 if (RowsAtCompileTime == 1)
142 else
144}
145
155template <typename Derived>
156template <typename CustomNullaryOp>
157EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
158#ifndef EIGEN_PARSED_BY_DOXYGEN
160#else
162#endif
163 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func) {
164 return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
165}
166
180template <typename Derived>
181EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
183 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
184}
185
201template <typename Derived>
202EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
204 return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
205}
206
216template <typename Derived>
217EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
219 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
220 return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime,
221 internal::scalar_constant_op<Scalar>(value));
222}
223
233template <typename Derived>
234EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<
235 Derived>::RandomAccessLinSpacedReturnType
236DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high) {
237 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
238 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low, high, size));
239}
240
245template <typename Derived>
246EIGEN_DEPRECATED EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<
247 Derived>::RandomAccessLinSpacedReturnType
248DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high) {
249 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
250 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
251 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime,
252 internal::linspaced_op<Scalar>(low, high, Derived::SizeAtCompileTime));
254
278template <typename Derived>
279EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
280DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high) {
281 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
282 return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar>(low, high, size));
283}
284
289template <typename Derived>
290EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
292 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
293 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
294 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime,
295 internal::linspaced_op<Scalar>(low, high, Derived::SizeAtCompileTime));
296}
297
298template <typename Derived>
299EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessEqualSpacedReturnType
300DenseBase<Derived>::EqualSpaced(Index size, const Scalar& low, const Scalar& step) {
301 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
302 return DenseBase<Derived>::NullaryExpr(size, internal::equalspaced_op<Scalar>(low, step));
305template <typename Derived>
306EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessEqualSpacedReturnType
308 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
309 return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::equalspaced_op<Scalar>(low, step));
311
313template <typename Derived>
314EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApproxToConstant(const Scalar& val, const RealScalar& prec) const {
315 typename internal::nested_eval<Derived, 1>::type self(derived());
316 for (Index j = 0; j < cols(); ++j)
317 for (Index i = 0; i < rows(); ++i)
318 if (!internal::isApprox(self.coeff(i, j), val, prec)) return false;
319 return true;
320}
321
325template <typename Derived>
326EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant(const Scalar& val, const RealScalar& prec) const {
327 return isApproxToConstant(val, prec);
328}
334template <typename Derived>
335EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val) {
336 setConstant(val);
337}
344template <typename Derived>
345EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val) {
346 return derived() = Constant(rows(), cols(), val);
347}
348
359template <typename Derived>
360EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val) {
361 resize(size);
362 return setConstant(val);
363}
364
377template <typename Derived>
378EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setConstant(Index rows, Index cols,
379 const Scalar& val) {
380 resize(rows, cols);
381 return setConstant(val);
382}
383
391template <typename Derived>
392EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setConstant(NoChange_t, Index cols,
393 const Scalar& val) {
394 return setConstant(rows(), cols, val);
395}
396
404template <typename Derived>
405EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setConstant(Index rows, NoChange_t,
406 const Scalar& val) {
407 return setConstant(rows, cols(), val);
408}
409
426template <typename Derived>
427EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low,
428 const Scalar& high) {
429 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
430 return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low, high, newSize));
431}
432
446template <typename Derived>
447EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high) {
448 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
449 return setLinSpaced(size(), low, high);
450}
451
452template <typename Derived>
453EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setEqualSpaced(Index newSize, const Scalar& low,
454 const Scalar& step) {
455 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
456 return derived() = Derived::NullaryExpr(newSize, internal::equalspaced_op<Scalar>(low, step));
457}
458template <typename Derived>
459EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setEqualSpaced(const Scalar& low,
460 const Scalar& step) {
461 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
462 return setEqualSpaced(size(), low, step);
463}
464
465// zero:
466
481template <typename Derived>
482EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Zero(
483 Index rows, Index cols) {
484 return Constant(rows, cols, Scalar(0));
485}
486
503template <typename Derived>
504EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Zero(
505 Index size) {
506 return Constant(size, Scalar(0));
507}
508
519template <typename Derived>
520EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Zero() {
521 return Constant(Scalar(0));
522}
523
532template <typename Derived>
533EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const {
534 typename internal::nested_eval<Derived, 1>::type self(derived());
535 for (Index j = 0; j < cols(); ++j)
536 for (Index i = 0; i < rows(); ++i)
537 if (!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec)) return false;
538 return true;
539}
540
548template <typename Derived>
549EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero() {
550 return setConstant(Scalar(0));
551}
552
562template <typename Derived>
563EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setZero(Index newSize) {
564 resize(newSize);
565 return setConstant(Scalar(0));
566}
567
578template <typename Derived>
579EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setZero(Index rows, Index cols) {
580 resize(rows, cols);
581 return setConstant(Scalar(0));
582}
583
591template <typename Derived>
592EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setZero(NoChange_t, Index cols) {
593 return setZero(rows(), cols);
594}
595
603template <typename Derived>
604EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setZero(Index rows, NoChange_t) {
605 return setZero(rows, cols());
606}
607
608// ones:
609
624template <typename Derived>
625EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Ones(
626 Index rows, Index cols) {
627 return Constant(rows, cols, Scalar(1));
628}
629
646template <typename Derived>
647EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Ones(
648 Index newSize) {
649 return Constant(newSize, Scalar(1));
650}
651
662template <typename Derived>
663EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType DenseBase<Derived>::Ones() {
664 return Constant(Scalar(1));
665}
666
675template <typename Derived>
676EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes(const RealScalar& prec) const {
677 return isApproxToConstant(Scalar(1), prec);
678}
679
687template <typename Derived>
688EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes() {
689 return setConstant(Scalar(1));
690}
701template <typename Derived>
702EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setOnes(Index newSize) {
703 resize(newSize);
704 return setConstant(Scalar(1));
717template <typename Derived>
718EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setOnes(Index rows, Index cols) {
719 resize(rows, cols);
720 return setConstant(Scalar(1));
721}
722
730template <typename Derived>
731EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setOnes(Index rows, NoChange_t) {
732 return setOnes(rows, cols());
733}
734
742template <typename Derived>
743EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& PlainObjectBase<Derived>::setOnes(NoChange_t, Index cols) {
744 return setOnes(rows(), cols);
745}
746
747// Identity:
748
763template <typename Derived>
764EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
766 return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
767}
768
779template <typename Derived>
780EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
782 EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
783 return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
784}
785
795template <typename Derived>
796bool MatrixBase<Derived>::isIdentity(const RealScalar& prec) const {
797 typename internal::nested_eval<Derived, 1>::type self(derived());
798 for (Index j = 0; j < cols(); ++j) {
799 for (Index i = 0; i < rows(); ++i) {
800 if (i == j) {
801 if (!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec)) return false;
802 } else {
803 if (!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec)) return false;
804 }
805 }
806 }
807 return true;
808}
809
810namespace internal {
811
812template <typename Derived, bool Big = (Derived::SizeAtCompileTime >= 16)>
813struct setIdentity_impl {
814 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) {
815 return m = Derived::Identity(m.rows(), m.cols());
816 }
817};
818
819template <typename Derived>
820struct setIdentity_impl<Derived, true> {
821 EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) {
822 m.setZero();
823 const Index size = numext::mini(m.rows(), m.cols());
824 for (Index i = 0; i < size; ++i) m.coeffRef(i, i) = typename Derived::Scalar(1);
825 return m;
826 }
827};
828
829} // end namespace internal
830
838template <typename Derived>
839EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity() {
840 return internal::setIdentity_impl<Derived>::run(derived());
841}
842
853template <typename Derived>
854EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols) {
855 derived().resize(rows, cols);
856 return setIdentity();
857}
858
865template <typename Derived>
866EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(
867 Index newSize, Index i) {
868 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
869 return BasisReturnType(SquareMatrixType::Identity(newSize, newSize), i);
870}
871
880template <typename Derived>
881EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(
882 Index i) {
883 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
884 return BasisReturnType(SquareMatrixType::Identity(), i);
885}
886
894template <typename Derived>
895EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX() {
896 return Derived::Unit(0);
897}
898
906template <typename Derived>
907EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY() {
908 return Derived::Unit(1);
909}
910
918template <typename Derived>
919EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ() {
920 return Derived::Unit(2);
921}
922
930template <typename Derived>
931EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW() {
932 return Derived::Unit(3);
933}
934
943template <typename Derived>
944EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i) {
945 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
946 eigen_assert(i < size());
947 derived().setZero();
948 derived().coeffRef(i) = Scalar(1);
949 return derived();
950}
951
961template <typename Derived>
962EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i) {
963 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
964 eigen_assert(i < newSize);
965 derived().resize(newSize);
966 return setUnit(i);
967}
968
969} // end namespace Eigen
970
971#endif // EIGEN_CWISE_NULLARY_OP_H
Generic expression of a matrix where all coefficients are defined by a functor.
Definition CwiseNullaryOp.h:64
const NullaryOp & functor() const
Definition CwiseNullaryOp.h:79
Base class for all dense matrices, vectors, and arrays.
Definition DenseBase.h:44
void resize(Index newSize)
Definition DenseBase.h:228
internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:62
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
Dense storage base class for matrices and arrays.
Definition PlainObjectBase.h:121
const unsigned int RowMajorBit
Definition Constants.h:70
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int Dynamic
Definition Constants.h:25