x030.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xmatrix.h"
4 #include "xvector.h"
5 #include <gsl/gsl_matrix.h>
6 #include <iomanip>
7 #include <iostream>
8 
9 namespace nmx::apps::x030 {
10 
14 inline void ex1() {
15  //Spalten und Reihen der Matrix
16  const size_t nrows = 4, nclmns = 4;
17 
18  //reserviere Speicher für die Matrix
19  gsl_matrix *m = gsl_matrix_alloc(nrows, nclmns);
20 
21  //Initialisierung der Elemente
22  for (size_t i = 0; i < nrows; i++) {
23  for (size_t j = 0; j < nclmns; j++) {
24  gsl_matrix_set(m, i, j, std::pow(sin(i + j), 2));
25  }
26  }
27  //Ausgabe der Matrix auf dem Bildschirm
28  std::cout << std::setprecision(10) << std::scientific;
29  for (size_t i = 0; i < nrows; i++) {
30  for (size_t j = 0; j < nclmns; j++) {
31  std::cout << std::setw(10) //
32  << gsl_matrix_get(m, i, j) << "\t";
33  }
34  std::cout << "\n";
35  }
36 
37  //vertausche Reihen 2 und 3
38  gsl_matrix_swap_rows(m, 2, 3);
39 
40  //Ausgabe der geänderten Matrix auf dem Bildschirm
41  std::cout << "--------------------" << std::endl;
42  for (size_t i = 0; i < nrows; i++) {
43  for (size_t j = 0; j < nclmns; j++) {
44  std::cout << std::setw(10) //
45  << gsl_matrix_get(m, i, j) << "\t";
46  }
47  std::cout << "\n";
48  }
49 
50  //Freigabe des Speichers
51  gsl_matrix_free(m);
52 }
53 
57 inline void ex2() {
58  //Spalten und Reihen der Matrix
59  const size_t nrows = 4, nclmns = 4;
60 
61  //statisches C-Feld
62  double data[16];
63 
64  //eine Sicht auf das C-Feld. Die Daten werden als Matrix verwaltet
65  gsl_matrix_view m = gsl_matrix_view_array(data, nrows, nclmns);
66 
67  //Initialisierung der Elemente
68  for (size_t i = 0; i < nrows; i++) {
69  for (size_t j = 0; j < nclmns; j++) {
70  gsl_matrix_set(&m.matrix, i, j, std::pow(sin(i + j), 2));
71  }
72  }
73 
74  //Ausgabe der geänderten Matrix auf dem Bildschirm
75  std::cout << std::setprecision(10) << std::scientific;
76  for (size_t i = 0; i < nrows; i++) {
77  for (size_t j = 0; j < nclmns; j++) {
78  std::cout << std::setw(10) //
79  << gsl_matrix_get(&m.matrix, i, j) << "\t";
80  }
81  std::cout << "\n";
82  }
83 
84  std::cout << "--------------------" << std::endl;
85 
86  //vertausche Reihen 2 und 3
87  gsl_matrix_swap_rows(&m.matrix, 2, 3);
88 
89  //Ausgabe der geänderten Matrix auf dem Bildschirm
90  for (size_t i = 0; i < nrows; i++) {
91  for (size_t j = 0; j < nclmns; j++) {
92  std::cout << std::setw(10) << //
93  gsl_matrix_get(&m.matrix, i, j) << "\t";
94  }
95  std::cout << "\n";
96  }
97 }
98 
102 inline void ex3() {
103  gsl::Matrix m{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
104  gsl::Vector v{ 1, 2, 3 };
105  auto vout = dot(m, v);
106  std::cout << m << std::endl;
107  std::cout << v << std::endl;
108  std::cout << vout << std::endl;
109  auto mout = dot(m, m);
110  std::cout << mout << std::endl;
111 }
112 
118 inline auto get_output_stream(const char *name, Format fmt = Output::csv) {
119  auto ofs = Output::get_stream("x030", fmt, name);
120  fmt.set_defaults(ofs);
121  return ofs;
122 }
123 
127 inline void mat1() {
128  std::ofstream ofs = get_output_stream(__func__);
129  const size_t rows = 2, columns = 2;
130  //erzeuge eine 2x2 Matrix und setze alle Elemente gleich 5
131  gsl::Matrix m(2, 2, 5.0);
132  //schreibe Matrix in Datei
133  for (size_t i = 0; i < rows; i++) {
134  for (size_t j = 0; j < columns; j++) {
135  ofs << m(i, j) << "\t";
136  }
137  ofs << std::endl;
138  }
139 }
140 
144 inline void mat2() {
145  std::ofstream ofs = get_output_stream(__func__);
146  constexpr size_t rows = 3, columns = 3;
147  //die zu kopierende Daten
148  double f[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
149  //Erzeuge eine 3x3 Sicht (Matrix) auf die Daten
150  gsl::Matrix::View m{ 3, 3, f };
151  //schreibe Matrix in Datei
152  for (size_t i = 0; i < rows; i++) {
153  for (size_t j = 0; j < columns; j++) {
154  ofs << m(i, j) << "\t";
155  }
156  ofs << std::endl;
157  }
158 }
159 
164 inline void mat3() {
165  std::ofstream ofs = get_output_stream(__func__);
166  //drei Vektoren ergeben folgende 3x3-Matrix
167  gsl::Matrix m{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
168  ofs << m;
169 }
170 
174 inline void mat4() {
175  std::ofstream ofs = get_output_stream(__func__);
176  //erzeuge eine 3x3 Matrix
177  gsl::Matrix m1{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
178  //schreibe Matrix in Datei
179  ofs << m1;
180  //kopiere m1
181  gsl::Matrix m2{ m1 };
182  ofs << "---------------------" << std::endl << m2;
183 }
184 
188 inline void mat5() {
189  std::ofstream ofs = get_output_stream(__func__);
190  //erzeuge 3x3 Matrix
191  gsl::Matrix m1{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
192  ofs << m1;
193  //übertrage Inhalt der Matrix
194  gsl::Matrix m2 = std::move(m1);
195  ofs << "---------------------" << std::endl;
196  if (m1.empty()) {
197  ofs << "m1 is empty" << std::endl;
198  } else {
199  ofs << "m1 is not empty" << std::endl;
200  }
201  ofs << "---------------------" << std::endl << m2;
202 }
203 
207 inline void mat6() {
208  std::ofstream ofs = get_output_stream(__func__);
209  ofs << std::fixed << std::setprecision(2);
210 
211  //lambda Funktion zum schreiben der Daten in eine Datei
212  auto print = [&ofs](const char *txt, const gsl::Matrix &m) {
213  ofs << "--------" << txt << "----------" << std::endl;
214  ofs << m;
215  };
216 
217  gsl::Matrix m1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
218  print("m1", m1);
219  gsl::Matrix m2{ { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
220  print("m2", m2);
221 
222  //vertausche Matrizen
223  gsl::Matrix tmp = m2;
224  m2 = m1;
225  m1 = tmp;
226 
227  //Ausgabe
228  print("m1", m1);
229  print("m2", m2);
230 }
231 
235 inline void mat7() {
236  using Matrix = gsl::Matrix;
237  std::ofstream ofs = get_output_stream(__func__);
238  ofs << std::fixed << std::setprecision(2);
239 
240  Matrix m1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
241  ofs << "----------m1-----------" << std::endl;
242  m1.save(ofs);
243 
244  Matrix m2{ { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
245  ofs << "----------m2----------" << std::endl;
246  m2.save(ofs);
247 
248  gsl::Matrix tmp = std::move(m1);
249  m1 = std::move(m2);
250  m2 = std::move(tmp);
251 
252  ofs << "----------m1-----------" << std::endl << m1;
253  ofs << "----------m2----------" << std::endl << m2;
254  std::swap(m1, m2);
255  ofs << "----------m1-----------" << std::endl << m1;
256  ofs << "----------m2----------" << std::endl << m2;
257 }
258 
262 inline void mat8() {
263  std::ofstream ofs = get_output_stream(__func__);
264  ofs << std::fixed << std::setprecision(2);
265 
266  gsl::Matrix m{ 3, 3 };
267  gsl_matrix_set_identity(m.gsl());
268 
269  for (size_t i = 0; i < m.gsl()->size1; i++) {
270  for (size_t j = 0; j < m.gsl()->size2; j++) {
271  ofs << gsl_matrix_get(m.gsl(), i, j) //
272  << Output::csv.sep();
273  }
274  ofs << Output::csv.lend();
275  }
276 }
277 
281 inline void mat9() {
282  std::ofstream ofs = get_output_stream(__func__);
283  //erzeuge Einheitsmatrix
285 
286  ofs << "---------------------" << std::endl << m;
287  //verändere Elemente
288  m(2, 2) = 22;
289  m(1, 2) = 12;
290  ofs << "---------------------" << std::endl << m;
291 }
292 
296 inline void mat10() {
297  std::ofstream ofs = get_output_stream(__func__);
298  ofs << std::scientific << std::setprecision(2);
299  //erzeuge Matrix
300  gsl::Matrix m{ { 0, 20, 129, 3, 300 }, //
301  { 311, 0, 40, -1, -600 },
302  { -100, 3, 0, 5, 123 },
303  { -10, -342, -90, 1 } };
304 
305  ofs << m << "---------------\n";
306  //suche Reihe und Spalte des kleinsten Elements
307  auto [imin, jmin] = m.min_index();
308  ofs << "min:" << imin << "," << jmin << "," << m.min() << "\n";
309 
310  //suche Reihe und Spalte des größten Elements
311  auto [imax, jmax] = m.max_index();
312  ofs << "max:" << imax << "," << jmax << "," << m.max() << "\n";
313 }
314 
318 inline void mat11() {
319  std::ofstream ofs = get_output_stream(__func__);
320  ofs << std::fixed << std::setprecision(2);
321 
322  gsl::Matrix m1{ { 1, 2, 3 }, { 5, 6, 7 }, { 8, 9, 10 } };
323  ofs << "---------- m ----------" << std::endl << m1;
324 
325  gsl::Matrix m2 = m1 + m1;
326  ofs << "----------- m1+m1 ---------" << std::endl << m2;
327 
328  gsl::Matrix m3 = m1 - m1;
329  ofs << "----------- m1-m1 --------" << std::endl << m3;
330 }
331 
335 inline void mat12() {
336  std::ofstream ofs = get_output_stream(__func__);
337  ofs << std::fixed << std::setprecision(2);
338 
339  //erzeuge Matrix
340  gsl::Matrix m1{ { 1, 2, 3 }, { 5, 6, 7 }, { 8, 9, 10 } };
341 
342  ofs << "---------------------" << std::endl << m1;
343  //kopiere Matrix
344  gsl::Matrix m2 = m1;
345 
346  //addiere zu allen Elemente eine Zahl
347  m2 += 100.;
348  ofs << "---------------------" << std::endl << m2;
349 }
350 
354 inline void mat13() {
355  using Matrix = gsl::Matrix;
356  std::ofstream ofs = get_output_stream(__func__);
357  //erzeuge Matrizen
358  Matrix m1{ { 4, 3 }, { 1, 1 } }, m2{ { 1, -3 }, { -1, 4 } };
359 
360  //berechne Matrixprodukt
361  Matrix m3 = dot(m1, m2);
362 
363  const char *sep = "---------------------";
364  ofs << m1 << sep << "\n" << m2 << sep << "\n" << m3;
365 }
366 
370 inline void mat14() {
371  std::ofstream ofs = get_output_stream(__func__);
372  //Koeffizientenmatrix
373  gsl::Matrix m{ { 1, 3, -2 }, { 3, 5, 6 }, { 2, 4, 3 } };
374 
375  //Lösung und /rechte Seite des Gleichungssystems
376  gsl::Vector x{ -15.0, 8.0, 2.0 }, b{ 5, 7, 8 };
377 
378  const char *sep = "---------------------";
379  ofs << sep << std::endl << m << sep << std::endl << x;
380  ofs << sep << std::endl << b;
381 
382  //multipliziere Koeffizientenmatrix mit Lösung
383  gsl::Vector vSolution = dot(m, x);
384  ofs << sep << std::endl << vSolution;
385  ofs << std::boolalpha << (vSolution == b) << std::endl;
386 }
387 
391 inline void mat15() {
392  using Matrix = gsl::Matrix;
393  std::ofstream ofs = get_output_stream(__func__);
394  ofs << std::setprecision(8);
395  //Drehung um x-Achse
396  auto Dx = [](double phi) -> Matrix {
397  return { { 1, 0, 0 }, //
398  { 0, cos(phi), -sin(phi) },
399  { 0, sin(phi), cos(phi) } };
400  };
401  //Drehung um y-Achse
402  auto Dy = [](double phi) -> Matrix {
403  return { { cos(phi), 0, -sin(phi) }, //
404  { 0, 1, 0 },
405  { sin(phi), 0, cos(phi) } };
406  };
407  //Drehung um z-Achse
408  auto Dz = [](double phi) -> Matrix {
409  return { { cos(phi), -sin(phi), 0 }, //
410  { sin(phi), cos(phi), 0 },
411  { 0, 0, 1 } };
412  };
413  //Drehmatrix
414  Matrix D = { { 0.3536, -0.6124, -0.7071 },
415  { 0.5732, 0.7392, -0.3536 },
416  { 0.7392, -0.2803, 0.6124 } };
417  //Winkel
418  const auto phix = 30.0_deg;
419  const auto phiy = 45.0_deg;
420  const auto phiz = 60.0_deg;
421  auto DxDyDz = dot(dot(Dx(phix), Dy(phiy)), Dz(phiz));
422 
423  //Prüfe ob Matrizen gleich sind (Vorsicht bei double-Zahlen)
424  if (D == DxDyDz) {
425  ofs << "true" << std::endl;
426  } else {
427  ofs << "false" << std::endl;
428  }
429  ofs << "----------------------------" << std::endl;
430  //speichere Drehmatrix und Produkt der Drehmatrizen in Datei
431  D.save(ofs);
432  ofs << "----------------------------" << std::endl;
433  DxDyDz.save(ofs);
434  //Berechne Differenz
435  Matrix D1 = DxDyDz - D;
436  ofs << "----------------------------" << std::endl;
437  D1.save(ofs);
438 }
439 
443 inline void mat16() {
444  using Matrix = gsl::Matrix;
445  std::ofstream ofs = get_output_stream(__func__);
446  Matrix m{ { 2, -1, 5, 10 }, { 1, 1, -3, -2 }, { 2, 4, 1, 1 } };
447  ofs << m;
448 
449  //multipliziere Reihe 1 mit 2
450  auto mr1 = 2 * m.row(1);
451  //und ersetze Reihe 1 durch das Ergebnis
452  m.set_row(mr1, 1);
453  //Reihe 2 - Reihe 1
454  auto mr2 = m.row(2) - m.row(1);
455  //ersetze Reihe 2 durch das Ergebnis
456  m.set_row(mr2, 2);
457  //Reihe1 - Reihe 0
458  mr1 = m.row(1) - m.row(0);
459  //ersetze Reihe 1 durch das Ergebnis
460  m.set_row(mr1, 1);
461 
462  const char *sep = "----------------------------";
463  ofs << sep << std::endl << m;
464  mr2 = 3 * m.row(2);
465  m.set_row(mr2, 2);
466  ofs << sep << std::endl << m;
467  auto v = m.row(1);
468  mr2 = m.row(2) - 2 * m.row(1);
469  m.set_row(mr2, 2);
470  ofs << sep << std::endl << m;
471  mr2 = m.row(2) / 43;
472  m.set_row(mr2, 2);
473  ofs << sep << std::endl << m;
474 }
475 
479 inline void mat17() {
480  using Matrix = gsl::Matrix;
481  using Vector = gsl::Vector;
482 
483  Matrix m{ { 2, -1, 5, 10 }, //
484  { 1, 1, -3, -2 },
485  { 2, 4, 1, 1 } };
486  std::cout << m;
487 
488  Vector mr1 = 2.0 * m.row_view(1);
489  std::cout << m;
490 }
491 
492 inline void mat18() {
493  using Matrix = gsl::Matrix;
494  using Vector = gsl::Vector;
495 
496  Matrix m{ { 1, 7, 8 }, //
497  { 4, 5, 3 },
498  { 2, 9, 7 } };
499  Vector x{ 1, 2, 3 };
500  Vector y(x.size());
501 
502  dgemv(CblasNoTrans, 3, m, x, 0, y);
503  std::cout << y;
504 }
505 
506 inline void mat19() {
507  using Matrix = gsl::Matrix;
508  using Vector = gsl::Vector;
509 
510  Matrix m{ { 1, 7, 8 }, //
511  { 4, 5, 3 },
512  { 2, 9, 7 } };
513  Vector x{ 1, 2, 3 };
514  Vector y{ 3, 4, 6 };
515  dgemv(CblasNoTrans, 3, m, x, 2, y);
516  std::cout << y;
517 }
518 
519 } // namespace nmx::apps::x030
void mat19()
Definition: x030.h:506
The Matrix class Schnittstelle zur gsl Bibliothek.
Definition: xmatrix.h:55
void mat15()
mat15 Euler&#39;sche Drehmatrizen
Definition: x030.h:391
void ex2()
ex2 Rechnen mit Matrizen (gsl-Routinen und gsl-Sichten)
Definition: x030.h:57
static const Format csv
Definition: xoutput.h:20
void ex1()
ex1 Rechnen mit Matrizen (gsl-Routinen)
Definition: x030.h:14
void mat8()
mat8 direkter Zugriff auf die gsl-Routinen
Definition: x030.h:262
void mat9()
mat9 verändere Elemente mithilfe des Klammeroperators
Definition: x030.h:281
void mat6()
mat6 Einsatz des Zuweisungsoperators
Definition: x030.h:207
void mat17()
mat17
Definition: x030.h:479
auto get_output_stream(const char *name, Format fmt=Output::csv)
get_output_stream
Definition: x030.h:118
void mat11()
mat11 algebraische Operationen mit Matrizen
Definition: x030.h:318
LU::Vector Vector
Definition: x038.h:10
static Matrix identity(size_t N)
identity Erzeugt eine NxN Einheitsmatrix
Definition: xmatrix.h:211
static std::ofstream get_stream(const std::string &fname)
get_output_stream Öffnen einer Ausgabedatei falls die Datei nicht geöffnet werden kann wird ein Fehle...
Definition: xoutput.h:29
auto min_index() const
min_index Position des minimalen Elements
Definition: xmatrix2.h:54
void mat14()
mat14 BLAS Matrix-Vektor-Multiplikation
Definition: x030.h:370
gsl::Vector row(size_t n) const
row Kopie einer Reihe erzeugen
Definition: xmatrix4.h:69
void mat1()
mat1 Initialisierungen und Zuweisungen
Definition: x030.h:127
void mat18()
Definition: x030.h:492
void save(std::ostream &os, Format fmt=Output::array()) const
save Speichert reihenweise eine Matrix
Definition: xmatrix0.h:65
LU::Matrix Matrix
Definition: x038.h:9
const std::string & lend() const
Definition: xfmt.h:140
void mat3()
mat3 Erzeuge eine Matrix aus verschachtelten Listen von Zahlen
Definition: x030.h:164
void mat12()
mat12 algebraische Operationen
Definition: x030.h:335
The Vector class Klasse für gsl-Vektoren zussamengesetzt aus Komponenten.
Definition: xvector.h:76
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
void mat7()
mat7 swap-Funktion
Definition: x030.h:235
void mat13()
mat13 BLAS Matrix-Matrix Multiplikation
Definition: x030.h:354
void mat2()
mat2 Erzeuge View aus C-Feld
Definition: x030.h:144
void mat5()
mat5 Einsatz des move-Konstruktors
Definition: x030.h:188
void mat16()
mat16 Manipulation von Reihen und Spalten einer Matrix
Definition: x030.h:443
The MView class Sicht auf eine gsl-Matrix.
Definition: xmatrix.h:17
double f(double x, void *params)
f
Definition: x035.h:15
void mat10()
mat10 suche Minimum und Maximum
Definition: x030.h:296
void mat4()
mat4 Einsatz des Kopierkonstruktors
Definition: x030.h:174
void ex3()
ex3
Definition: x030.h:102