33#ifndef GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
34#define GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
43#include <ginkgo/core/base/dim.hpp>
44#include <ginkgo/core/base/math.hpp>
45#include <ginkgo/core/base/range.hpp>
46#include <ginkgo/core/base/range_accessors.hpp>
47#include <ginkgo/core/base/types.hpp>
48#include <ginkgo/core/base/utils.hpp>
58template <
typename ValueType,
typename IndexType>
66template <
typename ValueType,
typename Distribution,
typename Generator>
67typename std::enable_if<!is_complex_s<ValueType>::value, ValueType>::type
74template <
typename ValueType,
typename Distribution,
typename Generator>
75typename std::enable_if<is_complex_s<ValueType>::value, ValueType>::type
88template <
typename ValueType,
typename IndexType>
90 using value_type = ValueType;
91 using index_type = IndexType;
95 : row(r), column(c), value(v)
100 return std::tie(this->row, this->column, this->value) ==
105 return std::tie(this->row, this->column, this->value) !=
109#define GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(_op) \
110 bool operator _op(const matrix_data_entry& other) const \
112 return std::tie(this->row, this->column) \
113 _op std::tie(other.row, other.column); \
116 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<);
117 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>);
118 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<=);
119 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>=);
121#undef GKO_DEFINE_DEFAULT_COMPARE_OPERATOR
123 friend std::ostream& operator<<(std::ostream&
os,
126 os <<
'(' << x.row <<
',' << x.column <<
',' << x.value <<
')';
154template <
typename ValueType = default_precision,
typename IndexType =
int32>
156 using value_type = ValueType;
157 using index_type = IndexType;
175 nonzeros.emplace_back(row, col, value);
190 template <
typename RandomDistribution,
typename RandomEngine>
198 detail::get_rand_value<ValueType>(
dist,
engine);
200 nonzeros.emplace_back(row, col, value);
211 matrix_data(std::initializer_list<std::initializer_list<ValueType>> values)
214 for (
size_type row = 0; row < values.size(); ++row) {
215 const auto row_data = begin(values)[row];
218 const auto& val = begin(
row_data)[col];
220 nonzeros.emplace_back(row, col, val);
234 std::initializer_list<detail::input_triple<ValueType, IndexType>>
239 for (
const auto&
elem : nonzeros_) {
253 nonzeros.reserve(size_[0] * size_[1] *
block.nonzeros.size());
254 for (
size_type row = 0; row < size_[0]; ++row) {
255 for (
size_type col = 0; col < size_[1]; ++col) {
256 for (
const auto&
elem :
block.nonzeros) {
273 template <
typename Accessor>
275 :
size{data.length(0), data.length(1)}
280 nonzeros.emplace_back(row, col, data(row, col));
298 const auto num_nnz = std::min(size_[0], size_[1]);
301 res.nonzeros.emplace_back(
i,
i, value);
316 std::initializer_list<ValueType> nonzeros_)
319 res.nonzeros.reserve(nonzeros_.size());
321 for (
auto value : nonzeros_) {
322 res.nonzeros.emplace_back(
pos,
pos, value);
339 const auto num_blocks = std::min(size_[0], size_[1]);
342 for (
const auto&
elem :
block.nonzeros) {
362 template <
typename ForwardIterator>
367 return dim<2>{s[0] +
d.size[0], s[1] +
d.size[1]};
372 for (
auto it = begin;
it != end; ++
it) {
373 for (
const auto&
elem :
it->nonzeros) {
416 template <
typename RandomDistribution,
typename RandomEngine>
460 template <
typename RandomDistribution,
typename RandomEngine>
466 std::forward<RandomDistribution>(
dist),
467 std::forward<RandomEngine>(
engine),
size - 1);
491 return std::tie(x.row, x.column) < std::tie(y.row, y.column);
531 template <
typename Accessor>
532 static void initialize_diag_with_cond(
541 matrix =
zero(matrix);
561 template <
typename Accessor>
568 matrix.length(0), 1u, 1u);
575 template <
typename Accessor>
582 work_data, 1u, matrix.length(0), matrix.length(0));
A range is a multidimensional view of the memory.
Definition range.hpp:326
constexpr size_type length(size_type dimension) const
Returns the length of the specified dimension of the range.
Definition range.hpp:429
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:354
constexpr T zero()
Returns the additive identity for T.
Definition math.hpp:775
constexpr bool is_zero(T value)
Returns true if and only if the given value is zero.
Definition math.hpp:840
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition batch_dim.hpp:148
constexpr auto conj(const T &x)
Returns the conjugate of an object.
Definition math.hpp:1071
constexpr bool is_nonzero(T value)
Returns true if and only if the given value is not zero.
Definition math.hpp:855
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
Type used to store nonzeros.
Definition matrix_data.hpp:89
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155
static matrix_data diag(dim< 2 > size_, const matrix_data &block)
Initializes a block-diagonal matrix.
Definition matrix_data.hpp:336
static matrix_data diag(dim< 2 > size_, ValueType value)
Initializes a diagonal matrix.
Definition matrix_data.hpp:294
dim< 2 > size
Size of the matrix.
Definition matrix_data.hpp:473
matrix_data(const range< Accessor > &data)
Initializes a matrix from a range.
Definition matrix_data.hpp:274
matrix_data(dim< 2 > size_=dim< 2 >{}, ValueType value=zero< ValueType >())
Initializes a matrix filled with the specified value.
Definition matrix_data.hpp:166
std::vector< nonzero_type > nonzeros
A vector of tuples storing the non-zeros of the matrix.
Definition matrix_data.hpp:482
void remove_zeros()
Remove entries with value zero from the matrix data.
Definition matrix_data.hpp:498
static matrix_data diag(ForwardIterator begin, ForwardIterator end)
Initializes a block-diagonal matrix from a list of diagonal blocks.
Definition matrix_data.hpp:363
static matrix_data cond(size_type size, remove_complex< ValueType > condition_number, RandomDistribution &&dist, RandomEngine &&engine, size_type num_reflectors)
Initializes a random dense matrix with a specific condition number.
Definition matrix_data.hpp:417
static matrix_data diag(std::initializer_list< matrix_data > blocks)
Initializes a block-diagonal matrix from a list of diagonal blocks.
Definition matrix_data.hpp:392
static matrix_data diag(dim< 2 > size_, std::initializer_list< ValueType > nonzeros_)
Initializes a diagonal matrix using a list of diagonal elements.
Definition matrix_data.hpp:315
matrix_data(dim< 2 > size_, const matrix_data &block)
Initializes a matrix out of a matrix block via duplication.
Definition matrix_data.hpp:250
void sum_duplicates()
Sum up all values that refer to the same matrix entry.
Definition matrix_data.hpp:510
matrix_data(std::initializer_list< std::initializer_list< ValueType > > values)
List-initializes the structure from a matrix of values.
Definition matrix_data.hpp:211
void ensure_row_major_order()
Sorts the nonzero vector so the values follow row-major order.
Definition matrix_data.hpp:487
matrix_data(dim< 2 > size_, RandomDistribution &&dist, RandomEngine &&engine)
Initializes a matrix with random values from the specified distribution.
Definition matrix_data.hpp:191
matrix_data(dim< 2 > size_, std::initializer_list< detail::input_triple< ValueType, IndexType > > nonzeros_)
Initializes the structure from a list of nonzeros.
Definition matrix_data.hpp:232
static matrix_data cond(size_type size, remove_complex< ValueType > condition_number, RandomDistribution &&dist, RandomEngine &&engine)
Initializes a random dense matrix with a specific condition number.
Definition matrix_data.hpp:461