X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Fmatrix.hh;h=a6c13f837cf81c12b465718a4417979f83b403c3;hb=47db9a3883d726ca53e2133a3b2298f78dd6a32e;hp=d94601a5ce76fb3290cdfe46e2b92f917a3fb44c;hpb=1cf3d59c1559fb9774c4c1c8cae155cfe54a927c;p=lilypond.git diff --git a/flower/include/matrix.hh b/flower/include/matrix.hh index d94601a5ce..a6c13f837c 100644 --- a/flower/include/matrix.hh +++ b/flower/include/matrix.hh @@ -1,159 +1,77 @@ /* - matrix.hh -- declare Matrix + This file is part of LilyPond, the GNU music typesetter. - source file of the Flower Library + Copyright (C) 2006--2015 Joe Neeman - (c) 1996, 1997--1998 Han-Wen Nienhuys -*/ + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . +*/ #ifndef MATRIX_HH #define MATRIX_HH -#include "full-storage.hh" -#include "vector.hh" -#include "offset.hh" - -/** a Real matrix. This is a class for a nonsquare block of #Real#s. The - implementation of sparse matrices is done in the appropriate #smat# - class. Matrix only does the mathematical actions (adding, - multiplying, etc.) - - - TODO - implement ref counting? */ +#include "std-vector.hh" - -class Matrix { - friend Matrix operator *(Matrix const &m1, Matrix const &m2); - -protected: - Full_storage *dat_; - void set (Matrix_storage*); +template > +class Matrix +{ public: - void OK() const { dat_->OK(); } - void set_band (); - int calc_band_i () const; - int cols() const { return dat_->cols (); } - int rows() const { return dat_->rows (); } - - /** return the size of a matrix. - PRE - the matrix needs to be square. - */ - int dim() const; - - /** - the band size of the matrix. - @ret - - 0 <= band_i() <= dim - */ - int band_i() const; - - /// set entries to r - void fill (Real r); - - /// set diagonal to d - void set_diag (Real d); - - void set_diag (Vector d); - /// set unit matrix - void unit() { set_diag (1.0); } - - void operator+=(Matrix const &m); - void operator-=(Matrix const &m); - void operator*=(Real a); - void operator/=(Real a) { (*this) *= 1/a; } - - /** add a row. - add a row to the matrix before row k - - PRE - v.dim() == cols () - 0 <= k <= rows() - */ - void insert_row (Vector v,int k); - /** . - delete a row from this matrix. - - PRE - 0 <= k < rows(); - */ - void delete_row (int k) { dat_->delete_row (k); } - void delete_column (int k) { dat_->delete_column (k); } - - /** - square n matrix, initialised to null - */ - Matrix (int n); - - /** - n x m matrix, init to 0 - */ - Matrix (int n, int m); - Matrix (Matrix const &m); - - /// dyadic product: v * w.transpose - Matrix (Vector v, Vector w); - void operator=(Matrix const &m); - - /// access an element - Real operator()(int i,int j) const { return dat_->elem (i,j); } - - /// access an element - Real &operator()(int i, int j) { return dat_->elem (i,j); } - - /// Matrix multiply with vec (from right) - Vector operator *(Vector const &v) const; - - /// set this to m1*m2. - void set_product (Matrix const &m1, Matrix const &m2); - - Vector left_multiply (Vector const &) const; - - Matrix operator-() const; - - /// transpose this. - void transpose(); - - /// return a transposed copy. - Matrix transposed() const ; - - Real norm() const; - /** swap. - PRE - 0 <= c1,c2 < cols() - */ - void swap_columns (int c1, int c2); - - /** swap. - PRE - 0 <= c1,c2 < rows() - */ - void swap_rows (int c1, int c2); - - - Vector row (int) const; - Vector col (int) const; - - String str () const; - void print() const; - ~Matrix (); + Matrix () + { + rank_ = 0; + } + + Matrix (vsize rows, vsize columns, T const &t) + : data_ (rows *columns, t) + { + rank_ = rows; + } + + const T &at (vsize row, vsize col) const + { + assert (row < rank_ && col * rank_ + row < data_.size ()); + + return data_[col * rank_ + row]; + } + + T &at (vsize row, vsize col) + { + assert (row < rank_ && col * rank_ + row < data_.size ()); + + return data_[col * rank_ + row]; + } + + void resize (vsize rows, vsize columns, T const &t) + { + if (rows == rank_) + data_.resize (rows * columns, t); + else + { + vector new_data; + new_data.resize (rows * columns, t); + vsize cur_cols = rank_ ? data_.size () / rank_ : 0; + + for (vsize i = 0; i < cur_cols; i++) + for (vsize j = 0; j < rank_; j++) + new_data[i * rows + j] = data_[i * rank_ + j]; + rank_ = rows; + data_ = new_data; + } + } + +private: + vector data_; + vsize rank_; }; -inline Vector -operator *(Vector &v, Matrix const & m) { return m.left_multiply (v); } -Matrix operator *(Matrix const & m1,Matrix const &m2); -Matrix operator /(Matrix const &m1,Real a); -inline Matrix operator -(Matrix m1,const Matrix m2) -{ - m1 -= m2; - return m1; -} -inline Matrix operator +(Matrix m1,const Matrix m2) -{ - m1 += m2; - return m1; -} -#endif +#endif /* MATRIX_HH */