]> git.donarmstrong.com Git - lilypond.git/blob - flower/lib/include/matrix.hh
partial: 0.0.42.pre3.hanjan
[lilypond.git] / flower / lib / include / matrix.hh
1 #ifndef MATRIX_HH
2 #define MATRIX_HH
3
4
5 #include "vsmat.hh"
6 #include "vector.hh"
7
8 /**  a Real matrix.  This is a class for a nonsquare block of #Real#s.  The
9     implementation of sparse matrices is done in the appropriate #smat#
10     class. Matrix only does the mathematical actions (adding,
11     multiplying, etc.)
12
13     
14     TODO
15     implement ref counting?  */
16
17
18 class Matrix {
19     virtual_smat *dat;
20     
21 public:
22     void OK() const { dat->OK(); }
23     int cols() const { return dat->cols(); }
24     int rows() const { return dat->rows(); }
25
26     /**  return the size of a matrix. 
27       PRE
28       the matrix needs to be square.
29     */
30     int dim() const;
31      
32     // Matrix() { dat = 0; } 
33     ~Matrix() { delete dat; }
34
35     /// set entries to r 
36     void fill(Real r);
37
38     /// set diagonal to d
39     void set_diag(Real d);
40
41     void set_diag(Vector d);
42     /// set unit matrix
43     void unit() { set_diag(1.0); }
44
45     void operator+=(const Matrix&m);
46     void operator-=(const Matrix&m);    
47     void operator*=(Real a);
48     void operator/=(Real a) { (*this) *= 1/a; }
49     
50     /**  add a row. 
51       add a row to the matrix before  row k
52
53       PRE
54       v.dim() == cols()
55       0 <= k <= rows()
56     */
57     void insert_row(Vector v,int k);
58     /** . 
59       delete a row from this matrix.
60
61       PRE
62       0 <= k < rows();
63     */
64     void delete_row(int k) { dat->delete_row(k); }
65     void delete_column(int k) { dat->delete_column(k); }
66
67     /**
68       square n matrix, initialised to null
69     */
70     Matrix(int n);
71
72     /**
73       n x m matrix, init to 0
74     */
75     Matrix(int n, int m);
76     Matrix(const Matrix &m);
77
78     /// dyadic product: v * w.transpose
79     Matrix(Vector v, Vector w);
80     void operator=(const Matrix&m);
81
82     /// access an element
83     Real operator()(int i,int j) const { return dat->elem(i,j); }
84
85     /// access an element
86     Real &operator()(int i, int j) { return dat->elem(i,j); }
87
88     /// Matrix multiply with vec (from right)
89     Vector operator *(const Vector &v) const;
90
91     /// set this to m1*m2.
92     void set_product(const Matrix &m1, const Matrix &m2);
93
94
95     Vector left_multiply(Vector const &) const;
96     
97     Matrix operator-() const;
98     
99     /// transpose this.
100     void transpose();
101     
102     /// return a transposed copy.
103     Matrix transposed() const ;
104
105     Real norm() const;
106     /**  swap. 
107       PRE
108       0 <= c1,c2 < cols()
109     */
110     void swap_columns(int c1, int c2);
111
112     /**  swap. 
113       PRE
114       0 <= c1,c2 < rows()
115     */
116     void swap_rows(int c1, int c2);
117
118
119     Vector row(int ) const;
120     Vector col(int) const;
121
122     operator String() const;
123     void print() const;
124 };
125
126 inline Vector
127 operator *(Vector &v, const Matrix& m) { return m.left_multiply(v); }
128 Matrix operator *(const Matrix& m1,const Matrix &m2);
129 Matrix operator /(const Matrix &m1,Real a);
130 inline Matrix operator -(Matrix m1,const Matrix m2)
131 {
132     m1 -= m2;
133     return m1;
134 }
135 #endif