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>
45 #include <blaze/system/Inline.h>
46 #include <blaze/util/Assert.h>
47 #include <blaze/util/DisableIf.h>
48 #include <blaze/util/EnableIf.h>
50 #include <blaze/util/Types.h>
52 #include <blaze/util/Unused.h>
53 
54 
55 namespace blaze {
56 
57 //=================================================================================================
58 //
59 // CLASS DEFINITION
60 //
61 //=================================================================================================
62 
63 //*************************************************************************************************
75 template< typename VT // Type of the vector
76  , bool TF > // Transpose flag
77 struct Vector
78 {
79  //**Type definitions****************************************************************************
80  typedef VT VectorType;
81  //**********************************************************************************************
82 
83  //**Non-const conversion operator***************************************************************
88  BLAZE_ALWAYS_INLINE VectorType& operator~() noexcept {
89  return *static_cast<VectorType*>( this );
90  }
91  //**********************************************************************************************
92 
93  //**Const conversion operators******************************************************************
98  BLAZE_ALWAYS_INLINE const VectorType& operator~() const noexcept {
99  return *static_cast<const VectorType*>( this );
100  }
101  //**********************************************************************************************
102 };
103 //*************************************************************************************************
104 
105 
106 
107 
108 //=================================================================================================
109 //
110 // GLOBAL FUNCTIONS
111 //
112 //=================================================================================================
113 
114 //*************************************************************************************************
117 template< typename VT, bool TF >
119 
120 template< typename VT, bool TF >
121 BLAZE_ALWAYS_INLINE typename VT::ConstIterator begin( const Vector<VT,TF>& vector );
122 
123 template< typename VT, bool TF >
125 
126 template< typename VT, bool TF >
128 
129 template< typename VT, bool TF >
130 BLAZE_ALWAYS_INLINE typename VT::ConstIterator end( const Vector<VT,TF>& vector );
131 
132 template< typename VT, bool TF >
133 BLAZE_ALWAYS_INLINE typename VT::ConstIterator cend( const Vector<VT,TF>& vector );
134 
135 template< typename VT, bool TF >
136 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept;
137 
138 template< typename VT, bool TF >
139 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept;
140 
141 template< typename VT, bool TF >
142 BLAZE_ALWAYS_INLINE size_t nonZeros( const Vector<VT,TF>& vector );
143 
144 template< typename VT, bool TF >
145 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve=true );
146 
147 template< typename VT, bool TF >
148 inline const typename VT::ResultType evaluate( const Vector<VT,TF>& vector );
149 
150 template< typename VT1, bool TF1, typename VT2, bool TF2 >
151 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept;
153 //*************************************************************************************************
154 
155 
156 //*************************************************************************************************
163 template< typename VT // Type of the vector
164  , bool TF > // Transpose flag of the vector
166 {
167  return (~vector).begin();
168 }
169 //*************************************************************************************************
170 
171 
172 //*************************************************************************************************
179 template< typename VT // Type of the vector
180  , bool TF > // Transpose flag of the vector
182 {
183  return (~vector).begin();
184 }
185 //*************************************************************************************************
186 
187 
188 //*************************************************************************************************
195 template< typename VT // Type of the vector
196  , bool TF > // Transpose flag of the vector
198 {
199  return (~vector).begin();
200 }
201 //*************************************************************************************************
202 
203 
204 //*************************************************************************************************
211 template< typename VT // Type of the vector
212  , bool TF > // Transpose flag of the vector
214 {
215  return (~vector).end();
216 }
217 //*************************************************************************************************
218 
219 
220 //*************************************************************************************************
227 template< typename VT // Type of the vector
228  , bool TF > // Transpose flag of the vector
230 {
231  return (~vector).end();
232 }
233 //*************************************************************************************************
234 
235 
236 //*************************************************************************************************
243 template< typename VT // Type of the vector
244  , bool TF > // Transpose flag of the vector
246 {
247  return (~vector).end();
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
259 template< typename VT // Type of the vector
260  , bool TF > // Transpose flag of the vector
261 BLAZE_ALWAYS_INLINE size_t size( const Vector<VT,TF>& vector ) noexcept
262 {
263  return (~vector).size();
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
275 template< typename VT // Type of the vector
276  , bool TF > // Transpose flag of the vector
277 BLAZE_ALWAYS_INLINE size_t capacity( const Vector<VT,TF>& vector ) noexcept
278 {
279  return (~vector).capacity();
280 }
281 //*************************************************************************************************
282 
283 
284 //*************************************************************************************************
294 template< typename VT // Type of the vector
295  , bool TF > // Transpose flag of the vector
297 {
298  return (~vector).nonZeros();
299 }
300 //*************************************************************************************************
301 
302 
303 //*************************************************************************************************
317 template< typename VT // Type of the vector
318  , bool TF > // Transpose flag of the vector
320  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
321 {
322  UNUSED_PARAMETER( preserve );
323 
324  if( (~vector).size() != n ) {
325  BLAZE_THROW_INVALID_ARGUMENT( "Vector cannot be resized" );
326  }
327 }
329 //*************************************************************************************************
330 
331 
332 //*************************************************************************************************
344 template< typename VT // Type of the vector
345  , bool TF > // Transpose flag of the vector
347  resize_backend( Vector<VT,TF>& vector, size_t n, bool preserve )
348 {
349  (~vector).resize( n, preserve );
350 }
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
383 template< typename VT // Type of the vector
384  , bool TF > // Transpose flag of the vector
385 BLAZE_ALWAYS_INLINE void resize( Vector<VT,TF>& vector, size_t n, bool preserve )
386 {
387  resize_backend( vector, n, preserve );
388 }
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
442 template< typename VT // Type of the vector
443  , bool TF > // Transpose flag of the vector
444 inline const typename VT::ResultType evaluate( const Vector<VT,TF>& vector )
445 {
446  const typename VT::ResultType tmp( ~vector );
447  return tmp;
448 }
449 //*************************************************************************************************
450 
451 
452 //*************************************************************************************************
485 template< typename VT1 // Type of the left-hand side vector
486  , bool TF1 // Transpose flag of the left-hand side vector
487  , typename VT2 // Type of the right-hand side vector
488  , bool TF2 > // Transpose flag of the right-hand side vector
489 BLAZE_ALWAYS_INLINE bool isSame( const Vector<VT1,TF1>& a, const Vector<VT2,TF2>& b ) noexcept
490 {
491  return ( IsSame<VT1,VT2>::value &&
492  reinterpret_cast<const void*>( &a ) == reinterpret_cast<const void*>( &b ) );
493 }
494 //*************************************************************************************************
495 
496 
497 //*************************************************************************************************
512 template< typename VT1 // Type of the left-hand side vector
513  , bool TF1 // Transpose flag of the left-hand side vector
514  , typename VT2 // Type of the right-hand side vector
515  , bool TF2 > // Transpose flag of the right-hand side vector
516 BLAZE_ALWAYS_INLINE void assign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
517 {
519 
520  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
521  (~lhs).assign( ~rhs );
522 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
542 template< typename VT1 // Type of the left-hand side vector
543  , bool TF1 // Transpose flag of the left-hand side vector
544  , typename VT2 // Type of the right-hand side vector
545  , bool TF2 > // Transpose flag of the right-hand side vector
546 BLAZE_ALWAYS_INLINE void addAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
547 {
549 
550  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
551  (~lhs).addAssign( ~rhs );
552 }
554 //*************************************************************************************************
555 
556 
557 //*************************************************************************************************
572 template< typename VT1 // Type of the left-hand side vector
573  , bool TF1 // Transpose flag of the left-hand side vector
574  , typename VT2 // Type of the right-hand side vector
575  , bool TF2 > // Transpose flag of the right-hand side vector
576 BLAZE_ALWAYS_INLINE void subAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
577 {
579 
580  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
581  (~lhs).subAssign( ~rhs );
582 }
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
602 template< typename VT1 // Type of the left-hand side vector
603  , bool TF1 // Transpose flag of the left-hand side vector
604  , typename VT2 // Type of the right-hand side vector
605  , bool TF2 > // Transpose flag of the right-hand side vector
606 BLAZE_ALWAYS_INLINE void multAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
607 {
609 
610  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
611  (~lhs).multAssign( ~rhs );
612 }
614 //*************************************************************************************************
615 
616 
617 //*************************************************************************************************
632 template< typename VT1 // Type of the left-hand side vector
633  , bool TF1 // Transpose flag of the left-hand side vector
634  , typename VT2 // Type of the right-hand side vector
635  , bool TF2 > // Transpose flag of the right-hand side vector
636 BLAZE_ALWAYS_INLINE void divAssign( Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs )
637 {
639 
640  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
641  (~lhs).divAssign( ~rhs );
642 }
644 //*************************************************************************************************
645 
646 
647 //*************************************************************************************************
662 template< typename VT1 // Type of the left-hand side vector
663  , bool TF1 // Transpose flag of the left-hand side vector
664  , typename VT2 // Type of the right-hand side vector
665  , bool TF2 > // Transpose flag of the right-hand side vector
666 BLAZE_ALWAYS_INLINE bool tryAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
667 {
668  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
669  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
670 
671  UNUSED_PARAMETER( lhs, rhs, index );
672 
673  return true;
674 }
676 //*************************************************************************************************
677 
678 
679 //*************************************************************************************************
694 template< typename VT1 // Type of the left-hand side vector
695  , bool TF1 // Transpose flag of the left-hand side vector
696  , typename VT2 // Type of the right-hand side vector
697  , bool TF2 > // Transpose flag of the right-hand side vector
698 BLAZE_ALWAYS_INLINE bool tryAddAssign( const Vector<VT1,TF1>& lhs, const Vector<VT2,TF2>& rhs, size_t index )
699 {
700  BLAZE_INTERNAL_ASSERT( index <= (~lhs).size(), "Invalid vector access index" );
701  BLAZE_INTERNAL_ASSERT( (~rhs).size() <= (~lhs).size() - index, "Invalid vector size" );
702 
703  UNUSED_PARAMETER( lhs, rhs, index );
704 
705  return true;
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 trySubAssign( 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 tryMultAssign( 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 tryDivAssign( 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 VT // Type of the vector
823  , bool TF > // Transpose flag of the vector
824 BLAZE_ALWAYS_INLINE VT& derestrict( Vector<VT,TF>& vector )
825 {
826  return ~vector;
827 }
829 //*************************************************************************************************
830 
831 } // namespace blaze
832 
833 #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:352
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:721
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:261
Header file for the IsSame and IsStrictlySame type traits.
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:194
typename DisableIf< Condition, T >::Type DisableIf_
Auxiliary type for the DisableIf class template.The DisableIf_ alias declaration provides a convenien...
Definition: DisableIf.h:223
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:390
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:138
VT VectorType
Type of the vector.
Definition: Vector.h:80
const MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:657
BLAZE_ALWAYS_INLINE VectorType & operator~() noexcept
Conversion operator for non-constant vectors.
Definition: Vector.h:88
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:304
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:238
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
const Element * ConstIterator
Iterator over constant elements.
Definition: CompressedMatrix.h:2939
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:544
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:260
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:93
Element * Iterator
Iterator over non-constant elements.
Definition: CompressedMatrix.h:2938
typename EnableIf< Condition, T >::Type EnableIf_
Auxiliary alias declaration for the EnableIf class template.The EnableIf_ alias declaration provides ...
Definition: EnableIf.h:223
Base class for N-dimensional vectors.The Vector class is a base class for all arbitrarily sized (N-di...
Definition: Forward.h:164
This ResultType
Result type for expression template evaluations.
Definition: CompressedMatrix.h:2930
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:98