Main MRPT website > C++ reference
MRPT logo

Random.h

Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_RANDOM_H
00026 #define EIGEN_RANDOM_H
00027 
00028 namespace internal {
00029 
00030 template<typename Scalar> struct scalar_random_op {
00031   EIGEN_EMPTY_STRUCT_CTOR(scalar_random_op)
00032   template<typename Index>
00033   inline const Scalar operator() (Index, Index = 0) const { return random<Scalar>(); }
00034 };
00035 
00036 template<typename Scalar>
00037 struct functor_traits<scalar_random_op<Scalar> >
00038 { enum { Cost = 5 * NumTraits<Scalar>::MulCost, PacketAccess = false, IsRepeatable = false }; };
00039 
00040 } // end namespace internal
00041 
00042 /** \returns a random matrix expression
00043   *
00044   * The parameters \a rows and \a cols are the number of rows and of columns of
00045   * the returned matrix. Must be compatible with this MatrixBase type.
00046   *
00047   * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
00048   * it is redundant to pass \a rows and \a cols as arguments, so Random() should be used
00049   * instead.
00050   *
00051   * Example: \include MatrixBase_random_int_int.cpp
00052   * Output: \verbinclude MatrixBase_random_int_int.out
00053   *
00054   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00055   * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
00056   * behavior with expressions involving random matrices.
00057   *
00058   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index), MatrixBase::Random()
00059   */
00060 template<typename Derived>
00061 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00062 DenseBase<Derived>::Random(Index rows, Index cols)
00063 {
00064   return NullaryExpr(rows, cols, internal::scalar_random_op<Scalar>());
00065 }
00066 
00067 /** \returns a random vector expression
00068   *
00069   * The parameter \a size is the size of the returned vector.
00070   * Must be compatible with this MatrixBase type.
00071   *
00072   * \only_for_vectors
00073   *
00074   * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
00075   * it is redundant to pass \a size as argument, so Random() should be used
00076   * instead.
00077   *
00078   * Example: \include MatrixBase_random_int.cpp
00079   * Output: \verbinclude MatrixBase_random_int.out
00080   *
00081   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00082   * a temporary vector whenever it is nested in a larger expression. This prevents unexpected
00083   * behavior with expressions involving random matrices.
00084   *
00085   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random()
00086   */
00087 template<typename Derived>
00088 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00089 DenseBase<Derived>::Random(Index size)
00090 {
00091   return NullaryExpr(size, internal::scalar_random_op<Scalar>());
00092 }
00093 
00094 /** \returns a fixed-size random matrix or vector expression
00095   *
00096   * This variant is only for fixed-size MatrixBase types. For dynamic-size types, you
00097   * need to use the variants taking size arguments.
00098   *
00099   * Example: \include MatrixBase_random.cpp
00100   * Output: \verbinclude MatrixBase_random.out
00101   *
00102   * This expression has the "evaluate before nesting" flag so that it will be evaluated into
00103   * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
00104   * behavior with expressions involving random matrices.
00105   *
00106   * \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random(Index)
00107   */
00108 template<typename Derived>
00109 inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
00110 DenseBase<Derived>::Random()
00111 {
00112   return NullaryExpr(RowsAtCompileTime, ColsAtCompileTime, internal::scalar_random_op<Scalar>());
00113 }
00114 
00115 /** Sets all coefficients in this expression to random values.
00116   *
00117   * Example: \include MatrixBase_setRandom.cpp
00118   * Output: \verbinclude MatrixBase_setRandom.out
00119   *
00120   * \sa class CwiseNullaryOp, setRandom(Index), setRandom(Index,Index)
00121   */
00122 template<typename Derived>
00123 inline Derived& DenseBase<Derived>::setRandom()
00124 {
00125   return *this = Random(rows(), cols());
00126 }
00127 
00128 /** Resizes to the given \a size, and sets all coefficients in this expression to random values.
00129   *
00130   * \only_for_vectors
00131   *
00132   * Example: \include Matrix_setRandom_int.cpp
00133   * Output: \verbinclude Matrix_setRandom_int.out
00134   *
00135   * \sa MatrixBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, MatrixBase::Random()
00136   */
00137 template<typename Derived>
00138 EIGEN_STRONG_INLINE Derived&
00139 PlainObjectBase<Derived>::setRandom(Index size)
00140 {
00141   resize(size);
00142   return setRandom();
00143 }
00144 
00145 /** Resizes to the given size, and sets all coefficients in this expression to random values.
00146   *
00147   * \param rows the new number of rows
00148   * \param cols the new number of columns
00149   *
00150   * Example: \include Matrix_setRandom_int_int.cpp
00151   * Output: \verbinclude Matrix_setRandom_int_int.out
00152   *
00153   * \sa MatrixBase::setRandom(), setRandom(Index), class CwiseNullaryOp, MatrixBase::Random()
00154   */
00155 template<typename Derived>
00156 EIGEN_STRONG_INLINE Derived&
00157 PlainObjectBase<Derived>::setRandom(Index rows, Index cols)
00158 {
00159   resize(rows, cols);
00160   return setRandom();
00161 }
00162 
00163 #endif // EIGEN_RANDOM_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011