47# include <type_traits>
67 template <
typename Trow =
int,
typename Tcol =
int,
typename NumType =
double>
68 requires AlDomainKey<Trow>
and AlDomainKey<Tcol>
and AlNumber<NumType>
77 using RCPair = std::pair<Trow, Tcol>;
93 bool is_zero(
const NumType & val)
const noexcept
102 static std::shared_ptr<D>
borrow(
const D & d)
104 return std::shared_ptr<D>(&
const_cast<D &
>(d), [](D *) {});
109 static std::shared_ptr<D>
own(D && d)
111 return std::make_shared<D>(std::move(d));
125 <<
"epsilon must be greater or equal to zero";
130 typedef std::pair<RCPair, NumType>
Pair;
206 template <
typename R,
typename C,
typename... Rest>
207 requires (std::is_integral_v<std::remove_cvref_t<R>> or
208 std::is_integral_v<std::remove_cvref_t<C>>)
219 std::initializer_list<std::initializer_list<NumType>>
l,
222 :
Matrix(std::move(rdomain), std::move(cdomain), e)
232 <<
"mismatch with number of columns";
233 auto itc = itr->begin();
236 const NumType & val = *itc++;
254 std::initializer_list<std::initializer_list<NumType>>
l,
270 std::initializer_list<std::initializer_list<NumType>>
l,
273 :
Matrix(
own(std::move(rdomain)),
own(std::move(cdomain)),
l, e)
278 std::initializer_list<std::initializer_list<NumType>>
l,
286 std::initializer_list<std::initializer_list<NumType>>
l,
303 :
Matrix(std::move(rdomain), std::move(cdomain), e)
309 auto t = it.get_curr();
310 const Trow & row_idx = get<0>(t);
313 <<
"mismatch with number of columns";
316 auto t = it.get_curr();
317 const NumType & val = get<1>(t);
320 const Tcol & col_idx = get<0>(t);
353 :
Matrix(
own(std::move(rdomain)),
own(std::move(cdomain)),
l, e)
386 entries(std::move(other.entries)),
412 entries = std::move(other.entries);
421 template <
class Vec,
class D>
426 << name <<
"(): " << vectors.
size() <<
" vectors for "
427 << domain.size() <<
" elements of the domain";
428 const auto & first = vectors.
get_first().get_domain_ptr();
431 return v.get_domain_ptr() == first;
433 << name <<
"(): the vectors have different domains";
442 Matrix ret(rdomain,
rows.get_first().get_domain_ptr(), e);
444 for (
auto it =
zip_it(rdomain->keys(),
rows); it.has_curr(); it.next())
446 auto t = it.get_curr();
447 const Trow & row_idx = get<0>(t);
449 for (
auto it_col =
row.get_it(); it_col.has_curr(); it_col.next())
451 auto & p = it_col.get_curr();
452 const NumType & val = p.second;
455 const Tcol & col_idx = p.first;
469 Matrix ret(
cols.get_first().get_domain_ptr(), cdomain, e);
471 for (
auto it =
zip_it(cdomain->keys(),
cols); it.has_curr(); it.next())
473 auto t = it.get_curr();
474 const Tcol & col_idx = get<0>(t);
476 for (
auto it_col =
col.get_it(); it_col.has_curr(); it_col.next())
478 auto & p = it_col.get_curr();
479 const NumType & val = p.second;
482 const Trow & row_idx = p.first;
624 const RCPair & rc = p.first;
625 const NumType & val = p.second;
626 ret_val.
set_entry(rc.second, rc.first, val);
655 template maps<NumType>([
this, &
row](
const Tcol &
col)
684 cols.for_each([
this, &vec, &
row](
const Tcol &
col)
688 if (val_ptr !=
nullptr)
706 rows.for_each([
this, &vec, &
col](
const Trow &
row)
721 <<
"get_row_vector(): invalid row";
736 <<
"get_col_vector(): invalid col";
760 <<
"mult_matrix_vector_linear_comb: domain mismatch";
762 vec.for_each([
this, &ret_val](
const std::pair<Tcol, NumType> & p)
764 const Tcol &
col = p.first;
765 const NumType & val = p.second;
784 <<
"mult_matrix_vector_sparse: domain mismatch";
788 const Trow &
row = p.first.first;
789 const Tcol &
col = p.first.second;
790 const NumType & entry_val = p.second;
791 const NumType *vec_entry = vec.search_entry(
col);
792 if (vec_entry !=
nullptr)
794 entry_val * *vec_entry);
813 <<
"mult_vector_matrix_linear_comb: domain mismatch";
815 vec.for_each([
this, &ret_val](
const std::pair<Trow, NumType> & p)
817 const Trow &
row = p.first;
818 const NumType & val = p.second;
837 <<
"mult_matrix_vector_dot_product: domain mismatch";
859 <<
"mult_vector_matrix_dot_product: domain mismatch";
885 template <
typename T2row,
typename T2col>
889 static_assert(std::is_same_v<T2row, Tcol>,
890 "AxB: the rows of B must be of the type of the columns of A");
892 <<
"AxB: Column domain of A must be identical to row domain of B";
905 <<
"set_vector_as_row: domain vec mismatch";
906 vec.
for_each([
this, &
row](
const std::pair<Tcol, NumType> & p)
908 const Tcol &
col = p.first;
909 const NumType & val = p.second;
924 <<
"set_vector_as_col: domain vec mismatch";
925 vec.
for_each([
this, &
col](
const std::pair<Trow, NumType> & p)
927 const Trow &
row = p.first;
928 const NumType & val = p.second;
942 static_assert(std::is_same_v<Trow, Tcol>,
943 "Row domain is not equal to column domain");
962 <<
"Matrix addition domain mismatch";
965 const Trow &
row = p.first.first;
966 const Tcol &
col = p.first.second;
968 auto *ptr =
entries.search(rcp);
972 ptr->second += p.second;
995 <<
"Matrix subtraction domain mismatch";
998 const Trow &
row = p.first.first;
999 const Tcol &
col = p.first.second;
1001 auto *ptr =
entries.search(rcp);
1003 entries.insert(rcp, -p.second);
1005 ptr->second -= p.second;
1028 NumType & val =
const_cast<Pair &
>(p).second;
1041 template <
typename T2row,
typename T2col>
1063 template <
typename T2row,
typename T2col>
1074 *
this *
m.get_col_vector(
col));
1083 std::cout <<
"Non zero entries = " <<
'\n';
1098 typedef std::pair<std::string, std::string> StrPair;
1102 template maps<std::string>([](
const ColVector & vec)
1105 template foldl<std::string>
1106 (
"", [](
const std::string & s,
const NumType & val)
1109 return val_str.size() > s.size() ? val_str : s;
1121 template maps<std::string>([](
const StrPair & p)
1123 return p.first.size() > p.second.size() ?
1134 const std::string longer_row_label = row_labels.template foldl<std::string>
1135 (std::string(), [](
const std::string & s,
1136 const std::string & row_label)
1138 return row_label.size() > s.size() ? row_label : s;
1141 size_t longer_row_label_sz = longer_row_label.
size();
1143 std::string ret_val = std::string(longer_row_label_sz,
' ') +
" |";
1144 ret_val =
zip(col_labels, longers).template foldl<std::string>
1145 (ret_val, [](
const std::string & s,
const StrPair & p)
1147 const size_t fsz = p.first.size();
1148 const size_t ssz = p.second.size();
1149 return s + std::string((fsz > ssz ? fsz - ssz : ssz - fsz) + 1,
' ') + p.first;
1152 ret_val +=
"\n" + std::string(ret_val.size(),
'-');
1155 ([
this, &ret_val, &longers, &longer_row_label_sz]
1156 (
const std::pair<std::string, Trow> & p)
1158 const std::string & label = p.first;
1159 const Trow &
row = p.second;
1162 std::string(longer_row_label_sz - label.size(),
' ') + label +
" |";
1165 (ret_val, [](
const std::string & s,
1166 const std::pair<NumType, std::string> & q)
1169 const std::string & len_str = q.second;
1170 assert(len_str.size() - val.size() + 1);
1171 return s + std::string(len_str.size() - val.size() + 1,
' ') + val;
1179 bool are_equal(
const NumType & n1,
const NumType & n2)
const
1194 <<
"invalid matrix's domains";
1197 const Trow &
row = p.first.first;
1198 const Tcol &
col = p.first.second;
1201 m.entries.
all([
this](
const Pair & p)
1203 const Trow &
row = p.first.first;
1204 const Tcol &
col = p.first.second;
1225 template <
typename Trow,
typename Tcol,
typename NumType>
1226 requires AlMultiplicativeNumber<NumType>
1236 template <
typename Trow,
typename Tcol,
typename NumType>
1237 requires AlAdditiveNumber<NumType>
and AlMultiplicativeNumber<NumType>
1242 return m.mult_vector_matrix_linear_comb(vec);
1245 template <
typename Trow,
typename Tcol,
typename NumType>
1246 requires AlDomainKey<Trow>
and AlDomainKey<Tcol>
and AlNumber<NumType>
1250 template <
typename Trow,
typename Tcol,
typename NumType>
1251 requires AlOrderedDomainKey<Trow>
and AlOrderedDomainKey<Tcol>
and
1252 AlStreamable<Trow>
and AlStreamable<Tcol>
and AlStreamable<NumType>
1256 return s <<
m.to_str();
1267 template <
typename Trow,
typename Tcol,
typename NumType =
double>
1268 requires AlMultiplicativeNumber<NumType>
1275 v1.
for_each([&ret_val, &v2](
const std::pair<Trow, NumType> & p1)
1277 v2.
for_each([&ret_val, &p1](
const std::pair<Tcol, NumType> & p2)
1280 p1.second * p2.second);
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Sparse vector with named elements.
Generic domain class based on hash set.
Doubly-linked list (defined in tpl_dynList.H).
T & get_first()
Return the first item of the list.
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
auto for_each(Operation &operation) const -> decltype(self())
Apply an operation to each element (read-only).
Sparse matrix with generic row and column domains.
const NumType & get_epsilon() const noexcept
Return the epsilon value used for zero comparisons.
const CDomainPtr & get_col_domain_ptr() const noexcept
Return the shared pointer to the column domain.
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (shared_ptr version).
DynList< ColVector > to_collist() const and AlOrderedDomainKey< Tcol >
Return a list of vectors corresponding to the columns.
Vector< Trow, NumType > mult_matrix_vector_sparse(const Vector< Tcol, NumType > &vec) const and AlMultiplicativeNumber< NumType >
Matrix-vector multiplication using sparse iteration.
Matrix(const Matrix &other)
Copy constructor.
Matrix & operator+=(const Matrix &m)
Add another matrix to this one.
void validate_domains_for_mult(const Matrix< T2row, T2col, NumType > &m) const
Validate domain compatibility for matrix multiplication.
Matrix(RDomain &&rdomain, const CDomain &cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
This is an overloaded member function, provided for convenience. It differs from the above function o...
static Matrix create_by_columns_impl(CDomainPtr cdomain, const DynList< Vector< Trow, NumType > > &cols, const NumType &e)
Matrix(const RDomain &rdomain, const CDomain &cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (reference version).
Matrix operator-(const Matrix &m) const
Matrix subtraction operator.
static Matrix create_by_rows(RDomain &&rdomain, const DynList< Vector< Tcol, NumType > > &rows, const NumType &e=default_epsilon)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const CDomain & get_col_domain() const noexcept
Return the column domain.
std::string to_str() const and AlOrderedDomainKey< Tcol > and AlStreamable< Trow > and AlStreamable< Tcol > and AlStreamable< NumType >
Convert matrix to a formatted string representation.
std::shared_ptr< RDomain > RDomainPtr
std::shared_ptr< CDomain > CDomainPtr
Vector< Tcol, NumType > get_row_vector(const Trow &row) const
Given a row, build a vector corresponding to the row.
bool are_equal(const NumType &n1, const NumType &n2) const
Check if two values are equal within epsilon tolerance.
Matrix< Trow, T2col, NumType > matrix_vector_mult(const Matrix< T2row, T2col, NumType > &m) const and AlMultiplicativeNumber< NumType >
Matrix multiplication using matrix times column-vector approach.
static std::shared_ptr< D > own(D &&d)
DynList< Tcol > col_domain_list() const
Return a sorted list of column domain keys.
Matrix & mult_by_scalar(const NumType &scalar)
Multiply all entries by a scalar (in-place).
static Matrix create_by_rows(const RDomain &rdomain, const DynList< Vector< Tcol, NumType > > &rows, const NumType &e=default_epsilon)
Create a matrix from a list of row vectors.
Matrix(Matrix &&other) noexcept
Move constructor.
Matrix< Trow, T2col, NumType > vector_matrix_mult(const Matrix< T2row, T2col, NumType > &m) const and AlMultiplicativeNumber< NumType >
Matrix multiplication using row-vector times matrix approach.
static Matrix create_by_columns(const CDomain &cdomain, const DynList< Vector< Trow, NumType > > &cols, const NumType &e=default_epsilon)
Create a matrix from a list of column vectors.
Matrix(const RDomain &rdomain, CDomain &&cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
This is an overloaded member function, provided for convenience. It differs from the above function o...
static const NumType default_epsilon
std::pair< RCPair, NumType > Pair
Type alias for entries stored in the hash table: ((row,col), value).
Vector< Tcol, NumType > mult_vector_matrix_dot_product(const Vector< Trow, NumType > &vec) const and AlMultiplicativeNumber< NumType >
Vector-matrix multiplication using dot products.
Matrix operator+(const Matrix &m) const
Matrix addition operator.
Vector< Trow, NumType > operator*(const Vector< Tcol, NumType > &vec) const and AlMultiplicativeNumber< NumType >
Matrix-vector multiplication operator (uses linear combination method).
bool operator!=(const Matrix &m) const
Inequality operator.
NumType get_entry(const Trow &row, const Tcol &col)
Get an entry value (non-const version with lazy cleanup).
bool equal_to(const Matrix &m) const
Check if this matrix equals another (within epsilon).
Matrix(RDomain &&rdomain, CDomain &&cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a list of lists over domains of which one, or both, are temporary: the matrix...
Matrix(const RDomain &rdomain, CDomain &&cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
This is an overloaded member function, provided for convenience. It differs from the above function o...
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a 2D initializer list (shared_ptr version).
static std::shared_ptr< D > borrow(const D &d)
static Matrix create_by_rows_impl(RDomainPtr rdomain, const DynList< Vector< Tcol, NumType > > &rows, const NumType &e)
DynList< Trow > row_domain_list() const
Return a sorted list of row domain keys.
Vector< Trow, NumType > mult_matrix_vector_dot_product(const Vector< Tcol, NumType > &vec) const and AlMultiplicativeNumber< NumType >
Matrix-vector multiplication using dot products.
Matrix(RDomain &&rdomain, const CDomain &cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynList< NumType > get_row_as_list(const Trow &row) const
Get a row as a list of values (in column order).
Matrix & operator=(const Matrix &other)
Copy assignment operator.
Vector< Trow, NumType > get_col_vector(const Tcol &col) const
Given a column, build a vector corresponding to the column.
bool operator==(const Matrix &m) const
Equality operator (uses equal_to).
NumType get_entry(const Trow &row, const Tcol &col) const noexcept
Get an entry value (const version, no cleanup).
void set_entry(const Trow &row, const Tcol &col, const NumType &val)
Set an entry value.
Matrix(const RDomain &rdomain, CDomain &&cdomain, const NumType &e=default_epsilon)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Matrix & operator-=(const Matrix &m)
Subtract another matrix from this one.
Vector< Trow, NumType > mult_matrix_vector_linear_comb(const Vector< Tcol, NumType > &vec) const and AlMultiplicativeNumber< NumType >
return this*vec in terms of linear combinations
void set_epsilon(const NumType &e)
Set the epsilon value for zero comparisons.
const RDomain & get_row_domain() const noexcept
Return the row domain.
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a list of lists (shared_ptr version).
Matrix identity() const
Create an identity matrix (only for square matrices).
DynList< NumType > get_col_as_list(const Tcol &col) const
Get a column as a list of values (in row order).
bool is_zero(const NumType &val) const noexcept
Matrix< Tcol, Trow, NumType > transpose() const
Compute the transpose of this matrix.
Matrix & set_vector_as_col(const Tcol &col, const Vector< Trow, NumType > &vec)
Set a column from a vector.
Matrix(RDomain &&rdomain, CDomain &&cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a 2D initializer list over domains of which one, or both, are temporary: the ...
Matrix(R &&, C &&, Rest &&...)=delete
A matrix needs domains: Matrix(IntRange(n), IntRange(m)), not Matrix(n, m), which would turn the numb...
Matrix(RDomain &&rdomain, const CDomain &cdomain, const NumType &e=default_epsilon)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Matrix(RDomain &&rdomain, CDomain &&cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over domains of which one, or both, are temporary: the matrix keeps ...
Matrix(const RDomain &rdomain, const CDomain &cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a list of lists (reference version).
void print() const and AlStreamable< Tcol > and AlStreamable< NumType >
Print non-zero entries to stdout.
std::pair< Trow, Tcol > RCPair
Matrix(const RDomain &rdomain, const CDomain &cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon) and AlOrderedDomainKey< Tcol >
Construct a matrix from a 2D initializer list (reference version).
Vector< Tcol, NumType > mult_vector_matrix_linear_comb(const Vector< Trow, NumType > &vec) const and AlMultiplicativeNumber< NumType >
return vec*this in terms of linear combinations
Matrix & set_vector_as_row(const Trow &row, const Vector< Tcol, NumType > &vec)
Set a row from a vector.
static Matrix create_by_columns(CDomain &&cdomain, const DynList< Vector< Trow, NumType > > &cols, const NumType &e=default_epsilon)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static void check_vectors(const D &domain, const DynList< Vec > &vectors, const char *name)
DynList< Vector< Tcol, NumType > > to_rowlist() const and AlOrderedDomainKey< Tcol >
Return a list of vectors corresponding to the rows.
const RDomainPtr & get_row_domain_ptr() const noexcept
Return the shared pointer to the row domain.
Sparse vector implementation with domain-based indexing.
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
void set_entry(const T &i, const NumType &value)
Set a vector entry at given index.
DynList< NumType > to_list() const
The values of the vector, in the order of the sorted keys of its domain.
NumType get_entry(const T &i)
Get vector entry at given index (non-const version)
const DomainPtr & get_domain_ptr() const noexcept
Return the shared pointer to the domain.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
A number that adds and subtracts: what the sums, the differences and the comparisons of vectors and m...
A number that multiplies: what the products by a scalar, and the products of vectors and matrices,...
A key of a domain that < orders: what AlDomain::keys(), and the members that take the keys in their o...
A value that << writes to a stream: what the members that print keys or numbers need.
Main namespace for Aleph-w library functions.
ZipIterator< Cs... > zip_it(const Cs &...cs)
Alias for get_zip_it.
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zip(const Container1 &a, const Container2 &b)
Zip two containers into a list of pairs.
size_t pair_snd_hash_ptr_fct(const std::pair< K1, K2 > &p) noexcept
and
Check uniqueness with explicit hash + equality functors.
Matrix< Trow, Tcol, NumType > outer_product(const Vector< Trow, NumType > &v1, const Vector< Tcol, NumType > &v2)
Compute the outer product of two vectors.
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
size_t pair_dft_hash_ptr_fct(const std::pair< K1, K2 > &p) noexcept
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Aleph::DynList< T > keys() const
See items().
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)