Blaze  3.6
SVecSVecCrossExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_SVECSVECCROSSEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_SVECSVECCROSSEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
44 #include <blaze/math/Aliases.h>
52 #include <blaze/math/Exception.h>
63 #include <blaze/util/Assert.h>
64 #include <blaze/util/DisableIf.h>
65 #include <blaze/util/EnableIf.h>
67 #include <blaze/util/MaybeUnused.h>
68 #include <blaze/util/mpl/If.h>
69 #include <blaze/util/Types.h>
70 
71 
72 namespace blaze {
73 
74 //=================================================================================================
75 //
76 // CLASS SVECSVECCROSSEXPR
77 //
78 //=================================================================================================
79 
80 //*************************************************************************************************
87 template< typename VT1 // Type of the left-hand side sparse vector
88  , typename VT2 // Type of the right-hand side sparse vector
89  , bool TF > // Transpose flag
90 class SVecSVecCrossExpr
91  : public CrossExpr< DenseVector< SVecSVecCrossExpr<VT1,VT2,TF>, TF > >
92  , private Computation
93 {
94  private:
95  //**Type definitions****************************************************************************
104  //**********************************************************************************************
105 
106  //**Return type evaluation**********************************************************************
108 
113  static constexpr bool returnExpr = ( !IsTemporary_v<RN1> && !IsTemporary_v<RN2> );
114 
116  using ExprReturnType = decltype( ( std::declval<RN1>() * std::declval<RN2>() ) -
117  ( std::declval<RN1>() * std::declval<RN2>() ) );
118  //**********************************************************************************************
119 
120  public:
121  //**Type definitions****************************************************************************
127 
130 
132  using CompositeType = const ResultType;
133 
135  using LeftOperand = If_t< IsExpression_v<VT1>, const VT1, const VT1& >;
136 
138  using RightOperand = If_t< IsExpression_v<VT2>, const VT2, const VT2& >;
139 
142 
145  //**********************************************************************************************
146 
147  //**Compilation flags***************************************************************************
149  static constexpr bool simdEnabled = false;
150 
152  static constexpr bool smpAssignable = false;
153  //**********************************************************************************************
154 
155  //**Constructor*********************************************************************************
161  explicit inline SVecSVecCrossExpr( const VT1& lhs, const VT2& rhs ) noexcept
162  : lhs_( lhs ) // Left-hand side sparse vector of the cross product expression
163  , rhs_( rhs ) // Right-hand side sparse vector of the cross product expression
164  {
165  BLAZE_INTERNAL_ASSERT( lhs.size() == 3UL, "Invalid vector size" );
166  BLAZE_INTERNAL_ASSERT( rhs.size() == 3UL, "Invalid vector size" );
167  }
168  //**********************************************************************************************
169 
170  //**Subscript operator**************************************************************************
176  inline ReturnType operator[]( size_t index ) const {
177  BLAZE_INTERNAL_ASSERT( index < 3UL, "Invalid vector access index" );
178 
179  if( index == 0UL )
180  return lhs_[1UL] * rhs_[2UL] - lhs_[2UL] * rhs_[1UL];
181  else if( index == 1UL )
182  return lhs_[2UL] * rhs_[0UL] - lhs_[0UL] * rhs_[2UL];
183  else
184  return lhs_[0UL] * rhs_[1UL] - lhs_[1UL] * rhs_[0UL];
185  }
186  //**********************************************************************************************
187 
188  //**At function*********************************************************************************
195  inline ReturnType at( size_t index ) const {
196  if( index >= 3UL ) {
197  BLAZE_THROW_OUT_OF_RANGE( "Invalid vector access index" );
198  }
199  return (*this)[index];
200  }
201  //**********************************************************************************************
202 
203  //**Size function*******************************************************************************
208  inline constexpr size_t size() const noexcept {
209  return 3UL;
210  }
211  //**********************************************************************************************
212 
213  //**Left operand access*************************************************************************
218  inline LeftOperand leftOperand() const noexcept {
219  return lhs_;
220  }
221  //**********************************************************************************************
222 
223  //**Right operand access************************************************************************
228  inline RightOperand rightOperand() const noexcept {
229  return rhs_;
230  }
231  //**********************************************************************************************
232 
233  //**********************************************************************************************
239  template< typename T >
240  inline bool canAlias( const T* alias ) const noexcept {
241  return ( lhs_.canAlias( alias ) || rhs_.canAlias( alias ) );
242  }
243  //**********************************************************************************************
244 
245  //**********************************************************************************************
251  template< typename T >
252  inline bool isAliased( const T* alias ) const noexcept {
253  return ( lhs_.isAliased( alias ) || rhs_.isAliased( alias ) );
254  }
255  //**********************************************************************************************
256 
257  private:
258  //**Member variables****************************************************************************
261  //**********************************************************************************************
262 
263  //**Assignment to dense vectors*****************************************************************
275  template< typename VT > // Type of the target dense vector
276  friend inline void assign( DenseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
277  {
279 
280  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
281  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
282 
283  LT x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
284  RT y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
285 
286  (~lhs)[0] = x[1UL]*y[2UL] - x[2UL]*y[1UL];
287  (~lhs)[1] = x[2UL]*y[0UL] - x[0UL]*y[2UL];
288  (~lhs)[2] = x[0UL]*y[1UL] - x[1UL]*y[0UL];
289  }
291  //**********************************************************************************************
292 
293  //**Assignment to sparse vectors****************************************************************
305  template< typename VT > // Type of the target sparse vector
306  friend inline void assign( SparseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
307  {
309 
313 
314  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
315  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
316 
317  const ResultType tmp( serial( rhs ) );
318  assign( ~lhs, tmp );
319  }
321  //**********************************************************************************************
322 
323  //**Addition assignment to dense vectors********************************************************
335  template< typename VT > // Type of the target dense vector
336  friend inline void addAssign( DenseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
337  {
339 
340  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
341  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
342 
343  LT x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
344  RT y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
345 
346  (~lhs)[0] += x[1UL]*y[2UL] - x[2UL]*y[1UL];
347  (~lhs)[1] += x[2UL]*y[0UL] - x[0UL]*y[2UL];
348  (~lhs)[2] += x[0UL]*y[1UL] - x[1UL]*y[0UL];
349  }
351  //**********************************************************************************************
352 
353  //**Addition assignment to sparse vectors*******************************************************
354  // No special implementation for the addition assignment to sparse vectors.
355  //**********************************************************************************************
356 
357  //**Subtraction assignment to dense vectors*****************************************************
369  template< typename VT > // Type of the target dense vector
370  friend inline void subAssign( DenseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
371  {
373 
374  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
375  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
376 
377  LT x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
378  RT y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
379 
380  (~lhs)[0] -= x[1UL]*y[2UL] - x[2UL]*y[1UL];
381  (~lhs)[1] -= x[2UL]*y[0UL] - x[0UL]*y[2UL];
382  (~lhs)[2] -= x[0UL]*y[1UL] - x[1UL]*y[0UL];
383  }
385  //**********************************************************************************************
386 
387  //**Subtraction assignment to sparse vectors****************************************************
388  // No special implementation for the subtraction assignment to sparse vectors.
389  //**********************************************************************************************
390 
391  //**Multiplication assignment to dense vectors**************************************************
403  template< typename VT > // Type of the target dense vector
404  friend inline void multAssign( DenseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
405  {
407 
408  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
409  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
410 
411  LT x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
412  RT y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
413 
414  (~lhs)[0] *= x[1UL]*y[2UL] - x[2UL]*y[1UL];
415  (~lhs)[1] *= x[2UL]*y[0UL] - x[0UL]*y[2UL];
416  (~lhs)[2] *= x[0UL]*y[1UL] - x[1UL]*y[0UL];
417  }
419  //**********************************************************************************************
420 
421  //**Multiplication assignment to sparse vectors*************************************************
422  // No special implementation for the multiplication assignment to sparse vectors.
423  //**********************************************************************************************
424 
425  //**Division assignment to dense vectors********************************************************
437  template< typename VT > // Type of the target dense vector
438  friend inline void divAssign( DenseVector<VT,TF>& lhs, const SVecSVecCrossExpr& rhs )
439  {
441 
442  BLAZE_INTERNAL_ASSERT( (~lhs).size() == 3UL, "Invalid vector size" );
443  BLAZE_INTERNAL_ASSERT( (~rhs).size() == 3UL, "Invalid vector size" );
444 
445  LT x( serial( rhs.lhs_ ) ); // Evaluation of the left-hand side sparse vector operand
446  RT y( serial( rhs.rhs_ ) ); // Evaluation of the right-hand side sparse vector operand
447 
448  (~lhs)[0] /= x[1UL]*y[2UL] - x[2UL]*y[1UL];
449  (~lhs)[1] /= x[2UL]*y[0UL] - x[0UL]*y[2UL];
450  (~lhs)[2] /= x[0UL]*y[1UL] - x[1UL]*y[0UL];
451  }
453  //**********************************************************************************************
454 
455  //**Division assignment to sparse vectors*******************************************************
456  // No special implementation for the division assignment to sparse vectors.
457  //**********************************************************************************************
458 
459  //**Compile time checks*************************************************************************
468  //**********************************************************************************************
469 };
470 //*************************************************************************************************
471 
472 
473 
474 
475 //=================================================================================================
476 //
477 // GLOBAL BINARY ARITHMETIC OPERATORS
478 //
479 //=================================================================================================
480 
481 //*************************************************************************************************
494 template< typename VT1 // Type of the left-hand side sparse vector
495  , typename VT2 // Type of the right-hand side sparse vector
496  , bool TF // Transpose flag
497  , DisableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
498 inline const SVecSVecCrossExpr<VT1,VT2,TF>
499  svecsveccross( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
500 {
502 
503  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
504 
505  return SVecSVecCrossExpr<VT1,VT2,TF>( ~lhs, ~rhs );
506 }
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
524 template< typename VT1 // Type of the left-hand side sparse vector
525  , typename VT2 // Type of the right-hand side sparse vector
526  , bool TF // Transpose flag
527  , EnableIf_t< IsZero_v<VT1> || IsZero_v<VT2> >* = nullptr >
528 inline decltype(auto)
529  svecsveccross( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
530 {
532 
533  MAYBE_UNUSED( lhs, rhs );
534 
535  BLAZE_INTERNAL_ASSERT( (~lhs).size() == (~rhs).size(), "Invalid vector sizes" );
536 
537  using ReturnType = const CrossTrait_t< ResultType_t<VT1>, ResultType_t<VT2> >;
538 
541 
542  return ReturnType( 3UL );
543 }
545 //*************************************************************************************************
546 
547 
548 //*************************************************************************************************
573 template< typename VT1 // Type of the left-hand side sparse vector
574  , typename VT2 // Type of the right-hand side sparse vector
575  , bool TF > // Transpose flag
576 inline decltype(auto)
577  operator%( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
578 {
580 
581  if( (~lhs).size() != 3UL || (~rhs).size() != 3UL ) {
582  BLAZE_THROW_INVALID_ARGUMENT( "Invalid vector size for cross product" );
583  }
584 
585  return svecsveccross( ~lhs, ~rhs );
586 }
587 //*************************************************************************************************
588 
589 
590 //*************************************************************************************************
615 template< typename VT1 // Type of the left-hand side sparse vector
616  , typename VT2 // Type of the right-hand side sparse vector
617  , bool TF > // Transpose flag
618 inline decltype(auto) cross( const SparseVector<VT1,TF>& lhs, const SparseVector<VT2,TF>& rhs )
619 {
620  return lhs % rhs;
621 }
622 //*************************************************************************************************
623 
624 } // namespace blaze
625 
626 #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 auxiliary alias declarations.
const StaticVector< ET2, 3UL, TF > RT
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:144
Header file for basic type definitions.
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
RightOperand rightOperand() const noexcept
Returns the right-hand side sparse vector operand.
Definition: SVecSVecCrossExpr.h:228
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
Header file for the serial shim.
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: SVecSVecCrossExpr.h:252
const StaticVector< ET1, 3UL, TF > LT
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:141
Constraint on the data type.
Header file for the DenseVector base class.
Header file for the MAYBE_UNUSED function template.
Header file for the Computation base class.
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: SVecSVecCrossExpr.h:240
ReturnType_t< VT2 > RN2
Return type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:99
LeftOperand lhs_
Left-hand side sparse vector of the cross product expression.
Definition: SVecSVecCrossExpr.h:259
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
ElementType_t< VT1 > ET1
Element type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:102
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
Efficient implementation of a fixed-sized vector.The StaticVector class template is the representatio...
Definition: Forward.h:61
Constraint on the transpose flag of vector types.
Header file for the DisableIf class template.
TransposeType_t< ResultType > TransposeType
Transpose type for expression template evaluations.
Definition: SVecSVecCrossExpr.h:125
Header file for the IsTemporary type trait class.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
#define BLAZE_CONSTRAINT_MUST_BE_ZERO_TYPE(T)
Constraint on the data type.In case the given data type T is not a zero vector or matrix type,...
Definition: Zero.h:61
#define BLAZE_THROW_OUT_OF_RANGE(MESSAGE)
Macro for the emission of a std::out_of_range exception.This macro encapsulates the default way of Bl...
Definition: Exception.h:331
Header file for all forward declarations for dense vectors and matrices.
ElementType_t< VT2 > ET2
Element type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:103
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
LeftOperand leftOperand() const noexcept
Returns the left-hand side sparse vector operand.
Definition: SVecSVecCrossExpr.h:218
static constexpr bool returnExpr
Compilation switch for the selection of the subscript operator return type.
Definition: SVecSVecCrossExpr.h:113
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
#define BLAZE_CONSTRAINT_MUST_BE_SPARSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a sparse, N-dimensional vector type,...
Definition: SparseVector.h:61
Constraint on the data type.
ReturnType operator[](size_t index) const
Subscript operator for the direct access to the vector elements.
Definition: SVecSVecCrossExpr.h:176
CrossTrait_t< RT1, RT2 > ResultType
Result type for expression template evaluations.
Definition: SVecSVecCrossExpr.h:124
Constraint on the data type.
Header file for the exception macros of the math module.
If_t< IsExpression_v< VT1 >, const VT1, const VT1 & > LeftOperand
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:135
RightOperand rhs_
Right-hand side sparse vector of the cross product expression.
Definition: SVecSVecCrossExpr.h:260
If_t< IsExpression_v< VT2 >, const VT2, const VT2 & > RightOperand
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:138
static constexpr bool simdEnabled
Compilation switch for the expression template evaluation strategy.
Definition: SVecSVecCrossExpr.h:149
Constraint on the data type.
Constraint on the data type.
constexpr size_t size() const noexcept
Returns the current size/dimension of the vector.
Definition: SVecSVecCrossExpr.h:208
ResultType_t< VT2 > RT2
Result type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:97
Header file for the EnableIf class template.
const If_t< returnExpr, ExprReturnType, ElementType > ReturnType
Return type for expression template evaluations.
Definition: SVecSVecCrossExpr.h:129
typename CrossTrait< T1, T2 >::Type CrossTrait_t
Auxiliary alias declaration for the CrossTrait class template.The CrossTrait_t alias declaration prov...
Definition: CrossTrait.h:164
Header file for the CrossExpr base class.
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.The TransposeType_t alias declaration pro...
Definition: Aliases.h:470
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Header file for the cross product trait.
ReturnType_t< VT1 > RN1
Return type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:98
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: SVecSVecCrossExpr.h:152
Header file for the IsZero type trait.
#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
Header file for all forward declarations for expression class templates.
decltype((std::declval< RN1 >() *std::declval< RN2 >()) -(std::declval< RN1 >() *std::declval< RN2 >())) ExprReturnType
Expression return type for the subscript operator.
Definition: SVecSVecCrossExpr.h:117
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:808
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:81
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_VECTOR_TYPE(T)
Constraint on the data type.In case the given data type T is not a dense, N-dimensional vector type,...
Definition: DenseVector.h:61
const ResultType CompositeType
Data type for composite expression templates.
Definition: SVecSVecCrossExpr.h:132
ElementType_t< ResultType > ElementType
Resulting element type.
Definition: SVecSVecCrossExpr.h:126
ReturnType at(size_t index) const
Checked access to the vector elements.
Definition: SVecSVecCrossExpr.h:195
Header file for the IsComputation type trait class.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
SVecSVecCrossExpr(const VT1 &lhs, const VT2 &rhs) noexcept
Constructor for the SVecSVecCrossExpr class.
Definition: SVecSVecCrossExpr.h:161
CompositeType_t< VT1 > CT1
Composite type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:100
ResultType_t< VT1 > RT1
Result type of the left-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:96
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ZERO_TYPE(T)
Constraint on the data type.In case the given data type T is a zero vector or matrix type,...
Definition: Zero.h:81
#define BLAZE_CONSTRAINT_MUST_BE_VECTOR_WITH_TRANSPOSE_FLAG(T, TF)
Constraint on the data type.In case the given data type T is not a dense or sparse vector type and in...
Definition: TransposeFlag.h:63
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Expression object for sparse vector-sparse vector cross products.The SVecSVecCrossExpr class represen...
Definition: Forward.h:160
CompositeType_t< VT2 > CT2
Composite type of the right-hand side sparse vector expression.
Definition: SVecSVecCrossExpr.h:101
Header file for the IsExpression type trait class.
Header file for the function trace functionality.
decltype(auto) cross(const DenseVector< VT1, TF > &lhs, const DenseVector< VT2, TF > &rhs)
Cross product of two dense vectors ( ).
Definition: DVecDVecCrossExpr.h:544