SparseColumn.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSECOLUMN_H_
36 #define _BLAZE_MATH_SPARSECOLUMN_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
44 #include <blaze/math/views/Row.h>
47 #include <blaze/util/Exception.h>
48 #include <blaze/util/Random.h>
49 
50 
51 namespace blaze {
52 
53 //=================================================================================================
54 //
55 // RAND SPECIALIZATION
56 //
57 //=================================================================================================
58 
59 //*************************************************************************************************
66 template< typename MT // Type of the sparse matrix
67  , bool SO > // Storage order
68 class Rand< SparseColumn<MT,SO> >
69 {
70  public:
71  //**Randomize functions*************************************************************************
74  inline void randomize( SparseColumn<MT,SO>& column ) const;
75  inline void randomize( SparseColumn<MT,SO>& column, size_t nonzeros ) const;
76 
77  template< typename Arg >
78  inline void randomize( SparseColumn<MT,SO>& column, const Arg& min, const Arg& max ) const;
79 
80  template< typename Arg >
81  inline void randomize( SparseColumn<MT,SO>& column, size_t nonzeros, const Arg& min, const Arg& max ) const;
83  //**********************************************************************************************
84 };
86 //*************************************************************************************************
87 
88 
89 //*************************************************************************************************
96 template< typename MT // Type of the sparse matrix
97  , bool SO > // Storage order
98 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column ) const
99 {
101 
102  const size_t size( column.size() );
103 
104  if( size == 0UL ) return;
105 
106  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
107 
108  column.reset();
109  column.reserve( nonzeros );
110 
111  while( column.nonZeros() < nonzeros ) {
112  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
113  }
114 }
116 //*************************************************************************************************
117 
118 
119 //*************************************************************************************************
128 template< typename MT // Type of the sparse matrix
129  , bool SO > // Storage order
130 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column, size_t nonzeros ) const
131 {
133 
134  const size_t size( column.size() );
135 
136  if( nonzeros > size ) {
137  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
138  }
139 
140  if( size == 0UL ) return;
141 
142  column.reset();
143  column.reserve( nonzeros );
144 
145  while( column.nonZeros() < nonzeros ) {
146  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
147  }
148 }
150 //*************************************************************************************************
151 
152 
153 //*************************************************************************************************
162 template< typename MT // Type of the sparse matrix
163  , bool SO > // Storage order
164 template< typename Arg > // Min/max argument type
165 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column,
166  const Arg& min, const Arg& max ) const
167 {
169 
170  const size_t size( column.size() );
171 
172  if( size == 0UL ) return;
173 
174  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
175 
176  column.reset();
177  column.reserve( nonzeros );
178 
179  while( column.nonZeros() < nonzeros ) {
180  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
181  }
182 }
184 //*************************************************************************************************
185 
186 
187 //*************************************************************************************************
198 template< typename MT // Type of the sparse matrix
199  , bool SO > // Storage order
200 template< typename Arg > // Min/max argument type
201 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column, size_t nonzeros,
202  const Arg& min, const Arg& max ) const
203 {
205 
206  const size_t size( column.size() );
207 
208  if( nonzeros > size ) {
209  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
210  }
211 
212  if( size == 0UL ) return;
213 
214  column.reset();
215  column.reserve( nonzeros );
216 
217  while( column.nonZeros() < nonzeros ) {
218  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
219  }
220 }
222 //*************************************************************************************************
223 
224 } // namespace blaze
225 
226 #endif
#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
DisableIf< Or< IsComputation< MT >, IsTransExpr< MT > >, typename ColumnExprTrait< MT >::Type >::Type column(Matrix< MT, SO > &matrix, size_t index)
Creating a view on a specific column of the given matrix.
Definition: Column.h:107
Implementation of a random number generator.
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 SparseColumn class template.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2586
Header file for all restructuring column functions.
Header file for the SparseRow class template.
Header file for exception macros.
Header file for all restructuring row functions.