PineForge v0.13.1-379-g9b50973
Deterministic PineScript v6 backtest runtime — C ABI reference
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2#include <Eigen/Dense>
3#include <memory>
4#include <vector>
5#include <stdexcept>
6#include <utility>
7
8namespace pineforge {
9
10class NumericMatrix {
11 struct Storage {
12 Eigen::MatrixXd data;
13
14 Storage() = default;
15 explicit Storage(Eigen::MatrixXd value)
16 : data(std::move(value)) {}
17 };
18
19 static constexpr const char* kNaIdError =
20 "matrix operation on na ID";
21 static constexpr const char* kInvalidSnapshotError =
22 "matrix restore from invalid snapshot";
23
24 std::shared_ptr<Storage> storage_;
25
26 explicit NumericMatrix(Eigen::MatrixXd data)
27 : storage_(std::make_shared<Storage>(std::move(data))) {}
28
29 [[nodiscard]] Storage& require_storage();
30 [[nodiscard]] const Storage& require_storage() const;
31
32public:
33 // A NumericMatrix is a reference handle (a source language's matrix ID):
34 // ordinary C++ copies and assignments alias one backing store, and
35 // copy() is the operation that allocates an independent handle.
36 NumericMatrix() noexcept = default;
37 NumericMatrix(const NumericMatrix&) noexcept = default;
38 NumericMatrix& operator=(const NumericMatrix&) noexcept = default;
39
40 // Treat C++ moves like handle assignment as well. A compiler-generated
41 // move must not turn the source handle into na.
42 NumericMatrix(NumericMatrix&& other) noexcept : storage_(other.storage_) {}
43 NumericMatrix& operator=(NumericMatrix&& other) noexcept {
44 if (this != &other) storage_ = other.storage_;
45 return *this;
46 }
47
48 // Opaque generated-checkpoint hook. The snapshot owns a detached value
49 // copy of the matrix contents and retains the original ID. restore()
50 // mutates that ID in place before reattaching the receiving handle, so all
51 // aliases observe rollback even if the receiver was rebound or set to na.
52 class Snapshot {
53 std::shared_ptr<Storage> identity_;
54 Eigen::MatrixXd state_;
55
56 Snapshot(std::shared_ptr<Storage> identity,
57 const Eigen::MatrixXd& state)
58 : identity_(std::move(identity)), state_(state) {}
59
60 friend class NumericMatrix;
61
62 public:
63 Snapshot(const Snapshot&) = default;
64 Snapshot& operator=(const Snapshot&) = default;
65 Snapshot(Snapshot&&) = default;
66 Snapshot& operator=(Snapshot&&) = default;
67 };
68
69 // Construction
70 static NumericMatrix new_(int rows, int cols, double init_val = 0.0);
71
72 // Access
73 double get(int row, int col) const;
74 void set(int row, int col, double val);
75 void fill(double val);
76 std::vector<double> row(int idx) const;
77 std::vector<double> col(int idx) const;
78
79 // Row/Col ops
80 int rows() const;
81 int columns() const;
82 void add_row(int idx, const std::vector<double>& values);
83 void add_col(int idx, const std::vector<double>& values);
84 void remove_row(int idx);
85 void remove_col(int idx);
86
87 // Swap
88 void swap_rows(int i, int j);
89 void swap_columns(int i, int j);
90
91 // Transform
92 NumericMatrix copy() const;
93 NumericMatrix submatrix(int from_row, int to_row, int from_col, int to_col) const;
94 void reshape(int rows, int cols);
95 void reverse();
96 NumericMatrix transpose() const;
97 void sort(int column, bool ascending = true);
98 NumericMatrix concat(const NumericMatrix& other, bool horizontal) const;
99
100 // Aggregation
101 double avg() const;
102 double min() const;
103 double max() const;
104 double mode() const;
105 double sum() const;
106
107 // Arithmetic
108 NumericMatrix diff(const NumericMatrix& other) const;
109 NumericMatrix mult(const NumericMatrix& other) const;
110 NumericMatrix pow(int n) const;
111
112 // Linear algebra
113 double det() const;
114 NumericMatrix inv() const;
115 NumericMatrix pinv() const;
116 int rank() const;
117 double trace() const;
118 std::vector<double> eigenvalues() const;
119 NumericMatrix eigenvectors() const;
120
121 // Kronecker
122 NumericMatrix kron(const NumericMatrix& other) const;
123
124 // Count
125 int elements_count() const;
126
127 // Handle state. A default-constructed value represents ``na``; new_()
128 // returns a valid handle even when its dimensions are 0x0.
129 [[nodiscard]] bool is_na() const noexcept { return !storage_; }
130
131 [[nodiscard]] Snapshot snapshot() const;
133
134 // Properties
135 bool is_square() const;
136 bool is_identity() const;
137 bool is_diagonal() const;
138 bool is_antidiagonal() const;
139 bool is_symmetric() const;
140 bool is_antisymmetric() const;
141 bool is_triangular() const;
142 bool is_stochastic() const;
143 bool is_binary() const;
144 bool is_zero() const;
145
146 // Access to internal data for friend operations
147 const Eigen::MatrixXd& data() const { return require_storage().data; }
148 Eigen::MatrixXd& data() { return require_storage().data; }
149};
150
151inline bool is_na(const NumericMatrix& matrix) noexcept {
152 return matrix.is_na();
153}
154
155// Deprecated spelling, kept as an exact alias so generated code and the
156// source adapter compile unchanged (R5 lane N14, the same treatment
157// session_time.hpp / str_utils.hpp received in lane L11). New code names
158// NumericMatrix.
160
161} // namespace pineforge
Snapshot & operator=(const Snapshot &)=default
Snapshot(const Snapshot &)=default
Snapshot & operator=(Snapshot &&)=default
NumericMatrix transpose() const
static NumericMatrix new_(int rows, int cols, double init_val=0.0)
NumericMatrix() noexcept=default
void swap_columns(int i, int j)
bool is_stochastic() const
bool is_na() const noexcept
Definition matrix.hpp:129
std::vector< double > row(int idx) const
void add_row(int idx, const std::vector< double > &values)
NumericMatrix kron(const NumericMatrix &other) const
NumericMatrix & operator=(NumericMatrix &&other) noexcept
Definition matrix.hpp:43
void add_col(int idx, const std::vector< double > &values)
NumericMatrix copy() const
NumericMatrix pow(int n) const
NumericMatrix inv() const
NumericMatrix eigenvectors() const
bool is_antidiagonal() const
void set(int row, int col, double val)
std::vector< double > col(int idx) const
Snapshot snapshot() const
NumericMatrix pinv() const
void sort(int column, bool ascending=true)
void reshape(int rows, int cols)
void restore(const Snapshot &snapshot)
bool is_antisymmetric() const
void swap_rows(int i, int j)
NumericMatrix concat(const NumericMatrix &other, bool horizontal) const
NumericMatrix mult(const NumericMatrix &other) const
NumericMatrix submatrix(int from_row, int to_row, int from_col, int to_col) const
bool is_triangular() const
std::vector< double > eigenvalues() const
const Eigen::MatrixXd & data() const
Definition matrix.hpp:147
Eigen::MatrixXd & data()
Definition matrix.hpp:148
NumericMatrix diff(const NumericMatrix &other) const
void remove_col(int idx)
void fill(double val)
double get(int row, int col) const
void remove_row(int idx)
NumericMatrix PineMatrix
Definition matrix.hpp:159
bool is_na(const Line &h)
Definition drawing.hpp:42