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 
48 #include <blaze/util/Exception.h>
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  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
142  }
143 
144  if( size == 0UL ) return;
145 
146  subvector.reset();
147  subvector.reserve( nonzeros );
148 
149  while( subvector.nonZeros() < nonzeros ) {
150  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
151  }
152 }
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
166 template< typename VT // Type of the sparse vector
167  , bool AF // Alignment flag
168  , bool TF > // Transpose flag
169 template< typename Arg > // Min/max argument type
170 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector,
171  const Arg& min, const Arg& max ) const
172 {
174 
175  const size_t size( subvector.size() );
176 
177  if( size == 0UL ) return;
178 
179  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
180 
181  subvector.reset();
182  subvector.reserve( nonzeros );
183 
184  while( subvector.nonZeros() < nonzeros ) {
185  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
186  }
187 }
189 //*************************************************************************************************
190 
191 
192 //*************************************************************************************************
203 template< typename VT // Type of the sparse vector
204  , bool AF // Alignment flag
205  , bool TF > // Transpose flag
206 template< typename Arg > // Min/max argument type
207 inline void Rand< SparseSubvector<VT,AF,TF> >::randomize( SparseSubvector<VT,AF,TF>& subvector,
208  size_t nonzeros, const Arg& min, const Arg& max ) const
209 {
211 
212  const size_t size( subvector.size() );
213 
214  if( nonzeros > size ) {
215  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
216  }
217 
218  if( size == 0UL ) return;
219 
220  subvector.reset();
221  subvector.reserve( nonzeros );
222 
223  while( subvector.nonZeros() < nonzeros ) {
224  subvector[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
225  }
226 }
228 //*************************************************************************************************
229 
230 } // namespace blaze
231 
232 #endif
Header file for all restructuring submatrix functions.
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exceptionThis macro encapsulates the default way of...
Definition: Exception.h:187
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1729
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:252
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1041
Implementation of a random number generator.
Header file for all restructuring subvector functions.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
void randomize(T &value) const
Randomization of the given variable with a new value in the range .
Definition: Random.h:260
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1682
Header file for the SparseSubvector class template.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
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:143
Header file for the DenseSubvector class template.
Header file for the SparseSubmatrix class template.
Header file for exception macros.