]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/vector.hh
release: 0.1.59
[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     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     operator String() const;
45
46     void operator +=(Vector v) {
47         assert (v.dim() == dim ());
48         for (int i=0; i < dim(); i++)
49             dat[i] += v.dat[i];
50     }
51     
52     void operator -=(Vector v) {
53         assert (v.dim() == dim ());
54         for (int i=0; i < dim(); i++)
55             dat[i] -= v (i);    
56     }
57
58     Real operator *(Vector v) const {
59         Real ip=0;
60         assert (v.dim() == dim ());
61         for (int i=0; i < dim(); i++)
62             ip += dat[i] *v (i);
63         return ip;
64     }
65     void operator *=(Real a) {
66         for (int i=0; i < dim(); i++)
67             dat[i] *= a;
68     }
69     void operator /=(Real a) {
70         (*this) *= 1/a;
71     }
72     Vector operator-() const;
73     Real norm_sq() {
74         return ((*this) * (*this));
75     }
76     Real norm() {
77         return sqrt (norm_sq());
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