fn_sprandu.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup fn_sprandu
  16. //! @{
  17. //! Generate a sparse matrix with a randomly selected subset of the elements
  18. //! set to random values in the [0,1] interval (uniform distribution)
  19. template<typename obj_type>
  20. arma_warn_unused
  21. inline
  22. obj_type
  23. sprandu
  24. (
  25. const uword n_rows,
  26. const uword n_cols,
  27. const double density,
  28. const typename arma_SpMat_SpCol_SpRow_only<obj_type>::result* junk = 0
  29. )
  30. {
  31. arma_extra_debug_sigprint();
  32. arma_ignore(junk);
  33. if(is_SpCol<obj_type>::value)
  34. {
  35. arma_debug_check( (n_cols != 1), "sprandu(): incompatible size" );
  36. }
  37. else
  38. if(is_SpRow<obj_type>::value)
  39. {
  40. arma_debug_check( (n_rows != 1), "sprandu(): incompatible size" );
  41. }
  42. obj_type out;
  43. out.sprandu(n_rows, n_cols, density);
  44. return out;
  45. }
  46. template<typename obj_type>
  47. arma_warn_unused
  48. inline
  49. obj_type
  50. sprandu(const SizeMat& s, const double density, const typename arma_SpMat_SpCol_SpRow_only<obj_type>::result* junk = 0)
  51. {
  52. arma_extra_debug_sigprint();
  53. arma_ignore(junk);
  54. return sprandu<obj_type>(s.n_rows, s.n_cols, density);
  55. }
  56. arma_warn_unused
  57. inline
  58. sp_mat
  59. sprandu(const uword n_rows, const uword n_cols, const double density)
  60. {
  61. arma_extra_debug_sigprint();
  62. sp_mat out;
  63. out.sprandu(n_rows, n_cols, density);
  64. return out;
  65. }
  66. arma_warn_unused
  67. inline
  68. sp_mat
  69. sprandu(const SizeMat& s, const double density)
  70. {
  71. arma_extra_debug_sigprint();
  72. sp_mat out;
  73. out.sprandu(s.n_rows, s.n_cols, density);
  74. return out;
  75. }
  76. //! Generate a sparse matrix with the non-zero values in the same locations as in the given sparse matrix X,
  77. //! with the non-zero values set to random values in the [0,1] interval (uniform distribution)
  78. template<typename T1>
  79. arma_warn_unused
  80. inline
  81. SpMat<typename T1::elem_type>
  82. sprandu(const SpBase<typename T1::elem_type, T1>& X)
  83. {
  84. arma_extra_debug_sigprint();
  85. typedef typename T1::elem_type eT;
  86. SpMat<eT> out( X.get_ref() );
  87. arma_rng::randu<eT>::fill( access::rwp(out.values), out.n_nonzero );
  88. return out;
  89. }
  90. //! @}