x020.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xdata.h"
4 #include "xdefs.h"
5 #include "xmath.h"
6 #include <algorithm>
7 #include <numeric>
8 
9 namespace nmx::apps::x020 {
10 
14 inline void ex16() {
15  Data<3> data; //Tabelle mit drei Spalten
16  //fülle Tabelle mit Daten
17  data += { 1, 2, 3 }; // Reihe 0
18  data += { 10, 20, 30 }; // Reihe 1
19  data += { 100, 200, 300 }; // Reihe 2
20  data += { 1000, 2000, 3000 }; // Reihe 3
21  //Formatierung
22  Format fmt{ __func__, ",", "\n", "csv" };
23  //Ausgabe
24  auto csvstream = Output::get_stream("x020", fmt);
25  data.save(csvstream, fmt);
26 }
27 
32 inline void ex17() {
33  using Data = Data<3>;
34  //Tabelle mit drei Spalten
35  Data data;
36 
37  //fülle Tabellen mit Daten
38  for (const auto x : { 6, 3, 2 }) {
39  const double phi = Math::PI / x;
40  data += { phi, sin(phi), cos(phi) };
41  }
42 
43  //Formatierung
44  Format fmt{ __func__, ",", "\n", "csv" };
45 
46  //Ausgabe
47  auto csvstream = Output::get_stream("x020", fmt);
48  data.save(csvstream, fmt, [](const auto &crow) {
49  return Data::Row{ Math::to_degrees(crow[0]), crow[1], crow[2] };
50  });
51 }
52 
57 inline void ex18() {
58  //Tabelle mit vier Spalten
59  using Data = Data<4>;
60  Data table;
61  table += { -4, 3, 7, 1 };
62  table += { -2, 8, 2, 0 };
63  table += { 9, 1, 1, 2 };
64 
65  //speichere Normen der Vektoren
66  Data::Row results;
67  //addieren Quadrate der Elemente
68  table.apply([&results](const Data::Row &crow) {
69  for (size_t idx = 0; idx < crow.size(); idx++) {
70  results[idx] += std::pow(crow[idx], 2);
71  }
72  });
73 
74  for (auto &res : results) {
75  res = std::sqrt(res);
76  }
77 
78  //Kopiere Tabelle
79  Data normtable = table;
80  //normiere Vektoren
81  for (auto &crow : normtable.data()) {
82  for (size_t idx = 0; idx < crow.size(); idx++) {
83  crow[idx] /= results[idx];
84  }
85  }
86  // füge als letzte Reihe die Norm hinzu
87  normtable += results;
88 
89  //formatierte Ausgabe
90  Format fmt{ __func__, ",\t", "\n", "csv" };
91  fmt.precision = 3;
92  auto csvstream = Output::get_stream("x020", fmt);
93  //ursprüngliche Tabelle
94  table.save(csvstream, fmt);
95  csvstream << "-------------------------------" << std::endl;
96  //Tabelle mit normierten Vektoren und Normen
97  normtable.save(csvstream, fmt);
98 }
99 
103 inline void ex19() {
104  //Tabelle mit vier Spalten
105  using Data = Data<4>;
106  Data table;
107 
108  table += { 1, 2, 3, 4 };
109  table += { 5, 6, 7, 8 };
110  table += { 9, 10, 11, 12 };
111 
112  //ändere alle Zahlen, die größer als 6 sind
113  table.apply([](Data::Row &crow) { //
114  std::replace_if(ALL(crow), [](double x) { return x > 6; }, -1);
115  });
116 
117  //Formatierung
118  Format fmt{ __func__, ",", "\n", "csv" };
119 
120  //Ausgabe
121  auto csvstream = Output::get_stream("x020", fmt);
122  table.save(csvstream, fmt);
123 }
124 
129 inline void ex20() {
130  using Data = Data<4>;
131  Data table;
132 
133  //generiere Namen
134  std::string filename = settings::Config::data_directory + //
135  "/x020/ex19.csv";
136 
137  //lade Tabelle
138  std::ifstream ifs(filename);
139  table.read_csv(ifs);
140 
141  //ändere alle Zahlen, die kleiner als 6 sind
142  table.apply([](Data::Row &crow) { //
143  std::replace_if(ALL(crow), [](double x) { return x < 6; }, 0);
144  });
145 
146  //Ausgabe
147  Format fmt{ __func__, ",\t", "\n", "csv" }; //Formatierung
148  auto csvstream = Output::get_stream("x020", fmt);
149  table.save(csvstream, fmt);
150 }
151 
155 inline void ex21() {
156  using Data = Data<3>;
157  Data table;
158 
159  //Erzeuge Daten
160  for (double x = -5; x < 5; x += 1) {
161  table += { x, pow(x, 3), pow(x, 5) };
162  }
163 
164  //Filter: alle positiven Funktionswerte
165  const auto view = table.view(
166  [](const Data::Row &crow) {
167  return crow[1] > 0 && crow[2] > 0; //
168  },
169  false,
170  false);
171 
172  //Ausgabe
173  Format fmt{ __func__, ",\t", "\n", "csv" }; //Formatierung
174  auto csvstream = Output::get_stream("x020", fmt);
175  view.save(csvstream, fmt);
176 }
177 
182 inline void ex22() {
183  using Data = Data<3>;
184  Data table;
185 
186  //Erzeuge Daten
187  for (double x = -4; x < 4; x += 1) {
188  table += { x, pow(x, 3), sin(x) };
189  }
190 
191  //Filter: nehme jede zweite Reihe beginne ab Reihe 1
192  //ignoriere letzten Eintrag
193  const auto view = table.select_rows(2, 1, false);
194 
195  //formatierte Ausgabe
196  Format fmt{ __func__, ",\t", "\n", "csv" };
197  auto csvstream = Output::get_stream("x020", fmt);
198  table.save(csvstream, fmt);
199  csvstream << "--------------" << std::endl;
200  view.save(csvstream, fmt);
201 }
202 
206 inline void ex23() {
207  using Data = Data<3>;
208  Data table;
209 
210  //erzeuge Daten
211  for (double x = -Math::PI; x < Math::PI; x += 0.25 * Math::PI) {
212  table += { x, std::cos(x), std::sin(x) };
213  }
214 
215  //View zeigt auf die Reihen mit cos x < 0
216  const auto view = table.view(
217  [](const auto &r) { //
218  return r[1] < 0;
219  },
220  false,
221  false);
222 
223  //Ausgabe
224  Format fmt{ __func__, ",\t", "\n", "csv" }; //Formatierung
225  auto csvstream = Output::get_stream("x020", fmt);
226  table.save(csvstream, fmt);
227  csvstream << "--------------" << std::endl;
228  view.save(csvstream, fmt);
229  csvstream << "--------------" << std::endl;
230  Data::Column clmn = view.column(1);
231  fmt.write_line(csvstream, clmn);
232 }
233 
234 } // namespace nmx::apps::x020
void apply(FN fn)
apply ändere alle oder bestimmte Elemente
Definition: xdata.h:226
static double to_degrees(double x)
Definition: xmath.h:17
void ex18()
ex18 Bearbeitung einer Tabelle. Berechnung der Normen von Vektoren
Definition: x020.h:57
auto select_rows(size_t step, size_t startidx=0, bool last=false) const
view Teile der Tabelle als View
Definition: xdata.h:319
void ex19()
ex19 Suchen und Bearbeiten von Elementen einer Tabelle
Definition: x020.h:103
void ex23()
ex23 Kopie einer Spalte
Definition: x020.h:206
std::vector< double > Column
Definition: xdata.h:27
auto view() const
view die ganze Tabelle als view
Definition: xdata.h:258
void save(std::ostream &ofs, Format fmt) const
save Schreibe Tabelle in Datei
Definition: xdata.h:160
void ex20()
ex20 lade Tabelle aus csv-Datei ändere Werte und speichere Tabelle wieder ab
Definition: x020.h:129
The Data class Eine Klasse für Zahlentabellen mit fester Anzahl von Spalten. Die Anzahl der Reihen wä...
Definition: xdata.h:22
const auto & data() const
data lesender Zugriff auf die interne Struktur
Definition: xdata.h:43
std::streamsize precision
Definition: xfmt.h:17
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 ex21()
ex21 Filter für Funktionswerte (Sicht auf Tabelle)
Definition: x020.h:155
void ex17()
ex17 Ausgabe einer Tabelle in Datei. Jede Zeile wird vor dem Speichern bearbeitet ...
Definition: x020.h:32
void read_csv(std::ifstream &ifs)
read_csv lese Daten aus csv-Datei
Definition: xdata.h:236
void ex16()
ex16 Aufbau einer Tabelle und Ausgabe in Datei
Definition: x020.h:14
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
static const std::string data_directory
Definition: xconfig.h:23
void ex22()
ex22 Filter für Funktionswerte: Filter für jede zweite Reihe der Tabelle
Definition: x020.h:182
static constexpr double PI
Definition: xmath.h:11
#define ALL(x)
std::array< double, N > Row
Definition: xdata.h:25