35 #ifndef _BLAZE_MATH_SPARSE_SPARSEMATRIX_H_ 36 #define _BLAZE_MATH_SPARSE_SPARSEMATRIX_H_ 103 template<
typename MT,
bool SO,
typename ST >
104 auto operator*=( SparseMatrix<MT,SO>& mat, ST scalar )
105 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
107 template<
typename MT,
bool SO,
typename ST >
108 auto operator*=( SparseMatrix<MT,SO>&& mat, ST scalar )
109 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
111 template<
typename MT,
bool SO,
typename ST >
112 auto operator/=( SparseMatrix<MT,SO>& mat, ST scalar )
113 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
115 template<
typename MT,
bool SO,
typename ST >
116 auto operator/=( SparseMatrix<MT,SO>&& mat, ST scalar )
117 -> EnableIf_t< IsNumeric_v<ST>, MT& >;
135 template<
typename MT
143 if( IsRestricted_v<MT> ) {
144 if( !tryMult( ~mat, 0UL, 0UL, (~mat).
rows(), (~mat).
columns(), scalar ) ) {
155 BLAZE_DECLTYPE_AUTO( left, derestrict( ~mat ) );
158 for(
size_t i=0UL; i<iend; ++i ) {
159 const auto last( left.end(i) );
160 for(
auto element=left.begin(i); element!=last; ++element ) {
161 element->value() *= scalar;
186 template<
typename MT
212 template<
typename MT
222 if( IsRestricted_v<MT> ) {
223 if( !tryDiv( ~mat, 0UL, 0UL, (~mat).
rows(), (~mat).
columns(), scalar ) ) {
229 IsFloatingPoint_v< UnderlyingBuiltin_t<ST> >
235 BLAZE_DECLTYPE_AUTO( left, derestrict( ~mat ) );
237 if( IsInvertible_v<ScalarType> ) {
238 const ScalarType tmp( ScalarType(1)/static_cast<ScalarType>( scalar ) );
240 for(
size_t i=0UL; i<iend; ++i ) {
241 const auto last( left.end(i) );
242 for(
auto element=left.begin(i); element!=last; ++element ) {
243 element->value() *= tmp;
249 for(
size_t i=0UL; i<iend; ++i ) {
250 const auto last( left.end(i) );
251 for(
auto element=left.begin(i); element!=last; ++element ) {
252 element->value() /= scalar;
279 template<
typename MT
301 template<
typename MT,
bool SO >
302 bool isnan(
const SparseMatrix<MT,SO>& sm );
304 template<
bool RF,
typename MT,
bool SO >
307 template<
bool RF,
typename MT,
bool SO >
310 template<
bool RF,
typename MT,
bool SO >
311 bool isUniform(
const SparseMatrix<MT,SO>& sm );
313 template<
bool RF,
typename MT,
bool SO >
314 bool isZero(
const SparseMatrix<MT,SO>& sm );
316 template<
bool RF,
typename MT,
bool SO >
317 bool isLower(
const SparseMatrix<MT,SO>& sm );
319 template<
bool RF,
typename MT,
bool SO >
320 bool isUniLower(
const SparseMatrix<MT,SO>& sm );
322 template<
bool RF,
typename MT,
bool SO >
325 template<
bool RF,
typename MT,
bool SO >
326 bool isUpper(
const SparseMatrix<MT,SO>& sm );
328 template<
bool RF,
typename MT,
bool SO >
329 bool isUniUpper(
const SparseMatrix<MT,SO>& sm );
331 template<
bool RF,
typename MT,
bool SO >
334 template<
bool RF,
typename MT,
bool SO >
335 bool isDiagonal(
const SparseMatrix<MT,SO>& sm );
337 template<
bool RF,
typename MT,
bool SO >
338 bool isIdentity(
const SparseMatrix<MT,SO>& sm );
363 template<
typename MT
370 for(
size_t i=0UL; i<A.rows(); ++i ) {
371 for(
auto element=A.begin(i); element!=A.end(i); ++element )
372 if(
isnan( element->value() ) )
return true;
376 for(
size_t j=0UL; j<A.columns(); ++j ) {
377 for(
auto element=A.begin(j); element!=A.end(j); ++element )
378 if(
isnan( element->value() ) )
return true;
430 if( IsSymmetric_v<MT> )
436 if( IsUniform_v<MT> || (~sm).
rows() < 2UL )
442 for(
size_t i=0UL; i<A.rows(); ++i ) {
443 for(
auto element=A.begin(i); element!=A.end(i); ++element )
445 const size_t j( element->index() );
447 if( i == j || isDefault<RF>( element->value() ) )
450 const auto pos( A.find( j, i ) );
451 if( pos == A.end(j) || !equal<RF>( pos->value(), element->value() ) )
457 for(
size_t j=0UL; j<A.columns(); ++j ) {
458 for(
auto element=A.begin(j); element!=A.end(j); ++element )
460 const size_t i( element->index() );
462 if( j == i || isDefault<RF>( element->value() ) )
465 const auto pos( A.find( j, i ) );
466 if( pos == A.end(i) || !equal<RF>( pos->value(), element->value() ) )
523 if( IsHermitian_v<MT> )
526 if( !IsNumeric_v<ET> || !
isSquare( ~sm ) )
529 if( IsBuiltin_v<ET> && IsUniform_v<MT> )
535 for(
size_t i=0UL; i<A.rows(); ++i ) {
536 for(
auto element=A.begin(i); element!=A.end(i); ++element )
538 const size_t j( element->index() );
540 if( isDefault<RF>( element->value() ) )
543 if( i == j && !isReal<RF>( element->value() ) )
546 const auto pos( A.find( j, i ) );
547 if( pos == A.end(j) || !equal<RF>( pos->value(),
conj( element->value() ) ) )
553 for(
size_t j=0UL; j<A.columns(); ++j ) {
554 for(
auto element=A.begin(j); element!=A.end(j); ++element )
556 const size_t i( element->index() );
558 if( isDefault<RF>( element->value() ) )
561 if( j == i && !isReal<RF>( element->value() ) )
564 const auto pos( A.find( j, i ) );
565 if( pos == A.end(i) || !equal<RF>( pos->value(),
conj( element->value() ) ) )
586 bool isUniform_backend(
const SparseMatrix<MT,false>& sm,
TrueType )
594 const size_t ibegin( ( IsStrictlyLower_v<MT> )?( 1UL ):( 0UL ) );
595 const size_t iend ( ( IsStrictlyUpper_v<MT> )?( (~sm).
rows()-1UL ):( (~sm).
rows() ) );
597 for(
size_t i=ibegin; i<iend; ++i ) {
598 for(
auto element=(~sm).
begin(i); element!=(~sm).
end(i); ++element ) {
599 if( !isDefault<RF>( element->value() ) )
620 bool isUniform_backend(
const SparseMatrix<MT,true>& sm,
TrueType )
628 const size_t jbegin( ( IsStrictlyUpper_v<MT> )?( 1UL ):( 0UL ) );
629 const size_t jend ( ( IsStrictlyLower_v<MT> )?( (~sm).
columns()-1UL ):( (~sm).
columns() ) );
631 for(
size_t j=jbegin; j<jend; ++j ) {
632 for(
auto element=(~sm).
begin(j); element!=(~sm).
end(j); ++element ) {
633 if( !isDefault<RF>( element->value() ) )
654 bool isUniform_backend(
const SparseMatrix<MT,false>& sm,
FalseType )
662 const size_t maxElements( (~sm).
rows() * (~sm).
columns() );
664 if( (~sm).
nonZeros() != maxElements )
666 for(
size_t i=0UL; i<(~sm).
rows(); ++i ) {
667 for(
auto element=(~sm).begin(i); element!=(~sm).
end(i); ++element ) {
668 if( !isDefault<RF>( element->value() ) )
677 const auto& cmp( (~sm)(0UL,0UL) );
679 for(
size_t i=0UL; i<(~sm).
rows(); ++i ) {
680 for(
auto element=(~sm).begin(i); element!=(~sm).
end(i); ++element ) {
681 if( !equal<RF>( element->value(), cmp ) )
703 bool isUniform_backend(
const SparseMatrix<MT,true>& sm,
FalseType )
711 const size_t maxElements( (~sm).
rows() * (~sm).
columns() );
713 if( (~sm).
nonZeros() != maxElements )
715 for(
size_t j=0UL; j<(~sm).
columns(); ++j ) {
716 for(
auto element=(~sm).begin(j); element!=(~sm).
end(j); ++element ) {
717 if( !isDefault<RF>( element->value() ) )
726 const auto& cmp( (~sm)(0UL,0UL) );
728 for(
size_t j=0UL; j<(~sm).
columns(); ++j ) {
729 for(
auto element=(~sm).begin(j); element!=(~sm).
end(j); ++element ) {
730 if( !equal<RF>( element->value(), cmp ) )
780 if( IsUniform_v<MT> ||
782 ( (~sm).
rows() == 1UL && (~sm).
columns() == 1UL ) )
785 if( IsUniTriangular_v<MT> )
833 const size_t M( (~sm).
rows() );
834 const size_t N( (~sm).
columns() );
836 if( IsZero_v<MT> || M == 0UL || N == 0UL )
839 if( IsUniTriangular_v<MT> )
844 const size_t iend( SO ==
rowMajor ? A.rows() : A.columns() );
846 for(
size_t i=0UL; i<iend; ++i ) {
847 for(
auto element=A.begin(i); element!=A.end(i); ++element ) {
848 if( !isZero<RF>( element->value() ) ) {
918 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
924 for(
size_t i=0UL; i<A.rows()-1UL; ++i ) {
925 for(
auto element=A.lowerBound(i,i+1UL); element!=A.end(i); ++element )
927 if( !isDefault<RF>( element->value() ) )
933 for(
size_t j=1UL; j<A.columns(); ++j ) {
934 for(
auto element=A.begin(j); element!=A.end(j); ++element )
936 if( element->index() >= j )
939 if( !isDefault<RF>( element->value() ) )
1002 if( IsUniLower_v<MT> )
1011 for(
size_t i=0UL; i<A.rows(); ++i )
1013 auto element( A.lowerBound(i,i) );
1015 if( element == A.end(i) || element->index() != i || !isOne<RF>( element->value() ) )
1020 for( ; element!=A.end(i); ++element ) {
1021 if( !isZero<RF>( element->value() ) )
1027 for(
size_t j=0UL; j<A.columns(); ++j )
1029 bool hasDiagonalElement(
false );
1031 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1033 if( element->index() >= j ) {
1034 if( element->index() != j || !isOne<RF>( element->value() ) )
1036 hasDiagonalElement =
true;
1040 if( !isZero<RF>( element->value() ) )
1044 if( !hasDiagonalElement ) {
1108 if( IsStrictlyLower_v<MT> )
1114 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1117 if( IsUniLower_v<MT> || IsUniUpper_v<MT> )
1123 for(
size_t i=0UL; i<A.rows(); ++i ) {
1124 for(
auto element=A.lowerBound(i,i); element!=A.end(i); ++element )
1126 if( !isDefault<RF>( element->value() ) )
1132 for(
size_t j=0UL; j<A.columns(); ++j ) {
1133 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1135 if( element->index() > j )
1138 if( !isDefault<RF>( element->value() ) )
1208 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1214 for(
size_t i=1UL; i<A.rows(); ++i ) {
1215 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1217 if( element->index() >= i )
1220 if( !isDefault<RF>( element->value() ) )
1226 for(
size_t j=0UL; j<A.columns()-1UL; ++j ) {
1227 for(
auto element=A.lowerBound(j+1UL,j); element!=A.end(j); ++element )
1229 if( !isDefault<RF>( element->value() ) )
1292 if( IsUniUpper_v<MT> )
1301 for(
size_t i=0UL; i<A.rows(); ++i )
1303 bool hasDiagonalElement(
false );
1305 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1307 if( element->index() >= i ) {
1308 if( element->index() != i || !isOne<RF>( element->value() ) )
1310 hasDiagonalElement =
true;
1313 else if( !isZero<RF>( element->value() ) ) {
1318 if( !hasDiagonalElement ) {
1324 for(
size_t j=0UL; j<A.columns(); ++j )
1326 auto element( A.lowerBound(j,j) );
1328 if( element == A.end(j) || element->index() != j || !isOne<RF>( element->value() ) )
1333 for( ; element!=A.end(j); ++element ) {
1334 if( !isZero<RF>( element->value() ) )
1398 if( IsStrictlyUpper_v<MT> )
1404 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1407 if( IsUniLower_v<MT> || IsUniUpper_v<MT> )
1413 for(
size_t i=0UL; i<A.rows(); ++i ) {
1414 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1416 if( element->index() > i )
1419 if( !isDefault<RF>( element->value() ) )
1425 for(
size_t j=0UL; j<A.columns(); ++j ) {
1426 for(
auto element=A.lowerBound(j,j); element!=A.end(j); ++element )
1428 if( !isDefault<RF>( element->value() ) )
1492 if( IsDiagonal_v<MT> )
1498 if( IsZero_v<MT> || (~sm).
rows() < 2UL )
1504 for(
size_t i=0UL; i<A.rows(); ++i ) {
1505 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1506 if( element->index() != i && !isDefault<RF>( element->value() ) )
1511 for(
size_t j=0UL; j<A.columns(); ++j ) {
1512 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1513 if( element->index() != j && !isDefault<RF>( element->value() ) )
1576 if( IsIdentity_v<MT> )
1585 for(
size_t i=0UL; i<A.rows(); ++i )
1587 bool hasDiagonalElement(
false );
1589 for(
auto element=A.begin(i); element!=A.end(i); ++element )
1591 if( element->index() == i ) {
1592 if( !isOne<RF>( element->value() ) )
1594 hasDiagonalElement =
true;
1596 else if( !isZero<RF>( element->value() ) ) {
1601 if( !hasDiagonalElement ) {
1607 for(
size_t j=0UL; j<A.columns(); ++j )
1609 bool hasDiagonalElement(
false );
1611 for(
auto element=A.begin(j); element!=A.end(j); ++element )
1613 if( element->index() == j ) {
1614 if( !isOne<RF>( element->value() ) )
1616 hasDiagonalElement =
true;
1618 else if( !isZero<RF>( element->value() ) ) {
1623 if( !hasDiagonalElement ) {
1648 template<
typename MT
1650 ,
typename... Args >
1651 auto erase( SparseMatrix<MT,SO>& sm, Args&&... args )
1652 -> decltype( (~sm).erase( std::forward<Args>( args )... ) )
1654 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: FalseType.h:61
#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:1004
bool isUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is an upper triangular matrix.
Definition: DenseMatrix.h:1271
bool isStrictlyLower(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a strictly lower triangular matrix.
Definition: DenseMatrix.h:1179
#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
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:354
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 declaration for the If class template.The If_t alias declaration provides a convenien...
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 FalseType type/value trait base class.
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:591
bool isUniLower(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a lower unitriangular matrix.
Definition: DenseMatrix.h:1092
#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:1539
size_t nonZeros(const Matrix< MT, SO > &matrix)
Returns the total number of non-zero elements in the matrix.
Definition: Matrix.h:584
Header file for the decltype(auto) workaround.
BoolConstant< true > TrueType
Type traits base class.The TrueType class is used as base class for type traits and value traits that...
Definition: TrueType.h:61
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:137
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:1644
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:673
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.
Header file for the IsStrictlyUpper type trait.
Header file for the IsSymmetric type trait.
constexpr bool IsResizable_v
Auxiliary variable template for the IsResizable type trait.The IsResizable_v variable template provid...
Definition: IsResizable.h:134
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:713
bool isUniform(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a uniform matrix.
Definition: DenseMatrix.h:849
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.
bool isStrictlyUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is a strictly upper triangular matrix.
Definition: DenseMatrix.h:1446
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:617
Header file for the isDefault shim.
bool isSymmetric(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is symmetric.
Definition: DenseMatrix.h:539
#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.
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:290
bool isUniUpper(const DenseMatrix< MT, SO > &dm)
Checks if the given dense matrix is an upper unitriangular matrix.
Definition: DenseMatrix.h:1359
bool isIntact(const DiagonalMatrix< MT, SO, DF > &m)
Returns whether the invariants of the given diagonal matrix are intact.
Definition: DiagonalMatrix.h:263
Header file for the IsComplex type trait.
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:1326
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, the program execution is terminated. The BLAZE_INTERNAL_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERTION flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:101
Header file for the TrueType type/value trait base class.
Header file for the IsExpression type trait class.