~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to extern/Eigen3/Eigen/src/Core/CwiseNullaryOp.h

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// 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.
 
10
//
 
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.
 
15
//
 
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.
 
20
//
 
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/>.
 
24
 
 
25
#ifndef EIGEN_CWISE_NULLARY_OP_H
 
26
#define EIGEN_CWISE_NULLARY_OP_H
 
27
 
 
28
/** \class CwiseNullaryOp
 
29
  * \ingroup Core_Module
 
30
  *
 
31
  * \brief Generic expression of a matrix where all coefficients are defined by a functor
 
32
  *
 
33
  * \param NullaryOp template functor implementing the operator
 
34
  * \param PlainObjectType the underlying plain matrix/array type
 
35
  *
 
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.
 
39
  *
 
40
  * However, if you want to write a function returning such an expression, you
 
41
  * will need to use this class.
 
42
  *
 
43
  * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr()
 
44
  */
 
45
 
 
46
namespace internal {
 
47
template<typename NullaryOp, typename PlainObjectType>
 
48
struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
 
49
{
 
50
  enum {
 
51
    Flags = (traits<PlainObjectType>::Flags
 
52
      & (  HereditaryBits
 
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
 
57
  };
 
58
};
 
59
}
 
60
 
 
61
template<typename NullaryOp, typename PlainObjectType>
 
62
class CwiseNullaryOp : internal::no_assignment_operator,
 
63
  public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type
 
64
{
 
65
  public:
 
66
 
 
67
    typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
 
68
    EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)
 
69
 
 
70
    CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
 
71
      : m_rows(rows), m_cols(cols), m_functor(func)
 
72
    {
 
73
      eigen_assert(rows >= 0
 
74
            && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
 
75
            &&  cols >= 0
 
76
            && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
 
77
    }
 
78
 
 
79
    EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
 
80
    EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }
 
81
 
 
82
    EIGEN_STRONG_INLINE const Scalar coeff(Index rows, Index cols) const
 
83
    {
 
84
      return m_functor(rows, cols);
 
85
    }
 
86
 
 
87
    template<int LoadMode>
 
88
    EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
 
89
    {
 
90
      return m_functor.packetOp(row, col);
 
91
    }
 
92
 
 
93
    EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
 
94
    {
 
95
      return m_functor(index);
 
96
    }
 
97
 
 
98
    template<int LoadMode>
 
99
    EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
 
100
    {
 
101
      return m_functor.packetOp(index);
 
102
    }
 
103
 
 
104
  protected:
 
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;
 
108
};
 
109
 
 
110
 
 
111
/** \returns an expression of a matrix defined by a custom functor \a func
 
112
  *
 
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.
 
115
  *
 
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
 
118
  * instead.
 
119
  *
 
120
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
121
  *
 
122
  * \sa class CwiseNullaryOp
 
123
  */
 
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)
 
128
{
 
129
  return CwiseNullaryOp<CustomNullaryOp, Derived>(rows, cols, func);
 
130
}
 
131
 
 
132
/** \returns an expression of a matrix defined by a custom functor \a func
 
133
  *
 
134
  * The parameter \a size is the size of the returned vector.
 
135
  * Must be compatible with this MatrixBase type.
 
136
  *
 
137
  * \only_for_vectors
 
138
  *
 
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
 
141
  * instead.
 
142
  *
 
143
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
144
  *
 
145
  * \sa class CwiseNullaryOp
 
146
  */
 
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)
 
151
{
 
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);
 
155
}
 
156
 
 
157
/** \returns an expression of a matrix defined by a custom functor \a func
 
158
  *
 
159
  * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
 
160
  * need to use the variants taking size arguments.
 
161
  *
 
162
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
163
  *
 
164
  * \sa class CwiseNullaryOp
 
165
  */
 
166
template<typename Derived>
 
167
template<typename CustomNullaryOp>
 
168
EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, Derived>
 
169
DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
 
170
{
 
171
  return CwiseNullaryOp<CustomNullaryOp, Derived>(RowsAtCompileTime, ColsAtCompileTime, func);
 
172
}
 
173
 
 
174
/** \returns an expression of a constant matrix of value \a value
 
175
  *
 
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.
 
178
  *
 
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
 
181
  * instead.
 
182
  *
 
183
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
184
  *
 
185
  * \sa class CwiseNullaryOp
 
186
  */
 
187
template<typename Derived>
 
188
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
189
DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
 
190
{
 
191
  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
 
192
}
 
193
 
 
194
/** \returns an expression of a constant matrix of value \a value
 
195
  *
 
196
  * The parameter \a size is the size of the returned vector.
 
197
  * Must be compatible with this DenseBase type.
 
198
  *
 
199
  * \only_for_vectors
 
200
  *
 
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
 
203
  * instead.
 
204
  *
 
205
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
206
  *
 
207
  * \sa class CwiseNullaryOp
 
208
  */
 
209
template<typename Derived>
 
210
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
211
DenseBase<Derived>::Constant(Index size, const Scalar& value)
 
212
{
 
213
  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
 
214
}
 
215
 
 
216
/** \returns an expression of a constant matrix of value \a value
 
217
  *
 
218
  * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
 
219
  * need to use the variants taking size arguments.
 
220
  *
 
221
  * The template parameter \a CustomNullaryOp is the type of the functor.
 
222
  *
 
223
  * \sa class CwiseNullaryOp
 
224
  */
 
225
template<typename Derived>
 
226
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
227
DenseBase<Derived>::Constant(const Scalar& value)
 
228
{
 
229
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
 
230
  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
 
231
}
 
232
 
 
233
/**
 
234
  * \brief Sets a linearly space vector.
 
235
  *
 
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.
 
240
  *
 
241
  * \only_for_vectors
 
242
  *
 
243
  * Example: \include DenseBase_LinSpaced_seq.cpp
 
244
  * Output: \verbinclude DenseBase_LinSpaced_seq.out
 
245
  *
 
246
  * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp
 
247
  */
 
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)
 
251
{
 
252
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
 
253
  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
 
254
}
 
255
 
 
256
/**
 
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.
 
259
  */
 
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)
 
263
{
 
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));
 
267
}
 
268
 
 
269
/**
 
270
  * \brief Sets a linearly space vector.
 
271
  *
 
272
  * The function generates 'size' equally spaced values in the closed interval [low,high].
 
273
  *
 
274
  * \only_for_vectors
 
275
  *
 
276
  * Example: \include DenseBase_LinSpaced.cpp
 
277
  * Output: \verbinclude DenseBase_LinSpaced.out
 
278
  *
 
279
  * \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp
 
280
  */
 
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)
 
284
{
 
285
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
 
286
  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,true>(low,high,size));
 
287
}
 
288
 
 
289
/**
 
290
  * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
 
291
  * Special version for fixed size types which does not require the size parameter.
 
292
  */
 
293
template<typename Derived>
 
294
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
 
295
DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
 
296
{
 
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));
 
300
}
 
301
 
 
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
 
306
{
 
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))
 
310
        return false;
 
311
  return true;
 
312
}
 
313
 
 
314
/** This is just an alias for isApproxToConstant().
 
315
  *
 
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
 
320
{
 
321
  return isApproxToConstant(value, prec);
 
322
}
 
323
 
 
324
/** Alias for setConstant(): sets all coefficients in this expression to \a value.
 
325
  *
 
326
  * \sa setConstant(), Constant(), class CwiseNullaryOp
 
327
  */
 
328
template<typename Derived>
 
329
EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& value)
 
330
{
 
331
  setConstant(value);
 
332
}
 
333
 
 
334
/** Sets all coefficients in this expression to \a value.
 
335
  *
 
336
  * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
 
337
  */
 
338
template<typename Derived>
 
339
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& value)
 
340
{
 
341
  return derived() = Constant(rows(), cols(), value);
 
342
}
 
343
 
 
344
/** Resizes to the given \a size, and sets all coefficients in this expression to the given \a value.
 
345
  *
 
346
  * \only_for_vectors
 
347
  *
 
348
  * Example: \include Matrix_setConstant_int.cpp
 
349
  * Output: \verbinclude Matrix_setConstant_int.out
 
350
  *
 
351
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
 
352
  */
 
353
template<typename Derived>
 
354
EIGEN_STRONG_INLINE Derived&
 
355
PlainObjectBase<Derived>::setConstant(Index size, const Scalar& value)
 
356
{
 
357
  resize(size);
 
358
  return setConstant(value);
 
359
}
 
360
 
 
361
/** Resizes to the given size, and sets all coefficients in this expression to the given \a value.
 
362
  *
 
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
 
366
  *
 
367
  * Example: \include Matrix_setConstant_int_int.cpp
 
368
  * Output: \verbinclude Matrix_setConstant_int_int.out
 
369
  *
 
370
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
 
371
  */
 
372
template<typename Derived>
 
373
EIGEN_STRONG_INLINE Derived&
 
374
PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& value)
 
375
{
 
376
  resize(rows, cols);
 
377
  return setConstant(value);
 
378
}
 
379
 
 
380
/**
 
381
  * \brief Sets a linearly space vector.
 
382
  *
 
383
  * The function generates 'size' equally spaced values in the closed interval [low,high].
 
384
  *
 
385
  * \only_for_vectors
 
386
  *
 
387
  * Example: \include DenseBase_setLinSpaced.cpp
 
388
  * Output: \verbinclude DenseBase_setLinSpaced.out
 
389
  *
 
390
  * \sa CwiseNullaryOp
 
391
  */
 
392
template<typename Derived>
 
393
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index size, const Scalar& low, const Scalar& high)
 
394
{
 
395
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
 
396
  return derived() = Derived::NullaryExpr(size, internal::linspaced_op<Scalar,false>(low,high,size));
 
397
}
 
398
 
 
399
// zero:
 
400
 
 
401
/** \returns an expression of a zero matrix.
 
402
  *
 
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.
 
405
  *
 
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
 
408
  * instead.
 
409
  *
 
410
  * Example: \include MatrixBase_zero_int_int.cpp
 
411
  * Output: \verbinclude MatrixBase_zero_int_int.out
 
412
  *
 
413
  * \sa Zero(), Zero(Index)
 
414
  */
 
415
template<typename Derived>
 
416
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
417
DenseBase<Derived>::Zero(Index rows, Index cols)
 
418
{
 
419
  return Constant(rows, cols, Scalar(0));
 
420
}
 
421
 
 
422
/** \returns an expression of a zero vector.
 
423
  *
 
424
  * The parameter \a size is the size of the returned vector.
 
425
  * Must be compatible with this MatrixBase type.
 
426
  *
 
427
  * \only_for_vectors
 
428
  *
 
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
 
431
  * instead.
 
432
  *
 
433
  * Example: \include MatrixBase_zero_int.cpp
 
434
  * Output: \verbinclude MatrixBase_zero_int.out
 
435
  *
 
436
  * \sa Zero(), Zero(Index,Index)
 
437
  */
 
438
template<typename Derived>
 
439
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
440
DenseBase<Derived>::Zero(Index size)
 
441
{
 
442
  return Constant(size, Scalar(0));
 
443
}
 
444
 
 
445
/** \returns an expression of a fixed-size zero matrix or vector.
 
446
  *
 
447
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
 
448
  * need to use the variants taking size arguments.
 
449
  *
 
450
  * Example: \include MatrixBase_zero.cpp
 
451
  * Output: \verbinclude MatrixBase_zero.out
 
452
  *
 
453
  * \sa Zero(Index), Zero(Index,Index)
 
454
  */
 
455
template<typename Derived>
 
456
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
457
DenseBase<Derived>::Zero()
 
458
{
 
459
  return Constant(Scalar(0));
 
460
}
 
461
 
 
462
/** \returns true if *this is approximately equal to the zero matrix,
 
463
  *          within the precision given by \a prec.
 
464
  *
 
465
  * Example: \include MatrixBase_isZero.cpp
 
466
  * Output: \verbinclude MatrixBase_isZero.out
 
467
  *
 
468
  * \sa class CwiseNullaryOp, Zero()
 
469
  */
 
470
template<typename Derived>
 
471
bool DenseBase<Derived>::isZero(RealScalar prec) const
 
472
{
 
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))
 
476
        return false;
 
477
  return true;
 
478
}
 
479
 
 
480
/** Sets all coefficients in this expression to zero.
 
481
  *
 
482
  * Example: \include MatrixBase_setZero.cpp
 
483
  * Output: \verbinclude MatrixBase_setZero.out
 
484
  *
 
485
  * \sa class CwiseNullaryOp, Zero()
 
486
  */
 
487
template<typename Derived>
 
488
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
 
489
{
 
490
  return setConstant(Scalar(0));
 
491
}
 
492
 
 
493
/** Resizes to the given \a size, and sets all coefficients in this expression to zero.
 
494
  *
 
495
  * \only_for_vectors
 
496
  *
 
497
  * Example: \include Matrix_setZero_int.cpp
 
498
  * Output: \verbinclude Matrix_setZero_int.out
 
499
  *
 
500
  * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
 
501
  */
 
502
template<typename Derived>
 
503
EIGEN_STRONG_INLINE Derived&
 
504
PlainObjectBase<Derived>::setZero(Index size)
 
505
{
 
506
  resize(size);
 
507
  return setConstant(Scalar(0));
 
508
}
 
509
 
 
510
/** Resizes to the given size, and sets all coefficients in this expression to zero.
 
511
  *
 
512
  * \param rows the new number of rows
 
513
  * \param cols the new number of columns
 
514
  *
 
515
  * Example: \include Matrix_setZero_int_int.cpp
 
516
  * Output: \verbinclude Matrix_setZero_int_int.out
 
517
  *
 
518
  * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
 
519
  */
 
520
template<typename Derived>
 
521
EIGEN_STRONG_INLINE Derived&
 
522
PlainObjectBase<Derived>::setZero(Index rows, Index cols)
 
523
{
 
524
  resize(rows, cols);
 
525
  return setConstant(Scalar(0));
 
526
}
 
527
 
 
528
// ones:
 
529
 
 
530
/** \returns an expression of a matrix where all coefficients equal one.
 
531
  *
 
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.
 
534
  *
 
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
 
537
  * instead.
 
538
  *
 
539
  * Example: \include MatrixBase_ones_int_int.cpp
 
540
  * Output: \verbinclude MatrixBase_ones_int_int.out
 
541
  *
 
542
  * \sa Ones(), Ones(Index), isOnes(), class Ones
 
543
  */
 
544
template<typename Derived>
 
545
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
546
DenseBase<Derived>::Ones(Index rows, Index cols)
 
547
{
 
548
  return Constant(rows, cols, Scalar(1));
 
549
}
 
550
 
 
551
/** \returns an expression of a vector where all coefficients equal one.
 
552
  *
 
553
  * The parameter \a size is the size of the returned vector.
 
554
  * Must be compatible with this MatrixBase type.
 
555
  *
 
556
  * \only_for_vectors
 
557
  *
 
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
 
560
  * instead.
 
561
  *
 
562
  * Example: \include MatrixBase_ones_int.cpp
 
563
  * Output: \verbinclude MatrixBase_ones_int.out
 
564
  *
 
565
  * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
 
566
  */
 
567
template<typename Derived>
 
568
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
569
DenseBase<Derived>::Ones(Index size)
 
570
{
 
571
  return Constant(size, Scalar(1));
 
572
}
 
573
 
 
574
/** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
 
575
  *
 
576
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
 
577
  * need to use the variants taking size arguments.
 
578
  *
 
579
  * Example: \include MatrixBase_ones.cpp
 
580
  * Output: \verbinclude MatrixBase_ones.out
 
581
  *
 
582
  * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
 
583
  */
 
584
template<typename Derived>
 
585
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
 
586
DenseBase<Derived>::Ones()
 
587
{
 
588
  return Constant(Scalar(1));
 
589
}
 
590
 
 
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.
 
593
  *
 
594
  * Example: \include MatrixBase_isOnes.cpp
 
595
  * Output: \verbinclude MatrixBase_isOnes.out
 
596
  *
 
597
  * \sa class CwiseNullaryOp, Ones()
 
598
  */
 
599
template<typename Derived>
 
600
bool DenseBase<Derived>::isOnes
 
601
(RealScalar prec) const
 
602
{
 
603
  return isApproxToConstant(Scalar(1), prec);
 
604
}
 
605
 
 
606
/** Sets all coefficients in this expression to one.
 
607
  *
 
608
  * Example: \include MatrixBase_setOnes.cpp
 
609
  * Output: \verbinclude MatrixBase_setOnes.out
 
610
  *
 
611
  * \sa class CwiseNullaryOp, Ones()
 
612
  */
 
613
template<typename Derived>
 
614
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
 
615
{
 
616
  return setConstant(Scalar(1));
 
617
}
 
618
 
 
619
/** Resizes to the given \a size, and sets all coefficients in this expression to one.
 
620
  *
 
621
  * \only_for_vectors
 
622
  *
 
623
  * Example: \include Matrix_setOnes_int.cpp
 
624
  * Output: \verbinclude Matrix_setOnes_int.out
 
625
  *
 
626
  * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
 
627
  */
 
628
template<typename Derived>
 
629
EIGEN_STRONG_INLINE Derived&
 
630
PlainObjectBase<Derived>::setOnes(Index size)
 
631
{
 
632
  resize(size);
 
633
  return setConstant(Scalar(1));
 
634
}
 
635
 
 
636
/** Resizes to the given size, and sets all coefficients in this expression to one.
 
637
  *
 
638
  * \param rows the new number of rows
 
639
  * \param cols the new number of columns
 
640
  *
 
641
  * Example: \include Matrix_setOnes_int_int.cpp
 
642
  * Output: \verbinclude Matrix_setOnes_int_int.out
 
643
  *
 
644
  * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
 
645
  */
 
646
template<typename Derived>
 
647
EIGEN_STRONG_INLINE Derived&
 
648
PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
 
649
{
 
650
  resize(rows, cols);
 
651
  return setConstant(Scalar(1));
 
652
}
 
653
 
 
654
// Identity:
 
655
 
 
656
/** \returns an expression of the identity matrix (not necessarily square).
 
657
  *
 
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.
 
660
  *
 
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
 
663
  * instead.
 
664
  *
 
665
  * Example: \include MatrixBase_identity_int_int.cpp
 
666
  * Output: \verbinclude MatrixBase_identity_int_int.out
 
667
  *
 
668
  * \sa Identity(), setIdentity(), isIdentity()
 
669
  */
 
670
template<typename Derived>
 
671
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
 
672
MatrixBase<Derived>::Identity(Index rows, Index cols)
 
673
{
 
674
  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
 
675
}
 
676
 
 
677
/** \returns an expression of the identity matrix (not necessarily square).
 
678
  *
 
679
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
 
680
  * need to use the variant taking size arguments.
 
681
  *
 
682
  * Example: \include MatrixBase_identity.cpp
 
683
  * Output: \verbinclude MatrixBase_identity.out
 
684
  *
 
685
  * \sa Identity(Index,Index), setIdentity(), isIdentity()
 
686
  */
 
687
template<typename Derived>
 
688
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
 
689
MatrixBase<Derived>::Identity()
 
690
{
 
691
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
 
692
  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
 
693
}
 
694
 
 
695
/** \returns true if *this is approximately equal to the identity matrix
 
696
  *          (not necessarily square),
 
697
  *          within the precision given by \a prec.
 
698
  *
 
699
  * Example: \include MatrixBase_isIdentity.cpp
 
700
  * Output: \verbinclude MatrixBase_isIdentity.out
 
701
  *
 
702
  * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
 
703
  */
 
704
template<typename Derived>
 
705
bool MatrixBase<Derived>::isIdentity
 
706
(RealScalar prec) const
 
707
{
 
708
  for(Index j = 0; j < cols(); ++j)
 
709
  {
 
710
    for(Index i = 0; i < rows(); ++i)
 
711
    {
 
712
      if(i == j)
 
713
      {
 
714
        if(!internal::isApprox(this->coeff(i, j), static_cast<Scalar>(1), prec))
 
715
          return false;
 
716
      }
 
717
      else
 
718
      {
 
719
        if(!internal::isMuchSmallerThan(this->coeff(i, j), static_cast<RealScalar>(1), prec))
 
720
          return false;
 
721
      }
 
722
    }
 
723
  }
 
724
  return true;
 
725
}
 
726
 
 
727
namespace internal {
 
728
 
 
729
template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
 
730
struct setIdentity_impl
 
731
{
 
732
  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
 
733
  {
 
734
    return m = Derived::Identity(m.rows(), m.cols());
 
735
  }
 
736
};
 
737
 
 
738
template<typename Derived>
 
739
struct setIdentity_impl<Derived, true>
 
740
{
 
741
  typedef typename Derived::Index Index;
 
742
  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
 
743
  {
 
744
    m.setZero();
 
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);
 
747
    return m;
 
748
  }
 
749
};
 
750
 
 
751
} // end namespace internal
 
752
 
 
753
/** Writes the identity expression (not necessarily square) into *this.
 
754
  *
 
755
  * Example: \include MatrixBase_setIdentity.cpp
 
756
  * Output: \verbinclude MatrixBase_setIdentity.out
 
757
  *
 
758
  * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
 
759
  */
 
760
template<typename Derived>
 
761
EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
 
762
{
 
763
  return internal::setIdentity_impl<Derived>::run(derived());
 
764
}
 
765
 
 
766
/** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
 
767
  *
 
768
  * \param rows the new number of rows
 
769
  * \param cols the new number of columns
 
770
  *
 
771
  * Example: \include Matrix_setIdentity_int_int.cpp
 
772
  * Output: \verbinclude Matrix_setIdentity_int_int.out
 
773
  *
 
774
  * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
 
775
  */
 
776
template<typename Derived>
 
777
EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
 
778
{
 
779
  derived().resize(rows, cols);
 
780
  return setIdentity();
 
781
}
 
782
 
 
783
/** \returns an expression of the i-th unit (basis) vector.
 
784
  *
 
785
  * \only_for_vectors
 
786
  *
 
787
  * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
788
  */
 
789
template<typename Derived>
 
790
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index size, Index i)
 
791
{
 
792
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
 
793
  return BasisReturnType(SquareMatrixType::Identity(size,size), i);
 
794
}
 
795
 
 
796
/** \returns an expression of the i-th unit (basis) vector.
 
797
  *
 
798
  * \only_for_vectors
 
799
  *
 
800
  * This variant is for fixed-size vector only.
 
801
  *
 
802
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
803
  */
 
804
template<typename Derived>
 
805
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
 
806
{
 
807
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
 
808
  return BasisReturnType(SquareMatrixType::Identity(),i);
 
809
}
 
810
 
 
811
/** \returns an expression of the X axis unit vector (1{,0}^*)
 
812
  *
 
813
  * \only_for_vectors
 
814
  *
 
815
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
816
  */
 
817
template<typename Derived>
 
818
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
 
819
{ return Derived::Unit(0); }
 
820
 
 
821
/** \returns an expression of the Y axis unit vector (0,1{,0}^*)
 
822
  *
 
823
  * \only_for_vectors
 
824
  *
 
825
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
826
  */
 
827
template<typename Derived>
 
828
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
 
829
{ return Derived::Unit(1); }
 
830
 
 
831
/** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
 
832
  *
 
833
  * \only_for_vectors
 
834
  *
 
835
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
836
  */
 
837
template<typename Derived>
 
838
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
 
839
{ return Derived::Unit(2); }
 
840
 
 
841
/** \returns an expression of the W axis unit vector (0,0,0,1)
 
842
  *
 
843
  * \only_for_vectors
 
844
  *
 
845
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
 
846
  */
 
847
template<typename Derived>
 
848
EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
 
849
{ return Derived::Unit(3); }
 
850
 
 
851
#endif // EIGEN_CWISE_NULLARY_OP_H