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 
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 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  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
145  }
146 
147  if( m == 0UL || n == 0UL ) return;
148 
149  submatrix.reset();
150  submatrix.reserve( nonzeros );
151 
152  while( submatrix.nonZeros() < nonzeros ) {
153  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>();
154  }
155 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
169 template< typename MT // Type of the sparse matrix
170  , bool AF // Alignment flag
171  , bool SO > // Storage order
172 template< typename Arg > // Min/max argument type
173 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix,
174  const Arg& min, const Arg& max ) const
175 {
177 
178  const size_t m( submatrix.rows() );
179  const size_t n( submatrix.columns() );
180 
181  if( m == 0UL || n == 0UL ) return;
182 
183  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
184 
185  submatrix.reset();
186  submatrix.reserve( nonzeros );
187 
188  while( submatrix.nonZeros() < nonzeros ) {
189  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
190  }
191 }
193 //*************************************************************************************************
194 
195 
196 //*************************************************************************************************
207 template< typename MT // Type of the sparse matrix
208  , bool AF // Alignment flag
209  , bool SO > // Storage order
210 template< typename Arg > // Min/max argument type
211 inline void Rand< SparseSubmatrix<MT,AF,SO> >::randomize( SparseSubmatrix<MT,AF,SO>& submatrix,
212  size_t nonzeros, const Arg& min, const Arg& max ) const
213 {
215 
216  const size_t m( submatrix.rows() );
217  const size_t n( submatrix.columns() );
218 
219  if( nonzeros > m*n ) {
220  BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" );
221  }
222 
223  if( m == 0UL || n == 0UL ) return;
224 
225  submatrix.reset();
226  submatrix.reserve( nonzeros );
227 
228  while( submatrix.nonZeros() < nonzeros ) {
229  submatrix( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<ElementType>( min, max );
230  }
231 }
233 //*************************************************************************************************
234 
235 } // namespace blaze
236 
237 #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
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
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:146
Header file for the SparseSubmatrix class template.
Header file for exception macros.