1
// This file is part of Eigen, a lightweight C++ template library
2
// for linear algebra. Eigen itself is part of the KDE project.
4
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
6
// Eigen is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 3 of the License, or (at your option) any later version.
11
// Alternatively, you can redistribute it and/or
12
// modify it under the terms of the GNU General Public License as
13
// published by the Free Software Foundation; either version 2 of
14
// the License, or (at your option) any later version.
16
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19
// GNU General Public License for more details.
21
// You should have received a copy of the GNU Lesser General Public
22
// License and a copy of the GNU General Public License along with
23
// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
#ifndef EIGEN_RANDOM_H
26
#define EIGEN_RANDOM_H
28
template<typename Scalar> struct ei_scalar_random_op EIGEN_EMPTY_STRUCT {
29
inline ei_scalar_random_op(void) {}
30
inline const Scalar operator() (int, int) const { return ei_random<Scalar>(); }
32
template<typename Scalar>
33
struct ei_functor_traits<ei_scalar_random_op<Scalar> >
34
{ enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false, IsRepeatable = false }; };
38
* \returns a random matrix (not an expression, the matrix is immediately evaluated).
40
* The parameters \a rows and \a cols are the number of rows and of columns of
41
* the returned matrix. Must be compatible with this MatrixBase type.
43
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
44
* it is redundant to pass \a rows and \a cols as arguments, so ei_random() should be used
47
* \addexample RandomExample \label How to create a matrix with random coefficients
49
* Example: \include MatrixBase_random_int_int.cpp
50
* Output: \verbinclude MatrixBase_random_int_int.out
52
* \sa MatrixBase::setRandom(), MatrixBase::Random(int), MatrixBase::Random()
54
template<typename Derived>
55
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
56
MatrixBase<Derived>::Random(int rows, int cols)
58
return NullaryExpr(rows, cols, ei_scalar_random_op<Scalar>());
63
* \returns a random vector (not an expression, the vector is immediately evaluated).
65
* The parameter \a size is the size of the returned vector.
66
* Must be compatible with this MatrixBase type.
70
* This variant is meant to be used for dynamic-size vector types. For fixed-size types,
71
* it is redundant to pass \a size as argument, so ei_random() should be used
74
* Example: \include MatrixBase_random_int.cpp
75
* Output: \verbinclude MatrixBase_random_int.out
77
* \sa MatrixBase::setRandom(), MatrixBase::Random(int,int), MatrixBase::Random()
79
template<typename Derived>
80
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
81
MatrixBase<Derived>::Random(int size)
83
return NullaryExpr(size, ei_scalar_random_op<Scalar>());
88
* \returns a fixed-size random matrix or vector
89
* (not an expression, the matrix is immediately evaluated).
91
* This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
92
* need to use the variants taking size arguments.
94
* Example: \include MatrixBase_random.cpp
95
* Output: \verbinclude MatrixBase_random.out
97
* \sa MatrixBase::setRandom(), MatrixBase::Random(int,int), MatrixBase::Random(int)
99
template<typename Derived>
100
inline const CwiseNullaryOp<ei_scalar_random_op<typename ei_traits<Derived>::Scalar>, Derived>
101
MatrixBase<Derived>::Random()
103
return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, ei_scalar_random_op<Scalar>());
108
* Sets all coefficients in this expression to random values.
110
* Example: \include MatrixBase_setRandom.cpp
111
* Output: \verbinclude MatrixBase_setRandom.out
113
* \sa class CwiseNullaryOp, setRandom(int), setRandom(int,int)
115
template<typename Derived>
116
inline Derived& MatrixBase<Derived>::setRandom()
118
return *this = Random(rows(), cols());
121
/** Resizes to the given \a size, and sets all coefficients in this expression to random values.
125
* Example: \include Matrix_setRandom_int.cpp
126
* Output: \verbinclude Matrix_setRandom_int.out
128
* \sa MatrixBase::setRandom(), setRandom(int,int), class CwiseNullaryOp, MatrixBase::Random()
130
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
131
EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
132
Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setRandom(int size)
138
/** Resizes to the given size, and sets all coefficients in this expression to random values.
140
* \param rows the new number of rows
141
* \param cols the new number of columns
143
* Example: \include Matrix_setRandom_int_int.cpp
144
* Output: \verbinclude Matrix_setRandom_int_int.out
146
* \sa MatrixBase::setRandom(), setRandom(int), class CwiseNullaryOp, MatrixBase::Random()
148
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
149
EIGEN_STRONG_INLINE Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>&
150
Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::setRandom(int rows, int cols)
156
#endif // EIGEN_RANDOM_H