1 //================================================================================================= 2 /*! 3 // \file blaze/math/CompressedVector.h 4 // \brief Header file for the complete CompressedVector implementation 5 // 6 // Copyright (C) 2012-2020 Klaus Iglberger - All Rights Reserved 7 // 8 // This file is part of the Blaze library. You can redistribute it and/or modify it under 9 // the terms of the New (Revised) BSD License. Redistribution and use in source and binary 10 // forms, with or without modification, are permitted provided that the following conditions 11 // are met: 12 // 13 // 1. Redistributions of source code must retain the above copyright notice, this list of 14 // conditions and the following disclaimer. 15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list 16 // of conditions and the following disclaimer in the documentation and/or other materials 17 // provided with the distribution. 18 // 3. Neither the names of the Blaze development group nor the names of its contributors 19 // may be used to endorse or promote products derived from this software without specific 20 // prior written permission. 21 // 22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 23 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 // SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 27 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 31 // DAMAGE. 32 */ 33 //================================================================================================= 34 35 #ifndef _BLAZE_MATH_COMPRESSEDVECTOR_H_ 36 #define _BLAZE_MATH_COMPRESSEDVECTOR_H_ 37 38 39 //************************************************************************************************* 40 // Includes 41 //************************************************************************************************* 42 43 #include <blaze/math/dense/StaticVector.h> 44 #include <blaze/math/CompressedMatrix.h> 45 #include <blaze/math/Exception.h> 46 #include <blaze/math/sparse/CompressedVector.h> 47 #include <blaze/math/SparseVector.h> 48 #include <blaze/math/ZeroVector.h> 49 #include <blaze/util/Indices.h> 50 #include <blaze/util/Random.h> 51 52 53 namespace blaze { 54 55 //================================================================================================= 56 // 57 // RAND SPECIALIZATION 58 // 59 //================================================================================================= 60 61 //************************************************************************************************* 62 /*! \cond BLAZE_INTERNAL */ 63 /*!\brief Specialization of the Rand class template for CompressedVector. 64 // \ingroup random 65 // 66 // This specialization of the Rand class creates random instances of CompressedVector. 67 */ 68 template< typename Type // Data type of the vector 69 , bool TF // Transpose flag 70 , typename Tag > // Type tag 71 class Rand< CompressedVector<Type,TF,Tag> > 72 { 73 public: 74 //********************************************************************************************** 75 /*!\brief Generation of a random CompressedVector. 76 // 77 // \param size The size of the random vector. 78 // \return The generated random vector. 79 */ generate(size_t size)80 inline const CompressedVector<Type,TF,Tag> generate( size_t size ) const 81 { 82 CompressedVector<Type,TF,Tag> vector( size ); 83 randomize( vector ); 84 85 return vector; 86 } 87 //********************************************************************************************** 88 89 //********************************************************************************************** 90 /*!\brief Generation of a random CompressedVector. 91 // 92 // \param size The size of the random vector. 93 // \param nonzeros The number of non-zero elements of the random vector. 94 // \return The generated random vector. 95 // \exception std::invalid_argument Invalid number of non-zero elements. 96 */ generate(size_t size,size_t nonzeros)97 inline const CompressedVector<Type,TF,Tag> generate( size_t size, size_t nonzeros ) const 98 { 99 if( nonzeros > size ) { 100 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" ); 101 } 102 103 CompressedVector<Type,TF,Tag> vector( size, nonzeros ); 104 randomize( vector, nonzeros ); 105 106 return vector; 107 } 108 //********************************************************************************************** 109 110 //********************************************************************************************** 111 /*!\brief Generation of a random CompressedVector. 112 // 113 // \param size The size of the random vector. 114 // \param min The smallest possible value for a vector element. 115 // \param max The largest possible value for a vector element. 116 // \return The generated random vector. 117 */ 118 template< typename Arg > // Min/max argument type 119 inline const CompressedVector<Type,TF,Tag> generate(size_t size,const Arg & min,const Arg & max)120 generate( size_t size, const Arg& min, const Arg& max ) const 121 { 122 CompressedVector<Type,TF,Tag> vector( size ); 123 randomize( vector, min, max ); 124 125 return vector; 126 } 127 //********************************************************************************************** 128 129 //********************************************************************************************** 130 /*!\brief Generation of a random CompressedVector. 131 // 132 // \param size The size of the random vector. 133 // \param nonzeros The number of non-zero elements of the random vector. 134 // \param min The smallest possible value for a vector element. 135 // \param max The largest possible value for a vector element. 136 // \return The generated random vector. 137 // \exception std::invalid_argument Invalid number of non-zero elements. 138 */ 139 template< typename Arg > // Min/max argument type 140 inline const CompressedVector<Type,TF,Tag> generate(size_t size,size_t nonzeros,const Arg & min,const Arg & max)141 generate( size_t size, size_t nonzeros, const Arg& min, const Arg& max ) const 142 { 143 if( nonzeros > size ) { 144 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" ); 145 } 146 147 CompressedVector<Type,TF,Tag> vector( size, nonzeros ); 148 randomize( vector, nonzeros, min, max ); 149 150 return vector; 151 } 152 //********************************************************************************************** 153 154 //********************************************************************************************** 155 /*!\brief Randomization of a CompressedVector. 156 // 157 // \param vector The vector to be randomized. 158 // \return void 159 */ randomize(CompressedVector<Type,TF,Tag> & vector)160 inline void randomize( CompressedVector<Type,TF,Tag>& vector ) const 161 { 162 const size_t size( vector.size() ); 163 164 if( size == 0UL ) return; 165 166 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) ); 167 168 randomize( vector, nonzeros ); 169 } 170 //********************************************************************************************** 171 172 //********************************************************************************************** 173 /*!\brief Randomization of a CompressedVector. 174 // 175 // \param vector The vector to be randomized. 176 // \param nonzeros The number of non-zero elements of the random vector. 177 // \return void 178 // \exception std::invalid_argument Invalid number of non-zero elements. 179 */ randomize(CompressedVector<Type,TF,Tag> & vector,size_t nonzeros)180 inline void randomize( CompressedVector<Type,TF,Tag>& vector, size_t nonzeros ) const 181 { 182 const size_t size( vector.size() ); 183 184 if( nonzeros > size ) { 185 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" ); 186 } 187 188 if( size == 0UL ) return; 189 190 vector.reset(); 191 vector.reserve( nonzeros ); 192 193 const Indices<size_t> indices( 0UL, vector.size()-1UL, nonzeros ); 194 195 for( size_t index : indices ) { 196 vector.append( index, rand<Type>() ); 197 } 198 } 199 //********************************************************************************************** 200 201 //********************************************************************************************** 202 /*!\brief Randomization of a CompressedVector. 203 // 204 // \param vector The vector to be randomized. 205 // \param min The smallest possible value for a vector element. 206 // \param max The largest possible value for a vector element. 207 // \return void 208 */ 209 template< typename Arg > // Min/max argument type randomize(CompressedVector<Type,TF,Tag> & vector,const Arg & min,const Arg & max)210 inline void randomize( CompressedVector<Type,TF,Tag>& vector, 211 const Arg& min, const Arg& max ) const 212 { 213 const size_t size( vector.size() ); 214 215 if( size == 0UL ) return; 216 217 const size_t nonzeros( rand<size_t>( 1UL, std::ceil( 0.5*size ) ) ); 218 219 randomize( vector, nonzeros, min, max ); 220 } 221 //********************************************************************************************** 222 223 //********************************************************************************************** 224 /*!\brief Randomization of a CompressedVector. 225 // 226 // \param vector The vector to be randomized. 227 // \param nonzeros The number of non-zero elements of the random vector. 228 // \param min The smallest possible value for a vector element. 229 // \param max The largest possible value for a vector element. 230 // \return void 231 // \exception std::invalid_argument Invalid number of non-zero elements. 232 */ 233 template< typename Arg > // Min/max argument type randomize(CompressedVector<Type,TF,Tag> & vector,size_t nonzeros,const Arg & min,const Arg & max)234 inline void randomize( CompressedVector<Type,TF,Tag>& vector, 235 size_t nonzeros, const Arg& min, const Arg& max ) const 236 { 237 const size_t size( vector.size() ); 238 239 if( nonzeros > size ) { 240 BLAZE_THROW_INVALID_ARGUMENT( "Invalid number of non-zero elements" ); 241 } 242 243 if( size == 0UL ) return; 244 245 vector.reset(); 246 vector.reserve( nonzeros ); 247 248 const Indices<size_t> indices( 0UL, vector.size()-1UL, nonzeros ); 249 250 for( size_t index : indices ) { 251 vector.append( index, rand<Type>( min, max ) ); 252 } 253 } 254 //********************************************************************************************** 255 }; 256 /*! \endcond */ 257 //************************************************************************************************* 258 259 } // namespace blaze 260 261 #endif 262