]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/vector.hh
7f7573534498fdd125edccaf5a2dcaa51ece4187
[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 set_dim(int i)
27     {
28         dat.set_size(i);
29     }
30         
31     void insert(Real v, int i) {
32         dat.insert(v,i);
33     }
34     void del(int i) { dat.del(i); }
35     operator String() const;
36     void fill(Real r) {
37         for (int i=0; i < dim(); i++)
38             dat[i] =r;
39     }
40
41     void operator +=(Vector v) {
42         assert(v.dim() == dim());
43         for (int i=0; i < dim(); i++)
44             dat[i] += v.dat[i];
45     }
46     
47     void operator /=(Real a) {
48         (*this) *= 1/a;
49     }
50
51     void operator *=(Real a) {
52         for (int i=0; i < dim(); i++)
53             dat[i] *= a;
54     }
55
56     void operator -=(Vector v) {
57         assert(v.dim() == dim());
58         for (int i=0; i < dim(); i++)
59             dat[i] -= v(i);     
60     }
61
62     Real &operator()(int i) { return dat[i]; }
63     Real operator()(int i) const { return dat[i]; }
64     Real elem(int i) { return dat[i]; }
65     Real operator *(Vector v) const {
66         Real ip=0;
67         assert(v.dim() == dim());
68         for (int i=0; i < dim(); i++)
69             ip += dat[i] *v(i);
70         return ip;
71     }
72     Vector operator-() const;
73     Real norm() {
74         return sqrt(norm_sq() );
75     }
76     Real norm_sq() {
77         return ((*this) * (*this));
78     }
79     operator Array<Real> () { return dat; }
80     void print() const;
81     /// set to j-th element of unit-base
82     void set_unit(int j) ;
83 };
84
85 inline Vector
86 operator+(Vector a, Vector const &b) {
87     a += b;
88     return a;
89 }
90
91 inline Vector
92 operator-(Vector a, Vector const &b) {
93     a -= b;
94     return a;
95 }
96
97 inline Vector
98 operator*(Vector v, Real a) {
99     v *= a;
100     return v;
101 }
102
103 inline Vector
104 operator*( Real a,Vector v) {
105     v *= a;
106     return v;
107 }
108
109 inline Vector
110 operator/(Vector v,Real a) {
111     v *= 1/a;
112     return v;
113 }
114
115 #endif