x038.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xlu.h"
4 #include "xmatrix.h"
5 
6 namespace nmx::apps::x038 {
7 
9 using Matrix = LU::Matrix;
11 
17 inline auto get_output_stream(const char *name, Format fmt = Output::csv) {
18  auto ofs = Output::get_stream("x038", fmt, name);
19  ofs << std::fixed << std::setprecision(2);
20  return ofs;
21 }
22 
26 inline void lu1() {
27  //öffne Ausgabestrom
28  std::ofstream ofs = get_output_stream(__func__);
29 
30  //Koeffizientenmatrix
31  Matrix left{ { { 3, -0.1, -0.2 }, //
32  { 0.1, 7, -0.3 },
33  { 0.3, -0.2, 10 } } };
34 
35  //rechte Seite des Gleichungssystems
36  Vector right{ 7.85, -19.3, 71.4 };
37 
38  //Berechnung der Lösung
39  LU lusolver{ left, right };
40  lusolver.solve();
41  const char *sep = "---------------------\n";
42 
43  auto mupper = lusolver.upper_matrix();
44  auto mlower = lusolver.lower_matrix();
45  auto pmatrix = lusolver.permutation_matrix();
46  auto pdotleft = dot(pmatrix, left);
47  auto ldotu = dot(lusolver.lower_matrix(), lusolver.upper_matrix());
48 
49  //schreibe Ergebnisse in Datei
50  ofs << left << sep << right << sep << lusolver.solution();
51  ofs << sep << lusolver.get_left();
52  ofs << sep << mupper << sep << mlower << sep << pmatrix;
53  ofs << sep << pdotleft << sep << ldotu;
54 }
55 
59 inline void lu2() {
60  //die Cramer'sche Regel für x_i als lambda-Funktion
61  auto cramer_i = [](size_t i, const Matrix &m, const Vector &v) {
62  // Kopiere linke Seite des GL-Systems
63  auto m1 = m;
64  //ersetze i-te Spalte durch rechte Seite
65  m1.set_column(v, i);
66  //Determinante Nenner
67  auto det = LU::det(m);
68  //Determinante Zähler
69  auto det1 = LU::det(m1);
70  // Lösung x_i
71  return det1 / det;
72  };
73 
74  std::ofstream ofs = get_output_stream(__func__);
75  Matrix left{ { { 3, -0.1, -0.2 }, //
76  { 0.1, 7, -0.3 },
77  { 0.3, -0.2, 10 } } };
78  Vector right{ 7.85, -19.3, 71.4 };
79 
80  //der Lösungsvektor
81  const size_t N = right.size();
82  Vector solution(N);
83  for (size_t i = 0; i < N; i++) {
84  solution[i] = cramer_i(i, left, right);
85  }
86  const char *sep = "---------------------\n";
87  //Ausgabe
88  ofs << sep << left << sep << right << sep << solution;
89 }
90 
94 inline void lu3() {
95  //erzeuge Ausgabestrom
96  std::ofstream ofs = get_output_stream(__func__);
97 
98  //Koeffizientenmatrix
99  Matrix left{ { { 3, -0.1, -0.2 }, //
100  { 0.1, 7, -0.3 },
101  { 0.3, -0.2, 10 } } };
102 
103  //die rechte Seite des Gleichungssystems
104  Vector right{ 7.85, -19.3, 71.4 };
105 
106  //Berechnung der inversen Koeffizientenmatrix
107  Matrix leftinverse = LU::inverse(left);
108 
109  //Lösung
110  Vector solution = dot(leftinverse, right);
111 
112  const char *sep = "---------------------\n";
113  //Ausgabe
114  ofs << sep << left << sep << right;
115  ofs << sep << leftinverse << sep << solution;
116 
117  auto m = dot(leftinverse, left);
118  ofs << sep << std::endl << m;
119 }
120 
126 inline void lu0() {
127  // die Koeffizientenmatrix A
128  double a_data[] = { 0.18, 0.60, 0.57, 0.96, //
129  0.41, 0.24, 0.99, 0.58, //
130  0.14, 0.30, 0.97, 0.66, //
131  0.51, 0.13, 0.19, 0.85 };
132 
133  // die rechte Seite des Gleichungssystems b
134  double b_data[] = { 1.0, 2.0, 3.0, 4.0 };
135 
136  // eine view auf das lineare Feld
137  // ...kann wie eine gsl Matrix behandelt werden
138  gsl_matrix_view m = gsl_matrix_view_array(a_data, 4, 4);
139 
140  // eine view auf die rechte Seite
141  // kann wie ein gsl Vektor behandelt werden
142  gsl_vector_view b = gsl_vector_view_array(b_data, 4);
143 
144  // der Lösungsvektor
145  gsl_vector *x = gsl_vector_alloc(4);
146  //speichere Informationen zur Erzeugung der
147  //Permutationsmatrix
148  gsl_permutation *p = gsl_permutation_alloc(4);
149 
150  // s = (-1)^n n die Anzahl der Vertauschungen
151  int s;
152  //LU-Zerlegung der Koeffizientenmatrix. Sie enthält nach der
153  //Zerlegung die Daten der oberen und unteren Dreiecksmatrix
154  gsl_linalg_LU_decomp(&m.matrix, p, &s);
155  //Lösung des Gleichungssystems
156  gsl_linalg_LU_solve(&m.matrix, p, &b.vector, x);
157  //schreibe Lösungsvektor
158  printf("x = \n");
159  gsl_vector_fprintf(stdout, x, "%g");
160  //gebe gsl-Resourcen frei
161  gsl_permutation_free(p);
162  gsl_vector_free(x);
163 }
164 
168 inline void lu11() {
169  //öffne Ausgabestrom
170  std::ofstream ofs = get_output_stream(__func__);
171 
172  //Koeffizientenmatrix
173  Matrix left{ { 0.18, 0.60, 0.57, 0.96 },
174  { 0.41, 0.24, 0.99, 0.58 },
175  { 0.14, 0.30, 0.97, 0.66 },
176  { 0.51, 0.13, 0.19, 0.85 } };
177 
178  //rechte Seite des Gleichungssystems
179  Vector right{ -4.05205, -12.6056, 1.66091, 8.69377 };
180 
181  //Berechnung der Lösung
182  LU lusolver{ left, right };
183  lusolver.solve();
184  const char *sep = "---------------------";
185  ofs << sep << std::endl << left;
186  ofs << sep << std::endl << right;
187  ofs << sep << std::endl << lusolver.solution();
188  ofs << sep << std::endl << lusolver.get_left();
189  auto mupper = lusolver.upper_matrix();
190  ofs << sep << std::endl << mupper;
191  auto mlower = lusolver.lower_matrix();
192  ofs << sep << std::endl << mlower;
193  auto pmatrix = lusolver.permutation_matrix();
194  ofs << sep << std::endl << pmatrix;
195  ofs << sep << std::endl;
196  ofs << lusolver.permutation_vector();
197  ofs << sep << std::endl;
198  ofs << dot(pmatrix, left) << std::endl;
199  ofs << sep << std::endl;
200  ofs << dot(lusolver.lower_matrix(), lusolver.upper_matrix()) << std::endl;
201 }
202 
203 } // namespace nmx::apps::x038
void lu1()
lu1 Lösung eines linearen Gleichungssystems durch LU-Zerlegung
Definition: x038.h:26
void lu3()
lu3 Lösung eines Gleichungssystems mittels der inversen Matrix
Definition: x038.h:94
gsl::Matrix Matrix
Definition: xlu.h:14
void lu0()
lu0 LU-Zerlegung einer Matrix Originalbeispiel mit Kommentaren. Lösung eines linearen Gleichungssyste...
Definition: x038.h:126
The Matrix class Schnittstelle zur gsl Bibliothek.
Definition: xmatrix.h:55
void lu11()
lu11 Lösung eines linearen Gleichungssystems durch LU-Zerlegung
Definition: x038.h:168
static const Format csv
Definition: xoutput.h:20
void solve()
solve Berechnung des Lösungsvektors
Definition: xlu.h:116
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
void lu2()
lu2 Lösung eines Gleichungssystems mit der Cramer&#39;schen Regel
Definition: x038.h:59
The LU_Decomposition class Lösung eines linearen Gleichungssystems mittels der LU-Zerlegung.
Definition: xlu.h:11
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
auto get_output_stream(const char *name, Format fmt=Output::csv)
get_output_stream generiere Ausgabestrom
Definition: x038.h:17
static Matrix inverse(const Matrix &m)
inverse Berechnung der Inversen einer Matrix
Definition: xlu.h:228
gsl::Vector Vector
Definition: xlu.h:15
static double det(const Matrix &m)
det Berechnung der Determinante
Definition: xlu.h:214