matrix.h 2.63 KiB
#pragma once
#include <iostream>
namespace linalg {
    class Matrix {
        public:
            size_t rows() const { return this->m_rows; }; //get number of rows
            size_t colums() const { return this->m_colums; }; //get number of colums
            bool empty() const { return (this->m_rows == 0 && this->m_colums == 0); }; //is matrix empty
            void reshape(size_t rows, size_t colums); //change number of rows and colums
            void print(); //print matrix
            Matrix() noexcept : m_ptr(nullptr), m_rows(0), m_colums(0) {}; //default constructor
            Matrix(size_t rows); //undefined colum constructor
            Matrix(size_t rows, size_t colums); //undefined matrix constructor
            Matrix(const Matrix& m); //copy constructor
            Matrix(Matrix&& m) noexcept ; //move constructor
            Matrix(std::initializer_list<double> values); //defined colum constructor
            Matrix(std::initializer_list<std::initializer_list<double>> values);  //defined matrix constructor
            ~Matrix() { delete[] m_ptr;}; //destructor
            Matrix& operator=(const Matrix& m); //copy =
            Matrix& operator=(Matrix&& m) noexcept; //move =
            double& operator() (size_t rows, size_t colums); //indexing
            const double& operator() (size_t rows, size_t colums) const; //indexing for const
            Matrix& operator+= (const Matrix& m); //matrix1 += matrix2 matrix1 + matrix2
            Matrix& operator -= (const Matrix& m);  //matrix1 -= matrix2 matrix1 - matrix2
            Matrix& operator*= (const Matrix& m); //matrix1*=matrix2 matrix1*matrix2
            Matrix& operator*= (double a); //matrix1*=a matrix1*a
            //bool operator== (const Matrix & m) const; // is matrix1 equal matrix2?
            //bool operator!= (const Matrix & m) const; //is matrix1 not equal matrix2?
            double trace() const;
            double norm() const;
        private:
            double* m_ptr;
            size_t m_rows;
            size_t m_colums;
    std::ostream& operator<< (std::ostream& os, const Matrix& matrix); //output
    Matrix operator+ (const Matrix& m1, const Matrix& m2); //matrix1 + matrix2
    Matrix operator- (const Matrix& m1, const Matrix& m2); //matrix1 - matrix2
    Matrix operator*(const Matrix& m1, const Matrix& m2); //matrix1 * matrix2 
    Matrix operator*(const Matrix& m, double a); // matrix*a
    Matrix operator* (double a, const Matrix& m); //a*matrix
    bool operator== (const Matrix& m1, const Matrix& m2); // is matrix1 equal matrix2?
    bool operator!= (const Matrix& m1, const Matrix& m2); //is matrix1 not equal matrix2?