All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseSubmatrix.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_SPARSESUBMATRIX_H_
36 #define _BLAZE_MATH_SPARSESUBMATRIX_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 MT // Type of the sparse matrix
68  , bool AF // Alignment flag
69  , bool SO > // Storage order
70 class Rand< SparseSubmatrix<MT,AF,SO> >
71 {
72  public:
73  //**Randomize functions*************************************************************************
76  inline void randomize( SparseSubmatrix<MT,AF,SO>& submatrix ) const;
77  inline void randomize( SparseSubmatrix<MT,AF,SO>& submatrix, size_t nonzeros ) const;
78 
79  template< typename Arg >
80  inline void randomize( SparseSubmatrix<MT,AF,SO>& submatrix, const Arg& min, const Arg& max ) const;
81 
82  template< typename Arg >
83  inline void randomize( SparseSubmatrix<MT,AF,SO>& submatrix, size_t nonzeros,
84  const Arg& min, const Arg& max ) const;
86  //**********************************************************************************************
87 };
89 //*************************************************************************************************
90 
91 
92 //*************************************************************************************************
99 template< typename MT // Type of the sparse matrix
100  , bool AF // Alignment flag
101  , bool SO > // Storage order
102 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix ) const
103 {
105 
106  const size_t m( submatrix.rows() );
107  const size_t n( submatrix.columns() );
108 
109  if( m == 0UL || n == 0UL ) return;
110 
111  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
112 
113  submatrix.reset();
114  submatrix.reserve( nonzeros );
115 
116  while( submatrix.nonZeros() < nonzeros ) {
117  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
118  }
119 }
121 //*************************************************************************************************
122 
123 
124 //*************************************************************************************************
133 template< typename MT // Type of the sparse matrix
134  , bool AF // Alignment flag
135  , bool SO > // Storage order
136 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix, size_t nonzeros ) const
137 {
139 
140  const size_t m( submatrix.rows() );
141  const size_t n( submatrix.columns() );
142 
143  if( nonzeros > m*n )
144  throw std::invalid_argument( "Invalid number of non-zero elements" );
145 
146  if( m == 0UL || n == 0UL ) return;
147 
148  submatrix.reset();
149  submatrix.reserve( nonzeros );
150 
151  while( submatrix.nonZeros() < nonzeros ) {
152  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
153  }
154 }
156 //*************************************************************************************************
157 
158 
159 //*************************************************************************************************
168 template< typename MT // Type of the sparse matrix
169  , bool AF // Alignment flag
170  , bool SO > // Storage order
171 template< typename Arg > // Min/max argument type
172 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix,
173  const Arg& min, const Arg& max ) const
174 {
176 
177  const size_t m( submatrix.rows() );
178  const size_t n( submatrix.columns() );
179 
180  if( m == 0UL || n == 0UL ) return;
181 
182  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
183 
184  submatrix.reset();
185  submatrix.reserve( nonzeros );
186 
187  while( submatrix.nonZeros() < nonzeros ) {
188  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
189  }
190 }
192 //*************************************************************************************************
193 
194 
195 //*************************************************************************************************
206 template< typename MT // Type of the sparse matrix
207  , bool AF // Alignment flag
208  , bool SO > // Storage order
209 template< typename Arg > // Min/max argument type
210 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix,
211  size_t nonzeros, const Arg& min, const Arg& max ) const
212 {
214 
215  const size_t m( submatrix.rows() );
216  const size_t n( submatrix.columns() );
217 
218  if( nonzeros > m*n )
219  throw std::invalid_argument( "Invalid number of non-zero elements" );
220 
221  if( m == 0UL || n == 0UL ) return;
222 
223  submatrix.reset();
224  submatrix.reserve( nonzeros );
225 
226  while( submatrix.nonZeros() < nonzeros ) {
227  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
228  }
229 }
231 //*************************************************************************************************
232 
233 } // namespace blaze
234 
235 #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
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
Header file for the DenseSubvector class template.
SubmatrixExprTrait< MT, unaligned >::Type submatrix(Matrix< MT, SO > &matrix, size_t row, size_t column, size_t m, size_t n)
Creating a view on a specific submatrix of the given matrix.
Definition: Submatrix.h:137
Header file for the SparseSubmatrix class template.