"Fossies" - the Fresh Open Source Software Archive

Member "augustus-3.3.3/include/matrix.hh" (22 May 2019, 2762 Bytes) of package /linux/misc/augustus-3.3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "matrix.hh" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.3.2_vs_3.3.3.

    1 /*
    2  * matrix.hh
    3  *
    4  * License: Artistic License, see file LICENSE.TXT or 
    5  *          https://opensource.org/licenses/artistic-license-1.0
    6  */
    7 
    8 #ifndef _MATRIX_HH
    9 #define _MATRIX_HH
   10 
   11 // standard C/C++ includes
   12 #include <vector>
   13 #include <ostream>  // for operator<<
   14 
   15 
   16 using namespace std;
   17 
   18 /**
   19  * @brief A simple matrix class. Base class for all mathematical matrix objects.
   20  * @details This class provides the functionality of a matrix. It is designed
   21  *          to easily allocate space without the need to free this explicitily.
   22  *
   23  * @author  Emmanouil Stafilarakis
   24  * @version 0.1
   25  */
   26 template <class T>
   27 class Matrix {
   28 public:
   29     /// The type of the stored values
   30     typedef T         value_t;
   31 public:
   32     /**
   33      * Constructor
   34      */
   35     Matrix(int n = 0, int m = 0) {
   36     init(n,m);
   37     }
   38     /**
   39      * Destructor
   40      */
   41     ~Matrix( ){ 
   42     delete[] data;
   43     }
   44    /**
   45      * this is a row: just a pointer
   46      */
   47     value_t* operator[] ( int i ) {
   48     return data + i*width;
   49     }
   50     /**
   51      *
   52      */
   53     const value_t* operator[] ( int i ) const {
   54     return data + i*width;
   55     }
   56     /**
   57      *
   58      */
   59     value_t& operator() ( int i, int j ) {
   60     return (*this)[i][j];
   61     }
   62     /**
   63      *
   64      */
   65     const value_t& operator() ( int i, int j ) const {
   66     return (*this)[i][j];
   67     }
   68     /**
   69      * this clones the row
   70      */
   71     vector<T> getRow(int i) const {
   72     return vector<T>(data + i*width, data + (i+1)*width);
   73     }
   74     /**
   75      *
   76      */
   77     vector<T> getColumn(int j) const {
   78     vector<T> result;
   79     result.reserve(height);
   80     for (value_t* p = data + j; p < data + size; p+=width)
   81         result.push_back(*p);
   82     return result;
   83     }
   84     /**
   85      *
   86      */
   87     int getColSize( ) const {
   88     return height;
   89     }
   90     /**
   91      *
   92      */
   93     int getRowSize( ) const {
   94     return width;
   95     }
   96     /**
   97      *
   98      */
   99     void operator=(const Matrix<value_t>& mat) {
  100     resize(mat.height, mat.width);
  101     for (int i=0; i<size; i++) 
  102         data[i] = mat.data[i];
  103     }
  104     void assign(int n, int m, value_t t = value_t()) {
  105     resize(n,m);
  106     for (int i=0; i<size; i++) 
  107         data[i] = t;
  108     }
  109     friend ostream& operator<<(ostream& out, const Matrix<value_t>& mat) {
  110     for (int i = 0; i < mat.size; i++) {
  111         out << mat.data[i];
  112         if (i+1 % mat.width)
  113         out << "\t";
  114         else
  115         out << endl;
  116     }
  117     return out;
  118     }
  119 
  120 private:
  121     void resize(int n = 0, int m = 0) {
  122     if (n == height && m == width) 
  123         return;
  124     delete[] data;
  125     init(n,m);
  126     }
  127     void init(int n = 0, int m = 0) {
  128     if (n>0 && m>0) {
  129         height=n; width=m; size=n*m;
  130         data  = new value_t[size];
  131     } else {
  132         size=height=width=0; data=0;
  133     }
  134     }
  135     int     height;  // n
  136     int     width;  // m
  137     int     size;    // n*m;
  138     value_t   *data;  // the nxm matrix
  139 };
  140 
  141 #endif   //  _MATRIX_HH
  142