Blaze 3.9
DMatExpExpr.h
Go to the documentation of this file.
1//=================================================================================================
33//=================================================================================================
34
35#ifndef _BLAZE_MATH_EXPRESSIONS_DMATEXPEXPR_H_
36#define _BLAZE_MATH_EXPRESSIONS_DMATEXPEXPR_H_
37
38
39//*************************************************************************************************
40// Includes
41//*************************************************************************************************
42
43#include <blaze/math/Aliases.h>
62#include <blaze/util/Assert.h>
64#include <blaze/util/mpl/If.h>
65#include <blaze/util/Types.h>
66
67
68namespace blaze {
69
70//=================================================================================================
71//
72// CLASS DMATEXPEXPR
73//
74//=================================================================================================
75
76//*************************************************************************************************
83template< typename MT // Type of the dense matrix
84 , bool SO > // Storage order
86 : public MatExpExpr< DenseMatrix< DMatExpExpr<MT,SO>, SO > >
87 , private Computation
88{
89 private:
90 //**Type definitions****************************************************************************
93 //**********************************************************************************************
94
95 //**********************************************************************************************
96 static constexpr size_t K = 18;
97 //**********************************************************************************************
98
99 public:
100 //**Type definitions****************************************************************************
103
106
112
115
117 using Operand = If_t< IsExpression_v<MT>, const MT, const MT& >;
118 //**********************************************************************************************
119
120 //**Compilation flags***************************************************************************
122 static constexpr bool simdEnabled = false;
123
125 static constexpr bool smpAssignable = false;
126 //**********************************************************************************************
127
128 //**Constructor*********************************************************************************
133 explicit inline DMatExpExpr( const MT& dm ) noexcept
134 : dm_( dm ) // Dense matrix of the exponential expression
135 {
136 BLAZE_INTERNAL_ASSERT( isSquare( *dm ), "Non-square matrix detected" );
137 }
138 //**********************************************************************************************
139
140 //**Rows function*******************************************************************************
145 inline size_t rows() const noexcept {
146 return dm_.columns();
147 }
148 //**********************************************************************************************
149
150 //**Columns function****************************************************************************
155 inline size_t columns() const noexcept {
156 return dm_.rows();
157 }
158 //**********************************************************************************************
159
160 //**Operand access******************************************************************************
165 inline Operand operand() const noexcept {
166 return dm_;
167 }
168 //**********************************************************************************************
169
170 //**********************************************************************************************
176 template< typename T >
177 inline bool canAlias( const T* alias ) const noexcept {
178 return dm_.isAliased( alias );
179 }
180 //**********************************************************************************************
181
182 //**********************************************************************************************
188 template< typename T >
189 inline bool isAliased( const T* alias ) const noexcept {
190 return dm_.isAliased( alias );
191 }
192 //**********************************************************************************************
193
194 private:
195 //**Member variables****************************************************************************
197 //**********************************************************************************************
198
199 //**Assignment to dense matrices****************************************************************
211 template< typename MT2 // Type of the target dense matrix
212 , bool SO2 > // Storage order of the target dense matrix
213 friend inline void assign( DenseMatrix<MT2,SO2>& lhs, const DMatExpExpr& rhs )
214 {
216
217 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
218 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
219
220 const size_t N( rhs.rows() );
221
222 if( IsDiagonal_v<MT> || N < 2UL )
223 {
224 assign( *lhs, rhs.dm_ );
225 for( size_t i=0UL; i<N; ++i ) {
226 (*lhs)(i,i) = exp( (*lhs)(i,i) );
227 }
228 }
229 else
230 {
231 using BT = UnderlyingBuiltin_t<ET>;
232
233 const BT norm( maxNorm( rhs.dm_ ) );
234
235 int exponent( 0 );
236 frexp( norm, &exponent );
237 exponent = max( 0, exponent );
238
239 ResultType R( rhs.dm_ / pow( 2.0, double(exponent) ) );
240 ResultType A( R );
241 ResultType B( R );
242
243 for( size_t i=0UL; i<N; ++i ) {
244 B(i,i) += BT(1);
245 }
246
247 BT factor( 1 );
248 for( size_t k=2UL; k<K; ++k ) {
249 factor *= BT( k );
250 A *= R;
251 addAssign( B, ( A / factor ) );
252 }
253
254 for( int i=0; i<exponent; ++i ) {
255 B *= B;
256 }
257
258 assign( *lhs, B );
259 }
260 }
262 //**********************************************************************************************
263
264 //**Assignment to sparse matrices***************************************************************
276 template< typename MT2 // Type of the target sparse matrix
277 , bool SO2 > // Storage order of the target sparse matrix
278 friend inline void assign( SparseMatrix<MT2,SO2>& lhs, const DMatExpExpr& rhs )
279 {
281
285
286 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
287 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
288
289 const ResultType tmp( serial( rhs ) );
290 assign( *lhs, tmp );
291 }
293 //**********************************************************************************************
294
295 //**Addition assignment to dense matrices*******************************************************
307 template< typename MT2 // Type of the target dense matrix
308 , bool SO2 > // Storage order of the target dense matrix
309 friend inline void addAssign( DenseMatrix<MT2,SO2>& lhs, const DMatExpExpr& rhs )
310 {
312
313 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
314 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
315
316 const size_t N( rhs.rows() );
317
318 if( IsDiagonal_v<MT> || N < 2UL )
319 {
320 CompositeType_t<MT> tmp( rhs.dm_ );
321 for( size_t i=0UL; i<N; ++i ) {
322 (*lhs)(i,i) += exp( tmp(i,i) );
323 }
324 }
325 else
326 {
327 using BT = UnderlyingBuiltin_t<ET>;
328
329 const BT norm( maxNorm( rhs.dm_ ) );
330
331 int exponent( 0 );
332 frexp( norm, &exponent );
333 exponent = max( 0, exponent );
334
335 ResultType R( rhs.dm_ / pow( 2.0, double(exponent) ) );
336 ResultType A( R );
337 ResultType B( R );
338
339 for( size_t i=0UL; i<N; ++i ) {
340 B(i,i) += BT(1);
341 }
342
343 BT factor( 1 );
344 for( size_t k=2UL; k<K; ++k ) {
345 factor *= BT( k );
346 A *= R;
347 addAssign( B, ( A / factor ) );
348 }
349
350 for( int i=0; i<exponent; ++i ) {
351 B *= B;
352 }
353
354 addAssign( *lhs, B );
355 }
356 }
358 //**********************************************************************************************
359
360 //**Addition assignment to sparse matrices******************************************************
361 // No special implementation for the addition assignment to sparse matrices.
362 //**********************************************************************************************
363
364 //**Subtraction assignment to dense matrices****************************************************
376 template< typename MT2 // Type of the target dense matrix
377 , bool SO2 > // Storage order of the target dense matrix
378 friend inline void subAssign( DenseMatrix<MT2,SO2>& lhs, const DMatExpExpr& rhs )
379 {
381
382 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
383 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
384
385 const size_t N( rhs.rows() );
386
387 if( IsDiagonal_v<MT> || N < 2UL )
388 {
389 CompositeType_t<MT> tmp( rhs.dm_ );
390 for( size_t i=0UL; i<N; ++i ) {
391 (*lhs)(i,i) -= exp( tmp(i,i) );
392 }
393 }
394 else
395 {
396 using BT = UnderlyingBuiltin_t<ET>;
397
398 const BT norm( maxNorm( rhs.dm_ ) );
399
400 int exponent( 0 );
401 frexp( norm, &exponent );
402 exponent = max( 0, exponent );
403
404 ResultType R( rhs.dm_ / pow( 2.0, double(exponent) ) );
405 ResultType A( R );
406 ResultType B( R );
407
408 for( size_t i=0UL; i<N; ++i ) {
409 B(i,i) += BT(1);
410 }
411
412 BT factor( 1 );
413 for( size_t k=2UL; k<K; ++k ) {
414 factor *= BT( k );
415 A *= R;
416 addAssign( B, ( A / factor ) );
417 }
418
419 for( int i=0; i<exponent; ++i ) {
420 B *= B;
421 }
422
423 subAssign( *lhs, B );
424 }
425 }
427 //**********************************************************************************************
428
429 //**Subtraction assignment to sparse matrices***************************************************
430 // No special implementation for the subtraction assignment to sparse matrices.
431 //**********************************************************************************************
432
433 //**Schur product assignment to dense matrices**************************************************
445 template< typename MT2 // Type of the target dense matrix
446 , bool SO2 > // Storage order of the target dense matrix
447 friend inline void schurAssign( DenseMatrix<MT2,SO2>& lhs, const DMatExpExpr& rhs )
448 {
450
451 BLAZE_INTERNAL_ASSERT( (*lhs).rows() == rhs.rows() , "Invalid number of rows" );
452 BLAZE_INTERNAL_ASSERT( (*lhs).columns() == rhs.columns(), "Invalid number of columns" );
453
454 const size_t N( rhs.rows() );
455
456 if( IsDiagonal_v<MT> || N < 2UL )
457 {
458 CompositeType_t<MT> tmp( rhs.dm_ );
459 for( size_t i=0UL; i<N; ++i ) {
460 (*lhs)(i,i) *= exp( tmp(i,i) );
461 }
462 }
463 else
464 {
465 using BT = UnderlyingBuiltin_t<ET>;
466
467 const BT norm( maxNorm( rhs.dm_ ) );
468
469 int exponent( 0 );
470 frexp( norm, &exponent );
471 exponent = max( 0, exponent );
472
473 ResultType R( rhs.dm_ / pow( 2.0, double(exponent) ) );
474 ResultType A( R );
475 ResultType B( R );
476
477 for( size_t i=0UL; i<N; ++i ) {
478 B(i,i) += BT(1);
479 }
480
481 BT factor( 1 );
482 for( size_t k=2UL; k<K; ++k ) {
483 factor *= BT( k );
484 A *= R;
485 addAssign( B, ( A / factor ) );
486 }
487
488 for( int i=0; i<exponent; ++i ) {
489 B *= B;
490 }
491
492 schurAssign( *lhs, B );
493 }
494 }
496 //**********************************************************************************************
497
498 //**Schur product assignment to sparse matrices*************************************************
499 // No special implementation for the Schur product assignment to sparse matrices.
500 //**********************************************************************************************
501
502 //**Multiplication assignment to dense matrices*************************************************
503 // No special implementation for the multiplication assignment to dense matrices.
504 //**********************************************************************************************
505
506 //**Multiplication assignment to sparse matrices************************************************
507 // No special implementation for the multiplication assignment to sparse matrices.
508 //**********************************************************************************************
509
510 //**Compile time checks*************************************************************************
515 //**********************************************************************************************
516};
517//*************************************************************************************************
518
519
520
521
522//=================================================================================================
523//
524// GLOBAL OPERATORS
525//
526//=================================================================================================
527
528//*************************************************************************************************
562template< typename MT // Type of the dense matrix
563 , bool SO > // Storage order
564inline decltype(auto) matexp( const DenseMatrix<MT,SO>& dm )
565{
567
569
570 if( !isSquare( *dm ) ) {
571 BLAZE_THROW_INVALID_ARGUMENT( "Invalid non-square matrix provided" );
572 }
573
574 using ReturnType = const DMatExpExpr<MT,SO>;
575 return ReturnType( *dm );
576}
577//*************************************************************************************************
578
579
580
581
582//=================================================================================================
583//
584// GLOBAL RESTRUCTURING FUNCTIONS
585//
586//=================================================================================================
587
588//*************************************************************************************************
605template< typename MT // Type of the dense matrix
606 , bool SO > // Storage order
607inline decltype(auto) det( const DMatExpExpr<MT,SO>& dm )
608{
610
611 return det( evaluate( *dm ) );
612}
614//*************************************************************************************************
615
616} // namespace blaze
617
618#endif
Header file for auxiliary alias declarations.
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.
Definition: Aliases.h:470
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.
Definition: Aliases.h:450
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.
Definition: Aliases.h:190
typename T::OppositeType OppositeType_t
Alias declaration for nested OppositeType type definitions.
Definition: Aliases.h:310
typename T::TransposeType TransposeType_t
Alias declaration for nested TransposeType type definitions.
Definition: Aliases.h:550
Header file for run time assertion macros.
Constraint on the data type.
Header file for the frexp shim.
Header file for the function trace functionality.
Header file for the If class template.
Header file for the IsDiagonal type trait.
Header file for the IsExpression type trait class.
Header file for the RemoveAdaptor type trait.
Header file for the UnderlyingBuiltin type trait.
Expression object for dense matrix exponential.
Definition: DMatExpExpr.h:88
static constexpr bool smpAssignable
Compilation switch for the expression template assignment strategy.
Definition: DMatExpExpr.h:125
Operand dm_
Dense matrix of the exponential expression.
Definition: DMatExpExpr.h:196
static constexpr bool simdEnabled
Compilation switch for the expression template evaluation strategy.
Definition: DMatExpExpr.h:122
bool isAliased(const T *alias) const noexcept
Returns whether the expression is aliased with the given address alias.
Definition: DMatExpExpr.h:189
ElementType_t< MT > ElementType
Resulting element type.
Definition: DMatExpExpr.h:110
static constexpr size_t K
The approximation limit for the exponential computation.
Definition: DMatExpExpr.h:96
size_t columns() const noexcept
Returns the current number of columns of the matrix.
Definition: DMatExpExpr.h:155
ResultType_t< MT > RT
Result type of the dense matrix expression.
Definition: DMatExpExpr.h:91
bool canAlias(const T *alias) const noexcept
Returns whether the expression can alias with the given address alias.
Definition: DMatExpExpr.h:177
If_t< IsExpression_v< MT >, const MT, const MT & > Operand
Composite data type of the dense matrix expression.
Definition: DMatExpExpr.h:117
DMatExpExpr(const MT &dm) noexcept
Constructor for the DMatExpExpr class.
Definition: DMatExpExpr.h:133
OppositeType_t< MT > OppositeType
Result type with opposite storage order for expression template evaluations.
Definition: DMatExpExpr.h:108
const ResultType CompositeType
Data type for composite expression templates.
Definition: DMatExpExpr.h:114
ReturnType_t< MT > ReturnType
Return type for expression template evaluations.
Definition: DMatExpExpr.h:111
size_t rows() const noexcept
Returns the current number of rows of the matrix.
Definition: DMatExpExpr.h:145
RemoveAdaptor_t< RT > ResultType
Result type for expression template evaluations.
Definition: DMatExpExpr.h:107
Operand operand() const noexcept
Returns the dense matrix operand.
Definition: DMatExpExpr.h:165
ElementType_t< MT > ET
Element type of the dense matrix expression.
Definition: DMatExpExpr.h:92
TransposeType_t< MT > TransposeType
Transpose type for expression template evaluations.
Definition: DMatExpExpr.h:109
Base class for dense matrices.
Definition: DenseMatrix.h:82
Base class for sparse matrices.
Definition: SparseMatrix.h:77
Constraint on the data type.
Constraint on the data type.
Header file for the Computation base class.
Header file for the DenseMatrix base class.
Header file for the MatExpExpr base class.
decltype(auto) exp(const DenseMatrix< MT, SO > &dm)
Computes for each single element of the dense matrix dm.
Definition: DMatMapExpr.h:1801
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) maxNorm(const DenseMatrix< MT, SO > &dm)
Computes the maximum norm for the given dense matrix.
Definition: DMatNormExpr.h:825
decltype(auto) pow(const DenseMatrix< MT1, SO1 > &lhs, const DenseMatrix< MT2, SO2 > &rhs)
Computes the componentwise exponential value for the dense matrices lhs and rhs.
Definition: DMatDMatMapExpr.h:1448
decltype(auto) norm(const DenseMatrix< MT, SO > &dm)
Computes the L2 norm for the given dense matrix.
Definition: DMatNormExpr.h:573
decltype(auto) serial(const DenseMatrix< MT, SO > &dm)
Forces the serial evaluation of the given dense matrix expression dm.
Definition: DMatSerialExpr.h:812
ElementType_t< MT > det(const DenseMatrix< MT, SO > &dm)
Computation of the determinant of the given dense square matrix.
Definition: DMatDetExpr.h:384
decltype(auto) matexp(const DenseMatrix< MT, SO > &dm)
Calculation of the exponential of the given dense matrix.
Definition: DMatExpExpr.h:564
#define BLAZE_CONSTRAINT_MUST_BE_BLAS_COMPATIBLE_TYPE(T)
Constraint on the data type.
Definition: BLASCompatible.h:61
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.
Definition: RequiresEvaluation.h:81
#define BLAZE_CONSTRAINT_MUST_BE_DENSE_MATRIX_TYPE(T)
Constraint on the data type.
Definition: DenseMatrix.h:61
#define BLAZE_CONSTRAINT_MUST_BE_MATRIX_WITH_STORAGE_ORDER(T, SO)
Constraint on the data type.
Definition: StorageOrder.h:63
typename UnderlyingBuiltin< T >::Type UnderlyingBuiltin_t
Auxiliary alias declaration for the UnderlyingBuiltin type trait.
Definition: UnderlyingBuiltin.h:117
typename RemoveAdaptor< T >::Type RemoveAdaptor_t
Auxiliary alias declaration for the RemoveAdaptor type trait.
Definition: RemoveAdaptor.h:153
MT::ResultType evaluate(const Matrix< MT, SO > &matrix)
Evaluates the given matrix expression.
Definition: Matrix.h:1282
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:1383
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.
Definition: Assert.h:101
typename If< Condition >::template Type< T1, T2 > If_t
Auxiliary alias template for the If class template.
Definition: If.h:108
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.
Definition: Exception.h:235
#define BLAZE_FUNCTION_TRACE
Function trace macro.
Definition: FunctionTrace.h:94
Header file for the exception macros of the math module.
Constraints on the storage order of matrix types.
Header file for all forward declarations for expression class templates.
Header file for the exp shim.
Header file for the pow shim.
Header file for the serial shim.
Base class for all compute expression templates.
Definition: Computation.h:68
Base class for all matrix exponential expression templates.
Definition: MatExpExpr.h:68
Header file for basic type definitions.
Header file for the generic max algorithm.