lilypond-0.0.27
[lilypond.git] / flower / vector.hh
blob2fdf41482a68e52f4477e2a495c4c08ac57d1424
1 #ifndef VECTOR_HH
2 #define VECTOR_HH
4 #include <math.h>
5 #include "real.hh"
6 #include "varray.hh"
8 class Dstream;
9 class String;
10 void set_matrix_debug(Dstream&ds);
12 /// a row of numbers
13 class Vector {
14 Array<Real> dat;
15 public:
16 void OK() const { dat.OK();}
17 int dim() const { return dat.size(); }
18 Vector() { }
19 Vector(const Vector&n);
20 Vector(int n) {
21 dat.set_size(n);
22 fill(0);
24 void insert(Real v, int i) {
25 dat.insert(v,i);
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;
34 void operator +=(Vector v) {
35 assert(v.dim() == dim());
36 for (int i=0; i < dim(); i++)
37 dat[i] += v.dat[i];
40 void operator /=(Real a) {
41 (*this) *= 1/a;
44 void operator *=(Real a) {
45 for (int i=0; i < dim(); i++)
46 dat[i] *= a;
49 void operator -=(Vector v) {
50 assert(v.dim() == dim());
51 for (int i=0; i < dim(); i++)
52 dat[i] -= v(i);
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;
65 Vector operator-() const;
66 Real norm() {
67 return sqrt(norm_sq() );
69 Real norm_sq() {
70 return ((*this) * (*this));
72 operator Array<Real> () { return dat; }
73 void print() const;
74 /// set to j-th element of unit-base
75 void set_unit(int j) ;
77 /**
78 a vector. Storage is handled in Array, Vector only does the mathematics.
81 inline Vector
82 operator+(Vector a, Vector const &b) {
83 a += b;
84 return a;
87 inline Vector
88 operator-(Vector a, Vector const &b) {
89 a -= b;
90 return a;
93 inline Vector
94 operator*(Vector v, Real a) {
95 v *= a;
96 return v;
99 inline Vector
100 operator*( Real a,Vector v) {
101 v *= a;
102 return v;
105 inline Vector
106 operator/(Vector v,Real a) {
107 v *= 1/a;
108 return v;
111 #endif