学习笔记:矩阵的基本运算的实现

2017-09-05 21:33:33

writer:pprp

昨天开始就上课了,没有整天整天的时间去编代码了,充分抓住每天晚上的时间吧,

今天下午预习了一下线性代数中矩阵最基本的运算,今晚就打算实现一下基本的功能

矩阵加法,矩阵减法,矩阵乘法,矩阵转置

代码如下:

/*
@theme:矩阵类的实现
@writer:pprp
@begin:20:48
@end:21:30
@declare:注意行还有列的赋值
@date:2017/9/5
*/

#include <bits/stdc++.h>

using namespace std;
#define N 100
int MAXN, n, mod;

class Matrix
{
public:
    int data[N][N];
    int col, row;
    void Init()
    {
        memset(data,0,sizeof(data));
    }
    //test:ok
    void MatrixMultiply(const Matrix& A, const Matrix & B)
    {
        if(A.col != B.row)
        {
            cout << "A.col != B.row" << endl;
            return ;
        }
        Init();
        row = A.row, col = B.col; //更新乘法得到的矩阵的行和列
        for(int i = 0 ; i < A.row ; i++)
        {
            for(int j = 0 ; j < B.col; j++)
            {
                data[i][j] = 0;
                for(int k = 0 ; k < A.col; k++)
                {
                    data[i][j] += A.data[i][k] * B.data[k][j];
                }
            }
        }
    }
    //test:ok
    void MatrixAdd(const Matrix & A,const Matrix & B)
    {
        if(A.row != B.row || A.col != B.col)
        {
            cout << "can‘t add these two Matrix!" << endl;
            return ;
        }
        Init();
        row = B.row, col = B.col;
        for(int i = 0 ; i < row; i++)
        {
            for(int j = 0; j < col; j++)
            {
                data[i][j] = A.data[i][j] + B.data[i][j];
            }
        }
    }
    //test:ok
    void MatrixSub(const Matrix & A, const Matrix & B)
    {
        if(A.row != B.row || A.col != B.row)
        {
            cout << "can‘t sub the two Matrix!" << endl;
            return;
        }
        row = B.row, col = A.col;
        for(int i = 0 ; i < row; i++)
        {
            for(int j = 0 ; j < col; j++)
            {
                data[i][j] = A.data[i][j] - B.data[i][j];
            }
        }
    }
    //test:ok
    void MatrixReverse(const Matrix & A)
    {
        row = A.col, col = A.row;
        Init();
        for(int i = 0 ; i < row; i++)
        {
            for(int j = 0 ; j < col; j++)
            {
                data[i][j] = A.data[j][i];
            }
        }
    }
    //test:ok
    void OutputMatrix()const
    {
        for(int i = 0 ; i < row; i++)
        {
            for(int j = 0 ; j < col; j++)
            {
                cout << data[i][j] << " ";
            }
            cout << endl;
        }
    }
    //test:ok
    void SetRC(int r,int c)
    {
        row = r, col = c;
    }
    //test:ok
    void InputMatrix()
    {
        Init();
        for(int i = 0 ; i < row; i++)
        {
            for(int j = 0; j < col; j++)
            {
                cin >> data[i][j];
            }
        }
    }
};

int main()
{
    freopen("in.txt","r",stdin);
    Matrix mx1, mx2, ans;
    //第一个矩阵
    cout << "input the row and the col of the first Matrix" << endl;
    int row, col;
    cin >> row >> col;
    mx1.SetRC(row,col);
    cout << "input the first Matrix" << endl;
    mx1.InputMatrix();
    mx1.OutputMatrix();

    //第二个矩阵
    cout << "input the row and the col of the second Matrix" << endl;
    cin >> row >> col;
    mx2.SetRC(row,col);
    cout << "input the second Matrix" << endl;
    mx2.InputMatrix();
    mx2.OutputMatrix();
/*
    cout << "add:" << endl;
    ans.MatrixAdd(mx1,mx2);
    ans.OutputMatrix();

    cout << "sub:" << endl;
    ans.MatrixSub(mx1,mx2);
    ans.OutputMatrix();

    cout << "reverse:" << endl;
    ans.MatrixReverse(mx2);
    ans.OutputMatrix();

*/
    cout << "multiply: " << endl;
    ans.MatrixMultiply(mx1,mx2);
    ans.OutputMatrix();
    return 0;
}

/*
test:

2 3
1 2 3
4 5 6
3 3
1 -1 2
0 1 1
-1 1 -1
*/
时间: 2024-08-08 14:38:38

学习笔记:矩阵的基本运算的实现的相关文章

学习笔记——矩阵(1) 02.15

在这种作业极少的美好夜晚里 听着轻音乐看看书学学东西再和心爱的姑娘说上几句 真是再美好不过了 由 m × n 个数aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵.记作: 这m×n 个数称为矩阵A的元素,简称为元,数aij位于矩阵A的第i行第j列,称为矩阵A的(i,j)元,以数 aij为(i,j)元的矩阵可记为(aij)或(aij)m × n,m×n矩阵A也记作Amn. 加法 矩阵的加法满足下列运算律(A,B,C都是同型矩阵): 应该注意的是只有同型矩阵之间才可以进行加法 减法

学习笔记::矩阵树定理

我很懒惰,没有理解 是这样做的 先计算每个点的度数 a[i][j]=i到j边数*-1 进行高斯消元 最后把对角线乘起来就是答案 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; #define eps 1e-9 #define N 20 int n,m; int d[N][N],c[N][N]; double a[N][N];

JAVA学习笔记-矩阵相加

package MyDuoWeiShuZu; public class Mycode { public static void show(int[][] c){ for(int j=0;j<c.length;j++){ for(int i=0;i<c.length;i++){ System.out.print(c[j][i]+"\t"); } System.out.println(); } } public static int[][] add(int[][] a,int[

算法学习笔记 递归之 快速幂、斐波那契矩阵加速

递归的定义 原文地址为:http://blog.csdn.net/thisinnocence 递归和迭代是编程中最为常用的基本技巧,而且递归常常比迭代更为简洁和强大.它的定义就是:直接或间接调用自身.经典问题有:幂运算.阶乘.组合数.斐波那契数列.汉诺塔等.其算法思想: 原问题可分解子问题(必要条件): 原与分解后的子问题相似(递归方程): 分解次数有限(子问题有穷): 最终问题可直接解决(递归边界): 对于递归的应用与优化,直接递归时要预估时空复杂度,以免出现用时过长或者栈溢出.优化递归就是以

《学习opencv》笔记——矩阵和图像操作——cvGEMM,cvGetCol,cvGetCols and cvGetDiag

矩阵和图像的操作 (1)cvGEMM函数 其结构 double cvGEMM(//矩阵的广义乘法运算 const CvArr* src1,//乘数矩阵 const CvArr* src2,//乘数矩阵 double alpha,//1号矩阵系数 const CvArr* src3,//加权矩阵 double beta,//2号矩阵系数 CvArr* dst,//结果矩阵 int tABC = 0//变换标记 ); tABC变换标记及其对应的含义 CV_GEMM_A_T 转置 src1 CV_GE

《学习opencv》笔记——矩阵和图像操作——cvDet,cvDit,cvDotProduct,cvEigenVV and cvFlip

矩阵和图像的操作 (1)cvDet函数 其结构 double cvDet(//计算矩阵的行列式 const CvArr* mat ); 实例代码 #include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main() { double va[] = {1,0,0,0,2,0,0,0,3}; CvMat Va=cvMa

《学习opencv》笔记——矩阵和图像操作——cvSetIdentity,cvSolve,cvSplit,cvSub,cvSubS and cvSubRS

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

《学习opencv》笔记——矩阵和图像操作——cvAdd、cvAddS and cvAddWeighted

矩阵和图像的操作 (1)cvAdd函数 其结构 void cvAdd(//图像加和 const CvArr* src1,//第一个原矩阵 const CvArr* src2,//第二个原矩阵 CvArr* dst, //存放矩阵 const CvArr* mask = NULL: //控制点 ); 就是单纯的将两个图像加和,mask变量控制加和的元素点,相当于"开关的作用"; 程序实例 #include <cv.h> #include <highgui.h> #

《学习opencv》笔记——矩阵和图像操作——cvCrossProduct and cvCvtColor

矩阵和图像的操作 (1)cvCrossProduct函数 其结构 void cvCrossProdust(//计算两个三维向量的叉积 const CvArr* src1, const CvArr* src2, CvArr* dst ); 实例代码 #include <cv.h> #include <highgui.h> #include <stdio.h> #include <iostream> using namespace std; int main()

《学习opencv》笔记——矩阵和图像操作——cvCalcCovarMatrix,cvCmp and cvCmpS

矩阵和图像的操作 (1)cvCalcCovarMatrix函数 其结构 void cvCalcCovarMatrix(计算给定点的均值和协方差矩阵 const CvArr** vects,//给定向量 int count,//给定向量的组数 CvArr* cov_mat,//结果矩阵 CvArr* avg,//根据flag得到结果 int flags//标记位 ); 标记位参数值极其意义 标志参数的具体标志值 意义 CV_COVAR_NORMAL 计算均值和协方差 CV_COVAR__SCRAM