Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Random.h Source File

Random.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_RANDOM_H
00011 #define EIGEN_RANDOM_H
00012 
00013 namespace Eigen { 
00014 
00015 namespace internal {
00016 
00017 template<typename Scalar> struct scalar_random_op {
00018   EIGEN_EMPTY_STRUCT_CTOR(scalar_random_op)
00019   template<typename Index>
00020   inline const Scalar operator() (Index, Index = 0) const { return random<Scalar>(); }
00021 };
00022 
00023 template<typename Scalar>
00024 struct functor_traits<scalar_random_op<Scalar> >
00025 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false, IsRepeatable = false }; };
00026 
00027 } // end namespace internal
00028 
00029 /** \returns a random matrix expression
00030   *
00031   * The parameters \a rows and \a cols are the number of rows and of columns of
00032   * the returned matrix. Must be compatible with this MatrixBase type.
00033   *
00034   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00035   * it is redundant to pass \a rows and \a cols as arguments, so Random() should be used
00036   * instead.
00037   *
00038   * Example: \include MatrixBase_random_int_int.cpp
00039   * Output: \verbinclude MatrixBase_random_int_int.out
00040   *
00041   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00042   * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
00043   * behavior with expressions involving random matrices.
00044   *
00045   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index), MatrixBase::Random()
00046   */
00047 template<typename Derived>
00048 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00049 DenseBase<Derived>::Random(Index rows, Index cols)
00050 {
00051   return NullaryExpr(rows, cols, internal::scalar_random_op<Scalar>());
00052 }
00053 
00054 /** \returns a random vector expression
00055   *
00056   * The parameter \a size is the size of the returned vector.
00057   * Must be compatible with this MatrixBase type.
00058   *
00059   * \only_for_vectors
00060   *
00061   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00062   * it is redundant to pass \a size as argument, so Random() should be used
00063   * instead.
00064   *
00065   * Example: \include MatrixBase_random_int.cpp
00066   * Output: \verbinclude MatrixBase_random_int.out
00067   *
00068   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00069   * a temporary vector whenever it is nested in a larger expression. This prevents unexpected
00070   * behavior with expressions involving random matrices.
00071   *
00072   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random()
00073   */
00074 template<typename Derived>
00075 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00076 DenseBase<Derived>::Random(Index size)
00077 {
00078   return NullaryExpr(size, internal::scalar_random_op<Scalar>());
00079 }
00080 
00081 /** \returns a fixed-size random matrix or vector expression
00082   *
00083   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
00084   * need to use the variants taking size arguments.
00085   *
00086   * Example: \include MatrixBase_random.cpp
00087   * Output: \verbinclude MatrixBase_random.out
00088   *
00089   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00090   * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
00091   * behavior with expressions involving random matrices.
00092   *
00093   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random(Index)
00094   */
00095 template<typename Derived>
00096 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00097 DenseBase<Derived>::Random()
00098 {
00099   return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_random_op<Scalar>());
00100 }
00101 
00102 /** Sets all coefficients in this expression to random values.
00103   *
00104   * Example: \include MatrixBase_setRandom.cpp
00105   * Output: \verbinclude MatrixBase_setRandom.out
00106   *
00107   * \sa class CwiseNullaryOp, setRandom(Index), setRandom(Index,Index)
00108   */
00109 template<typename Derived>
00110 inline Derived& DenseBase<Derived>::setRandom()
00111 {
00112   return *this = Random(rows(), cols());
00113 }
00114 
00115 /** Resizes to the given \a newSize, and sets all coefficients in this expression to random values.
00116   *
00117   * \only_for_vectors
00118   *
00119   * Example: \include Matrix_setRandom_int.cpp
00120   * Output: \verbinclude Matrix_setRandom_int.out
00121   *
00122   * \sa MatrixBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, MatrixBase::Random()
00123   */
00124 template<typename Derived>
00125 EIGEN_STRONG_INLINE Derived&
00126 PlainObjectBase<Derived>::setRandom(Index newSize)
00127 {
00128   resize(newSize);
00129   return setRandom();
00130 }
00131 
00132 /** Resizes to the given size, and sets all coefficients in this expression to random values.
00133   *
00134   * \param nbRows the new number of rows
00135   * \param nbCols the new number of columns
00136   *
00137   * Example: \include Matrix_setRandom_int_int.cpp
00138   * Output: \verbinclude Matrix_setRandom_int_int.out
00139   *
00140   * \sa MatrixBase::setRandom(), setRandom(Index), class CwiseNullaryOp, MatrixBase::Random()
00141   */
00142 template<typename Derived>
00143 EIGEN_STRONG_INLINE Derived&
00144 PlainObjectBase<Derived>::setRandom(Index nbRows, Index nbCols)
00145 {
00146   resize(nbRows, nbCols);
00147   return setRandom();
00148 }
00149 
00150 } // end namespace Eigen
00151 
00152 #endif // EIGEN_RANDOM_H