Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JTable2D.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JTABLE2D__
2 #define __JTOOLS__JTABLE2D__
3 
4 #include "JMath/JMath.hh"
5 #include "JMath/JZero.hh"
6 #include "JIO/JSerialisable.hh"
7 
8 
9 /**
10  * \author mdejong
11  */
12 
13 namespace JTOOLS {}
14 namespace JPP { using namespace JTOOLS; }
15 
16 namespace JTOOLS {
17 
18  using JMATH::JMath;
19  using JIO::JReader;
20  using JIO::JWriter;
21 
22 
23  /**
24  * 2D table with arithmetic capabilities.
25  */
26  template<unsigned int NX, unsigned int NY, class JData_t = double>
27  struct JTable2D :
28  public JMath< JTable2D<NX, NY, JData_t> >
29  {
30  typedef JData_t data_type;
31 
32  /**
33  * Default constructor.
34  */
36  {
37  for (int i = 0; i != NX; ++i) {
38  for (int j = 0; j != NY; ++j) {
39  data[i][j] = JMATH::zero;
40  }
41  }
42  }
43 
44 
45  /**
46  * Get number of rows.
47  *
48  * \return number of rows
49  */
50  static int getNX()
51  {
52  return NX;
53  }
54 
55 
56  /**
57  * Get number of columns.
58  *
59  * \return number of columns
60  */
61  static int getNY()
62  {
63  return NY;
64  }
65 
66 
67  /**
68  * Get row data.
69  *
70  * \param row row number
71  * \return row data
72  */
73  const data_type* operator[](int row) const
74  {
75  return data[row];
76  };
77 
78 
79  /**
80  * Get row data.
81  *
82  * \param row row number
83  * \return row data
84  */
86  {
87  return data[row];
88  };
89 
90 
91  /**
92  * Negate table.
93  *
94  * \return this table
95  */
97  {
98  for (int i = 0; i != NX; ++i) {
99  for (int j = 0; j != NY; ++j) {
100  data[i][j] = -data[i][j];
101  }
102  }
103 
104  return *this;
105  }
106 
107 
108  /**
109  * Add table.
110  *
111  * \param table table
112  * \return this table
113  */
114  JTable2D& add(const JTable2D& table)
115  {
116  for (int i = 0; i != NX; ++i) {
117  for (int j = 0; j != NY; ++j) {
118  data[i][j] += table.data[i][j];
119  }
120  }
121 
122  return *this;
123  }
124 
125 
126  /**
127  * Subtract table.
128  *
129  * \param table table
130  * \return this table
131  */
132  JTable2D& sub(const JTable2D& table)
133  {
134  for (int i = 0; i != NX; ++i) {
135  for (int j = 0; j != NY; ++j) {
136  data[i][j] -= table.data[i][j];
137  }
138  }
139 
140  return *this;
141  }
142 
143 
144  /**
145  * Scale table.
146  *
147  * \param factor multiplication factor
148  * \return this table
149  */
150  JTable2D& mul(const double factor)
151  {
152  for (int i = 0; i != NX; ++i) {
153  for (int j = 0; j != NY; ++j) {
154  data[i][j] *= factor;
155  }
156  }
157 
158  return *this;
159  }
160 
161 
162  /**
163  * Scale table.
164  *
165  * \param factor division factor
166  * \return this table
167  */
168  JTable2D& div(const double factor)
169  {
170  for (int i = 0; i != NX; ++i) {
171  for (int j = 0; j != NY; ++j) {
172  data[i][j] /= factor;
173  }
174  }
175 
176  return *this;
177  }
178 
179 
180  /**
181  * Read table from input.
182  *
183  * \param in reader
184  * \param table table
185  * \return reader
186  */
187  friend inline JReader& operator>>(JReader& in, JTable2D& table)
188  {
189  for (int i = 0; i != NX; ++i) {
190  for (int j = 0; j != NY; ++j) {
191  in >> table.data[i][j];
192  }
193  }
194 
195  return in;
196  }
197 
198 
199  /**
200  * Write table to output.
201  *
202  * \param out writer
203  * \param table table
204  * \return writer
205  */
206  friend inline JWriter& operator<<(JWriter& out, const JTable2D& table)
207  {
208  for (int i = 0; i != NX; ++i) {
209  for (int j = 0; j != NY; ++j) {
210  out << table.data[i][j];
211  }
212  }
213 
214  return out;
215  }
216 
217 
218  protected:
219  data_type data[NX][NY];
220  };
221 }
222 
223 #endif
Base class for data structures with artithmetic capabilities.
Definition of zero value for any class.
Interface for binary input.
Interface for binary output.
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
int j
Definition: JPolint.hh:792
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:347
2D table with arithmetic capabilities.
Definition: JTable2D.hh:29
JTable2D & negate()
Negate table.
Definition: JTable2D.hh:96
JTable2D & mul(const double factor)
Scale table.
Definition: JTable2D.hh:150
JTable2D & div(const double factor)
Scale table.
Definition: JTable2D.hh:168
JData_t data_type
Definition: JTable2D.hh:30
friend JWriter & operator<<(JWriter &out, const JTable2D &table)
Write table to output.
Definition: JTable2D.hh:206
JTable2D & sub(const JTable2D &table)
Subtract table.
Definition: JTable2D.hh:132
friend JReader & operator>>(JReader &in, JTable2D &table)
Read table from input.
Definition: JTable2D.hh:187
static int getNX()
Get number of rows.
Definition: JTable2D.hh:50
JTable2D()
Default constructor.
Definition: JTable2D.hh:35
JTable2D & add(const JTable2D &table)
Add table.
Definition: JTable2D.hh:114
data_type data[NX][NY]
Definition: JTable2D.hh:219
static int getNY()
Get number of columns.
Definition: JTable2D.hh:61
data_type * operator[](int row)
Get row data.
Definition: JTable2D.hh:85
const data_type * operator[](int row) const
Get row data.
Definition: JTable2D.hh:73