finitediff
SNgaussian.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 __SNgaussian_H__174236
20 #define __SNgaussian_H__174236
21 
22 #include <array>
23 
24 #include "SNgeneric.h"
25 #include "m_num.h"
26 #include "../exceptions/SNexceptions.cpp"
27 #include "../Utilities.h"
28 
29 
30 // forward definition
31 template <class T,unsigned int tp_size>
32 class SNgeneric;
33 //template <class T,unsigned int tp_size>
34 //class SNmatrix;
35 //template <class T,unsigned int tp_size>
36 //class SNmultiGaussian;
37 template <class T>
39 
40 
41 // THE CLASS HEADER -----------------------------------------
42 
60 template <class T,unsigned int tp_size>
61 class SNgaussian : public SNgeneric<T,tp_size>
62 {
63 
64  private:
65  std::array<T,tp_size> data; // see implementation of "_at"
67 
85  SpecialValue<T> checkForSpecialElements(const m_num& i,const m_num& j) const;
86 
87  //** populate the matrix from the elements of the given matrix */
88  template <class U,unsigned int s>
89  void populate_from(const SNgeneric<U,s>&);
90 
92  SNgaussian(const std::array<T,tp_size>& d, const m_num& c);
93 
94  T _get(m_num,m_num) const override;
95  T& _at(m_num,m_num) override;
96  public :
104  SNgaussian();
105 
119  template <class U,unsigned int s>
120  SNgaussian(const SNgeneric<U,s>& A, const m_num& c=0);
121 
126  void setColumn(const m_num& col);
127  m_num getColumn() const;
128 };
129 
130 
131 // GETTER/SETTER ---------------------------------------
132 
133 template <class T,unsigned int tp_size>
135 {
136  if (col>tp_size-1)
137  {
138  throw OutOfRangeColumnNumber("The specified column number is larger than the size of the matrix.");
139  }
140  data_column=col;
141 }
142 
143 template <class T,unsigned int tp_size>
145 {
146  return data_column;
147 }
148 
149 // CONSTRUCTOR ---------------------------------------
150 
151 template <class T,unsigned int tp_size>
152 template <class U, unsigned int s>
154 {
155  m_num column=getColumn();
156  if (s!=tp_size)
157  {
158  throw IncompatibleMatrixSizeException(tp_size,s);
159  }
160 
161  T m = A.get(column,column);
162  for (m_num i=column+1;i<tp_size;i++)
163  {
164  this->at(i,column)=-A.get(i,column)/m;
165  }
166 }
167 
168 // from a generic
169 
170 template <class T,unsigned int tp_size>
171 template<class U,unsigned int s>
173  data_column(c)
174 {
175  populate_from(A);
176 }
177 
178 // from the array
179 
180 template <class T,unsigned int tp_size>
181 SNgaussian<T,tp_size>::SNgaussian(const std::array<T,tp_size>& d, const m_num& c):
182  data(d),
183  data_column(c)
184 {}
185 
186 // from nothing (the we leave a trash in 'data_column')
187 
188 template <class T,unsigned int tp_size>
190  data_column(tp_size+1)
191 {}
192 
193 // UTILITIES ---------------------------------------
194 
195 template <class T,unsigned int tp_size>
197 {
198  if (i==j)
199  {
200  return SpecialValue<T>(1,true);
201  }
202  if (j!=getColumn())
203  {
204  return SpecialValue<T>(0,true);
205  }
206  if (i<j)
207  {
208  return SpecialValue<T>(0,true);
209  }
210  return SpecialValue<T>(0,false);
211 }
212 
213 // _AT AND _GET METHODS ---------------------------------------
214 
215 template <class T,unsigned int tp_size>
217 {
219  if (sv.special)
220  {
221  return sv.value;
222  }
223  return data.at(i-getColumn()-1); //if you change here, you have to change _at
224 }
225 
226 template <class T,unsigned int tp_size>
228 
229 
241 {
242  if (data_column==tp_size+1)
243  {
244  throw NotInitializedMemberException("You are trying to populate a 'SNgaussian' before to initialize the member 'data_column'. Use setColumn().");
245  }
246 
248  if (sv.special)
249  {
250  throw SNchangeNotAllowedException(i,j);
251  }
252  return data.at(i-getColumn()-1); //if you change here, you have to change _get
253 }
254 
255 // MATHEMATICS ---------------------------------------
256 
257 
258 template <class T,unsigned int tp_size>
260 {
261  std::array<T,tp_size> new_data(data);
262  for (unsigned int k=0;k<tp_size-getColumn()-1;++k)
263  {
264  new_data.at(k)=-new_data.at(k);
265  }
266  return SNgaussian(new_data,getColumn());
267 }
268 
269 #endif
bool special
Definition: SNgeneric.h:130
virtual T & at(const m_num, const m_num) final
Definition: SNgeneric.h:218
Definition: SNgaussian.h:61
When trying to perform operation with matrices with incompatible sizes.
Definition: SNexceptions.cpp:43
This is the base class for the other matrices types.
Definition: MathUtilities.h:32
SpecialValue< T > checkForSpecialElements(const m_num &i, const m_num &j) const
Definition: SNgaussian.h:196
Definition: m_num.h:34
SNgaussian()
Definition: SNgaussian.h:189
std::array< T, tp_size > data
Definition: SNgaussian.h:65
virtual T get(const m_num, const m_num) const final
Definition: SNgeneric.h:211
Definition: SNgaussian.h:38
void populate_from(const SNgeneric< U, s > &)
Definition: SNgaussian.h:153
m_num getColumn() const
Definition: SNgaussian.h:144
T value
Definition: SNgeneric.h:129
SNgaussian< T, tp_size > inverse() const
Definition: SNgaussian.h:259
Definition: SNexceptions.cpp:119
Definition: SNexceptions.cpp:104
T & _at(m_num, m_num) override
Definition: SNgaussian.h:227
void setColumn(const m_num &col)
Definition: SNgaussian.h:134
T _get(m_num, m_num) const override
Definition: SNgaussian.h:216
m_num data_column
Definition: SNgaussian.h:66
Definition: SNexceptions.cpp:81