]> git.donarmstrong.com Git - lilypond.git/blob - flower/matrix-storage.cc
release: 0.1.1
[lilypond.git] / flower / matrix-storage.cc
1 /*
2   matrix-storage.cc -- implement Matrix_storage
3
4   source file of the Flower Library
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include "full-storage.hh"
10 #include "diagonal-storage.hh"
11
12 void
13 Matrix_storage::set_addition_result(Matrix_storage *&dat, Matrix_storage *right)
14 {
15     if (dat && dat->name() == Diagonal_storage::static_name() 
16          && right->name() == Diagonal_storage::static_name()) {
17         Diagonal_storage *L = (Diagonal_storage*)dat;
18         Diagonal_storage* R = (Diagonal_storage*) right;
19
20         if ( R->band_size_i() > L->band_size_i()) { 
21             L->set_band_size(R->band_size_i());
22         }
23             return ;
24     }
25     if  (!dat || !dat->is_type_b(Full_storage::static_name() )) {
26
27         Matrix_storage *new_stor = (dat)? new Full_storage(dat) : 
28             new Full_storage( right->rows(), right->cols());
29         delete dat;
30         dat = new_stor;
31     }
32 }
33
34 Matrix_storage*
35 Matrix_storage::get_product_result(Matrix_storage*left, Matrix_storage*right)
36 {
37     Matrix_storage* dest =0;
38     set_product_result(dest, left,right);
39     return dest;
40 }
41     
42
43 /*
44   hairy
45  */
46 void
47 Matrix_storage::set_product_result(Matrix_storage*&dest, 
48                            Matrix_storage*left, Matrix_storage*right)
49 {
50     if ( left->name() == Diagonal_storage::static_name() 
51          && right->name() == Diagonal_storage::static_name()) {
52         Diagonal_storage *L = (Diagonal_storage*)left;
53         Diagonal_storage* R = (Diagonal_storage*) right;
54
55         if  (L->band_size_i() + R->band_size_i() < L->dim()/2 ) {
56             if (dest ->name() != Diagonal_storage::static_name()){
57                 delete dest;
58                 dest = new Diagonal_storage;
59             }
60          
61             dest->set_size(L->dim());
62             return;
63         }
64     }
65
66     if ( dest && dest->name() == Full_storage::static_name()) {
67         dest->set_size(left->rows(), right->cols());
68     } else {
69         delete dest;
70         dest = new Full_storage( left->rows(), right->cols());
71     }
72 }
73
74 IMPLEMENT_IS_TYPE_B(Matrix_storage);
75
76 Matrix_storage *
77 Matrix_storage::get_full(int n, int m)
78 {
79     return new Full_storage(n,m);
80 }
81
82
83
84  bool
85 Matrix_storage::try_right_multiply(Matrix_storage *, 
86                                    const Matrix_storage *)const
87 {
88     return false;
89 }
90
91 Array<Real>
92 Matrix_storage::row(int n) const
93 {
94     Array<Real> r;
95     for (int j = 0; j < cols(); j++)
96         r.push(elem(n,j));
97     return r;
98 }
99
100 Array<Real>
101 Matrix_storage::column(int n) const
102 {
103     Array<Real> r;
104     for (int i = 0; i < rows(); i++)
105         r.push(elem(i,n));
106     return r;
107 }
108
109 void
110 Matrix_storage::set_size(int rows, int cols)
111 {
112         
113     resize(rows,cols);
114 }
115         
116 void
117 Matrix_storage::set_size(int rows)
118 {
119         
120         resize(rows);
121 }
122         
123
124 void
125 Matrix_storage::set_band(Matrix_storage *&mat, int b)
126 {
127     Matrix_storage* ns = new Diagonal_storage(mat, b);
128     delete mat;
129     mat=ns;
130 }
131         
132
133 void
134 Matrix_storage::set_full(Matrix_storage *&mat)
135 {
136     Matrix_storage* ns = new Full_storage(mat);
137     delete mat;
138     mat=ns;
139 }