xc
TMatrix.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 //TMatrix.h
22 #ifndef TMATRIX_H
23 #define TMATRIX_H
24 
25 #include <iostream>
26 #include "ProtoMatrix.h"
27 #include "BoxConstRef.h"
28 #include "RowConstRef.h"
29 #include "ColumnConstRef.h"
30 #include <list>
31 #include <algorithm>
32 #include <sstream>
33 
34 //T Elementos a almacenar.
35 //STO Objeto para almacenamiento.
36 template <class T, class STO>
37 class TMatrix: public ProtoMatrix, public STO
38  {
39  protected:
40  inline virtual size_t Indice(const size_t &iRow,const size_t &iCol) const
41  { return iRow*n_columns-n_columns+iCol-1; }
42  bool equal_to(const TMatrix<T,STO> &m2) const;
43  TMatrix(const TMatrix<T,STO> &orig,size_t f1, size_t c1, size_t f2, size_t c2);
44  public:
45  typedef std::list<T> lst_T;
46  typedef typename STO::iterator iterator;
47  typedef typename STO::const_iterator const_iterator;
48  typedef typename STO::value_type value_type;
49  typedef typename STO::reference reference;
50  typedef typename STO::const_reference const_reference;
54 
55  TMatrix(size_t rows= 1,size_t n_columns= 1);
56  TMatrix(size_t n_rows,size_t n_columns,T val);
57  template <class InputIterator>
58  TMatrix(const size_t &n_rows,const size_t &n_columns,InputIterator b,InputIterator e);
59  TMatrix(const TMatrix<T,STO> &);
61  TMatrix<T,STO>& operator=(const T &n)
62  { return Con(n); }
63  virtual bool operator==(const TMatrix<T,STO> &) const;
64  void resize(size_t n_rows,size_t n_columns,T val);
65  inline size_t size(void) const
66  { return STO::size(); }
67  inline const STO &getVector(void) const
68  { return static_cast<const STO &>(*this); }
69 
70  inline const_reference front() const
71  { return STO::front(); }
72  inline reference front()
73  { return STO::front(); }
74  inline const_reference back() const
75  { return STO::back(); }
76  inline reference back()
77  { return STO::back(); }
78  inline const_iterator begin() const
79  { return STO::begin(); }
80  inline iterator begin()
81  { return STO::begin(); }
82  inline const_iterator end() const
83  { return STO::end(); }
84  inline iterator end()
85  { return STO::end(); }
86  TMatrix<T,STO>& Con(const T &t);
87  virtual reference operator()(size_t iRow=1,size_t col=1)
88  { return STO::operator[](Indice(iRow,col)); }
89  virtual const_reference operator()(size_t iRow=1,size_t col=1) const
90  { return STO::operator[](Indice(iRow,col)); }
91  virtual reference at(size_t iRow=1,size_t col=1)
92  { return STO::at(Indice(iRow,col)); }
93  virtual const_reference at(size_t iRow=1,size_t col=1) const
94  { return STO::at(Indice(iRow,col)); }
96  void assign(size_t row,size_t col,const T &value)
97  { STO::assign(Indice(row,col),value); }
98  void swap(size_t f1,size_t c1,size_t f2, size_t c2)
99  { std::swap((*this)(f1,c1),(*this)(f2,c2)); }
100  TMatrix<T,STO> &Trn(void)
101  {
102  TMatrix<T,STO> temp= GetTrn();
103  (*this)= temp;
104  return *this;
105  }
106  TMatrix<T,STO> GetTrn(void) const;
107  TMatrix<T,STO> getBox(size_t f1, size_t c1, size_t f2, size_t c2) const
108  { return TMatrix<T,STO>(*this,f1,c1,f2,c2); }
109  box_const_ref GetBoxConstRef(size_t f1, size_t c1, size_t f2, size_t c2) const
110  { return box_const_ref(*this,f1,c1,f2,c2); }
111  box_const_ref GetBoxConstRef(const RangoIndice &row_range,const RangoIndice &column_range) const
112  { return box_const_ref(*this,row_range,column_range); }
113  box_const_ref GetBoxConstRef(size_t f=1, size_t c=1) const
114  { return box_const_ref(*this,f,c); }
115  TMatrix<T,STO> getRow(size_t iRow) const
116  { return getBox(iRow,1,iRow,n_columns); }
117  row_const_ref getRowConstRef(size_t f, size_t c1, size_t c2) const
118  { return row_const_ref(*this,f,c1,c2); }
119  row_const_ref getRowConstRef(size_t f, const RangoIndice &column_range) const
120  { return row_const_ref(*this,f,column_range); }
121  row_const_ref getRowConstRef(size_t f=1, size_t c=1) const
122  { return row_const_ref(*this,f,c); }
123  const_ref_col getColumnConstRef(size_t c, size_t f1, size_t f2) const
124  { return const_ref_col(*this,c,f1,f2); }
125  const_ref_col getColumnConstRef(const RangoIndice &row_range,size_t c) const
126  { return const_ref_col(*this,row_range,c); }
127  const_ref_col getColumnConstRef(size_t c=1, size_t f=1) const
128  { return const_ref_col(*this,c,f); }
129  TMatrix<T,STO> getColumn(size_t col) const
130  { return getBox(1,col,n_rows,col); }
131  TMatrix<T,STO> GetMenor(size_t f,size_t c) const;
132  void putBox(size_t f,size_t c,const TMatrix<T,STO> &);
133  void putRow(size_t iRow,const TMatrix<T,STO> &f)
134  {
135  if (!compareColumnNumber(*this,f)) return;
136  putBox(iRow,1,f);
137  }
138  void PutCol(size_t col,const TMatrix<T,STO> &c)
139  {
140  if (!compareRowNumber(*this,c)) return;
141  putBox(1,col,c);
142  }
143  void OrlaCol(const TMatrix<T,STO> &c)
144  {
145  TMatrix<T,STO> m(n_rows,n_columns+1);
146  m.putBox(1,1,*this);
147  m.PutCol(n_columns+1,c);
148  *this= m;
149  }
150  void decorateRow(const TMatrix<T,STO> &f)
151  {
152  TMatrix<T,STO> m(n_rows+1,n_columns);
153  m.putBox(1,1,*this);
154  m.putRow(n_rows+1,f);
155  *this= m;
156  }
157  void swapRows(size_t f1,size_t f2);
158  void swapColumns(size_t c1,size_t c2);
159  virtual void Print(std::ostream &) const;
160  virtual void Input(std::istream &);
161  virtual void Input(const std::string &);
162  virtual ~TMatrix(void) {}
163  };
164 
166 template <class T,class STO>
167 TMatrix<T,STO>::TMatrix(size_t n_rows,size_t n_columns)
168  : ProtoMatrix(n_rows,n_columns), STO(Tam()) {}
170 template <class T,class STO>
171 TMatrix<T,STO>::TMatrix(size_t n_rows,size_t n_columns,T val)
172  : ProtoMatrix(n_rows,n_columns), STO(Tam(),val) {}
174 template <class T,class STO>
176  : ProtoMatrix(other),STO(other) {}
177 
179 template <class T,class STO> template<class InputIterator>
180 TMatrix<T,STO>::TMatrix(const size_t &n_rows,const size_t &n_columns,InputIterator b,InputIterator e)
181  : ProtoMatrix(n_rows,n_columns), STO(b,e)
182  {
183  if(STO::size()!=(n_rows*n_columns))
184  std::cerr << "TMatrix; the number of elements of the list"
185  << " is not equal to the number of elements of the"
186  << " matrix." << std::endl;
187  }
188 
189 
191 template <class T,class STO>
193  {
194  ProtoMatrix::operator=(m);
195  STO::operator=(m);
196  return *this;
197  }
198 
200 template <class T,class STO>
201 void TMatrix<T,STO>::resize(size_t n_rows,size_t n_columns,T val)
202  {
203  ProtoMatrix::resize(n_rows,n_columns);
204  STO::resize(Tam());
205  }
206 
208 template <class T,class STO>
210  {
211  bool retval= false;
212  if(this==&other)
213  retval= true;
214  else
215  {
216  retval= ProtoMatrix::operator==(other);
217  if(retval)
218  retval= this->equal_to(other);
219  }
220  return retval;
221  }
222 
223 template <class MATR>
224 MATR GetMenor(const MATR &matrix,const size_t &f,const size_t &c)
225  {
226  const size_t fl= matrix.getNumberOfRows();
227  const size_t cl= matrix.getNumberOfColumns();
228  MATR menor(fl-1,cl-1);
229  size_t m,m1,p,p1;
230  for(m= m1= 1; m<=fl;m++)
231  {
232  if (m == f) continue;
233  for(p= p1= 1;p<=cl;p++)
234  {
235  if (p == c) continue;
236  menor(m1,p1)= matrix(m,p);
237  p1++;
238  }
239  m1++;
240  }
241  return menor;
242  }
243 
246 template <class T,class STO>
247 TMatrix<T,STO> TMatrix<T,STO>::GetMenor(size_t f,size_t c) const
248  { return ::GetMenor(*this,f,c); }
249 
251 template <class T,class STO>
252 void TMatrix<T,STO>::putBox(size_t f,size_t c,const TMatrix<T,STO> &box)
253  {
254  check_put_box(f,c,box);
255  for(size_t i=1;i<=box.n_rows;i++)
256  for(size_t j=1;j<=box.n_columns;j++)
257  TMatrix<T,STO>::operator()(i+f-1,j+c-1)= box(i,j);
258  }
259 
261 template <class T,class STO>
262 std::ostream &operator<<(std::ostream &os,const TMatrix<T,STO> &m)
263  {
264  m.Print(os);
265  return os;
266  }
267 
269 template <class T,class STO>
270 void TMatrix<T,STO>::Input(std::istream &is)
271  {
272  std::cerr << "TMatrix<T,STO>::" << __FUNCTION__
273  << " not implemented." << std::endl;
274  return;
275  }
276 
278 template <class T,class STO>
279 void TMatrix<T,STO>::Input(const std::string &str)
280  {
281  std::istringstream iss(str);
282  Input(iss);
283  }
284 
285 template <class T,class STO>
286 std::istream &operator >> (std::istream &is,TMatrix<T,STO> &m)
287  {
288  m.Input(is);
289  return is;
290  }
291 
292 template <class M>
293 inline M traspuesta(const M &m)
294  { return m.GetTrn(); }
295 
296 template<class T,class STO>
297 TMatrix<T,STO>::TMatrix(const TMatrix<T,STO> &orig,size_t f1, size_t c1, size_t f2, size_t c2): ProtoMatrix(f2-f1+1,c2-c1+1), STO(Tam())
298  {
299  orig.check_get_box(f1,c1,f2,c2);
300  for(size_t i=1;i<=n_rows;i++)
301  for(size_t j=1;j<=n_columns;j++)
302  (*this)(i,j)= orig(i+f1-1,j+c1-1);
303  }
304 
305 template<class T,class STO>
307  {
308  for(size_t i=1;i<=n_rows;i++)
309  for(size_t j=1;j<=n_columns;j++)
310  (*this)(i,j)= t;
311  return *this;
312  }
313 
314 template<class T,class STO>
316  {
317  TMatrix<T,STO> retval(n_columns,n_rows);
318  for(size_t i=1;i<=n_rows;i++)
319  for(size_t j=1;j<=n_columns;j++)
320  retval(j,i)= (*this)(i,j);
321  return retval;
322  }
323 template<class T,class STO>
324 void TMatrix<T,STO>::swapRows(size_t f1,size_t f2)
325  { for(size_t c=1;c<=n_columns;c++) swap(f1,c,f2,c); }
326 template<class T,class STO>
327 void TMatrix<T,STO>::swapColumns(size_t c1,size_t c2)
328  { for(size_t f=1;f<=n_rows;f++) swap(f,c1,f,c2); }
329 template<class T,class STO>
330 void TMatrix<T,STO>::Print(std::ostream &os) const
331  {
332  os << '[';
333  const size_t n_rows= getNumberOfRows(),n_columns= getNumberOfColumns();
334  for(size_t i= 1;i<=n_rows;i++)
335  {
336  os << '[';
337  if(n_columns > 0) os << (*this)(i,1);
338  for(size_t j= 2;j<=n_columns;j++)
339  os << ',' << (*this)(i,j);
340  os << ']';
341  }
342  os << ']';
343  }
344 template<class T,class STO>
345 bool TMatrix<T,STO>::equal_to(const TMatrix<T,STO> &m2) const
346  {
347  if (!CompDim(*this,m2)) return false;
348  for(size_t i=1;i<=n_rows;i++)
349  for(size_t j=1;j<=n_columns;j++)
350  if ((*this)(i,j) != m2(i,j)) return false;
351  return true;
352  }
353 
360 template <class MATRV,class MATRI>
361 MATRV ExtraeValores(const MATRV &matrix,const MATRI &row_indexes,const size_t &icol= 1)
362  {
363  MATRV retval(row_indexes.getNumberOfRows(),1);
364  for(size_t iRow=1;iRow<=retval.getNumberOfRows();iRow++)
365  retval(iRow,1)= matrix(row_indexes(iRow),icol);
366  return retval;
367  }
368 
369 #endif
370 
Reference to a matrix box.
Definition: BoxConstRef.h:47
Definition: ProtoMatrix.h:32
TMatrix< T, STO > & operator=(const TMatrix< T, STO > &m)
Assignment operator.
Definition: TMatrix.h:192
Constant reference to a column of a matrix.
Definition: ColumnConstRef.h:30
virtual bool operator==(const TMatrix< T, STO > &) const
Comparison operator.
Definition: TMatrix.h:209
void assign(size_t row, size_t col, const T &value)
Put the box int the position (f,c) of this matrix.
Definition: TMatrix.h:96
TMatrix< T, STO > GetMenor(size_t f, size_t c) const
Return the minor of the matrix that corresponds to the row and the column arguments.
Definition: TMatrix.h:247
virtual void Input(std::istream &)
Lectura desde istream.
Definition: TMatrix.h:270
Definition: TMatrix.h:37
Reference to a matrix row.
Definition: RowConstRef.h:30
void putBox(size_t f, size_t c, const TMatrix< T, STO > &)
Put the box int the position (f,c) of this matrix.
Definition: TMatrix.h:252
void resize(size_t n_rows, size_t n_columns, T val)
Assignment operator.
Definition: TMatrix.h:201
Rango de variación de un índice, se emplea en BoxConstRef.
Definition: RangoIndice.h:30