lilypond-0.0.1
[lilypond.git] / vector.hh
blob5be792c54b058bb4edc9a845b4375507f5586b67
1 #ifndef VECTOR_HH
2 #define VECTOR_HH
4 #include "glob.hh"
5 #include "vray.hh"
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);
19 void insert(Real v, int i) {
20 dat.insert(v,i);
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;
29 void operator +=(Vector v) {
30 assert(v.dim() == dim());
31 for (int i=0; i < dim(); i++)
32 dat[i] += v.dat[i];
35 void operator /=(Real a) {
36 (*this) *= 1/a;
39 void operator *=(Real a) {
40 for (int i=0; i < dim(); i++)
41 dat[i] *= a;
44 void operator -=(Vector v) {
45 assert(v.dim() == dim());
46 for (int i=0; i < dim(); i++)
47 dat[i] -= v(i);
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;
60 Vector operator-() const;
61 Real norm() {
62 return sqrt(norm_sq() );
64 Real norm_sq() {
65 return ((*this) * (*this));
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) ;
72 /**
73 a vector. Storage is handled in svec, Vector only does the mathematics.
76 inline Vector
77 operator+(Vector a, Vector const &b) {
78 a += b;
79 return a;
82 inline Vector
83 operator-(Vector a, Vector const &b) {
84 a -= b;
85 return a;
88 inline Vector
89 operator*(Vector v, Real a) {
90 v *= a;
91 return v;
94 inline Vector
95 operator*( Real a,Vector v) {
96 v *= a;
97 return v;
100 inline Vector
101 operator/(Vector v,Real a) {
102 v *= 1/a;
103 return v;
106 #endif