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>
65 #include <blaze/util/Assert.h>
68 #include <blaze/util/TypeList.h>
69 
70 
71 namespace blaze {
72 
73 //=================================================================================================
74 //
75 // GLOBAL FUNCTIONS
76 //
77 //=================================================================================================
78 
79 //*************************************************************************************************
100 template< typename VT // Type of the sparse vector
101  , bool TF // Transpose flag
102  , typename Abs // Type of the abs operation
103  , typename Power // Type of the power operation
104  , typename Root > // Type of the root operation
105 decltype(auto) norm_backend( const SparseVector<VT,TF>& sv, Abs abs, Power power, Root root )
106 {
107  using CT = CompositeType_t<VT>;
108  using ET = ElementType_t<VT>;
109  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
110 
111  if( (~sv).size() == 0UL ) return RT();
112 
113  CT tmp( ~sv );
114 
115  const auto end( tmp.end() );
116  auto element( tmp.begin() );
117 
118  if( element == end ) return RT();
119 
120  ET norm( power( abs( element->value() ) ) );
121  ++element;
122 
123  for( ; element!=end; ++element ) {
124  norm += power( abs( element->value() ) );
125  }
126 
127  return evaluate( root( norm ) );
128 }
130 //*************************************************************************************************
131 
132 
133 //*************************************************************************************************
148 template< typename VT // Type of the sparse vector
149  , bool TF > // Transpose flag
150 decltype(auto) norm( const SparseVector<VT,TF>& sv )
151 {
153 
154  return norm_backend( ~sv, SqrAbs(), Noop(), Sqrt() );
155 }
156 //*************************************************************************************************
157 
158 
159 //*************************************************************************************************
174 template< typename VT // Type of the sparse vector
175  , bool TF > // Transpose flag
176 decltype(auto) sqrNorm( const SparseVector<VT,TF>& sv )
177 {
179 
180  return norm_backend( ~sv, SqrAbs(), Noop(), Noop() );
181 }
182 //*************************************************************************************************
183 
184 
185 //*************************************************************************************************
200 template< typename VT // Type of the sparse vector
201  , bool TF > // Transpose flag
202 decltype(auto) l1Norm( const SparseVector<VT,TF>& sv )
203 {
205 
206  return norm_backend( ~sv, Abs(), Noop(), Noop() );
207 }
208 //*************************************************************************************************
209 
210 
211 //*************************************************************************************************
226 template< typename VT // Type of the sparse vector
227  , bool TF > // Transpose flag
228 decltype(auto) l2Norm( const SparseVector<VT,TF>& sv )
229 {
231 
232  return norm_backend( ~sv, SqrAbs(), Noop(), Sqrt() );
233 }
234 //*************************************************************************************************
235 
236 
237 //*************************************************************************************************
252 template< typename VT // Type of the sparse vector
253  , bool TF > // Transpose flag
254 decltype(auto) l3Norm( const SparseVector<VT,TF>& sv )
255 {
257 
258  return norm_backend( ~sv, Abs(), Pow3(), Cbrt() );
259 }
260 //*************************************************************************************************
261 
262 
263 //*************************************************************************************************
278 template< typename VT // Type of the sparse vector
279  , bool TF > // Transpose flag
280 decltype(auto) l4Norm( const SparseVector<VT,TF>& sv )
281 {
283 
284  return norm_backend( ~sv, SqrAbs(), Pow2(), Qdrt() );
285 }
286 //*************************************************************************************************
287 
288 
289 //*************************************************************************************************
309 template< typename VT // Type of the sparse vector
310  , bool TF // Transpose flag
311  , typename ST > // Type of the norm parameter
312 decltype(auto) lpNorm( const SparseVector<VT,TF>& sv, ST p )
313 {
315 
316  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
317 
318  using ScalarType = MultTrait_t< UnderlyingBuiltin_t<VT>, decltype( inv( p ) ) >;
319  return norm_backend( ~sv, Abs(), UnaryPow<ScalarType>( p ), UnaryPow<ScalarType>( inv( p ) ) );
320 }
321 //*************************************************************************************************
322 
323 
324 //*************************************************************************************************
343 template< size_t P // Compile time norm parameter
344  , typename VT // Type of the sparse vector
345  , bool TF > // Transpose flag
346 inline decltype(auto) lpNorm( const SparseVector<VT,TF>& sv )
347 {
348  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
349 
351  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
352 
353  return Norm()( ~sv );
354 }
355 //*************************************************************************************************
356 
357 
358 //*************************************************************************************************
373 template< typename VT // Type of the sparse vector
374  , bool TF > // Transpose flag
375 decltype(auto) maxNorm( const SparseVector<VT,TF>& sv )
376 {
378 
379  return max( abs( ~sv ) );
380 }
381 //*************************************************************************************************
382 
383 } // namespace blaze
384 
385 #endif
decltype(auto) sqrNorm(const DenseMatrix< MT, SO > &dm)
Computes the squared L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:496
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, 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:80
Generic wrapper for the squared abs() function.
Definition: SqrAbs.h:84
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:574
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:548
decltype(auto) l4Norm(const DenseMatrix< MT, SO > &dm)
Computes the L4 norm for the given dense matrix.
Definition: DMatNormExpr.h:600
Header file for the isZero shim.
Generic wrapper for the pow() function with fixed exponent.
Definition: Forward.h:129
decltype(auto) lpNorm(const DenseMatrix< MT, SO > &dm, ST p)
Computes the Lp norm for the given dense matrix.
Definition: DMatNormExpr.h:632
Header file for the invert shim.
Generic wrapper for the qdrt() function.
Definition: Qdrt.h:81
#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:673
Generic wrapper for the abs() function.
Definition: Abs.h:82
Generic wrapper for the sqrt() function.
Definition: Sqrt.h:82
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:522
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:1147
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:59
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:1179
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:1152
decltype(auto) maxNorm(const DenseMatrix< MT, SO > &dm)
Computes the maximum norm for the given dense matrix.
Definition: DMatNormExpr.h:695
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:77
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
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:470
Generic wrapper for the pow3() function.
Definition: Pow3.h:77
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:138
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.