5#ifndef GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
6#define GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
14#include <ginkgo/core/base/dim.hpp>
15#include <ginkgo/core/base/math.hpp>
16#include <ginkgo/core/base/range.hpp>
17#include <ginkgo/core/base/range_accessors.hpp>
18#include <ginkgo/core/base/types.hpp>
19#include <ginkgo/core/base/utils.hpp>
29template <
typename ValueType,
typename IndexType>
37template <
typename ValueType,
typename Distribution,
typename Generator>
38typename std::enable_if<!is_complex_s<ValueType>::value, ValueType>::type
39get_rand_value(Distribution&& dist, Generator&& gen)
45template <
typename ValueType,
typename Distribution,
typename Generator>
46typename std::enable_if<is_complex_s<ValueType>::value, ValueType>::type
47get_rand_value(Distribution&& dist, Generator&& gen)
49 return ValueType(dist(gen), dist(gen));
59template <
typename ValueType,
typename IndexType>
60struct matrix_data_entry {
61 using value_type = ValueType;
62 using index_type = IndexType;
63 matrix_data_entry() =
default;
65 GKO_ATTRIBUTES matrix_data_entry(index_type r, index_type c, value_type v)
66 : row(r), column(c), value(v)
69 bool operator==(
const matrix_data_entry& other)
const
71 return std::tie(this->row, this->column, this->value) ==
72 std::tie(other.row, other.column, other.value);
74 bool operator!=(
const matrix_data_entry& other)
const
76 return std::tie(this->row, this->column, this->value) !=
77 std::tie(other.row, other.column, other.value);
80#define GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(_op) \
81 bool operator _op(const matrix_data_entry& other) const \
83 return std::tie(this->row, this->column) \
84 _op std::tie(other.row, other.column); \
87 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<);
88 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>);
89 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<=);
90 GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>=);
92#undef GKO_DEFINE_DEFAULT_COMPARE_OPERATOR
94 friend std::ostream& operator<<(std::ostream& os,
95 const matrix_data_entry& x)
97 os <<
'(' << x.row <<
',' << x.column <<
',' << x.value <<
')';
125template <
typename ValueType = default_precision,
typename IndexType =
int32>
127 using value_type = ValueType;
128 using index_type = IndexType;
146 nonzeros.emplace_back(row, col, value);
161 template <
typename RandomDistribution,
typename RandomEngine>
169 detail::get_rand_value<ValueType>(dist, engine);
171 nonzeros.emplace_back(row, col, value);
182 matrix_data(std::initializer_list<std::initializer_list<ValueType>> values)
185 for (
size_type row = 0; row < values.size(); ++row) {
186 const auto row_data = begin(values)[row];
187 size[1] = std::max(
size[1], row_data.size());
188 for (
size_type col = 0; col < row_data.size(); ++col) {
189 const auto& val = begin(row_data)[col];
191 nonzeros.emplace_back(row, col, val);
205 std::initializer_list<detail::input_triple<ValueType, IndexType>>
210 for (
const auto& elem : nonzeros_) {
211 nonzeros.emplace_back(elem.row, elem.col, elem.val);
225 for (
size_type row = 0; row < size_[0]; ++row) {
226 for (
size_type col = 0; col < size_[1]; ++col) {
227 for (
const auto& elem : block.
nonzeros) {
229 col * block.
size[1] + elem.column,
244 template <
typename Accessor>
246 :
size{data.length(0), data.length(1)}
251 nonzeros.emplace_back(row, col, data(row, col));
269 const auto num_nnz = std::min(size_[0], size_[1]);
271 for (
size_type i = 0; i < num_nnz; ++i) {
272 res.
nonzeros.emplace_back(i, i, value);
287 std::initializer_list<ValueType> nonzeros_)
290 res.
nonzeros.reserve(nonzeros_.size());
292 for (
auto value : nonzeros_) {
293 res.
nonzeros.emplace_back(pos, pos, value);
310 const auto num_blocks = std::min(size_[0], size_[1]);
312 for (
size_type b = 0; b < num_blocks; ++b) {
313 for (
const auto& elem : block.
nonzeros) {
315 b * block.
size[1] + elem.column,
333 template <
typename ForwardIterator>
338 return dim<2>{s[0] + d.size[0], s[1] + d.size[1]};
343 for (
auto it = begin; it != end; ++it) {
344 for (
const auto& elem : it->nonzeros) {
345 res.nonzeros.emplace_back(row_offset + elem.row,
346 col_offset + elem.column, elem.value);
348 row_offset += it->size[0];
349 col_offset += it->size[1];
365 return diag(begin(blocks), end(blocks));
387 template <
typename RandomDistribution,
typename RandomEngine>
390 RandomDistribution&& dist, RandomEngine&& engine,
395 std::vector<ValueType> ref_data(
size);
396 std::vector<ValueType> work(
size);
398 range reflector(ref_data.data(),
size, 1u, 1u);
400 initialize_diag_with_cond(condition_number,
matrix);
401 for (
size_type i = 0; i < num_reflectors; ++i) {
402 generate_random_reflector(dist, engine, reflector);
403 reflect_domain(reflector,
matrix, work.data());
404 generate_random_reflector(dist, engine, reflector);
405 reflect_range(reflector,
matrix, work.data());
431 template <
typename RandomDistribution,
typename RandomEngine>
434 RandomDistribution&& dist, RandomEngine&& engine)
437 std::forward<RandomDistribution>(dist),
438 std::forward<RandomEngine>(engine),
size - 1);
462 return std::tie(x.row, x.column) < std::tie(y.row, y.column);
481 [](nonzero_type nz) {
return is_zero(nz.value); }),
492 std::vector<nonzero_type> new_nonzeros;
494 new_nonzeros.emplace_back(
nonzeros.front().row,
498 if (entry.row != new_nonzeros.back().row ||
499 entry.column != new_nonzeros.back().column) {
500 new_nonzeros.emplace_back(entry.row, entry.column,
503 new_nonzeros.back().value += entry.value;
510 template <
typename Accessor>
511 static void initialize_diag_with_cond(
517 const auto min_sigma =
one(condition_number) / sqrt(condition_number);
518 const auto max_sigma = sqrt(condition_number);
522 matrix(i, i) = max_sigma *
static_cast<sigma_type
>(
size - i - 1) /
523 static_cast<sigma_type
>(
size - 1) +
524 min_sigma *
static_cast<sigma_type
>(i) /
525 static_cast<sigma_type
>(
size - 1);
529 template <
typename RandomDistribution,
typename RandomEngine,
531 static void generate_random_reflector(RandomDistribution&& dist,
532 RandomEngine&& engine,
533 const range<Accessor>& reflector)
536 reflector(i, 0) = detail::get_rand_value<ValueType>(dist, engine);
540 template <
typename Accessor>
541 static void reflect_domain(
const range<Accessor>& reflector,
542 const range<Accessor>& matrix,
543 ValueType* work_data)
546 range<accessor::row_major<ValueType, 2>> work(work_data,
547 matrix.length(0), 1u, 1u);
548 work = mmul(matrix, reflector);
550 const auto scale = two / mmul(ct_reflector, reflector)(0, 0);
551 matrix = matrix - scale * mmul(work, ct_reflector);
554 template <
typename Accessor>
555 static void reflect_range(
const range<Accessor>& reflector,
556 const range<Accessor>& matrix,
557 ValueType* work_data)
560 range<accessor::row_major<ValueType, 2>> work(
561 work_data, 1u, matrix.length(0), matrix.length(0));
563 work = mmul(ct_reflector, matrix);
564 const auto scale = two / mmul(ct_reflector, reflector)(0, 0);
565 matrix = matrix - scale * mmul(reflector, work);
A range is a multidimensional view of the memory.
Definition range.hpp:297
The matrix namespace.
Definition dense_cache.hpp:15
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:630
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:260
constexpr T zero()
Returns the additive identity for T.
Definition math.hpp:602
constexpr bool is_zero(T value)
Returns true if and only if the given value is zero.
Definition math.hpp:668
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
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:119
constexpr auto conj(const T &x)
Returns the conjugate of an object.
Definition math.hpp:899
constexpr bool is_nonzero(T value)
Returns true if and only if the given value is not zero.
Definition math.hpp:683
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
Type used to store nonzeros.
Definition matrix_data.hpp:60
static matrix_data diag(dim< 2 > size_, const matrix_data &block)
Initializes a block-diagonal matrix.
Definition matrix_data.hpp:307
static matrix_data diag(dim< 2 > size_, ValueType value)
Initializes a diagonal matrix.
Definition matrix_data.hpp:265
dim< 2 > size
Size of the matrix.
Definition matrix_data.hpp:444
matrix_data(const range< Accessor > &data)
Initializes a matrix from a range.
Definition matrix_data.hpp:245
matrix_data(dim< 2 > size_=dim< 2 >{}, ValueType value=zero< ValueType >())
Initializes a matrix filled with the specified value.
Definition matrix_data.hpp:137
std::vector< nonzero_type > nonzeros
A vector of tuples storing the non-zeros of the matrix.
Definition matrix_data.hpp:453
void remove_zeros()
Remove entries with value zero from the matrix data.
Definition matrix_data.hpp:477
static matrix_data diag(ForwardIterator begin, ForwardIterator end)
Initializes a block-diagonal matrix from a list of diagonal blocks.
Definition matrix_data.hpp:334
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:388
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:363
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:286
matrix_data(dim< 2 > size_, const matrix_data &block)
Initializes a matrix out of a matrix block via duplication.
Definition matrix_data.hpp:221
void sum_duplicates()
Sum up all values that refer to the same matrix entry.
Definition matrix_data.hpp:489
void sort_row_major()
Sorts the nonzero vector so the values follow row-major order.
Definition matrix_data.hpp:458
matrix_data(std::initializer_list< std::initializer_list< ValueType > > values)
List-initializes the structure from a matrix of values.
Definition matrix_data.hpp:182
void ensure_row_major_order()
Definition matrix_data.hpp:469
matrix_data(dim< 2 > size_, RandomDistribution &&dist, RandomEngine &&engine)
Initializes a matrix with random values from the specified distribution.
Definition matrix_data.hpp:162
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:203
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:432