anpcpp 0.1.0
Analytic Network Process computational library
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <stdexcept>
10#include <string>
11#include <vector>
12
17namespace anpcpp {
18
22class DimensionError : public std::runtime_error {
23public:
27 explicit DimensionError(const std::string& message)
28 : std::runtime_error(message) {}
29};
30
34class Vector {
35public:
37 Vector() = default;
38
44 explicit Vector(std::size_t size, double fill = 0.0);
45
47 Vector(const Vector&) = default;
49 Vector(Vector&&) noexcept = default;
51 Vector& operator=(const Vector&) = default;
53 Vector& operator=(Vector&&) noexcept = default;
54
56 [[nodiscard]] std::size_t size() const noexcept { return data_.size(); }
58 [[nodiscard]] bool empty() const noexcept { return data_.empty(); }
59
66 double& operator[](std::size_t i);
67
74 [[nodiscard]] const double& operator[](std::size_t i) const;
75
77 [[nodiscard]] double* data() noexcept { return data_.data(); }
79 [[nodiscard]] const double* data() const noexcept { return data_.data(); }
80
82 [[nodiscard]] Vector operator+(const Vector& other) const;
84 [[nodiscard]] Vector operator-(const Vector& other) const;
86 [[nodiscard]] Vector operator*(double scalar) const;
87
89 Vector& operator+=(const Vector& other);
91 Vector& operator-=(const Vector& other);
93 Vector& operator*=(double scalar);
94
96 [[nodiscard]] double sum() const;
97
99 void normalize();
100
102 [[nodiscard]] Vector normalized() const;
103
109 [[nodiscard]] bool is_equal(const Vector& other,
110 double abs_tol = 1e-12) const;
111
118 [[nodiscard]] bool is_near(const Vector& other,
119 double abs_tol = 1e-9,
120 double rel_tol = 1e-9) const;
121
123 friend Vector operator*(double scalar, const Vector& v);
124
125private:
126 std::vector<double> data_;
127
128 void check_same_size(const Vector& other, const char* op) const;
129};
130
136class Matrix {
137public:
139 Matrix() = default;
140
145 Matrix(std::size_t rows, std::size_t cols, double fill = 0.0);
146
148 Matrix(const Matrix&) = default;
150 Matrix(Matrix&&) noexcept = default;
152 Matrix& operator=(const Matrix&) = default;
154 Matrix& operator=(Matrix&&) noexcept = default;
155
157 [[nodiscard]] static Matrix identity(std::size_t n);
159 [[nodiscard]] static Matrix zeros(std::size_t rows, std::size_t cols);
161 [[nodiscard]] static Matrix ones(std::size_t rows, std::size_t cols);
162
164 [[nodiscard]] std::size_t rows() const noexcept { return rows_; }
166 [[nodiscard]] std::size_t cols() const noexcept { return cols_; }
168 [[nodiscard]] bool empty() const noexcept { return rows_ == 0 || cols_ == 0; }
169
176 double& operator()(std::size_t i, std::size_t j);
177
184 [[nodiscard]] const double& operator()(std::size_t i, std::size_t j) const;
185
187 [[nodiscard]] double* data() noexcept { return data_.data(); }
189 [[nodiscard]] const double* data() const noexcept { return data_.data(); }
190
192 [[nodiscard]] Matrix operator+(const Matrix& other) const;
194 [[nodiscard]] Matrix operator-(const Matrix& other) const;
196 [[nodiscard]] Matrix operator*(double scalar) const;
201 [[nodiscard]] Matrix operator*(const Matrix& other) const;
206 [[nodiscard]] Vector operator*(const Vector& v) const;
207
209 Matrix& operator+=(const Matrix& other);
211 Matrix& operator-=(const Matrix& other);
213 Matrix& operator*=(double scalar);
214
216 [[nodiscard]] Matrix transposed() const;
217
219 [[nodiscard]] Vector row_sums() const;
221 [[nodiscard]] Vector col_sums() const;
222
227
229 [[nodiscard]] Matrix rows_normalized() const;
231 [[nodiscard]] Matrix cols_normalized() const;
232
234 [[nodiscard]] bool is_equal(const Matrix& other,
235 double abs_tol = 1e-12) const;
237 [[nodiscard]] bool is_near(const Matrix& other,
238 double abs_tol = 1e-9,
239 double rel_tol = 1e-9) const;
240
242 friend Matrix operator*(double scalar, const Matrix& m);
243
244private:
245 std::size_t rows_ = 0;
246 std::size_t cols_ = 0;
247 std::vector<double> data_; // row-major
248
249 [[nodiscard]] std::size_t index(std::size_t i, std::size_t j) const;
250 void check_bounds(std::size_t i, std::size_t j) const;
251 void check_same_shape(const Matrix& other, const char* op) const;
252};
253
254} // namespace anpcpp
Thrown when vector/matrix dimensions do not match an operation.
Definition matrix.hpp:22
DimensionError(const std::string &message)
Definition matrix.hpp:27
Dense row-major matrix stored in a flat buffer.
Definition matrix.hpp:136
const double & operator()(std::size_t i, std::size_t j) const
Const element access.
static Matrix identity(std::size_t n)
Matrix operator*(double scalar) const
Scalar multiplication.
Matrix & operator+=(const Matrix &other)
In-place element-wise addition.
Matrix operator+(const Matrix &other) const
Element-wise addition.
Matrix operator*(const Matrix &other) const
Matrix product (this * other).
Matrix(const Matrix &)=default
Copy constructor.
void normalize_rows()
Divides each row by its sum; zero-sum rows are unchanged.
static Matrix ones(std::size_t rows, std::size_t cols)
std::size_t rows() const noexcept
Definition matrix.hpp:164
Matrix rows_normalized() const
Matrix transposed() const
Vector operator*(const Vector &v) const
Matrix-vector product.
Matrix & operator-=(const Matrix &other)
In-place element-wise subtraction.
Vector row_sums() const
bool is_near(const Matrix &other, double abs_tol=1e-9, double rel_tol=1e-9) const
Absolute and relative tolerance equality.
void normalize_cols()
Divides each column by its sum; zero-sum columns are unchanged.
Matrix(std::size_t rows, std::size_t cols, double fill=0.0)
Constructs an rows by cols matrix filled with fill.
Vector col_sums() const
static Matrix zeros(std::size_t rows, std::size_t cols)
double & operator()(std::size_t i, std::size_t j)
Mutable element access.
std::size_t cols() const noexcept
Definition matrix.hpp:166
bool empty() const noexcept
Definition matrix.hpp:168
bool is_equal(const Matrix &other, double abs_tol=1e-12) const
Per-element absolute tolerance equality.
friend Matrix operator*(double scalar, const Matrix &m)
Scalar multiplication from the left.
Matrix(Matrix &&) noexcept=default
Move constructor.
Matrix cols_normalized() const
double * data() noexcept
Definition matrix.hpp:187
Matrix & operator*=(double scalar)
In-place scalar multiplication.
Matrix operator-(const Matrix &other) const
Element-wise subtraction.
const double * data() const noexcept
Definition matrix.hpp:189
Matrix()=default
Default-constructs a 0x0 matrix.
One-dimensional array of doubles with element-wise arithmetic.
Definition matrix.hpp:34
Vector(Vector &&) noexcept=default
Move constructor.
Vector(std::size_t size, double fill=0.0)
Constructs a vector of the given length.
const double * data() const noexcept
Definition matrix.hpp:79
double * data() noexcept
Definition matrix.hpp:77
friend Vector operator*(double scalar, const Vector &v)
Scalar multiplication from the left (scalar * v).
Vector()=default
Default-constructs an empty vector.
bool empty() const noexcept
Definition matrix.hpp:58
std::size_t size() const noexcept
Definition matrix.hpp:56
const double & operator[](std::size_t i) const
Const element access.
Vector & operator+=(const Vector &other)
In-place element-wise addition.
Vector operator+(const Vector &other) const
Element-wise addition.
Vector(const Vector &)=default
Copy constructor (deep-copies element storage).
Vector normalized() const
bool is_equal(const Vector &other, double abs_tol=1e-12) const
Exact equality within absolute tolerance.
Vector operator*(double scalar) const
Scalar multiplication (each element times scalar).
Vector & operator*=(double scalar)
In-place scalar multiplication.
Vector & operator-=(const Vector &other)
In-place element-wise subtraction.
Vector operator-(const Vector &other) const
Element-wise subtraction.
double sum() const
void normalize()
Divides each element by sum(); no-op if sum is zero.
bool is_near(const Vector &other, double abs_tol=1e-9, double rel_tol=1e-9) const
Approximate equality (absolute and relative tolerances).
double & operator[](std::size_t i)
Mutable element access.
Analytic Network Process computational library.
Definition eigen.hpp:14