]> git.donarmstrong.com Git - lilypond.git/blob - flower/matrix-storage.cc
release: 0.1.11
[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            {
18         Diagonal_storage *L = (Diagonal_storage*)dat;
19         Diagonal_storage* R = (Diagonal_storage*) right;
20
21         if ( R->band_size_i() > L->band_size_i ()) 
22           {
23             L->set_band_size (R->band_size_i());
24           }
25             return ;
26     }
27   if  (!dat || !dat->is_type_b (Full_storage::static_name())) 
28     {
29
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;
34     }
35 }
36
37 Matrix_storage*
38 Matrix_storage::get_product_result (Matrix_storage*left, Matrix_storage*right)
39 {
40   Matrix_storage* dest =0;
41   set_product_result (dest, left,right);
42   return dest;
43 }
44   
45
46 /*
47   hairy
48  */
49 void
50 Matrix_storage::set_product_result (Matrix_storage*&dest, 
51                            Matrix_storage*left, Matrix_storage*right)
52 {
53   if ( left->name() == Diagonal_storage::static_name () 
54          && right->name() == Diagonal_storage::static_name ()) 
55            {
56         Diagonal_storage *L = (Diagonal_storage*)left;
57         Diagonal_storage* R = (Diagonal_storage*) right;
58
59         if  (L->band_size_i() + R->band_size_i () < L->dim ()/2) 
60           {
61             if (dest ->name() != Diagonal_storage::static_name ())
62               {
63                 delete dest;
64                 dest = new Diagonal_storage;
65               }
66          
67             dest->set_size (L->dim());
68             return;
69           }
70     }
71
72   if ( dest && dest->name() == Full_storage::static_name ()) 
73     {
74         dest->set_size (left->rows(), right->cols ());
75     }
76   else 
77     {
78         delete dest;
79         dest = new Full_storage (left->rows(), right->cols ());
80     }
81 }
82
83 IMPLEMENT_IS_TYPE_B(Matrix_storage);
84
85 Matrix_storage *
86 Matrix_storage::get_full (int n, int m)
87 {
88   return new Full_storage (n,m);
89 }
90
91
92
93  bool
94 Matrix_storage::try_right_multiply (Matrix_storage *, 
95                                    const Matrix_storage *) const
96 {
97   return false;
98 }
99
100 Array<Real>
101 Matrix_storage::row (int n) const
102 {
103   Array<Real> r;
104   for (int j = 0; j < cols(); j++)
105         r.push (elem (n,j));
106   return r;
107 }
108
109 Array<Real>
110 Matrix_storage::column (int n) const
111 {
112   Array<Real> r;
113   for (int i = 0; i < rows(); i++)
114         r.push (elem (i,n));
115   return r;
116 }
117
118 void
119 Matrix_storage::set_size (int rows, int cols)
120 {
121       
122   resize (rows,cols);
123 }
124         
125 void
126 Matrix_storage::set_size (int rows)
127 {
128       
129         resize (rows);
130 }
131         
132
133 void
134 Matrix_storage::set_band (Matrix_storage *&mat, int b)
135 {
136   Matrix_storage* ns = new Diagonal_storage (mat, b);
137   delete mat;
138   mat=ns;
139 }
140         
141
142 void
143 Matrix_storage::set_full (Matrix_storage *&mat)
144 {
145   Matrix_storage* ns = new Full_storage (mat);
146   delete mat;
147   mat=ns;
148 }