Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:48:23

0001 #ifndef MATRIX_D_H
0002 #define MATRIX_D_H
0003 
0004 /**
0005  * @file MatrixD.h
0006  * @author Bryan BERTHOU (Irfu/SPhN - CEA Saclay)
0007  * @author Nabil CHOUIKA (Irfu/SPhN - CEA Saclay)
0008  * @date 23 October 2014
0009  */
0010 
0011 #include <string>
0012 #include <vector>
0013 #include <stddef.h>
0014 
0015 #include "../vector/VectorD.h"
0016 
0017 namespace NumA {
0018 
0019 class VectorD;
0020 
0021 /**
0022  * @class MatrixD
0023  *
0024  * @brief Represents a two-dimensional array of double.
0025  *
0026  * The indices start at 0 up to `size`-1.
0027  *
0028  * Examples:
0029  * ```cpp
0030  * NumA::MatrixD A(5,5); // 5x5 matrix with coefficients equal to 0.
0031  * A.at(1,3) = 10.; // Defines one coefficient (second row, fourth column).
0032  * NumA::MatrixD B = NumA::MatrixD::Id(5); // Identity matrix.
0033  * B.at(4,4) = 5.; // Last row, last column.
0034  * // Matrix operations:
0035  * NumA::MatrixD C;
0036  * C = A*B + B/2.;
0037  * C.toString();
0038  * ```
0039  * This returns:
0040  * ```
0041  * 0.5 0 0 0 0
0042  * 0 0.5 0 10 0
0043  * 0 0 0.5 0 0
0044  * 0 0 0 0.5 0
0045  * 0 0 0 0 2.5
0046  * ```
0047  */
0048 
0049 class MatrixD {
0050 public:
0051     /**
0052      * Default constructor. \n
0053      * Initializes an empty matrix.
0054      */
0055     MatrixD();
0056 
0057     /**
0058      * Creates a matrix with specific dimensions and fills it with given values separated by comma.
0059      *
0060      * TODO: Write something better?
0061      *
0062      * If the number and type of arguments are not right, UNDEFINED BEHAVIOUR !!!
0063      * This constructor is very DANGEROUS!!! USE IT RIGHT, OR SCRAP IT ALL AND REWRITE SOMETHING BETTER!
0064      *
0065      * @param _rowsNumber : number of rows
0066      * @param _columnsNumber : number of columns
0067      * @param first_value : first value of the matrix
0068      */
0069     MatrixD(const size_t _rowsNumber, const size_t _columnsNumber,
0070             double first_value, ...);
0071 
0072     /**
0073      * Creates matrix with specific dimensions and fills it with 0.
0074      *
0075      * @param _rowsNumber : number of rows
0076      * @param _columnsNumber : number of columns
0077      */
0078     MatrixD(const size_t _rowsNumber, const size_t _columnsNumber);
0079 
0080     /**
0081      * Copy constructor.
0082      *
0083      * @param rhs Matrix to be copied.
0084      */
0085     MatrixD(const MatrixD& rhs);
0086 
0087     /**
0088      * VectorD constructor (creates a one-column matrix).
0089      *
0090      * @param rhs Vector to be converted to a matrix.
0091      */
0092     MatrixD(const VectorD& rhs);
0093 
0094     /**
0095      * Default destructor.
0096      */
0097     virtual ~MatrixD();
0098 
0099     /**
0100      * Assigns a new matrix with all coefficients set to `value`.
0101      * @param _rowsNumber Number of rows.
0102      * @param _columnsNumber Number of columns.
0103      * @param value Default value for all the coefficients.
0104      */
0105     void assign(const size_t _rowsNumber, const size_t _columnsNumber,
0106             double value = 0.);
0107 
0108     /**
0109      * Sets an existing line of the matrix to new values given by a vector.
0110      * @param i Index of the line to be modified.
0111      * @param line Vector to copy in the matrix.
0112      */
0113     void setLine(size_t i, const NumA::VectorD& line);
0114     /**
0115      * Adds a new line in the matrix (its dimension is therefore incremented) with the values given by a vector.
0116      * @param i Index the row where the line will be inserted.
0117      * @param line Vector to copy in the matrix.
0118      */
0119     void addLine(size_t i, const NumA::VectorD& line);
0120     /**
0121      * Adds a new line at the end of the matrix (its dimension is therefore incremented) with the values given by a vector.
0122      * @param line Vector to copy in the matrix.
0123      */
0124     void appendLine(const NumA::VectorD& line);
0125     /**
0126      * Extracts a single line from the matrix.
0127      * @param lineIndex Index of the row to be extracted.
0128      * @return Vector corresponding to the row.
0129      */
0130     NumA::VectorD getLine(const size_t lineIndex) const;
0131 
0132     // Matrix/vector operations
0133     /**
0134      * Matrix vector multiplication.
0135      * @param rhs Vector.
0136      * @return Vector \f$ M \cdot V \f$.
0137      */
0138     NumA::VectorD operator*(const NumA::VectorD& rhs) const;
0139     //std::vector<double> operator*=(const std::vector<double>& rhs);
0140 
0141     // Matrix/matrix operations
0142     /**
0143      * Matrix-matrix multiplication.
0144      * @param rhs Matrix.
0145      * @return Product of matrices.
0146      */
0147     NumA::MatrixD operator*(const NumA::MatrixD& rhs) const;
0148     /**
0149      * Matrix addition.
0150      * @param rhs Matrix.
0151      * @return Sum of matrices.
0152      */
0153     NumA::MatrixD operator+(const NumA::MatrixD& rhs) const;
0154     /**
0155      * Matrix subtraction.
0156      * @param rhs Matrix.
0157      * @return Difference of matrices.
0158      */
0159     NumA::MatrixD operator-(const NumA::MatrixD& rhs) const;
0160 
0161     // Matrix/double operations
0162     /**
0163      * Multiplication of all the coefficients by a scalar.
0164      * @param rhs Scalar.
0165      * @return Matrix.
0166      */
0167     NumA::MatrixD operator*(double rhs) const;
0168     /**
0169      * Addition coefficient by coefficient with a scalar.
0170      * @param rhs Scalar.
0171      * @return Matrix.
0172      */
0173     NumA::MatrixD operator+(double rhs) const;
0174     /**
0175      * Subtraction coefficient by coefficient with a scalar.
0176      * @param rhs Scalar.
0177      * @return Matrix.
0178      */
0179     NumA::MatrixD operator-(double rhs) const;
0180     /**
0181      * Division of all the coefficients by a scalar.
0182      * @param rhs Scalar.
0183      * @return Matrix.
0184      */
0185     NumA::MatrixD operator/(double rhs) const;
0186 
0187     /**
0188      * Matrix transpose.
0189      * @return Matrix transposed.
0190      */
0191     NumA::MatrixD transpose() const;
0192 
0193     /**
0194      * Matrix identity.
0195      * @param n Dimension.
0196      * @return Matrix identity.
0197      */
0198     static NumA::MatrixD Id(unsigned int n);
0199 
0200     /**
0201      * Update the value at the given indices.
0202      *
0203      * @param i Row index.
0204      * @param j Column index.
0205      * @param value New value for the coefficient.
0206      */
0207     void update(const size_t i, const size_t j, const double value);
0208 
0209     /**
0210      * Element access.
0211      * Can be used to set the value of a coefficient.
0212      * @param i Row index.
0213      * @param j Column index.
0214      * @return Reference to a certain coefficient of the matrix.
0215      */
0216     double & at(const size_t i, const size_t j);
0217     /**
0218      * Element access.
0219      * @param i Row index.
0220      * @param j Column index.
0221      * @return Constant reference to a certain coefficient of the matrix.
0222      */
0223     const double & at(const size_t i, const size_t j) const;
0224 
0225     /**
0226      * Return a formatted characters string to display matrix's values.
0227      *
0228      * @return std::string
0229      */
0230     std::string toString() const;
0231 
0232     // ##### GETTERS & SETTERS #####
0233     /**
0234      *
0235      * @return Number of columns.
0236      */
0237     size_t cols() const;
0238     /**
0239      *
0240      * @return Number of rows.
0241      */
0242     size_t rows() const;
0243 
0244 private:
0245     std::vector<double> m_matrix; ///< A flat std::vector to represent the matrix.
0246     size_t m_rowsNumber;    ///< Number of rows.
0247     size_t m_columnsNumber;    ///< Number of columns.
0248 };
0249 
0250 } /* namespace NumA */
0251 
0252 #endif /* MATRIX_D_H */