Blaze 3.9
SVecNormExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SVECNORMEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SVECNORMEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <utility>
44#include <blaze/math/Aliases.h>
66#include <blaze/util/Assert.h>
69#include <blaze/util/TypeList.h>
71
72
73namespace blaze {
74
75//=================================================================================================
76//
77// GLOBAL FUNCTIONS
78//
79//=================================================================================================
80
81//*************************************************************************************************
102template< typename VT // Type of the sparse vector
103 , bool TF // Transpose flag
104 , typename Abs // Type of the abs operation
105 , typename Power // Type of the power operation
106 , typename Root > // Type of the root operation
107decltype(auto) norm_backend( const SparseVector<VT,TF>& sv, Abs abs, Power power, Root root )
108{
109 using CT = CompositeType_t<VT>;
110 using ET = ElementType_t<VT>;
111 using PT = RemoveCVRef_t< decltype( power( abs( std::declval<ET>() ) ) ) >;
112 using RT = RemoveCVRef_t< decltype( evaluate( root( std::declval<PT>() ) ) ) >;
113
114 if( (*sv).size() == 0UL ) return RT{};
115
116 CT tmp( *sv );
117
118 const auto end( tmp.end() );
119 auto element( tmp.begin() );
120
121 if( element == end ) return RT{};
122
123 PT norm( power( abs( element->value() ) ) );
124 ++element;
125
126 for( ; element!=end; ++element ) {
127 norm += power( abs( element->value() ) );
128 }
129
130 return evaluate( root( norm ) );
131}
133//*************************************************************************************************
134
135
136//*************************************************************************************************
151template< typename VT // Type of the sparse vector
152 , bool TF > // Transpose flag
153decltype(auto) norm( const SparseVector<VT,TF>& sv )
154{
156
157 return norm_backend( *sv, SqrAbs(), Noop(), Sqrt() );
158}
159//*************************************************************************************************
160
161
162//*************************************************************************************************
177template< typename VT // Type of the sparse vector
178 , bool TF > // Transpose flag
179decltype(auto) sqrNorm( const SparseVector<VT,TF>& sv )
180{
182
183 return norm_backend( *sv, SqrAbs(), Noop(), Noop() );
184}
185//*************************************************************************************************
186
187
188//*************************************************************************************************
203template< typename VT // Type of the sparse vector
204 , bool TF > // Transpose flag
205decltype(auto) l1Norm( const SparseVector<VT,TF>& sv )
206{
208
209 return norm_backend( *sv, Abs(), Noop(), Noop() );
210}
211//*************************************************************************************************
212
213
214//*************************************************************************************************
229template< typename VT // Type of the sparse vector
230 , bool TF > // Transpose flag
231decltype(auto) l2Norm( const SparseVector<VT,TF>& sv )
232{
234
235 return norm_backend( *sv, SqrAbs(), Noop(), Sqrt() );
236}
237//*************************************************************************************************
238
239
240//*************************************************************************************************
255template< typename VT // Type of the sparse vector
256 , bool TF > // Transpose flag
257decltype(auto) l3Norm( const SparseVector<VT,TF>& sv )
258{
260
261 return norm_backend( *sv, Abs(), Pow3(), Cbrt() );
262}
263//*************************************************************************************************
264
265
266//*************************************************************************************************
281template< typename VT // Type of the sparse vector
282 , bool TF > // Transpose flag
283decltype(auto) l4Norm( const SparseVector<VT,TF>& sv )
284{
286
287 return norm_backend( *sv, SqrAbs(), Pow2(), Qdrt() );
288}
289//*************************************************************************************************
290
291
292//*************************************************************************************************
312template< typename VT // Type of the sparse vector
313 , bool TF // Transpose flag
314 , typename ST > // Type of the norm parameter
315decltype(auto) lpNorm( const SparseVector<VT,TF>& sv, ST p )
316{
318
319 BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
320
321 using ScalarType = MultTrait_t< UnderlyingBuiltin_t<VT>, decltype( inv( p ) ) >;
322 using UnaryPow = Bind2nd<Pow,ScalarType>;
323 return norm_backend( *sv, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
324}
325//*************************************************************************************************
326
327
328//*************************************************************************************************
347template< size_t P // Compile time norm parameter
348 , typename VT // Type of the sparse vector
349 , bool TF > // Transpose flag
350inline decltype(auto) lpNorm( const SparseVector<VT,TF>& sv )
351{
352 BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
353
355 using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
356
357 return Norm()( *sv );
358}
359//*************************************************************************************************
360
361
362//*************************************************************************************************
377template< typename VT // Type of the sparse vector
378 , bool TF > // Transpose flag
379decltype(auto) linfNorm( const SparseVector<VT,TF>& sv )
380{
382
383 return max( abs( *sv ) );
384}
385//*************************************************************************************************
386
387
388//*************************************************************************************************
403template< typename VT // Type of the sparse vector
404 , bool TF > // Transpose flag
405decltype(auto) maxNorm( const SparseVector<VT,TF>& sv )
406{
408
409 return linfNorm( *sv );
410}
411//*************************************************************************************************
412
413
414//*************************************************************************************************
429template< typename VT // Type of the sparse vector
430 , bool TF > // Transpose flag
431decltype(auto) minNorm( const SparseVector<VT,TF>& sv )
432{
434
435 return min( abs( *sv ) );
436}
437//*************************************************************************************************
438
439
440//*************************************************************************************************
450template< typename VT // Type of the sparse vector
451 , bool TF > // Transpose flag
452inline decltype(auto) sqrLength( const SparseVector<VT,TF>& sv )
453{
454 return sqrNorm( *sv );
455}
456//*************************************************************************************************
457
458
459//*************************************************************************************************
469template< typename VT // Type of the sparse vector
470 , bool TF > // Transpose flag
471inline decltype(auto) length( const SparseVector<VT,TF>& sv )
472{
473 return norm( *sv );
474}
475//*************************************************************************************************
476
477} // namespace blaze
478
479#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 invert shim.
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 RemoveCVRef type trait.
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 sparse vectors.
Definition: SparseVector.h:72
Header file for the SparseVector 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
typename MultTrait< T1, T2 >::Type MultTrait_t
Auxiliary alias declaration for the MultTrait class template.
Definition: MultTrait.h:165
MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:1282
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:584
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.
Definition: Assert.h:117
decltype(auto) maxNorm(const SparseVector< VT, TF > &sv)
Computes the maximum norm for the given sparse vector.
Definition: SVecNormExpr.h:405
decltype(auto) l4Norm(const SparseVector< VT, TF > &sv)
Computes the L4 norm for the given sparse vector.
Definition: SVecNormExpr.h:283
decltype(auto) norm(const SparseVector< VT, TF > &sv)
Computes the L2 norm for the given sparse vector.
Definition: SVecNormExpr.h:153
decltype(auto) sqrLength(const SparseVector< VT, TF > &sv)
Calculation of the square length (magnitude) of the sparse vector .
Definition: SVecNormExpr.h:452
decltype(auto) linfNorm(const SparseVector< VT, TF > &sv)
Computes the infinity norm for the given sparse vector.
Definition: SVecNormExpr.h:379
decltype(auto) length(const SparseVector< VT, TF > &sv)
Calculation of the length (magnitude) of the sparse vector .
Definition: SVecNormExpr.h:471
decltype(auto) minNorm(const SparseVector< VT, TF > &sv)
Computes the minimum norm for the given sparse vector.
Definition: SVecNormExpr.h:431
decltype(auto) l3Norm(const SparseVector< VT, TF > &sv)
Computes the L3 norm for the given sparse vector.
Definition: SVecNormExpr.h:257
decltype(auto) l2Norm(const SparseVector< VT, TF > &sv)
Computes the L2 norm for the given sparse vector.
Definition: SVecNormExpr.h:231
decltype(auto) lpNorm(const SparseVector< VT, TF > &sv)
Computes the Lp norm for the given sparse vector.
Definition: SVecNormExpr.h:350
decltype(auto) sqrNorm(const SparseVector< VT, TF > &sv)
Computes the squared L2 norm for the given sparse vector.
Definition: SVecNormExpr.h:179
decltype(auto) l1Norm(const SparseVector< VT, TF > &sv)
Computes the L1 norm for the given sparse vector.
Definition: SVecNormExpr.h:205
#define BLAZE_STATIC_ASSERT_MSG(expr, msg)
Compile time assertion macro.
Definition: StaticAssert.h:123
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
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