Blaze  3.6
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>
70 
71 
72 namespace blaze {
73 
74 //=================================================================================================
75 //
76 // GLOBAL FUNCTIONS
77 //
78 //=================================================================================================
79 
80 //*************************************************************************************************
101 template< typename VT // Type of the sparse vector
102  , bool TF // Transpose flag
103  , typename Abs // Type of the abs operation
104  , typename Power // Type of the power operation
105  , typename Root > // Type of the root operation
106 decltype(auto) norm_backend( const SparseVector<VT,TF>& sv, Abs abs, Power power, Root root )
107 {
108  using CT = CompositeType_t<VT>;
109  using ET = ElementType_t<VT>;
110  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
111 
112  if( (~sv).size() == 0UL ) return RT();
113 
114  CT tmp( ~sv );
115 
116  const auto end( tmp.end() );
117  auto element( tmp.begin() );
118 
119  if( element == end ) return RT();
120 
121  ET norm( power( abs( element->value() ) ) );
122  ++element;
123 
124  for( ; element!=end; ++element ) {
125  norm += power( abs( element->value() ) );
126  }
127 
128  return evaluate( root( norm ) );
129 }
131 //*************************************************************************************************
132 
133 
134 //*************************************************************************************************
149 template< typename VT // Type of the sparse vector
150  , bool TF > // Transpose flag
151 decltype(auto) norm( const SparseVector<VT,TF>& sv )
152 {
154 
155  return norm_backend( ~sv, SqrAbs(), Noop(), Sqrt() );
156 }
157 //*************************************************************************************************
158 
159 
160 //*************************************************************************************************
175 template< typename VT // Type of the sparse vector
176  , bool TF > // Transpose flag
177 decltype(auto) sqrNorm( const SparseVector<VT,TF>& sv )
178 {
180 
181  return norm_backend( ~sv, SqrAbs(), Noop(), Noop() );
182 }
183 //*************************************************************************************************
184 
185 
186 //*************************************************************************************************
201 template< typename VT // Type of the sparse vector
202  , bool TF > // Transpose flag
203 decltype(auto) l1Norm( const SparseVector<VT,TF>& sv )
204 {
206 
207  return norm_backend( ~sv, Abs(), Noop(), Noop() );
208 }
209 //*************************************************************************************************
210 
211 
212 //*************************************************************************************************
227 template< typename VT // Type of the sparse vector
228  , bool TF > // Transpose flag
229 decltype(auto) l2Norm( const SparseVector<VT,TF>& sv )
230 {
232 
233  return norm_backend( ~sv, SqrAbs(), Noop(), Sqrt() );
234 }
235 //*************************************************************************************************
236 
237 
238 //*************************************************************************************************
253 template< typename VT // Type of the sparse vector
254  , bool TF > // Transpose flag
255 decltype(auto) l3Norm( const SparseVector<VT,TF>& sv )
256 {
258 
259  return norm_backend( ~sv, Abs(), Pow3(), Cbrt() );
260 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
279 template< typename VT // Type of the sparse vector
280  , bool TF > // Transpose flag
281 decltype(auto) l4Norm( const SparseVector<VT,TF>& sv )
282 {
284 
285  return norm_backend( ~sv, SqrAbs(), Pow2(), Qdrt() );
286 }
287 //*************************************************************************************************
288 
289 
290 //*************************************************************************************************
310 template< typename VT // Type of the sparse vector
311  , bool TF // Transpose flag
312  , typename ST > // Type of the norm parameter
313 decltype(auto) lpNorm( const SparseVector<VT,TF>& sv, ST p )
314 {
316 
317  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
318 
319  using ScalarType = MultTrait_t< UnderlyingBuiltin_t<VT>, decltype( inv( p ) ) >;
320  using UnaryPow = Bind2nd<Pow,ScalarType>;
321  return norm_backend( ~sv, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
322 }
323 //*************************************************************************************************
324 
325 
326 //*************************************************************************************************
345 template< size_t P // Compile time norm parameter
346  , typename VT // Type of the sparse vector
347  , bool TF > // Transpose flag
348 inline decltype(auto) lpNorm( const SparseVector<VT,TF>& sv )
349 {
350  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
351 
353  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
354 
355  return Norm()( ~sv );
356 }
357 //*************************************************************************************************
358 
359 
360 //*************************************************************************************************
375 template< typename VT // Type of the sparse vector
376  , bool TF > // Transpose flag
377 decltype(auto) linfNorm( const SparseVector<VT,TF>& sv )
378 {
380 
381  return max( abs( ~sv ) );
382 }
383 //*************************************************************************************************
384 
385 
386 //*************************************************************************************************
401 template< typename VT // Type of the sparse vector
402  , bool TF > // Transpose flag
403 decltype(auto) maxNorm( const SparseVector<VT,TF>& sv )
404 {
406 
407  return linfNorm( ~sv );
408 }
409 //*************************************************************************************************
410 
411 
412 //*************************************************************************************************
422 template< typename VT // Type of the sparse vector
423  , bool TF > // Transpose flag
424 inline decltype(auto) sqrLength( const SparseVector<VT,TF>& sv )
425 {
426  return sqrNorm( ~sv );
427 }
428 //*************************************************************************************************
429 
430 
431 //*************************************************************************************************
441 template< typename VT // Type of the sparse vector
442  , bool TF > // Transpose flag
443 inline decltype(auto) length( const SparseVector<VT,TF>& sv )
444 {
445  return norm( ~sv );
446 }
447 //*************************************************************************************************
448 
449 } // namespace blaze
450 
451 #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
Header file for the Sqrt functor.
Header file for auxiliary alias declarations.
#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 SparseVector base class.
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.
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
#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.
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) inv(const DenseMatrix< MT, SO > &dm)
Calculation of the inverse of the given dense matrix.
Definition: DMatInvExpr.h:423
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
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:438
Header file for the Abs functor.
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.
Generic wrapper for the pow2() function.
Definition: Pow2.h:78
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 Noop functor.
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.
Base class for sparse vectors.The SparseVector class is a base class for all arbitrarily sized (N-dim...
Definition: Forward.h:146
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.
Header file for the Pow3 functor.
Header file for the function trace functionality.