Blaze 3.9
SMatNormExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_SMATNORMEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_SMATNORMEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <utility>
44#include <blaze/math/Aliases.h>
69#include <blaze/util/Assert.h>
72#include <blaze/util/TypeList.h>
73#include <blaze/util/Types.h>
75
76
77namespace blaze {
78
79//=================================================================================================
80//
81// GLOBAL FUNCTIONS
82//
83//=================================================================================================
84
85//*************************************************************************************************
100template< typename MT // Type of the sparse matrix
101 , bool SO // Storage order
102 , typename Abs // Type of the abs operation
103 , typename Power // Type of the power operation
104 , typename Root > // Type of the root operation
105inline decltype(auto) norm_backend( const SparseMatrix<MT,SO>& sm, Abs abs, Power power, Root root )
106{
107 using CT = CompositeType_t<MT>;
108 using ET = ElementType_t<MT>;
109 using PT = RemoveCVRef_t< decltype( power( abs( std::declval<ET>() ) ) ) >;
110 using RT = RemoveCVRef_t< decltype( evaluate( root( std::declval<PT>() ) ) ) >;
111
112 if( (*sm).rows() == 0UL || (*sm).columns() == 0UL ) return RT{};
113
114 CT tmp( *sm );
115
116 const size_t N( IsRowMajorMatrix_v<MT> ? tmp.rows(): tmp.columns() );
117
118 PT norm{};
119
120 for( size_t i=0UL; i<N; ++i )
121 {
122 const auto end( tmp.end(i) );
123 for( auto element=tmp.begin(i); element!=end; ++element ) {
124 if( IsResizable_v<ET> && isDefault( norm ) )
125 norm = power( abs( element->value() ) );
126 else
127 norm += power( abs( element->value() ) );
128 }
129 }
130
131 return evaluate( root( norm ) );
132}
134//*************************************************************************************************
135
136
137//*************************************************************************************************
152template< typename MT // Type of the sparse matrix
153 , bool SO > // Storage order
154decltype(auto) norm( const SparseMatrix<MT,SO>& sm )
155{
157
158 return norm_backend( *sm, SqrAbs(), Noop(), Sqrt() );
159}
160//*************************************************************************************************
161
162
163//*************************************************************************************************
178template< typename MT // Type of the sparse matrix
179 , bool SO > // Storage order
180decltype(auto) sqrNorm( const SparseMatrix<MT,SO>& sm )
181{
183
184 return norm_backend( *sm, SqrAbs(), Noop(), Noop() );
185}
186//*************************************************************************************************
187
188
189//*************************************************************************************************
204template< typename MT // Type of the sparse matrix
205 , bool SO > // Storage order
206decltype(auto) l1Norm( const SparseMatrix<MT,SO>& sm )
207{
209
210 return norm_backend( *sm, Abs(), Noop(), Noop() );
211}
212//*************************************************************************************************
213
214
215//*************************************************************************************************
230template< typename MT // Type of the sparse matrix
231 , bool SO > // Storage order
232decltype(auto) l2Norm( const SparseMatrix<MT,SO>& sm )
233{
235
236 return norm_backend( *sm, SqrAbs(), Noop(), Sqrt() );
237}
238//*************************************************************************************************
239
240
241//*************************************************************************************************
256template< typename MT // Type of the sparse matrix
257 , bool SO > // Storage order
258decltype(auto) l3Norm( const SparseMatrix<MT,SO>& sm )
259{
261
262 return norm_backend( *sm, Abs(), Pow3(), Cbrt() );
263}
264//*************************************************************************************************
265
266
267//*************************************************************************************************
282template< typename MT // Type of the sparse matrix
283 , bool SO > // Storage order
284decltype(auto) l4Norm( const SparseMatrix<MT,SO>& sm )
285{
287
288 return norm_backend( *sm, SqrAbs(), Pow2(), Qdrt() );
289}
290//*************************************************************************************************
291
292
293//*************************************************************************************************
313template< typename MT // Type of the sparse matrix
314 , bool SO // Storage order
315 , typename ST > // Type of the norm parameter
316decltype(auto) lpNorm( const SparseMatrix<MT,SO>& sm, ST p )
317{
319
320 BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
321
322 using ScalarType = MultTrait_t< UnderlyingBuiltin_t<MT>, decltype( inv( p ) ) >;
323 using UnaryPow = Bind2nd<Pow,ScalarType>;
324 return norm_backend( *sm, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
325}
326//*************************************************************************************************
327
328
329//*************************************************************************************************
348template< size_t P // Compile time norm parameter
349 , typename MT // Type of the sparse matrix
350 , bool SO > // Storage order
351inline decltype(auto) lpNorm( const SparseMatrix<MT,SO>& sm )
352{
353 BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
354
356 using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
357
358 return Norm()( *sm );
359}
360//*************************************************************************************************
361
362
363//*************************************************************************************************
378template< typename MT // Type of the sparse matrix
379 , bool SO > // Storage order
380decltype(auto) linfNorm( const SparseMatrix<MT,SO>& sm )
381{
383
384 return max( abs( *sm ) );
385}
386//*************************************************************************************************
387
388
389//*************************************************************************************************
404template< typename MT // Type of the sparse matrix
405 , bool SO > // Storage order
406decltype(auto) maxNorm( const SparseMatrix<MT,SO>& sm )
407{
409
410 return linfNorm( *sm );
411}
412//*************************************************************************************************
413
414
415//*************************************************************************************************
430template< typename MT // Type of the sparse matrix
431 , bool SO > // Storage order
432decltype(auto) minNorm( const SparseMatrix<MT,SO>& sm )
433{
435
436 return min( abs( *sm ) );
437}
438//*************************************************************************************************
439
440} // namespace blaze
441
442#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 isDefault shim.
Header file for the IsResizable type trait.
Header file for the IsRowMajorMatrix 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 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 matrices.
Definition: SparseMatrix.h:77
Header file for the SparseMatrix 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
bool isDefault(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the given diagonal matrix is in default state.
Definition: DiagonalMatrix.h:169
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) sqrNorm(const SparseMatrix< MT, SO > &sm)
Computes the squared L2 norm for the given sparse matrix.
Definition: SMatNormExpr.h:180
decltype(auto) minNorm(const SparseMatrix< MT, SO > &sm)
Computes the minimum norm for the given sparse matrix.
Definition: SMatNormExpr.h:432
decltype(auto) linfNorm(const SparseMatrix< MT, SO > &sm)
Computes the infinity norm for the given sparse matrix.
Definition: SMatNormExpr.h:380
decltype(auto) norm(const SparseMatrix< MT, SO > &sm)
Computes the L2 norm for the given sparse matrix.
Definition: SMatNormExpr.h:154
decltype(auto) maxNorm(const SparseMatrix< MT, SO > &sm)
Computes the maximum norm for the given sparse matrix.
Definition: SMatNormExpr.h:406
decltype(auto) lpNorm(const SparseMatrix< MT, SO > &sm)
Computes the Lp norm for the given sparse matrix.
Definition: SMatNormExpr.h:351
decltype(auto) l1Norm(const SparseMatrix< MT, SO > &sm)
Computes the L1 norm for the given sparse matrix.
Definition: SMatNormExpr.h:206
decltype(auto) l4Norm(const SparseMatrix< MT, SO > &sm)
Computes the L4 norm for the given sparse matrix.
Definition: SMatNormExpr.h:284
decltype(auto) l2Norm(const SparseMatrix< MT, SO > &sm)
Computes the L2 norm for the given sparse matrix.
Definition: SMatNormExpr.h:232
decltype(auto) l3Norm(const SparseMatrix< MT, SO > &sm)
Computes the L3 norm for the given sparse matrix.
Definition: SMatNormExpr.h:258
#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
Header file for basic type definitions.