]> git.donarmstrong.com Git - lilypond.git/blob - vector.hh
release: 0.0.2
[lilypond.git] / vector.hh
1 #ifndef VECTOR_HH
2 #define VECTOR_HH
3
4 #include "glob.hh"
5 #include "vray.hh"
6
7 /// a row of numbers
8 class Vector  {
9     svec<Real> dat;
10 public:
11     void OK() const { dat.OK();}
12     int dim() const { return dat.sz(); }
13     Vector() { }
14     Vector(const Vector&n);
15     Vector(int n) {
16         dat.set_size(n);
17         fill(0);
18     }
19     void insert(Real v, int i) {
20         dat.insert(v,i);
21     }
22     void del(int i) { dat.del(i); }
23     operator String() const;
24     void fill(Real r) {
25         for (int i=0; i < dim(); i++)
26             dat[i] =r;
27     }
28
29     void operator +=(Vector v) {
30         assert(v.dim() == dim());
31         for (int i=0; i < dim(); i++)
32             dat[i] += v.dat[i];
33     }
34     
35     void operator /=(Real a) {
36         (*this) *= 1/a;
37     }
38
39     void operator *=(Real a) {
40         for (int i=0; i < dim(); i++)
41             dat[i] *= a;
42     }
43
44     void operator -=(Vector v) {
45         assert(v.dim() == dim());
46         for (int i=0; i < dim(); i++)
47             dat[i] -= v(i);     
48     }
49
50     Real &operator()(int i) { return dat[i]; }
51     Real operator()(int i) const { return dat[i]; }
52     Real elem(int i) { return dat[i]; }
53     Real operator *(Vector v) const {
54         Real ip=0;
55         assert(v.dim() == dim());
56         for (int i=0; i < dim(); i++)
57             ip += dat[i] *v(i);
58         return ip;
59     }
60     Vector operator-() const;
61     Real norm() {
62         return sqrt(norm_sq() );
63     }
64     Real norm_sq() {
65         return ((*this) * (*this));
66     }
67     operator svec<Real> () { return dat; }
68     void print() const;
69     /// set to j-th element of unit-base
70     void set_unit(int j) ;
71 };
72 /**
73     a vector. Storage is handled in svec, Vector only does the mathematics.
74  */
75
76 inline Vector
77 operator+(Vector a, Vector const &b) {
78     a += b;
79     return a;
80 }
81
82 inline Vector
83 operator-(Vector a, Vector const &b) {
84     a -= b;
85     return a;
86 }
87
88 inline Vector
89 operator*(Vector v, Real a) {
90     v *= a;
91     return v;
92 }
93
94 inline Vector
95 operator*( Real a,Vector v) {
96     v *= a;
97     return v;
98 }
99
100 inline Vector
101 operator/(Vector v,Real a) {
102     v *= 1/a;
103     return v;
104 }
105
106 #endif