]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/matrix.hh
321aeaadcf96978735aa3c18e0dd0dc631788b55
[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 #include "offset.hh"
16
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.)
21
22     
23     TODO
24     implement ref counting?  */
25
26
27 class Matrix {
28   friend Matrix operator *(Matrix const &m1, Matrix const &m2);
29
30 protected:
31   Matrix_storage *dat;
32   void set (Matrix_storage*);
33   Matrix (Matrix_storage*);
34 public:
35   void OK() const { dat->OK(); }
36   int cols() const { return dat->cols (); }
37   int rows() const { return dat->rows (); }
38
39   /**  return the size of a matrix. 
40     PRE
41     the matrix needs to be square.
42     */
43   int dim() const;
44      
45   /**
46     the band size of the matrix.
47     @ret
48
49     0 <= band_i() <= dim
50     */
51   int band_i() const;
52   bool band_b() const;
53   void set_full() const;
54   void try_set_band() const;
55   ~Matrix() { delete dat; }
56
57   /// set entries to r 
58   void fill (Real r);
59
60   /// set diagonal to d
61   void set_diag (Real d);
62
63   void set_diag (Vector d);
64   /// set unit matrix
65   void unit() { set_diag (1.0); }
66
67   void operator+=(Matrix const &m);
68   void operator-=(Matrix const &m);    
69   void operator*=(Real a);
70   void operator/=(Real a) { (*this) *= 1/a; }
71     
72   /**  add a row. 
73     add a row to the matrix before  row k
74
75     PRE
76     v.dim() == cols ()
77     0 <= k <= rows()
78     */
79   void insert_row (Vector v,int k);
80   /** . 
81     delete a row from this matrix.
82
83     PRE
84     0 <= k < rows();
85     */
86   void delete_row (int k) { dat->delete_row (k); }
87   void delete_column (int k) { dat->delete_column (k); }
88
89   /**
90     square n matrix, initialised to null
91     */
92   Matrix (int n);
93    
94   /**
95     n x m matrix, init to 0
96     */
97   Matrix (int n, int m);
98   Matrix (Matrix const &m);
99
100   /// dyadic product: v * w.transpose
101   Matrix (Vector v, Vector w);
102   void operator=(Matrix const &m);
103
104   /// access an element
105   Real operator()(int i,int j) const { return dat->elem (i,j); }
106
107   /// access an element
108   Real &operator()(int i, int j) { return dat->elem (i,j); }
109
110   /// Matrix multiply with vec (from right)
111   Vector operator *(Vector const &v) const;
112
113   /// set this to m1*m2.
114   void set_product (Matrix const &m1, Matrix const &m2);
115
116   Vector left_multiply (Vector const &) const;
117     
118   Matrix operator-() const;
119     
120   /// transpose this.
121   void transpose();
122     
123   /// return a transposed copy.
124   Matrix transposed() const ;
125
126   Real norm() const;
127   /**  swap. 
128     PRE
129     0 <= c1,c2 < cols()
130     */
131   void swap_columns (int c1, int c2);
132
133   /**  swap. 
134     PRE
135     0 <= c1,c2 < rows()
136     */
137   void swap_rows (int c1, int c2);
138
139
140   Vector row (int) const;
141   Vector col (int) const;
142
143   operator String() const;
144   void print() const;
145 };
146
147 inline Vector
148 operator *(Vector &v, Matrix const & m) { return m.left_multiply (v); }
149 Matrix operator *(Matrix const & m1,Matrix const &m2);
150 Matrix operator /(Matrix const &m1,Real a);
151 inline Matrix operator -(Matrix m1,const Matrix m2)
152 {
153   m1 -= m2;
154   return m1;
155 }
156 inline Matrix operator +(Matrix m1,const Matrix m2)
157 {
158   m1 += m2;
159   return m1;
160 }
161 #endif