]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/vector.hh
release: 1.1.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   Array<Real> const &to_array()const { return dat; }  
82     void print() const;
83     /// set to j-th element of unit-base
84     void set_unit (int j) ;
85 };
86
87 inline Vector
88 operator+(Vector a, Vector const &b) {
89     a += b;
90     return a;
91 }
92
93 inline Vector
94 operator-(Vector a, Vector const &b) {
95     a -= b;
96     return a;
97 }
98
99 inline Vector
100 operator*(Vector v, Real a) {
101     v *= a;
102     return v;
103 }
104
105 inline Vector
106 operator*(Real a,Vector v) {
107     v *= a;
108     return v;
109 }
110
111 inline Vector
112 operator/(Vector v,Real a) {
113     v *= 1/a;
114     return v;
115 }
116
117 #endif