2007-10-12 05:15:25 +00:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra. Eigen itself is part of the KDE project.
|
2007-10-10 06:09:56 +00:00
|
|
|
//
|
2008-01-07 09:34:21 +00:00
|
|
|
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
|
2007-10-10 06:09:56 +00:00
|
|
|
//
|
2008-02-28 15:44:45 +00:00
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
2008-03-04 12:34:58 +00:00
|
|
|
// License as published by the Free Software Foundation; either
|
2008-02-28 15:44:45 +00:00
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
2008-03-04 12:34:58 +00:00
|
|
|
// published by the Free Software Foundation; either version 2 of
|
2008-02-28 15:44:45 +00:00
|
|
|
// the License, or (at your option) any later version.
|
2007-10-10 06:09:56 +00:00
|
|
|
//
|
2007-10-12 05:15:25 +00:00
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
2007-10-10 06:09:56 +00:00
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
2008-02-28 15:44:45 +00:00
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
|
// GNU General Public License for more details.
|
2007-10-10 06:09:56 +00:00
|
|
|
//
|
2008-03-04 12:34:58 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
2008-02-28 15:44:45 +00:00
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-10 06:09:56 +00:00
|
|
|
|
2007-11-26 08:47:07 +00:00
|
|
|
#ifndef EIGEN_RANDOM_H
|
|
|
|
|
#define EIGEN_RANDOM_H
|
2007-10-10 06:09:56 +00:00
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \class Random
|
|
|
|
|
*
|
|
|
|
|
* \brief Expression of a random matrix or vector.
|
|
|
|
|
*
|
2008-01-11 15:56:21 +00:00
|
|
|
* \sa MatrixBase::random(), MatrixBase::random(int), MatrixBase::random(int,int),
|
|
|
|
|
* MatrixBase::setRandom()
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename MatrixType>
|
|
|
|
|
struct Scalar<Random<MatrixType> >
|
|
|
|
|
{ typedef typename Scalar<MatrixType>::Type Type; };
|
|
|
|
|
|
2007-12-16 12:41:37 +00:00
|
|
|
template<typename MatrixType> class Random : NoOperatorEquals,
|
2008-03-10 17:23:11 +00:00
|
|
|
public MatrixBase<Random<MatrixType> >
|
2007-10-10 06:09:56 +00:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef typename MatrixType::Scalar Scalar;
|
2008-03-10 17:23:11 +00:00
|
|
|
friend class MatrixBase<Random>;
|
|
|
|
|
friend class MatrixBase<Random>::Traits;
|
|
|
|
|
typedef MatrixBase<Random> Base;
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2007-10-10 06:09:56 +00:00
|
|
|
private:
|
2008-01-10 20:45:35 +00:00
|
|
|
enum {
|
|
|
|
|
RowsAtCompileTime = MatrixType::Traits::RowsAtCompileTime,
|
2008-01-13 19:55:23 +00:00
|
|
|
ColsAtCompileTime = MatrixType::Traits::ColsAtCompileTime,
|
|
|
|
|
MaxRowsAtCompileTime = MatrixType::Traits::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = MatrixType::Traits::MaxColsAtCompileTime
|
2008-01-10 20:45:35 +00:00
|
|
|
};
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2008-03-04 17:08:23 +00:00
|
|
|
const Random& _asArg() const { return *this; }
|
2008-01-13 19:55:23 +00:00
|
|
|
int _rows() const { return m_rows.value(); }
|
|
|
|
|
int _cols() const { return m_cols.value(); }
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2007-12-17 07:25:11 +00:00
|
|
|
Scalar _coeff(int, int) const
|
2007-10-10 06:09:56 +00:00
|
|
|
{
|
2008-02-28 12:38:12 +00:00
|
|
|
return ei_random<Scalar>();
|
2007-10-10 06:09:56 +00:00
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2007-12-31 13:29:51 +00:00
|
|
|
public:
|
|
|
|
|
Random(int rows, int cols) : m_rows(rows), m_cols(cols)
|
|
|
|
|
{
|
|
|
|
|
assert(rows > 0
|
2008-01-06 13:17:07 +00:00
|
|
|
&& (RowsAtCompileTime == Dynamic || RowsAtCompileTime == rows)
|
2007-12-31 13:29:51 +00:00
|
|
|
&& cols > 0
|
2008-01-06 13:17:07 +00:00
|
|
|
&& (ColsAtCompileTime == Dynamic || ColsAtCompileTime == cols));
|
2007-12-31 13:29:51 +00:00
|
|
|
}
|
2008-03-04 12:34:58 +00:00
|
|
|
|
2007-10-10 06:09:56 +00:00
|
|
|
protected:
|
2008-01-13 19:55:23 +00:00
|
|
|
const IntAtRunTimeIfDynamic<RowsAtCompileTime> m_rows;
|
|
|
|
|
const IntAtRunTimeIfDynamic<ColsAtCompileTime> m_cols;
|
2007-10-10 06:09:56 +00:00
|
|
|
};
|
|
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \returns a random matrix (not an expression, the matrix is immediately evaluated).
|
|
|
|
|
*
|
|
|
|
|
* 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,
|
2008-02-28 12:38:12 +00:00
|
|
|
* it is redundant to pass \a rows and \a cols as arguments, so ei_random() should be used
|
2008-01-03 19:36:32 +00:00
|
|
|
* instead.
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_random_int_int.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_random_int_int.out
|
|
|
|
|
*
|
2008-02-28 12:38:12 +00:00
|
|
|
* \sa ei_random(), ei_random(int)
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-01-15 13:55:47 +00:00
|
|
|
const Eval<Random<Derived> >
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::random(int rows, int cols)
|
2007-10-10 06:09:56 +00:00
|
|
|
{
|
2007-10-11 20:14:01 +00:00
|
|
|
return Random<Derived>(rows, cols).eval();
|
2007-10-10 06:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \returns a random vector (not an expression, the vector is immediately evaluated).
|
|
|
|
|
*
|
|
|
|
|
* 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,
|
2008-02-28 12:38:12 +00:00
|
|
|
* it is redundant to pass \a size as argument, so ei_random() should be used
|
2008-01-03 19:36:32 +00:00
|
|
|
* instead.
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_random_int.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_random_int.out
|
|
|
|
|
*
|
2008-02-28 12:38:12 +00:00
|
|
|
* \sa ei_random(), ei_random(int,int)
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-01-15 13:55:47 +00:00
|
|
|
const Eval<Random<Derived> >
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::random(int size)
|
2007-12-15 18:16:30 +00:00
|
|
|
{
|
2008-01-06 13:17:07 +00:00
|
|
|
assert(Traits::IsVectorAtCompileTime);
|
|
|
|
|
if(Traits::RowsAtCompileTime == 1) return Random<Derived>(1, size).eval();
|
2007-12-15 18:16:30 +00:00
|
|
|
else return Random<Derived>(size, 1).eval();
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-03 19:36:32 +00:00
|
|
|
/** \returns a fixed-size random matrix or vector
|
|
|
|
|
* (not an expression, the matrix is immediately evaluated).
|
|
|
|
|
*
|
|
|
|
|
* 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_random.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_random.out
|
|
|
|
|
*
|
2008-02-28 12:38:12 +00:00
|
|
|
* \sa ei_random(int), ei_random(int,int)
|
2008-01-03 19:36:32 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
2008-01-15 13:55:47 +00:00
|
|
|
const Eval<Random<Derived> >
|
2008-03-10 17:23:11 +00:00
|
|
|
MatrixBase<Derived>::random()
|
2007-12-15 18:16:30 +00:00
|
|
|
{
|
2008-01-06 13:17:07 +00:00
|
|
|
return Random<Derived>(Traits::RowsAtCompileTime, Traits::ColsAtCompileTime).eval();
|
2007-12-15 18:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-11 15:08:04 +00:00
|
|
|
/** Sets all coefficients in this expression to random values.
|
|
|
|
|
*
|
|
|
|
|
* Example: \include MatrixBase_setRandom.cpp
|
|
|
|
|
* Output: \verbinclude MatrixBase_setRandom.out
|
|
|
|
|
*
|
2008-02-28 12:38:12 +00:00
|
|
|
* \sa class Random, ei_random()
|
2008-01-11 15:08:04 +00:00
|
|
|
*/
|
2008-03-10 17:23:11 +00:00
|
|
|
template<typename Derived>
|
|
|
|
|
Derived& MatrixBase<Derived>::setRandom()
|
2008-01-11 15:08:04 +00:00
|
|
|
{
|
|
|
|
|
return *this = Random<Derived>(rows(), cols());
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-26 08:47:07 +00:00
|
|
|
#endif // EIGEN_RANDOM_H
|