All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CompressedVector.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_MATH_COMPRESSEDVECTOR_H_
23 #define _BLAZE_MATH_COMPRESSEDVECTOR_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 vector
53  , bool TF > // Transpose flag
54 class Rand< CompressedVector<Type,TF> >
55 {
56  public:
57  //**Constructors********************************************************************************
60  explicit inline Rand( size_t size );
61  explicit inline Rand( size_t size, size_t nonzeros );
63  //**********************************************************************************************
64 
65  //**Conversion operators************************************************************************
68  inline operator CompressedVector<Type,TF>() const;
70  //**********************************************************************************************
71 
72  private:
73  //**Member variables****************************************************************************
76  CompressedVector<Type,TF> vector_;
77 
78  //**********************************************************************************************
79 };
81 //*************************************************************************************************
82 
83 
84 //*************************************************************************************************
90 template< typename Type // Data type of the vector
91  , bool TF > // Transpose flag
92 inline Rand< CompressedVector<Type,TF> >::Rand( size_t size )
93  : vector_( size ) // The random vector
94 {
95  if( size == 0UL ) return;
96 
97  const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) );
98 
99  vector_.reserve( nonzeros );
100 
101  while( vector_.nonZeros() < nonzeros ) {
102  vector_[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
103  }
104 }
106 //*************************************************************************************************
107 
108 
109 //*************************************************************************************************
117 template< typename Type // Data type of the vector
118  , bool TF > // Transpose flag
119 inline Rand< CompressedVector<Type,TF> >::Rand( size_t size, size_t nonzeros )
120  : vector_( size, nonzeros ) // The random vector
121 {
122  if( nonzeros > size )
123  throw std::invalid_argument( "Invalid number of non-zero elements" );
124 
125  if( size == 0UL ) return;
126 
127  while( vector_.nonZeros() < nonzeros ) {
128  vector_[ rand<size_t>( 0UL, size-1UL ) ] = rand<Type>();
129  }
130 }
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
141 template< typename Type // Data type of the vector
142  , bool TF > // Transpose flag
143 inline Rand< CompressedVector<Type,TF> >::operator CompressedVector<Type,TF>() const
144 {
145  return vector_;
146 }
148 //*************************************************************************************************
149 
150 
151 
152 
153 //=================================================================================================
154 //
155 // TYPE DEFINITIONS
156 //
157 //=================================================================================================
158 
159 //*************************************************************************************************
164 //*************************************************************************************************
165 
166 
167 //*************************************************************************************************
172 //*************************************************************************************************
173 
174 
175 //*************************************************************************************************
180 //*************************************************************************************************
181 
182 } // namespace blaze
183 
184 #endif