xc
matrix_by_boxes.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. PĂ©rez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //matrix_by_boxes.h
22 
23 #ifndef MATRIX_BY_BOXES_H
24 #define MATRIX_BY_BOXES_H
25 
26 #include "ZMatrix.h"
27 
30 template <class TBOX>
31 class MatrixByBoxes: public ZMatrix<TBOX>
32  {
33  protected:
34  size_t get_rows_row(const size_t &f) const;
35  size_t get_columns_column(const size_t f) const;
36  std::vector<size_t> get_dim_rows(void) const;
37  std::vector<size_t> get_dim_columns(void) const;
38 
39  public:
40  typedef ZMatrix<TBOX> box_matrix;
41  typedef typename box_matrix::size_type size_type;
42  typedef typename TBOX::value_type value_type;
43  typedef typename box_matrix::iterator iterator;
44  typedef typename box_matrix::const_iterator const_iterator;
45 
46  MatrixByBoxes(void);
47  MatrixByBoxes(size_type n_rows,size_type n_columns);
48  MatrixByBoxes(size_type n_rows,size_type n_columns,const TBOX &val);
49  MatrixByBoxes(const std::vector<size_t> &);
50  MatrixByBoxes(const std::vector<size_t> &,const std::vector<size_t> &);
53 
54  size_t getTotalNumberOfRows(void) const;
55  size_t getTotalNumberOfColumns(void) const;
56  void Con(const value_type &d);
57  MatrixByBoxes<TBOX> &Trn(void);
58  MatrixByBoxes<TBOX> GetTrn(void) const;
59  TBOX QuitaBloques(void);
60  MatrixByBoxes<TBOX> &operator*=(const TBOX &m);
61  MatrixByBoxes<TBOX> &operator*=(const value_type &d);
63  };
64 
66 template <class TBOX>
68  : box_matrix(1,1) {}
69 
71 template <class TBOX>
72 MatrixByBoxes<TBOX>::MatrixByBoxes(size_type n_rows,size_type n_columns)
73  : box_matrix(n_rows,n_columns) {}
74 
76 template <class TBOX>
77 MatrixByBoxes<TBOX>::MatrixByBoxes(size_type n_rows,size_type n_columns,const TBOX &val)
78  : box_matrix(n_rows,n_columns,val) {}
79 
83 template <class TBOX>
84 MatrixByBoxes<TBOX>::MatrixByBoxes(const std::vector<size_t> &dim)
85  : box_matrix(dim.size(),dim.size())
86  {
87  for(size_t i=1;i<=this->n_rows;i++)
88  for(size_t j=1;j<=this->n_columns;j++)
89  (*this)(i,j)= m_double(dim[i-1],dim[j-1]);
90  }
91 
96 template <class TBOX>
97 MatrixByBoxes<TBOX>::MatrixByBoxes(const std::vector<size_t> &dim_rows,const std::vector<size_t> &dim_columns)
98  : box_matrix(dim_rows.size(),dim_columns.size())
99  {
100  for(size_t i=1;i<=this->n_rows;i++)
101  for(size_t j=1;j<=this->n_columns;j++)
102  (*this)(i,j)= m_double(dim_rows[i-1],dim_columns[j-1]);
103  }
104 
106 template <class TBOX>
108  : box_matrix(other) {}
109 
111 template <class TBOX>
113  {
114  box_matrix::operator=(m);
115  return *this;
116  }
117 
118 
120 template <class TBOX>
121 size_t MatrixByBoxes<TBOX>::get_rows_row(const size_t &iRow) const
122  {
123  size_t retval= 0;
124  const size_t ncls= this->getNumberOfColumns();
125  for(size_t j=1;j<=ncls;j++)
126  {
127  const size_t nrows_j= (*this)(iRow,j).getNumberOfRows();
128  if(nrows_j>retval) retval= nrows_j;
129  }
130  return retval;
131  }
132 
134 template <class TBOX>
135 size_t MatrixByBoxes<TBOX>::get_columns_column(const size_t c) const
136  {
137  size_t retval= 0;
138  const size_t nrows= this->getNumberOfRows();
139  for(size_t i=1;i<=nrows;i++)
140  {
141  const size_t ncls_i= (*this)(i,c).getNumberOfRows();
142  if(ncls_i>retval) retval= ncls_i;
143  }
144  return retval;
145  }
146 
148 template <class TBOX>
149 std::vector<size_t> MatrixByBoxes<TBOX>::get_dim_rows(void) const
150  {
151  const size_t nrows= this->getNumberOfRows();
152  std::vector<size_t> retval(nrows,0);
153  for(size_t i=1;i<=nrows;i++)
154  retval[i-1]= get_rows_row(i);
155  return retval;
156  }
158 template <class TBOX>
159 std::vector<size_t> MatrixByBoxes<TBOX>::get_dim_columns(void) const
160  {
161  const size_t ncls= this->getNumberOfColumns();
162  std::vector<size_t> retval(ncls,0);
163  for(size_t j=1;j<=ncls;j++)
164  retval[j-1]= get_columns_column(j);
165  return retval;
166  }
168 template <class TBOX>
170  {
171  std::vector<size_t> dim_rows= get_dim_rows();
172  size_t tot_rows= dim_rows[0];
173  for(size_t i=1;i<this->n_rows;i++)
174  tot_rows+= dim_rows[i];
175  return tot_rows;
176  }
178 template <class TBOX>
180  {
181  std::vector<size_t> dim_columns= get_dim_columns();
182  size_t tot_n_columns= dim_columns[0];
183  for(size_t j=1;j<=this->n_columns;j++)
184  tot_n_columns+= dim_columns[j];
185  return tot_n_columns;
186  }
187 
189 template <class TBOX>
190 void MatrixByBoxes<TBOX>::Con(const value_type &d)
191  {
192  for(iterator i= this->begin();i!=this->end();i++)
193  (*i).Con(d);
194  }
196 template <class TBOX>
198  {
199  box_matrix::Trn();
200  for(iterator i= this->begin();i!=this->end();i++)
201  (*i).Trn();
202  return *this;
203  }
205 template <class TBOX>
207  {
208  MatrixByBoxes<TBOX> m= *this;
209  m.Trn();
210  return m;
211  }
212 
216 template <class TBOX>
218  {
220  size_t f= 1,c= 1;
221  TBOX box;
222  for(size_t i= 1;i<=this->getNumberOfRows();i++)
223  {
224  for(size_t j= 1;j<=this->getNumberOfColumns();j++)
225  {
226  box= (*this)(i,j);
227  retval.putBox(f,c,box);
228  c+= box.getNumberOfColumns();
229  }
230  f+= box.getNumberOfRows();
231  c= 1;
232  }
233  return retval;
234  }
236 template <class TBOX>
238  {
239  this->Prod(m);
240  return *this;
241  }
243 template <class TBOX>
245  {
246  size_type i,sz= this->size();
247  for(i= 0;i<sz;i++)
248  (*this)[i]*=d;
249  return *this;
250  }
252 template <class TBOX>
254  {
255  box_matrix::operator*=(m);
256  return *this;
257  }
258 
259 #endif
MatrixByBoxes(void)
Constructor.
Definition: matrix_by_boxes.h:67
MatrixByBoxes< TBOX > GetTrn(void) const
Return transposed.
Definition: matrix_by_boxes.h:206
Matrix which element type has estructura de anillo respecto a las operaciones + y *...
Definition: ZMatrix.h:37
std::vector< size_t > get_dim_columns(void) const
Return el number of rows of each column.
Definition: matrix_by_boxes.h:159
size_t getTotalNumberOfColumns(void) const
Return the sum of the columns of boxes.
Definition: matrix_by_boxes.h:179
Matrix made of boxes.
Definition: matrix_by_boxes.h:31
TBOX QuitaBloques(void)
Return the matrix that has the same number of boxes that this has blocks.
Definition: matrix_by_boxes.h:217
MatrixByBoxes & operator=(const MatrixByBoxes &m)
Assignment operator.
Definition: matrix_by_boxes.h:112
size_t get_rows_row(const size_t &f) const
Return el number of rows of the iRow-th row matrices.
Definition: matrix_by_boxes.h:121
size_t getTotalNumberOfRows(void) const
Return the sum of the rows of boxes.
Definition: matrix_by_boxes.h:169
void Con(const value_type &d)
Assigns the argument to all the boxes.
Definition: matrix_by_boxes.h:190
MatrixByBoxes< TBOX > & Trn(void)
Transpose the matrix.
Definition: matrix_by_boxes.h:197
size_t get_columns_column(const size_t f) const
Return el number of columns de las matrices de the column c.
Definition: matrix_by_boxes.h:135
std::vector< size_t > get_dim_rows(void) const
Return el number of rows of each row.
Definition: matrix_by_boxes.h:149
MatrixByBoxes< TBOX > & operator*=(const TBOX &m)
Operador *=.
Definition: matrix_by_boxes.h:237