• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace Eigen {
14 
15 /** \class CwiseNullaryOp
16   * \ingroup Core_Module
17   *
18   * \brief Generic expression of a matrix where all coefficients are defined by a functor
19   *
20   * \param NullaryOp template functor implementing the operator
21   * \param PlainObjectType the underlying plain matrix/array type
22   *
23   * This class represents an expression of a generic nullary operator.
24   * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
25   * and most of the time this is the only way it is used.
26   *
27   * However, if you want to write a function returning such an expression, you
28   * will need to use this class.
29   *
30   * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr()
31   */
32 
33 namespace internal {
34 template<typename NullaryOp, typename PlainObjectType>
35 struct 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 
48 template<typename NullaryOp, typename PlainObjectType>
49 class 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 nbRows, Index nbCols, const NullaryOp& func = NullaryOp())
58       : m_rows(nbRows), m_cols(nbCols), m_functor(func)
59     {
60       eigen_assert(nbRows >= 0
61             && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == nbRows)
62             &&  nbCols >= 0
63             && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == nbCols));
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 rowId, Index colId) const
70     {
71       return m_functor(rowId, colId);
72     }
73 
74     template<int LoadMode>
75     EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
76     {
77       return m_functor.packetOp(rowId, colId);
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 
91     /** \returns the functor representing the nullary operation */
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 
101 /** \returns an expression of a matrix defined by a custom functor \a func
102   *
103   * The parameters \a rows and \a cols are the number of rows and of columns of
104   * the returned matrix. Must be compatible with this MatrixBase type.
105   *
106   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
107   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
108   * instead.
109   *
110   * The template parameter \a CustomNullaryOp is the type of the functor.
111   *
112   * \sa class CwiseNullaryOp
113   */
114 template<typename Derived>
115 template<typename CustomNullaryOp>
116 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
117 DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
118 {
119   return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
120 }
121 
122 /** \returns an expression of a matrix defined by a custom functor \a func
123   *
124   * The parameter \a size is the size of the returned vector.
125   * Must be compatible with this MatrixBase type.
126   *
127   * \only_for_vectors
128   *
129   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
130   * it is redundant to pass \a size as argument, so Zero() should be used
131   * instead.
132   *
133   * The template parameter \a CustomNullaryOp is the type of the functor.
134   *
135   * \sa class CwiseNullaryOp
136   */
137 template<typename Derived>
138 template<typename CustomNullaryOp>
139 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
140 DenseBase<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 
147 /** \returns an expression of a matrix defined by a custom functor \a func
148   *
149   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
150   * need to use the variants taking size arguments.
151   *
152   * The template parameter \a CustomNullaryOp is the type of the functor.
153   *
154   * \sa class CwiseNullaryOp
155   */
156 template<typename Derived>
157 template<typename CustomNullaryOp>
158 EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
159 DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
160 {
161   return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
162 }
163 
164 /** \returns an expression of a constant matrix of value \a value
165   *
166   * The parameters \a nbRows and \a nbCols are the number of rows and of columns of
167   * the returned matrix. Must be compatible with this DenseBase type.
168   *
169   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
170   * it is redundant to pass \a nbRows and \a nbCols as arguments, so Zero() should be used
171   * instead.
172   *
173   * The template parameter \a CustomNullaryOp is the type of the functor.
174   *
175   * \sa class CwiseNullaryOp
176   */
177 template<typename Derived>
178 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
179 DenseBase<Derived>::Constant(Index nbRows, Index nbCols, const Scalar& value)
180 {
181   return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_constant_op<Scalar>(value));
182 }
183 
184 /** \returns an expression of a constant matrix of value \a value
185   *
186   * The parameter \a size is the size of the returned vector.
187   * Must be compatible with this DenseBase type.
188   *
189   * \only_for_vectors
190   *
191   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
192   * it is redundant to pass \a size as argument, so Zero() should be used
193   * instead.
194   *
195   * The template parameter \a CustomNullaryOp is the type of the functor.
196   *
197   * \sa class CwiseNullaryOp
198   */
199 template<typename Derived>
200 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
201 DenseBase<Derived>::Constant(Index size, const Scalar& value)
202 {
203   return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
204 }
205 
206 /** \returns an expression of a constant matrix of value \a value
207   *
208   * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
209   * need to use the variants taking size arguments.
210   *
211   * The template parameter \a CustomNullaryOp is the type of the functor.
212   *
213   * \sa class CwiseNullaryOp
214   */
215 template<typename Derived>
216 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
217 DenseBase<Derived>::Constant(const Scalar& value)
218 {
219   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
220   return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
221 }
222 
223 /**
224   * \brief Sets a linearly space vector.
225   *
226   * The function generates 'size' equally spaced values in the closed interval [low,high].
227   * This particular version of LinSpaced() uses sequential access, i.e. vector access is
228   * assumed to be a(0), a(1), ..., a(size). This assumption allows for better vectorization
229   * and yields faster code than the random access version.
230   *
231   * When size is set to 1, a vector of length 1 containing 'high' is returned.
232   *
233   * \only_for_vectors
234   *
235   * Example: \include DenseBase_LinSpaced_seq.cpp
236   * Output: \verbinclude DenseBase_LinSpaced_seq.out
237   *
238   * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp
239   */
240 template<typename Derived>
241 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
242 DenseBase<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   */
252 template<typename Derived>
253 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
254 DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
255 {
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 }
260 
261 /**
262   * \brief Sets a linearly space vector.
263   *
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   */
274 template<typename Derived>
275 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
276 DenseBase<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 
282 /**
283   * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
284   * Special version for fixed size types which does not require the size parameter.
285   */
286 template<typename Derived>
287 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
288 DenseBase<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 
295 /** \returns true if all coefficients in this matrix are approximately equal to \a val, to within precision \a prec */
296 template<typename Derived>
297 bool DenseBase<Derived>::isApproxToConstant
298 (const Scalar& val, const 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), val, prec))
303         return false;
304   return true;
305 }
306 
307 /** This is just an alias for isApproxToConstant().
308   *
309   * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
310 template<typename Derived>
311 bool DenseBase<Derived>::isConstant
312 (const Scalar& val, const RealScalar& prec) const
313 {
314   return isApproxToConstant(val, prec);
315 }
316 
317 /** Alias for setConstant(): sets all coefficients in this expression to \a val.
318   *
319   * \sa setConstant(), Constant(), class CwiseNullaryOp
320   */
321 template<typename Derived>
322 EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
323 {
324   setConstant(val);
325 }
326 
327 /** Sets all coefficients in this expression to \a value.
328   *
329   * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
330   */
331 template<typename Derived>
332 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
333 {
334   return derived() = Constant(rows(), cols(), val);
335 }
336 
337 /** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value.
338   *
339   * \only_for_vectors
340   *
341   * Example: \include Matrix_setConstant_int.cpp
342   * Output: \verbinclude Matrix_setConstant_int.out
343   *
344   * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
345   */
346 template<typename Derived>
347 EIGEN_STRONG_INLINE Derived&
348 PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
349 {
350   resize(size);
351   return setConstant(val);
352 }
353 
354 /** Resizes to the given size, and sets all coefficients in this expression to the given \a value.
355   *
356   * \param nbRows the new number of rows
357   * \param nbCols the new number of columns
358   * \param val the value to which all coefficients are set
359   *
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   */
365 template<typename Derived>
366 EIGEN_STRONG_INLINE Derived&
367 PlainObjectBase<Derived>::setConstant(Index nbRows, Index nbCols, const Scalar& val)
368 {
369   resize(nbRows, nbCols);
370   return setConstant(val);
371 }
372 
373 /**
374   * \brief Sets a linearly space vector.
375   *
376   * The function generates 'size' equally spaced values in the closed interval [low,high].
377   * When size is set to 1, a vector of length 1 containing 'high' is returned.
378   *
379   * \only_for_vectors
380   *
381   * Example: \include DenseBase_setLinSpaced.cpp
382   * Output: \verbinclude DenseBase_setLinSpaced.out
383   *
384   * \sa CwiseNullaryOp
385   */
386 template<typename Derived>
387 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
388 {
389   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
390   return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,false>(low,high,newSize));
391 }
392 
393 /**
394   * \brief Sets a linearly space vector.
395   *
396   * The function fill *this with equally spaced values in the closed interval [low,high].
397   * When size is set to 1, a vector of length 1 containing 'high' is returned.
398   *
399   * \only_for_vectors
400   *
401   * \sa setLinSpaced(Index, const Scalar&, const Scalar&), CwiseNullaryOp
402   */
403 template<typename Derived>
404 EIGEN_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 
412 /** \returns an expression of a zero matrix.
413   *
414   * The parameters \a rows and \a cols are the number of rows and of columns of
415   * the returned matrix. Must be compatible with this MatrixBase type.
416   *
417   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
418   * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
419   * instead.
420   *
421   * Example: \include MatrixBase_zero_int_int.cpp
422   * Output: \verbinclude MatrixBase_zero_int_int.out
423   *
424   * \sa Zero(), Zero(Index)
425   */
426 template<typename Derived>
427 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
428 DenseBase<Derived>::Zero(Index nbRows, Index nbCols)
429 {
430   return Constant(nbRows, nbCols, Scalar(0));
431 }
432 
433 /** \returns an expression of a zero vector.
434   *
435   * The parameter \a size is the size of the returned vector.
436   * Must be compatible with this MatrixBase type.
437   *
438   * \only_for_vectors
439   *
440   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
441   * it is redundant to pass \a size as argument, so Zero() should be used
442   * instead.
443   *
444   * Example: \include MatrixBase_zero_int.cpp
445   * Output: \verbinclude MatrixBase_zero_int.out
446   *
447   * \sa Zero(), Zero(Index,Index)
448   */
449 template<typename Derived>
450 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
451 DenseBase<Derived>::Zero(Index size)
452 {
453   return Constant(size, Scalar(0));
454 }
455 
456 /** \returns an expression of a fixed-size zero matrix or vector.
457   *
458   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
459   * need to use the variants taking size arguments.
460   *
461   * Example: \include MatrixBase_zero.cpp
462   * Output: \verbinclude MatrixBase_zero.out
463   *
464   * \sa Zero(Index), Zero(Index,Index)
465   */
466 template<typename Derived>
467 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
468 DenseBase<Derived>::Zero()
469 {
470   return Constant(Scalar(0));
471 }
472 
473 /** \returns true if *this is approximately equal to the zero matrix,
474   *          within the precision given by \a prec.
475   *
476   * Example: \include MatrixBase_isZero.cpp
477   * Output: \verbinclude MatrixBase_isZero.out
478   *
479   * \sa class CwiseNullaryOp, Zero()
480   */
481 template<typename Derived>
482 bool DenseBase<Derived>::isZero(const 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 
491 /** Sets all coefficients in this expression to zero.
492   *
493   * Example: \include MatrixBase_setZero.cpp
494   * Output: \verbinclude MatrixBase_setZero.out
495   *
496   * \sa class CwiseNullaryOp, Zero()
497   */
498 template<typename Derived>
499 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
500 {
501   return setConstant(Scalar(0));
502 }
503 
504 /** Resizes to the given \a size, and sets all coefficients in this expression to zero.
505   *
506   * \only_for_vectors
507   *
508   * Example: \include Matrix_setZero_int.cpp
509   * Output: \verbinclude Matrix_setZero_int.out
510   *
511   * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
512   */
513 template<typename Derived>
514 EIGEN_STRONG_INLINE Derived&
515 PlainObjectBase<Derived>::setZero(Index newSize)
516 {
517   resize(newSize);
518   return setConstant(Scalar(0));
519 }
520 
521 /** Resizes to the given size, and sets all coefficients in this expression to zero.
522   *
523   * \param nbRows the new number of rows
524   * \param nbCols the new number of columns
525   *
526   * Example: \include Matrix_setZero_int_int.cpp
527   * Output: \verbinclude Matrix_setZero_int_int.out
528   *
529   * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
530   */
531 template<typename Derived>
532 EIGEN_STRONG_INLINE Derived&
533 PlainObjectBase<Derived>::setZero(Index nbRows, Index nbCols)
534 {
535   resize(nbRows, nbCols);
536   return setConstant(Scalar(0));
537 }
538 
539 // ones:
540 
541 /** \returns an expression of a matrix where all coefficients equal one.
542   *
543   * The parameters \a nbRows and \a nbCols are the number of rows and of columns of
544   * the returned matrix. Must be compatible with this MatrixBase type.
545   *
546   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
547   * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
548   * instead.
549   *
550   * Example: \include MatrixBase_ones_int_int.cpp
551   * Output: \verbinclude MatrixBase_ones_int_int.out
552   *
553   * \sa Ones(), Ones(Index), isOnes(), class Ones
554   */
555 template<typename Derived>
556 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
557 DenseBase<Derived>::Ones(Index nbRows, Index nbCols)
558 {
559   return Constant(nbRows, nbCols, Scalar(1));
560 }
561 
562 /** \returns an expression of a vector where all coefficients equal one.
563   *
564   * The parameter \a newSize is the size of the returned vector.
565   * Must be compatible with this MatrixBase type.
566   *
567   * \only_for_vectors
568   *
569   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
570   * it is redundant to pass \a size as argument, so Ones() should be used
571   * instead.
572   *
573   * Example: \include MatrixBase_ones_int.cpp
574   * Output: \verbinclude MatrixBase_ones_int.out
575   *
576   * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
577   */
578 template<typename Derived>
579 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
580 DenseBase<Derived>::Ones(Index newSize)
581 {
582   return Constant(newSize, Scalar(1));
583 }
584 
585 /** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
586   *
587   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
588   * need to use the variants taking size arguments.
589   *
590   * Example: \include MatrixBase_ones.cpp
591   * Output: \verbinclude MatrixBase_ones.out
592   *
593   * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
594   */
595 template<typename Derived>
596 EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
597 DenseBase<Derived>::Ones()
598 {
599   return Constant(Scalar(1));
600 }
601 
602 /** \returns true if *this is approximately equal to the matrix where all coefficients
603   *          are equal to 1, within the precision given by \a prec.
604   *
605   * Example: \include MatrixBase_isOnes.cpp
606   * Output: \verbinclude MatrixBase_isOnes.out
607   *
608   * \sa class CwiseNullaryOp, Ones()
609   */
610 template<typename Derived>
611 bool DenseBase<Derived>::isOnes
612 (const RealScalar& prec) const
613 {
614   return isApproxToConstant(Scalar(1), prec);
615 }
616 
617 /** Sets all coefficients in this expression to one.
618   *
619   * Example: \include MatrixBase_setOnes.cpp
620   * Output: \verbinclude MatrixBase_setOnes.out
621   *
622   * \sa class CwiseNullaryOp, Ones()
623   */
624 template<typename Derived>
625 EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
626 {
627   return setConstant(Scalar(1));
628 }
629 
630 /** Resizes to the given \a newSize, and sets all coefficients in this expression to one.
631   *
632   * \only_for_vectors
633   *
634   * Example: \include Matrix_setOnes_int.cpp
635   * Output: \verbinclude Matrix_setOnes_int.out
636   *
637   * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
638   */
639 template<typename Derived>
640 EIGEN_STRONG_INLINE Derived&
641 PlainObjectBase<Derived>::setOnes(Index newSize)
642 {
643   resize(newSize);
644   return setConstant(Scalar(1));
645 }
646 
647 /** Resizes to the given size, and sets all coefficients in this expression to one.
648   *
649   * \param nbRows the new number of rows
650   * \param nbCols the new number of columns
651   *
652   * Example: \include Matrix_setOnes_int_int.cpp
653   * Output: \verbinclude Matrix_setOnes_int_int.out
654   *
655   * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
656   */
657 template<typename Derived>
658 EIGEN_STRONG_INLINE Derived&
659 PlainObjectBase<Derived>::setOnes(Index nbRows, Index nbCols)
660 {
661   resize(nbRows, nbCols);
662   return setConstant(Scalar(1));
663 }
664 
665 // Identity:
666 
667 /** \returns an expression of the identity matrix (not necessarily square).
668   *
669   * The parameters \a nbRows and \a nbCols are the number of rows and of columns of
670   * the returned matrix. Must be compatible with this MatrixBase type.
671   *
672   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
673   * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
674   * instead.
675   *
676   * Example: \include MatrixBase_identity_int_int.cpp
677   * Output: \verbinclude MatrixBase_identity_int_int.out
678   *
679   * \sa Identity(), setIdentity(), isIdentity()
680   */
681 template<typename Derived>
682 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
683 MatrixBase<Derived>::Identity(Index nbRows, Index nbCols)
684 {
685   return DenseBase<Derived>::NullaryExpr(nbRows, nbCols, internal::scalar_identity_op<Scalar>());
686 }
687 
688 /** \returns an expression of the identity matrix (not necessarily square).
689   *
690   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
691   * need to use the variant taking size arguments.
692   *
693   * Example: \include MatrixBase_identity.cpp
694   * Output: \verbinclude MatrixBase_identity.out
695   *
696   * \sa Identity(Index,Index), setIdentity(), isIdentity()
697   */
698 template<typename Derived>
699 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
700 MatrixBase<Derived>::Identity()
701 {
702   EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
703   return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
704 }
705 
706 /** \returns true if *this is approximately equal to the identity matrix
707   *          (not necessarily square),
708   *          within the precision given by \a prec.
709   *
710   * Example: \include MatrixBase_isIdentity.cpp
711   * Output: \verbinclude MatrixBase_isIdentity.out
712   *
713   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
714   */
715 template<typename Derived>
716 bool MatrixBase<Derived>::isIdentity
717 (const 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 
738 namespace internal {
739 
740 template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
741 struct 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 
749 template<typename Derived>
750 struct 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 
764 /** Writes the identity expression (not necessarily square) into *this.
765   *
766   * Example: \include MatrixBase_setIdentity.cpp
767   * Output: \verbinclude MatrixBase_setIdentity.out
768   *
769   * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
770   */
771 template<typename Derived>
772 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
773 {
774   return internal::setIdentity_impl<Derived>::run(derived());
775 }
776 
777 /** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
778   *
779   * \param nbRows the new number of rows
780   * \param nbCols the new number of columns
781   *
782   * Example: \include Matrix_setIdentity_int_int.cpp
783   * Output: \verbinclude Matrix_setIdentity_int_int.out
784   *
785   * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
786   */
787 template<typename Derived>
788 EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index nbRows, Index nbCols)
789 {
790   derived().resize(nbRows, nbCols);
791   return setIdentity();
792 }
793 
794 /** \returns an expression of the i-th unit (basis) vector.
795   *
796   * \only_for_vectors
797   *
798   * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
799   */
800 template<typename Derived>
801 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
802 {
803   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
804   return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
805 }
806 
807 /** \returns an expression of the i-th unit (basis) vector.
808   *
809   * \only_for_vectors
810   *
811   * This variant is for fixed-size vector only.
812   *
813   * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
814   */
815 template<typename Derived>
816 EIGEN_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 
822 /** \returns an expression of the X axis unit vector (1{,0}^*)
823   *
824   * \only_for_vectors
825   *
826   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
827   */
828 template<typename Derived>
829 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
830 { return Derived::Unit(0); }
831 
832 /** \returns an expression of the Y axis unit vector (0,1{,0}^*)
833   *
834   * \only_for_vectors
835   *
836   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
837   */
838 template<typename Derived>
839 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
840 { return Derived::Unit(1); }
841 
842 /** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
843   *
844   * \only_for_vectors
845   *
846   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
847   */
848 template<typename Derived>
849 EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
850 { return Derived::Unit(2); }
851 
852 /** \returns an expression of the W axis unit vector (0,0,0,1)
853   *
854   * \only_for_vectors
855   *
856   * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
857   */
858 template<typename Derived>
859 EIGEN_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
865