All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseSubvector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSESUBVECTOR_H_
36 #define _BLAZE_MATH_SPARSESUBVECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <stdexcept>
49 #include <blaze/util/Random.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // RAND SPECIALIZATION
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
67 template< typename VT // Type of the sparse vector
68  , bool AF // Alignment flag
69  , bool TF > // Transpose flag
70 class Rand< SparseSubvector<VT,AF,TF> >
71 {
72  public:
73  //**Randomize functions*************************************************************************
76  inline void randomize( SparseSubvector<VT,AF,TF>& subvector ) const;
77  inline void randomize( SparseSubvector<VT,AF,TF>& subvector, size_t nonzeros ) const;
78 
79  template< typename Arg >
80  inline void randomize( SparseSubvector<VT,AF,TF>& subvector, const Arg& min, const Arg& max ) const;
81 
82  template< typename Arg >
83  inline void randomize( SparseSubvector<VT,AF,TF>& subvector, size_t nonzeros, const Arg& min, const Arg& max ) const;
85  //**********************************************************************************************
86 };
88 //*************************************************************************************************
89 
90 
91 //*************************************************************************************************
98 template< typename VT // Type of the sparse vector
99  , bool AF // Alignment flag
100  , bool TF > // Transpose flag
101 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector ) const
102 {
104 
105  const size_t size( subvector.size() );
106 
107  if( size == 0UL ) return;
108 
109  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
110 
111  subvector.reset();
112  subvector.reserve( nonzeros );
113 
114  while( subvector.nonZeros() < nonzeros ) {
115  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
116  }
117 }
119 //*************************************************************************************************
120 
121 
122 //*************************************************************************************************
131 template< typename VT // Type of the sparse vector
132  , bool AF // Alignment flag
133  , bool TF > // Transpose flag
134 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector, size_t nonzeros ) const
135 {
137 
138  const size_t size( subvector.size() );
139 
140  if( nonzeros > size )
141  throw std::invalid_argument( "Invalid number of non-zero elements" );
142 
143  if( size == 0UL ) return;
144 
145  subvector.reset();
146  subvector.reserve( nonzeros );
147 
148  while( subvector.nonZeros() < nonzeros ) {
149  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
150  }
151 }
153 //*************************************************************************************************
154 
155 
156 //*************************************************************************************************
165 template< typename VT // Type of the sparse vector
166  , bool AF // Alignment flag
167  , bool TF > // Transpose flag
168 template< typename Arg > // Min/max argument type
169 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector,
170  const Arg& min, const Arg& max ) const
171 {
173 
174  const size_t size( subvector.size() );
175 
176  if( size == 0UL ) return;
177 
178  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
179 
180  subvector.reset();
181  subvector.reserve( nonzeros );
182 
183  while( subvector.nonZeros() < nonzeros ) {
184  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
185  }
186 }
188 //*************************************************************************************************
189 
190 
191 //*************************************************************************************************
202 template< typename VT // Type of the sparse vector
203  , bool AF // Alignment flag
204  , bool TF > // Transpose flag
205 template< typename Arg > // Min/max argument type
206 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector,
207  size_t nonzeros, const Arg& min, const Arg& max ) const
208 {
210 
211  const size_t size( subvector.size() );
212 
213  if( nonzeros > size )
214  throw std::invalid_argument( "Invalid number of non-zero elements" );
215 
216  if( size == 0UL ) return;
217 
218  subvector.reset();
219  subvector.reserve( nonzeros );
220 
221  while( subvector.nonZeros() < nonzeros ) {
222  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
223  }
224 }
226 //*************************************************************************************************
227 
228 } // namespace blaze
229 
230 #endif
Header file for all restructuring submatrix functions.
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:994
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:258
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1043
Implementation of a random number generator.
Header file for all restructuring subvector functions.
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:262
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:947
Header file for the SparseSubvector class template.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2476
SubvectorExprTrait< VT, unaligned >::Type subvector(Vector< VT, TF > &vector, size_t index, size_t size)
Creating a view on a specific subvector of the given vector.
Definition: Subvector.h:135
Header file for the DenseSubvector class template.
Header file for the SparseSubmatrix class template.