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>
63 #include <blaze/math/SIMD.h>
70 #include <blaze/util/Assert.h>
71 #include <blaze/util/FalseType.h>
73 #include <blaze/util/mpl/And.h>
74 #include <blaze/util/mpl/Bool.h>
75 #include <blaze/util/mpl/If.h>
77 #include <blaze/util/Template.h>
78 #include <blaze/util/TrueType.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 CT = RemoveReference_< CompositeType_<VT> >;
106  //**********************************************************************************************
107 
108  //**SIMD support detection**********************************************************************
110  BLAZE_CREATE_HAS_DATA_OR_FUNCTION_MEMBER_TYPE_TRAIT( HasSIMDEnabled, simdEnabled );
111 
114 
116  struct UseSIMDEnabledFlag {
117  enum : bool { value = Power::BLAZE_TEMPLATE simdEnabled< ElementType_<VT> >() };
118  };
119  //**********************************************************************************************
120 
121  //**********************************************************************************************
122  enum : bool { value = useOptimizedKernels &&
123  CT::simdEnabled &&
124  If_< And< HasSIMDEnabled<Abs>, HasSIMDEnabled<Power> >
125  , UseSIMDEnabledFlag
126  , And< HasLoad<Abs>, HasLoad<Power> > >::value &&
127  HasSIMDAdd< ElementType_<CT>, ElementType_<CT> >::value };
128  //**********************************************************************************************
129 };
131 //*************************************************************************************************
132 
133 
134 
135 
136 //=================================================================================================
137 //
138 // GLOBAL FUNCTIONS
139 //
140 //=================================================================================================
141 
142 //*************************************************************************************************
157 template< typename VT // Type of the dense vector
158  , bool TF // Transpose flag
159  , typename Abs // Type of the abs operation
160  , typename Power // Type of the power operation
161  , typename Root > // Type of the root operation
162 inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, FalseType )
163 {
164  using CT = CompositeType_<VT>;
165  using ET = ElementType_<VT>;
166  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
167 
168  if( (~dv).size() == 0UL ) return RT();
169 
170  CT tmp( ~dv );
171 
172  const size_t N( tmp.size() );
173 
174  ET norm( power( abs( tmp[0UL] ) ) );
175  size_t i( 1UL );
176 
177  for( ; (i+4UL) <= N; i+=4UL ) {
178  norm += power( abs( tmp[i ] ) ) + power( abs( tmp[i+1UL] ) ) +
179  power( abs( tmp[i+2UL] ) ) + power( abs( tmp[i+3UL] ) );
180  }
181  for( ; (i+2UL) <= N; i+=2UL ) {
182  norm += power( abs( tmp[i] ) ) + power( abs( tmp[i+1UL] ) );
183  }
184  for( ; i<N; ++i ) {
185  norm += power( abs( tmp[i] ) );
186  }
187 
188  return evaluate( root( norm ) );
189 }
191 //*************************************************************************************************
192 
193 
194 //*************************************************************************************************
209 template< typename VT // Type of the dense vector
210  , bool TF // Transpose flag
211  , typename Abs // Type of the abs operation
212  , typename Power // Type of the power operation
213  , typename Root > // Type of the root operation
214 inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, TrueType )
215 {
216  using CT = CompositeType_<VT>;
217  using ET = ElementType_<VT>;
218  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
219 
220  enum : size_t { SIMDSIZE = SIMDTrait<ET>::size };
221 
222  if( (~dv).size() == 0UL ) return RT();
223 
224  CT tmp( ~dv );
225 
226  const size_t N( tmp.size() );
227 
228  constexpr bool remainder( !usePadding || !IsPadded<VT>::value );
229 
230  const size_t ipos( ( remainder )?( N & size_t(-SIMDSIZE) ):( N ) );
231  BLAZE_INTERNAL_ASSERT( !remainder || ( N - ( N % SIMDSIZE ) ) == ipos, "Invalid end calculation" );
232 
233  SIMDTrait_<ET> xmm1, xmm2, xmm3, xmm4;
234  size_t i( 0UL );
235 
236  for( ; (i+SIMDSIZE*3UL) < ipos; i+=SIMDSIZE*4UL ) {
237  xmm1 += power( abs( tmp.load(i ) ) );
238  xmm2 += power( abs( tmp.load(i+SIMDSIZE ) ) );
239  xmm3 += power( abs( tmp.load(i+SIMDSIZE*2UL) ) );
240  xmm4 += power( abs( tmp.load(i+SIMDSIZE*3UL) ) );
241  }
242  for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
243  xmm1 += power( abs( tmp.load(i ) ) );
244  xmm2 += power( abs( tmp.load(i+SIMDSIZE) ) );
245  }
246  for( ; i<ipos; i+=SIMDSIZE ) {
247  xmm1 += power( abs( tmp.load(i) ) );
248  }
249 
250  ET norm( sum( xmm1 + xmm2 + xmm3 + xmm4 ) );
251 
252  for( ; remainder && i<N; ++i ) {
253  norm += power( abs( tmp[i] ) );
254  }
255 
256  return evaluate( root( norm ) );
257 }
259 //*************************************************************************************************
260 
261 
262 //*************************************************************************************************
283 template< typename VT // Type of the dense vector
284  , bool TF // Transpose flag
285  , typename Abs // Type of the abs operation
286  , typename Power // Type of the power operation
287  , typename Root > // Type of the root operation
288 decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root )
289 {
290  return norm_backend( ~dv, abs, power, root, Bool< DVecNormHelper<VT,Abs,Power>::value >() );
291 }
293 //*************************************************************************************************
294 
295 
296 //*************************************************************************************************
311 template< typename VT // Type of the dense vector
312  , bool TF > // Transpose flag
313 decltype(auto) norm( const DenseVector<VT,TF>& dv )
314 {
316 
317  return norm_backend( ~dv, Noop(), Pow2(), Sqrt() );
318 }
319 //*************************************************************************************************
320 
321 
322 //*************************************************************************************************
337 template< typename VT // Type of the dense vector
338  , bool TF > // Transpose flag
339 decltype(auto) sqrNorm( const DenseVector<VT,TF>& dv )
340 {
342 
343  return norm_backend( ~dv, Noop(), Pow2(), Noop() );
344 }
345 //*************************************************************************************************
346 
347 
348 //*************************************************************************************************
363 template< typename VT // Type of the dense vector
364  , bool TF > // Transpose flag
365 decltype(auto) l1Norm( const DenseVector<VT,TF>& dv )
366 {
368 
369  return norm_backend( ~dv, Abs(), Noop(), Noop() );
370 }
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
389 template< typename VT // Type of the dense vector
390  , bool TF > // Transpose flag
391 decltype(auto) l2Norm( const DenseVector<VT,TF>& dv )
392 {
394 
395  return norm_backend( ~dv, Noop(), Pow2(), Sqrt() );
396 }
397 //*************************************************************************************************
398 
399 
400 //*************************************************************************************************
415 template< typename VT // Type of the dense vector
416  , bool TF > // Transpose flag
417 decltype(auto) l3Norm( const DenseVector<VT,TF>& dv )
418 {
420 
421  return norm_backend( ~dv, Abs(), Pow3(), Cbrt() );
422 }
423 //*************************************************************************************************
424 
425 
426 //*************************************************************************************************
441 template< typename VT // Type of the dense vector
442  , bool TF > // Transpose flag
443 decltype(auto) l4Norm( const DenseVector<VT,TF>& dv )
444 {
446 
447  return norm_backend( ~dv, Noop(), Pow4(), Qdrt() );
448 }
449 //*************************************************************************************************
450 
451 
452 //*************************************************************************************************
472 template< typename VT // Type of the dense vector
473  , bool TF // Transpose flag
474  , typename ST > // Type of the norm parameter
475 decltype(auto) lpNorm( const DenseVector<VT,TF>& dv, ST p )
476 {
478 
479  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
480 
481  using ScalarType = MultTrait_< UnderlyingBuiltin_<VT>, decltype( inv( p ) ) >;
482  return norm_backend( ~dv, Abs(), UnaryPow<ScalarType>( p ), UnaryPow<ScalarType>( inv( p ) ) );
483 }
484 //*************************************************************************************************
485 
486 
487 //*************************************************************************************************
506 template< size_t P // Compile time norm parameter
507  , typename VT // Type of the dense vector
508  , bool TF > // Transpose flag
509 inline decltype(auto) lpNorm( const DenseVector<VT,TF>& dv )
510 {
511  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
512 
514  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
515 
516  return Norm()( ~dv );
517 }
518 //*************************************************************************************************
519 
520 
521 //*************************************************************************************************
536 template< typename VT // Type of the dense vector
537  , bool TF > // Transpose flag
538 decltype(auto) maxNorm( const DenseVector<VT,TF>& dv )
539 {
541 
542  return max( abs( ~dv ) );
543 }
544 //*************************************************************************************************
545 
546 } // namespace blaze
547 
548 #endif
decltype(auto) sqrNorm(const DenseMatrix< MT, SO > &dm)
Computes the squared L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:504
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: FalseType.h:61
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, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
Generic wrapper for the cbrt() function.
Definition: Cbrt.h:62
Header file for the Pow4 functor.
Header file for basic type definitions.
#define BLAZE_CREATE_HAS_DATA_OR_FUNCTION_MEMBER_TYPE_TRAIT(TYPE_TRAIT_NAME, MEMBER_NAME)
Macro for the creation of a type trait for compile time checks for member data and functions...
Definition: HasMember.h:98
decltype(auto) l3Norm(const DenseMatrix< MT, SO > &dm)
Computes the L3 norm for the given dense matrix.
Definition: DMatNormExpr.h:582
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:556
Header file for the FalseType type/value trait base class.
decltype(auto) l4Norm(const DenseMatrix< MT, SO > &dm)
Computes the L4 norm for the given dense matrix.
Definition: DMatNormExpr.h:608
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 And class template.
const ElementType_< MT > min(const DenseMatrix< MT, SO > &dm)
Returns the smallest element of the dense matrix.
Definition: DenseMatrix.h:1903
Header file for the DenseVector base class.
Generic wrapper for the pow() function with fixed exponent.
Definition: Forward.h:119
decltype(auto) lpNorm(const DenseMatrix< MT, SO > &dm, ST p)
Computes the Lp norm for the given dense matrix.
Definition: DMatNormExpr.h:640
Header file for the invert shim.
typename MultTrait< T1, T2 >::Type MultTrait_
Auxiliary alias declaration for the MultTrait class template.The MultTrait_ alias declaration provide...
Definition: MultTrait.h:291
Generic wrapper for the qdrt() function.
Definition: Qdrt.h:61
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
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:888
const ElementType_< MT > max(const DenseMatrix< MT, SO > &dm)
Returns the largest element of the dense matrix.
Definition: DenseMatrix.h:1950
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:670
Generic wrapper for the sqrt() function.
Definition: Sqrt.h:62
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 nested template disabiguation.
Header file for the If class template.
Compile time assertion.
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:530
Header file for the HasSIMDAdd type trait.
Header file for the isZero shim.
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:59
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:1176
decltype(auto) maxNorm(const DenseMatrix< MT, SO > &dm)
Computes the maximum norm for the given dense matrix.
Definition: DMatNormExpr.h:703
Generic wrapper for the pow4() function.
Definition: Pow4.h:61
Header file for the evaluate shim.
Generic wrapper for the pow2() function.
Definition: Pow2.h:61
BLAZE_ALWAYS_INLINE ValueType_< T > sum(const SIMDi8< T > &a) noexcept
Returns the sum of all elements in the 8-bit integral SIMD vector.
Definition: Reduction.h:65
Header file for run time assertion macros.
Indexing a type list.The TypeAt class can be used to access a type list at a specified position to qu...
Definition: TypeAt.h:76
Header file for the Unique class template.
Header file for the UnaryPow functor.
#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 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:478
Generic wrapper for the pow3() function.
Definition: Pow3.h:61
Header file for the L3Norm functor.
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, 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 Bool class template.
Header file for the Pow3 functor.
Header file for the TrueType type/value trait base class.
Header file for the function trace functionality.