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