CwiseNullaryOp.h 30.7 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
//
Don Gagne's avatar
Don Gagne committed
6 7 8
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
9 10 11 12

#ifndef EIGEN_CWISE_NULLARY_OP_H
#define EIGEN_CWISE_NULLARY_OP_H

Don Gagne's avatar
Don Gagne committed
13 14
namespace Eigen {

15 16 17 18 19 20 21 22 23 24 25
namespace internal {
template<typename NullaryOp, typename PlainObjectType>
struct traits<CwiseNullaryOp<NullaryOp, PlainObjectType> > : traits<PlainObjectType>
{
  enum {
    Flags = traits<PlainObjectType>::Flags & RowMajorBit
  };
};

} // namespace internal

LM's avatar
LM committed
26 27 28 29 30
/** \class CwiseNullaryOp
  * \ingroup Core_Module
  *
  * \brief Generic expression of a matrix where all coefficients are defined by a functor
  *
31 32
  * \tparam NullaryOp template functor implementing the operator
  * \tparam PlainObjectType the underlying plain matrix/array type
LM's avatar
LM committed
33 34 35 36 37 38 39 40
  *
  * This class represents an expression of a generic nullary operator.
  * It is the return type of the Ones(), Zero(), Constant(), Identity() and Random() methods,
  * and most of the time this is the only way it is used.
  *
  * However, if you want to write a function returning such an expression, you
  * will need to use this class.
  *
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  * The functor NullaryOp must expose one of the following method:
    <table class="manual">
    <tr            ><td>\c operator()() </td><td>if the procedural generation does not depend on the coefficient entries (e.g., random numbers)</td></tr>
    <tr class="alt"><td>\c operator()(Index i)</td><td>if the procedural generation makes sense for vectors only and that it depends on the coefficient index \c i (e.g., linspace) </td></tr>
    <tr            ><td>\c operator()(Index i,Index j)</td><td>if the procedural generation depends on the matrix coordinates \c i, \c j (e.g., to generate a checkerboard with 0 and 1)</td></tr>
    </table>
  * It is also possible to expose the last two operators if the generation makes sense for matrices but can be optimized for vectors.
  *
  * See DenseBase::NullaryExpr(Index,const CustomNullaryOp&) for an example binding
  * C++11 random number generators.
  *
  * A nullary expression can also be used to implement custom sophisticated matrix manipulations
  * that cannot be covered by the existing set of natively supported matrix manipulations.
  * See this \ref TopicCustomizing_NullaryExpr "page" for some examples and additional explanations
  * on the behavior of CwiseNullaryOp.
  *
  * \sa class CwiseUnaryOp, class CwiseBinaryOp, DenseBase::NullaryExpr
LM's avatar
LM committed
58 59
  */
template<typename NullaryOp, typename PlainObjectType>
60
class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
LM's avatar
LM committed
61 62 63 64 65 66
{
  public:

    typedef typename internal::dense_xpr_base<CwiseNullaryOp>::type Base;
    EIGEN_DENSE_PUBLIC_INTERFACE(CwiseNullaryOp)

67 68 69
    EIGEN_DEVICE_FUNC
    CwiseNullaryOp(Index rows, Index cols, const NullaryOp& func = NullaryOp())
      : m_rows(rows), m_cols(cols), m_functor(func)
LM's avatar
LM committed
70
    {
71 72 73 74
      eigen_assert(rows >= 0
            && (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
            &&  cols >= 0
            && (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
LM's avatar
LM committed
75 76
    }

77
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
78
    EIGEN_STRONG_INLINE Index rows() const { return m_rows.value(); }
79
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
80 81
    EIGEN_STRONG_INLINE Index cols() const { return m_cols.value(); }

Don Gagne's avatar
Don Gagne committed
82
    /** \returns the functor representing the nullary operation */
83
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
84 85
    const NullaryOp& functor() const { return m_functor; }

LM's avatar
LM committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  protected:
    const internal::variable_if_dynamic<Index, RowsAtCompileTime> m_rows;
    const internal::variable_if_dynamic<Index, ColsAtCompileTime> m_cols;
    const NullaryOp m_functor;
};


/** \returns an expression of a matrix defined by a custom functor \a func
  *
  * The parameters \a rows and \a cols are the number of rows and of columns of
  * the returned matrix. Must be compatible with this MatrixBase type.
  *
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
  * instead.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
template<typename CustomNullaryOp>
108
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
LM's avatar
LM committed
109 110
DenseBase<Derived>::NullaryExpr(Index rows, Index cols, const CustomNullaryOp& func)
{
111
  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(rows, cols, func);
LM's avatar
LM committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
}

/** \returns an expression of a matrix defined by a custom functor \a func
  *
  * The parameter \a size is the size of the returned vector.
  * Must be compatible with this MatrixBase type.
  *
  * \only_for_vectors
  *
  * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  * it is redundant to pass \a size as argument, so Zero() should be used
  * instead.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
127 128 129
  * Here is an example with C++11 random generators: \include random_cpp11.cpp
  * Output: \verbinclude random_cpp11.out
  * 
LM's avatar
LM committed
130 131 132 133
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
template<typename CustomNullaryOp>
134
EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
LM's avatar
LM committed
135 136 137
DenseBase<Derived>::NullaryExpr(Index size, const CustomNullaryOp& func)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
138 139
  if(RowsAtCompileTime == 1) return CwiseNullaryOp<CustomNullaryOp, PlainObject>(1, size, func);
  else return CwiseNullaryOp<CustomNullaryOp, PlainObject>(size, 1, func);
LM's avatar
LM committed
140 141 142 143 144 145 146 147 148 149 150 151 152
}

/** \returns an expression of a matrix defined by a custom functor \a func
  *
  * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
  * need to use the variants taking size arguments.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
template<typename CustomNullaryOp>
153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const CwiseNullaryOp<CustomNullaryOp, typename DenseBase<Derived>::PlainObject>
LM's avatar
LM committed
154 155
DenseBase<Derived>::NullaryExpr(const CustomNullaryOp& func)
{
156
  return CwiseNullaryOp<CustomNullaryOp, PlainObject>(RowsAtCompileTime, ColsAtCompileTime, func);
LM's avatar
LM committed
157 158 159 160
}

/** \returns an expression of a constant matrix of value \a value
  *
161
  * The parameters \a rows and \a cols are the number of rows and of columns of
LM's avatar
LM committed
162 163 164
  * the returned matrix. Must be compatible with this DenseBase type.
  *
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
165
  * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
LM's avatar
LM committed
166 167 168 169 170 171 172 173
  * instead.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
174
DenseBase<Derived>::Constant(Index rows, Index cols, const Scalar& value)
LM's avatar
LM committed
175
{
176
  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_constant_op<Scalar>(value));
LM's avatar
LM committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

/** \returns an expression of a constant matrix of value \a value
  *
  * The parameter \a size is the size of the returned vector.
  * Must be compatible with this DenseBase type.
  *
  * \only_for_vectors
  *
  * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  * it is redundant to pass \a size as argument, so Zero() should be used
  * instead.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
195
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
LM's avatar
LM committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
DenseBase<Derived>::Constant(Index size, const Scalar& value)
{
  return DenseBase<Derived>::NullaryExpr(size, internal::scalar_constant_op<Scalar>(value));
}

/** \returns an expression of a constant matrix of value \a value
  *
  * This variant is only for fixed-size DenseBase types. For dynamic-size types, you
  * need to use the variants taking size arguments.
  *
  * The template parameter \a CustomNullaryOp is the type of the functor.
  *
  * \sa class CwiseNullaryOp
  */
template<typename Derived>
211
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
LM's avatar
LM committed
212 213 214 215 216 217
DenseBase<Derived>::Constant(const Scalar& value)
{
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  return DenseBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_constant_op<Scalar>(value));
}

218
/** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(Index,const Scalar&,const Scalar&)
LM's avatar
LM committed
219
  *
220
  * \sa LinSpaced(Index,Scalar,Scalar), setLinSpaced(Index,const Scalar&,const Scalar&)
LM's avatar
LM committed
221 222
  */
template<typename Derived>
223
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
LM's avatar
LM committed
224 225 226
DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
227
  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
LM's avatar
LM committed
228 229
}

230 231 232
/** \deprecated because of accuracy loss. In Eigen 3.3, it is an alias for LinSpaced(const Scalar&,const Scalar&)
  *
  * \sa LinSpaced(Scalar,Scalar)
LM's avatar
LM committed
233 234
  */
template<typename Derived>
235
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
LM's avatar
LM committed
236 237 238 239
DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
240
  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
LM's avatar
LM committed
241 242 243
}

/**
244
  * \brief Sets a linearly spaced vector.
LM's avatar
LM committed
245 246
  *
  * The function generates 'size' equally spaced values in the closed interval [low,high].
Don Gagne's avatar
Don Gagne committed
247
  * When size is set to 1, a vector of length 1 containing 'high' is returned.
LM's avatar
LM committed
248 249 250 251 252 253
  *
  * \only_for_vectors
  *
  * Example: \include DenseBase_LinSpaced.cpp
  * Output: \verbinclude DenseBase_LinSpaced.out
  *
254 255 256 257 258 259 260 261 262 263 264
  * For integer scalar types, an even spacing is possible if and only if the length of the range,
  * i.e., \c high-low is a scalar multiple of \c size-1, or if \c size is a scalar multiple of the
  * number of values \c high-low+1 (meaning each value can be repeated the same number of time).
  * If one of these two considions is not satisfied, then \c high is lowered to the largest value
  * satisfying one of this constraint.
  * Here are some examples:
  *
  * Example: \include DenseBase_LinSpacedInt.cpp
  * Output: \verbinclude DenseBase_LinSpacedInt.out
  *
  * \sa setLinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
LM's avatar
LM committed
265 266
  */
template<typename Derived>
267
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
LM's avatar
LM committed
268 269 270
DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
271
  return DenseBase<Derived>::NullaryExpr(size, internal::linspaced_op<Scalar,PacketScalar>(low,high,size));
LM's avatar
LM committed
272 273 274 275 276 277 278
}

/**
  * \copydoc DenseBase::LinSpaced(Index, const Scalar&, const Scalar&)
  * Special version for fixed size types which does not require the size parameter.
  */
template<typename Derived>
279
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
LM's avatar
LM committed
280 281 282 283
DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
284
  return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, internal::linspaced_op<Scalar,PacketScalar>(low,high,Derived::SizeAtCompileTime));
LM's avatar
LM committed
285 286
}

Don Gagne's avatar
Don Gagne committed
287
/** \returns true if all coefficients in this matrix are approximately equal to \a val, to within precision \a prec */
LM's avatar
LM committed
288
template<typename Derived>
289
EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isApproxToConstant
Don Gagne's avatar
Don Gagne committed
290
(const Scalar& val, const RealScalar& prec) const
LM's avatar
LM committed
291
{
292
  typename internal::nested_eval<Derived,1>::type self(derived());
LM's avatar
LM committed
293 294
  for(Index j = 0; j < cols(); ++j)
    for(Index i = 0; i < rows(); ++i)
295
      if(!internal::isApprox(self.coeff(i, j), val, prec))
LM's avatar
LM committed
296 297 298 299 300 301 302 303
        return false;
  return true;
}

/** This is just an alias for isApproxToConstant().
  *
  * \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
template<typename Derived>
304
EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isConstant
Don Gagne's avatar
Don Gagne committed
305
(const Scalar& val, const RealScalar& prec) const
LM's avatar
LM committed
306
{
Don Gagne's avatar
Don Gagne committed
307
  return isApproxToConstant(val, prec);
LM's avatar
LM committed
308 309
}

Don Gagne's avatar
Don Gagne committed
310
/** Alias for setConstant(): sets all coefficients in this expression to \a val.
LM's avatar
LM committed
311 312 313 314
  *
  * \sa setConstant(), Constant(), class CwiseNullaryOp
  */
template<typename Derived>
315
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void DenseBase<Derived>::fill(const Scalar& val)
LM's avatar
LM committed
316
{
Don Gagne's avatar
Don Gagne committed
317
  setConstant(val);
LM's avatar
LM committed
318 319
}

320
/** Sets all coefficients in this expression to value \a val.
LM's avatar
LM committed
321 322 323 324
  *
  * \sa fill(), setConstant(Index,const Scalar&), setConstant(Index,Index,const Scalar&), setZero(), setOnes(), Constant(), class CwiseNullaryOp, setZero(), setOnes()
  */
template<typename Derived>
325
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setConstant(const Scalar& val)
LM's avatar
LM committed
326
{
Don Gagne's avatar
Don Gagne committed
327
  return derived() = Constant(rows(), cols(), val);
LM's avatar
LM committed
328 329
}

330
/** Resizes to the given \a size, and sets all coefficients in this expression to the given value \a val.
LM's avatar
LM committed
331 332 333 334 335 336 337 338 339
  *
  * \only_for_vectors
  *
  * Example: \include Matrix_setConstant_int.cpp
  * Output: \verbinclude Matrix_setConstant_int.out
  *
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
  */
template<typename Derived>
340
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
Don Gagne's avatar
Don Gagne committed
341
PlainObjectBase<Derived>::setConstant(Index size, const Scalar& val)
LM's avatar
LM committed
342 343
{
  resize(size);
Don Gagne's avatar
Don Gagne committed
344
  return setConstant(val);
LM's avatar
LM committed
345 346
}

347
/** Resizes to the given size, and sets all coefficients in this expression to the given value \a val.
LM's avatar
LM committed
348
  *
349 350
  * \param rows the new number of rows
  * \param cols the new number of columns
Don Gagne's avatar
Don Gagne committed
351
  * \param val the value to which all coefficients are set
LM's avatar
LM committed
352 353 354 355 356 357 358
  *
  * Example: \include Matrix_setConstant_int_int.cpp
  * Output: \verbinclude Matrix_setConstant_int_int.out
  *
  * \sa MatrixBase::setConstant(const Scalar&), setConstant(Index,const Scalar&), class CwiseNullaryOp, MatrixBase::Constant(const Scalar&)
  */
template<typename Derived>
359 360
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
PlainObjectBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val)
LM's avatar
LM committed
361
{
362
  resize(rows, cols);
Don Gagne's avatar
Don Gagne committed
363
  return setConstant(val);
LM's avatar
LM committed
364 365 366
}

/**
367
  * \brief Sets a linearly spaced vector.
LM's avatar
LM committed
368 369
  *
  * The function generates 'size' equally spaced values in the closed interval [low,high].
Don Gagne's avatar
Don Gagne committed
370
  * When size is set to 1, a vector of length 1 containing 'high' is returned.
LM's avatar
LM committed
371 372 373 374 375 376
  *
  * \only_for_vectors
  *
  * Example: \include DenseBase_setLinSpaced.cpp
  * Output: \verbinclude DenseBase_setLinSpaced.out
  *
377 378 379 380
  * For integer scalar types, do not miss the explanations on the definition
  * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
  *
  * \sa LinSpaced(Index,const Scalar&,const Scalar&), CwiseNullaryOp
LM's avatar
LM committed
381 382
  */
template<typename Derived>
383
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index newSize, const Scalar& low, const Scalar& high)
Don Gagne's avatar
Don Gagne committed
384 385
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
386
  return derived() = Derived::NullaryExpr(newSize, internal::linspaced_op<Scalar,PacketScalar>(low,high,newSize));
Don Gagne's avatar
Don Gagne committed
387 388 389
}

/**
390
  * \brief Sets a linearly spaced vector.
Don Gagne's avatar
Don Gagne committed
391
  *
392
  * The function fills \c *this with equally spaced values in the closed interval [low,high].
Don Gagne's avatar
Don Gagne committed
393 394 395 396
  * When size is set to 1, a vector of length 1 containing 'high' is returned.
  *
  * \only_for_vectors
  *
397 398 399 400
  * For integer scalar types, do not miss the explanations on the definition
  * of \link LinSpaced(Index,const Scalar&,const Scalar&) even spacing \endlink.
  *
  * \sa LinSpaced(Index,const Scalar&,const Scalar&), setLinSpaced(Index, const Scalar&, const Scalar&), CwiseNullaryOp
Don Gagne's avatar
Don Gagne committed
401 402
  */
template<typename Derived>
403
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high)
LM's avatar
LM committed
404 405
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
Don Gagne's avatar
Don Gagne committed
406
  return setLinSpaced(size(), low, high);
LM's avatar
LM committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
}

// zero:

/** \returns an expression of a zero matrix.
  *
  * The parameters \a rows and \a cols are the number of rows and of columns of
  * the returned matrix. Must be compatible with this MatrixBase type.
  *
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  * it is redundant to pass \a rows and \a cols as arguments, so Zero() should be used
  * instead.
  *
  * Example: \include MatrixBase_zero_int_int.cpp
  * Output: \verbinclude MatrixBase_zero_int_int.out
  *
  * \sa Zero(), Zero(Index)
  */
template<typename Derived>
426 427
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
DenseBase<Derived>::Zero(Index rows, Index cols)
LM's avatar
LM committed
428
{
429
  return Constant(rows, cols, Scalar(0));
LM's avatar
LM committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
}

/** \returns an expression of a zero vector.
  *
  * The parameter \a size is the size of the returned vector.
  * Must be compatible with this MatrixBase type.
  *
  * \only_for_vectors
  *
  * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  * it is redundant to pass \a size as argument, so Zero() should be used
  * instead.
  *
  * Example: \include MatrixBase_zero_int.cpp
  * Output: \verbinclude MatrixBase_zero_int.out
  *
  * \sa Zero(), Zero(Index,Index)
  */
template<typename Derived>
449
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
LM's avatar
LM committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
DenseBase<Derived>::Zero(Index size)
{
  return Constant(size, Scalar(0));
}

/** \returns an expression of a fixed-size zero matrix or vector.
  *
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  * need to use the variants taking size arguments.
  *
  * Example: \include MatrixBase_zero.cpp
  * Output: \verbinclude MatrixBase_zero.out
  *
  * \sa Zero(Index), Zero(Index,Index)
  */
template<typename Derived>
466
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
LM's avatar
LM committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480
DenseBase<Derived>::Zero()
{
  return Constant(Scalar(0));
}

/** \returns true if *this is approximately equal to the zero matrix,
  *          within the precision given by \a prec.
  *
  * Example: \include MatrixBase_isZero.cpp
  * Output: \verbinclude MatrixBase_isZero.out
  *
  * \sa class CwiseNullaryOp, Zero()
  */
template<typename Derived>
481
EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isZero(const RealScalar& prec) const
LM's avatar
LM committed
482
{
483
  typename internal::nested_eval<Derived,1>::type self(derived());
LM's avatar
LM committed
484 485
  for(Index j = 0; j < cols(); ++j)
    for(Index i = 0; i < rows(); ++i)
486
      if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<Scalar>(1), prec))
LM's avatar
LM committed
487 488 489 490 491 492 493 494 495 496 497 498
        return false;
  return true;
}

/** Sets all coefficients in this expression to zero.
  *
  * Example: \include MatrixBase_setZero.cpp
  * Output: \verbinclude MatrixBase_setZero.out
  *
  * \sa class CwiseNullaryOp, Zero()
  */
template<typename Derived>
499
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setZero()
LM's avatar
LM committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513
{
  return setConstant(Scalar(0));
}

/** Resizes to the given \a size, and sets all coefficients in this expression to zero.
  *
  * \only_for_vectors
  *
  * Example: \include Matrix_setZero_int.cpp
  * Output: \verbinclude Matrix_setZero_int.out
  *
  * \sa DenseBase::setZero(), setZero(Index,Index), class CwiseNullaryOp, DenseBase::Zero()
  */
template<typename Derived>
514
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
Don Gagne's avatar
Don Gagne committed
515
PlainObjectBase<Derived>::setZero(Index newSize)
LM's avatar
LM committed
516
{
Don Gagne's avatar
Don Gagne committed
517
  resize(newSize);
LM's avatar
LM committed
518 519 520 521 522
  return setConstant(Scalar(0));
}

/** Resizes to the given size, and sets all coefficients in this expression to zero.
  *
523 524
  * \param rows the new number of rows
  * \param cols the new number of columns
LM's avatar
LM committed
525 526 527 528 529 530 531
  *
  * Example: \include Matrix_setZero_int_int.cpp
  * Output: \verbinclude Matrix_setZero_int_int.out
  *
  * \sa DenseBase::setZero(), setZero(Index), class CwiseNullaryOp, DenseBase::Zero()
  */
template<typename Derived>
532 533
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
PlainObjectBase<Derived>::setZero(Index rows, Index cols)
LM's avatar
LM committed
534
{
535
  resize(rows, cols);
LM's avatar
LM committed
536 537 538 539 540 541 542
  return setConstant(Scalar(0));
}

// ones:

/** \returns an expression of a matrix where all coefficients equal one.
  *
543
  * The parameters \a rows and \a cols are the number of rows and of columns of
LM's avatar
LM committed
544 545 546 547 548 549 550 551 552 553 554 555
  * the returned matrix. Must be compatible with this MatrixBase type.
  *
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  * it is redundant to pass \a rows and \a cols as arguments, so Ones() should be used
  * instead.
  *
  * Example: \include MatrixBase_ones_int_int.cpp
  * Output: \verbinclude MatrixBase_ones_int_int.out
  *
  * \sa Ones(), Ones(Index), isOnes(), class Ones
  */
template<typename Derived>
556 557
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
DenseBase<Derived>::Ones(Index rows, Index cols)
LM's avatar
LM committed
558
{
559
  return Constant(rows, cols, Scalar(1));
LM's avatar
LM committed
560 561 562 563
}

/** \returns an expression of a vector where all coefficients equal one.
  *
Don Gagne's avatar
Don Gagne committed
564
  * The parameter \a newSize is the size of the returned vector.
LM's avatar
LM committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578
  * Must be compatible with this MatrixBase type.
  *
  * \only_for_vectors
  *
  * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
  * it is redundant to pass \a size as argument, so Ones() should be used
  * instead.
  *
  * Example: \include MatrixBase_ones_int.cpp
  * Output: \verbinclude MatrixBase_ones_int.out
  *
  * \sa Ones(), Ones(Index,Index), isOnes(), class Ones
  */
template<typename Derived>
579
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
Don Gagne's avatar
Don Gagne committed
580
DenseBase<Derived>::Ones(Index newSize)
LM's avatar
LM committed
581
{
Don Gagne's avatar
Don Gagne committed
582
  return Constant(newSize, Scalar(1));
LM's avatar
LM committed
583 584 585 586 587 588 589 590 591 592 593 594 595
}

/** \returns an expression of a fixed-size matrix or vector where all coefficients equal one.
  *
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  * need to use the variants taking size arguments.
  *
  * Example: \include MatrixBase_ones.cpp
  * Output: \verbinclude MatrixBase_ones.out
  *
  * \sa Ones(Index), Ones(Index,Index), isOnes(), class Ones
  */
template<typename Derived>
596
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename DenseBase<Derived>::ConstantReturnType
LM's avatar
LM committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610
DenseBase<Derived>::Ones()
{
  return Constant(Scalar(1));
}

/** \returns true if *this is approximately equal to the matrix where all coefficients
  *          are equal to 1, within the precision given by \a prec.
  *
  * Example: \include MatrixBase_isOnes.cpp
  * Output: \verbinclude MatrixBase_isOnes.out
  *
  * \sa class CwiseNullaryOp, Ones()
  */
template<typename Derived>
611
EIGEN_DEVICE_FUNC bool DenseBase<Derived>::isOnes
Don Gagne's avatar
Don Gagne committed
612
(const RealScalar& prec) const
LM's avatar
LM committed
613 614 615 616 617 618 619 620 621 622 623 624
{
  return isApproxToConstant(Scalar(1), prec);
}

/** Sets all coefficients in this expression to one.
  *
  * Example: \include MatrixBase_setOnes.cpp
  * Output: \verbinclude MatrixBase_setOnes.out
  *
  * \sa class CwiseNullaryOp, Ones()
  */
template<typename Derived>
625
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setOnes()
LM's avatar
LM committed
626 627 628 629
{
  return setConstant(Scalar(1));
}

Don Gagne's avatar
Don Gagne committed
630
/** Resizes to the given \a newSize, and sets all coefficients in this expression to one.
LM's avatar
LM committed
631 632 633 634 635 636 637 638 639
  *
  * \only_for_vectors
  *
  * Example: \include Matrix_setOnes_int.cpp
  * Output: \verbinclude Matrix_setOnes_int.out
  *
  * \sa MatrixBase::setOnes(), setOnes(Index,Index), class CwiseNullaryOp, MatrixBase::Ones()
  */
template<typename Derived>
640
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
Don Gagne's avatar
Don Gagne committed
641
PlainObjectBase<Derived>::setOnes(Index newSize)
LM's avatar
LM committed
642
{
Don Gagne's avatar
Don Gagne committed
643
  resize(newSize);
LM's avatar
LM committed
644 645 646 647 648
  return setConstant(Scalar(1));
}

/** Resizes to the given size, and sets all coefficients in this expression to one.
  *
649 650
  * \param rows the new number of rows
  * \param cols the new number of columns
LM's avatar
LM committed
651 652 653 654 655 656 657
  *
  * Example: \include Matrix_setOnes_int_int.cpp
  * Output: \verbinclude Matrix_setOnes_int_int.out
  *
  * \sa MatrixBase::setOnes(), setOnes(Index), class CwiseNullaryOp, MatrixBase::Ones()
  */
template<typename Derived>
658 659
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived&
PlainObjectBase<Derived>::setOnes(Index rows, Index cols)
LM's avatar
LM committed
660
{
661
  resize(rows, cols);
LM's avatar
LM committed
662 663 664 665 666 667 668
  return setConstant(Scalar(1));
}

// Identity:

/** \returns an expression of the identity matrix (not necessarily square).
  *
669
  * The parameters \a rows and \a cols are the number of rows and of columns of
LM's avatar
LM committed
670 671 672 673 674 675 676 677 678 679 680 681
  * the returned matrix. Must be compatible with this MatrixBase type.
  *
  * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
  * it is redundant to pass \a rows and \a cols as arguments, so Identity() should be used
  * instead.
  *
  * Example: \include MatrixBase_identity_int_int.cpp
  * Output: \verbinclude MatrixBase_identity_int_int.out
  *
  * \sa Identity(), setIdentity(), isIdentity()
  */
template<typename Derived>
682 683
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
MatrixBase<Derived>::Identity(Index rows, Index cols)
LM's avatar
LM committed
684
{
685
  return DenseBase<Derived>::NullaryExpr(rows, cols, internal::scalar_identity_op<Scalar>());
LM's avatar
LM committed
686 687 688 689 690 691 692 693 694 695 696 697 698
}

/** \returns an expression of the identity matrix (not necessarily square).
  *
  * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
  * need to use the variant taking size arguments.
  *
  * Example: \include MatrixBase_identity.cpp
  * Output: \verbinclude MatrixBase_identity.out
  *
  * \sa Identity(Index,Index), setIdentity(), isIdentity()
  */
template<typename Derived>
699
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::IdentityReturnType
LM's avatar
LM committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
MatrixBase<Derived>::Identity()
{
  EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
  return MatrixBase<Derived>::NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_identity_op<Scalar>());
}

/** \returns true if *this is approximately equal to the identity matrix
  *          (not necessarily square),
  *          within the precision given by \a prec.
  *
  * Example: \include MatrixBase_isIdentity.cpp
  * Output: \verbinclude MatrixBase_isIdentity.out
  *
  * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), setIdentity()
  */
template<typename Derived>
bool MatrixBase<Derived>::isIdentity
Don Gagne's avatar
Don Gagne committed
717
(const RealScalar& prec) const
LM's avatar
LM committed
718
{
719
  typename internal::nested_eval<Derived,1>::type self(derived());
LM's avatar
LM committed
720 721 722 723 724 725
  for(Index j = 0; j < cols(); ++j)
  {
    for(Index i = 0; i < rows(); ++i)
    {
      if(i == j)
      {
726
        if(!internal::isApprox(self.coeff(i, j), static_cast<Scalar>(1), prec))
LM's avatar
LM committed
727 728 729 730
          return false;
      }
      else
      {
731
        if(!internal::isMuchSmallerThan(self.coeff(i, j), static_cast<RealScalar>(1), prec))
LM's avatar
LM committed
732 733 734 735 736 737 738 739 740 741 742 743
          return false;
      }
    }
  }
  return true;
}

namespace internal {

template<typename Derived, bool Big = (Derived::SizeAtCompileTime>=16)>
struct setIdentity_impl
{
744
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
745 746 747 748 749 750 751 752 753
  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
  {
    return m = Derived::Identity(m.rows(), m.cols());
  }
};

template<typename Derived>
struct setIdentity_impl<Derived, true>
{
754
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
755 756 757
  static EIGEN_STRONG_INLINE Derived& run(Derived& m)
  {
    m.setZero();
758
    const Index size = numext::mini(m.rows(), m.cols());
LM's avatar
LM committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    for(Index i = 0; i < size; ++i) m.coeffRef(i,i) = typename Derived::Scalar(1);
    return m;
  }
};

} // end namespace internal

/** Writes the identity expression (not necessarily square) into *this.
  *
  * Example: \include MatrixBase_setIdentity.cpp
  * Output: \verbinclude MatrixBase_setIdentity.out
  *
  * \sa class CwiseNullaryOp, Identity(), Identity(Index,Index), isIdentity()
  */
template<typename Derived>
774
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity()
LM's avatar
LM committed
775 776 777 778 779 780
{
  return internal::setIdentity_impl<Derived>::run(derived());
}

/** \brief Resizes to the given size, and writes the identity expression (not necessarily square) into *this.
  *
781 782
  * \param rows the new number of rows
  * \param cols the new number of columns
LM's avatar
LM committed
783 784 785 786 787 788 789
  *
  * Example: \include Matrix_setIdentity_int_int.cpp
  * Output: \verbinclude Matrix_setIdentity_int_int.out
  *
  * \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Identity()
  */
template<typename Derived>
790
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setIdentity(Index rows, Index cols)
LM's avatar
LM committed
791
{
792
  derived().resize(rows, cols);
LM's avatar
LM committed
793 794 795 796 797 798 799 800 801 802
  return setIdentity();
}

/** \returns an expression of the i-th unit (basis) vector.
  *
  * \only_for_vectors
  *
  * \sa MatrixBase::Unit(Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
803
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index newSize, Index i)
LM's avatar
LM committed
804 805
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
Don Gagne's avatar
Don Gagne committed
806
  return BasisReturnType(SquareMatrixType::Identity(newSize,newSize), i);
LM's avatar
LM committed
807 808 809 810 811 812 813 814 815 816 817
}

/** \returns an expression of the i-th unit (basis) vector.
  *
  * \only_for_vectors
  *
  * This variant is for fixed-size vector only.
  *
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::UnitX(), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
818
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::Unit(Index i)
LM's avatar
LM committed
819 820 821 822 823 824 825 826 827 828 829 830
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return BasisReturnType(SquareMatrixType::Identity(),i);
}

/** \returns an expression of the X axis unit vector (1{,0}^*)
  *
  * \only_for_vectors
  *
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
831
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitX()
LM's avatar
LM committed
832 833 834 835 836 837 838 839 840
{ return Derived::Unit(0); }

/** \returns an expression of the Y axis unit vector (0,1{,0}^*)
  *
  * \only_for_vectors
  *
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
841
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitY()
LM's avatar
LM committed
842 843 844 845 846 847 848 849 850
{ return Derived::Unit(1); }

/** \returns an expression of the Z axis unit vector (0,0,1{,0}^*)
  *
  * \only_for_vectors
  *
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
851
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitZ()
LM's avatar
LM committed
852 853 854 855 856 857 858 859 860
{ return Derived::Unit(2); }

/** \returns an expression of the W axis unit vector (0,0,0,1)
  *
  * \only_for_vectors
  *
  * \sa MatrixBase::Unit(Index,Index), MatrixBase::Unit(Index), MatrixBase::UnitY(), MatrixBase::UnitZ(), MatrixBase::UnitW()
  */
template<typename Derived>
861
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
LM's avatar
LM committed
862 863
{ return Derived::Unit(3); }

Don Gagne's avatar
Don Gagne committed
864 865
} // end namespace Eigen

LM's avatar
LM committed
866
#endif // EIGEN_CWISE_NULLARY_OP_H