OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
GSLMatrix.h
Go to the documentation of this file.
1//
2// GSL Matrix/Vector compatibility to replace gsl_matrix, gsl_vector
3//
4// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17
18#ifndef OPAL_GSL_MATRIX_HH
19#define OPAL_GSL_MATRIX_HH
20
21#include <cstring>
22#include <stdexcept>
23#include <vector>
25
29struct gsl_matrix {
30 size_t size1;
31 size_t size2;
32 size_t tda; // physical row dimension
33 double* data;
34 size_t owner; // ownership flag
35
36 gsl_matrix() : size1(0), size2(0), tda(0), data(nullptr), owner(0) {}
37
38 double* ptr(size_t i, size_t j) { return data + i * tda + j; }
39
40 const double* ptr(size_t i, size_t j) const { return data + i * tda + j; }
41};
42
46 size_t size1;
47 size_t size2;
48 size_t tda;
50 size_t owner;
51
52 gsl_matrix_complex() : size1(0), size2(0), tda(0), data(nullptr), owner(0) {}
53
54 gsl_complex* ptr(size_t i, size_t j) { return data + i * tda + j; }
55
56 const gsl_complex* ptr(size_t i, size_t j) const { return data + i * tda + j; }
57};
58
61struct gsl_vector {
62 size_t size;
63 size_t stride;
64 double* data;
65 size_t owner;
66
67 gsl_vector() : size(0), stride(0), data(nullptr), owner(0) {}
68
69 double* ptr(size_t i) { return data + i * stride; }
70
71 const double* ptr(size_t i) const { return data + i * stride; }
72};
73
77 size_t size;
78 size_t stride;
80 size_t owner;
81
82 gsl_vector_complex() : size(0), stride(0), data(nullptr), owner(0) {}
83
84 gsl_complex* ptr(size_t i) { return data + i * stride; }
85
86 const gsl_complex* ptr(size_t i) const { return data + i * stride; }
87};
88
94inline gsl_matrix* gsl_matrix_alloc(size_t n1, size_t n2) {
95 gsl_matrix* m = new gsl_matrix();
96 m->size1 = n1;
97 m->size2 = n2;
98 m->tda = n2;
99 m->data = new double[n1 * n2];
100 m->owner = 1;
101 std::fill(m->data, m->data + n1 * n2, 0.0);
102 return m;
103}
104
109 if (m && m->owner) {
110 delete[] m->data;
111 }
112 delete m;
113}
114
120inline gsl_matrix_complex* gsl_matrix_complex_alloc(size_t n1, size_t n2) {
122 m->size1 = n1;
123 m->size2 = n2;
124 m->tda = n2;
125 m->data = new gsl_complex[n1 * n2];
126 m->owner = 1;
127 for (size_t i = 0; i < n1 * n2; ++i) {
128 m->data[i] = gsl_complex(0.0, 0.0);
129 }
130 return m;
131}
132
137 if (m && m->owner) {
138 delete[] m->data;
139 }
140 delete m;
141}
142
147inline gsl_vector* gsl_vector_alloc(size_t n) {
148 gsl_vector* v = new gsl_vector();
149 v->size = n;
150 v->stride = 1;
151 v->data = new double[n];
152 v->owner = 1;
153 std::fill(v->data, v->data + n, 0.0);
154 return v;
155}
156
161 if (v && v->owner) {
162 delete[] v->data;
163 }
164 delete v;
165}
166
173 v->size = n;
174 v->stride = 1;
175 v->data = new gsl_complex[n];
176 v->owner = 1;
177 for (size_t i = 0; i < n; ++i) {
178 v->data[i] = gsl_complex(0.0, 0.0);
179 }
180 return v;
181}
182
187 if (v && v->owner) {
188 delete[] v->data;
189 }
190 delete v;
191}
192
199inline double* gsl_matrix_ptr(gsl_matrix* m, size_t i, size_t j) { return m->ptr(i, j); }
200
207inline const double* gsl_matrix_ptr(const gsl_matrix* m, size_t i, size_t j) {
208 return const_cast<gsl_matrix*>(m)->ptr(i, j);
209}
210
218 return m->ptr(i, j);
219}
220
227inline const gsl_complex* gsl_matrix_complex_ptr(const gsl_matrix_complex* m, size_t i, size_t j) {
228 return const_cast<gsl_matrix_complex*>(m)->ptr(i, j);
229}
230
236inline double* gsl_vector_ptr(gsl_vector* v, size_t i) { return v->ptr(i); }
237
243inline const double* gsl_vector_ptr(const gsl_vector* v, size_t i) {
244 return const_cast<gsl_vector*>(v)->ptr(i);
245}
246
252inline gsl_complex* gsl_vector_complex_ptr(gsl_vector_complex* v, size_t i) { return v->ptr(i); }
253
259inline const gsl_complex* gsl_vector_complex_ptr(const gsl_vector_complex* v, size_t i) {
260 return const_cast<gsl_vector_complex*>(v)->ptr(i);
261}
262
268inline void gsl_matrix_memcpy(gsl_matrix* dest, const gsl_matrix* src) {
269 if (dest->size1 != src->size1 || dest->size2 != src->size2) {
270 throw std::runtime_error("gsl_matrix_memcpy: size mismatch");
271 }
272 std::memcpy(dest->data, src->data, src->size1 * src->size2 * sizeof(double));
273}
274
280 if (dest->size1 != src->size1 || dest->size2 != src->size2) {
281 throw std::runtime_error("gsl_matrix_complex_memcpy: size mismatch");
282 }
283 std::memcpy(dest->data, src->data, src->size1 * src->size2 * sizeof(gsl_complex));
284}
285
291inline void gsl_matrix_transpose_memcpy(gsl_matrix* dest, const gsl_matrix* src) {
292 if (dest->size1 != src->size2 || dest->size2 != src->size1) {
293 throw std::runtime_error("gsl_matrix_transpose_memcpy: size mismatch");
294 }
295 for (size_t i = 0; i < src->size1; ++i) {
296 for (size_t j = 0; j < src->size2; ++j) {
297 *gsl_matrix_ptr(dest, j, i) = *gsl_matrix_ptr(src, i, j);
298 }
299 }
300}
301
308 gsl_matrix_complex* dest, const gsl_matrix_complex* src) {
309 if (dest->size1 != src->size2 || dest->size2 != src->size1) {
310 throw std::runtime_error("gsl_matrix_complex_transpose_memcpy: size mismatch");
311 }
312 for (size_t i = 0; i < src->size1; ++i) {
313 for (size_t j = 0; j < src->size2; ++j) {
314 *gsl_matrix_complex_ptr(dest, j, i) = *gsl_matrix_complex_ptr(src, i, j);
315 }
316 }
317}
318
324inline void gsl_matrix_scale(gsl_matrix* m, double x) {
325 for (size_t i = 0; i < m->size1 * m->size2; ++i) {
326 m->data[i] *= x;
327 }
328}
329
336 for (size_t i = 0; i < m->size1 * m->size2; ++i) {
337 m->data[i] = gsl_complex_mul(m->data[i], x);
338 }
339}
340
346inline void gsl_matrix_add(gsl_matrix* a, const gsl_matrix* b) {
347 if (a->size1 != b->size1 || a->size2 != b->size2) {
348 throw std::runtime_error("gsl_matrix_add: size mismatch");
349 }
350 for (size_t i = 0; i < a->size1 * a->size2; ++i) {
351 a->data[i] += b->data[i];
352 }
353}
354
361 if (a->size1 != b->size1 || a->size2 != b->size2) {
362 throw std::runtime_error("gsl_matrix_complex_add: size mismatch");
363 }
364 for (size_t i = 0; i < a->size1 * a->size2; ++i) {
365 a->data[i] = gsl_complex_add(a->data[i], b->data[i]);
366 }
367}
368
370// \brief Copy \p src into \p dest (real vectors).
374inline void gsl_vector_memcpy(gsl_vector* dest, const gsl_vector* src) {
375 if (dest->size != src->size) {
376 throw std::runtime_error("gsl_vector_memcpy: size mismatch");
377 }
378 for (size_t i = 0; i < src->size; ++i) {
379 *gsl_vector_ptr(dest, i) = *gsl_vector_ptr(src, i);
380 }
381}
382
388 if (dest->size != src->size) {
389 throw std::runtime_error("gsl_vector_complex_memcpy: size mismatch");
390 }
391 for (size_t i = 0; i < src->size; ++i) {
393 }
394}
395
401inline void gsl_vector_scale(gsl_vector* v, double x) {
402 for (size_t i = 0; i < v->size; ++i) {
403 *gsl_vector_ptr(v, i) *= x;
404 }
405}
406
413 for (size_t i = 0; i < v->size; ++i) {
415 }
416}
417
423inline void gsl_vector_add(gsl_vector* a, const gsl_vector* b) {
424 if (a->size != b->size) {
425 throw std::runtime_error("gsl_vector_add: size mismatch");
426 }
427 for (size_t i = 0; i < a->size; ++i) {
428 *gsl_vector_ptr(a, i) += *gsl_vector_ptr(b, i);
429 }
430}
431
438 if (a->size != b->size) {
439 throw std::runtime_error("gsl_vector_complex_add: size mismatch");
440 }
441 for (size_t i = 0; i < a->size; ++i) {
444 }
445}
446
453inline void gsl_matrix_set(gsl_matrix* m, size_t i, size_t j, double x) {
454 *gsl_matrix_ptr(m, i, j) = x;
455}
456
463inline double gsl_matrix_get(const gsl_matrix* m, size_t i, size_t j) {
464 return *gsl_matrix_ptr(m, i, j);
465}
466
471inline void gsl_matrix_set_all(gsl_matrix* m, double x) {
472 for (size_t i = 0; i < m->size1 * m->size2; ++i) {
473 m->data[i] = x;
474 }
475}
476
481
488 size_t n = (m->size1 < m->size2) ? m->size1 : m->size2;
489 for (size_t i = 0; i < n; ++i) {
490 gsl_matrix_set(m, i, i, 1.0);
491 }
492}
493
500inline void gsl_matrix_complex_set(gsl_matrix_complex* m, size_t i, size_t j, gsl_complex x) {
501 *gsl_matrix_complex_ptr(m, i, j) = x;
502}
503
510inline gsl_complex gsl_matrix_complex_get(const gsl_matrix_complex* m, size_t i, size_t j) {
511 return *gsl_matrix_complex_ptr(m, i, j);
512}
513
518 for (size_t i = 0; i < m->size1 * m->size2; ++i) {
519 m->data[i] = gsl_complex(0.0, 0.0);
520 }
521}
522
528inline void gsl_vector_set(gsl_vector* v, size_t i, double x) { *gsl_vector_ptr(v, i) = x; }
529
535inline double gsl_vector_get(const gsl_vector* v, size_t i) { return *gsl_vector_ptr(v, i); }
536
541inline void gsl_vector_set_all(gsl_vector* v, double x) {
542 for (size_t i = 0; i < v->size; ++i) {
543 *gsl_vector_ptr(v, i) = x;
544 }
545}
546
551
558 *gsl_vector_complex_ptr(v, i) = x;
559}
560
567 return *gsl_vector_complex_ptr(v, i);
568}
569
574 for (size_t i = 0; i < v->size; ++i) {
575 *gsl_vector_complex_ptr(v, i) = gsl_complex(0.0, 0.0);
576 }
577}
578
579#endif // OPAL_GSL_MATRIX_HH
gsl_complex gsl_complex_mul(gsl_complex a, gsl_complex b)
Product .
Definition GSLComplex.h:97
gsl_complex gsl_complex_add(gsl_complex a, gsl_complex b)
Sum .
Definition GSLComplex.h:79
gsl_matrix * gsl_matrix_alloc(size_t n1, size_t n2)
Allocate a zero-initialized real matrix of size .
Definition GSLMatrix.h:94
void gsl_matrix_complex_transpose_memcpy(gsl_matrix_complex *dest, const gsl_matrix_complex *src)
Copy the transpose into dest (complex).
Definition GSLMatrix.h:307
void gsl_vector_complex_add(gsl_vector_complex *a, const gsl_vector_complex *b)
Add another complex vector in-place.
Definition GSLMatrix.h:437
void gsl_matrix_set(gsl_matrix *m, size_t i, size_t j, double x)
Set .
Definition GSLMatrix.h:453
double gsl_matrix_get(const gsl_matrix *m, size_t i, size_t j)
Get .
Definition GSLMatrix.h:463
gsl_complex gsl_matrix_complex_get(const gsl_matrix_complex *m, size_t i, size_t j)
Get complex entry .
Definition GSLMatrix.h:510
void gsl_matrix_free(gsl_matrix *m)
Free a matrix allocated by gsl_matrix_alloc.
Definition GSLMatrix.h:108
void gsl_vector_free(gsl_vector *v)
Free a vector allocated by gsl_vector_alloc.
Definition GSLMatrix.h:160
void gsl_vector_scale(gsl_vector *v, double x)
Scale a vector in-place.
Definition GSLMatrix.h:401
gsl_complex * gsl_vector_complex_ptr(gsl_vector_complex *v, size_t i)
Return pointer to element in a complex vector.
Definition GSLMatrix.h:252
void gsl_matrix_complex_scale(gsl_matrix_complex *m, gsl_complex x)
Scale a complex matrix in-place.
Definition GSLMatrix.h:335
gsl_vector * gsl_vector_alloc(size_t n)
Allocate a zero-initialized real vector of length .
Definition GSLMatrix.h:147
void gsl_matrix_complex_memcpy(gsl_matrix_complex *dest, const gsl_matrix_complex *src)
Copy src into dest (complex matrices).
Definition GSLMatrix.h:279
void gsl_vector_memcpy(gsl_vector *dest, const gsl_vector *src)
Definition GSLMatrix.h:374
void gsl_vector_complex_set_zero(gsl_vector_complex *v)
Set all complex entries to zero.
Definition GSLMatrix.h:573
void gsl_matrix_transpose_memcpy(gsl_matrix *dest, const gsl_matrix *src)
Copy the transpose into dest.
Definition GSLMatrix.h:291
void gsl_matrix_complex_free(gsl_matrix_complex *m)
Free a matrix allocated by gsl_matrix_complex_alloc.
Definition GSLMatrix.h:136
void gsl_vector_set_zero(gsl_vector *v)
Set all entries to zero.
Definition GSLMatrix.h:550
void gsl_vector_complex_scale(gsl_vector_complex *v, gsl_complex x)
Scale a complex vector in-place.
Definition GSLMatrix.h:412
double * gsl_vector_ptr(gsl_vector *v, size_t i)
Return pointer to element in a real vector.
Definition GSLMatrix.h:236
void gsl_matrix_scale(gsl_matrix *m, double x)
Scale a matrix in-place.
Definition GSLMatrix.h:324
void gsl_vector_complex_memcpy(gsl_vector_complex *dest, const gsl_vector_complex *src)
Copy src into dest (complex vectors).
Definition GSLMatrix.h:387
void gsl_matrix_complex_set(gsl_matrix_complex *m, size_t i, size_t j, gsl_complex x)
Set complex entry .
Definition GSLMatrix.h:500
gsl_complex gsl_vector_complex_get(const gsl_vector_complex *v, size_t i)
Get complex entry .
Definition GSLMatrix.h:566
void gsl_vector_set_all(gsl_vector *v, double x)
Set all entries to x.
Definition GSLMatrix.h:541
double gsl_vector_get(const gsl_vector *v, size_t i)
Get .
Definition GSLMatrix.h:535
double * gsl_matrix_ptr(gsl_matrix *m, size_t i, size_t j)
Return pointer to element in a real matrix.
Definition GSLMatrix.h:199
gsl_vector_complex * gsl_vector_complex_alloc(size_t n)
Allocate a zero-initialized complex vector of length .
Definition GSLMatrix.h:171
void gsl_matrix_set_all(gsl_matrix *m, double x)
Set all entries to x.
Definition GSLMatrix.h:471
void gsl_matrix_set_identity(gsl_matrix *m)
Set to identity on the main diagonal.
Definition GSLMatrix.h:486
void gsl_vector_complex_set(gsl_vector_complex *v, size_t i, gsl_complex x)
Set complex entry .
Definition GSLMatrix.h:557
void gsl_vector_complex_free(gsl_vector_complex *v)
Free a vector allocated by gsl_vector_complex_alloc.
Definition GSLMatrix.h:186
void gsl_matrix_complex_add(gsl_matrix_complex *a, const gsl_matrix_complex *b)
Add another complex matrix in-place.
Definition GSLMatrix.h:360
gsl_complex * gsl_matrix_complex_ptr(gsl_matrix_complex *m, size_t i, size_t j)
Return pointer to element in a complex matrix.
Definition GSLMatrix.h:217
void gsl_vector_set(gsl_vector *v, size_t i, double x)
Set .
Definition GSLMatrix.h:528
gsl_matrix_complex * gsl_matrix_complex_alloc(size_t n1, size_t n2)
Allocate a zero-initialized complex matrix of size .
Definition GSLMatrix.h:120
void gsl_matrix_complex_set_zero(gsl_matrix_complex *m)
Set all complex entries to zero.
Definition GSLMatrix.h:517
void gsl_matrix_memcpy(gsl_matrix *dest, const gsl_matrix *src)
Copy src into dest (real matrices).
Definition GSLMatrix.h:268
void gsl_matrix_add(gsl_matrix *a, const gsl_matrix *b)
Add another matrix in-place.
Definition GSLMatrix.h:346
void gsl_vector_add(gsl_vector *a, const gsl_vector *b)
Add another vector in-place.
Definition GSLMatrix.h:423
void gsl_matrix_set_zero(gsl_matrix *m)
Set all entries to zero.
Definition GSLMatrix.h:480
Complex number stored as .
Definition GSLComplex.h:27
Dense complex matrix in row-major storage.
Definition GSLMatrix.h:45
gsl_complex * data
Definition GSLMatrix.h:49
const gsl_complex * ptr(size_t i, size_t j) const
Definition GSLMatrix.h:56
gsl_complex * ptr(size_t i, size_t j)
Definition GSLMatrix.h:54
Dense real matrix in row-major storage.
Definition GSLMatrix.h:29
size_t tda
Definition GSLMatrix.h:32
size_t owner
Definition GSLMatrix.h:34
size_t size1
Definition GSLMatrix.h:30
double * ptr(size_t i, size_t j)
Definition GSLMatrix.h:38
double * data
Definition GSLMatrix.h:33
const double * ptr(size_t i, size_t j) const
Definition GSLMatrix.h:40
size_t size2
Definition GSLMatrix.h:31
Dense complex vector with stride.
Definition GSLMatrix.h:76
const gsl_complex * ptr(size_t i) const
Definition GSLMatrix.h:86
gsl_complex * ptr(size_t i)
Definition GSLMatrix.h:84
gsl_complex * data
Definition GSLMatrix.h:79
Dense real vector with stride.
Definition GSLMatrix.h:61
size_t stride
Definition GSLMatrix.h:63
double * data
Definition GSLMatrix.h:64
const double * ptr(size_t i) const
Definition GSLMatrix.h:71
size_t size
Definition GSLMatrix.h:62
double * ptr(size_t i)
Definition GSLMatrix.h:69
size_t owner
Definition GSLMatrix.h:65