Vector.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_VECTOR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_VECTOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/math/Exception.h>
46 #include <blaze/system/Inline.h>
47 #include <blaze/util/Assert.h>
48 #include <blaze/util/DisableIf.h>
49 #include <blaze/util/EnableIf.h>
51 #include <blaze/util/Types.h>
53 #include <blaze/util/Unused.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // CLASS DEFINITION
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
76 template< typename VT // Type of the vector
77  , bool TF > // Transpose flag
78 struct Vector
79 {
80  //**Type definitions****************************************************************************
81  using VectorType = VT;
82  //**********************************************************************************************
83 
84  //**Non-const conversion operator***************************************************************
90  return *static_cast<VectorType*>( this );
91  }
92  //**********************************************************************************************
93 
94  //**Const conversion operators******************************************************************
99  BLAZE_ALWAYS_INLINE const VectorType& operator~() const noexcept {
100  return *static_cast<const VectorType*>( this );
101  }
102  //**********************************************************************************************
103 };
104 //*************************************************************************************************
105 
106 
107 
108 
109 //=================================================================================================
110 //
111 // GLOBAL FUNCTIONS
112 //
113 //=================================================================================================
114 
115 //*************************************************************************************************
118 template< typename VT, bool TF >
120 
121 template< typename VT, bool TF >
122 BLAZE_ALWAYS_INLINE typename VT::ConstIterator begin( const Vector<VT,TF>& vector );
123 
124 template< typename VT, bool TF >
126 
127 template< typename VT, bool TF >
129 
130 template< typename VT, bool TF >
131 BLAZE_ALWAYS_INLINE typename VT::ConstIterator end( const Vector<VT,TF>& vector );
132 
133 template< typename VT, bool TF >
134 BLAZE_ALWAYS_INLINE typename VT::ConstIterator cend( const Vector<VT,TF>& vector );
135 
136 template< typename VT, bool TF >
137 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept;
138 
139 template< typename VT, bool TF >
140 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept;
141 
142 template< typename VT, bool TF >
143 BLAZE_ALWAYS_INLINE size_t nonZeros( const Vector<VT,TF>& vector );
144 
145 template< typename VT, bool TF >
146 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve=true );
147 
148 template< typename VT, bool TF >
150 
151 template< typename VT, bool TF >
152 inline const typename VT::ResultType evaluate( const Vector<VT,TF>& vector );
153 
154 template< typename VT1, bool TF1, typename VT2, bool TF2 >
155 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept;
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
167 template< typename VT // Type of the vector
168  , bool TF > // Transpose flag of the vector
170 {
171  return (~vector).begin();
172 }
173 //*************************************************************************************************
174 
175 
176 //*************************************************************************************************
183 template< typename VT // Type of the vector
184  , bool TF > // Transpose flag of the vector
186 {
187  return (~vector).begin();
188 }
189 //*************************************************************************************************
190 
191 
192 //*************************************************************************************************
199 template< typename VT // Type of the vector
200  , bool TF > // Transpose flag of the vector
202 {
203  return (~vector).begin();
204 }
205 //*************************************************************************************************
206 
207 
208 //*************************************************************************************************
215 template< typename VT // Type of the vector
216  , bool TF > // Transpose flag of the vector
218 {
219  return (~vector).end();
220 }
221 //*************************************************************************************************
222 
223 
224 //*************************************************************************************************
231 template< typename VT // Type of the vector
232  , bool TF > // Transpose flag of the vector
234 {
235  return (~vector).end();
236 }
237 //*************************************************************************************************
238 
239 
240 //*************************************************************************************************
247 template< typename VT // Type of the vector
248  , bool TF > // Transpose flag of the vector
250 {
251  return (~vector).end();
252 }
253 //*************************************************************************************************
254 
255 
256 //*************************************************************************************************
263 template< typename VT // Type of the vector
264  , bool TF > // Transpose flag of the vector
265 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept
266 {
267  return (~vector).size();
268 }
269 //*************************************************************************************************
270 
271 
272 //*************************************************************************************************
279 template< typename VT // Type of the vector
280  , bool TF > // Transpose flag of the vector
281 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept
282 {
283  return (~vector).capacity();
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
298 template< typename VT // Type of the vector
299  , bool TF > // Transpose flag of the vector
301 {
302  return (~vector).nonZeros();
303 }
304 //*************************************************************************************************
305 
306 
307 //*************************************************************************************************
321 template< typename VT // Type of the vector
322  , bool TF > // Transpose flag of the vector
324  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
325 {
326  UNUSED_PARAMETER( preserve );
327 
328  if( (~vector).size() != n ) {
329  BLAZE_THROW_INVALID_ARGUMENT( "Vector cannot be resized" );
330  }
331 }
333 //*************************************************************************************************
334 
335 
336 //*************************************************************************************************
348 template< typename VT // Type of the vector
349  , bool TF > // Transpose flag of the vector
351  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
352 {
353  (~vector).resize( n, preserve );
354 }
356 //*************************************************************************************************
357 
358 
359 //*************************************************************************************************
387 template< typename VT // Type of the vector
388  , bool TF > // Transpose flag of the vector
389 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve )
390 {
391  resize_backend( vector, n, preserve );
392 }
393 //*************************************************************************************************
394 
395 
396 //*************************************************************************************************
404 template< typename VT // Type of the vector
405  , bool TF > // Transpose flag of the vector
407  shrinkToFit_backend( Vector<VT,TF>& vector )
408 {
409  UNUSED_PARAMETER( vector );
410 }
412 //*************************************************************************************************
413 
414 
415 //*************************************************************************************************
423 template< typename VT // Type of the vector
424  , bool TF > // Transpose flag of the vector
426  shrinkToFit_backend( Vector<VT,TF>& vector )
427 {
428  (~vector).shrinkToFit();
429 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
447 template< typename VT // Type of the vector
448  , bool TF > // Transpose flag of the vector
450 {
451  shrinkToFit_backend( vector );
452 }
453 //*************************************************************************************************
454 
455 
456 //*************************************************************************************************
506 template< typename VT // Type of the vector
507  , bool TF > // Transpose flag of the vector
508 inline const typename VT::ResultType evaluate( const Vector<VT,TF>& vector )
509 {
510  const typename VT::ResultType tmp( ~vector );
511  return tmp;
512 }
513 //*************************************************************************************************
514 
515 
516 //*************************************************************************************************
549 template< typename VT1 // Type of the left-hand side vector
550  , bool TF1 // Transpose flag of the left-hand side vector
551  , typename VT2 // Type of the right-hand side vector
552  , bool TF2 > // Transpose flag of the right-hand side vector
553 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept
554 {
555  return ( IsSame<VT1,VT2>::value &&
556  reinterpret_cast<const void*>( &a ) == reinterpret_cast<const void*>( &b ) );
557 }
558 //*************************************************************************************************
559 
560 
561 //*************************************************************************************************
576 template< typename VT1 // Type of the left-hand side vector
577  , bool TF1 // Transpose flag of the left-hand side vector
578  , typename VT2 // Type of the right-hand side vector
579  , bool TF2 > // Transpose flag of the right-hand side vector
580 BLAZE_ALWAYS_INLINE void assign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
581 {
583 
584  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
585  (~lhs).assign( ~rhs );
586 }
588 //*************************************************************************************************
589 
590 
591 //*************************************************************************************************
606 template< typename VT1 // Type of the left-hand side vector
607  , bool TF1 // Transpose flag of the left-hand side vector
608  , typename VT2 // Type of the right-hand side vector
609  , bool TF2 > // Transpose flag of the right-hand side vector
610 BLAZE_ALWAYS_INLINE void addAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
611 {
613 
614  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
615  (~lhs).addAssign( ~rhs );
616 }
618 //*************************************************************************************************
619 
620 
621 //*************************************************************************************************
636 template< typename VT1 // Type of the left-hand side vector
637  , bool TF1 // Transpose flag of the left-hand side vector
638  , typename VT2 // Type of the right-hand side vector
639  , bool TF2 > // Transpose flag of the right-hand side vector
640 BLAZE_ALWAYS_INLINE void subAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
641 {
643 
644  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
645  (~lhs).subAssign( ~rhs );
646 }
648 //*************************************************************************************************
649 
650 
651 //*************************************************************************************************
666 template< typename VT1 // Type of the left-hand side vector
667  , bool TF1 // Transpose flag of the left-hand side vector
668  , typename VT2 // Type of the right-hand side vector
669  , bool TF2 > // Transpose flag of the right-hand side vector
670 BLAZE_ALWAYS_INLINE void multAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
671 {
673 
674  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
675  (~lhs).multAssign( ~rhs );
676 }
678 //*************************************************************************************************
679 
680 
681 //*************************************************************************************************
696 template< typename VT1 // Type of the left-hand side vector
697  , bool TF1 // Transpose flag of the left-hand side vector
698  , typename VT2 // Type of the right-hand side vector
699  , bool TF2 > // Transpose flag of the right-hand side vector
700 BLAZE_ALWAYS_INLINE void divAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
701 {
703 
704  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
705  (~lhs).divAssign( ~rhs );
706 }
708 //*************************************************************************************************
709 
710 
711 //*************************************************************************************************
726 template< typename VT1 // Type of the left-hand side vector
727  , bool TF1 // Transpose flag of the left-hand side vector
728  , typename VT2 // Type of the right-hand side vector
729  , bool TF2 > // Transpose flag of the right-hand side vector
730 BLAZE_ALWAYS_INLINE bool tryAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
731 {
732  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
733  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
734 
735  UNUSED_PARAMETER( lhs, rhs, index );
736 
737  return true;
738 }
740 //*************************************************************************************************
741 
742 
743 //*************************************************************************************************
758 template< typename VT1 // Type of the left-hand side vector
759  , bool TF1 // Transpose flag of the left-hand side vector
760  , typename VT2 // Type of the right-hand side vector
761  , bool TF2 > // Transpose flag of the right-hand side vector
762 BLAZE_ALWAYS_INLINE bool tryAddAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
763 {
764  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
765  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
766 
767  UNUSED_PARAMETER( lhs, rhs, index );
768 
769  return true;
770 }
772 //*************************************************************************************************
773 
774 
775 //*************************************************************************************************
790 template< typename VT1 // Type of the left-hand side vector
791  , bool TF1 // Transpose flag of the left-hand side vector
792  , typename VT2 // Type of the right-hand side vector
793  , bool TF2 > // Transpose flag of the right-hand side vector
794 BLAZE_ALWAYS_INLINE bool trySubAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
795 {
796  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
797  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
798 
799  UNUSED_PARAMETER( lhs, rhs, index );
800 
801  return true;
802 }
804 //*************************************************************************************************
805 
806 
807 //*************************************************************************************************
822 template< typename VT1 // Type of the left-hand side vector
823  , bool TF1 // Transpose flag of the left-hand side vector
824  , typename VT2 // Type of the right-hand side vector
825  , bool TF2 > // Transpose flag of the right-hand side vector
826 BLAZE_ALWAYS_INLINE bool tryMultAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
827 {
828  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
829  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
830 
831  UNUSED_PARAMETER( lhs, rhs, index );
832 
833  return true;
834 }
836 //*************************************************************************************************
837 
838 
839 //*************************************************************************************************
854 template< typename VT1 // Type of the left-hand side vector
855  , bool TF1 // Transpose flag of the left-hand side vector
856  , typename VT2 // Type of the right-hand side vector
857  , bool TF2 > // Transpose flag of the right-hand side vector
858 BLAZE_ALWAYS_INLINE bool tryDivAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
859 {
860  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
861  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
862 
863  UNUSED_PARAMETER( lhs, rhs, index );
864 
865  return true;
866 }
868 //*************************************************************************************************
869 
870 
871 //*************************************************************************************************
886 template< typename VT // Type of the vector
887  , bool TF > // Transpose flag of the vector
888 BLAZE_ALWAYS_INLINE VT& derestrict( Vector<VT,TF>& vector )
889 {
890  return ~vector;
891 }
893 //*************************************************************************************************
894 
895 } // namespace blaze
896 
897 #endif
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for the UNUSED_PARAMETER function template.
BLAZE_ALWAYS_INLINE size_t capacity(const Matrix< MT, SO > &matrix) noexcept
Returns the maximum capacity of the matrix.
Definition: Matrix.h:356
BLAZE_ALWAYS_INLINE bool isSame(const Matrix< MT1, SO1 > &a, const Matrix< MT2, SO2 > &b) noexcept
Returns whether the two given matrices represent the same observable state.
Definition: Matrix.h:786
Header file for basic type definitions.
BLAZE_ALWAYS_INLINE size_t size(const Vector< VT, TF > &vector) noexcept
Returns the current size/dimension of the vector.
Definition: Vector.h:265
Header file for the IsSame and IsStrictlySame type traits.
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:3078
BLAZE_ALWAYS_INLINE MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:198
BLAZE_ALWAYS_INLINE void shrinkToFit(Matrix< MT, SO > &matrix)
Requesting the removal of unused capacity.
Definition: Matrix.h:609
typename DisableIf< Condition, T >::Type DisableIf_
Auxiliary type for the DisableIf class template.The DisableIf_ alias declaration provides a convenien...
Definition: DisableIf.h:224
BLAZE_ALWAYS_INLINE size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:394
Type relationship analysis.This class tests if the two data types A and B are equal. For this type comparison, the cv-qualifiers of both data types are ignored. If A and B are the same data type (ignoring the cv-qualifiers), then the value member constant is set to true, the nested type definition Type is TrueType, and the class derives from TrueType. Otherwise value is set to false, Type is FalseType, and the class derives from FalseType.
Definition: IsSame.h:140
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:3086
const MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:722
BLAZE_ALWAYS_INLINE VectorType & operator~() noexcept
Conversion operator for non-constant vectors.
Definition: Vector.h:89
BLAZE_ALWAYS_INLINE MT::ConstIterator cend(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:308
BLAZE_ALWAYS_INLINE MT::ConstIterator cbegin(const Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:242
Header file for the DisableIf class template.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
#define BLAZE_ALWAYS_INLINE
Platform dependent setup of an enforced inline keyword.
Definition: Inline.h:85
Expression object for sparse matrix-sparse vector multiplications.The TSMatSVecMultExpr class represe...
Definition: Forward.h:167
Header file for the IsShrinkable type trait.
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:3087
Header file for the exception macros of the math module.
BLAZE_ALWAYS_INLINE void resize(Matrix< MT, SO > &matrix, size_t rows, size_t columns, bool preserve=true)
Changing the size of the matrix.
Definition: Matrix.h:548
BLAZE_ALWAYS_INLINE MT::Iterator end(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator just past the last element of row/column i.
Definition: Matrix.h:264
Header file for the EnableIf class template.
Header file for run time assertion macros.
#define BLAZE_FUNCTION_TRACE
Function trace macro.This macro can be used to reliably trace function calls. In case function tracin...
Definition: FunctionTrace.h:94
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:224
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:177
void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
Header file for the IsResizable type trait.
System settings for the inline keywords.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the function trace functionality.
BLAZE_ALWAYS_INLINE const VectorType & operator~() const noexcept
Conversion operator for constant vectors.
Definition: Vector.h:99