#ifndef __JTOOLS__JTABLE2D__ #define __JTOOLS__JTABLE2D__ #include "JMath/JMath.hh" #include "JMath/JZero.hh" #include "JIO/JSerialisable.hh" /** * \author mdejong */ namespace JTOOLS {} namespace JPP { using namespace JTOOLS; } namespace JTOOLS { using JMATH::JMath; using JIO::JReader; using JIO::JWriter; /** * 2D table with arithmetic capabilities. */ template struct JTable2D : public JMath< JTable2D > { typedef JData_t data_type; /** * Default constructor. */ JTable2D() { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] = JMATH::zero; } } } /** * Get number of rows. * * \return number of rows */ static int getNX() { return NX; } /** * Get number of columns. * * \return number of columns */ static int getNY() { return NY; } /** * Get row data. * * \param row row number * \return row data */ const data_type* operator[](int row) const { return data[row]; }; /** * Get row data. * * \param row row number * \return row data */ data_type* operator[](int row) { return data[row]; }; /** * Negate table. * * \return this table */ JTable2D& negate() { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] = -data[i][j]; } } return *this; } /** * Add table. * * \param table table * \return this table */ JTable2D& add(const JTable2D& table) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] += table.data[i][j]; } } return *this; } /** * Subtract table. * * \param table table * \return this table */ JTable2D& sub(const JTable2D& table) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] -= table.data[i][j]; } } return *this; } /** * Scale table. * * \param factor multiplication factor * \return this table */ JTable2D& mul(const double factor) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] *= factor; } } return *this; } /** * Scale table. * * \param factor division factor * \return this table */ JTable2D& div(const double factor) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { data[i][j] /= factor; } } return *this; } /** * Read table from input. * * \param in reader * \param table table * \return reader */ friend inline JReader& operator>>(JReader& in, JTable2D& table) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { in >> table.data[i][j]; } } return in; } /** * Write table to output. * * \param out writer * \param table table * \return writer */ friend inline JWriter& operator<<(JWriter& out, const JTable2D& table) { for (int i = 0; i != NX; ++i) { for (int j = 0; j != NY; ++j) { out << table.data[i][j]; } } return out; } protected: data_type data[NX][NY]; }; } #endif