LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_UTIL_MATRIX_H
6#define LINE_UTIL_MATRIX_H
7
8/**
9 * @file
10 * @ingroup line_util
11 * Dense matrix and non-owning view.
12 *
13 * API functions take MatrixView<const T> for inputs and return Matrix<T>, so a
14 * caller that already holds the data (a numpy array through pybind11, a JSON
15 * buffer, another algorithm's output) passes it without a copy. Storage is
16 * row-major with an explicit row stride so a view can also address a
17 * submatrix or a column-major buffer transposed.
18 */
19
20#include <cstddef>
21#include <initializer_list>
22#include <vector>
23
24#include "line/num/number.h"
25#include "line/util/error.h"
26
27namespace line {
28
29template <class T>
31public:
32 MatrixView() : data_(nullptr), rows_(0), cols_(0), stride_(0) {}
33 MatrixView(T* data, std::size_t rows, std::size_t cols)
34 : data_(data), rows_(rows), cols_(cols), stride_(cols) {}
35 MatrixView(T* data, std::size_t rows, std::size_t cols, std::size_t stride)
36 : data_(data), rows_(rows), cols_(cols), stride_(stride) {}
37
38 std::size_t rows() const { return rows_; }
39 std::size_t cols() const { return cols_; }
40 std::size_t stride() const { return stride_; }
41 bool empty() const { return rows_ == 0 || cols_ == 0; }
42 T* data() const { return data_; }
43
44 T& operator()(std::size_t i, std::size_t j) const { return data_[i * stride_ + j]; }
45 T& at(std::size_t i, std::size_t j) const {
46 if (i >= rows_ || j >= cols_) throw InputError("MatrixView index out of range");
47 return data_[i * stride_ + j];
48 }
49
50private:
51 T* data_;
52 std::size_t rows_, cols_, stride_;
53};
54
55template <class T>
56class Matrix {
57public:
58 Matrix() : rows_(0), cols_(0) {}
59 Matrix(std::size_t rows, std::size_t cols) : rows_(rows), cols_(cols), v_(rows * cols) {}
60 Matrix(std::size_t rows, std::size_t cols, const T& fill)
61 : rows_(rows), cols_(cols), v_(rows * cols, fill) {}
62
63 /** Row-major initializer, e.g. Matrix<double>{{1,2},{3,4}}. */
64 Matrix(std::initializer_list<std::initializer_list<T>> rows) : rows_(rows.size()), cols_(0) {
65 for (const auto& r : rows) cols_ = r.size() > cols_ ? r.size() : cols_;
66 v_.assign(rows_ * cols_, T());
67 std::size_t i = 0;
68 for (const auto& r : rows) {
69 std::size_t j = 0;
70 for (const auto& x : r) v_[i * cols_ + (j++)] = x;
71 ++i;
72 }
73 }
74
75 static Matrix row(std::initializer_list<T> vals) {
76 Matrix m(1, vals.size());
77 std::size_t j = 0;
78 for (const auto& x : vals) m(0, j++) = x;
79 return m;
80 }
81
82 static Matrix col(std::initializer_list<T> vals) {
83 Matrix m(vals.size(), 1);
84 std::size_t i = 0;
85 for (const auto& x : vals) m(i++, 0) = x;
86 return m;
87 }
88
89 std::size_t rows() const { return rows_; }
90 std::size_t cols() const { return cols_; }
91 std::size_t size() const { return v_.size(); }
92 bool empty() const { return v_.empty(); }
93
94 T* data() { return v_.data(); }
95 const T* data() const { return v_.data(); }
96
97 T& operator()(std::size_t i, std::size_t j) { return v_[i * cols_ + j]; }
98 const T& operator()(std::size_t i, std::size_t j) const { return v_[i * cols_ + j]; }
99
100 /** Linear access, for vectors held as 1 x n or n x 1. */
101 T& operator[](std::size_t k) { return v_[k]; }
102 const T& operator[](std::size_t k) const { return v_[k]; }
103
104 MatrixView<T> view() { return MatrixView<T>(v_.data(), rows_, cols_); }
105 MatrixView<const T> view() const { return MatrixView<const T>(v_.data(), rows_, cols_); }
106 operator MatrixView<const T>() const { return view(); }
107
108 void fill(const T& x) { v_.assign(v_.size(), x); }
109
111 Matrix r(cols_, rows_);
112 for (std::size_t i = 0; i < rows_; ++i)
113 for (std::size_t j = 0; j < cols_; ++j) r(j, i) = (*this)(i, j);
114 return r;
115 }
116
117 T sum() const {
118 T s = T();
119 for (const auto& x : v_) s += x;
120 return s;
121 }
122
123private:
124 std::size_t rows_, cols_;
125 std::vector<T> v_;
126};
127
128/** Deep copy of a view into an owning matrix, converting the element type. */
129template <class T, class S>
131 Matrix<T> m(v.rows(), v.cols());
132 for (std::size_t i = 0; i < v.rows(); ++i)
133 for (std::size_t j = 0; j < v.cols(); ++j) m(i, j) = num_traits<T>::from_double(double(v(i, j)));
134 return m;
135}
136
137} // namespace line
138
139#endif // LINE_UTIL_MATRIX_H
InputError(const std::string &what)
Definition error.h:39
MatrixView(T *data, std::size_t rows, std::size_t cols)
Definition matrix.h:33
bool empty() const
Definition matrix.h:41
MatrixView(T *data, std::size_t rows, std::size_t cols, std::size_t stride)
Definition matrix.h:35
std::size_t rows() const
Definition matrix.h:38
std::size_t cols() const
Definition matrix.h:39
T * data() const
Definition matrix.h:42
std::size_t stride() const
Definition matrix.h:40
T & at(std::size_t i, std::size_t j) const
Definition matrix.h:45
T & operator()(std::size_t i, std::size_t j) const
Definition matrix.h:44
Matrix(std::size_t rows, std::size_t cols)
Definition matrix.h:59
T & operator[](std::size_t k)
Linear access, for vectors held as 1 x n or n x 1.
Definition matrix.h:101
MatrixView< T > view()
Definition matrix.h:104
const T & operator[](std::size_t k) const
Definition matrix.h:102
MatrixView< const T > view() const
Definition matrix.h:105
void fill(const Complex &x)
Definition matrix.h:108
T sum() const
Definition matrix.h:117
const T * data() const
Definition matrix.h:95
std::size_t size() const
Definition matrix.h:91
const T & operator()(std::size_t i, std::size_t j) const
Definition matrix.h:98
T * data()
Definition matrix.h:94
T & operator()(std::size_t i, std::size_t j)
Definition matrix.h:97
std::size_t cols() const
Definition matrix.h:90
Matrix(std::initializer_list< std::initializer_list< T > > rows)
Row-major initializer, e.g.
Definition matrix.h:64
std::size_t rows() const
Definition matrix.h:89
bool empty() const
Definition matrix.h:92
static Matrix col(std::initializer_list< T > vals)
Definition matrix.h:82
static Matrix row(std::initializer_list< T > vals)
Definition matrix.h:75
Matrix transpose() const
Definition matrix.h:110
Matrix(std::size_t rows, std::size_t cols, const T &fill)
Definition matrix.h:60
The exception types the port throws.
Matrix< T > matrix_from(const MatrixView< S > &v)
Deep copy of a view into an owning matrix, converting the element type.
Definition matrix.h:130
Number-type abstraction for the templated API port.