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