9 #include <forward_list> 11 #include <gsl/gsl_sf_trig.h> 22 gsl_vector *v = gsl_vector_alloc(n);
24 for (
size_t idx = 0; idx < n; idx++) {
25 gsl_vector_set(v, idx, idx);
28 for (
size_t idx = 0; idx < n; idx++) {
29 std::cout << gsl_vector_get(v, idx) <<
" ";
33 gsl_vector_swap_elements(v, 2, 3);
35 for (
size_t idx = 0; idx < n; idx++) {
36 std::cout << gsl_vector_get(v, idx) <<
" ";
48 double data[] = { 0, 1, 2, 3, 4 };
51 gsl_vector_view v = gsl_vector_view_array(data, n);
54 for (
size_t idx = 0; idx < n; idx++) {
55 std::cout << gsl_vector_get(&v.vector, idx) <<
"\t";
57 std::cout << std::endl;
59 gsl_vector_swap_elements(&v.vector, 2, 3);
62 for (
size_t idx = 0; idx < n; idx++) {
63 std::cout << gsl_vector_get(&v.vector, idx) <<
"\t";
65 std::cout << std::endl;
74 gsl_vector *v = gsl_vector_alloc(n);
77 for (
size_t idx = 0; idx < n; idx++) {
78 gsl_vector_set(v, idx, idx);
82 for (
size_t idx = 0; idx < n; idx++) {
83 std::cout << gsl_vector_get(v, idx) <<
"\t";
85 std::cout << std::endl;
88 gsl_vector_add_constant(v, 10);
90 gsl_vector_scale(v, 10);
93 for (
size_t idx = 0; idx < n; idx++) {
94 std::cout << gsl_vector_get(v, idx) <<
"\t";
96 std::cout << std::endl;
99 double f[] = { -10, -20, -30 };
101 gsl_vector_view view = gsl_vector_view_array(f, n);
105 gsl_vector_add(v, &view.vector);
108 for (
size_t idx = 0; idx < n; idx++) {
109 std::cout << gsl_vector_get(v, idx) <<
"\t";
112 std::cout << std::endl;
123 static inline std::ofstream get_output_stream(
const std::string &name,
133 std::ofstream ofs = get_output_stream(__func__);
136 Vector v1{ 1.0, 2.0, 3.0 };
147 double f[] = { 10, 20, 30 };
148 Vector::View v4(3, f);
159 std::ofstream ofs = get_output_stream(__func__);
165 Vector v2 = std::move(v1);
168 ofs <<
"v1 is empty" << std::endl;
180 std::ofstream ofs = get_output_stream(__func__);
185 for (
size_t idx = 0; idx < v1.size(); idx++) {
191 auto view = v1.view(3, 4);
195 auto view1 = v1.view(3, 2, 4);
204 std::ofstream ofs = get_output_stream(__func__);
205 ofs << std::fixed << std::setprecision(2);
211 v1.transform([](
double x) {
213 double static val = 0;
219 Vector v2 = apply([](
double x) {
return sin(x); }, v1);
221 Vector v3 = apply([](
double x) {
return cos(x); }, v1);
224 auto fn = [](
double x,
double y) {
225 return std::pow(x, 2) + std::pow(y, 2);
227 Vector v4 = apply(fn, v2, v3);
230 auto fn1 = [](
double x,
double y) {
231 return std::pow(cos(x), 2) + std::pow(sin(y), 2);
233 Vector v5 = apply(fn1, v1, v1);
242 std::ofstream ofs = get_output_stream(__func__);
243 constexpr
size_t dim = 6;
246 for (
size_t idx = 0; idx < dim; idx++) {
247 v[idx] = sin(idx + 1) / (idx + 1);
250 double maxelement = v[0], minelement = v[0];
251 for (
size_t jdx = 0; jdx < dim; jdx++) {
252 const double jelement = v[jdx];
253 maxelement = std::max(maxelement, jelement);
254 minelement = std::min(minelement, jelement);
257 auto imaxlement = std::max_element(std::begin(v),
259 auto iminlement = std::min_element(std::begin(v),
263 ofs <<
"max loop:" << maxelement << std::endl;
264 ofs <<
"min loop:" << minelement << std::endl;
265 ofs <<
"max method:" << v.
max() << std::endl;
266 ofs <<
"min method:" << v.min() << std::endl;
267 ofs <<
"max stl:" << *imaxlement << std::endl;
268 ofs <<
"min stl:" << *iminlement << std::endl;
276 std::ofstream ofs = get_output_stream(__func__);
281 auto paramForm = [r0, r1](
double l) {
return r0 + l * (r1 - r0); };
283 const double l1 = 1, l2 = 2;
284 const auto v1 = paramForm(l1);
285 const auto v2 = paramForm(l2);
289 ofs <<
"lambda 1=" << l1 << std::endl;
291 ofs <<
"lambda 2=" << l2 << std::endl;
300 std::ofstream ofs = get_output_stream(__func__);
302 ofs << fmt << std::fixed;
306 auto v1 = a + b, v2 = a - b, v3 = 2 * a, v4 = -a;
307 ofs << a << b << v1 << std::endl << v2 << v3 << v4 << std::endl;
310 if (nrm2(v1) <= abs(nrm2(v1)) + abs(nrm2(v2))) {
315 if (nrm2(v2) >= abs(abs(nrm2(v1)) - abs(nrm2(v2)))) {
316 ofs <<
"true" << std::endl;
318 ofs <<
"false" << std::endl;
328 std::ofstream ofs = get_output_stream(__func__);
332 const double n1 = pow(nrm2(c), 2);
333 const double n2 = pow(nrm2(d), 2);
334 const double cdotd = dot(c, d);
335 const double cosphi = cdotd / (nrm2(c) * nrm2(d));
338 ofs <<
"n1=" << n1 <<
"," 339 <<
"n2=" << n2 <<
"," 340 <<
"cdotd=" << cdotd <<
"," 341 <<
"cos(phi)=" << cosphi << std::endl;
352 vout[0] = v1[1] * v2[2] - v1[2] * v2[1];
353 vout[1] = v1[0] * v2[2] - v1[2] * v2[0];
354 vout[2] = v1[0] * v2[1] - v1[1] * v2[0];
363 std::ofstream ofs = get_output_stream(__func__);
372 ofs <<
"c = a x b=" << c;
381 std::ofstream ofs = get_output_stream(__func__);
382 const Vector r{ 5, -5, 0 },
f{ 2, 3, 0 };
385 ofs <<
"tau = r x f=" << tau;
393 std::ofstream ofs = get_output_stream(__func__);
394 const double mass = 100.0_g;
395 const double t = 1.0;
399 const gsl::Vector r{ 0, 10.0, 30.0 - 4.905 * pow(t, 2) };
401 const auto p = mass * v;
411 std::ofstream ofs = get_output_stream(__func__);
416 Vector v2 = apply([](
double x) {
return sin(x); }, v1);
418 Vector v3 = apply([](
double x) {
return cos(x); }, v1);
420 Vector v4 = apply([](
double x,
double y) {
return pow(x, 2) + pow(y, 2); }, v2, v3);
424 for (
const auto &x : v4) {
430 auto sum = std::accumulate(std::begin(v4), std::end(v4), 0);
431 ofs <<
"sum =" << sum << std::endl;
440 std::array<gsl::Vector, 7> forces;
441 forces[0] = { 0, -3 };
442 forces[1] = { 20, -10 };
443 forces[2] = { 1, 20 };
444 forces[3] = { 10, 0 };
445 forces[4] = { 5, 5 };
446 forces[5] = { -10, -10 };
447 forces[6] = { 0, 0 };
450 for (
size_t idx = 0; idx < 6; idx++) {
451 forces[6] += forces[idx];
456 std::ofstream ofs = get_output_stream(__func__,
Output::latex);
457 ofs << std::fixed << std::setprecision(2);
458 for (
size_t idx = 0; idx < 2; idx++) {
459 idx == 0 ? ofs <<
"$x$" : ofs <<
"$y$";
460 for (
size_t jdx = 0; jdx < 7; jdx++) {
461 ofs <<
" " << sep << forces[jdx][idx];
472 auto polar_to_rect = [](
double r,
double phi) {
474 gsl_sf_polar_to_rect(r, phi, &x, &y);
479 auto rect_to_polar = [](
double x,
double y) {
480 gsl_sf_result r, phi;
481 gsl_sf_rect_to_polar(x, y, &r, &phi);
486 std::array<gsl::Vector, 4> forces;
487 forces[0] = polar_to_rect(80.0, 40.0_deg);
488 forces[1] = polar_to_rect(120.0, 70.0_deg);
489 forces[2] = polar_to_rect(150.0, 145.0_deg);
490 forces[3] = forces[0] + forces[1] + forces[2];
493 std::ofstream ofs = get_output_stream(__func__,
Output::latex);
494 ofs << std::setprecision(2) << std::fixed;
496 for (
size_t idx = 0; idx < 4; idx++) {
497 const auto x = forces[idx][0], y = forces[idx][1];
498 const auto polar = rect_to_polar(x, y);
499 ofs << idx << sep << x << sep << y << sep
static double to_degrees(double x)
void vec11()
vec11 Daten werden von einem Vektor auf einen anderen übertragen
void ex00()
ex00 Beispiel (gsl-Programm)
void ex0()
ex0 Beispiel mit Vektoren (gsl-Programm)
void vec4()
vec4 Skalarprodukt cos
void vec9()
vec9 Drehimpuls eines Teilchens bezüglich des Ursprungs
void vec8()
vec8 Drehmoment durch Kraft auf Teilchen bezüglich des Ursprungs
void vec5()
vec5 minimales maximales Element
void ex1()
ex1 Rechnen mit gsl-Vektoren
void vec3()
vec3 algebraische Operationen
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...
void vec2()
vec2 mathematische Operationen mit Vektoren
void vec1()
vec1 Konstruktoren Vektor-Klasse und Views
gsl::Vector cross_product(const gsl::Vector &v1, const gsl::Vector &v2)
cross_product Kreuzprodukt von dreidimensionalen Vektoren
void vec0()
vec1 Konstruktoren Vektor-Klasse
The Vector class Klasse für gsl-Vektoren zussamengesetzt aus Komponenten.
void vec7()
vec7 Kreuzprodukt zweier Vektoren
void vec13()
vec13 Kräfte wirken auf Massenpunkt (in Polarkoordinaten)
static const Format latex
double f(double x, void *params)
f
void vec12()
vec12 Kräfte wirken auf Massenpunkt (kartesischen Koordinaten)
void vec6()
vec6 Parameterdarstellung einer Gerade