xvector1.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xfmt.h"
4 #include "xoutput.h"
5 #include "xvector0.h"
6 #include <gsl/gsl_vector.h>
7 #include <ostream>
8 
9 namespace nmx::gsl::vec {
10 
14 template<class T>
15 class IVBase : public IGslContainer<T, gsl_vector, 1>
16 {
17  // benutze Elementfunktionen der Basisklasse
19 
20 public:
25  inline size_t size() const { //
26  try {
27  return gsl()->size;
28  } catch (...) {
29  // wenn kein Speicher angefordert wurde
30  return 0;
31  }
32  }
33 
39  double operator[](size_t idx) const { //
40  return *gsl_vector_const_ptr(gsl(), idx);
41  }
42 
48  double &operator[](size_t idx) { //
49  return *gsl_vector_ptr(gsl(), idx);
50  }
51 
57  inline void save(std::ostream &os, Format fmt = Output::array()) const {
58  //schreibe erstes Element z.B. x0...
59  os << gsl_vector_get(gsl(), 0);
60  for (size_t idx = 1; idx < size(); idx++) {
61  //... ,x1,x2,...
62  os << fmt.sep() << gsl_vector_get(gsl(), idx);
63  }
64  os << fmt.lend();
65  }
66 
73  inline friend std::ostream &operator<<(std::ostream &os, const T &p) {
74  p.save(os);
75  return os;
76  }
77 
83  template<class FN>
84  inline T &transform(FN fn) {
85  for (size_t idx = 0; idx < size(); idx++) {
86  double val = gsl_vector_get(gsl(), idx);
87  gsl_vector_set(gsl(), fn(val), idx);
88  }
89  return static_cast<T &>(*this);
90  }
91 }; //IVBase
92 
93 } // namespace nmx::gsl::vec
T & transform(FN fn)
transform Transformation der Elemente eines Vektors
Definition: xvector1.h:84
size_t size() const
size
Definition: xvector1.h:25
static const Format & array()
array lese internes Formatierungsobjekt
Definition: xoutput.h:118
double operator[](size_t idx) const
operator [] Zugriff auf die Elemente des Vektors
Definition: xvector1.h:39
The IVBase struct Basisfunktionalität für eine Vektor-Klasse.
Definition: xvector1.h:15
friend std::ostream & operator<<(std::ostream &os, const T &p)
operator <<
Definition: xvector1.h:73
double & operator[](size_t idx)
operator [] Zugriff auf die Elemente des Vektors
Definition: xvector1.h:48
const gsl_vector * gsl() const
ermöglicht direkten Zugriff auf gsl-Funktionen
Definition: xvector0.h:18
void save(std::ostream &os, Format fmt=Output::array()) const
save Ausgabe in Datei oder auf dem Bildschirm
Definition: xvector1.h:57
The Format class Formatierte Ausgabe von Arrays.
Definition: xfmt.h:10
The IGslContainer struct Basisklasse für gsl-Vektoren und Matrizen.
Definition: xvector0.h:13