10namespace Stroika::Foundation::Math::LinearAlgebra {
18 class Matrix<T>::Rep_ {
20 Rep_ (
const DimensionType& dimensions)
21 : fDimensions_{dimensions}
25 T GetAt (
size_t row,
size_t col)
const
28 Require (row < fDimensions_.fRows);
29 Require (col < fDimensions_.fColumns);
30 return fData_[row * fDimensions_.fColumns + col];
32 void SetAt (
size_t row,
size_t col, T value)
35 Require (row < fDimensions_.fRows);
36 Require (col < fDimensions_.fColumns);
38 fData_[row * fDimensions_.fColumns + col] = value;
40 void push_back (Common::ArgByValueType<T> fillValue)
43 fData_.push_back (fillValue);
46 DimensionType GetDimensions ()
const
53 DimensionType fDimensions_;
57 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex fThisAssertExternallySynchronized_;
66 inline Matrix<T>::Matrix (
const DimensionType& dimensions)
67 : Matrix{dimensions, T{0}}
71 inline Matrix<T>::Matrix (
size_t rows,
size_t columns)
72 : Matrix{DimensionType{rows, columns}}
76 Matrix<T>::Matrix (
const DimensionType& dimensions, Common::ArgByValueType<T> fillValue)
77 : fRep_{make_shared<Rep_> (dimensions)}
79 for (
size_t r = 0; r < dimensions.fRows; ++r) {
80 for (
size_t c = 0; c < dimensions.fColumns; ++c) {
81 fRep_.rwget ()->push_back (fillValue);
86 inline Matrix<T>::Matrix (
size_t rows,
size_t columns, Common::ArgByValueType<T> fillValue)
87 : Matrix{DimensionType{rows, columns}, fillValue}
91 Matrix<T>::Matrix (
const DimensionType& dimensions,
const function<T ()>& filler)
92 : fRep_{make_shared<Rep_> (dimensions)}
94 for (
size_t r = 0; r < dimensions.fRows; ++r) {
95 for (
size_t c = 0; c < dimensions.fColumns; ++c) {
96 fRep_.rwget ()->push_back (filler ());
100 template <
typename T>
101 Matrix<T> Matrix<T>::Identity (
const DimensionType& dimensions)
105 return Matrix{dimensions, [&] () {
106 T result = (row == col) ? 1 : 0;
108 if (col > dimensions.fColumns) {
114 template <
typename T>
115 inline auto Matrix<T>::GetDimensions () const -> DimensionType
117 return fRep_.cget ()->GetDimensions ();
119 template <
typename T>
120 Containers::Sequence<Vector<T>> Matrix<T>::GetRows ()
const
122 Containers::Sequence<Vector<T>> result;
123 DimensionType dim = GetDimensions ();
124 for (
size_t r = 0; r < dim.fRows; ++r) {
126 for (
size_t c = 0; c < dim.fColumns; ++c) {
127 row.push_back (fRep_.cget ()->GetAt (r, c));
129 result += Vector<T>{row};
133 template <
typename T>
134 Containers::Sequence<Vector<T>> Matrix<T>::GetColumns ()
const
136 Containers::Sequence<Vector<T>> result;
137 DimensionType dim = GetDimensions ();
138 for (
size_t c = 0; c < dim.fColumns; ++c) {
140 for (
size_t r = 0; r < dim.fRows; ++r) {
141 col.push_back (fRep_.cget ()->GetAt (r, c));
143 result += Vector<T>{col};
147 template <
typename T>
148 inline T Matrix<T>::GetAt (
size_t r,
size_t c)
const
150 return fRep_.cget ()->GetAt (r, c);
152 template <
typename T>
153 inline void Matrix<T>::SetAt (
size_t r,
size_t c, T v)
155 fRep_.rwget ()->SetAt (r, c, v);
157 template <
typename T>
158 inline const typename Matrix<T>::ReadOnlyTemporaryRowReference_ Matrix<T>::operator[] (
size_t row)
const
160 return ReadOnlyTemporaryRowReference_{*
this, row};
162 template <
typename T>
163 Characters::String Matrix<T>::ToString ()
const
165 Characters::StringBuilder sb;
167 for (
size_t row = 0; row < GetDimensions ().fRows; ++row) {
169 for (
size_t col = 0; col < GetDimensions ().fColumns; ++col) {
170 sb << GetAt (row, col) <<
", "sv;
shared_lock< const AssertExternallySynchronizedMutex > ReadContext
Instantiate AssertExternallySynchronizedMutex::ReadContext to designate an area of code where protect...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...