lilypond-1.3.115
[lilypond.git] / flower / matrix-storage.cc
blob7d2c8fbcadfcbec6f291f71ff9b1fc4db1274c06
1 /*
2 matrix-storage.cc -- implement Matrix_storage
4 source file of the Flower Library
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include "full-storage.hh"
10 #include "diagonal-storage.hh"
12 void
13 Matrix_storage::set_addition_result (Matrix_storage *&dat, Matrix_storage *right)
15 if (dat && dat->name() == Diagonal_storage::static_name ()
16 && right->name() == Diagonal_storage::static_name ())
18 Diagonal_storage *L = (Diagonal_storage*)dat;
19 Diagonal_storage* R = (Diagonal_storage*) right;
21 if (R->band_size_i() > L->band_size_i ())
23 L->set_band_size (R->band_size_i());
25 return ;
27 if (!dat || !dat->is_type_b (Full_storage::static_name()))
30 Matrix_storage *new_stor = (dat)? new Full_storage (dat) :
31 new Full_storage (right->rows(), right->cols ());
32 delete dat;
33 dat = new_stor;
37 Matrix_storage*
38 Matrix_storage::get_product_result (Matrix_storage*left, Matrix_storage*right)
40 Matrix_storage* dest =0;
41 set_product_result (dest, left,right);
42 return dest;
47 hairy
49 void
50 Matrix_storage::set_product_result (Matrix_storage*&dest,
51 Matrix_storage*left, Matrix_storage*right)
53 if (left->name() == Diagonal_storage::static_name ()
54 && right->name() == Diagonal_storage::static_name ())
56 Diagonal_storage *L = (Diagonal_storage*)left;
57 Diagonal_storage* R = (Diagonal_storage*) right;
59 if (L->band_size_i() + R->band_size_i () < L->dim ()/2)
61 if (dest ->name() != Diagonal_storage::static_name ())
63 delete dest;
64 dest = new Diagonal_storage;
67 dest->set_size (L->dim());
68 return;
72 if (dest && dest->name() == Full_storage::static_name ())
74 dest->set_size (left->rows(), right->cols ());
76 else
78 delete dest;
79 dest = new Full_storage (left->rows(), right->cols ());
83 IMPLEMENT_IS_TYPE_B(Matrix_storage);
85 Matrix_storage *
86 Matrix_storage::get_full (int n, int m)
88 return new Full_storage (n,m);
93 bool
94 Matrix_storage::try_right_multiply (Matrix_storage *,
95 const Matrix_storage *) const
97 return false;
100 Array<Real>
101 Matrix_storage::row (int n) const
103 Array<Real> r;
104 for (int j = 0; j < cols(); j++)
105 r.push (elem (n,j));
106 return r;
109 Array<Real>
110 Matrix_storage::column (int n) const
112 Array<Real> r;
113 for (int i = 0; i < rows(); i++)
114 r.push (elem (i,n));
115 return r;
118 void
119 Matrix_storage::set_size (int rows, int cols)
122 resize (rows,cols);
125 void
126 Matrix_storage::set_size (int rows)
129 resize (rows);
133 void
134 Matrix_storage::set_band (Matrix_storage *&mat, int b)
136 Matrix_storage* ns = new Diagonal_storage (mat, b);
137 delete mat;
138 mat=ns;
142 void
143 Matrix_storage::set_full (Matrix_storage *&mat)
145 Matrix_storage* ns = new Full_storage (mat);
146 delete mat;
147 mat=ns;