xmatrix.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xmath.h"
4 #include "xmatrix6.h"
5 #include "xtools.h"
6 #include "xvector.h"
7 #include <gsl/gsl_matrix.h>
8 #include <vector>
9 
10 namespace nmx::gsl {
11 
12 using namespace nmx::gsl::mat;
13 
17 class MatrixView : public IMBase<MatrixView>, public IMCalc<MatrixView>
18 {
19 private:
20  gsl_matrix_view _view;
21 
22 public:
27  MatrixView(gsl_matrix_view v) { _view = v; }
28 
35  MatrixView(size_t nr, size_t nc, double f[]) { //
36  _view = gsl_matrix_view_array(f, nr, nc);
37  }
38 
43  inline const gsl_matrix *gsl() const { return &_view.matrix; }
44 
49  inline gsl_matrix *gsl() { return &_view.matrix; }
50 }; //MatrixView
51 
55 class Matrix : public IMBase<Matrix>,
56  public IMProperties<Matrix>,
57  public IMCalc<Matrix>,
58  public IMBlas<Matrix, Vector>,
59  public IMExchange<Matrix>,
60  public IMMinMax<Matrix>,
61  public IRowColumns<Matrix>,
62  public IRowColumnsViews<Matrix>
63 {
64 public:
65  using View = MatrixView;
66 
67 private:
68  gsl_matrix *_matrix = nullptr;
69 
70 public:
75  inline Matrix() {}
76 
82  explicit inline Matrix(size_t rows, size_t columns) { //
83  _matrix = gsl_matrix_calloc(rows, columns);
84  }
85 
93  explicit inline Matrix(size_t rows, size_t columns, double val)
94  : Matrix(rows, columns) {
95  gsl_matrix_set_all(gsl(), val);
96  }
97 
103  inline Matrix(std::initializer_list<std::initializer_list<double>> lst)
104  : Matrix() {
105  //Anzahl der verschachtelten Listen:
106  // für { {1,2,3},{4,5,6},{6,7,8}} ist die Anzahl 3
107  size_t irows = lst.size();
108  size_t iclmns = 0;
109  // suche maximale Spaltenzahl
110  for (const auto &row : lst) {
111  iclmns = std::max(iclmns, row.size());
112  }
113  //erzeuge Speicher, alle Element sind 0
114  _matrix = gsl_matrix_calloc(irows, iclmns);
115  //fülle Vektor
116  size_t i = 0, j = 0;
117  //jedes l ist eine verschachtelte Liste
118  for (const auto &l : lst) {
119  for (const auto &val : l) {
120  //setze Element (i,j) = val
121  gsl_matrix_set(gsl(), i, j, val);
122  ++j;
123  }
124  j = 0;
125  ++i;
126  }
127  }
128 
133  inline Matrix(const Matrix &m)
134  : Matrix(m.rows(), m.columns()) {
135  gsl_matrix_memcpy(gsl(), m.gsl());
136  }
137 
142  inline Matrix(Matrix &&m)
143  : Matrix() {
144  std::swap(_matrix, m._matrix);
145  m._matrix = nullptr;
146  }
147 
151  inline ~Matrix() {
152  if (_matrix != nullptr) {
153  gsl_matrix_free(_matrix);
154  }
155  }
156 
161  inline const gsl_matrix *gsl() const { return *&_matrix; }
162 
167  inline gsl_matrix *gsl() { return *&_matrix; }
168 
174  inline Matrix &operator=(const Matrix &m) {
175  if (this != &m) {
176  if (_matrix != nullptr) {
177  //gebe Speicher frei
178  gsl_matrix_free(gsl());
179  }
180  //erzeuge neuen Speicher
181  _matrix = gsl_matrix_alloc(m.rows(), m.columns());
182  //kopiere Matrix
183  gsl_matrix_memcpy(gsl(), m.gsl());
184  }
185  return *this;
186  }
187 
193  inline Matrix &operator=(Matrix &&m) {
194  if (this != &m) {
195  if (_matrix != nullptr) {
196  //gebe Speicher frei
197  gsl_matrix_free(gsl());
198  _matrix = nullptr;
199  }
200  //vertausche Zeiger auf gsl-Matrix
201  std::swap(_matrix, m._matrix);
202  }
203  return *this;
204  }
205 
211  inline static Matrix identity(size_t N) {
212  Matrix m{ N, N };
213  gsl_matrix_set_identity(m.gsl());
214  return m;
215  }
216 }; //Matrix
217 
218 } // namespace nmx::gsl
Matrix(size_t rows, size_t columns)
Matrix Konstruktor erzeugt eine Nullmatrix.
Definition: xmatrix.h:82
Matrix(std::initializer_list< std::initializer_list< double >> lst)
Matrix Konstruktor.
Definition: xmatrix.h:103
The Matrix class Schnittstelle zur gsl Bibliothek.
Definition: xmatrix.h:55
The IMMinMax class Suche das minimale und maximale Element einer Matrix.
Definition: xmatrix2.h:12
The IMProperties struct.
Definition: xmatrix6.h:11
Matrix(size_t rows, size_t columns, double val)
Matrix Konstruktor in der alle Elemente denselben Wert haben.
Definition: xmatrix.h:93
Matrix(Matrix &&m)
Matrix move Konstruktor.
Definition: xmatrix.h:142
The IRowColumns struct Kopien von Spalten und Reihen erzeugen.
Definition: xmatrix4.h:60
The IMBase class Basisfunktionalität für eine gsl-Matrix in Form einer Komponente T ist die Klasse...
Definition: xmatrix0.h:14
The IRowColumns struct erzeugt Sichten auf Reihen und Spalten einer Matrix.
Definition: xmatrix4.h:13
Matrix(const Matrix &m)
Matrix Kopierkonstruktor.
Definition: xmatrix.h:133
static Matrix identity(size_t N)
identity Erzeugt eine NxN Einheitsmatrix
Definition: xmatrix.h:211
Matrix & operator=(Matrix &&m)
operator = Move-Zuweisungsoperator
Definition: xmatrix.h:193
The IMCalc class kombinierte Rechenoperationen und Zuweisungen.
Definition: xmatrix1.h:12
size_t columns() const
columns
Definition: xmatrix0.h:30
~Matrix()
~Matrix
Definition: xmatrix.h:151
size_t rows() const
rows
Definition: xmatrix0.h:24
The IMExchange struct.
Definition: xmatrix3.h:11
The IMBlas class Schnittstelle zur BLAS mit Funktionen für Matrix-Vektor- und Matrix-Matrix-Multiplik...
Definition: xmatrix5.h:12
Matrix & operator=(const Matrix &m)
operator = erzeuge Kopie
Definition: xmatrix.h:174
gsl_matrix * gsl()
gsl_vec ermöglicht direkten Zugriff auf gsl Funktionen
Definition: xmatrix.h:167
const gsl_matrix * gsl() const
gsl_vec ermöglicht direkten Zugriff auf gsl Funktionen
Definition: xmatrix.h:161
MatrixView(gsl_matrix_view v)
MView Konstruktor.
Definition: xmatrix.h:27
The MView class Sicht auf eine gsl-Matrix.
Definition: xmatrix.h:17
double f(double x, void *params)
f
Definition: x035.h:15
MatrixView(size_t nr, size_t nc, double f[])
MView Konstruktor Sicht auf C-Feld.
Definition: xmatrix.h:35
const gsl_matrix * gsl() const
gsl ermöglicht direkten Zugriff auf gsl Funktionen
Definition: xmatrix.h:43
gsl_matrix * gsl()
gsl ermöglicht direkten Zugriff auf gsl Funktionen
Definition: xmatrix.h:49
Matrix()
Matrix Konstruktor erzeugt eine leere Matrix ohne Speicherreservierung.
Definition: xmatrix.h:75