Ray Tracer  2020
Matrix.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef MATRIX_H_INCLUDED
4 #define MATRIX_H_INCLUDED
5 
10 #include <vector>
11 #include <iostream>
12 
22 class Matrix {
23 
24 public:
25 
34  Matrix(size_t size=1);
35 
45  Matrix(size_t rows, size_t cols);
46 
55  Matrix(const Matrix& mat);
56 
63  virtual ~Matrix();
64 
74  Matrix& operator=(const Matrix& mat);
75 
76  // Handy factory methods
77 
89  static Matrix identity(size_t rows, size_t cols);
90 
101  static Matrix zero(size_t rows, size_t cols);
102 
115  double& operator()(size_t row, size_t col);
116 
128  const double& operator()(size_t row, size_t col) const;
129 
135  size_t numRows() const;
136 
142  size_t numCols() const;
143 
149  size_t numElements() const;
150 
159  Matrix operator-() const;
160 
170  Matrix& operator+=(const Matrix& mat);
171 
181  Matrix& operator-=(const Matrix& mat);
182 
200  friend Matrix operator*(const Matrix& lhs, const Matrix& rhs);
201 
215  Matrix& operator*=(double s);
216 
225  Matrix& operator/=(double s);
226 
234  Matrix transpose() const;
235 
236 protected:
237 
238  size_t rows_;
239  size_t cols_;
240  std::vector<double> data_;
241 
242 };
243 
255 Matrix operator+(const Matrix& lhs, const Matrix& rhs);
256 
268 Matrix operator-(const Matrix& lhs, const Matrix& rhs);
269 
280 Matrix operator*(double s, const Matrix& mat);
281 
292 Matrix operator*(const Matrix& mat, double s);
293 
306 Matrix operator/(const Matrix& mat, double s);
307 
318 std::ostream& operator<<(std::ostream& outputStream, const Matrix& mat);
319 
331 std::istream& operator>>(std::istream& inputStream, Matrix& mat);
332 
333 #endif // MATRIX_H_INCLUDED
Colour operator/(const Colour &colour, double s)
Colour-scalar division.
Definition: Colour.cpp:85
Colour operator+(const Colour &lhs, const Colour &rhs)
Colour addition.
Definition: Colour.cpp:35
Colour operator-(const Colour &lhs, const Colour &rhs)
Colour subtraction.
Definition: Colour.cpp:47
Basic class for matrices.
Definition: Matrix.h:22
std::ostream & operator<<(std::ostream &outputStream, const Matrix &mat)
Stream insertion operator.
Definition: Matrix.cpp:209
Matrix(size_t size=1)
Square Matrix constructor.
Definition: Matrix.cpp:12
Matrix & operator=(const Matrix &mat)
Matrix assignment operator.
Definition: Matrix.cpp:41
Matrix & operator/=(double s)
Matrix-scalar multiplication-assignment operator.
Definition: Matrix.cpp:190
std::istream & operator>>(std::istream &inputStream, Matrix &mat)
Stream extraction operator.
Definition: Matrix.cpp:222
size_t numRows() const
Number of rows in a Matrix.
Definition: Matrix.cpp:93
static Matrix zero(size_t rows, size_t cols)
Factory method for Zero Matrices.
Definition: Matrix.cpp:69
double & operator()(size_t row, size_t col)
Matrix element access.
Definition: Matrix.cpp:81
Matrix operator-() const
Unary minus.
Definition: Matrix.cpp:110
Matrix transpose() const
Matrix transpose.
Definition: Matrix.cpp:198
size_t numElements() const
Number of elements in a Matrix.
Definition: Matrix.cpp:103
friend Matrix operator*(const Matrix &lhs, const Matrix &rhs)
Matrix-Matrix multiplication operator.
Definition: Matrix.cpp:151
size_t cols_
Number of columns in the Matrix.
Definition: Matrix.h:239
Matrix & operator+=(const Matrix &mat)
Matrix addition-assignment operator.
Definition: Matrix.cpp:125
virtual ~Matrix()
Matrix destructor.
Definition: Matrix.cpp:36
std::vector< double > data_
Storage for Matrix data elements.
Definition: Matrix.h:240
Matrix & operator-=(const Matrix &mat)
Matrix subtraction-assignment operator.
Definition: Matrix.cpp:140
Matrix & operator*=(double s)
Matrix-scalar multiplication-assignment operator.
Definition: Matrix.cpp:177
size_t numCols() const
Number of columns in a Matrix.
Definition: Matrix.cpp:98
static Matrix identity(size_t rows, size_t cols)
Factory method for Identity Matrices.
Definition: Matrix.cpp:58
size_t rows_
Number of rows in the Matrix.
Definition: Matrix.h:238