]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/matrix.hh
release: 0.1.12
[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   friend Matrix operator *(Matrix const &m1, Matrix const &m2);
28
29 protected:
30   Matrix_storage *dat;
31   void set (Matrix_storage*);
32   Matrix (Matrix_storage*);
33 public:
34   void OK() const { dat->OK(); }
35   int cols() const { return dat->cols (); }
36   int rows() const { return dat->rows (); }
37
38   /**  return the size of a matrix. 
39     PRE
40     the matrix needs to be square.
41     */
42   int dim() const;
43      
44   /**
45     the band size of the matrix.
46     @ret
47
48     0 <= band_i() <= dim
49     */
50   int band_i() const;
51   bool band_b() const;
52   void set_full() const;
53   void try_set_band() const;
54   ~Matrix() { delete dat; }
55
56   /// set entries to r 
57   void fill (Real r);
58
59   /// set diagonal to d
60   void set_diag (Real d);
61
62   void set_diag (Vector d);
63   /// set unit matrix
64   void unit() { set_diag (1.0); }
65
66   void operator+=(Matrix const &m);
67   void operator-=(Matrix const &m);    
68   void operator*=(Real a);
69   void operator/=(Real a) { (*this) *= 1/a; }
70     
71   /**  add a row. 
72     add a row to the matrix before  row k
73
74     PRE
75     v.dim() == cols ()
76     0 <= k <= rows()
77     */
78   void insert_row (Vector v,int k);
79   /** . 
80     delete a row from this matrix.
81
82     PRE
83     0 <= k < rows();
84     */
85   void delete_row (int k) { dat->delete_row (k); }
86   void delete_column (int k) { dat->delete_column (k); }
87
88   /**
89     square n matrix, initialised to null
90     */
91   Matrix (int n);
92    
93   /**
94     n x m matrix, init to 0
95     */
96   Matrix (int n, int m);
97   Matrix (Matrix const &m);
98
99   /// dyadic product: v * w.transpose
100   Matrix (Vector v, Vector w);
101   void operator=(Matrix const &m);
102
103   /// access an element
104   Real operator()(int i,int j) const { return dat->elem (i,j); }
105
106   /// access an element
107   Real &operator()(int i, int j) { return dat->elem (i,j); }
108
109   /// Matrix multiply with vec (from right)
110   Vector operator *(Vector const &v) const;
111
112   /// set this to m1*m2.
113   void set_product (Matrix const &m1, Matrix const &m2);
114
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