lilypond-0.0.35
[lilypond.git] / flower / vector.hh
blob4d91397cc6c710d0a34118710a86c4d514aba725
1 #ifndef VECTOR_HH
2 #define VECTOR_HH
4 #include <math.h>
5 #include "real.hh"
6 #include "varray.hh"
8 class Dstream;
9 class String;
10 void set_matrix_debug(Dstream&ds);
12 /** a row of numbers.
13 a vector. Storage is handled in Array, Vector only does the mathematics.
15 class Vector {
16 Array<Real> dat;
17 public:
18 void OK() const { dat.OK();}
19 int dim() const { return dat.size(); }
20 Vector() { }
21 Vector(Array<Real> d );
22 Vector(const Vector&n);
23 Vector(int n) {
24 dat.set_size(n);
25 fill(0);
27 void insert(Real v, int i) {
28 dat.insert(v,i);
30 void del(int i) { dat.del(i); }
31 operator String() const;
32 void fill(Real r) {
33 for (int i=0; i < dim(); i++)
34 dat[i] =r;
37 void operator +=(Vector v) {
38 assert(v.dim() == dim());
39 for (int i=0; i < dim(); i++)
40 dat[i] += v.dat[i];
43 void operator /=(Real a) {
44 (*this) *= 1/a;
47 void operator *=(Real a) {
48 for (int i=0; i < dim(); i++)
49 dat[i] *= a;
52 void operator -=(Vector v) {
53 assert(v.dim() == dim());
54 for (int i=0; i < dim(); i++)
55 dat[i] -= v(i);
58 Real &operator()(int i) { return dat[i]; }
59 Real operator()(int i) const { return dat[i]; }
60 Real elem(int i) { return dat[i]; }
61 Real operator *(Vector v) const {
62 Real ip=0;
63 assert(v.dim() == dim());
64 for (int i=0; i < dim(); i++)
65 ip += dat[i] *v(i);
66 return ip;
68 Vector operator-() const;
69 Real norm() {
70 return sqrt(norm_sq() );
72 Real norm_sq() {
73 return ((*this) * (*this));
75 operator Array<Real> () { return dat; }
76 void print() const;
77 /// set to j-th element of unit-base
78 void set_unit(int j) ;
81 inline Vector
82 operator+(Vector a, Vector const &b) {
83 a += b;
84 return a;
87 inline Vector
88 operator-(Vector a, Vector const &b) {
89 a -= b;
90 return a;
93 inline Vector
94 operator*(Vector v, Real a) {
95 v *= a;
96 return v;
99 inline Vector
100 operator*( Real a,Vector v) {
101 v *= a;
102 return v;
105 inline Vector
106 operator/(Vector v,Real a) {
107 v *= 1/a;
108 return v;
111 #endif