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 
43 #include <stdexcept>
45 #include <blaze/math/views/Row.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  throw std::invalid_argument( "Invalid number of non-zero elements" );
138 
139  if( size == 0UL ) return;
140 
141  column.reset();
142  column.reserve( nonzeros );
143 
144  while( column.nonZeros() < nonzeros ) {
145  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>();
146  }
147 }
149 //*************************************************************************************************
150 
151 
152 //*************************************************************************************************
161 template< typename MT // Type of the sparse matrix
162  , bool SO > // Storage order
163 template< typename Arg > // Min/max argument type
164 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column,
165  const Arg& min, const Arg& max ) const
166 {
168 
169  const size_t size( column.size() );
170 
171  if( size == 0UL ) return;
172 
173  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
174 
175  column.reset();
176  column.reserve( nonzeros );
177 
178  while( column.nonZeros() < nonzeros ) {
179  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
180  }
181 }
183 //*************************************************************************************************
184 
185 
186 //*************************************************************************************************
197 template< typename MT // Type of the sparse matrix
198  , bool SO > // Storage order
199 template< typename Arg > // Min/max argument type
200 inline void Rand< SparseColumn<MT,SO> >::randomize( SparseColumn<MT,SO>& column, size_t nonzeros,
201  const Arg& min, const Arg& max ) const
202 {
204 
205  const size_t size( column.size() );
206 
207  if( nonzeros > size )
208  throw std::invalid_argument( "Invalid number of non-zero elements" );
209 
210  if( size == 0UL ) return;
211 
212  column.reset();
213  column.reserve( nonzeros );
214 
215  while( column.nonZeros() < nonzeros ) {
216  column[ rand<size_t>( 0UL, size-1UL ) ] = rand<ElementType>( min, max );
217  }
218 }
220 //*************************************************************************************************
221 
222 } // namespace blaze
223 
224 #endif
const MT::ElementType max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1649
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector)
Returns the current size/dimension of the vector.
Definition: Vector.h:264
void randomize(T &value)
Randomization of a given variable.
Definition: Random.h:1043
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:103
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:262
const MT::ElementType min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1602
Header file for the SparseColumn class template.
Type ElementType
Type of the sparse matrix elements.
Definition: CompressedMatrix.h:2505
Header file for all restructuring column functions.
Header file for the SparseRow class template.
Header file for all restructuring row functions.