Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:17:14

0001 //
0002 // APFEL++ 2017
0003 //
0004 // Author: Valerio Bertone: valerio.bertone@cern.ch
0005 //
0006 
0007 #pragma once
0008 
0009 #include <vector>
0010 #include <array>
0011 #include <stddef.h>
0012 #include <cstddef>
0013 
0014 namespace apfel
0015 {
0016   /**
0017    * @brief The matrix class is a simple implementation of 2d arrays
0018    * based on a continous memory allocation. Elements are accessible
0019    * throught the (i,j) operator.
0020    */
0021   template<typename T>
0022   class matrix
0023   {
0024   public:
0025     /**
0026      * @brief The matrix constructor.
0027      * @param row: number of rows
0028      * @param col: number of columns
0029      * @param d: vector of data (if empty the matrix is filled in with null values)
0030      */
0031     matrix(size_t const& row = 0, size_t const& col = 0, std::vector<T> const& d = {});
0032 
0033     /**
0034      * @brief The matrix copy constructor.
0035      * @param m: matrix
0036      */
0037     matrix(matrix<T> const& m);
0038 
0039     /**
0040      * @brief Function that resizes object and set default value.
0041      * @param row: number of rows
0042      * @param col: number of columns
0043      * @param v: the default value (default: 0)
0044      */
0045     void resize(size_t const& row, size_t const& col, T const& v = 0);
0046 
0047     /**
0048      * @brief Function that sets all entries of the matrix to the
0049      * input value.
0050      * @param v: the default value
0051      */
0052     void set(T const& v);
0053 
0054     /**
0055      * @brief Returns the (row,col) size pair.
0056      * @param dim: the dimension
0057      * @returns the number of rows and columns
0058      */
0059     size_t const& size(size_t const& dim) const { return _size[dim]; }
0060 
0061     /**
0062      * @brief Returns the pair of sizes.
0063      * @returns the number of rows and columns
0064      */
0065     std::array<size_t, 2> const& size() const { return _size; }
0066 
0067     /**
0068      * @brief Returns the vector of data as a const reference.
0069      * @returns the vector of data
0070      */
0071     std::vector<T> const& data() const { return _data; }
0072 
0073     /**
0074      * @brief Returns the vector of data as a non-constant reference
0075      * (can be used to set the data).
0076      * @returns the vector of data
0077      */
0078     std::vector<T>& data() { return _data; }
0079 
0080     /**
0081      * @name Binary operators involving matrices
0082      */
0083     ///@{
0084     T&       operator () (size_t const& i, size_t const& j)       { return _data[i * _size[1] + j]; }
0085     T const& operator () (size_t const& i, size_t const& j) const { return _data[i * _size[1] + j]; }
0086     matrix<T>& operator  = (matrix<T> const& m);
0087     matrix<T>& operator += (matrix<T> const& m);
0088     matrix<T>& operator -= (matrix<T> const& m);
0089     matrix<T>& operator *= (double const& f);
0090     matrix<T>& operator /= (double const& f);
0091     matrix<T>& operator *= (matrix<T> const& m);
0092     ///@}
0093   private:
0094     std::array<size_t, 2> _size; //!< The dimension pair
0095     std::vector<T>        _data; //!< The data array
0096   };
0097 
0098   /**
0099    * @name Ternary operators
0100    */
0101   ///@{
0102   template<class T>
0103   matrix<T> operator + (matrix<T> lhs, matrix<T> const& rhs); //!< matrix+matrix
0104   template<class T>
0105   matrix<T> operator - (matrix<T> lhs, matrix<T> const& rhs); //!< matrix-matrix
0106   template<class T>
0107   matrix<T> operator * (double const& s, matrix<T> rhs);      //!< Scalar*matrix
0108   template<class T>
0109   matrix<T> operator * (matrix<T> lhs, double const& s);      //!< matrix*Scalar
0110   template<class T>
0111   matrix<T> operator / (matrix<T> lhs, double const& s);      //!< matrix/Scalar
0112   template<class T>
0113   matrix<T> operator * (matrix<T> lhs, matrix<T> const& rhs); //!< matrix*matrix
0114   ///@}
0115 }