finitediff
SNline.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 
20 #ifndef __SNLINE_H__64621__
21 #define __SNLINE_H__64621__
22 
23 #include<array>
24 
25 #include "m_num.h"
26 
27 
28 template <class T,unsigned int tp_size>
29 class SNmatrix;
30 
31 // THE CLASS HEADER -----------------------------------------
32 
33 /*
34 * @brief This class describes a matrix line from a matrix of type 'SNmatrix'.
35 *
36 * An element contains
37 * - its line number
38 */
39 template <class T,unsigned int tp_size>
40 class SNline
41 {
42  friend class GaussTest;
43  private :
44  std::array<T,tp_size> data;
45  unsigned int line;
46  public :
47 
48  SNline(unsigned int line,SNmatrix<T,tp_size>& snmatrix);
49  SNline(const std::array<T,tp_size>&); // do not use this constructors otherwise than for testing purpose.
50 
51  template <class U,class V,unsigned int s>
52  friend bool operator==(const SNline<U,s>&,const SNline<V,s>&);
53  template <class U,unsigned int s>
54  friend SNline<U,s> operator*(const U m, const SNline<U,s>&);
55  template <class V,unsigned int s>
56  friend std::ostream& operator<<(std::ostream&, const SNline<V,s>&);
57 
62  T& at(const unsigned int i);
64  T get(const unsigned int i) const;
65 
67  unsigned int firstNonZeroColumn() const;
68 
74  void makeUnit();
75 };
76 
77 // CONSTRUCTOR, ASSIGNATION, ... -------------------------------------------
78 
79 template <class T,unsigned int tp_size>
81  line(l)
82 {
83  for (m_num c=0;c<tp_size;++c)
84  {
85  data.at(c)=snm.get(l,c);
86  }
87 }
88 
89 // the following constructor does not initialize the referenced matrix.
90 template <class T,unsigned int tp_size>
91 SNline<T,tp_size>::SNline(const std::array<T,tp_size>& ar) : data(ar) {};
92 
93 // OPERATORS -------------------------------------------
94 
95 template <class U,class V,unsigned int s>
96 bool operator==(const SNline<U,s>& A,const SNline<V,s>& B)
97 {
98  return A.data==B.data;
99 }
100 
101 // multiplication by a scalar.
102 // This is not in-place replacement.
103 template <class U,unsigned int s>
105 {
106  std::array<U,s> arr;
107  SNline<U,s> ans(arr);
108  for (unsigned int c=0;c<s;c++)
109  {
110  ans.at(c)=m*v.get(c);
111  }
112  return ans;
113 }
114 
115 template <class V,unsigned int s>
116 std::ostream& operator<<(std::ostream& stream, const SNline<V,s>& line)
117 {
118  for (unsigned int c=0;c<s;c++)
119  {
120  stream<<line.get(c)<<",";
121  }
122  return stream;
123 }
124 
125 // GETTER METHODS -------------------------------------------
126 
127 template <class T,unsigned int tp_size>
128 T& SNline<T,tp_size>::at(const unsigned int i)
129 {
130  if (i>tp_size)
131  {
132  std::cout<<"This SNline has size "<<tp_size<<". Attempt to access element "<<i<<" ."<<std::endl;
133  }
134  return data.at(i);
135 };
136 
137 template <class T,unsigned int tp_size>
138 T SNline<T,tp_size>::get(const unsigned int i) const
139 {
140  if (i>tp_size)
141  {
142  std::cout<<"This SNline has size "<<tp_size<<". Attempt to access element "<<i<<" ."<<std::endl;
143  }
144  return data.at(i);
145 };
146 
147 // OTHER FUNCTIONALITIES -------------------------------------------
148 
149 
150 template <class T,unsigned int tp_size>
152  // return the number of the first non-zero element.
153  // If they are all zero, return the size+1. This case should be treated
154  // with care because it means that the determinant is zero and the matrix
155  // non invertible.
156 {
157  for (unsigned int col=0;col<tp_size;col++)
158  {
159  if (get(col)!=0)
160  {
161  return col;
162  }
163  }
164  return tp_size+1;
165 }
166 
167 template <class T,unsigned int tp_size>
169 {
170  unsigned int col=firstNonZeroColumn();
171  if (col!=tp_size+1)
172  {
173  const T m = get(col);
174  at(col)=1; // the first one is by hand 1 (because we know it).
175  for (unsigned int c=col+1;c<tp_size;c++)
176  {
177  at(c)=get(c)/m;
178  }
179  }
180 }
181 
182 #endif
friend class GaussTest
Definition: SNline.h:42
Represent a square numerical matrix.
Definition: SNline.h:29
Definition: SNline.h:40
T & at(const unsigned int i)
Definition: SNline.h:128
T get(const unsigned int i) const
Definition: SNline.h:138
Definition: m_num.h:34
virtual T get(const m_num, const m_num) const final
Definition: SNgeneric.h:211
friend SNline< U, s > operator*(const U m, const SNline< U, s > &)
Definition: SNline.h:104
unsigned int line
Definition: SNline.h:45
void makeUnit()
Definition: SNline.h:168
SNline(unsigned int line, SNmatrix< T, tp_size > &snmatrix)
Definition: SNline.h:80
std::array< T, tp_size > data
Definition: SNline.h:44
unsigned int firstNonZeroColumn() const
Definition: SNline.h:151
friend bool operator==(const SNline< U, s > &, const SNline< V, s > &)
Definition: SNline.h:96