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