]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/vector.hh
release: 0.0.42.pre3
[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 void set_matrix_debug(Dstream&ds);
11
12 /**  a row of numbers. 
13     a vector. Storage is handled in Array, Vector only does the mathematics.
14  */
15 class Vector  {
16     Array<Real> dat;
17 public:
18     void OK() const { dat.OK();}
19     int dim() const { return dat.size(); }
20     Vector() { }
21     Vector(Array<Real> d );
22     Vector(Vector const &n);
23     Vector(int n) {
24         dat.set_size(n);
25         fill(0);
26     }
27     void insert(Real v, int i) {
28         dat.insert(v,i);
29     }
30     void del(int i) { dat.del(i); }
31     operator String() const;
32     void fill(Real r) {
33         for (int i=0; i < dim(); i++)
34             dat[i] =r;
35     }
36
37     void operator +=(Vector v) {
38         assert(v.dim() == dim());
39         for (int i=0; i < dim(); i++)
40             dat[i] += v.dat[i];
41     }
42     
43     void operator /=(Real a) {
44         (*this) *= 1/a;
45     }
46
47     void operator *=(Real a) {
48         for (int i=0; i < dim(); i++)
49             dat[i] *= a;
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()(int i) { return dat[i]; }
59     Real operator()(int i) const { return dat[i]; }
60     Real elem(int i) { return dat[i]; }
61     Real operator *(Vector v) const {
62         Real ip=0;
63         assert(v.dim() == dim());
64         for (int i=0; i < dim(); i++)
65             ip += dat[i] *v(i);
66         return ip;
67     }
68     Vector operator-() const;
69     Real norm() {
70         return sqrt(norm_sq() );
71     }
72     Real norm_sq() {
73         return ((*this) * (*this));
74     }
75     operator Array<Real> () { return dat; }
76     void print() const;
77     /// set to j-th element of unit-base
78     void set_unit(int j) ;
79 };
80
81 inline Vector
82 operator+(Vector a, Vector const &b) {
83     a += b;
84     return a;
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 v, Real a) {
95     v *= a;
96     return v;
97 }
98
99 inline Vector
100 operator*( Real a,Vector v) {
101     v *= a;
102     return v;
103 }
104
105 inline Vector
106 operator/(Vector v,Real a) {
107     v *= 1/a;
108     return v;
109 }
110
111 #endif