lilypond-1.3.18
[lilypond.git] / flower / include / matrix.hh
blob9078c378320e3719a617eaf70d5231444643b62c
1 /*
2 matrix.hh -- declare Matrix
4 source file of the Flower Library
6 (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
10 #ifndef MATRIX_HH
11 #define MATRIX_HH
13 #include "full-storage.hh"
14 #include "vector.hh"
15 #include "offset.hh"
17 /** a Real matrix. This is a class for a nonsquare block of #Real#s. The
18 implementation of sparse matrices is done in the appropriate #smat#
19 class. Matrix only does the mathematical actions (adding,
20 multiplying, etc.)
23 TODO
24 implement ref counting? */
27 class Matrix {
28 friend Matrix operator *(Matrix const &m1, Matrix const &m2);
30 protected:
31 Full_storage *dat_;
32 void set (Matrix_storage*);
33 public:
34 void OK() const { dat_->OK(); }
35 void set_band ();
36 int calc_band_i () const;
37 int cols() const { return dat_->cols (); }
38 int rows() const { return dat_->rows (); }
40 /** return the size of a matrix.
41 PRE
42 the matrix needs to be square.
44 int dim() const;
46 /**
47 the band size of the matrix.
48 @ret
50 0 <= band_i() <= dim
52 int band_i() const;
54 /// set entries to r
55 void fill (Real r);
57 /// set diagonal to d
58 void set_diag (Real d);
60 void set_diag (Vector d);
61 /// set unit matrix
62 void unit() { set_diag (1.0); }
64 void operator+=(Matrix const &m);
65 void operator-=(Matrix const &m);
66 void operator*=(Real a);
67 void operator/=(Real a) { (*this) *= 1/a; }
69 /** add a row.
70 add a row to the matrix before row k
72 PRE
73 v.dim() == cols ()
74 0 <= k <= rows()
76 void insert_row (Vector v,int k);
77 /** .
78 delete a row from this matrix.
80 PRE
81 0 <= k < rows();
83 void delete_row (int k) { dat_->delete_row (k); }
84 void delete_column (int k) { dat_->delete_column (k); }
86 /**
87 square n matrix, initialised to null
89 Matrix (int n);
91 /**
92 n x m matrix, init to 0
94 Matrix (int n, int m);
95 Matrix (Matrix const &m);
97 /// dyadic product: v * w.transpose
98 Matrix (Vector v, Vector w);
99 void operator=(Matrix const &m);
101 /// access an element
102 Real operator()(int i,int j) const { return dat_->elem (i,j); }
104 /// access an element
105 Real &operator()(int i, int j) { return dat_->elem (i,j); }
107 /// Matrix multiply with vec (from right)
108 Vector operator *(Vector const &v) const;
110 /// set this to m1*m2.
111 void set_product (Matrix const &m1, Matrix const &m2);
113 Vector left_multiply (Vector const &) const;
115 Matrix operator-() const;
117 /// transpose this.
118 void transpose();
120 /// return a transposed copy.
121 Matrix transposed() const ;
123 Real norm() const;
124 /** swap.
126 0 <= c1,c2 < cols()
128 void swap_columns (int c1, int c2);
130 /** swap.
132 0 <= c1,c2 < rows()
134 void swap_rows (int c1, int c2);
137 Vector row (int) const;
138 Vector col (int) const;
140 String str () const;
141 void print() const;
142 ~Matrix ();
145 inline Vector
146 operator *(Vector &v, Matrix const & m) { return m.left_multiply (v); }
147 Matrix operator *(Matrix const & m1,Matrix const &m2);
148 Matrix operator /(Matrix const &m1,Real a);
149 inline Matrix operator -(Matrix m1,const Matrix m2)
151 m1 -= m2;
152 return m1;
154 inline Matrix operator +(Matrix m1,const Matrix m2)
156 m1 += m2;
157 return m1;
159 #endif