Blaze  3.6
DVecNormExpr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_MATH_EXPRESSIONS_DVECNORMEXPR_H_
36 #define _BLAZE_MATH_EXPRESSIONS_DVECNORMEXPR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <utility>
44 #include <blaze/math/Aliases.h>
64 #include <blaze/math/SIMD.h>
73 #include <blaze/util/Assert.h>
76 #include <blaze/util/mpl/And.h>
77 #include <blaze/util/mpl/If.h>
79 #include <blaze/util/TypeList.h>
80 #include <blaze/util/Types.h>
83 
84 
85 namespace blaze {
86 
87 //=================================================================================================
88 //
89 // CLASS DEFINITION
90 //
91 //=================================================================================================
92 
93 //*************************************************************************************************
98 template< typename VT // Type of the dense vector
99  , typename Abs // Type of the abs operation
100  , typename Power > // Type of the power operation
101 struct DVecNormHelper
102 {
103  //**Type definitions****************************************************************************
105  using ET = ElementType_t<VT>;
106 
108  using CT = RemoveReference_t< CompositeType_t<VT> >;
109  //**********************************************************************************************
110 
111  //**********************************************************************************************
112  static constexpr bool value =
113  ( useOptimizedKernels &&
114  CT::simdEnabled &&
115  If_t< HasSIMDEnabled_v<Abs> && HasSIMDEnabled_v<Power>
116  , And_t< GetSIMDEnabled<Abs,ET>, GetSIMDEnabled<Power,ET> >
117  , And_t< HasLoad<Abs>, HasLoad<Power> > >::value &&
118  HasSIMDAdd_v< ElementType_t<CT>, ElementType_t<CT> > );
119  //**********************************************************************************************
120 };
122 //*************************************************************************************************
123 
124 
125 
126 
127 //=================================================================================================
128 //
129 // GLOBAL FUNCTIONS
130 //
131 //=================================================================================================
132 
133 //*************************************************************************************************
148 template< typename VT // Type of the dense vector
149  , bool TF // Transpose flag
150  , typename Abs // Type of the abs operation
151  , typename Power // Type of the power operation
152  , typename Root > // Type of the root operation
153 inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, FalseType )
154 {
155  using CT = CompositeType_t<VT>;
156  using ET = ElementType_t<VT>;
157  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
158 
159  if( (~dv).size() == 0UL ) return RT();
160 
161  CT tmp( ~dv );
162 
163  const size_t N( tmp.size() );
164 
165  ET norm( power( abs( tmp[0UL] ) ) );
166  size_t i( 1UL );
167 
168  for( ; (i+4UL) <= N; i+=4UL ) {
169  norm += power( abs( tmp[i ] ) ) + power( abs( tmp[i+1UL] ) ) +
170  power( abs( tmp[i+2UL] ) ) + power( abs( tmp[i+3UL] ) );
171  }
172  for( ; (i+2UL) <= N; i+=2UL ) {
173  norm += power( abs( tmp[i] ) ) + power( abs( tmp[i+1UL] ) );
174  }
175  for( ; i<N; ++i ) {
176  norm += power( abs( tmp[i] ) );
177  }
178 
179  return evaluate( root( norm ) );
180 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
200 template< typename VT // Type of the dense vector
201  , bool TF // Transpose flag
202  , typename Abs // Type of the abs operation
203  , typename Power // Type of the power operation
204  , typename Root > // Type of the root operation
205 inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, TrueType )
206 {
207  using CT = CompositeType_t<VT>;
208  using ET = ElementType_t<VT>;
209  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
210 
211  static constexpr size_t SIMDSIZE = SIMDTrait<ET>::size;
212 
213  if( (~dv).size() == 0UL ) return RT();
214 
215  CT tmp( ~dv );
216 
217  const size_t N( tmp.size() );
218 
219  constexpr bool remainder( !usePadding || !IsPadded_v< RemoveReference_t<VT> > );
220 
221  const size_t ipos( ( remainder )?( N & size_t(-SIMDSIZE) ):( N ) );
222  BLAZE_INTERNAL_ASSERT( !remainder || ( N - ( N % SIMDSIZE ) ) == ipos, "Invalid end calculation" );
223 
224  SIMDTrait_t<ET> xmm1, xmm2, xmm3, xmm4;
225  size_t i( 0UL );
226 
227  for( ; (i+SIMDSIZE*3UL) < ipos; i+=SIMDSIZE*4UL ) {
228  xmm1 += power( abs( tmp.load(i ) ) );
229  xmm2 += power( abs( tmp.load(i+SIMDSIZE ) ) );
230  xmm3 += power( abs( tmp.load(i+SIMDSIZE*2UL) ) );
231  xmm4 += power( abs( tmp.load(i+SIMDSIZE*3UL) ) );
232  }
233  for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
234  xmm1 += power( abs( tmp.load(i ) ) );
235  xmm2 += power( abs( tmp.load(i+SIMDSIZE) ) );
236  }
237  for( ; i<ipos; i+=SIMDSIZE ) {
238  xmm1 += power( abs( tmp.load(i) ) );
239  }
240 
241  ET norm( sum( xmm1 + xmm2 + xmm3 + xmm4 ) );
242 
243  for( ; remainder && i<N; ++i ) {
244  norm += power( abs( tmp[i] ) );
245  }
246 
247  return evaluate( root( norm ) );
248 }
250 //*************************************************************************************************
251 
252 
253 //*************************************************************************************************
274 template< typename VT // Type of the dense vector
275  , bool TF // Transpose flag
276  , typename Abs // Type of the abs operation
277  , typename Power // Type of the power operation
278  , typename Root > // Type of the root operation
279 inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root )
280 {
281  return norm_backend( ~dv, abs, power, root, Bool_t< DVecNormHelper<VT,Abs,Power>::value >() );
282 }
284 //*************************************************************************************************
285 
286 
287 //*************************************************************************************************
302 template< typename VT // Type of the dense vector
303  , bool TF > // Transpose flag
304 inline decltype(auto) norm( const DenseVector<VT,TF>& dv )
305 {
307 
308  return norm_backend( ~dv, SqrAbs(), Noop(), Sqrt() );
309 }
310 //*************************************************************************************************
311 
312 
313 //*************************************************************************************************
328 template< typename VT // Type of the dense vector
329  , bool TF > // Transpose flag
330 inline decltype(auto) sqrNorm( const DenseVector<VT,TF>& dv )
331 {
333 
334  return norm_backend( ~dv, SqrAbs(), Noop(), Noop() );
335 }
336 //*************************************************************************************************
337 
338 
339 //*************************************************************************************************
354 template< typename VT // Type of the dense vector
355  , bool TF > // Transpose flag
356 inline decltype(auto) l1Norm( const DenseVector<VT,TF>& dv )
357 {
359 
360  return norm_backend( ~dv, Abs(), Noop(), Noop() );
361 }
362 //*************************************************************************************************
363 
364 
365 //*************************************************************************************************
380 template< typename VT // Type of the dense vector
381  , bool TF > // Transpose flag
382 inline decltype(auto) l2Norm( const DenseVector<VT,TF>& dv )
383 {
385 
386  return norm_backend( ~dv, SqrAbs(), Noop(), Sqrt() );
387 }
388 //*************************************************************************************************
389 
390 
391 //*************************************************************************************************
406 template< typename VT // Type of the dense vector
407  , bool TF > // Transpose flag
408 inline decltype(auto) l3Norm( const DenseVector<VT,TF>& dv )
409 {
411 
412  return norm_backend( ~dv, Abs(), Pow3(), Cbrt() );
413 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
432 template< typename VT // Type of the dense vector
433  , bool TF > // Transpose flag
434 inline decltype(auto) l4Norm( const DenseVector<VT,TF>& dv )
435 {
437 
438  return norm_backend( ~dv, SqrAbs(), Pow2(), Qdrt() );
439 }
440 //*************************************************************************************************
441 
442 
443 //*************************************************************************************************
463 template< typename VT // Type of the dense vector
464  , bool TF // Transpose flag
465  , typename ST > // Type of the norm parameter
466 inline decltype(auto) lpNorm( const DenseVector<VT,TF>& dv, ST p )
467 {
469 
470  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
471 
472  using ScalarType = MultTrait_t< UnderlyingBuiltin_t<VT>, decltype( inv( p ) ) >;
473  using UnaryPow = Bind2nd<Pow,ScalarType>;
474  return norm_backend( ~dv, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
475 }
476 //*************************************************************************************************
477 
478 
479 //*************************************************************************************************
498 template< size_t P // Compile time norm parameter
499  , typename VT // Type of the dense vector
500  , bool TF > // Transpose flag
501 inline decltype(auto) lpNorm( const DenseVector<VT,TF>& dv )
502 {
503  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
504 
506  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
507 
508  return Norm()( ~dv );
509 }
510 //*************************************************************************************************
511 
512 
513 //*************************************************************************************************
528 template< typename VT // Type of the dense vector
529  , bool TF > // Transpose flag
530 inline decltype(auto) linfNorm( const DenseVector<VT,TF>& dv )
531 {
533 
534  return max( abs( ~dv ) );
535 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
554 template< typename VT // Type of the dense vector
555  , bool TF > // Transpose flag
556 inline decltype(auto) maxNorm( const DenseVector<VT,TF>& dv )
557 {
559 
560  return linfNorm( ~dv );
561 }
562 //*************************************************************************************************
563 
564 
565 //*************************************************************************************************
575 template< typename VT // Type of the dense vector
576  , bool TF > // Transpose flag
577 inline decltype(auto) sqrLength( const DenseVector<VT,TF>& dv )
578 {
579  return sqrNorm( ~dv );
580 }
581 //*************************************************************************************************
582 
583 
584 //*************************************************************************************************
594 template< typename VT // Type of the dense vector
595  , bool TF > // Transpose flag
596 inline decltype(auto) length( const DenseVector<VT,TF>& dv )
597 {
598  return norm( ~dv );
599 }
600 //*************************************************************************************************
601 
602 } // namespace blaze
603 
604 #endif
Header file for the Pow functor.
decltype(auto) sqrNorm(const DenseMatrix< MT, SO > &dm)
Computes the squared L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:495
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
Header file for the Sqrt functor.
Header file for auxiliary alias declarations.
Headerfile for the generic min algorithm.
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression,...
Definition: Assert.h:117
Generic wrapper for the cbrt() function.
Definition: Cbrt.h:81
Generic wrapper for the squared abs() function.
Definition: SqrAbs.h:85
Header file for the HasLoad type trait.
Header file for basic type definitions.
decltype(auto) l3Norm(const DenseMatrix< MT, SO > &dm)
Computes the L3 norm for the given dense matrix.
Definition: DMatNormExpr.h:573
Header file for the Pow2 functor.
decltype(auto) l2Norm(const DenseMatrix< MT, SO > &dm)
Computes the L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:547
decltype(auto) l4Norm(const DenseMatrix< MT, SO > &dm)
Computes the L4 norm for the given dense matrix.
Definition: DMatNormExpr.h:599
Header file for the isZero shim.
Header file for the Bind2nd functor.
Header file for the And_t alias template.
Header file for the DenseVector base class.
decltype(auto) lpNorm(const DenseMatrix< MT, SO > &dm, ST p)
Computes the Lp norm for the given dense matrix.
Definition: DMatNormExpr.h:631
Header file for the invert shim.
Generic wrapper for the qdrt() function.
Definition: Qdrt.h:82
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: IntegralConstant.h:132
System settings for performance optimizations.
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.In case of an invalid compile time expression, a compilation error is cr...
Definition: StaticAssert.h:123
const MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:912
Header file for the Cbrt functor.
Header file for the LpNorm functor.
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:677
Generic wrapper for the abs() function.
Definition: Abs.h:83
Generic wrapper for the sqrt() function.
Definition: Sqrt.h:83
Header file for the multiplication trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the L4Norm functor.
Header file for the If class template.
Compile time assertion.
Header file for the SqrAbs functor.
Header file for the UnderlyingBuiltin type trait.
decltype(auto) l1Norm(const DenseMatrix< MT, SO > &dm)
Computes the L1 norm for the given dense matrix.
Definition: DMatNormExpr.h:521
decltype(auto) min(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise minimum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1162
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2147
Header file for the HasSIMDAdd type trait.
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:423
Header file for all SIMD functionality.
Base class for N-dimensional dense vectors.The DenseVector class is a base class for all arbitrarily ...
Definition: DenseVector.h:76
Header file for the L2Norm functor.
Implementation of a type list.The TypeList class template represents a list of data types of arbitrar...
Definition: TypeList.h:119
Generic wrapper for the null function.
Definition: Noop.h:60
decltype(auto) max(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise maximum of the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1198
IntegralConstant< bool, B > Bool_t
Compile time integral constant wrapper for bool.The Bool_t alias template represents an integral wrap...
Definition: IntegralConstant.h:153
Header file for the Abs functor.
Header file for the IsPadded type trait.
decltype(auto) abs(const DenseMatrix< MT, SO > &dm)
Applies the abs() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1156
decltype(auto) maxNorm(const DenseMatrix< MT, SO > &dm)
Computes the maximum norm for the given dense matrix.
Definition: DMatNormExpr.h:721
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.The MultTrait_t alias declaration provid...
Definition: MultTrait.h:240
Header file for the evaluate shim.
Header file for the IsSIMDEnabled type trait.
Generic wrapper for the pow2() function.
Definition: Pow2.h:78
constexpr bool IsPadded_v
Auxiliary variable template for the IsPadded type trait.The IsPadded_v variable template provides a c...
Definition: IsPadded.h:134
decltype(auto) length(const DenseVector< VT, TF > &dv)
Calculation of the length (magnitude) of the dense vector .
Definition: DVecNormExpr.h:596
Header file for run time assertion macros.
Generic wrapper for the pow() function.
Definition: Pow.h:63
Indexing a type list.The TypeAt class can be used to access a type list at a specified position to qu...
Definition: TypeAt.h:75
Header file for the Unique class template.
#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
Generic wrapper for a binary operation with fixed 2nd argument.
Definition: Bind2nd.h:65
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:530
Header file for the HasMember type traits.
Header file for the Noop functor.
Header file for the RemoveReference type trait.
decltype(auto) norm(const DenseMatrix< MT, SO > &dm)
Computes the L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:469
decltype(auto) linfNorm(const DenseMatrix< MT, SO > &dm)
Computes the infinity norm for the given dense matrix.
Definition: DMatNormExpr.h:695
Generic wrapper for the pow3() function.
Definition: Pow3.h:78
Header file for the L3Norm functor.
Header file for the IntegralConstant class template.
decltype(auto) sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the square length (magnitude) of the dense vector .
Definition: DVecNormExpr.h:577
Header file for the L1Norm functor.
Header file for the Qdrt functor.
#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
Header file for the Pow3 functor.
Header file for the function trace functionality.