Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al-matrix.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
43# ifndef AH_MATRIX_H
44# define AH_MATRIX_H
45
46# include <al-vector.H>
47# include <ah-errors.H>
48
49namespace Aleph
50{
64 template <typename Trow = int, typename Tcol = int, typename NumType = double>
65 class Matrix
66 {
67 public:
70 using RDomainPtr = std::shared_ptr<RDomain>;
71 using CDomainPtr = std::shared_ptr<CDomain>;
72
73 using RCPair = std::pair<Trow, Tcol>;
74
76
78
80
81 private:
83
88
89 bool is_zero(const NumType & val) const noexcept
90 {
91 assert(epsilon >= 0);
92 return val >= -epsilon and val <= epsilon;
93 }
94
95 public:
98
103 void set_epsilon(const NumType & e)
104 {
105 ah_domain_error_if(e < 0)
106 << "epsilon must be greater or equal to zero";
107 epsilon = e;
108 }
109
111 typedef std::pair<RCPair, NumType> Pair;
112
115
118
121
124
139
155
164 std::initializer_list<std::initializer_list<NumType>> l,
165 const NumType & e = default_epsilon)
167 {
168 ah_range_error_if(l.size() != rdomain_ptr->size()) << "mismatch with number of rows";
169
170 auto itr = l.begin();
171 const DynList<Tcol> clist = sort(cdomain_ptr->keys());
172 sort(rdomain_ptr->keys()).
173 for_each([this, &itr, &clist](const Trow & row)
174 {
176 << "mismatch with number of columns";
177 auto itc = itr->begin();
178 clist.for_each([this, &itc, &row](const Tcol & col)
179 {
180 const NumType & val = *itc++;
181 if (is_zero(val))
182 return;
183
184 entries.insert(RCPair(row, col), val);
185 });
186 ++itr;
187 });
188 }
189
198 std::initializer_list<std::initializer_list<NumType>> l,
199 const NumType & e = default_epsilon)
201 CDomainPtr(&const_cast<CDomain &>(cdomain), [](CDomain *) {}), l, e)
202 {}
203
212 const DynList<DynList<NumType>> & l,
213 const NumType & e = default_epsilon)
215 {
216 ah_range_error_if(l.size() != rdomain_ptr->size()) << "mismatch with number of rows";
217
218 for (auto it = zip_it(rdomain_ptr->keys(), l); it.has_curr(); it.next())
219 {
220 auto t = it.get_curr();
221 const Trow & row_idx = get<0>(t);
222 const DynList<NumType> & row = get<1>(t);
223 for (auto it = zip_it(cdomain_ptr->keys(), row); it.has_curr(); it.next())
224 {
225 auto t = it.get_curr();
226 const NumType & val = get<1>(t);
227 if (is_zero(val))
228 continue;
229 const Tcol & col_idx = get<0>(t);
230 entries.insert(RCPair(row_idx, col_idx), val);
231 }
232 }
233 }
234
243 const DynList<DynList<NumType>> & l,
244 const NumType & e = default_epsilon)
246 CDomainPtr(&const_cast<CDomain &>(cdomain), [](CDomain *) {}), l, e)
247 {}
248
258
261 : rdomain_ptr(other.rdomain_ptr),
262 cdomain_ptr(other.cdomain_ptr),
263 entries(std::move(other.entries)),
264 epsilon(other.epsilon)
265 {
266 // empty
267 }
268
271 {
272 if (this != &other)
273 {
274 rdomain_ptr = other.rdomain_ptr;
275 cdomain_ptr = other.cdomain_ptr;
276 entries = other.entries;
277 epsilon = other.epsilon;
278 }
279 return *this;
280 }
281
284 {
285 if (this != &other)
286 {
287 rdomain_ptr = other.rdomain_ptr;
288 cdomain_ptr = other.cdomain_ptr;
289 entries = std::move(other.entries);
290 epsilon = other.epsilon;
291 }
292 return *this;
293 }
294
302 const DynList<Vector<Tcol, NumType>> & rows,
303 const NumType & e = default_epsilon)
304 {
305 Matrix ret(rdomain, rows.get_first().get_domain(), e);
306
307 for (auto it = zip_it(rdomain.keys(), rows); it.has_curr(); it.next())
308 {
309 auto t = it.get_curr();
310 const Trow & row_idx = get<0>(t);
311 const Vector<Tcol, NumType> & row = get<1>(t);
312 for (auto it_col = row.get_it(); it_col.has_curr(); it_col.next())
313 {
314 auto & p = it_col.get_curr();
315 const NumType & val = p.second;
316 if (ret.is_zero(val))
317 continue;
318 const Tcol & col_idx = p.first;
319 ret.entries.insert(RCPair(row_idx, col_idx), val);
320 }
321 }
322
323 return ret;
324 }
325
333 const DynList<Vector<Trow, NumType>> & cols,
334 const NumType & e = default_epsilon)
335 {
336 Matrix ret(cols.get_first().get_domain(), cdomain, e);
337
338 for (auto it = zip_it(cdomain.keys(), cols); it.has_curr(); it.next())
339 {
340 auto t = it.get_curr();
341 const Tcol & col_idx = get<0>(t);
342 const Vector<Trow, NumType> & col = get<1>(t);
343 for (auto it_col = col.get_it(); it_col.has_curr(); it_col.next())
344 {
345 auto & p = it_col.get_curr();
346 const NumType & val = p.second;
347 if (ret.is_zero(val))
348 continue;
349 const Trow & row_idx = p.first;
350 ret.entries.insert(RCPair(row_idx, col_idx), val);
351 }
352 }
353
354 return ret;
355 }
356
366 NumType get_entry(const Trow & row, const Tcol & col)
367 {
368 assert(rdomain_ptr->has(row) and cdomain_ptr->has(col));
369 auto *ptr = entries.search(RCPair(row, col));
370 if (ptr == nullptr)
371 return 0;
372 if (is_zero(ptr->second))
373 {
374 entries.remove_ptr(ptr);
375 return 0;
376 }
377 return ptr->second;
378 }
379
385 NumType get_entry(const Trow & row, const Tcol & col) const noexcept
386 {
387 assert(rdomain_ptr->has(row) and cdomain_ptr->has(col));
388 auto *ptr = entries.search(RCPair(row, col));
389 if (ptr == nullptr)
390 return 0;
391 return ptr->second;
392 }
393
403 void set_entry(const Trow & row, const Tcol & col, const NumType & val)
404 {
405 assert(rdomain_ptr->has(row) and cdomain_ptr->has(col));
406 auto *ptr = entries.search(RCPair(row, col));
407 if (is_zero(val))
408 {
409 if (ptr != nullptr)
410 entries.remove_ptr(ptr);
411 return;
412 }
413 if (ptr == nullptr)
414 entries.insert(RCPair(row, col), val);
415 else
416 ptr->second = val;
417 }
418
423 {
425 entries.for_each([&ret_val](const Pair & p)
426 {
427 const RCPair & rc = p.first;
428 const NumType & val = p.second;
429 ret_val.set_entry(rc.second, rc.first, val);
430 });
431 return ret_val;
432 }
433
436 {
437 return sort(rdomain_ptr->keys());
438 }
439
442 {
443 return sort(cdomain_ptr->keys());
444 }
445
452 {
453 ah_domain_error_if(not rdomain_ptr->has(row)) << "row is not inside the row domain";
454 return col_domain_list().
455 template maps<NumType>([this, &row](const Tcol & col)
456 {
457 return get_entry(row, col);
458 });
459 }
460
467 {
468 ah_domain_error_if(not cdomain_ptr->has(col)) << "col is not inside the column domain";
469 return row_domain_list().template maps<NumType>
470 ([this, &col](const Trow & row) { return get_entry(row, col); });
471 }
472
475 {
479 for_each([this, &ret_val, cols](const Trow & row)
480 {
482 cols.for_each([this, &vec, &row](const Tcol & col)
483 {
484 auto *val_ptr =
485 entries.search(RCPair(row, col));
486 if (val_ptr != nullptr)
487 vec.set_entry(col, val_ptr->second);
488 });
490 });
491 return ret_val;
492 }
493
496 {
500 for_each([this, &ret_val, &rows](const Tcol & col)
501 {
503 rows.for_each([this, &vec, &col](const Trow & row)
504 {
505 auto *ptr = entries.search(RCPair(row, col));
506 if (ptr != nullptr)
507 vec.set_entry(row, ptr->second);
508 });
510 });
511 return ret_val;
512 }
513
516 {
518 << "get_row_vector(): invalid row";
520 cdomain_ptr->for_each([&](const Tcol & col)
521 {
522 auto *ptr = entries.search(RCPair(row, col));
523 if (ptr != nullptr)
524 ret_val.set_entry(col, ptr->second);
525 });
526 return ret_val;
527 }
528
531 {
533 << "get_col_vector(): invalid col";
535 rdomain_ptr->for_each([&](const Trow & row)
536 {
537 auto *ptr = entries.search(RCPair(row, col));
538 if (ptr != nullptr)
539 ret_val.set_entry(row, ptr->second);
540 });
541 return ret_val;
542 }
543
554 {
555 ah_domain_error_if(&vec.get_domain() != &get_col_domain())
556 << "mult_matrix_vector_linear_comb: domain mismatch";
558 vec.for_each([this, &ret_val](const std::pair<Tcol, NumType> & p)
559 {
560 const Tcol & col = p.first;
561 const NumType & val = p.second;
562 ret_val += val * get_col_vector(col);
563 });
564 return ret_val;
565 }
566
577 {
578 ah_domain_error_if(&vec.get_domain() != &get_col_domain())
579 << "mult_matrix_vector_sparse: domain mismatch";
581 entries.for_each([&ret_val, &vec](const Pair & p)
582 {
583 const Trow & row = p.first.first;
584 const Tcol & col = p.first.second;
585 const NumType & entry_val = p.second;
586 NumType *vec_entry = vec.search_entry(col);
587 if (vec_entry != nullptr)
588 ret_val.set_entry(row, ret_val.get_entry(row) +
590 });
591 return ret_val;
592 }
593
605 {
606 ah_domain_error_if(&vec.get_domain() != &get_row_domain())
607 << "mult_vector_matrix_linear_comb: domain mismatch";
609 vec.for_each([this, &ret_val](const std::pair<Trow, NumType> & p)
610 {
611 const Trow & row = p.first;
612 const NumType & val = p.second;
613 ret_val += val * get_row_vector(row);
614 });
615 return ret_val;
616 }
617
628 {
629 ah_domain_error_if(&vec.get_domain() != &get_col_domain())
630 << "mult_matrix_vector_dot_product: domain mismatch";
632 rdomain_ptr->for_each([&](const Trow & row)
633 {
634 ret_val.set_entry(row, vec * get_row_vector(row));
635 });
636 return ret_val;
637 }
638
649 {
650 ah_domain_error_if(&vec.get_domain() != &get_row_domain())
651 << "mult_vector_matrix_dot_product: domain mismatch";
653 cdomain_ptr->for_each([&](const Tcol & col)
654 {
655 ret_val.set_entry(col, vec * get_col_vector(col));
656 });
657 return ret_val;
658 }
659
666
667 private:
676 template <typename T2row, typename T2col>
678 const
679 {
681 << "AxB: Column domain of A must be identical to row domain of B";
682 }
683
684 public:
692 {
693 ah_domain_error_if(&vec.get_domain() != cdomain_ptr.get())
694 << "set_vector_as_row: domain vec mismatch";
695 vec.for_each([this, &row](const std::pair<Tcol, NumType> & p)
696 {
697 const Tcol & col = p.first;
698 const NumType & val = p.second;
699 set_entry(row, col, val);
700 });
701 return *this;
702 }
703
711 {
712 ah_domain_error_if(&vec.get_domain() != rdomain_ptr.get())
713 << "set_vector_as_col: domain vec mismatch";
714 vec.for_each([this, &col](const std::pair<Trow, NumType> & p)
715 {
716 const Trow & row = p.first;
717 const NumType & val = p.second;
718 set_entry(row, col, val);
719 });
720 return *this;
721 }
722
730 {
731 static_assert(std::is_same_v<Trow, Tcol>,
732 "Row domain is not equal to column domain");
733
735 rdomain_ptr->for_each([&i](const Trow & row)
736 {
737 i.set_entry(row, row, 1);
738 });
739 return i;
740 }
741
748 {
750 << "Matrix addition domain mismatch";
751 m.entries.for_each([this](const Pair & p)
752 {
753 const Trow & row = p.first.first;
754 const Tcol & col = p.first.second;
755 const RCPair rcp = RCPair(row, col);
756 auto *ptr = entries.search(rcp);
757 if (ptr == nullptr)
758 entries.insert(rcp, p.second);
759 else
760 ptr->second += p.second;
761 });
762 return *this;
763 }
764
766 Matrix operator +(const Matrix & m) const
767 {
768 Matrix ret_val = *this;
769 ret_val += m;
770 return ret_val;
771 }
772
779 {
781 << "Matrix subtraction domain mismatch";
782 m.entries.for_each([this](const Pair & p)
783 {
784 const Trow & row = p.first.first;
785 const Tcol & col = p.first.second;
786 const RCPair rcp = RCPair(row, col);
787 auto *ptr = entries.search(rcp);
788 if (ptr == nullptr)
789 entries.insert(rcp, -p.second);
790 else
791 ptr->second -= p.second;
792 });
793 return *this;
794 }
795
797 Matrix operator -(const Matrix & m) const
798 {
799 Matrix ret_val = *this;
800 ret_val -= m;
801 return ret_val;
802 }
803
809 {
810 entries.for_each([&scalar](const Pair & p)
811 {
812 NumType & val = const_cast<Pair &>(p).second;
813 val *= scalar;
814 });
815 return *this;
816 }
817
825 template <typename T2row, typename T2col>
828 {
832 for_each([this, &m, &ret_val](const Trow & row)
833 {
834 ret_val.set_vector_as_row(row, get_row_vector(row) * m);
835 });
836 return ret_val;
837 }
838
846 template <typename T2row, typename T2col>
849 {
852 m.get_col_domain().
853 for_each([this, &m, &ret_val](const Tcol & col)
854 {
855 ret_val.set_vector_as_col(col,
856 *this * m.get_col_vector(col));
857 });
858 return ret_val;
859 }
860
862 void print() const
863 {
864 std::cout << "Non zero entries = " << '\n';
865 entries.for_each([](const Pair & p)
866 {
867 std::cout << "(" << Aleph::to_str(p.first.first) << ","
868 << Aleph::to_str(p.first.second) << ") : "
869 << Aleph::to_str(p.second) << '\n';
870 });
871 std::cout << '\n';
872 }
873
875 [[nodiscard]] std::string to_str() const
876 {
877 typedef std::pair<std::string, std::string> StrPair;
879 // list of longest strings for each column
880 const DynList<std::string> collens = cols.
881 template maps<std::string>([](const ColVector & vec)
882 {
883 return vec.to_list().
884 template foldl<std::string>
885 ("", [](const std::string & s, const NumType & val)
886 {
887 std::string val_str = Aleph::to_str(val);
888 return val_str.size() > s.size() ? val_str : s;
889 });
890 });
891
893 sort(cdomain_ptr->template maps<std::string>([](const Tcol & col)
894 {
895 return Aleph::to_str(col);
896 }));
897
899 template maps<std::string>([](const StrPair & p)
900 {
901 return p.first.size() > p.second.size() ?
902 p.first :
903 p.second;
904 });
905
907 sort(rdomain_ptr->template maps<std::string>([](const Trow & row)
908 {
909 return Aleph::to_str(row);
910 }));
911
912 const std::string longer_row_label = row_labels.template foldl<std::string>
913 (std::string(), [](const std::string & s,
914 const std::string & row_label)
915 {
916 return row_label.size() > s.size() ? row_label : s;
917 });
918
920
921 std::string ret_val = std::string(longer_row_label_sz, ' ') + " |";
923 (ret_val, [](const std::string & s, const StrPair & p)
924 {
925 const size_t fsz = p.first.size();
926 const size_t ssz = p.second.size();
927 return s + std::string((fsz > ssz ? fsz - ssz : ssz - fsz) + 1, ' ') + p.first;
928 });
929
930 ret_val += "\n" + std::string(ret_val.size(), '-');
931
932 zip(row_labels, row_domain_list()).for_each
934 (const std::pair<std::string, Trow> & p)
935 {
936 const std::string & label = p.first;
937 const Trow & row = p.second;
938
939 ret_val += "\n" +
940 std::string(longer_row_label_sz - label.size(), ' ') + label + " |";
941 ret_val =
943 (ret_val, [](const std::string & s,
944 const std::pair<NumType, std::string> & q)
945 {
946 const std::string val = Aleph::to_str(q.first);
947 const std::string & len_str = q.second;
948 assert(len_str.size() - val.size() + 1);
949 return s + std::string(len_str.size() - val.size() + 1, ' ') + val;
950 });
951 });
952
953 return ret_val;
954 }
955
957 bool are_equal(const NumType & n1, const NumType & n2) const
958 {
959 return is_zero(n1 - n2);
960 }
961
967 bool equal_to(const Matrix & m) const
968 {
970 << "invalid matrix's domains";
971 return entries.all([&m, this](const Pair & p)
972 {
973 const Trow & row = p.first.first;
974 const Tcol & col = p.first.second;
975 return are_equal(m.get_entry(row, col), p.second);
976 }) and
977 m.entries.all([this](const Pair & p)
978 {
979 const Trow & row = p.first.first;
980 const Tcol & col = p.first.second;
981 return are_equal(get_entry(row, col), p.second);
982 });
983 }
984
986 bool operator ==(const Matrix & m) const { return equal_to(m); }
987
989 bool operator !=(const Matrix & m) const { return not equal_to(m); }
990 };
991
993 template <typename Trow, typename Tcol, typename NumType>
994 inline
1001
1003 template <typename Trow, typename Tcol, typename NumType>
1004 inline
1010
1011 template <typename Trow, typename Tcol, typename NumType>
1013
1015 template <typename Trow, typename Tcol, typename NumType>
1016 inline
1017 std::ostream &operator <<(std::ostream & s, const Matrix<Trow, Tcol, NumType> & m)
1018 {
1019 return s << m.to_str();
1020 }
1021
1030 template <typename Trow, typename Tcol, typename NumType = double>
1031 inline
1033 const Vector<Tcol, NumType> & v2)
1034 {
1036
1037 v1.for_each([&ret_val, &v2](const std::pair<Trow, NumType> & p1)
1038 {
1039 v2.for_each([&ret_val, &p1](const std::pair<Tcol, NumType> & p2)
1040 {
1041 ret_val.set_entry(p1.first, p2.first,
1042 p1.second * p2.second);
1043 });
1044 });
1045 return ret_val;
1046 }
1047} // end namespace Aleph
1048
1049# endif // AH_MATRIX_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
Sparse vector with named elements.
Generic domain class based on hash set.
Definition al-domain.H:85
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
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.
Definition htlist.H:1319
Sparse matrix with generic row and column domains.
Definition al-matrix.H:66
const NumType & get_epsilon() const noexcept
Return the epsilon value used for zero comparisons.
Definition al-matrix.H:97
const CDomainPtr & get_col_domain_ptr() const noexcept
Return the shared pointer to the column domain.
Definition al-matrix.H:123
static const NumType default_epsilon
Definition al-matrix.H:82
CDomainPtr cdomain_ptr
Definition al-matrix.H:85
Matrix(RDomainPtr rdomain, CDomainPtr cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (shared_ptr version).
Definition al-matrix.H:130
Matrix(const Matrix &other)
Copy constructor.
Definition al-matrix.H:250
Matrix< Trow, T2col > vector_matrix_mult(const Matrix< T2row, T2col, NumType > &m) const
Matrix multiplication using row-vector times matrix approach.
Definition al-matrix.H:827
void validate_domains_for_mult(const Matrix< T2row, T2col, NumType > &m) const
Validate domain compatibility for matrix multiplication.
Definition al-matrix.H:677
void print() const
Print non-zero entries to stdout.
Definition al-matrix.H:862
Matrix(const RDomain &rdomain, const CDomain &cdomain, const NumType &e=default_epsilon)
Construct an empty sparse matrix over the given domains (reference version).
Definition al-matrix.H:146
const CDomain & get_col_domain() const noexcept
Return the column domain.
Definition al-matrix.H:117
Vector< Trow, NumType > mult_matrix_vector_dot_product(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication using dot products.
Definition al-matrix.H:627
std::shared_ptr< RDomain > RDomainPtr
Definition al-matrix.H:70
Matrix operator+(const Matrix &m) const
Matrix addition operator.
Definition al-matrix.H:766
NumType epsilon
Definition al-matrix.H:87
std::shared_ptr< CDomain > CDomainPtr
Definition al-matrix.H:71
Vector< Tcol, NumType > get_row_vector(const Trow &row) const
Given a row, build a vector corresponding to the row.
Definition al-matrix.H:515
DynList< ColVector > to_collist() const
Return a list of vectors corresponding to the columns.
Definition al-matrix.H:495
DynList< Vector< Tcol, NumType > > to_rowlist() const
Return a list of vectors corresponding to the rows.
Definition al-matrix.H:474
DynList< NumType > get_col_as_list(const Tcol &col) const
Get a column as a list of values (in row order).
Definition al-matrix.H:466
bool are_equal(const NumType &n1, const NumType &n2) const
Check if two values are equal within epsilon tolerance.
Definition al-matrix.H:957
bool operator!=(const Matrix &m) const
Inequality operator.
Definition al-matrix.H:989
Matrix(Matrix &&other) noexcept
Move constructor.
Definition al-matrix.H:260
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).
Definition al-matrix.H:242
Matrix & mult_by_scalar(const NumType &scalar)
Multiply all entries by a scalar (in-place).
Definition al-matrix.H:808
std::pair< RCPair, NumType > Pair
Type alias for entries stored in the hash table: ((row,col), value).
Definition al-matrix.H:111
DynList< NumType > get_row_as_list(const Trow &row) const
Get a row as a list of values (in column order).
Definition al-matrix.H:451
bool equal_to(const Matrix &m) const
Check if this matrix equals another (within epsilon).
Definition al-matrix.H:967
NumType get_entry(const Trow &row, const Tcol &col)
Get an entry value (non-const version with lazy cleanup).
Definition al-matrix.H:366
Vector< Tcol, NumType > mult_vector_matrix_linear_comb(const Vector< Trow, NumType > &vec) const
return vec*this in terms of linear combinations
Definition al-matrix.H:604
AlDomain< Tcol > CDomain
Definition al-matrix.H:69
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).
Definition al-matrix.H:197
Matrix< Trow, T2col > matrix_vector_mult(const Matrix< T2row, T2col, NumType > &m) const
Matrix multiplication using matrix times column-vector approach.
Definition al-matrix.H:848
Vector< Trow, NumType > mult_matrix_vector_linear_comb(const Vector< Tcol, NumType > &vec) const
return this*vec in terms of linear combinations
Definition al-matrix.H:553
Matrix operator-(const Matrix &m) const
Matrix subtraction operator.
Definition al-matrix.H:797
Matrix & operator=(const Matrix &other)
Copy assignment operator.
Definition al-matrix.H:270
Vector< Trow, NumType > get_col_vector(const Tcol &col) const
Given a column, build a vector corresponding to the column.
Definition al-matrix.H:530
std::string to_str() const
Convert matrix to a formatted string representation.
Definition al-matrix.H:875
Matrix transpose() const
Compute the transpose of this matrix.
Definition al-matrix.H:422
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.
Definition al-matrix.H:332
NumType get_entry(const Trow &row, const Tcol &col) const noexcept
Get an entry value (const version, no cleanup).
Definition al-matrix.H:385
void set_entry(const Trow &row, const Tcol &col, const NumType &val)
Set an entry value.
Definition al-matrix.H:403
void set_epsilon(const NumType &e)
Set the epsilon value for zero comparisons.
Definition al-matrix.H:103
const RDomain & get_row_domain() const noexcept
Return the row domain.
Definition al-matrix.H:114
Matrix & operator+=(const Matrix &m)
Add another matrix to this one.
Definition al-matrix.H:747
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).
Definition al-matrix.H:211
Matrix identity() const
Create an identity matrix (only for square matrices).
Definition al-matrix.H:729
DynList< Tcol > col_domain_list() const
Return a sorted list of column domain keys.
Definition al-matrix.H:441
bool is_zero(const NumType &val) const noexcept
Definition al-matrix.H:89
Matrix & set_vector_as_col(const Tcol &col, const Vector< Trow, NumType > &vec)
Set a column from a vector.
Definition al-matrix.H:710
Matrix & operator-=(const Matrix &m)
Subtract another matrix from this one.
Definition al-matrix.H:778
DynList< Trow > row_domain_list() const
Return a sorted list of row domain keys.
Definition al-matrix.H:435
bool operator==(const Matrix &m) const
Equality operator (uses equal_to).
Definition al-matrix.H:986
Vector< Tcol, NumType > mult_vector_matrix_dot_product(const Vector< Trow, NumType > &vec) const
Vector-matrix multiplication using dot products.
Definition al-matrix.H:648
std::pair< Trow, Tcol > RCPair
Definition al-matrix.H:73
Vector< Trow, NumType > mult_matrix_vector_sparse(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication using sparse iteration.
Definition al-matrix.H:576
RDomainPtr rdomain_ptr
Definition al-matrix.H:84
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.
Definition al-matrix.H:301
Vector< Trow, NumType > operator*(const Vector< Tcol, NumType > &vec) const
Matrix-vector multiplication operator (uses linear combination method).
Definition al-matrix.H:662
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).
Definition al-matrix.H:163
Matrix & set_vector_as_row(const Trow &row, const Vector< Tcol, NumType > &vec)
Set a row from a vector.
Definition al-matrix.H:691
const RDomainPtr & get_row_domain_ptr() const noexcept
Return the shared pointer to the row domain.
Definition al-matrix.H:120
Sparse vector implementation with domain-based indexing.
Definition al-vector.H:87
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
Definition al-vector.H:137
Iterator get_it() const noexcept
Definition al-vector.H:700
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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
Definition hash-fct.H:955
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
Definition al-matrix.H:995
size_t pair_snd_hash_fct(const std::pair< K1, K2 > &p) noexcept
Definition hash-fct.H:961
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
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.
Definition al-matrix.H:1032
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Definition ahField.H:121
ZipIterator< Cs... > zip_it(const Cs &... cs)
Alias for get_zip_it.
Definition ah-zip.H:275
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Aleph::DynList< T > keys() const
Definition ah-dry.H:1516
DynList< int > l