CPP Linear Algebra ToolBox  0.1.0
matrix.h
Go to the documentation of this file.
1 
10 #ifndef MATRIX_H
11 #define MATRIX_H
12 
13 #include "vector.h"
14 #include <iomanip>
15 
20 
29 class Matrix {
30  public:
31  /* CONSTRUCTORS*/
32  Matrix();
33  Matrix(uint16_t , uint16_t );
34  Matrix(const Matrix & );
35 
36  /* DESTRUCTOR */
37  virtual ~Matrix();
38 
39  /* ACCESSORS */
40  uint16_t get_n_rows() const;
41  uint16_t get_n_cols() const;
42  uint32_t get_n_elements() const;
43 
44  /* METHODS */
45 
46  bool is_square() const;
47  Vector row(uint16_t ) const;
48  Vector col(uint16_t ) const;
49  double norm1() const;
50  double normInf() const;
51  double normFrob() const;
52  Matrix transpose() const;
53 
54  /* OPERATORS */
55  double & operator()(uint32_t ) const;
56  double & operator()(uint16_t, uint16_t) const;
57 
58  Matrix & operator=(const Matrix & );
59  Matrix & operator+=(const Matrix & );
60  Matrix & operator-=(const Matrix & );
61 
62  Matrix operator-() const;
63 
64  friend bool operator==(const Matrix & , const Matrix & );
65 
66  friend Matrix operator*(const Matrix & , const Matrix & );
67  friend Vector operator*(const Vector & , const Matrix & );
68  friend Vector operator*(const Matrix & , const Vector & );
69  friend Matrix operator*(const double , const Matrix & );
70  friend Matrix operator*(const Matrix & , const double );
71 
72  friend Matrix operator+(const Matrix & , const Matrix & );
73  friend Matrix operator+(const double , const Matrix & );
74  friend Matrix operator+(const Matrix & , const double );
75 
76  friend Matrix operator-(const Matrix & , const Matrix & );
77  friend Matrix operator-(const Matrix & , const double );
78  friend Matrix operator-(const double , const Matrix & );
79 
80  friend Matrix operator/(const Matrix & , const double );
81 
82  friend std::ostream & operator<<(std::ostream & , const Matrix & );
83 
84  /* STATIC METHODS */
85  static Matrix zeros(uint16_t , uint16_t );
86  static Matrix ones(uint16_t , uint16_t );
87  static Matrix rand(uint16_t , uint16_t );
88  static Matrix hilbert(uint16_t , uint16_t );
89  static Matrix vandermonde(const Vector & , uint16_t );
90  static Matrix outer(const Vector & , const Vector & );
91  static Matrix matmul(const Matrix & , MATRIX_TRANSPOSE , const Matrix & , MATRIX_TRANSPOSE );
92 
93  protected:
94  /* ATTRIBUTES */
95  uint16_t n_rows;
96  uint16_t n_cols;
97  uint32_t n_elements;
98  double * data;
100  /* METHODS */
101  double & at(uint32_t ) const;
102  double & at(uint16_t , uint16_t ) const;
103  void show() const;
104  void default_data();
105 };
106 
107 #endif
A class describing a Matrix object. Matrices are array of size (n,m) whre n and m are integer greater...
Definition: matrix.h:29
uint32_t n_elements
Definition: matrix.h:97
static Matrix rand(uint16_t, uint16_t)
Construct a matrix object of dimension n times m filled with random values sampled from 0....
Definition: matrix.cpp:544
friend Matrix operator/(const Matrix &, const double)
Overload operator / for division of Matrix by scalar.
Definition: matrix.cpp:369
Vector row(uint16_t) const
Extract row from Matrix.
Definition: matrix.cpp:410
Matrix & operator+=(const Matrix &)
Overload operator +=.
Definition: matrix.cpp:132
static Matrix zeros(uint16_t, uint16_t)
Construct a matrix object of dimension n times m filled with zeros.
Definition: matrix.cpp:520
Matrix & operator-=(const Matrix &)
Overload operator -=.
Definition: matrix.cpp:147
static Matrix matmul(const Matrix &, MATRIX_TRANSPOSE, const Matrix &, MATRIX_TRANSPOSE)
Compute the product of two matrices combined with their transposition.
Definition: matrix.cpp:613
double normFrob() const
Compute the Frobenius norm of a Matrix object. For a Matrix M of dimension , its Frobenius norm is gi...
Definition: matrix.cpp:487
void default_data()
Fill the data array with zeros.
Definition: matrix.cpp:705
Matrix()
Construct a Matrix object. Default constructor. Set number of rows and number of columns to 0....
Definition: matrix.cpp:9
uint16_t n_rows
Definition: matrix.h:95
bool is_square() const
Test if a matrix has a square shape.
Definition: matrix.cpp:401
static Matrix vandermonde(const Vector &, uint16_t)
Construct a Vandermonde matrix. See https://en.wikipedia.org/wiki/Vandermonde_matrix.
Definition: matrix.cpp:578
double & at(uint32_t) const
Helper to access the n-th stored value of a Matrix object.
Definition: matrix.cpp:672
friend bool operator==(const Matrix &, const Matrix &)
Overload of operator == Two matrices are equal if and only they have the same dimensions and the same...
Definition: matrix.cpp:176
static Matrix outer(const Vector &, const Vector &)
Overload operator * between two vectors to compute their outer product.
Definition: matrix.cpp:595
double normInf() const
Compute the norm-Infinity of a Matrix object. For a Matrix M of dimension , its norm-Infinity is give...
Definition: matrix.cpp:464
friend Matrix operator+(const Matrix &, const Matrix &)
Overload operator + for addition of Matrices.
Definition: matrix.cpp:281
uint16_t get_n_rows() const
Accessor to the number of rows.
Definition: matrix.cpp:61
double * data
Definition: matrix.h:98
Matrix operator-() const
Overload unary operator -.
Definition: matrix.cpp:160
Matrix transpose() const
Compute the transpose Matrix of a Matrix object.
Definition: matrix.cpp:502
uint32_t get_n_elements() const
Accessor to the number of elements.
Definition: matrix.cpp:77
virtual ~Matrix()
Destroy the Matrix object.
Definition: matrix.cpp:51
Vector col(uint16_t) const
Extract column from Matrix.
Definition: matrix.cpp:424
static Matrix hilbert(uint16_t, uint16_t)
Construct an Hilbert matrix.
Definition: matrix.cpp:560
double norm1() const
Compute the norm-1 of a Matrix object. For a Matrix M of dimension , its norm-1 is given by the maxim...
Definition: matrix.cpp:441
void show() const
Helper to display a Matrix object to the standart output stream.
Definition: matrix.cpp:693
static Matrix ones(uint16_t, uint16_t)
Construct a matrix object of dimension n times m filled with ones.
Definition: matrix.cpp:532
uint16_t get_n_cols() const
Accessor to the number of columns.
Definition: matrix.cpp:69
friend Matrix operator*(const Matrix &, const Matrix &)
Overload operator * for multiplication of Matrices.
Definition: matrix.cpp:198
friend std::ostream & operator<<(std::ostream &, const Matrix &)
Overload output stream operator for Matrix object.
Definition: matrix.cpp:384
Matrix & operator=(const Matrix &)
Overload assignement operator.
Definition: matrix.cpp:109
double & operator()(uint32_t) const
Overload operator (i)
Definition: matrix.cpp:99
uint16_t n_cols
Definition: matrix.h:96
A lightweight class describing a Vector object. Vectors are array with size n (>0) of doubles.
Definition: vector.h:25
MATRIX_TRANSPOSE
Enumeration of transposition operation for Matrix object, useful for fast multiplication.
Definition: matrix.h:19
@ TRANSPOSE
Definition: matrix.h:19
@ NO_TRANSPOSE
Definition: matrix.h:19
Header file for Vector class.