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