reconciliation_lib 0.1
Library for data reconciliation algorithms
Loading...
Searching...
No Matches
gsl_wrapper.h
Go to the documentation of this file.
1#ifndef DVRLIB_GSL_WRAPPER_H
2#define DVRLIB_GSL_WRAPPER_H
3
4#include <gsl/gsl_vector.h>
5#include <gsl/gsl_matrix.h>
6
7#include <iostream>
8#include "dvr_assert.h"
9
10namespace dvrlib {
11
14 const char* reason;
15 const char* file;
16 int line;
18};
19
22
29void set_vdi_print_format(bool enable);
30
31class vector_view;
32
39class vector {
40 gsl_vector* v;
41
42public:
44 vector(int n);
46 vector(int n, double x);
48 vector(int n, const double* x);
50 vector(const vector& src);
52 ~vector();
53
55 gsl_vector* gsl_internal();
57 const gsl_vector* gsl_internal() const;
58
60 int size() const;
62 void set(int i, double val);
64 double get(int i) const;
66 double operator[](int i) const;
67
69 vector& operator=(const vector& src);
71 vector& operator=(const vector_view& src);
72 vector& operator+=(const vector& src);
73 vector operator+(const vector& src) const;
74 vector& operator-=(const vector& src);
75 vector operator-(const vector& src) const;
77 vector operator-() const;
78
80 vector& operator*=(double d);
82 vector operator*(double d) const;
84 double norm1() const;
86 double norm2() const;
87
94 vector_view subvector(int k, int n);
96 const vector_view subvector(int k, int n) const;
97 friend class matrix;
98};
99
101vector operator*(double d, const vector& src);
103std::ostream& operator<<(std::ostream& out, const vector& vec);
104
114 gsl_vector_view vv;
115 vector_view(const gsl_vector_view& vv);
116
117public:
119 vector_view(const vector_view& src);
120
122 int size() const;
124 double get(int i) const;
126 void set(int i, double val);
128 const gsl_vector* gsl_internal() const;
129
131 vector_view& operator=(const vector& src);
133 vector_view& operator=(const vector_view& src);
135 operator vector() const;
136
137 friend class vector;
138 friend class matrix;
139};
140
141class matrix_view;
142
149class matrix {
150 gsl_matrix* m;
151
152public:
163 matrix(int n1, int n2, bool id = false, const double* diag = nullptr);
165 matrix(int n1, int n2, const double* x);
171 template <int n>
172 matrix(int n1, int n2, const double (*x)[n]);
173
175 matrix(const matrix& src);
177 matrix(const gsl_matrix* src);
179 ~matrix();
180
182 gsl_matrix* gsl_internal();
184 const gsl_matrix* gsl_internal() const;
185
187 int size1() const;
189 int size2() const;
191 void set(int i, int j, double val);
193 double get(int i, int j) const;
195 vector_view operator[](int i);
197 const vector_view operator[](int i) const;
198
200 matrix& operator=(const matrix& src);
202 matrix& operator=(const matrix_view& src);
203
204 matrix operator+(const matrix& src) const;
205 matrix& operator+=(const matrix& src);
206 matrix operator-(const matrix& src) const;
207 matrix& operator-=(const matrix& src);
209 matrix operator-() const;
210
212 vector operator*(const vector& src) const;
214 matrix operator*(const matrix& src) const;
216 matrix operator*(double d) const;
218 matrix& operator*=(double d);
219
221 matrix transpose() const;
223 matrix inverse() const;
229 vector linsolve(const vector& b) const;
237 void svd(matrix& U, matrix& V, vector& s) const;
239 vector svd() const;
240
249 matrix_view submatrix(int k1, int k2, int n1, int n2);
251 const matrix_view submatrix(int k1, int k2, int n1, int n2) const;
252 friend class matrix_view;
253};
254
255template <int n>
256matrix::matrix(int n1, int n2, const double (*x)[n]) {
257 dvr_assert(n == n2);
258 m = gsl_matrix_alloc(n1, n2);
259 gsl_matrix_const_view src = gsl_matrix_const_view_array(x[0], n1, n2);
260 gsl_matrix_memcpy(m, &src.matrix);
261}
262
264matrix operator*(double d, const matrix& src);
265
267std::ostream& operator<<(std::ostream& out, const matrix& mat);
268
278 gsl_matrix_view mv;
279 matrix_view(const gsl_matrix_view& mv);
280
281public:
283 matrix_view(const matrix_view& src);
284
286 int size1() const;
288 int size2() const;
290 double get(int i, int j) const;
292 void set(int i, int j, double val);
294 const gsl_matrix* gsl_internal() const;
295
297 matrix_view& operator=(const matrix& src);
299 matrix_view& operator=(const matrix_view& src);
301 operator matrix() const;
302
303 friend class matrix;
304};
305
306std::ostream& operator<<(std::ostream& out, const gsl_vector& v);
307std::ostream& operator<<(std::ostream& out, const gsl_matrix& m);
308std::ostream& operator<<(std::ostream& out, const gsl_matrix_view& mv);
309
310} // namespace dvrlib
311
312#endif // DVRLIB_GSL_WRAPPER_H
Non-owning view into a rectangular sub-region of a matrix.
Definition gsl_wrapper.h:277
int size1() const
Return the number of rows.
Definition gsl_wrapper.cc:534
void set(int i, int j, double val)
Set element (i, j) to val (modifies the underlying data).
Definition gsl_wrapper.cc:546
matrix_view & operator=(const matrix &src)
Copy values from src into the viewed memory.
Definition gsl_wrapper.cc:554
gsl_matrix_view mv
Definition gsl_wrapper.h:278
double get(int i, int j) const
Return element (i, j).
Definition gsl_wrapper.cc:542
friend class matrix
Definition gsl_wrapper.h:303
const gsl_matrix * gsl_internal() const
Return a read-only pointer to the underlying gsl_matrix.
Definition gsl_wrapper.cc:550
int size2() const
Return the number of columns.
Definition gsl_wrapper.cc:538
RAII wrapper around a heap-allocated gsl_matrix.
Definition gsl_wrapper.h:149
vector operator*(const vector &src) const
Matrix–vector product.
Definition gsl_wrapper.cc:376
gsl_matrix * gsl_internal()
Return a pointer to the underlying gsl_matrix (mutable).
Definition gsl_wrapper.cc:296
vector svd() const
Compute and return only the singular values.
Definition gsl_wrapper.cc:447
matrix & operator+=(const matrix &src)
Definition gsl_wrapper.cc:351
matrix & operator=(const matrix &src)
Copy-assign from another matrix.
Definition gsl_wrapper.cc:330
matrix_view submatrix(int k1, int k2, int n1, int n2)
Return a view of the n1 × n2 sub-matrix starting at (k1, k2).
Definition gsl_wrapper.cc:459
matrix & operator*=(double d)
Scale all elements by d in place.
Definition gsl_wrapper.cc:397
vector linsolve(const vector &b) const
Solve the linear system A*x = b and return x.
Definition gsl_wrapper.cc:428
matrix operator+(const matrix &src) const
Definition gsl_wrapper.cc:342
matrix operator-() const
Unary negation — returns -(*this).
Definition gsl_wrapper.cc:370
double get(int i, int j) const
Return element (i, j).
Definition gsl_wrapper.cc:316
matrix(int n1, int n2, bool id=false, const double *diag=nullptr)
Allocate an n1 × n2 matrix.
Definition gsl_wrapper.cc:264
gsl_matrix * m
Definition gsl_wrapper.h:150
matrix & operator-=(const matrix &src)
Definition gsl_wrapper.cc:365
int size2() const
Return the number of columns.
Definition gsl_wrapper.cc:308
void set(int i, int j, double val)
Set element (i, j) to val.
Definition gsl_wrapper.cc:312
matrix transpose() const
Return the transpose.
Definition gsl_wrapper.cc:402
int size1() const
Return the number of rows.
Definition gsl_wrapper.cc:304
~matrix()
Destructor — frees the underlying GSL allocation.
Definition gsl_wrapper.cc:292
vector_view operator[](int i)
Return a mutable view of row i.
Definition gsl_wrapper.cc:320
matrix inverse() const
Return the inverse (via LU decomposition).
Definition gsl_wrapper.cc:418
Non-owning view into a contiguous slice of a vector or matrix row/column.
Definition gsl_wrapper.h:113
gsl_vector_view vv
Definition gsl_wrapper.h:114
vector_view & operator=(const vector &src)
Copy values from src into the viewed memory.
Definition gsl_wrapper.cc:244
const gsl_vector * gsl_internal() const
Return a read-only pointer to the underlying gsl_vector.
Definition gsl_wrapper.cc:240
void set(int i, double val)
Set element i to val (modifies the underlying data).
Definition gsl_wrapper.cc:236
int size() const
Return the number of elements.
Definition gsl_wrapper.cc:228
double get(int i) const
Return element i.
Definition gsl_wrapper.cc:232
friend class vector
Definition gsl_wrapper.h:137
RAII wrapper around a heap-allocated gsl_vector.
Definition gsl_wrapper.h:39
vector & operator=(const vector &src)
Copy-assign from another vector.
Definition gsl_wrapper.cc:108
vector operator*(double d) const
Return a copy scaled by d.
Definition gsl_wrapper.cc:153
double get(int i) const
Return element i.
Definition gsl_wrapper.cc:100
gsl_vector * gsl_internal()
Return a pointer to the underlying gsl_vector (mutable).
Definition gsl_wrapper.cc:84
void set(int i, double val)
Set element i to val.
Definition gsl_wrapper.cc:96
~vector()
Destructor — frees the underlying GSL allocation.
Definition gsl_wrapper.cc:80
vector & operator+=(const vector &src)
Definition gsl_wrapper.cc:120
vector operator+(const vector &src) const
Definition gsl_wrapper.cc:125
vector & operator-=(const vector &src)
Definition gsl_wrapper.cc:131
vector_view subvector(int k, int n)
Return a view of n elements starting at index k.
Definition gsl_wrapper.cc:159
vector operator-() const
Unary negation — returns -(*this).
Definition gsl_wrapper.cc:142
vector & operator*=(double d)
Scale all elements by d in place.
Definition gsl_wrapper.cc:148
double norm1() const
Return the L1 norm (sum of absolute values).
Definition gsl_wrapper.cc:173
gsl_vector * v
Definition gsl_wrapper.h:40
double operator[](int i) const
Return element i (read-only subscript).
Definition gsl_wrapper.cc:104
double norm2() const
Return the L2 (Euclidean) norm.
Definition gsl_wrapper.cc:177
int size() const
Return the number of elements.
Definition gsl_wrapper.cc:92
#define dvr_assert(cond)
Definition dvr_assert.h:18
Definition dvr_assert.h:7
std::ostream & operator<<(std::ostream &out, const vector &vec)
Write a vector to an output stream.
Definition gsl_wrapper.cc:209
vector operator*(double d, const vector &src)
Scalar multiplication d*src (commutative with vector::operator*).
Definition gsl_wrapper.cc:169
void gsl_enable_exceptions()
Install a GSL error handler that throws gsl_exception instead of aborting.
Definition gsl_wrapper.cc:19
void set_vdi_print_format(bool enable)
Switch between standard and VDI 2048 pretty-print format for vectors and matrices written to std::ost...
Definition gsl_wrapper.cc:25
Exception type thrown by GSL when gsl_enable_exceptions() is active.
Definition gsl_wrapper.h:13
const char * file
Source file where the error occurred.
Definition gsl_wrapper.h:15
const char * reason
Human-readable error description.
Definition gsl_wrapper.h:14
int gsl_errno
GSL error code (see gsl_errno.h)
Definition gsl_wrapper.h:17
int line
Line number where the error occurred.
Definition gsl_wrapper.h:16