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