├── README.md └── matrix.h /README.md: -------------------------------------------------------------------------------- 1 | # Matrix 2 | 3 | Matrix class in C++ with operators implemented. 4 | 5 | # Usage 6 | 7 | ```c++ 8 | #include "matrix.h" 9 | ... 10 | 11 | // Constructors : 12 | Matrix matrix(int height, int width); 13 | Matrix matrix(vector > &array); 14 | Matrix matrix(); 15 | ``` 16 | 17 | # Operators 18 | 19 | ```c++ 20 | Matrix a,b,c; 21 | int z; 22 | 23 | c = a + b; 24 | c = a - b; 25 | c = a * b; 26 | c = z * a; 27 | c = a / z; 28 | ``` 29 | 30 | # Example 31 | 32 | ```c++ 33 | Matrix A(4,5); 34 | Matrix B(4,5); 35 | Matrix C(5,6); 36 | 37 | Matrix D = A + B; // = A.add(B) 38 | Matrix D = A - B; // = A.subtract(B) 39 | Matrix D = A * B; // = A.multiply(B) 40 | Matrix D = B.dot(C); 41 | Matrix D = A.transpose(); 42 | Matrix D = A.subMatrix(0,0, 2,2); // get a sub matrix of size 2x2 from coordinates 0;0 in A 43 | 44 | A += B; 45 | A -= B; 46 | A *= B; 47 | 48 | int multiplyByTwo(int x){ 49 | return 2*x; 50 | } 51 | 52 | Matrix D = A.applyFunction(multiplyByTwo); 53 | 54 | // you can combine operations : 55 | 56 | Matrix D = (A+B).dot(C).applyFunction(multiplyByTwo); 57 | 58 | std::cout << D << std::endl; 59 | ``` 60 | -------------------------------------------------------------------------------- /matrix.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_MATRIX 2 | #define DEF_MATRIX 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | class Matrix{ 11 | private: 12 | std::vector > array; 13 | int height; 14 | int width; 15 | 16 | public: 17 | Matrix(int height, int width); 18 | Matrix(std::vector > const &array); 19 | Matrix(); 20 | 21 | int getHeight() const; 22 | int getWidth() const; 23 | 24 | Matrix add(const Matrix& m) const; 25 | Matrix subtract(const Matrix& m) const; 26 | Matrix multiply(const Matrix& m) const; 27 | Matrix dot(const Matrix& m) const; 28 | Matrix transpose() const; 29 | Matrix multiply(const T& value) const; 30 | Matrix divide(const T& value) const; 31 | 32 | Matrix applyFunction(T (*function)(T)) const; 33 | Matrix subMat(int startH, int startW, int h, int w) const; 34 | 35 | void fill(const T& value); 36 | void put(int h, int w, const T& value); 37 | T get(int h, int w) const; 38 | 39 | void print(std::ostream &flux) const; 40 | 41 | bool operator==(const Matrix& m); 42 | bool operator!=(const Matrix& m); 43 | Matrix operator+=(const Matrix& m); 44 | Matrix operator-=(const Matrix& m); 45 | Matrix operator*=(const Matrix& m); 46 | Matrix operator*=(const T &m); 47 | Matrix operator/=(const T &m); 48 | T& operator()(int y, int x); 49 | }; 50 | 51 | template Matrix operator+(const Matrix& a, const Matrix& b); 52 | template Matrix operator-(const Matrix& a, const Matrix& b); 53 | template Matrix operator*(const Matrix& a, const Matrix& b); 54 | template Matrix operator*(const T &b, const Matrix& a); 55 | template Matrix operator/(const Matrix& a, const T &b); 56 | template std::ostream& operator<<(std::ostream &flux, const Matrix& m); 57 | 58 | #endif 59 | 60 | 61 | template 62 | Matrix::Matrix(int height, int width){ 63 | this->height = height; 64 | this->width = width; 65 | this->array = std::vector >(height, std::vector(width)); 66 | } 67 | 68 | template 69 | Matrix::Matrix(std::vector > const &array){ 70 | if(array.size()==0) 71 | throw std::invalid_argument("Size of array must be greater than 0."); 72 | 73 | this->height = array.size(); 74 | this->width = array[0].size(); 75 | this->array = array; 76 | } 77 | 78 | template 79 | Matrix::Matrix(){ 80 | height = 0; 81 | width = 0; 82 | } 83 | 84 | template 85 | int Matrix::getHeight() const{ 86 | return height; 87 | } 88 | 89 | template 90 | int Matrix::getWidth() const{ 91 | return width; 92 | } 93 | 94 | template 95 | void Matrix::fill(const T& value){ 96 | for (int i=0 ; i 104 | void Matrix::put(int h, int w, const T& value){ 105 | if(!(h>=0 && h=0 && w 112 | T Matrix::get(int h, int w) const{ 113 | if(!(h>=0 && h=0 && w 120 | Matrix Matrix::multiply(const T& value) const{ 121 | Matrix result(array); 122 | for (int i=0 ; i 132 | Matrix Matrix::divide(const T& value) const{ 133 | Matrix result(array); 134 | for (int i=0 ; i 144 | Matrix Matrix::add(const Matrix& m) const{ 145 | if(!(height==m.height && width==m.width)) 146 | throw std::invalid_argument("Matrix dimension must be the same."); 147 | 148 | Matrix result(height, width); 149 | for (int i=0 ; i 159 | Matrix Matrix::subtract(const Matrix& m) const{ 160 | if(!(height==m.height && width==m.width)) 161 | throw std::invalid_argument("Matrix dimension must be the same."); 162 | 163 | Matrix result(height, width); 164 | for (int i=0 ; i 173 | Matrix Matrix::multiply(const Matrix& m) const{ 174 | if(!(height==m.height && width==m.width)) 175 | throw std::invalid_argument("Matrix dimension must be the same."); 176 | 177 | Matrix result(height, width); 178 | 179 | for (int i=0 ; i 188 | Matrix Matrix::dot(const Matrix& m) const{ 189 | if(!(width==m.height)) 190 | throw std::invalid_argument("Dot product not compatible."); 191 | 192 | T w=0; 193 | int mwidth = m.width; 194 | 195 | Matrix result(height, mwidth); 196 | for (int i=0 ; i 210 | Matrix Matrix::transpose() const{ 211 | Matrix result(width, height); 212 | 213 | for (int i=0 ; i 222 | Matrix Matrix::applyFunction(T (*function)(T)) const{ 223 | Matrix result(height, width); 224 | 225 | for (int i=0 ; i 235 | Matrix Matrix::subMat(int startH, int startW, int h, int w) const{ 236 | if(!(startH>=0 && startH+h<=height && startW>=0 && startW+w<=width)) 237 | throw std::invalid_argument("Index out of bounds"); 238 | 239 | Matrix result(h,w); 240 | for (int i=startH ; i 249 | void Matrix::print(std::ostream &flux) const{ 250 | int maxLength[width] = {}; 251 | std::stringstream ss; 252 | 253 | for (int i=0 ; i 277 | bool Matrix::operator==(const Matrix& m){ 278 | if(height==m.height && width==m.width){ 279 | for (int i=0 ; i 292 | bool Matrix::operator!=(const Matrix& m){ 293 | return !operator==(m); 294 | } 295 | 296 | template 297 | Matrix Matrix::operator+=(const Matrix& m){ 298 | this->array = add(m).array; 299 | return *this; 300 | } 301 | 302 | template 303 | Matrix Matrix::operator-=(const Matrix& m){ 304 | this->array = subtract(m).array; 305 | return *this; 306 | } 307 | 308 | template 309 | Matrix Matrix::operator*=(const Matrix& m){ 310 | this->array = multiply(m).array; 311 | return *this; 312 | } 313 | 314 | template 315 | Matrix Matrix::operator*=(const T &m){ 316 | *this = this->multiply(m); 317 | return *this; 318 | } 319 | 320 | template 321 | Matrix Matrix::operator/=(const T &m){ 322 | *this = this->divide(m); 323 | return *this; 324 | } 325 | 326 | template 327 | T& Matrix::operator()(int y, int x){ 328 | if(!(y>=0 && y=0 && x 334 | Matrix operator+(const Matrix& a, const Matrix& b){ 335 | return a.add(b); 336 | } 337 | 338 | template 339 | Matrix operator-(const Matrix& a, const Matrix& b){ 340 | return a.subtract(b); 341 | } 342 | 343 | template 344 | Matrix operator*(const Matrix& a, const Matrix& b){ 345 | return a.multiply(b); 346 | } 347 | 348 | template 349 | Matrix operator*(const T &b, const Matrix& a){ 350 | return a.multiply(b); 351 | } 352 | template 353 | Matrix operator/(const Matrix& a, const T &b){ 354 | return a.divide(b); 355 | } 356 | 357 | template 358 | std::ostream& operator<<(std::ostream &flux, const Matrix& m){ 359 | m.print(flux); 360 | return flux; 361 | } 362 | --------------------------------------------------------------------------------