All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedMatrix.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_COMPRESSEDMATRIX_H_
23 #define _BLAZE_MATH_COMPRESSEDMATRIX_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
33 #include <blaze/system/Precision.h>
34 #include <blaze/util/Random.h>
35 
36 
37 namespace blaze {
38 
39 //=================================================================================================
40 //
41 // RAND SPECIALIZATION
42 //
43 //=================================================================================================
44 
45 //*************************************************************************************************
52 template< typename Type // Data type of the matrix
53  , bool SO > // Storage order
54 class Rand< CompressedMatrix<Type,SO> >
55 {
56  public:
57  //**Constructors********************************************************************************
60  explicit inline Rand( size_t m, size_t n );
61  explicit inline Rand( size_t m, size_t n, size_t nonzeros );
63  //**********************************************************************************************
64 
65  //**Conversion operators************************************************************************
68  inline operator CompressedMatrix<Type,SO>() const;
70  //**********************************************************************************************
71 
72  private:
73  //**Member variables****************************************************************************
76  CompressedMatrix<Type,SO> matrix_;
77 
78  //**********************************************************************************************
79 };
81 //*************************************************************************************************
82 
83 
84 //*************************************************************************************************
91 template< typename Type // Data type of the matrix
92  , bool SO > // Storage order
93 inline Rand< CompressedMatrix<Type,SO> >::Rand( size_t m, size_t n )
94  : matrix_( m, n ) // The random matrix
95 {
96  if( m == 0UL || n == 0UL ) return;
97 
98  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*m*n ) ) );
99 
100  matrix_.reserve( nonzeros );
101 
102  while( matrix_.nonZeros() < nonzeros ) {
103  matrix_( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
104  }
105 }
107 //*************************************************************************************************
108 
109 
110 //*************************************************************************************************
119 template< typename Type // Data type of the matrix
120  , bool SO > // Storage order
121 inline Rand< CompressedMatrix<Type,SO> >::Rand( size_t m, size_t n, size_t nonzeros )
122  : matrix_( m, n, nonzeros ) // The random matrix
123 {
124  if( nonzeros > m*n )
125  throw std::invalid_argument( "Invalid number of non-zero elements" );
126 
127  if( m == 0UL || n == 0UL ) return;
128 
129  while( matrix_.nonZeros() < nonzeros ) {
130  matrix_( rand<size_t>( 0UL, m-1UL ), rand<size_t>( 0UL, n-1UL ) ) = rand<Type>();
131  }
132 }
134 //*************************************************************************************************
135 
136 
137 //*************************************************************************************************
143 template< typename Type // Data type of the matrix
144  , bool SO > // Storage order
145 inline Rand< CompressedMatrix<Type,SO> >::operator CompressedMatrix<Type,SO>() const
146 {
147  return matrix_;
148 }
150 //*************************************************************************************************
151 
152 
153 
154 
155 //=================================================================================================
156 //
157 // TYPE DEFINITIONS
158 //
159 //=================================================================================================
160 
161 //*************************************************************************************************
166 //*************************************************************************************************
167 
168 
169 //*************************************************************************************************
174 //*************************************************************************************************
175 
176 
177 //*************************************************************************************************
182 //*************************************************************************************************
183 
184 } // namespace blaze
185 
186 #endif