xc
ProtoMatrix.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 //ProtoMatrix.h
22 #ifndef PROTOMATRIX_H
23 #define PROTOMATRIX_H
24 
25 #include <string>
26 #include <iostream>
27 
33  {
34  protected:
35  size_t n_rows; //number of rows.
36  size_t n_columns; //number of columns.
37  inline void PutDim(size_t nRows,size_t nCols)
38  {
39  n_rows= nRows;
40  n_columns= nCols;
41  }
42  inline void inic(size_t n_rows,size_t n_columns)
43  { PutDim(n_rows,n_columns); }
44  inline virtual bool check_range(const size_t &iRow,const size_t &col) const
45  { return ((iRow<=n_rows) && (col<=n_columns)); }
46  inline void check_put_box(size_t f,size_t c,const ProtoMatrix &box) const
47  {
48  if(((f+box.getNumberOfRows())>(n_rows+1)) || ((c+box.getNumberOfColumns())>(n_columns+1)))
49  std::cerr << __FUNCTION__
50  << "; indices out of range." << std::endl;
51  }
52  inline void check_get_box(size_t f1, size_t c1, size_t f2, size_t c2) const
53  {
54  if ( (f2 < f1) || (c2 < c1) )
55  std::cerr << "Erroneous indexes in "
56  << __FUNCTION__ << " function." << std::endl;
57  }
58  inline void check_sto_sum(const ProtoMatrix &m) const
59  {
60  if (!CompDim(*this,m))
61  std::cerr << "Matrices de dimensiones distintas en operador += " << std::endl
62  << " this: " << " (" << n_rows << 'x' << n_columns << ')' << std::endl
63  << " m= " << " (" << m.n_rows << 'x' << m.n_columns << ')' << std::endl;
64  }
65  inline void check_sto_dif(const ProtoMatrix &m) const
66  {
67  if (!CompDim(*this,m))
68  std::cerr << "Matrices de dimensiones distintas en operador -=" << std::endl
69  << " this: " << " (" << n_rows << 'x' << n_columns << ')' << std::endl
70  << " m= " << " (" << m.n_rows << 'x' << m.n_columns << ')' << std::endl;
71  }
72  inline void check_traza(void) const
73  {
74  if(!Cuadrada())
75  std::cerr << "Not a square matrix." << std::endl;
76  }
77  inline friend int check_dot(const ProtoMatrix &v1,const ProtoMatrix &v2)
78  {
79  if (!v1.isRow())
80  std::cerr << "First matrix of scalar product is not a row matrix."
81  << std::endl;
82  if (!v2.isColumn())
83  std::cerr << "Second matrix of scalar product is not a column matrix."
84  << std::endl;
85  if (v1.n_columns != v2.n_rows)
86  std::cerr << "Matrices de dimensiones incompatibles en producto escalar."
87  << " m1= " << v1 << " (" << v1.n_rows << 'x' << v1.n_columns << ')' << std::endl
88  << " m2= " << v2 << " (" << v2.n_rows << 'x' << v2.n_columns << ')'
89  << std::endl;
90  return 1;
91  }
92  inline friend int check_sum(const ProtoMatrix &m1,const ProtoMatrix &m2)
93  {
94  if (!CompDim(m1,m2))
95  {
96  std::cerr << "Matrices de dimensiones distintas en operador +"
97  /* << m1.GetStrDim() << ' ' << m2.GetStrDim() */ << std::endl;
98  return 0;
99  }
100  return 1;
101  }
102  inline friend int check_dif(const ProtoMatrix &m1,const ProtoMatrix &m2)
103  {
104  if (!CompDim(m1,m2))
105  {
106  std::cerr << "Matrices de dimensiones distintas en operador -"
107  /* << m1.GetStrDim() << ' ' << m2.GetStrDim() */ << std::endl;
108  return 0;
109  }
110  return 1;
111  }
112  inline friend int check_prod(const ProtoMatrix &m1,const ProtoMatrix &m2)
113  {
114  if (m1.n_columns != m2.n_rows)
115  {
116  std::cerr << "Matrices de dimensiones incompatibles en producto." << std::endl;
117  std::cerr << " m1= " << m1 << std::endl;
118  std::cerr << " m2= " << m2 << std::endl;
119  return 0;
120  }
121  return 1;
122  }
123  public:
124  ProtoMatrix(size_t n_rows= 1,size_t n_columns= 1)
125  { inic(n_rows,n_columns); }
126  ProtoMatrix(const ProtoMatrix &other)
127  { inic(other.n_rows,other.n_columns); }
128  ProtoMatrix& operator =(const ProtoMatrix &m)
129  {
130  inic(m.n_rows,m.n_columns);
131  return *this;
132  }
133  virtual bool operator==(const ProtoMatrix &other) const
134  {
135  bool retval= false;
136  if(this==&other)
137  retval= true;
138  else
139  {
140  retval= (this->n_rows==other.n_rows) && (this->n_columns==other.n_columns);
141  }
142  return retval;
143  }
144  virtual ~ProtoMatrix(void) {}
145  inline virtual void resize(size_t n_rows,size_t n_columns)
146  { inic(n_rows,n_columns); }
147  inline virtual size_t Tam(void)
148  { return (n_rows*n_columns); }
149  inline size_t getNumberOfRows(void) const
150  { return n_rows; }
151  inline size_t getNumberOfColumns(void) const
152  { return n_columns; }
153  inline bool CheckIndices(const size_t &f,const size_t &c) const
154  { return check_range(f,c);; }
156  //" of the matrix "interior".
157  inline bool interior(const size_t &i,const size_t &j) const
158  { return ( (i>1) && (j>1) && (i<n_rows) && (j<n_columns) ); }
159  inline int Cuadrada(void) const
160  { return (n_rows == n_columns); }
161  inline bool isRow(void) const
162  { return (n_rows == 1); }
163  inline bool isColumn(void) const
164  { return (n_columns == 1); }
165  virtual void Print(std::ostream &os) const=0;
166  friend inline bool compareRowNumber(const ProtoMatrix &m1,const ProtoMatrix &m2)
167  { return (m1.n_rows == m2.n_rows); }
168  friend inline bool compareColumnNumber(const ProtoMatrix &m1,const ProtoMatrix &m2)
169  { return (m1.n_columns == m2.n_columns); }
170  friend inline bool CompDim(const ProtoMatrix &m1,const ProtoMatrix &m2)
171  { return (compareRowNumber(m1,m2) && compareColumnNumber(m1,m2)); }
172  friend inline std::ostream &operator<<(std::ostream &os,const ProtoMatrix &m)
173  {
174  m.Print(os);
175  return os;
176  }
177  };
178 
179 #endif
Definition: ProtoMatrix.h:32
bool interior(const size_t &i, const size_t &j) const
Return true if the indices correspond to a component.
Definition: ProtoMatrix.h:157