חזור לתיקייה: wet

Matrix

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    Matrix

    02340124

    מבוא לתכנות מערכות

    רשימות הרצאה לקריאה בשבת

    Matrix

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    matrix.h

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #pragma once
    #include <iostream>
    
    class Matrix {
    public:
        /**
         * Creates a new matrix with one value to fill all the cells.
         *
         * @param rows The number of rows the matrix will have
         * @param columns The number of columns the matrix will have
         * @param value The value to put in each cell of the matrix
         */
        Matrix(int rows = 0, int columns = 0, int value = 0);
    
        /**
         * Copies a matrix to a new one(this one)
         *
         * @param matrix The matrix to copy
         */
        Matrix(const Matrix& matrix);
    
        /**
         * Destroys the current matrix
         */
        ~Matrix();
    
        /**
         * copies the given matrix to this one. the values of the old matrix will be destroyed.
         *
         * @param matrix The matrix to copy
         *
         * @return A reference to this new matrix.
         */
        Matrix& operator=(const Matrix& matrix);
    
        /**
         * Gives you access to a single cell in the matrix for changing and copying.
         *
         * @param row The row of the cell you want to view
         * @param column The column of the cell you want to view
         *
         * @return A reference to the cell for editing and viewing
         */
        int& operator()(int row, int column);
    
        /**
         * Gives you access to a single cell in the matrix for reading only.
         *
         * @param row The row of the cell you want to view
         * @param column The column of the cell you want to view
         *
         * @return A copy of the cell value
         */
        int operator()(int row, int column) const;
    
        /**
         * Prints the current matrix to the stream given.
         *
         * @param ostream The stream to print to
         * @param matrix The matrix to print
         *
         * @return A reference to the stream printed to
         */
        friend std::ostream& operator<<(std::ostream& ostream, const Matrix& matrix);
    
        /**
         * Creates a new matrix that is the sum of the two given.
         *
         * @param mat1 The first matrix to add up
         * @param mat2 The second matrix to add up
         *
         * @return The new sum matrix
         */
        friend Matrix operator+(const Matrix& mat1, const Matrix& mat2);
    
        /**
         * Creates a new matrix that is the difference between the two given.
         *
         * @param mat1 The matrix to subtract from
         * @param mat2 The matrix that will subtract from mat1
         *
         * @return The new difference matrix
         */
        friend Matrix operator-(const Matrix& mat1, const Matrix& mat2);
    
        /**
         * Creates a new matrix that is the multiplication between the two given.
         *
         * @param mat1 The first matrix to multiply
         * @param mat2 The second matrix to multiply
         *
         * @return The new multiplication matrix
         */
        friend Matrix operator*(const Matrix& mat1, const Matrix& mat2);
    
        /**
         * Adds a matrix to the current one.
         *
         * @param matrix The matrix to add to this one
         *
         * @return A reference to this matrix after the addition
         */
        Matrix& operator+=(const Matrix& matrix);
    
        /**
         * Subtracts a matrix from the current one.
         *
         * @param matrix The matrix that will subtract from this one
         *
         * @return A reference to this matrix after the substraction
         */
        Matrix& operator-=(const Matrix& matrix);
    
        /**
         * Multiplies a matrix and this matrix and the result is saved to this matrix.
         *
         * @param matrix The matrix to multiply with
         *
         * @return A reference to this matrix after the multiplication
         */
        Matrix& operator*=(const Matrix& matrix);
    
        /**
         * Creates a new matrix witch is this one but inverted.
         *
         * @return The new inverted matrix
         */
        Matrix operator-() const;
    
        /**
         * Creates a new matrix witch is a multiplication between a scalar and a matrix given.
         *
         * @param mat The matrix to multiply
         * @param value The scalar to multiply with
         *
         * @return the new multiplied matrix
         */
        friend Matrix operator*(const Matrix& mat, int value);
    
        /**
         * Creates a new matrix witch is a multiplication between a scalar and a matrix given.
         *
         * @param value The scalar to multiply with
         * @param mat The matrix to multiply
         *
         * @return the new multiplied matrix
         */
        friend Matrix operator*(int value, const Matrix& mat);
    
        /**
         * Multiplies this matrix by a scalar given.
         *
         * @param value The scalar to multiply with
         *
         * @return A reference to this matrix after the multiplication
         */
        Matrix& operator*=(int value);
    
        /**
         * Evaluates between this matrix and the one given.
         *
         * @param matrix The matrix to check against
         *
         * @return True if and only if the matrices are equal
         */
        bool operator==(const Matrix& matrix) const;
    
        /**
         * Evaluates between this matrix and the one given.
         *
         * @param matrix The matrix to check against
         *
         * @return True if and only if the matrices are NOT equal
         */
        bool operator!=(const Matrix& matrix) const;
    
        /**
         * Creates a new matrix that is this one but rotated 90 deg counter-clockwise.
         *
         * @return The new rotated matrix
         */
        Matrix rotateCounterClockwise() const;
    
        /**
         * Creates a new matrix that is this one but rotated 90 deg clockwise.
         *
         * @return The new rotated matrix
         */
        Matrix rotateClockwise() const;
    
        /**
         * Creates a new matrix that is this one but transposed.
         *
         * @return The new transposed matrix
         */
        Matrix transpose() const;
    
        /**
         * Calculates the Frobenius Norm of the matrix given.
         *
         * @param mat The matrix to calculate the norm to
         *
         * @return The norm value
         */
        static double CalcFrobeniusNorm(const Matrix& mat);
    
        /**
         * Calculates the determinant of the given matrix.
         *
         * @param mat The matrix to calculate the determinant of
         *
         * @return The determinant value
         */
        static int CalcDeterminant(const Matrix& mat);
    
    private:
        int *mat, cols, rows;
    
        /**
         * Creates a minor
         *
         * @param mat The matrix.
         * @param index The column you develop by.
         *
         * @return A new matrix which is the minor
         */
        static Matrix getMinor(const Matrix& mat, int index);
    };