以下是一个简单的矩阵类的示例实现,包括了基本运算和功能函数的实现:

#include <iostream>
#include <vector>

class Matrix {
private:
    std::vector<std::vector<double>> data;
    int rows;
    int cols;

public:
    Matrix(int rows, int cols) : rows(rows), cols(cols) {
        data.resize(rows, std::vector<double>(cols, 0.0));
    }

    Matrix(const std::vector<std::vector<double>>& input) {
        rows = input.size();
        cols = input[0].size();
        data = input;
    }

    int getRows() const {
        return rows;
    }

    int getCols() const {
        return cols;
    }

    void setValue(int row, int col, double value) {
        data[row][col] = value;
    }

    double getValue(int row, int col) const {
        return data[row][col];
    }

    Matrix operator+(const Matrix& other) const {
        if (rows != other.rows || cols != other.cols) {
            throw std::invalid_argument("Matrix dimensions must match.");
        }

        Matrix result(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[i][j] = data[i][j] + other.data[i][j];
            }
        }

        return result;
    }

    Matrix operator-(const Matrix& other) const {
        if (rows != other.rows || cols != other.cols) {
            throw std::invalid_argument("Matrix dimensions must match.");
        }

        Matrix result(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[i][j] = data[i][j] - other.data[i][j];
            }
        }

        return result;
    }

    Matrix operator*(const Matrix& other) const {
        if (cols != other.rows) {
            throw std::invalid_argument("Matrix dimensions are not compatible for multiplication.");
        }

        Matrix result(rows, other.cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < other.cols; j++) {
                for (int k = 0; k < cols; k++) {
                    result.data[i][j] += data[i][k] * other.data[k][j];
                }
            }
        }

        return result;
    }

    Matrix transpose() const {
        Matrix result(cols, rows);
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                result.data[i][j] = data[j][i];
            }
        }

        return result;
    }

    void print() const {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                std::cout << data[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }
};

int main() {
    std::vector<std::vector<double>> input1 = {{1, 2, 3}, {4, 5, 6}};
    std::vector<std::vector<double>> input2 = {{7, 8}, {9, 10}, {11, 12}};

    Matrix matrix1(input1);
    Matrix matrix2(input2);

    Matrix result = matrix1 * matrix2;
    result.print();

    return 0;
}

这个示例中,矩阵类Matrix具有以下功能:

  • 构造函数:可以根据行数和列数构造一个零矩阵,也可以使用一个二维向量初始化矩阵。
  • getRows()getCols()函数:返回矩阵的行数和列数。
  • setValue()getValue()函数:设置和获取特定位置的元素值。
  • operator+()operator-()operator*()函数:实现矩阵的加法、减法和乘法运算,注意运算符两边的矩阵维度必须匹配。
  • transpose()函数:返回矩阵的转置矩阵。
  • print()函数:打印矩阵的元素。

main()函数中,我们使用了示例输入初始化了两个矩阵,然后计算了它们的乘积,并打印了结果。

这只是一个简单的示例,你可以根据需要进一步扩展该类,添加其他运算和功能函数

请仿照eigen库的矩阵实现设计一个矩阵类实现基本运算及功能函数

原文地址: https://www.cveoy.top/t/topic/ikSf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录