5 #include <gsl/gsl_matrix.h> 16 const size_t nrows = 4, nclmns = 4;
19 gsl_matrix *m = gsl_matrix_alloc(nrows, nclmns);
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));
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";
38 gsl_matrix_swap_rows(m, 2, 3);
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";
59 const size_t nrows = 4, nclmns = 4;
65 gsl_matrix_view m = gsl_matrix_view_array(data, nrows, nclmns);
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));
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";
84 std::cout <<
"--------------------" << std::endl;
87 gsl_matrix_swap_rows(&m.matrix, 2, 3);
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";
103 gsl::Matrix m{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
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;
120 fmt.set_defaults(ofs);
129 const size_t rows = 2, columns = 2;
133 for (
size_t i = 0; i < rows; i++) {
134 for (
size_t j = 0; j < columns; j++) {
135 ofs << m(i, j) <<
"\t";
146 constexpr
size_t rows = 3, columns = 3;
148 double f[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
152 for (
size_t i = 0; i < rows; i++) {
153 for (
size_t j = 0; j < columns; j++) {
154 ofs << m(i, j) <<
"\t";
167 gsl::Matrix m{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
177 gsl::Matrix m1{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
182 ofs <<
"---------------------" << std::endl << m2;
191 gsl::Matrix m1{ { 1, 2, 3 }, { 10, 20, 30 }, { 100, 200, 300 } };
195 ofs <<
"---------------------" << std::endl;
197 ofs <<
"m1 is empty" << std::endl;
199 ofs <<
"m1 is not empty" << std::endl;
201 ofs <<
"---------------------" << std::endl << m2;
209 ofs << std::fixed << std::setprecision(2);
212 auto print = [&ofs](
const char *txt,
const gsl::Matrix &m) {
213 ofs <<
"--------" << txt <<
"----------" << std::endl;
217 gsl::Matrix m1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
219 gsl::Matrix m2{ { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
238 ofs << std::fixed << std::setprecision(2);
240 Matrix m1{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
241 ofs <<
"----------m1-----------" << std::endl;
244 Matrix m2{ { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } };
245 ofs <<
"----------m2----------" << std::endl;
252 ofs <<
"----------m1-----------" << std::endl << m1;
253 ofs <<
"----------m2----------" << std::endl << m2;
255 ofs <<
"----------m1-----------" << std::endl << m1;
256 ofs <<
"----------m2----------" << std::endl << m2;
264 ofs << std::fixed << std::setprecision(2);
267 gsl_matrix_set_identity(m.gsl());
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)
286 ofs <<
"---------------------" << std::endl << m;
290 ofs <<
"---------------------" << std::endl << m;
298 ofs << std::scientific << std::setprecision(2);
301 { 311, 0, 40, -1, -600 },
302 { -100, 3, 0, 5, 123 },
303 { -10, -342, -90, 1 } };
305 ofs << m <<
"---------------\n";
308 ofs <<
"min:" << imin <<
"," << jmin <<
"," << m.min() <<
"\n";
311 auto [imax, jmax] = m.max_index();
312 ofs <<
"max:" << imax <<
"," << jmax <<
"," << m.max() <<
"\n";
320 ofs << std::fixed << std::setprecision(2);
322 gsl::Matrix m1{ { 1, 2, 3 }, { 5, 6, 7 }, { 8, 9, 10 } };
323 ofs <<
"---------- m ----------" << std::endl << m1;
326 ofs <<
"----------- m1+m1 ---------" << std::endl << m2;
329 ofs <<
"----------- m1-m1 --------" << std::endl << m3;
337 ofs << std::fixed << std::setprecision(2);
340 gsl::Matrix m1{ { 1, 2, 3 }, { 5, 6, 7 }, { 8, 9, 10 } };
342 ofs <<
"---------------------" << std::endl << m1;
348 ofs <<
"---------------------" << std::endl << m2;
358 Matrix m1{ { 4, 3 }, { 1, 1 } }, m2{ { 1, -3 }, { -1, 4 } };
363 const char *sep =
"---------------------";
364 ofs << m1 << sep <<
"\n" << m2 << sep <<
"\n" << m3;
373 gsl::Matrix m{ { 1, 3, -2 }, { 3, 5, 6 }, { 2, 4, 3 } };
378 const char *sep =
"---------------------";
379 ofs << sep << std::endl << m << sep << std::endl << x;
380 ofs << sep << std::endl << b;
384 ofs << sep << std::endl << vSolution;
385 ofs << std::boolalpha << (vSolution == b) << std::endl;
394 ofs << std::setprecision(8);
396 auto Dx = [](
double phi) ->
Matrix {
397 return { { 1, 0, 0 },
398 { 0, cos(phi), -sin(phi) },
399 { 0, sin(phi), cos(phi) } };
402 auto Dy = [](
double phi) ->
Matrix {
403 return { { cos(phi), 0, -sin(phi) },
405 { sin(phi), 0, cos(phi) } };
408 auto Dz = [](
double phi) ->
Matrix {
409 return { { cos(phi), -sin(phi), 0 },
410 { sin(phi), cos(phi), 0 },
414 Matrix D = { { 0.3536, -0.6124, -0.7071 },
415 { 0.5732, 0.7392, -0.3536 },
416 { 0.7392, -0.2803, 0.6124 } };
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));
425 ofs <<
"true" << std::endl;
427 ofs <<
"false" << std::endl;
429 ofs <<
"----------------------------" << std::endl;
432 ofs <<
"----------------------------" << std::endl;
436 ofs <<
"----------------------------" << std::endl;
446 Matrix m{ { 2, -1, 5, 10 }, { 1, 1, -3, -2 }, { 2, 4, 1, 1 } };
450 auto mr1 = 2 * m.
row(1);
454 auto mr2 = m.row(2) - m.row(1);
458 mr1 = m.row(1) - m.row(0);
462 const char *sep =
"----------------------------";
463 ofs << sep << std::endl << m;
466 ofs << sep << std::endl << m;
468 mr2 = m.row(2) - 2 * m.row(1);
470 ofs << sep << std::endl << m;
473 ofs << sep << std::endl << m;
483 Matrix m{ { 2, -1, 5, 10 },
488 Vector mr1 = 2.0 * m.row_view(1);
502 dgemv(CblasNoTrans, 3, m, x, 0, y);
515 dgemv(CblasNoTrans, 3, m, x, 2, y);
The Matrix class Schnittstelle zur gsl Bibliothek.
void mat15()
mat15 Euler'sche Drehmatrizen
void ex2()
ex2 Rechnen mit Matrizen (gsl-Routinen und gsl-Sichten)
void ex1()
ex1 Rechnen mit Matrizen (gsl-Routinen)
void mat8()
mat8 direkter Zugriff auf die gsl-Routinen
void mat9()
mat9 verändere Elemente mithilfe des Klammeroperators
void mat6()
mat6 Einsatz des Zuweisungsoperators
auto get_output_stream(const char *name, Format fmt=Output::csv)
get_output_stream
void mat11()
mat11 algebraische Operationen mit Matrizen
static Matrix identity(size_t N)
identity Erzeugt eine NxN Einheitsmatrix
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...
auto min_index() const
min_index Position des minimalen Elements
void mat14()
mat14 BLAS Matrix-Vektor-Multiplikation
gsl::Vector row(size_t n) const
row Kopie einer Reihe erzeugen
void mat1()
mat1 Initialisierungen und Zuweisungen
void save(std::ostream &os, Format fmt=Output::array()) const
save Speichert reihenweise eine Matrix
void mat3()
mat3 Erzeuge eine Matrix aus verschachtelten Listen von Zahlen
void mat12()
mat12 algebraische Operationen
The Vector class Klasse für gsl-Vektoren zussamengesetzt aus Komponenten.
void mat7()
mat7 swap-Funktion
void mat13()
mat13 BLAS Matrix-Matrix Multiplikation
void mat2()
mat2 Erzeuge View aus C-Feld
void mat5()
mat5 Einsatz des move-Konstruktors
void mat16()
mat16 Manipulation von Reihen und Spalten einer Matrix
The MView class Sicht auf eine gsl-Matrix.
double f(double x, void *params)
f
void mat10()
mat10 suche Minimum und Maximum
void mat4()
mat4 Einsatz des Kopierkonstruktors