[136] | 1 | // This file is part of Eigen, a lightweight C++ template library
|
---|
| 2 | // for linear algebra.
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 2008 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_RANDOM_H
|
---|
| 11 | #define EIGEN_RANDOM_H
|
---|
| 12 |
|
---|
| 13 | namespace Eigen {
|
---|
| 14 |
|
---|
| 15 | namespace internal {
|
---|
| 16 |
|
---|
| 17 | template<typename Scalar> struct scalar_random_op {
|
---|
| 18 | EIGEN_EMPTY_STRUCT_CTOR(scalar_random_op)
|
---|
| 19 | template<typename Index>
|
---|
| 20 | inline const Scalar operator() (Index, Index = 0) const { return random<Scalar>(); }
|
---|
| 21 | };
|
---|
| 22 |
|
---|
| 23 | template<typename Scalar>
|
---|
| 24 | struct functor_traits<scalar_random_op<Scalar> >
|
---|
| 25 | { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false, IsRepeatable = false }; };
|
---|
| 26 |
|
---|
| 27 | } // end namespace internal
|
---|
| 28 |
|
---|
| 29 | /** \returns a random matrix expression
|
---|
| 30 | *
|
---|
| 31 | * The parameters \a rows and \a cols are the number of rows and of columns of
|
---|
| 32 | * the returned matrix. Must be compatible with this MatrixBase type.
|
---|
| 33 | *
|
---|
| 34 | * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
|
---|
| 35 | * it is redundant to pass \a rows and \a cols as arguments, so Random() should be used
|
---|
| 36 | * instead.
|
---|
| 37 | *
|
---|
| 38 | * Example: \include MatrixBase_random_int_int.cpp
|
---|
| 39 | * Output: \verbinclude MatrixBase_random_int_int.out
|
---|
| 40 | *
|
---|
| 41 | * This expression has the "evaluate before nesting" flag so that it will be evaluated into
|
---|
| 42 | * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
|
---|
| 43 | * behavior with expressions involving random matrices.
|
---|
| 44 | *
|
---|
| 45 | * \sa MatrixBase::setRandom(), MatrixBase::Random(Index), MatrixBase::Random()
|
---|
| 46 | */
|
---|
| 47 | template<typename Derived>
|
---|
| 48 | inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
|
---|
| 49 | DenseBase<Derived>::Random(Index rows, Index cols)
|
---|
| 50 | {
|
---|
| 51 | return NullaryExpr(rows, cols, internal::scalar_random_op<Scalar>());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /** \returns a random vector expression
|
---|
| 55 | *
|
---|
| 56 | * The parameter \a size is the size of the returned vector.
|
---|
| 57 | * Must be compatible with this MatrixBase type.
|
---|
| 58 | *
|
---|
| 59 | * \only_for_vectors
|
---|
| 60 | *
|
---|
| 61 | * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
|
---|
| 62 | * it is redundant to pass \a size as argument, so Random() should be used
|
---|
| 63 | * instead.
|
---|
| 64 | *
|
---|
| 65 | * Example: \include MatrixBase_random_int.cpp
|
---|
| 66 | * Output: \verbinclude MatrixBase_random_int.out
|
---|
| 67 | *
|
---|
| 68 | * This expression has the "evaluate before nesting" flag so that it will be evaluated into
|
---|
| 69 | * a temporary vector whenever it is nested in a larger expression. This prevents unexpected
|
---|
| 70 | * behavior with expressions involving random matrices.
|
---|
| 71 | *
|
---|
| 72 | * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random()
|
---|
| 73 | */
|
---|
| 74 | template<typename Derived>
|
---|
| 75 | inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
|
---|
| 76 | DenseBase<Derived>::Random(Index size)
|
---|
| 77 | {
|
---|
| 78 | return NullaryExpr(size, internal::scalar_random_op<Scalar>());
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** \returns a fixed-size random matrix or vector expression
|
---|
| 82 | *
|
---|
| 83 | * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
|
---|
| 84 | * need to use the variants taking size arguments.
|
---|
| 85 | *
|
---|
| 86 | * Example: \include MatrixBase_random.cpp
|
---|
| 87 | * Output: \verbinclude MatrixBase_random.out
|
---|
| 88 | *
|
---|
| 89 | * This expression has the "evaluate before nesting" flag so that it will be evaluated into
|
---|
| 90 | * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
|
---|
| 91 | * behavior with expressions involving random matrices.
|
---|
| 92 | *
|
---|
| 93 | * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random(Index)
|
---|
| 94 | */
|
---|
| 95 | template<typename Derived>
|
---|
| 96 | inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
|
---|
| 97 | DenseBase<Derived>::Random()
|
---|
| 98 | {
|
---|
| 99 | return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_random_op<Scalar>());
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Sets all coefficients in this expression to random values.
|
---|
| 103 | *
|
---|
| 104 | * Example: \include MatrixBase_setRandom.cpp
|
---|
| 105 | * Output: \verbinclude MatrixBase_setRandom.out
|
---|
| 106 | *
|
---|
| 107 | * \sa class CwiseNullaryOp, setRandom(Index), setRandom(Index,Index)
|
---|
| 108 | */
|
---|
| 109 | template<typename Derived>
|
---|
| 110 | inline Derived& DenseBase<Derived>::setRandom()
|
---|
| 111 | {
|
---|
| 112 | return *this = Random(rows(), cols());
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Resizes to the given \a newSize, and sets all coefficients in this expression to random values.
|
---|
| 116 | *
|
---|
| 117 | * \only_for_vectors
|
---|
| 118 | *
|
---|
| 119 | * Example: \include Matrix_setRandom_int.cpp
|
---|
| 120 | * Output: \verbinclude Matrix_setRandom_int.out
|
---|
| 121 | *
|
---|
| 122 | * \sa MatrixBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, MatrixBase::Random()
|
---|
| 123 | */
|
---|
| 124 | template<typename Derived>
|
---|
| 125 | EIGEN_STRONG_INLINE Derived&
|
---|
| 126 | PlainObjectBase<Derived>::setRandom(Index newSize)
|
---|
| 127 | {
|
---|
| 128 | resize(newSize);
|
---|
| 129 | return setRandom();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** Resizes to the given size, and sets all coefficients in this expression to random values.
|
---|
| 133 | *
|
---|
| 134 | * \param nbRows the new number of rows
|
---|
| 135 | * \param nbCols the new number of columns
|
---|
| 136 | *
|
---|
| 137 | * Example: \include Matrix_setRandom_int_int.cpp
|
---|
| 138 | * Output: \verbinclude Matrix_setRandom_int_int.out
|
---|
| 139 | *
|
---|
| 140 | * \sa MatrixBase::setRandom(), setRandom(Index), class CwiseNullaryOp, MatrixBase::Random()
|
---|
| 141 | */
|
---|
| 142 | template<typename Derived>
|
---|
| 143 | EIGEN_STRONG_INLINE Derived&
|
---|
| 144 | PlainObjectBase<Derived>::setRandom(Index nbRows, Index nbCols)
|
---|
| 145 | {
|
---|
| 146 | resize(nbRows, nbCols);
|
---|
| 147 | return setRandom();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | } // end namespace Eigen
|
---|
| 151 |
|
---|
| 152 | #endif // EIGEN_RANDOM_H
|
---|