64 template <
typename Trow =
int,
typename Tcol =
int,
typename NumType =
double>
73 using RCPair = std::pair<Trow, Tcol>;
106 <<
"epsilon must be greater or equal to zero";
111 typedef std::pair<RCPair, NumType>
Pair;
164 std::initializer_list<std::initializer_list<NumType>>
l,
176 <<
"mismatch with number of columns";
198 std::initializer_list<std::initializer_list<NumType>>
l,
220 auto t = it.get_curr();
225 auto t = it.get_curr();
309 auto t = it.get_curr();
314 auto & p =
it_col.get_curr();
315 const NumType & val = p.second;
316 if (
ret.is_zero(val))
340 auto t = it.get_curr();
345 auto & p =
it_col.get_curr();
346 const NumType & val = p.second;
347 if (
ret.is_zero(val))
428 const NumType & val = p.second;
507 vec.set_entry(
row, ptr->second);
518 <<
"get_row_vector(): invalid row";
524 ret_val.set_entry(col, ptr->second);
533 <<
"get_col_vector(): invalid col";
556 <<
"mult_matrix_vector_linear_comb: domain mismatch";
560 const Tcol & col = p.first;
561 const NumType & val = p.second;
579 <<
"mult_matrix_vector_sparse: domain mismatch";
583 const Trow &
row = p.first.first;
584 const Tcol & col = p.first.second;
607 <<
"mult_vector_matrix_linear_comb: domain mismatch";
612 const NumType & val = p.second;
630 <<
"mult_matrix_vector_dot_product: domain mismatch";
651 <<
"mult_vector_matrix_dot_product: domain mismatch";
676 template <
typename T2row,
typename T2col>
681 <<
"AxB: Column domain of A must be identical to row domain of B";
694 <<
"set_vector_as_row: domain vec mismatch";
697 const Tcol & col = p.first;
698 const NumType & val = p.second;
713 <<
"set_vector_as_col: domain vec mismatch";
714 vec.
for_each([
this, &col](
const std::pair<Trow, NumType> & p)
717 const NumType & val = p.second;
731 static_assert(std::is_same_v<Trow, Tcol>,
732 "Row domain is not equal to column domain");
750 <<
"Matrix addition domain mismatch";
753 const Trow &
row = p.first.first;
754 const Tcol & col = p.first.second;
760 ptr->second += p.second;
781 <<
"Matrix subtraction domain mismatch";
784 const Trow &
row = p.first.first;
785 const Tcol & col = p.first.second;
791 ptr->second -= p.second;
825 template <
typename T2row,
typename T2col>
846 template <
typename T2row,
typename T2col>
864 std::cout <<
"Non zero entries = " <<
'\n';
877 typedef std::pair<std::string, std::string>
StrPair;
883 return vec.to_list().
885 (
"", [](
const std::string & s,
const NumType & val)
895 return Aleph::to_str(col);
901 return p.first.size() > p.second.size() ?
909 return Aleph::to_str(row);
913 (std::string(), [](
const std::string & s,
925 const size_t fsz = p.first.
size();
926 const size_t ssz = p.second.
size();
934 (
const std::pair<std::string, Trow> & p)
936 const std::string & label = p.first;
943 (
ret_val, [](
const std::string & s,
944 const std::pair<NumType, std::string> & q)
947 const std::string &
len_str = q.second;
949 return s + std::string(
len_str.
size() - val.size() + 1,
' ') + val;
970 <<
"invalid matrix's domains";
973 const Trow &
row = p.first.first;
974 const Tcol & col = p.first.second;
979 const Trow &
row = p.first.first;
980 const Tcol & col = p.first.second;
993 template <
typename Trow,
typename Tcol,
typename NumType>
1003 template <
typename Trow,
typename Tcol,
typename NumType>
1011 template <
typename Trow,
typename Tcol,
typename NumType>
1015 template <
typename Trow,
typename Tcol,
typename NumType>
1030 template <
typename Trow,
typename Tcol,
typename NumType =
double>
1041 ret_val.set_entry(p1.first, p2.first,
1042 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.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
T & append(const T &item)
Append a new item by copy.
T & get_first() const
Return the first item of the list.
auto for_each(Operation &operation) const -> decltype(self())
Apply an operation to each element (read-only).
size_t size() const noexcept
Count the number of elements of the list.
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.
static const NumType default_epsilon
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (shared_ptr version).
Matrix(const Matrix &other)
Copy constructor.
Matrix< Trow, T2col > vector_matrix_mult(const Matrix< T2row, T2col, NumType > &m) const
Matrix multiplication using row-vector times matrix approach.
void validate_domains_for_mult(const Matrix< T2row, T2col, NumType > &m) const
Validate domain compatibility for matrix multiplication.
void print() const
Print non-zero entries to stdout.
Matrix(const RDomain &rdomain, const CDomain &cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (reference version).
const CDomain & get_col_domain() const noexcept
Return the column domain.
Vector< Trow, NumType > mult_matrix_vector_dot_product(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication using dot products.
std::shared_ptr< RDomain > RDomainPtr
Matrix operator+(const Matrix &m) const
Matrix addition operator.
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.
DynList< ColVector > to_collist() const
Return a list of vectors corresponding to the columns.
DynList< Vector< Tcol, NumType > > to_rowlist() const
Return a list of vectors corresponding to the rows.
DynList< NumType > get_col_as_list(const Tcol &col) const
Get a column as a list of values (in row order).
bool are_equal(const NumType &n1, const NumType &n2) const
Check if two values are equal within epsilon tolerance.
bool operator!=(const Matrix &m) const
Inequality operator.
Matrix(Matrix &&other) noexcept
Move constructor.
Matrix(const RDomain &rdomain, const CDomain &cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon)
Construct a matrix from a list of lists (reference version).
Matrix & mult_by_scalar(const NumType &scalar)
Multiply all entries by a scalar (in-place).
std::pair< RCPair, NumType > Pair
Type alias for entries stored in the hash table: ((row,col), value).
DynList< NumType > get_row_as_list(const Trow &row) const
Get a row as a list of values (in column order).
bool equal_to(const Matrix &m) const
Check if this matrix equals another (within epsilon).
NumType get_entry(const Trow &row, const Tcol &col)
Get an entry value (non-const version with lazy cleanup).
Vector< Tcol, NumType > mult_vector_matrix_linear_comb(const Vector< Trow, NumType > &vec) const
return vec*this in terms of linear combinations
Matrix(const RDomain &rdomain, const CDomain &cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon)
Construct a matrix from a 2D initializer list (reference version).
Matrix< Trow, T2col > matrix_vector_mult(const Matrix< T2row, T2col, NumType > &m) const
Matrix multiplication using matrix times column-vector approach.
Vector< Trow, NumType > mult_matrix_vector_linear_comb(const Vector< Tcol, NumType > &vec) const
return this*vec in terms of linear combinations
Matrix operator-(const Matrix &m) const
Matrix subtraction operator.
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.
std::string to_str() const
Convert matrix to a formatted string representation.
Matrix transpose() const
Compute the transpose of this matrix.
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.
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.
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 & operator+=(const Matrix &m)
Add another matrix to this one.
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, const DynList< DynList< NumType > > &l, const NumType &e=default_epsilon)
Construct a matrix from a list of lists (shared_ptr version).
Matrix identity() const
Create an identity matrix (only for square matrices).
DynList< Tcol > col_domain_list() const
Return a sorted list of column domain keys.
bool is_zero(const NumType &val) const noexcept
Matrix & set_vector_as_col(const Tcol &col, const Vector< Trow, NumType > &vec)
Set a column from a vector.
Matrix & operator-=(const Matrix &m)
Subtract another matrix from this one.
DynList< Trow > row_domain_list() const
Return a sorted list of row domain keys.
bool operator==(const Matrix &m) const
Equality operator (uses equal_to).
Vector< Tcol, NumType > mult_vector_matrix_dot_product(const Vector< Trow, NumType > &vec) const
Vector-matrix multiplication using dot products.
std::pair< Trow, Tcol > RCPair
Vector< Trow, NumType > mult_matrix_vector_sparse(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication using sparse iteration.
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.
Vector< Trow, NumType > operator*(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication operator (uses linear combination method).
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, std::initializer_list< std::initializer_list< NumType > > l, const NumType &e=default_epsilon)
Construct a matrix from a 2D initializer list (shared_ptr version).
Matrix & set_vector_as_row(const Trow &row, const Vector< Tcol, NumType > &vec)
Set a row from a vector.
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.
Iterator get_it() const noexcept
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
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_dft_hash_fct(const std::pair< K1, K2 > &p) noexcept
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
size_t pair_snd_hash_fct(const std::pair< K1, K2 > &p) noexcept
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.
Matrix< Trow, Tcol, NumType > outer_product(const Vector< Trow, NumType > &v1, const Vector< Tcol, NumType > &v2)
Compute the outer product of two vectors.
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
ZipIterator< Cs... > zip_it(const Cs &... cs)
Alias for get_zip_it.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Aleph::DynList< T > keys() const