EllAlgo 1.6.13
Loading...
Searching...
No Matches
ell_matrix.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <algorithm>
9#include <cstddef>
10#include <valarray>
11#include <vector>
12
20class SliceView {
21 double* const _data;
22 const std::size_t _stride;
23 const std::size_t _len;
24
25 public:
26 SliceView(double* data, std::size_t stride, std::size_t len) noexcept
27 : _data{data}, _stride{stride}, _len{len} {}
28
29 double& operator[](std::size_t i) { return this->_data[i * this->_stride]; }
30 const double& operator[](std::size_t i) const { return this->_data[i * this->_stride]; }
31
32 SliceView& operator=(const std::valarray<double>& v) {
33 for (std::size_t i = 0; i < this->_len; ++i) this->_data[i * this->_stride] = v[i];
34 return *this;
35 }
36
38 for (std::size_t i = 0; i < this->_len; ++i) this->_data[i * this->_stride] = scalar;
39 return *this;
40 }
41
42 operator std::valarray<double>() const {
43 std::valarray<double> res(this->_len);
44 for (std::size_t i = 0; i < this->_len; ++i) res[i] = this->_data[i * this->_stride];
45 return res;
46 }
47
48 double sum() const {
49 double s = 0.0;
50 for (std::size_t i = 0; i < this->_len; ++i) s += this->_data[i * this->_stride];
51 return s;
52 }
53};
54
63class Matrix {
64 std::size_t ndim;
65 std::vector<double> data;
66
67 public:
73 explicit Matrix(std::size_t ndim, double init = 0.0) : ndim{ndim}, data(ndim * ndim, init) {}
74
76 void clear(double value = 0.0) { std::ranges::fill(this->data, value); }
77
79 double& operator()(std::size_t row, std::size_t col) {
80 return this->data[row * this->ndim + col];
81 }
82
84 const double& operator()(std::size_t row, std::size_t col) const {
85 return this->data[row * this->ndim + col];
86 }
87
90 for (auto& v : this->data) v *= alpha;
91 return *this;
92 }
93
95 Matrix operator*(double alpha) const {
96 Matrix res(*this);
97 return res *= alpha;
98 }
99
101 void identity() {
102 this->clear();
103 auto d = this->diagonal();
104 for (std::size_t i = 0; i < this->ndim; ++i) d[i] = 1.0;
105 }
106
108 SliceView diagonal() { return SliceView(this->data.data(), this->ndim + 1, this->ndim); }
109
111 SliceView row(std::size_t r) {
112 return SliceView(this->data.data() + r * this->ndim, 1, this->ndim);
113 }
114
116 SliceView column(std::size_t c) {
117 return SliceView(this->data.data() + c, this->ndim, this->ndim);
118 }
119
127 double trace() const {
128 double s = 0.0;
129 for (std::size_t i = 0; i < this->ndim; ++i) {
130 s += this->operator()(i, i);
131 }
132 return s;
133 }
134};
Square matrix with flat std::vector<double> storage.
Definition ell_matrix.hpp:63
SliceView column(std::size_t c)
Mutable view of column c (strided, stride = ndim)
Definition ell_matrix.hpp:116
void identity()
Set to identity matrix.
Definition ell_matrix.hpp:101
double & operator()(std::size_t row, std::size_t col)
Access element (row, col) — mutable.
Definition ell_matrix.hpp:79
Matrix operator*(double alpha) const
Return a scaled copy.
Definition ell_matrix.hpp:95
void clear(double value=0.0)
Reset all elements to value.
Definition ell_matrix.hpp:76
double trace() const
Sum of diagonal elements.
Definition ell_matrix.hpp:127
Matrix & operator*=(double alpha)
Scale all elements by alpha.
Definition ell_matrix.hpp:89
SliceView row(std::size_t r)
Mutable view of row r (contiguous, stride = 1)
Definition ell_matrix.hpp:111
Matrix(std::size_t ndim, double init=0.0)
Construct an ndim × ndim matrix.
Definition ell_matrix.hpp:73
SliceView diagonal()
Mutable view of the diagonal (stride = ndim + 1)
Definition ell_matrix.hpp:108
const double & operator()(std::size_t row, std::size_t col) const
Access element (row, col) — const.
Definition ell_matrix.hpp:84
Proxy view over a strided slice of a flat array.
Definition ell_matrix.hpp:20
double sum() const
Definition ell_matrix.hpp:48
SliceView & operator=(double scalar)
Definition ell_matrix.hpp:37
const double & operator[](std::size_t i) const
Definition ell_matrix.hpp:30
SliceView(double *data, std::size_t stride, std::size_t len) noexcept
Definition ell_matrix.hpp:26
double & operator[](std::size_t i)
Definition ell_matrix.hpp:29
SliceView & operator=(const std::valarray< double > &v)
Definition ell_matrix.hpp:32
auto invalid_value() -> T
Return an invalid/sentinel value for type T.
Definition cutting_plane.hpp:27