Blaze  3.6
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>
74 
75 
76 namespace blaze {
77 
78 //=================================================================================================
79 //
80 // GLOBAL FUNCTIONS
81 //
82 //=================================================================================================
83 
84 //*************************************************************************************************
99 template< typename MT // Type of the sparse matrix
100  , bool SO // Storage order
101  , typename Abs // Type of the abs operation
102  , typename Power // Type of the power operation
103  , typename Root > // Type of the root operation
104 inline decltype(auto) norm_backend( const SparseMatrix<MT,SO>& sm, Abs abs, Power power, Root root )
105 {
106  using CT = CompositeType_t<MT>;
107  using ET = ElementType_t<MT>;
108  using RT = decltype( evaluate( root( std::declval<ET>() ) ) );
109 
110  if( (~sm).rows() == 0UL || (~sm).columns() == 0UL ) return RT();
111 
112  CT tmp( ~sm );
113 
114  const size_t N( IsRowMajorMatrix_v<MT> ? tmp.rows(): tmp.columns() );
115 
116  ET norm{};
117 
118  for( size_t i=0UL; i<N; ++i )
119  {
120  const auto end( tmp.end(i) );
121  for( auto element=tmp.begin(i); element!=end; ++element ) {
122  if( IsResizable_v<ET> && isDefault( norm ) )
123  norm = power( abs( element->value() ) );
124  else
125  norm += power( abs( element->value() ) );
126  }
127  }
128 
129  return evaluate( root( norm ) );
130 }
132 //*************************************************************************************************
133 
134 
135 //*************************************************************************************************
150 template< typename MT // Type of the sparse matrix
151  , bool SO > // Storage order
152 decltype(auto) norm( const SparseMatrix<MT,SO>& sm )
153 {
155 
156  return norm_backend( ~sm, SqrAbs(), Noop(), Sqrt() );
157 }
158 //*************************************************************************************************
159 
160 
161 //*************************************************************************************************
176 template< typename MT // Type of the sparse matrix
177  , bool SO > // Storage order
178 decltype(auto) sqrNorm( const SparseMatrix<MT,SO>& sm )
179 {
181 
182  return norm_backend( ~sm, SqrAbs(), Noop(), Noop() );
183 }
184 //*************************************************************************************************
185 
186 
187 //*************************************************************************************************
202 template< typename MT // Type of the sparse matrix
203  , bool SO > // Storage order
204 decltype(auto) l1Norm( const SparseMatrix<MT,SO>& sm )
205 {
207 
208  return norm_backend( ~sm, Abs(), Noop(), Noop() );
209 }
210 //*************************************************************************************************
211 
212 
213 //*************************************************************************************************
228 template< typename MT // Type of the sparse matrix
229  , bool SO > // Storage order
230 decltype(auto) l2Norm( const SparseMatrix<MT,SO>& sm )
231 {
233 
234  return norm_backend( ~sm, SqrAbs(), Noop(), Sqrt() );
235 }
236 //*************************************************************************************************
237 
238 
239 //*************************************************************************************************
254 template< typename MT // Type of the sparse matrix
255  , bool SO > // Storage order
256 decltype(auto) l3Norm( const SparseMatrix<MT,SO>& sm )
257 {
259 
260  return norm_backend( ~sm, Abs(), Pow3(), Cbrt() );
261 }
262 //*************************************************************************************************
263 
264 
265 //*************************************************************************************************
280 template< typename MT // Type of the sparse matrix
281  , bool SO > // Storage order
282 decltype(auto) l4Norm( const SparseMatrix<MT,SO>& sm )
283 {
285 
286  return norm_backend( ~sm, SqrAbs(), Pow2(), Qdrt() );
287 }
288 //*************************************************************************************************
289 
290 
291 //*************************************************************************************************
311 template< typename MT // Type of the sparse matrix
312  , bool SO // Storage order
313  , typename ST > // Type of the norm parameter
314 decltype(auto) lpNorm( const SparseMatrix<MT,SO>& sm, ST p )
315 {
317 
318  BLAZE_USER_ASSERT( !isZero( p ), "Invalid p for Lp norm detected" );
319 
320  using ScalarType = MultTrait_t< UnderlyingBuiltin_t<MT>, decltype( inv( p ) ) >;
321  using UnaryPow = Bind2nd<Pow,ScalarType>;
322  return norm_backend( ~sm, Abs(), UnaryPow( Pow(), p ), UnaryPow( Pow(), inv( p ) ) );
323 }
324 //*************************************************************************************************
325 
326 
327 //*************************************************************************************************
346 template< size_t P // Compile time norm parameter
347  , typename MT // Type of the sparse matrix
348  , bool SO > // Storage order
349 inline decltype(auto) lpNorm( const SparseMatrix<MT,SO>& sm )
350 {
351  BLAZE_STATIC_ASSERT_MSG( P > 0UL, "Invalid norm parameter detected" );
352 
354  using Norm = typename TypeAt< Norms, min( P-1UL, 4UL ) >::Type;
355 
356  return Norm()( ~sm );
357 }
358 //*************************************************************************************************
359 
360 
361 //*************************************************************************************************
376 template< typename MT // Type of the sparse matrix
377  , bool SO > // Storage order
378 decltype(auto) linfNorm( const SparseMatrix<MT,SO>& sm )
379 {
381 
382  return max( abs( ~sm ) );
383 }
384 //*************************************************************************************************
385 
386 
387 //*************************************************************************************************
402 template< typename MT // Type of the sparse matrix
403  , bool SO > // Storage order
404 decltype(auto) maxNorm( const SparseMatrix<MT,SO>& sm )
405 {
407 
408  return linfNorm( ~sm );
409 }
410 //*************************************************************************************************
411 
412 } // namespace blaze
413 
414 #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 basic type definitions.
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
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
Header file for the Cbrt functor.
Header file for the LpNorm functor.
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes....
Definition: Forward.h:145
Header file for the SparseMatrix base class.
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
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
Header file for the isDefault shim.
Header file for the Noop functor.
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
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.
Header file for the IsRowMajorMatrix type trait.
bool isDefault(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is in default state.
Definition: DiagonalProxy.h:635
Header file for the L1Norm functor.
Header file for the Qdrt functor.
Header file for the IsResizable type trait.
Header file for the Pow3 functor.
Header file for the function trace functionality.