11namespace Stroika::Foundation::Math::LinearAlgebra {
19 class Matrix<T>::Rep_ {
21 Rep_ (
const DimensionType& dimensions)
22 : fDimensions_{dimensions}
26 T GetAt (
size_t row,
size_t col)
const
29 Require (row < fDimensions_.fRows);
30 Require (col < fDimensions_.fColumns);
31 return fData_[row * fDimensions_.fColumns + col];
33 void SetAt (
size_t row,
size_t col, T value)
36 Require (row < fDimensions_.fRows);
37 Require (col < fDimensions_.fColumns);
39 fData_[row * fDimensions_.fColumns + col] = value;
41 void push_back (Common::ArgByValueType<T> fillValue)
44 fData_.push_back (fillValue);
47 DimensionType GetDimensions ()
const
54 DimensionType fDimensions_;
58 [[no_unique_address]] Debug::AssertExternallySynchronizedMutex fThisAssertExternallySynchronized_;
67 inline Matrix<T>::Matrix (
const DimensionType& dimensions)
68 : Matrix{dimensions, T{0}}
72 inline Matrix<T>::Matrix (
size_t rows,
size_t columns)
73 : Matrix{DimensionType{rows, columns}}
77 Matrix<T>::Matrix (
const DimensionType& dimensions, Common::ArgByValueType<T> fillValue)
80 for (
size_t r = 0; r < dimensions.fRows; ++r) {
81 for (
size_t c = 0; c < dimensions.fColumns; ++c) {
82 fRep_.rwget ()->push_back (fillValue);
87 inline Matrix<T>::Matrix (
size_t rows,
size_t columns, Common::ArgByValueType<T> fillValue)
88 : Matrix{DimensionType{rows, columns}, fillValue}
92 Matrix<T>::Matrix (
const DimensionType& dimensions,
const function<T ()>& filler)
95 for (
size_t r = 0; r < dimensions.fRows; ++r) {
96 for (
size_t c = 0; c < dimensions.fColumns; ++c) {
97 fRep_.rwget ()->push_back (filler ());
101 template <
typename T>
102 Matrix<T> Matrix<T>::Identity (
const DimensionType& dimensions)
106 return Matrix{dimensions, [&] () {
107 T result = (row == col) ? 1 : 0;
109 if (col > dimensions.fColumns) {
115 template <
typename T>
116 inline auto Matrix<T>::GetDimensions () const -> DimensionType
118 return fRep_.cget ()->GetDimensions ();
120 template <
typename T>
121 Containers::Sequence<Vector<T>> Matrix<T>::GetRows ()
const
123 Containers::Sequence<Vector<T>> result;
124 DimensionType dim = GetDimensions ();
125 for (
size_t r = 0; r < dim.fRows; ++r) {
127 for (
size_t c = 0; c < dim.fColumns; ++c) {
128 row.push_back (fRep_.cget ()->GetAt (r, c));
130 result += Vector<T>{row};
134 template <
typename T>
135 Containers::Sequence<Vector<T>> Matrix<T>::GetColumns ()
const
137 Containers::Sequence<Vector<T>> result;
138 DimensionType dim = GetDimensions ();
139 for (
size_t c = 0; c < dim.fColumns; ++c) {
141 for (
size_t r = 0; r < dim.fRows; ++r) {
142 col.push_back (fRep_.cget ()->GetAt (r, c));
144 result += Vector<T>{col};
148 template <
typename T>
149 inline T Matrix<T>::GetAt (
size_t r,
size_t c)
const
151 return fRep_.cget ()->GetAt (r, c);
153 template <
typename T>
154 inline void Matrix<T>::SetAt (
size_t r,
size_t c, T v)
156 fRep_.rwget ()->SetAt (r, c, v);
158 template <
typename T>
159 inline const typename Matrix<T>::ReadOnlyTemporaryRowReference_ Matrix<T>::operator[] (
size_t row)
const
161 return ReadOnlyTemporaryRowReference_{*
this, row};
163 template <
typename T>
164 Characters::String Matrix<T>::ToString ()
const
166 Characters::StringBuilder sb;
168 for (
size_t row = 0; row < GetDimensions ().fRows; ++row) {
170 for (
size_t col = 0; col < GetDimensions ().fColumns; ++col) {
171 sb << GetAt (row, col) <<
", "sv;
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...
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...