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