35 #ifndef _BLAZE_MATH_SPARSE_SPARSEMATRIX_H_ 36 #define _BLAZE_MATH_SPARSE_SPARSEMATRIX_H_ 101 template<
typename MT,
bool SO,
typename ST >
102 auto operator*=( SparseMatrix<MT,SO>& mat, ST scalar )
103 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
105 template<
typename MT,
bool SO,
typename ST >
106 auto operator*=( SparseMatrix<MT,SO>&& mat, ST scalar )
107 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
109 template<
typename MT,
bool SO,
typename ST >
110 auto operator/=( SparseMatrix<MT,SO>& mat, ST scalar )
111 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
113 template<
typename MT,
bool SO,
typename ST >
114 auto operator/=( SparseMatrix<MT,SO>&& mat, ST scalar )
115 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
133 template<
typename MT
141 if( IsRestricted_v<MT> ) {
142 if( !tryMult( ~mat, 0UL, 0UL, (~mat).
rows(), (~mat).
columns(), scalar ) ) {
153 decltype(
auto) left( derestrict( ~mat ) );
156 for(
size_t i=0UL; i<iend; ++i ) {
157 const auto last( left.end(i) );
158 for(
auto element=left.begin(i); element!=last; ++element ) {
159 element->value() *= scalar;
184 template<
typename MT
210 template<
typename MT
220 if( IsRestricted_v<MT> ) {
221 if( !tryDiv( ~mat, 0UL, 0UL, (~mat).
rows(), (~mat).
columns(), scalar ) ) {
227 IsFloatingPoint_v< UnderlyingBuiltin_t<ST> >
233 decltype(
auto) left( derestrict( ~mat ) );
235 if( IsInvertible_v<ScalarType> ) {
236 const ScalarType tmp( ScalarType(1)/static_cast<ScalarType>( scalar ) );
238 for(
size_t i=0UL; i<iend; ++i ) {
239 const auto last( left.end(i) );
240 for(
auto element=left.begin(i); element!=last; ++element ) {
241 element->value() *= tmp;
247 for(
size_t i=0UL; i<iend; ++i ) {
248 const auto last( left.end(i) );
249 for(
auto element=left.begin(i); element!=last; ++element ) {
250 element->value() /= scalar;
277 template<
typename MT
299 template<
typename MT,
bool SO >
300 bool isnan(
const SparseMatrix<MT,SO>& sm );
302 template<
bool RF,
typename MT,
bool SO >
305 template<
bool RF,
typename MT,
bool SO >
308 template<
bool RF,
typename MT,
bool SO >
309 bool isUniform(
const SparseMatrix<MT,SO>& sm );
311 template<
bool RF,
typename MT,
bool SO >
312 bool isZero(
const SparseMatrix<MT,SO>& sm );
314 template<
bool RF,
typename MT,
bool SO >
315 bool isLower(
const SparseMatrix<MT,SO>& sm );
317 template<
bool RF,
typename MT,
bool SO >
318 bool isUniLower(
const SparseMatrix<MT,SO>& sm );
320 template<
bool RF,
typename MT,
bool SO >
323 template<
bool RF,
typename MT,
bool SO >
324 bool isUpper(
const SparseMatrix<MT,SO>& sm );
326 template<
bool RF,
typename MT,
bool SO >
327 bool isUniUpper(
const SparseMatrix<MT,SO>& sm );
329 template<
bool RF,
typename MT,
bool SO >
332 template<
bool RF,
typename MT,
bool SO >
333 bool isDiagonal(
const SparseMatrix<MT,SO>& sm );
335 template<
bool RF,
typename MT,
bool SO >
336 bool isIdentity(
const SparseMatrix<MT,SO>& sm );
361 template<
typename MT
368 for(
size_t i=0UL; i<A.rows(); ++i ) {
369 for(
auto element=A.begin(i); element!=A.end(i); ++element )
370 if(
isnan( element->value() ) )
return true;
374 for(
size_t j=0UL; j<A.columns(); ++j ) {
375 for(
auto element=A.begin(j); element!=A.end(j); ++element )
376 if(
isnan( element->value() ) )
return true;
428 if( IsSymmetric_v<MT> )
434 if( IsUniform_v<MT> || (~sm).
rows() < 2UL )
440 for(
size_t i=0UL; i<A.rows(); ++i ) {
441 for(
auto element=A.begin(i); element!=A.end(i); ++element )
443 const size_t j( element->index() );
445 if( i == j || isDefault<RF>( element->value() ) )
448 const auto pos( A.find( j, i ) );
449 if( pos == A.end(j) || !equal<RF>( pos->value(), element->value() ) )
455 for(
size_t j=0UL; j<A.columns(); ++j ) {
456 for(
auto element=A.begin(j); element!=A.end(j); ++element )
458 const size_t i( element->index() );
460 if( j == i || isDefault<RF>( element->value() ) )
463 const auto pos( A.find( j, i ) );
464 if( pos == A.end(i) || !equal<RF>( pos->value(), element->value() ) )
521 if( IsHermitian_v<MT> )
524 if( !IsNumeric_v<ET> || !
isSquare( ~sm ) )
527 if( IsBuiltin_v<ET> && IsUniform_v<MT> )
533 for(
size_t i=0UL; i<A.rows(); ++i ) {
534 for(
auto element=A.begin(i); element!=A.end(i); ++element )
536 const size_t j( element->index() );
538 if( isDefault<RF>( element->value() ) )
541 if( i == j && !isReal<RF>( element->value() ) )
544 const auto pos( A.find( j, i ) );
545 if( pos == A.end(j) || !equal<RF>( pos->value(),
conj( element->value() ) ) )
551 for(
size_t j=0UL; j<A.columns(); ++j ) {
552 for(
auto element=A.begin(j); element!=A.end(j); ++element )
554 const size_t i( element->index() );
556 if( isDefault<RF>( element->value() ) )
559 if( j == i && !isReal<RF>( element->value() ) )
562 const auto pos( A.find( j, i ) );
563 if( pos == A.end(i) || !equal<RF>( pos->value(),
conj( element->value() ) ) )
584 bool isUniform_backend(
const SparseMatrix<MT,false>& sm,
TrueType )
592 const size_t ibegin( ( IsStrictlyLower_v<MT> )?( 1UL ):( 0UL ) );
593 const size_t iend ( ( IsStrictlyUpper_v<MT> )?( (~sm).
rows()-1UL ):( (~sm).
rows() ) );
595 for(
size_t i=ibegin; i<iend; ++i ) {
596 for(
auto element=(~sm).
begin(i); element!=(~sm).
end(i); ++element ) {
597 if( !isDefault<RF>( element->value() ) )
618 bool isUniform_backend(
const SparseMatrix<MT,true>& sm,
TrueType )
626 const size_t jbegin( ( IsStrictlyUpper_v<MT> )?( 1UL ):( 0UL ) );
627 const size_t jend ( ( IsStrictlyLower_v<MT> )?( (~sm).
columns()-1UL ):( (~sm).
columns() ) );
629 for(
size_t j=jbegin; j<jend; ++j ) {
630 for(
auto element=(~sm).
begin(j); element!=(~sm).
end(j); ++element ) {
631 if( !isDefault<RF>( element->value() ) )
652 bool isUniform_backend(
const SparseMatrix<MT,false>& sm,
FalseType )
660 const size_t maxElements( (~sm).
rows() * (~sm).
columns() );
662 if( (~sm).
nonZeros() != maxElements )
664 for(
size_t i=0UL; i<(~sm).
rows(); ++i ) {
665 for(
auto element=(~sm).begin(i); element!=(~sm).
end(i); ++element ) {
666 if( !isDefault<RF>( element->value() ) )
675 const auto& cmp( (~sm)(0UL,0UL) );
677 for(
size_t i=0UL; i<(~sm).
rows(); ++i ) {
678 for(
auto element=(~sm).begin(i); element!=(~sm).
end(i); ++element ) {
679 if( !equal<RF>( element->value(), cmp ) )
701 bool isUniform_backend(
const SparseMatrix<MT,true>& sm,
FalseType )
709 const size_t maxElements( (~sm).
rows() * (~sm).
columns() );
711 if( (~sm).
nonZeros() != maxElements )
713 for(
size_t j=0UL; j<(~sm).
columns(); ++j ) {
714 for(
auto element=(~sm).begin(j); element!=(~sm).
end(j); ++element ) {
715 if( !isDefault<RF>( element->value() ) )
724 const auto& cmp( (~sm)(0UL,0UL) );
726 for(
size_t j=0UL; j<(~sm).
columns(); ++j ) {
727 for(
auto element=(~sm).begin(j); element!=(~sm).
end(j); ++element ) {
728 if( !equal<RF>( element->value(), cmp ) )
778 if( IsUniform_v<MT> ||
780 ( (~sm).
rows() == 1UL && (~sm).
columns() == 1UL ) )
783 if( IsUniTriangular_v<MT> )
831 const size_t M( (~sm).
rows() );
832 const size_t N( (~sm).
columns() );
834 if( IsZero_v<MT> || M == 0UL || N == 0UL )
837 if( IsUniTriangular_v<MT> )
842 const size_t iend( SO ==
rowMajor ? A.rows() : A.columns() );
844 for(
size_t i=0UL; i<iend; ++i ) {
845 for(
auto element=A.begin(i); element!=A.end(i); ++element ) {
846 if( !isZero<RF>( element->value() ) ) {
916 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
922 for(
size_t i=0UL; i<A.rows()-1UL; ++i ) {
923 for(
auto element=A.lowerBound(i,i+1UL); element!=A.end(i); ++element )
925 if( !isDefault<RF>( element->value() ) )
931 for(
size_t j=1UL; j<A.columns(); ++j ) {
932 for(
auto element=A.begin(j); element!=A.end(j); ++element )
934 if( element->index() >= j )
937 if( !isDefault<RF>( element->value() ) )
1000 if( IsUniLower_v<MT> )
1009 for(
size_t i=0UL; i<A.rows(); ++i )
1011 auto element( A.lowerBound(i,i) );
1013 if( element == A.end(i) || element->index() != i || !isOne<RF>( element->value() ) )
1018 for( ; element!=A.end(i); ++element ) {
1019 if( !isZero<RF>( element->value() ) )
1025 for(
size_t j=0UL; j<A.columns(); ++j )
1027 bool hasDiagonalElement(
false );
1029 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1031 if( element->index() >= j ) {
1032 if( element->index() != j || !isOne<RF>( element->value() ) )
1034 hasDiagonalElement =
true;
1038 if( !isZero<RF>( element->value() ) )
1042 if( !hasDiagonalElement ) {
1106 if( IsStrictlyLower_v<MT> )
1112 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1115 if( IsUniLower_v<MT> || IsUniUpper_v<MT> )
1121 for(
size_t i=0UL; i<A.rows(); ++i ) {
1122 for(
auto element=A.lowerBound(i,i); element!=A.end(i); ++element )
1124 if( !isDefault<RF>( element->value() ) )
1130 for(
size_t j=0UL; j<A.columns(); ++j ) {
1131 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1133 if( element->index() > j )
1136 if( !isDefault<RF>( element->value() ) )
1206 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1212 for(
size_t i=1UL; i<A.rows(); ++i ) {
1213 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1215 if( element->index() >= i )
1218 if( !isDefault<RF>( element->value() ) )
1224 for(
size_t j=0UL; j<A.columns()-1UL; ++j ) {
1225 for(
auto element=A.lowerBound(j+1UL,j); element!=A.end(j); ++element )
1227 if( !isDefault<RF>( element->value() ) )
1290 if( IsUniUpper_v<MT> )
1299 for(
size_t i=0UL; i<A.rows(); ++i )
1301 bool hasDiagonalElement(
false );
1303 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1305 if( element->index() >= i ) {
1306 if( element->index() != i || !isOne<RF>( element->value() ) )
1308 hasDiagonalElement =
true;
1311 else if( !isZero<RF>( element->value() ) ) {
1316 if( !hasDiagonalElement ) {
1322 for(
size_t j=0UL; j<A.columns(); ++j )
1324 auto element( A.lowerBound(j,j) );
1326 if( element == A.end(j) || element->index() != j || !isOne<RF>( element->value() ) )
1331 for( ; element!=A.end(j); ++element ) {
1332 if( !isZero<RF>( element->value() ) )
1396 if( IsStrictlyUpper_v<MT> )
1402 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1405 if( IsUniLower_v<MT> || IsUniUpper_v<MT> )
1411 for(
size_t i=0UL; i<A.rows(); ++i ) {
1412 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1414 if( element->index() > i )
1417 if( !isDefault<RF>( element->value() ) )
1423 for(
size_t j=0UL; j<A.columns(); ++j ) {
1424 for(
auto element=A.lowerBound(j,j); element!=A.end(j); ++element )
1426 if( !isDefault<RF>( element->value() ) )
1490 if( IsDiagonal_v<MT> )
1496 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1502 for(
size_t i=0UL; i<A.rows(); ++i ) {
1503 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1504 if( element->index() != i && !isDefault<RF>( element->value() ) )
1509 for(
size_t j=0UL; j<A.columns(); ++j ) {
1510 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1511 if( element->index() != j && !isDefault<RF>( element->value() ) )
1574 if( IsIdentity_v<MT> )
1583 for(
size_t i=0UL; i<A.rows(); ++i )
1585 bool hasDiagonalElement(
false );
1587 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1589 if( element->index() == i ) {
1590 if( !isOne<RF>( element->value() ) )
1592 hasDiagonalElement =
true;
1594 else if( !isZero<RF>( element->value() ) ) {
1599 if( !hasDiagonalElement ) {
1605 for(
size_t j=0UL; j<A.columns(); ++j )
1607 bool hasDiagonalElement(
false );
1609 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1611 if( element->index() == j ) {
1612 if( !isOne<RF>( element->value() ) )
1614 hasDiagonalElement =
true;
1616 else if( !isZero<RF>( element->value() ) ) {
1621 if( !hasDiagonalElement ) {
1646 template<
typename MT
1648 ,
typename... Args >
1649 auto erase( SparseMatrix<MT,SO>& sm, Args&&... args )
1650 -> decltype( (~sm).erase( std::forward<Args>( args )... ) )
1652 return (~sm).erase( std::forward<Args>( args )... );
#define BLAZE_CONSTRAINT_MUST_BE_TRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is not a lower or upper triangular matrix t...
Definition: Triangular.h:61
Header file for the isnan shim.
Header file for the UnderlyingNumeric type trait.
BoolConstant< false > FalseType
Type/value traits base class.The FalseType class is used as base class for type traits and value trai...
Definition: IntegralConstant.h:121
#define BLAZE_THROW_INVALID_ARGUMENT(MESSAGE)
Macro for the emission of a std::invalid_argument exception.This macro encapsulates the default way o...
Definition: Exception.h:235
Header file for auxiliary alias declarations.
bool isLower(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a lower triangular matrix.
Definition: DenseMatrix.h:1793
bool isUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is an upper triangular matrix.
Definition: DenseMatrix.h:2060
bool isStrictlyLower(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a strictly lower triangular matrix.
Definition: DenseMatrix.h:1968
#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
Header file for the IsUniUpper type trait.
Compile time check for triangular matrix types.This type trait tests whether or not the given templat...
Definition: IsTriangular.h:86
Header file for basic type definitions.
#define BLAZE_CONSTRAINT_MUST_NOT_BE_TRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower or upper triangular matrix type,...
Definition: Triangular.h:81
typename If< Condition, T1, T2 >::Type If_t
Auxiliary alias template for the If class template.The If_t alias template provides a convenient shor...
Definition: If.h:109
typename T::ResultType ResultType_t
Alias declaration for nested ResultType type definitions.The ResultType_t alias declaration provides ...
Definition: Aliases.h:390
Header file for the isZero shim.
Header file for the IsDiagonal type trait.
typename DivTrait< T1, T2 >::Type DivTrait_t
Auxiliary alias declaration for the DivTrait class template.The DivTrait_t alias declaration provides...
Definition: DivTrait.h:239
MT::Iterator begin(Matrix< MT, SO > &matrix, size_t i)
Returns an iterator to the first element of row/column i.
Definition: Matrix.h:372
void reset(const DiagonalProxy< MT > &proxy)
Resetting the represented element to the default initial values.
Definition: DiagonalProxy.h:595
bool isUniLower(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a lower unitriangular matrix.
Definition: DenseMatrix.h:1881
#define BLAZE_CONSTRAINT_MUST_NOT_BE_UNITRIANGULAR_MATRIX_TYPE(T)
Constraint on the data type.In case the given data type T is a lower or upper unitriangular matrix ty...
Definition: UniTriangular.h:81
Header file for the IsIdentity type trait.
bool isDiagonal(const DenseMatrix< MT, SO > &dm)
Checks if the give dense matrix is diagonal.
Definition: DenseMatrix.h:2328
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: IntegralConstant.h:132
typename T::ReturnType ReturnType_t
Alias declaration for nested ReturnType type definitions.The ReturnType_t alias declaration provides ...
Definition: Aliases.h:410
Header file for the IsUniLower type trait.
constexpr size_t columns(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of columns of the matrix.
Definition: Matrix.h:514
constexpr bool rowMajor
Storage order flag for row-major matrices.
Definition: StorageOrder.h:71
Base class for sparse matrices.The SparseMatrix class is a base class for all sparse matrix classes....
Definition: Forward.h:145
typename T::ElementType ElementType_t
Alias declaration for nested ElementType type definitions.The ElementType_t alias declaration provide...
Definition: Aliases.h:170
bool isIdentity(const DenseMatrix< MT, SO > &dm)
Checks if the give dense matrix is an identity matrix.
Definition: DenseMatrix.h:2433
Header file for the SparseMatrix base class.
Header file for the IsSquare type trait.
bool isZero(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is 0.
Definition: DiagonalProxy.h:677
Header file for the matrix storage order types.
typename EnableIf< Condition, T >::Type EnableIf_t
Auxiliary type for the EnableIf class template.The EnableIf_t alias declaration provides a convenient...
Definition: EnableIf.h:138
Constraint on the data type.
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.The IsResizable_v variable template provid...
Definition: IsResizable.h:133
Header file for the IsStrictlyUpper type trait.
Header file for the IsSymmetric type trait.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Header file for the If class template.
Header file for the IsFloatingPoint type trait.
Header file for the UnderlyingBuiltin type trait.
Header file for the IsLower type trait.
Header file for the equal shim.
Header file for the IsUniTriangular type trait.
Header file for the IsTriangular type trait.
bool isnan(const DiagonalProxy< MT > &proxy)
Returns whether the represented element is not a number.
Definition: DiagonalProxy.h:717
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:1638
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
Constraint on the data type.
Header file for the EnableIf class template.
Header file for the IsStrictlyLower type trait.
Header file for the isOne shim.
Header file for the conjugate shim.
Constraint on the data type.
Header file for the IsNumeric type trait.
auto operator/=(DenseMatrix< MT, SO > &mat, ST scalar) -> EnableIf_t< IsNumeric_v< ST >, MT & >
Division assignment operator for the division of a dense matrix by a scalar value ( ).
Definition: DenseMatrix.h:558
bool isStrictlyUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a strictly upper triangular matrix.
Definition: DenseMatrix.h:2235
Header file for run time assertion macros.
typename T::CompositeType CompositeType_t
Alias declaration for nested CompositeType type definitions.The CompositeType_t alias declaration pro...
Definition: Aliases.h:90
Header file for the division trait.
Header file for the IsZero type trait.
bool isHermitian(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is Hermitian.
Definition: DenseMatrix.h:1406
Header file for the isDefault shim.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:1328
#define BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION(T)
Constraint on the data type.In case the given data type T requires an intermediate evaluation within ...
Definition: RequiresEvaluation.h:81
constexpr size_t rows(const Matrix< MT, SO > &matrix) noexcept
Returns the current number of rows of the matrix.
Definition: Matrix.h:498
Header file for the IsInvertible type trait.
Header file for the IsBuiltin type trait.
Header file for the IntegralConstant class template.
bool isUniUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is an upper unitriangular matrix.
Definition: DenseMatrix.h:2148
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:264
Header file for the IsComplex type trait.
auto operator *=(DenseMatrix< MT, SO > &mat, ST scalar) -> EnableIf_t< IsNumeric_v< ST >, MT & >
Multiplication assignment operator for the multiplication of a dense matrix and a scalar value ( ).
Definition: DenseMatrix.h:494
Header file for the IsUpper type trait.
decltype(auto) conj(const DenseMatrix< MT, SO > &dm)
Returns a matrix containing the complex conjugate of each single element of dm.
Definition: DMatMapExpr.h:1324
Header file for the IsHermitian type trait.
bool isSquare(const Matrix< MT, SO > &matrix) noexcept
Checks if the given matrix is a square matrix.
Definition: Matrix.h:951
Header file for the IsResizable type trait.
Header file for the IsRestricted type trait.
Header file for the isReal shim.
#define BLAZE_INTERNAL_ASSERT(expr, msg)
Run time assertion macro for internal checks.In case of an invalid run time expression,...
Definition: Assert.h:101
Header file for the IsExpression type trait class.