CwiseNullaryOp.h
Go to the documentation of this file.
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 template <typename NullaryOp, typename PlainObjectType>
20 struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType> {
22 };
23 
24 } // namespace internal
25 
62 template <typename NullaryOp, typename PlainObjectType>
63 class CwiseNullaryOp : public internal::dense_xpr_base<CwiseNullaryOp<NullaryOp, PlainObjectType> >::type,
65  public:
68 
69  EIGEN_DEVICE_FUNC CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
71  eigen_assert(rows >= 0 && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows) && cols >= 0 &&
72  (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
73  }
74  EIGEN_DEVICE_FUNC CwiseNullaryOp(Index size, const NullaryOp& func = NullaryOp())
75  : CwiseNullaryOp(RowsAtCompileTime == 1 ? 1 : size, RowsAtCompileTime == 1 ? size : 1, func) {
76  EIGEN_STATIC_ASSERT(CwiseNullaryOp::IsVectorAtCompileTime, YOU_TRIED_CALLING_A_VECTOR_METHOD_ON_A_MATRIX);
77  }
78 
81 
83  EIGEN_DEVICE_FUNC const NullaryOp& functor() const { return m_functor; }
84 
85  protected:
88  const NullaryOp m_functor;
89 };
90 
104 template <typename Derived>
105 template <typename CustomNullaryOp>
107 #ifndef EIGEN_PARSED_BY_DOXYGEN
109 #else
111 #endif
114 }
115 
134 template <typename Derived>
135 template <typename CustomNullaryOp>
137 #ifndef EIGEN_PARSED_BY_DOXYGEN
139 #else
141 #endif
142  DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func) {
144  if (RowsAtCompileTime == 1)
146  else
148 }
149 
159 template <typename Derived>
160 template <typename CustomNullaryOp>
162 #ifndef EIGEN_PARSED_BY_DOXYGEN
164 #else
166 #endif
167  DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func) {
168  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
169 }
170 
184 template <typename Derived>
188 }
189 
205 template <typename Derived>
209 }
210 
220 template <typename Derived>
224  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime,
226 }
227 
237 template <typename Derived>
239  Derived>::RandomAccessLinSpacedReturnType
243 }
244 
249 template <typename Derived>
255  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime,
256  internal::linspaced_op<Scalar>(low, high, Derived::SizeAtCompileTime));
257 }
258 
282 template <typename Derived>
287 }
288 
293 template <typename Derived>
295 DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high) {
298  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime,
299  internal::linspaced_op<Scalar>(low, high, Derived::SizeAtCompileTime));
300 }
301 
302 template <typename Derived>
306  return DenseBase<Derived>::NullaryExpr(size, internal::equalspaced_op<Scalar>(low, step));
307 }
308 
309 template <typename Derived>
311 DenseBase<Derived>::EqualSpaced(const Scalar& low, const Scalar& step) {
313  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::equalspaced_op<Scalar>(low, step));
314 }
315 
317 template <typename Derived>
319  typename internal::nested_eval<Derived, 1>::type self(derived());
320  for (Index j = 0; j < cols(); ++j)
321  for (Index i = 0; i < rows(); ++i)
322  if (!internal::isApprox(self.coeff(i, j), val, prec)) return false;
323  return true;
324 }
325 
329 template <typename Derived>
331  return isApproxToConstant(val, prec);
332 }
333 
338 template <typename Derived>
341 }
342 
348 template <typename Derived>
351  return derived();
352 }
353 
364 template <typename Derived>
366  resize(size);
367  return setConstant(val);
368 }
369 
382 template <typename Derived>
384  const Scalar& val) {
385  resize(rows, cols);
386  return setConstant(val);
387 }
388 
396 template <typename Derived>
398  const Scalar& val) {
399  return setConstant(rows(), cols, val);
400 }
401 
409 template <typename Derived>
411  const Scalar& val) {
412  return setConstant(rows, cols(), val);
413 }
414 
431 template <typename Derived>
433  const Scalar& high) {
435  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar>(low, high, newSize));
436 }
437 
451 template <typename Derived>
454  return setLinSpaced(size(), low, high);
455 }
456 
457 template <typename Derived>
459  const Scalar& step) {
461  return derived() = Derived::NullaryExpr(newSize, internal::equalspaced_op<Scalar>(low, step));
462 }
463 template <typename Derived>
465  const Scalar& step) {
467  return setEqualSpaced(size(), low, step);
468 }
469 
470 // zero:
471 
486 template <typename Derived>
488  Index rows, Index cols) {
489  return ZeroReturnType(rows, cols);
490 }
491 
508 template <typename Derived>
510  Index size) {
511  return ZeroReturnType(size);
512 }
513 
524 template <typename Derived>
526  return ZeroReturnType(RowsAtCompileTime, ColsAtCompileTime);
527 }
528 
537 template <typename Derived>
539  typename internal::nested_eval<Derived, 1>::type self(derived());
540  for (Index j = 0; j < cols(); ++j)
541  for (Index i = 0; i < rows(); ++i)
542  if (!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec)) return false;
543  return true;
544 }
545 
553 template <typename Derived>
556  return derived();
557 }
558 
568 template <typename Derived>
570  resize(newSize);
571  return setZero();
572 }
573 
584 template <typename Derived>
586  resize(rows, cols);
587  return setZero();
588 }
589 
597 template <typename Derived>
599  return setZero(rows(), cols);
600 }
601 
609 template <typename Derived>
611  return setZero(rows, cols());
612 }
613 
614 // ones:
615 
630 template <typename Derived>
632  Index rows, Index cols) {
633  return Constant(rows, cols, Scalar(1));
634 }
635 
652 template <typename Derived>
654  Index newSize) {
655  return Constant(newSize, Scalar(1));
656 }
657 
668 template <typename Derived>
670  return Constant(Scalar(1));
671 }
672 
681 template <typename Derived>
683  return isApproxToConstant(Scalar(1), prec);
684 }
685 
693 template <typename Derived>
695  return setConstant(Scalar(1));
696 }
697 
707 template <typename Derived>
709  resize(newSize);
710  return setConstant(Scalar(1));
711 }
712 
723 template <typename Derived>
725  resize(rows, cols);
726  return setConstant(Scalar(1));
727 }
728 
736 template <typename Derived>
738  return setOnes(rows, cols());
739 }
740 
748 template <typename Derived>
750  return setOnes(rows(), cols);
751 }
752 
753 // Identity:
754 
769 template <typename Derived>
773 }
774 
785 template <typename Derived>
789  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
790 }
791 
801 template <typename Derived>
803  typename internal::nested_eval<Derived, 1>::type self(derived());
804  for (Index j = 0; j < cols(); ++j) {
805  for (Index i = 0; i < rows(); ++i) {
806  if (i == j) {
807  if (!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec)) return false;
808  } else {
809  if (!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec)) return false;
810  }
811  }
812  }
813  return true;
814 }
815 
816 namespace internal {
817 
818 template <typename Derived, bool Big = (Derived::SizeAtCompileTime >= 16)>
820  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) {
821  return m = Derived::Identity(m.rows(), m.cols());
822  }
823 };
824 
825 template <typename Derived>
826 struct setIdentity_impl<Derived, true> {
827  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Derived& run(Derived& m) {
828  m.setZero();
829  const Index size = numext::mini(m.rows(), m.cols());
830  for (Index i = 0; i < size; ++i) m.coeffRef(i, i) = typename Derived::Scalar(1);
831  return m;
832  }
833 };
834 
835 } // end namespace internal
836 
844 template <typename Derived>
847 }
848 
859 template <typename Derived>
861  derived().resize(rows, cols);
862  return setIdentity();
863 }
864 
871 template <typename Derived>
873  Index newSize, Index i) {
875  return BasisReturnType(SquareMatrixType::Identity(newSize, newSize), i);
876 }
877 
886 template <typename Derived>
888  Index i) {
890  return BasisReturnType(SquareMatrixType::Identity(), i);
891 }
892 
900 template <typename Derived>
902  return Derived::Unit(0);
903 }
904 
912 template <typename Derived>
914  return Derived::Unit(1);
915 }
916 
924 template <typename Derived>
926  return Derived::Unit(2);
927 }
928 
936 template <typename Derived>
938  return Derived::Unit(3);
939 }
940 
949 template <typename Derived>
952  eigen_assert(i < size());
953  derived().setZero();
954  derived().coeffRef(i) = Scalar(1);
955  return derived();
956 }
957 
967 template <typename Derived>
970  eigen_assert(i < newSize);
971  derived().resize(newSize);
972  return setUnit(i);
973 }
974 
975 } // end namespace Eigen
976 
977 #endif // EIGEN_CWISE_NULLARY_OP_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
v setLinSpaced(5, 0.5f, 1.5f)
#define EIGEN_DEPRECATED
Definition: Macros.h:931
#define EIGEN_CONSTEXPR
Definition: Macros.h:758
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1171
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
v resize(3)
v setConstant(3, 5)
m setIdentity(3, 3)
v setOnes(3)
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_FIXED_SIZE(TYPE)
Definition: StaticAssert.h:40
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:110
Generic expression of a matrix where all coefficients are defined by a functor.
Definition: CwiseNullaryOp.h:64
const internal::variable_if_dynamic< Index, ColsAtCompileTime > m_cols
Definition: CwiseNullaryOp.h:87
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const
Definition: CwiseNullaryOp.h:79
const internal::variable_if_dynamic< Index, RowsAtCompileTime > m_rows
Definition: CwiseNullaryOp.h:86
EIGEN_DEVICE_FUNC CwiseNullaryOp(Index size, const NullaryOp &func=NullaryOp())
Definition: CwiseNullaryOp.h:74
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const
Definition: CwiseNullaryOp.h:80
EIGEN_DEVICE_FUNC const NullaryOp & functor() const
Definition: CwiseNullaryOp.h:83
const NullaryOp m_functor
Definition: CwiseNullaryOp.h:88
internal::dense_xpr_base< CwiseNullaryOp >::type Base
Definition: CwiseNullaryOp.h:66
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:44
static EIGEN_DEVICE_FUNC const ConstantReturnType Constant(Index rows, Index cols, const Scalar &value)
Definition: CwiseNullaryOp.h:186
static EIGEN_DEVICE_FUNC const ConstantReturnType Ones()
Definition: CwiseNullaryOp.h:669
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition: CwiseNullaryOp.h:432
EIGEN_DEVICE_FUNC Derived & setConstant(const Scalar &value)
Definition: CwiseNullaryOp.h:349
NumTraits< Scalar >::Real RealScalar
Definition: DenseBase.h:69
static EIGEN_DEVICE_FUNC const RandomAccessEqualSpacedReturnType EqualSpaced(Index size, const Scalar &low, const Scalar &step)
Definition: CwiseNullaryOp.h:304
EIGEN_DEVICE_FUNC void fill(const Scalar &value)
Definition: CwiseNullaryOp.h:339
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
EIGEN_DEPRECATED static EIGEN_DEVICE_FUNC const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &low, const Scalar &high)
Definition: CwiseNullaryOp.h:240
EIGEN_DEVICE_FUNC Derived & setEqualSpaced(Index size, const Scalar &low, const Scalar &step)
Definition: CwiseNullaryOp.h:458
static EIGEN_DEVICE_FUNC const ZeroReturnType Zero()
Definition: CwiseNullaryOp.h:525
EIGEN_DEVICE_FUNC bool isOnes(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:682
static EIGEN_DEVICE_FUNC const CwiseNullaryOp< CustomNullaryOp, PlainObject > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
EIGEN_DEVICE_FUNC bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:538
EIGEN_DEVICE_FUNC bool isApproxToConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:318
EIGEN_DEVICE_FUNC bool isConstant(const Scalar &value, const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:330
EIGEN_DEVICE_FUNC Derived & setZero()
Definition: CwiseNullaryOp.h:554
EIGEN_DEVICE_FUNC Derived & setOnes()
Definition: CwiseNullaryOp.h:694
static EIGEN_DEVICE_FUNC const BasisReturnType UnitZ()
Definition: CwiseNullaryOp.h:925
static EIGEN_DEVICE_FUNC const BasisReturnType Unit(Index size, Index i)
Definition: CwiseNullaryOp.h:872
static EIGEN_DEVICE_FUNC const IdentityReturnType Identity()
Definition: CwiseNullaryOp.h:787
bool isIdentity(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: CwiseNullaryOp.h:802
static EIGEN_DEVICE_FUNC const BasisReturnType UnitY()
Definition: CwiseNullaryOp.h:913
static EIGEN_DEVICE_FUNC const BasisReturnType UnitX()
Definition: CwiseNullaryOp.h:901
static EIGEN_DEVICE_FUNC const BasisReturnType UnitW()
Definition: CwiseNullaryOp.h:937
EIGEN_DEVICE_FUNC Derived & setIdentity()
Definition: CwiseNullaryOp.h:845
EIGEN_DEVICE_FUNC Derived & setUnit(Index i)
Set the coefficients of *this to the i-th unit (basis) vector.
Definition: CwiseNullaryOp.h:950
EIGEN_DEVICE_FUNC Derived & setOnes(Index size)
Definition: CwiseNullaryOp.h:708
internal::traits< Derived >::Scalar Scalar
Definition: PlainObjectBase.h:127
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:365
Definition: XprHelper.h:134
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR T value()
Definition: XprHelper.h:161
const unsigned int RowMajorBit
Definition: Constants.h:70
if n return
Definition: level1_cplx_impl.h:31
int * m
Definition: level2_cplx_impl.h:294
res setZero()
EIGEN_DEVICE_FUNC bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1923
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
NoChange_t
Definition: Constants.h:359
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Sequential_t
Definition: Constants.h:360
const int Dynamic
Definition: Constants.h:25
Extend namespace for flags.
Definition: fsi_chan_precond_driver.cc:56
val
Definition: calibrate.py:119
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
Definition: XprHelper.h:558
Definition: NullaryFunctors.h:172
Definition: NullaryFunctors.h:152
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: NullaryFunctors.h:21
Definition: NullaryFunctors.h:52
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:827
Definition: CwiseNullaryOp.h:819
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived & run(Derived &m)
Definition: CwiseNullaryOp.h:820
Definition: ForwardDeclarations.h:21
Definition: benchGeometry.cpp:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
void run(const string &dir_name, LinearSolver *linear_solver_pt, const unsigned nel_1d, bool mess_up_order)
Definition: two_d_poisson_compare_solvers.cc:317