Матрица представлена ​​в виде одного массива в памяти и проблема с умножением матрицы - PullRequest
0 голосов
/ 23 мая 2019

Я попытался сделать заголовочный файл Matrix. Я также дал представление о представлении матрицы одним массивом. Каждый метод, который я написал, работал просто отлично. Напротив, я не мог понять проблему с умножением матриц. Кажется, хорошо, хотя. Я удалил код, чтобы сделать его маленьким. любая помощь приветствуется.

#ifndef MATRIX_HPP
#define MATRIX_HPP

#include <iostream>
#include <random>
#include <chrono>
#include <ctime>
#include <iomanip>
using std::cout;
using std::endl;
using std::ios;

static std::ostream & pretty_print(std::ostream & output) {
    output.setf(ios::showpoint);
    output.setf(ios::showpos);
    output.width(6);
    output.precision(2);

    return output;
}

class Matrix {
public:
    // Constructor functions
    Matrix();
    Matrix(size_t r, size_t c, double v = 0);
    Matrix(size_t r, size_t c, double *array);
    Matrix(const Matrix &mat);

    // helping functions
    void randomize(double a = -1, double b = 1);
    void addMat(const Matrix &mat);
    void subMat(const Matrix &mat);
    void multiply_matrix(const Matrix &mat);
    Matrix transpose();
    double* toArray();
    void display();

    // Static functions
    static Matrix matMul(const Matrix &mat1, const Matrix &mat2);
    static Matrix transpose(const Matrix &mat);
    static Matrix fromArray(double *arr, size_t size);

    // Overloaded operator functions and some friend functions
    /*friend ostream& operator<<(ostream &dout, Matrix &mat);
    friend istream& operator>>(istream &din, Matrix &mat);*/
private:
    size_t rows;
    size_t cols;
    double *matrix;

    // Random number engine
    static uint32_t generate_seed();
    static double get_random(double a,double b);
};
// Private static functions
uint32_t Matrix::generate_seed() {
    {
        std::random_device random;
        if (random.entropy() > 0.0) {
            return random();
        }
    }

    return std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

//--------------------------------------------------------------------

double Matrix::get_random(double a, double b) {
    static std::mt19937 random(Matrix::generate_seed());
    std::uniform_real_distribution<double> double_dist{a, b};
    return double_dist(random);
}

//--------------------------------------------------------------------

Matrix::Matrix() {
    rows = 0;
    cols = 0;
    matrix = new double[rows * cols];
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < cols; ++j) {
            *(matrix + i * cols + j) = 0;
        }
    }
}

//--------------------------------------------------------------------

Matrix::Matrix::Matrix(size_t r, size_t c, double v) {
    rows = r;
    cols = c;
    matrix = new double[rows * cols];
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < cols; ++j) {
            *(matrix + i * cols + j) = v;
        }
    }
}

//--------------------------------------------------------------------

Matrix::Matrix(size_t r, size_t c, double *array) {
    rows = r;
    cols = c;
    matrix = new double[rows * cols];
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < cols; ++j) {
            *(matrix + i * cols + j) = *(array + i * cols + j);
        }
    }
}

//----------------------------------------------------------------

Matrix::Matrix(const Matrix &mat) {
    rows = mat.rows;
    cols = mat.cols;
    matrix = new double[rows * cols];
    for (size_t i = 0; i < rows; ++i) {
        for (size_t j = 0; j < cols; ++j) {
            *(matrix + i * cols + j) = *(mat.matrix + i * rows + cols);
        }
    }
}

//------------------------------------------------------------------

void Matrix::randomize(double a, double b) {
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            *(matrix + i * cols + j) = Matrix::get_random(a, b);
        }
    }
}

//-----------------------------------------------------------------------

Matrix Matrix::transpose() {
    Matrix result(cols, rows);
    for (size_t i = 0; i < result.rows; i++) {
        for (size_t j = 0; j < result.cols; j++) {
            *(result.matrix + i * result.cols + j) = *(matrix + j * cols + i);
        }
    }

    return result;
}

//-----------------------------------------------------------------------

void Matrix::display() {
    cout<<"[";
    for (size_t i = 0; i < rows; i++) {
        cout<<"[";
        for (size_t j = 0; j < cols; j++) {
            if (j != cols - 1) {
                cout<<pretty_print<<*(matrix + i * cols + j)<<", ";
            } else if (i != rows - 1 && j == cols - 1) {
                cout<<pretty_print<<*(matrix + i * cols + j)<<"],"<<endl<<" ";
            } else if (i == rows - 1 && j == cols - 1) {
                cout<<pretty_print<<*(matrix + i * cols + j)<<"]]"<<endl;
            }
        }
    }
    cout<<endl;
}

//-----------------------------------------------------------------------

Matrix Matrix::matMul(const Matrix& mat1, const Matrix& mat2) {
    if (mat1.cols == mat2.rows) {
        Matrix result(mat1.rows, mat2.cols);

        double sum; // mat1[i][k] * mat2[k][j];
        for (size_t i = 0; i < result.rows; i++) {
            for (size_t j = 0; j < result.cols; j++) {
                sum = 0;
                for (size_t k = 0; k < mat1.cols; k++) {
                    sum += *(mat1.matrix + i * mat1.cols + k) * *(mat2.matrix + k * mat2.cols + j);
                }
                *(result.matrix + i * result.cols + j) = sum;
            }
        }
        return result;
    } else {
        cout<<"Matrix multiplication is not possible!"<<endl;
        return Matrix();
    }
}
...