]> git.donarmstrong.com Git - lilypond.git/blob - flower/vector.hh
release: 0.0.2
[lilypond.git] / flower / vector.hh
1 #ifndef VECTOR_HH
2 #define VECTOR_HH
3
4 #include <math.h>
5 #include "real.hh"
6 #include "vray.hh"
7
8 class Dstream;
9 class String;
10 void set_matrix_debug(Dstream&ds);
11
12 /// a row of numbers
13 class Vector  {
14     svec<Real> dat;
15 public:
16     void OK() const { dat.OK();}
17     int dim() const { return dat.sz(); }
18     Vector() { }
19     Vector(const Vector&n);
20     Vector(int n) {
21         dat.set_size(n);
22         fill(0);
23     }
24     void insert(Real v, int i) {
25         dat.insert(v,i);
26     }
27     void del(int i) { dat.del(i); }
28     operator String() const;
29     void fill(Real r) {
30         for (int i=0; i < dim(); i++)
31             dat[i] =r;
32     }
33
34     void operator +=(Vector v) {
35         assert(v.dim() == dim());
36         for (int i=0; i < dim(); i++)
37             dat[i] += v.dat[i];
38     }
39     
40     void operator /=(Real a) {
41         (*this) *= 1/a;
42     }
43
44     void operator *=(Real a) {
45         for (int i=0; i < dim(); i++)
46             dat[i] *= a;
47     }
48
49     void operator -=(Vector v) {
50         assert(v.dim() == dim());
51         for (int i=0; i < dim(); i++)
52             dat[i] -= v(i);     
53     }
54
55     Real &operator()(int i) { return dat[i]; }
56     Real operator()(int i) const { return dat[i]; }
57     Real elem(int i) { return dat[i]; }
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     Vector operator-() const;
66     Real norm() {
67         return sqrt(norm_sq() );
68     }
69     Real norm_sq() {
70         return ((*this) * (*this));
71     }
72     operator svec<Real> () { return dat; }
73     void print() const;
74     /// set to j-th element of unit-base
75     void set_unit(int j) ;
76 };
77 /**
78     a vector. Storage is handled in svec, Vector only does the mathematics.
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