]> git.donarmstrong.com Git - lilypond.git/blob - flower/matrix.hh
release: 0.0.2
[lilypond.git] / flower / 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
9 class Matrix {
10     virtual_smat *dat;
11     
12 public:
13     void OK() const { dat->OK(); }
14     int cols() const { return dat->cols(); }
15     int rows() const { return dat->rows(); }
16
17     /// return the size of a matrix
18     int dim() const;
19     /**
20       PRE
21       the matrix needs to be square.
22     */
23      
24     // Matrix() { dat = 0; } 
25     ~Matrix() { delete dat; }
26
27     /// set entries to r 
28     void fill(Real r);
29
30     /// set diagonal to d
31     void set_diag(Real d);
32
33     void set_diag(Vector d);
34     /// set unit matrix
35     void unit() { set_diag(1.0); }
36
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; }
41     
42     /// add a row
43     void insert_row(Vector v,int k);
44     /**
45       add a row to the matrix before  row k
46
47       PRE
48       v.dim() == cols()
49       0 <= k <= rows()
50     */
51     ///
52     void delete_row(int k) { dat->delete_row(k); }
53     /**
54       delete a row from this matrix.
55
56       PRE
57       0 <= k < rows();
58     */
59     void delete_column(int k) { dat->delete_column(k); }
60     ///
61     Matrix(int n);
62     /**
63       square n matrix, initialised to null
64     */
65     ///
66     Matrix(int n, int m);
67     /**
68       n x m matrix, init to 0
69     */
70     Matrix(const Matrix &m);
71
72     /// dyadic product: v * w.transpose
73     Matrix(Vector v, Vector w);
74     void operator=(const Matrix&m);
75
76     /// access an element
77     Real operator()(int i,int j) const;
78
79     /// access an element
80     Real &operator()(int i, int j);
81
82     /// Matrix multiply with vec (from right)
83     Vector operator *(const Vector &v) const;
84
85     /// set this to m1*m2.
86     void set_product(const Matrix &m1, const Matrix &m2);
87
88
89     Vector left_multiply(Vector const &) const;
90     
91     Matrix operator-() const;
92     
93     /// transpose this.
94     void transpose();
95     
96     /// return a transposed copy.
97     Matrix transposed() const ;
98
99     Real norm() const;
100     /// swap
101     void swap_columns(int c1, int c2);
102     /**
103       PRE
104       0 <= c1,c2 < cols()
105     */
106
107     /// swap
108     void swap_rows(int c1, int c2);
109     /**
110       PRE
111       0 <= c1,c2 < rows()
112     */
113
114
115     Vector row(int ) const;
116     Vector col(int) const;
117
118     operator String() const;
119     void print() const;
120 };
121
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.)
126
127     
128     TODO
129     implement ref counting?  */
130
131
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)
137 {
138     m1 -= m2;
139     return m1;
140 }
141 #endif