finitediff
SNgeneric.h
Go to the documentation of this file.
1 /*
2 Copyright 2017 Laurent Claessens
3 contact : laurent@claessens-donadello.eu
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef __SNGENERIC_H__142708_
20 #define __SNGENERIC_H__142708_
21 
22 #include <cmath>
23 #include <array>
24 #include <string>
25 
26 #include "SNgaussian.h"
27 #include "SNline.h"
28 #include "m_num.h"
29 
30 #include "MathUtilities.h"
31 #include "../Utilities.h"
32 
33 
34 // forward
35 template <class T,unsigned int tp_size>
36 class SNgaussian;
37 
70 template <class T,unsigned int tp_size>
71 class SNgeneric
72 {
73  private :
74 
76  virtual T _get(const m_num,const m_num) const=0;
77 
79  virtual T& _at(const m_num,const m_num)=0;
80 
81 
86  virtual void checkRangeCorectness(const m_num&,const m_num&) const final;
87  public:
88  virtual unsigned int getSize() const final;
89 
90  virtual T& at(const m_num,const m_num) final;
91  virtual T get(const m_num,const m_num) const final;
92 
93  virtual SNline<T,tp_size> getSNline(m_num l) const;
94 
100  template <class V,unsigned int s>
101  void subtract(const SNgeneric<V,s>&);
102  template <class V,unsigned int s>
103  void subtract(const SNgaussian<V,s>&);
104 
108  SNgaussian<T,tp_size> getGaussian(const m_num c) const;
109 
118  template <class V,unsigned int s>
119  bool isNumericallyEqual(const SNgeneric<V,s>& A,const double& epsilon) const;
120 };
121 
122 
123 // UTILITIES --------------------------
124 
125 template <class T>
126 class SpecialValue
127 {
128  public :
129  T value;
130  bool special;
131  SpecialValue(T v,bool s) :
132  value(v),
133  special(s)
134  {}
135 };
136 
137 template <class T,unsigned int tp_size>
139 {
140  if (l>tp_size or c>tp_size)
141  {
142  throw SNoutOfRangeException(l,c,getSize());
143  }
144 }
145 
146 template <class T,unsigned int tp_size>
148 {
149  std::array<T,tp_size> al;
150  for (m_num c=0;c<tp_size;c++)
151  {
152  al.at(c)=this->get(l,c);
153  }
154  return SNline<T,tp_size>(al);
155 }
156 
157 // OPERATORS ------------------------------
158 
159 template <class V,unsigned int s>
160 std::ostream& operator<<(std::ostream& stream,const SNgeneric<V,s>& snm)
161 {
162  const unsigned int tp_size=s; // homogenize the notations.
163  std::array<int,tp_size> col_size;
164 
165  // - we parse each column to know its size (max length of the elements).
166  // - the size of the columns are stored in `col_size`
167  // - we parse the matrix line by line, adding the right
168  // amount of ' ' (blanck) to fit the length of the column
169  // (+2 for aesthetics)
170 
171  for (m_num col=0;col<tp_size;++col)
172  {
173  unsigned int acc=0;
174  for (m_num line=0;line<tp_size;++line)
175  {
176  V value = snm.get(line,col);
177  unsigned int l_value = value_length(value);
178 
179  if (l_value>acc)
180  {
181  acc=l_value;
182  }
183  }
184  col_size.at(col)=acc;
185  col_size.at(col)=10;
186  }
187 
188  for (m_num l=0;l<tp_size;l++)
189  {
190  for (m_num c=0;c<tp_size;++c)
191  {
192  V value = snm.get(l,c);
193  stream<<value<<std::string(col_size.at(c)-value_length(value)+2,' ');
194  }
195  stream<<std::endl;
196  }
197  return stream;
198 }
199 
200 // GET SIZE ------------------------------
201 
202 template <class T,unsigned int tp_size>
203 unsigned int SNgeneric<T,tp_size>::getSize() const
204 {
205  return tp_size;
206 }
207 
208 // GET AND AT METHODS ------------------------------
209 
210 template <class T,unsigned int tp_size>
211 T SNgeneric<T,tp_size>::get(const m_num i,const m_num j) const
212 {
214  return _get(i,j);
215 }
216 
217 template <class T,unsigned int tp_size>
219 {
221  return _at(i,j);
222 }
223 
224 
225 // MATHEMATICAL FUNCTIONALITIES ------------------------
226 
227 
228 template <class T,unsigned int tp_size>
230 {
231  return SNgaussian<T,tp_size>(*this,c);
232 }
233 
234 
235 template <class T,unsigned int tp_size>
236 template <class V,unsigned int s>
238 {
239  checkSizeCompatibility(*this,S);
240  for (m_num i=0;i<tp_size;i++)
241  {
242  for (m_num j=0;j<tp_size;j++)
243  {
244  this->at(i,j)-=S.get(i,j);
245  }
246  }
247 }
248 
249 template <class T,unsigned int tp_size>
250 template <class V,unsigned int s>
252 {
253  checkSizeCompatibility(*this,G);
254  m_num c=G.getColumn();
255 
256  // subtract the non trivial "half column"
257  for (m_num i=c+1;i<tp_size;i++)
258  {
259  this->at(i,c)-=G.get(i,c);
260  }
261  // subtract the diagonal
262  for (m_num i=0;i<tp_size;i++)
263  {
264  this->at(i,i)-=1;
265  }
266 }
267 
268 template <class T,unsigned int tp_size>
269 template <class V,unsigned int s>
270 bool SNgeneric<T,tp_size>::isNumericallyEqual(const SNgeneric<V,s>& A,const double& epsilon) const
271 {
272 
273  //tooGenericWarning("This is a very generic comparison function SNgeneric Vs SNgeneric. Cant'you be more specific ?");
274 
275  checkSizeCompatibility(*this,A);
276  T abs_diff;
277  for (m_num i=0;i<tp_size;i++)
278  {
279  for (m_num j=0;j<tp_size;j++)
280  {
281  abs_diff=std::abs( this->get(i,j)-A.get(i,j) );
282  if (abs_diff>epsilon)
283  {
284  return false;
285  }
286  }
287  }
288  return true;
289 }
290 
291 #endif
bool special
Definition: SNgeneric.h:130
void subtract(const SNgeneric< V, s > &)
Definition: SNgeneric.h:237
virtual T & at(const m_num, const m_num) final
Definition: SNgeneric.h:218
Definition: SNgaussian.h:61
void checkSizeCompatibility(const SNgeneric< U, s > &A, const SNgeneric< V, t > &B)
Definition: MathUtilities.h:35
Definition: SNline.h:40
This is the base class for the other matrices types.
Definition: MathUtilities.h:32
T get(const unsigned int i) const
Definition: SNline.h:138
Definition: m_num.h:34
SpecialValue(T v, bool s)
Definition: SNgeneric.h:131
virtual T _get(const m_num, const m_num) const =0
SNgaussian< T, tp_size > getGaussian(const m_num c) const
Definition: SNgeneric.h:229
virtual T get(const m_num, const m_num) const final
Definition: SNgeneric.h:211
Definition: SNexceptions.cpp:144
Definition: SNgaussian.h:38
m_num getColumn() const
Definition: SNgaussian.h:144
virtual SNline< T, tp_size > getSNline(m_num l) const
Definition: SNgeneric.h:147
T value
Definition: SNgeneric.h:129
virtual void checkRangeCorectness(const m_num &, const m_num &) const final
Definition: SNgeneric.h:138
virtual T & _at(const m_num, const m_num)=0
unsigned int value_length(const T &value)
Definition: Utilities.h:44
virtual unsigned int getSize() const final
Definition: SNgeneric.h:203
bool isNumericallyEqual(const SNgeneric< V, s > &A, const double &epsilon) const
Definition: SNgeneric.h:270