【问题标题】:How can I sum two complex matrix in C++? [closed]如何在 C++ 中对两个复杂矩阵求和? [关闭]
【发布时间】:2021-02-28 20:08:32
【问题描述】:

我有一个复数类,它的数据成员是 im(虚部)和 re(实部)。现在我想对来自类 Matrix(A 和 B)的 2 个矩阵求和,并将总和放入另一个矩阵(C)中。这是我尝试过的。有什么建议吗?

我得到的错误是

no match for 'operator[]' (operand types are 'Matrix' and 'int')
inline Matrix& operator+(Matrix& A, Matrix& B)
{

 
        Matrix *C = new Matrix;
        C->rows = A.rows;
        C->columns = A.columns;
        for(int i = 0; i <  A.rows; i++)
            for(int j = 0; j <  A.columns; j++)
                C[i][j] = A[i][j] + B[i][j];
    }
    return **C;
}

【问题讨论】:

  • 为什么要用new来创建矩阵?使用Matrix C;,并将返回类型更改为Matrix。参数类型也应更改为const Matrix &amp;。如果没有看到确切的错误消息和minimal reproducible example,很难说更多。
  • 回答编辑:错误意味着您没有为矩阵类提供重载的operator[],因此编译器不知道A[i][j](和其他)的含义。跨度>

标签: c++ oop matrix sum add


【解决方案1】:

在类中添加运算符“+”,如:

Matrix  Matrix::operator+(const Matrix &other){
    Matrix result = Matrix<T>(rows, other.columns);
    if (this->rows == other.columns && this->columns == other.rows) {
        for (int i = 0; i < this->rows; i++) {
            for (int j = 0; j < this->columns; j++) {
                result.n[i][j] = other.n[i][j] + this->n[i][j];
            }
        }
    }
        return result;
    }

和操作符'='复制结果

Matrix&  Matrix::operator=(const Matrix &other ){
        if(this->columns<=other.columns && this->rows<=other.rows){
            for(int i=0;i<this->rows;i++){
                for(int j=0;j<this->columns;j++){
                    this->n[i][j]=other.n[i][j];
                    }
                }
            return (*this);
        }else{
            printf("ERROR");
        }
}

【讨论】:

  • 您好,谢谢您的回答,但我不明白“n[i][j]”是什么以及它来自哪里,您能帮帮我吗?
  • @AleAlly n 它是类内的二维数组参数
【解决方案2】:

这是您可以使用的基本 Matrix 实现。
std::complex 或您拥有的任何类型兼容,只要它是可复制构造的(请参阅下面链接中的 main())。

template<class T>
struct Matrix {
    std::vector<T> data;
    size_t rows, cols;

    Matrix(size_t rows_, size_t cols_, T value=T()) 
    : data(rows_*cols_, value)
    , rows(rows_)
    , cols(cols_)
    {
    }

    Matrix() = default;

    Matrix(const Matrix& other) = default;
    Matrix& operator=(const Matrix& other) = default;

    Matrix(Matrix&& other) = default;
    Matrix& operator=(Matrix&& other) = default;
    
    T& operator()(size_t row, size_t col) {
        return data.at(flatIndex(row, col));
    }
    
    const T& operator()(size_t row, size_t col) const {
        return data.at(flatIndex(row, col));
    }

    Matrix operator+(const Matrix& other) const {
        assert(data.size() == other.data.size());
        assert(rows == other.rows);
        assert(cols == other.cols);

        Matrix sum = *this;
        for (size_t i=0; i<data.size(); i++) {
            sum.data.at(i) += other.data.at(i);
        }

        return sum;
    }

protected:
    size_t flatIndex(size_t row, size_t col) const {
        return row * cols + col;
    }
};

template<class T>
std::ostream& operator<<(std::ostream& os, const Matrix<T>& m) {
    for (size_t r = 0; r<m.rows; r++) {
        for (size_t c = 0; c<m.cols; c++) {
            os << m(r, c) << " ";
        }
        os << '\n';
    }
    return os;
}

https://godbolt.org/z/Gvx5ac

如果您确实需要高性能/安全矩阵,请考虑使用 LinAlg 库,例如 Eigen。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多