Blaze 3.9
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>
65#include <blaze/math/SIMD.h>
74#include <blaze/util/Assert.h>
77#include <blaze/util/mpl/And.h>
78#include <blaze/util/mpl/If.h>
80#include <blaze/util/TypeList.h>
81#include <blaze/util/Types.h>
85
86
87namespace blaze {
88
89//=================================================================================================
90//
91// CLASS DEFINITION
92//
93//=================================================================================================
94
95//*************************************************************************************************
100template< typename VT // Type of the dense vector
101 , typename Abs // Type of the abs operation
102 , typename Power > // Type of the power operation
103struct DVecNormHelper
104{
105 //**Type definitions****************************************************************************
107 using ET = ElementType_t<VT>;
108
110 using CT = RemoveReference_t< CompositeType_t<VT> >;
111 //**********************************************************************************************
112
113 //**********************************************************************************************
114 static constexpr bool value =
115 ( useOptimizedKernels &&
116 CT::simdEnabled &&
117 If_t< HasSIMDEnabled_v<Abs> && HasSIMDEnabled_v<Power>
118 , And_t< GetSIMDEnabled<Abs,ET>, GetSIMDEnabled<Power,ET> >
119 , And_t< HasLoad<Abs>, HasLoad<Power> > >::value &&
120 HasSIMDAdd_v< ElementType_t<CT>, ElementType_t<CT> > );
121 //**********************************************************************************************
122};
124//*************************************************************************************************
125
126
127
128
129//=================================================================================================
130//
131// GLOBAL FUNCTIONS
132//
133//=================================================================================================
134
135//*************************************************************************************************
150template< typename VT // Type of the dense vector
151 , bool TF // Transpose flag
152 , typename Abs // Type of the abs operation
153 , typename Power // Type of the power operation
154 , typename Root > // Type of the root operation
155inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, FalseType )
156{
157 using CT = CompositeType_t<VT>;
158 using ET = ElementType_t<VT>;
159 using PT = RemoveCVRef_t< decltype( power( abs( std::declval<ET>() ) ) ) >;
160 using RT = RemoveCVRef_t< decltype( evaluate( root( std::declval<PT>() ) ) ) >;
161
162 if( (*dv).size() == 0UL ) return RT{};
163
164 CT tmp( *dv );
165
166 const size_t N( tmp.size() );
167
168 PT norm( power( abs( tmp[0UL] ) ) );
169 size_t i( 1UL );
170
171 for( ; (i+4UL) <= N; i+=4UL ) {
172 norm += power( abs( tmp[i ] ) ) + power( abs( tmp[i+1UL] ) ) +
173 power( abs( tmp[i+2UL] ) ) + power( abs( tmp[i+3UL] ) );
174 }
175 for( ; (i+2UL) <= N; i+=2UL ) {
176 norm += power( abs( tmp[i] ) ) + power( abs( tmp[i+1UL] ) );
177 }
178 for( ; i<N; ++i ) {
179 norm += power( abs( tmp[i] ) );
180 }
181
182 return evaluate( root( norm ) );
183}
185//*************************************************************************************************
186
187
188//*************************************************************************************************
203template< typename VT // Type of the dense vector
204 , bool TF // Transpose flag
205 , typename Abs // Type of the abs operation
206 , typename Power // Type of the power operation
207 , typename Root > // Type of the root operation
208inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root, TrueType )
209{
210 using CT = CompositeType_t<VT>;
211 using ET = ElementType_t<VT>;
212 using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
213
214 static constexpr size_t SIMDSIZE = SIMDTrait<ET>::size;
215
216 if( (*dv).size() == 0UL ) return RT{};
217
218 CT tmp( *dv );
219
220 const size_t N( tmp.size() );
221
222 constexpr bool remainder( !IsPadded_v< RemoveReference_t<VT> > );
223
224 const size_t ipos( remainder ? prevMultiple( N, SIMDSIZE ) : N );
225 BLAZE_INTERNAL_ASSERT( ipos <= N, "Invalid end calculation" );
226
227 size_t i( 0UL );
228 ET norm{};
229
230 if( SIMDSIZE*3UL < ipos )
231 {
232 SIMDTrait_t<ET> xmm1{}, xmm2{}, xmm3{}, xmm4{};
233
234 for( ; (i+SIMDSIZE*3UL) < ipos; i+=SIMDSIZE*4UL ) {
235 xmm1 += power( abs( tmp.load(i ) ) );
236 xmm2 += power( abs( tmp.load(i+SIMDSIZE ) ) );
237 xmm3 += power( abs( tmp.load(i+SIMDSIZE*2UL) ) );
238 xmm4 += power( abs( tmp.load(i+SIMDSIZE*3UL) ) );
239 }
240 for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
241 xmm1 += power( abs( tmp.load(i ) ) );
242 xmm2 += power( abs( tmp.load(i+SIMDSIZE) ) );
243 }
244 for( ; i<ipos; i+=SIMDSIZE ) {
245 xmm1 += power( abs( tmp.load(i) ) );
246 }
247
248 norm = sum( xmm1 + xmm2 + xmm3 + xmm4 );
249 }
250 else if( SIMDSIZE < ipos )
251 {
252 SIMDTrait_t<ET> xmm1{}, xmm2{};
253
254 for( ; (i+SIMDSIZE) < ipos; i+=SIMDSIZE*2UL ) {
255 xmm1 += power( abs( tmp.load(i ) ) );
256 xmm2 += power( abs( tmp.load(i+SIMDSIZE) ) );
257 }
258 for( ; i<ipos; i+=SIMDSIZE ) {
259 xmm1 += power( abs( tmp.load(i) ) );
260 }
261
262 norm = sum( xmm1 + xmm2 );
263 }
264 else
265 {
266 SIMDTrait_t<ET> xmm1{};
267
268 for( ; i<ipos; i+=SIMDSIZE ) {
269 xmm1 += power( abs( tmp.load(i) ) );
270 }
271
272 norm = sum( xmm1 );
273 }
274
275 for( ; remainder && i<N; ++i ) {
276 norm += power( abs( tmp[i] ) );
277 }
278
279 return evaluate( root( norm ) );
280}
282//*************************************************************************************************
283
284
285//*************************************************************************************************
306template< typename VT // Type of the dense vector
307 , bool TF // Transpose flag
308 , typename Abs // Type of the abs operation
309 , typename Power // Type of the power operation
310 , typename Root > // Type of the root operation
311inline decltype(auto) norm_backend( const DenseVector<VT,TF>& dv, Abs abs, Power power, Root root )
312{
313 return norm_backend( *dv, abs, power, root, Bool_t< DVecNormHelper<VT,Abs,Power>::value >() );
314}
316//*************************************************************************************************
317
318
319//*************************************************************************************************
334template< typename VT // Type of the dense vector
335 , bool TF > // Transpose flag
336inline decltype(auto) norm( const DenseVector<VT,TF>& dv )
337{
339
340 return norm_backend( *dv, SqrAbs(), Noop(), Sqrt() );
341}
342//*************************************************************************************************
343
344
345//*************************************************************************************************
360template< typename VT // Type of the dense vector
361 , bool TF > // Transpose flag
362inline decltype(auto) sqrNorm( const DenseVector<VT,TF>& dv )
363{
365
366 return norm_backend( *dv, SqrAbs(), Noop(), Noop() );
367}
368//*************************************************************************************************
369
370
371//*************************************************************************************************
386template< typename VT // Type of the dense vector
387 , bool TF > // Transpose flag
388inline decltype(auto) l1Norm( const DenseVector<VT,TF>& dv )
389{
391
392 return norm_backend( *dv, Abs(), Noop(), Noop() );
393}
394//*************************************************************************************************
395
396
397//*************************************************************************************************
412template< typename VT // Type of the dense vector
413 , bool TF > // Transpose flag
414inline decltype(auto) l2Norm( const DenseVector<VT,TF>& dv )
415{
417
418 return norm_backend( *dv, SqrAbs(), Noop(), Sqrt() );
419}
420//*************************************************************************************************
421
422
423//*************************************************************************************************
438template< typename VT // Type of the dense vector
439 , bool TF > // Transpose flag
440inline decltype(auto) l3Norm( const DenseVector<VT,TF>& dv )
441{
443
444 return norm_backend( *dv, Abs(), Pow3(), Cbrt() );
445}
446//*************************************************************************************************
447
448
449//*************************************************************************************************
464template< typename VT // Type of the dense vector
465 , bool TF > // Transpose flag
466inline decltype(auto) l4Norm( const DenseVector<VT,TF>& dv )
467{
469
470 return norm_backend( *dv, SqrAbs(), Pow2(), Qdrt() );
471}
472//*************************************************************************************************
473
474
475//*************************************************************************************************
495template< typename VT // Type of the dense vector
496 , bool TF // Transpose flag
497 , typename ST > // Type of the norm parameter
498inline decltype(auto) lpNorm( const DenseVector<VT,TF>& dv, ST p )
499{
501
502 BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
503
504 using ScalarType = MultTrait_t< UnderlyingBuiltin_t<VT>, decltype( inv( p ) ) >;
505 using UnaryPow = Bind2nd<Pow,ScalarType>;
506 return norm_backend( *dv, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
507}
508//*************************************************************************************************
509
510
511//*************************************************************************************************
530template< size_t P // Compile time norm parameter
531 , typename VT // Type of the dense vector
532 , bool TF > // Transpose flag
533inline decltype(auto) lpNorm( const DenseVector<VT,TF>& dv )
534{
535 BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
536
538 using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
539
540 return Norm()( *dv );
541}
542//*************************************************************************************************
543
544
545//*************************************************************************************************
560template< typename VT // Type of the dense vector
561 , bool TF > // Transpose flag
562inline decltype(auto) linfNorm( const DenseVector<VT,TF>& dv )
563{
565
566 return max( abs( *dv ) );
567}
568//*************************************************************************************************
569
570
571//*************************************************************************************************
586template< typename VT // Type of the dense vector
587 , bool TF > // Transpose flag
588inline decltype(auto) maxNorm( const DenseVector<VT,TF>& dv )
589{
591
592 return linfNorm( *dv );
593}
594//*************************************************************************************************
595
596
597//*************************************************************************************************
612template< typename VT // Type of the dense vector
613 , bool TF > // Transpose flag
614inline decltype(auto) minNorm( const DenseVector<VT,TF>& dv )
615{
617
618 return min( abs( *dv ) );
619}
620//*************************************************************************************************
621
622
623//*************************************************************************************************
633template< typename VT // Type of the dense vector
634 , bool TF > // Transpose flag
635inline decltype(auto) sqrLength( const DenseVector<VT,TF>& dv )
636{
637 return sqrNorm( *dv );
638}
639//*************************************************************************************************
640
641
642//*************************************************************************************************
652template< typename VT // Type of the dense vector
653 , bool TF > // Transpose flag
654inline decltype(auto) length( const DenseVector<VT,TF>& dv )
655{
656 return norm( *dv );
657}
658//*************************************************************************************************
659
660} // namespace blaze
661
662#endif
Header file for auxiliary alias declarations.
Header file for run time assertion macros.
Header file for the Bind2nd functor.
Header file for the evaluate shim.
Header file for the function trace functionality.
Header file for the HasLoad type trait.
Header file for the HasMember type traits.
Header file for the HasSIMDAdd type trait.
Header file for the If class template.
Header file for the IntegralConstant class template.
Header file for the invert shim.
Header file for the IsPadded type trait.
Header file for the IsSIMDEnabled type trait.
Header file for the L1Norm functor.
Header file for the L2Norm functor.
Header file for the L3Norm functor.
Header file for the L4Norm functor.
Header file for the LpNorm functor.
Header file for the multiplication trait.
Header file for the Noop functor.
Header file for the prevMultiple shim.
Header file for the RemoveCVRef type trait.
Header file for the RemoveReference type trait.
Header file for all SIMD functionality.
Header file for the SqrAbs functor.
Compile time assertion.
Header file for the type list functionality.
Header file for the UnderlyingBuiltin type trait.
Base class for N-dimensional dense vectors.
Definition: DenseVector.h:77
Header file for the DenseVector base class.
Header file for the Abs functor.
Header file for the Cbrt functor.
Header file for the Pow2 functor.
Header file for the Pow3 functor.
Header file for the Pow functor.
Header file for the Qdrt functor.
Header file for the Sqrt functor.
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:1339
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:1375
decltype(auto) abs(const DenseMatrix< MT, SO > &dm)
Applies the abs() function to each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1296
decltype(auto) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:405
bool isZero(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a zero matrix.
Definition: DenseMatrix.h:1819
decltype(auto) sum(const DenseMatrix< MT, SO > &dm)
Reduces the given dense matrix by means of addition.
Definition: DMatReduceExpr.h:2156
decltype(auto) lpNorm(const DenseVector< VT, TF > &dv)
Computes the Lp norm for the given dense vector.
Definition: DVecNormExpr.h:533
decltype(auto) l4Norm(const DenseVector< VT, TF > &dv)
Computes the L4 norm for the given dense vector.
Definition: DVecNormExpr.h:466
decltype(auto) norm(const DenseVector< VT, TF > &dv)
Computes the L2 norm for the given dense vector.
Definition: DVecNormExpr.h:336
decltype(auto) minNorm(const DenseVector< VT, TF > &dv)
Computes the minimum norm for the given dense vector.
Definition: DVecNormExpr.h:614
decltype(auto) l1Norm(const DenseVector< VT, TF > &dv)
Computes the L1 norm for the given dense vector.
Definition: DVecNormExpr.h:388
decltype(auto) length(const DenseVector< VT, TF > &dv)
Calculation of the length (magnitude) of the dense vector .
Definition: DVecNormExpr.h:654
decltype(auto) sqrNorm(const DenseVector< VT, TF > &dv)
Computes the squared L2 norm for the given dense vector.
Definition: DVecNormExpr.h:362
decltype(auto) l3Norm(const DenseVector< VT, TF > &dv)
Computes the L3 norm for the given dense vector.
Definition: DVecNormExpr.h:440
decltype(auto) maxNorm(const DenseVector< VT, TF > &dv)
Computes the maximum norm for the given dense vector.
Definition: DVecNormExpr.h:588
decltype(auto) sqrLength(const DenseVector< VT, TF > &dv)
Calculation of the square length (magnitude) of the dense vector .
Definition: DVecNormExpr.h:635
decltype(auto) l2Norm(const DenseVector< VT, TF > &dv)
Computes the L2 norm for the given dense vector.
Definition: DVecNormExpr.h:414
decltype(auto) linfNorm(const DenseVector< VT, TF > &dv)
Computes the infinity norm for the given dense vector.
Definition: DVecNormExpr.h:562
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.
Definition: MultTrait.h:165
constexpr bool IsPadded_v
Auxiliary variable template for the IsPadded type trait.
Definition: IsPadded.h:134
BLAZE_ALWAYS_INLINE constexpr auto prevMultiple(T1 value, T2 factor) noexcept
Rounds down an integral value to the previous multiple of a given factor.
Definition: PrevMultiple.h:68
MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:1282
constexpr size_t size(const Matrix< MT, SO > &matrix) noexcept
Returns the total number of elements of the matrix.
Definition: Matrix.h:676
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.
Definition: Assert.h:117
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.
Definition: StaticAssert.h:123
BoolConstant< true > TrueType
Type traits base class.
Definition: IntegralConstant.h:132
BoolConstant< false > FalseType
Type/value traits base class.
Definition: IntegralConstant.h:121
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
IntegralConstant< bool, B > Bool_t
Compile time integral constant wrapper for bool.
Definition: IntegralConstant.h:153
Header file for the isZero shim.
Generic wrapper for the abs() function.
Definition: Abs.h:85
Generic wrapper for an operation with fixed 2nd argument.
Definition: Bind2nd.h:66
Generic wrapper for the cbrt() function.
Definition: Cbrt.h:83
Generic wrapper for the null function.
Definition: Noop.h:62
Generic wrapper for the pow2() function.
Definition: Pow2.h:80
Generic wrapper for the pow3() function.
Definition: Pow3.h:80
Generic wrapper for the pow() function.
Definition: Pow.h:65
Generic wrapper for the qdrt() function.
Definition: Qdrt.h:84
Generic wrapper for the squared abs() function.
Definition: SqrAbs.h:86
Generic wrapper for the sqrt() function.
Definition: Sqrt.h:85
Indexing a type list.
Definition: TypeAt.h:75
Implementation of a type list.
Definition: TypeList.h:120
System settings for performance optimizations.
Header file for basic type definitions.
Header file for the generic min algorithm.
Header file for the And_t alias template.