首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

编写一个矩阵程序,要求可以运算矩阵的加减乘除,帮帮忙。该怎么解决

2012-05-24 
编写一个矩阵程序,要求可以运算矩阵的加减乘除,帮帮忙。具体要求和题目一样,谢谢.....^_^[解决办法]线性代

编写一个矩阵程序,要求可以运算矩阵的加减乘除,帮帮忙。
具体要求和题目一样,谢谢.....^_^

[解决办法]
线性代数库,blas, lapack, intel mkl ....


开源,商业,各种 线性代数的库。 拿来主义就行了
[解决办法]
唉,哥给你写矩阵的加法和减法吧,乘法和除法自己去实现,都不难的(感到难就去翻线代),共楼主参考:

C/C++ code
#include <iostream>using namespace std;class Matrix{private:    double* data;    int row;    int column;public:    Matrix(int row, int column)    {        this->row = row;        this->column = column;        data = new double[row * column];//(double*)malloc(sizeof(double) * row * column);    }    double* get_data()    {        return data;    }    int get_row()    {        return row;    }    int get_column()    {        return column;    }    Matrix& ADD(Matrix& matrix)    {        if((row != matrix.get_row()) || (column != matrix.get_column()))        {            cout << "Fails to add two matrices with different row or column." << endl;        }        for(int i = 0; i < row; ++i)        {            for(int j = 0; j < column; ++j)            {                *(data + i * column + j) += *(matrix.get_data() + i * column + j);            }        }        return *this;    }    Matrix& MINUS(Matrix& matrix)    {        if((row != matrix.get_row()) || (column != matrix.get_column()))        {            cout << "Fails to minus a matrix with different row or column." << endl;        }        for(int i = 0; i < row; ++i)        {            for(int j = 0; j < column; ++j)            {                *(data + i * column + j) -= *(matrix.get_data() + i * column + j);            }        }        return *this;    }    void display()    {        for(int i = 0; i < row; ++i)        {            for(int j = 0; j < column; ++j)            {                cout << *(data + i * column + j) << "\t";            }            cout << endl;        }    }    ~Matrix()    {        //free(data);        delete[] data;    }};int main(int argc, char** argv){    Matrix a(2, 2);    for(int i = 0; i < 2; ++i)    {        for(int j = 0; j < 2; ++j)        {            *(a.get_data() + i * 2 + j) = 1 + i * 2 + j;        }    }    a.display();    cout << "============" << endl;    Matrix b(2, 2);    for(int i = 0; i < 2; ++i)    {        for(int j = 0; j < 2; ++j)        {            *(b.get_data() + i * 2 + j) = 2 + i * 2 + j;        }    }    b.display();    cout << "============" << endl;    //a.ADD(b);    //a.display();    //cout << "============" << endl;    b.MINUS(a);    b.display();    return 0;} 

热点排行