lilypond-0.0.3
[lilypond.git] / matrix.hh
blobe092a5fbe67bf343f89ba1f208af779d1c5c9db8
1 #ifndef MATRIX_HH
2 #define MATRIX_HH
5 #include "vsmat.hh"
6 #include "vector.hh"
8 /// a Real matrix
9 class Matrix {
10 virtual_smat *dat;
12 public:
13 void OK() const { dat->OK(); }
14 int cols() const { return dat->cols(); }
15 int rows() const { return dat->rows(); }
17 /// return the size of a matrix
18 int dim() const;
19 /**
20 PRE
21 the matrix needs to be square.
24 // Matrix() { dat = 0; }
25 ~Matrix() { delete dat; }
27 /// set entries to r
28 void fill(Real r);
30 /// set diagonal to d
31 void set_diag(Real d);
33 void set_diag(Vector d);
34 /// set unit matrix
35 void unit() { set_diag(1.0); }
37 void operator+=(const Matrix&m);
38 void operator-=(const Matrix&m);
39 void operator*=(Real a);
40 void operator/=(Real a) { (*this) *= 1/a; }
42 /// add a row
43 void insert_row(Vector v,int k);
44 /**
45 add a row to the matrix before row k
47 PRE
48 v.dim() == cols()
49 0 <= k <= rows()
51 ///
52 void delete_row(int k) { dat->delete_row(k); }
53 /**
54 delete a row from this matrix.
56 PRE
57 0 <= k < rows();
59 void delete_column(int k) { dat->delete_column(k); }
60 ///
61 Matrix(int n);
62 /**
63 square n matrix, initialised to null
65 ///
66 Matrix(int n, int m);
67 /**
68 n x m matrix, init to 0
70 Matrix(const Matrix &m);
72 /// dyadic product: v * w.transpose
73 Matrix(Vector v, Vector w);
74 void operator=(const Matrix&m);
76 /// access an element
77 Real operator()(int i,int j) const;
79 /// access an element
80 Real &operator()(int i, int j);
82 /// Matrix multiply with vec (from right)
83 Vector operator *(const Vector &v) const;
85 /// set this to m1*m2.
86 void set_product(const Matrix &m1, const Matrix &m2);
89 Vector left_multiply(Vector const &) const;
91 Matrix operator-() const;
93 /// transpose this.
94 void transpose();
96 /// return a transposed copy.
97 Matrix transposed() const ;
99 Real norm() const;
100 /// swap
101 void swap_columns(int c1, int c2);
104 0 <= c1,c2 < cols()
107 /// swap
108 void swap_rows(int c1, int c2);
111 0 <= c1,c2 < rows()
115 Vector row(int ) const;
116 Vector col(int) const;
118 operator String() const;
119 void print() const;
122 /** This is a class for a nonsquare block of #Real#s. The
123 implementation of sparse matrices is done in the appropriate #smat#
124 class. Matrix only does the mathematical actions (adding,
125 multiplying, etc.)
128 TODO
129 implement ref counting? */
132 inline Vector
133 operator *(Vector &v, const Matrix& m) { return m.left_multiply(v); }
134 Matrix operator *(const Matrix& m1,const Matrix &m2);
135 Matrix operator /(const Matrix &m1,Real a);
136 inline Matrix operator -(Matrix m1,const Matrix m2)
138 m1 -= m2;
139 return m1;
141 #endif