1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
6
// Eigen is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 3 of the License, or (at your option) any later version.
11
// Alternatively, you can redistribute it and/or
12
// modify it under the terms of the GNU General Public License as
13
// published by the Free Software Foundation; either version 2 of
14
// the License, or (at your option) any later version.
16
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19
// GNU General Public License for more details.
21
// You should have received a copy of the GNU Lesser General Public
22
// License and a copy of the GNU General Public License along with
23
// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
#ifndef EIGEN_CWISE_NULLARY_OP_H
26
#define EIGEN_CWISE_NULLARY_OP_H
28
/** \class CwiseNullaryOp
29
* \ingroup Core_Module
31
* \brief Generic expression of a matrix where all coefficients are defined by a functor
33
* \param NullaryOp template functor implementing the operator
34
* \param PlainObjectType the underlying plain matrix/array type
36
* This class represents an expression of a generic nullary operator.
37
* It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
38
* and most of the time this is the only way it is used.
40
* However, if you want to write a function returning such an expression, you
41
* will need to use this class.
43
* \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr()
47
template<typename NullaryOp, typename PlainObjectType>
48
struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
51
Flags = (traits<PlainObjectType>::Flags
53
| (functor_has_linear_access<NullaryOp>::ret ? LinearAccessBit : 0)
54
| (functor_traits<NullaryOp>::PacketAccess ? PacketAccessBit : 0)))
55
| (functor_traits<NullaryOp>::IsRepeatable ? 0 : EvalBeforeNestingBit),
56
CoeffReadCost = functor_traits<NullaryOp>::Cost
61
template<typename NullaryOp, typename PlainObjectType>
62
class CwiseNullaryOp : internal::no_assignment_operator,
63
public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type
67
typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
68
EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
70
CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
71
: m_rows(rows), m_cols(cols), m_functor(func)
73
eigen_assert(rows >= 0
74
&& (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
76
&& (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
79
EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
80
EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
82
EIGEN_STRONG_INLINE const Scalar coeff(Index rows, Index cols) const
84
return m_functor(rows, cols);
87
template<int LoadMode>
88
EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
90
return m_functor.packetOp(row, col);
93
EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
95
return m_functor(index);
98
template<int LoadMode>
99
EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
101
return m_functor.packetOp(index);
105
const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
106
const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
107
const NullaryOp m_functor;
111
/** \returns an expression of a matrix defined by a custom functor \a func
113
* The parameters \a rows and \a cols are the number of rows and of columns of
114
* the returned matrix. Must be compatible with this MatrixBase type.
116
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
117
* it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
120
* The template parameter \a CustomNullaryOp is the type of the functor.
122
* \sa class CwiseNullaryOp
124
template<typename Derived>
125
template<typename CustomNullaryOp>
126
EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
127
DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
129
return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
132
/** \returns an expression of a matrix defined by a custom functor \a func
134
* The parameter \a size is the size of the returned vector.
135
* Must be compatible with this MatrixBase type.
139
* This variant is meant to be used for dynamic-size vector types. For fixed-size types,
140
* it is redundant to pass \a size as argument, so Zero() should be used
143
* The template parameter \a CustomNullaryOp is the type of the functor.
145
* \sa class CwiseNullaryOp
147
template<typename Derived>
148
template<typename CustomNullaryOp>
149
EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
150
DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
152
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
153
if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, Derived>(1, size, func);
154
else return CwiseNullaryOp<CustomNullaryOp, Derived>(size, 1, func);
157
/** \returns an expression of a matrix defined by a custom functor \a func
159
* This variant is only for fixed-size DenseBase types. For dynamic-size types, you
160
* need to use the variants taking size arguments.
162
* The template parameter \a CustomNullaryOp is the type of the functor.
164
* \sa class CwiseNullaryOp
166
template<typename Derived>
167
template<typename CustomNullaryOp>
168
EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
169
DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
171
return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
174
/** \returns an expression of a constant matrix of value \a value
176
* The parameters \a rows and \a cols are the number of rows and of columns of
177
* the returned matrix. Must be compatible with this DenseBase type.
179
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
180
* it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
183
* The template parameter \a CustomNullaryOp is the type of the functor.
185
* \sa class CwiseNullaryOp
187
template<typename Derived>
188
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
189
DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
191
return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
194
/** \returns an expression of a constant matrix of value \a value
196
* The parameter \a size is the size of the returned vector.
197
* Must be compatible with this DenseBase type.
201
* This variant is meant to be used for dynamic-size vector types. For fixed-size types,
202
* it is redundant to pass \a size as argument, so Zero() should be used
205
* The template parameter \a CustomNullaryOp is the type of the functor.
207
* \sa class CwiseNullaryOp
209
template<typename Derived>
210
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
211
DenseBase<Derived>::Constant(Index size, const Scalar& value)
213
return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
216
/** \returns an expression of a constant matrix of value \a value
218
* This variant is only for fixed-size DenseBase types. For dynamic-size types, you
219
* need to use the variants taking size arguments.
221
* The template parameter \a CustomNullaryOp is the type of the functor.
223
* \sa class CwiseNullaryOp
225
template<typename Derived>
226
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
227
DenseBase<Derived>::Constant(const Scalar& value)
229
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
230
return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
234
* \brief Sets a linearly space vector.
236
* The function generates 'size' equally spaced values in the closed interval [low,high].
237
* This particular version of LinSpaced() uses sequential access, i.e. vector access is
238
* assumed to be a(0), a(1), ..., a(size). This assumption allows for better vectorization
239
* and yields faster code than the random access version.
243
* Example: \include DenseBase_LinSpaced_seq.cpp
244
* Output: \verbinclude DenseBase_LinSpaced_seq.out
246
* \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp
248
template<typename Derived>
249
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
250
DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
252
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
253
return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
257
* \copydoc DenseBase::LinSpaced(Sequential_t, Index, const Scalar&, const Scalar&)
258
* Special version for fixed size types which does not require the size parameter.
260
template<typename Derived>
261
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
262
DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
264
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
265
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
266
return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime));
270
* \brief Sets a linearly space vector.
272
* The function generates 'size' equally spaced values in the closed interval [low,high].
276
* Example: \include DenseBase_LinSpaced.cpp
277
* Output: \verbinclude DenseBase_LinSpaced.out
279
* \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp
281
template<typename Derived>
282
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
283
DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
285
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
286
return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size));
290
* \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
291
* Special version for fixed size types which does not require the size parameter.
293
template<typename Derived>
294
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
295
DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
297
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
298
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
299
return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime));
302
/** \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
303
template<typename Derived>
304
bool DenseBase<Derived>::isApproxToConstant
305
(const Scalar& value, RealScalar prec) const
307
for(Index j = 0; j < cols(); ++j)
308
for(Index i = 0; i < rows(); ++i)
309
if(!internal::isApprox(this->coeff(i, j), value, prec))
314
/** This is just an alias for isApproxToConstant().
316
* \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
317
template<typename Derived>
318
bool DenseBase<Derived>::isConstant
319
(const Scalar& value, RealScalar prec) const
321
return isApproxToConstant(value, prec);
324
/** Alias for setConstant(): sets all coefficients in this expression to \a value.
326
* \sa setConstant(), Constant(), class CwiseNullaryOp
328
template<typename Derived>
329
EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& value)
334
/** Sets all coefficients in this expression to \a value.
336
* \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
338
template<typename Derived>
339
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& value)
341
return derived() = Constant(rows(), cols(), value);
344
/** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value.
348
* Example: \include Matrix_setConstant_int.cpp
349
* Output: \verbinclude Matrix_setConstant_int.out
351
* \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
353
template<typename Derived>
354
EIGEN_STRONG_INLINE Derived&
355
PlainObjectBase<Derived>::setConstant(Index size, const Scalar& value)
358
return setConstant(value);
361
/** Resizes to the given size, and sets all coefficients in this expression to the given \a value.
363
* \param rows the new number of rows
364
* \param cols the new number of columns
365
* \param value the value to which all coefficients are set
367
* Example: \include Matrix_setConstant_int_int.cpp
368
* Output: \verbinclude Matrix_setConstant_int_int.out
370
* \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
372
template<typename Derived>
373
EIGEN_STRONG_INLINE Derived&
374
PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& value)
377
return setConstant(value);
381
* \brief Sets a linearly space vector.
383
* The function generates 'size' equally spaced values in the closed interval [low,high].
387
* Example: \include DenseBase_setLinSpaced.cpp
388
* Output: \verbinclude DenseBase_setLinSpaced.out
392
template<typename Derived>
393
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index size, const Scalar& low, const Scalar& high)
395
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
396
return derived() = Derived::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
401
/** \returns an expression of a zero matrix.
403
* The parameters \a rows and \a cols are the number of rows and of columns of
404
* the returned matrix. Must be compatible with this MatrixBase type.
406
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
407
* it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
410
* Example: \include MatrixBase_zero_int_int.cpp
411
* Output: \verbinclude MatrixBase_zero_int_int.out
413
* \sa Zero(), Zero(Index)
415
template<typename Derived>
416
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
417
DenseBase<Derived>::Zero(Index rows, Index cols)
419
return Constant(rows, cols, Scalar(0));
422
/** \returns an expression of a zero vector.
424
* The parameter \a size is the size of the returned vector.
425
* Must be compatible with this MatrixBase type.
429
* This variant is meant to be used for dynamic-size vector types. For fixed-size types,
430
* it is redundant to pass \a size as argument, so Zero() should be used
433
* Example: \include MatrixBase_zero_int.cpp
434
* Output: \verbinclude MatrixBase_zero_int.out
436
* \sa Zero(), Zero(Index,Index)
438
template<typename Derived>
439
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
440
DenseBase<Derived>::Zero(Index size)
442
return Constant(size, Scalar(0));
445
/** \returns an expression of a fixed-size zero matrix or vector.
447
* This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
448
* need to use the variants taking size arguments.
450
* Example: \include MatrixBase_zero.cpp
451
* Output: \verbinclude MatrixBase_zero.out
453
* \sa Zero(Index), Zero(Index,Index)
455
template<typename Derived>
456
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
457
DenseBase<Derived>::Zero()
459
return Constant(Scalar(0));
462
/** \returns true if *this is approximately equal to the zero matrix,
463
* within the precision given by \a prec.
465
* Example: \include MatrixBase_isZero.cpp
466
* Output: \verbinclude MatrixBase_isZero.out
468
* \sa class CwiseNullaryOp, Zero()
470
template<typename Derived>
471
bool DenseBase<Derived>::isZero(RealScalar prec) const
473
for(Index j = 0; j < cols(); ++j)
474
for(Index i = 0; i < rows(); ++i)
475
if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<Scalar>(1), prec))
480
/** Sets all coefficients in this expression to zero.
482
* Example: \include MatrixBase_setZero.cpp
483
* Output: \verbinclude MatrixBase_setZero.out
485
* \sa class CwiseNullaryOp, Zero()
487
template<typename Derived>
488
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
490
return setConstant(Scalar(0));
493
/** Resizes to the given \a size, and sets all coefficients in this expression to zero.
497
* Example: \include Matrix_setZero_int.cpp
498
* Output: \verbinclude Matrix_setZero_int.out
500
* \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
502
template<typename Derived>
503
EIGEN_STRONG_INLINE Derived&
504
PlainObjectBase<Derived>::setZero(Index size)
507
return setConstant(Scalar(0));
510
/** Resizes to the given size, and sets all coefficients in this expression to zero.
512
* \param rows the new number of rows
513
* \param cols the new number of columns
515
* Example: \include Matrix_setZero_int_int.cpp
516
* Output: \verbinclude Matrix_setZero_int_int.out
518
* \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
520
template<typename Derived>
521
EIGEN_STRONG_INLINE Derived&
522
PlainObjectBase<Derived>::setZero(Index rows, Index cols)
525
return setConstant(Scalar(0));
530
/** \returns an expression of a matrix where all coefficients equal one.
532
* The parameters \a rows and \a cols are the number of rows and of columns of
533
* the returned matrix. Must be compatible with this MatrixBase type.
535
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
536
* it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
539
* Example: \include MatrixBase_ones_int_int.cpp
540
* Output: \verbinclude MatrixBase_ones_int_int.out
542
* \sa Ones(), Ones(Index), isOnes(), class Ones
544
template<typename Derived>
545
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
546
DenseBase<Derived>::Ones(Index rows, Index cols)
548
return Constant(rows, cols, Scalar(1));
551
/** \returns an expression of a vector where all coefficients equal one.
553
* The parameter \a size is the size of the returned vector.
554
* Must be compatible with this MatrixBase type.
558
* This variant is meant to be used for dynamic-size vector types. For fixed-size types,
559
* it is redundant to pass \a size as argument, so Ones() should be used
562
* Example: \include MatrixBase_ones_int.cpp
563
* Output: \verbinclude MatrixBase_ones_int.out
565
* \sa Ones(), Ones(Index,Index), isOnes(), class Ones
567
template<typename Derived>
568
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
569
DenseBase<Derived>::Ones(Index size)
571
return Constant(size, Scalar(1));
574
/** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
576
* This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
577
* need to use the variants taking size arguments.
579
* Example: \include MatrixBase_ones.cpp
580
* Output: \verbinclude MatrixBase_ones.out
582
* \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
584
template<typename Derived>
585
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
586
DenseBase<Derived>::Ones()
588
return Constant(Scalar(1));
591
/** \returns true if *this is approximately equal to the matrix where all coefficients
592
* are equal to 1, within the precision given by \a prec.
594
* Example: \include MatrixBase_isOnes.cpp
595
* Output: \verbinclude MatrixBase_isOnes.out
597
* \sa class CwiseNullaryOp, Ones()
599
template<typename Derived>
600
bool DenseBase<Derived>::isOnes
601
(RealScalar prec) const
603
return isApproxToConstant(Scalar(1), prec);
606
/** Sets all coefficients in this expression to one.
608
* Example: \include MatrixBase_setOnes.cpp
609
* Output: \verbinclude MatrixBase_setOnes.out
611
* \sa class CwiseNullaryOp, Ones()
613
template<typename Derived>
614
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
616
return setConstant(Scalar(1));
619
/** Resizes to the given \a size, and sets all coefficients in this expression to one.
623
* Example: \include Matrix_setOnes_int.cpp
624
* Output: \verbinclude Matrix_setOnes_int.out
626
* \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
628
template<typename Derived>
629
EIGEN_STRONG_INLINE Derived&
630
PlainObjectBase<Derived>::setOnes(Index size)
633
return setConstant(Scalar(1));
636
/** Resizes to the given size, and sets all coefficients in this expression to one.
638
* \param rows the new number of rows
639
* \param cols the new number of columns
641
* Example: \include Matrix_setOnes_int_int.cpp
642
* Output: \verbinclude Matrix_setOnes_int_int.out
644
* \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
646
template<typename Derived>
647
EIGEN_STRONG_INLINE Derived&
648
PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
651
return setConstant(Scalar(1));
656
/** \returns an expression of the identity matrix (not necessarily square).
658
* The parameters \a rows and \a cols are the number of rows and of columns of
659
* the returned matrix. Must be compatible with this MatrixBase type.
661
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
662
* it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
665
* Example: \include MatrixBase_identity_int_int.cpp
666
* Output: \verbinclude MatrixBase_identity_int_int.out
668
* \sa Identity(), setIdentity(), isIdentity()
670
template<typename Derived>
671
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
672
MatrixBase<Derived>::Identity(Index rows, Index cols)
674
return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
677
/** \returns an expression of the identity matrix (not necessarily square).
679
* This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
680
* need to use the variant taking size arguments.
682
* Example: \include MatrixBase_identity.cpp
683
* Output: \verbinclude MatrixBase_identity.out
685
* \sa Identity(Index,Index), setIdentity(), isIdentity()
687
template<typename Derived>
688
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
689
MatrixBase<Derived>::Identity()
691
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
692
return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
695
/** \returns true if *this is approximately equal to the identity matrix
696
* (not necessarily square),
697
* within the precision given by \a prec.
699
* Example: \include MatrixBase_isIdentity.cpp
700
* Output: \verbinclude MatrixBase_isIdentity.out
702
* \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
704
template<typename Derived>
705
bool MatrixBase<Derived>::isIdentity
706
(RealScalar prec) const
708
for(Index j = 0; j < cols(); ++j)
710
for(Index i = 0; i < rows(); ++i)
714
if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec))
719
if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec))
729
template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
730
struct setIdentity_impl
732
static EIGEN_STRONG_INLINE Derived& run(Derived& m)
734
return m = Derived::Identity(m.rows(), m.cols());
738
template<typename Derived>
739
struct setIdentity_impl<Derived, true>
741
typedef typename Derived::Index Index;
742
static EIGEN_STRONG_INLINE Derived& run(Derived& m)
745
const Index size = (std::min)(m.rows(), m.cols());
746
for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
751
} // end namespace internal
753
/** Writes the identity expression (not necessarily square) into *this.
755
* Example: \include MatrixBase_setIdentity.cpp
756
* Output: \verbinclude MatrixBase_setIdentity.out
758
* \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
760
template<typename Derived>
761
EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
763
return internal::setIdentity_impl<Derived>::run(derived());
766
/** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
768
* \param rows the new number of rows
769
* \param cols the new number of columns
771
* Example: \include Matrix_setIdentity_int_int.cpp
772
* Output: \verbinclude Matrix_setIdentity_int_int.out
774
* \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
776
template<typename Derived>
777
EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
779
derived().resize(rows, cols);
780
return setIdentity();
783
/** \returns an expression of the i-th unit (basis) vector.
787
* \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
789
template<typename Derived>
790
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index size, Index i)
792
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
793
return BasisReturnType(SquareMatrixType::Identity(size,size), i);
796
/** \returns an expression of the i-th unit (basis) vector.
800
* This variant is for fixed-size vector only.
802
* \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
804
template<typename Derived>
805
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
807
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
808
return BasisReturnType(SquareMatrixType::Identity(),i);
811
/** \returns an expression of the X axis unit vector (1{,0}^*)
815
* \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
817
template<typename Derived>
818
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
819
{ return Derived::Unit(0); }
821
/** \returns an expression of the Y axis unit vector (0,1{,0}^*)
825
* \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
827
template<typename Derived>
828
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
829
{ return Derived::Unit(1); }
831
/** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
835
* \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
837
template<typename Derived>
838
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
839
{ return Derived::Unit(2); }
841
/** \returns an expression of the W axis unit vector (0,0,0,1)
845
* \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
847
template<typename Derived>
848
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
849
{ return Derived::Unit(3); }
851
#endif // EIGEN_CWISE_NULLARY_OP_H