一、矩阵的加减乘除

矩阵也存在加减乘除

矩阵就是填满数字的表格,一般用大写字母表示,关于矩阵很重要的一点是,它不是一个自然的概念,它是数值的一种表示方法,矩阵的运算也是人为约定的(人造的规则,完全可以采用不同的方法)

假设:

$$ A = \left[ \begin {matrix} a & b \\ c & d \end {matrix} \right] $$ ,$$ B = \left[ \begin {matrix} e & f \\ g & h \end {matrix} \right] $$$$ C = \left[ \begin {matrix} i & j & k \\ l & m & n \end {matrix} \right] $$$$ D = \left[ \begin {matrix} o & p \\ q & r \\ s & t \end {matrix} \right] $$

矩阵加法

$$ A + B = \left[ \begin {matrix} a+e & b+f \\ c+g & d+h \end {matrix} \right] $$

注:同位置相加,所以 A 无法加 C

矩阵减法

标量与矩阵相乘

$$ a \cdot B = a \cdot \left[ \begin {matrix} e & f \\ g & h \end {matrix} \right] = \left[ \begin {matrix} a \cdot e & a \cdot f \\ a \cdot g & a \cdot h \end {matrix} \right] $$

所以

$$ A - B = A + (-1) \cdot B = \left[ \begin {matrix} a & b \\ c & d \end {matrix} \right] + \left[ \begin {matrix} -e & -f \\ -g & -h \end {matrix} \right] = \left[ \begin {matrix} a-e & b-f \\ c-g & d-h \end {matrix} \right]$$

矩阵乘法

其实是行向量与列向量的点积

$$ A \cdot B = \left[ \begin {matrix} a \cdot e + b \cdot g & a \cdot f + b \cdot h \\ c \cdot e + d \cdot g & c \cdot f + d \cdot h \end {matrix} \right] $$

矩阵除法

方阵:行和列相同的矩阵称为方阵,A和B都可以称为方阵

对角矩阵:除反对角线外,其它数据都为0的方阵,称为对角矩阵,例如:

$$E=\left[\begin{matrix}3 & 0\\0 & 2\end{matrix}\right] \qquad or \qquad F=\left[\begin{matrix}2 & 0 & 0\\0 & 5 & 0\\0 & 0 & 3\end{matrix}\right]$$

单位矩阵:反对角线的数据全为1的对角矩阵,称为单位矩阵,例如:

$$I=\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right] \qquad or \qquad I^{'}=\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$

注:单位矩阵I与方阵A有个属性,I·A=A,A·I=A

常规数学中,

$$\frac{1}{a} \cdot a = 1$$

矩阵中,也存在类似的式子,

A^{-1} \cdot A = I

其中,A^{-1}为矩阵A的逆矩阵,矩阵世界中,单位元是单位矩阵

求矩阵的逆矩阵,就是矩阵除法

2x2矩阵的逆矩阵

假设

A=\left[\begin{matrix}a & b\\c & d\end{matrix}\right]

矩阵A的行列式为:

\left | A \right | = ad-bc

矩阵A的逆矩阵为:

A^{-1}=\frac{1}{\left | A \right |} \left [ \begin {matrix} d & -b \\ -c & a \end {matrix} \right ] = \frac{1}{ad - bc} \left [ \begin {matrix} d & -b \\ -c & a \end {matrix} \right ]

3x3矩阵的逆矩阵-方法1

求解过程较复杂,主要步骤为:

1. 从矩阵A求余子式

2. 从余子式求代数余子式

3. 从代数余子式求伴随矩阵

4. 从矩阵A或矩阵A和代数余子式求矩阵A的行列式

5. 从矩阵A的行列式和伴随矩阵求矩阵A的逆矩阵

下面为详细步骤:

假设

A = \left [ \begin{matrix} 1 & 0 & 1\\ 0 & 2 & 1\\ 1 & 1 & 1 \end{matrix} \right ]

1. 从矩阵A求余子式,等于矩阵A去掉某一数字元素所在的行和列后,剩余的数字元素形成的2x2矩阵的行列式:

matrix \ of \ minors = \begin{bmatrix} \begin{vmatrix} 2 & 1\\ 1 & 1 \end{vmatrix} & \begin{vmatrix} 0 & 1\\ 1 & 1 \end{vmatrix} & \begin{vmatrix} 0 & 2\\ 1 & 1 \end{vmatrix}\\ \begin{vmatrix} 0 & 1\\ 1 & 1 \end{vmatrix} & \begin{vmatrix} 1 & 1\\ 1 & 1 \end{vmatrix} & \begin{vmatrix} 1 & 0\\ 1 & 1 \end{vmatrix}\\ \begin{vmatrix} 0 & 1\\ 2 & 1 \end{vmatrix} & \begin{vmatrix} 1 & 1\\ 0 & 1 \end{vmatrix} & \begin{vmatrix} 1 & 0\\ 0 & 2 \end{vmatrix} \end{bmatrix} = \begin{bmatrix} 1 & -1 & -2\\ -1 & 0 & 1\\ -2 & 1 & 2 \end{bmatrix}

2. 从余子式求代数余子式,等于余子式与符号矩阵对应元素相乘,注意,不是行向量与列向量的点积:

符号矩阵固定为:

\begin{bmatrix} +1 & -1 & +1\\ -1 & +1 & -1\\ +1 & -1 & +1 \end{bmatrix}

因此,代数余子式cofactors为:

cofactors = \begin{bmatrix} 1*(+1) & (-1)*(-1) & (-2)*1\\ (-1)*(-1) & 0*1 & 1*(-1)\\ (-2)*1 & 1*(-1) & 2*1 \end{bmatrix} = \begin{vmatrix} 1 & 1 & -2\\ 1 & 0 & -1\\ -2 & -1 & 2 \end{vmatrix}

3. 从代数余子式求伴随矩阵,等于代数余子式沿反对角线转置,也就是行和列进行转换:

adjugate = \begin{bmatrix} 1 & 1 & -2\\ 1 & 0 & -1\\ -2 & -1 & 2 \end{bmatrix}

4. 从矩阵A和伴随矩阵求矩阵A的行列式,等于矩阵A中任意一行的元素与伴随矩阵相应行的元素,相乘然后相加:

假设我们选取矩阵A中的第二行元素,相应的,也会选取伴随矩阵中的第二行元素,

\left | A \right | = 0*1 \ + \ 2*0 \ + \ 1*(-1) = -1

5. 从矩阵A的行列式和伴随矩阵求矩阵A的逆矩阵,等于1除以矩阵A的行列式,然后再乘以伴随矩阵:

A^{-1} = \frac{1}{\left | A \right |}\cdot adjugate = \begin{bmatrix} -1 & -1 & 2\\ -1 & 0 & 1\\ 2 & 1 & -2 \end{bmatrix}

3x3矩阵的逆矩阵-方法2

该方法称为高斯消去法(Gauss-Jordan elimination,查看证明过程),主要步骤为:

1. 增广原矩阵

2. 基础行运算

下面为详细步骤:

假设

A=\begin{bmatrix} 1 & 0 & 1\\ 0 & 2 & 1\\ 1 & 1 & 1 \end{bmatrix}

1. 增广原矩阵,等于在原矩阵的右侧增加一个同等大小的单位矩阵:

augment \quad A = \left [ \begin{matrix} 1 & 0 & 1\\ 0 & 2 & 1\\ 1 & 1 & 1 \end{matrix} \left | \begin{matrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix} \right ]

2. 基础行运算,等于原始矩阵执行一堆行运算,相应的,在单位矩阵执行相同的行运算,直到原始矩阵变成单位矩阵,此时,原始单位矩阵就变成了原始矩阵的逆矩阵;行运算--行可以由原始行乘以任意数字来代替,也可以对任意两行进行交换,还可以用其他行加上或减去某一行,然后用结果代替原始行;下面开始基础行运算:

第三行减去第一行,然后用结果代替第三行,增广矩阵变为:

\left [ \begin{matrix} 1 & 0 & 1\\ 0 & 2 & 1\\ 0 & 1 & 0 \end{matrix} \left | \begin{matrix} 1 & 0 & 0\\ 0 & 1 & 0\\ -1 & 0 & 1 \end{matrix} \right ]

第三行与第二行进行交换:

\left [ \begin{matrix} 1 & 0 & 1\\ 0 & 1 & 0\\ 0 & 2 & 1 \end{matrix} \left | \begin{matrix} 1 & 0 & 0\\ -1 & 0 & 1\\ 0 & 1 & 0 \end{matrix} \right ]

第三行减去(第二行乘以2),然后用结果代替第三行:

\left [ \begin{matrix} 1 & 0 & 1\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix} \left | \begin{matrix} 1 & 0 & 0\\ -1 & 0 & 1\\ 2 & 1 & -2 \end{matrix} \right ]

第一行减去第三行,然后用结果代替第一行:

\left [ \begin{matrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix} \left | \begin{matrix} -1 & -1 & 2\\ -1 & 0 & 1\\ 2 & 1 & -2 \end{matrix} \right ]

此时,增广矩阵的左侧已经变成了单位矩阵,那么,增广矩阵的右侧就是原始矩阵的逆矩阵

注:高斯消去法也可以用来求解2x2矩阵的逆矩阵

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我来为您介绍一下C++中如何用类来实现矩阵加减乘除操作。 首先,我们需要定义一个矩阵类,可以包含矩阵的行列数和矩阵元素。下面是一个矩阵类的示例代码: ```c++ class Matrix { private: int rows; // 矩阵的行数 int cols; // 矩阵的列数 vector<vector<double>> data; // 矩阵元素 public: Matrix(int r, int c) : rows(r), cols(c), data(r, vector<double>(c, 0)) {}; // 构造函数,初始化矩阵元素为0 // 获取矩阵的行数和列数 int getRows() const { return rows; } int getCols() const { return cols; } // 获取矩阵中某个位置的元素 double getElem(int i, int j) const { return data[i][j]; } void setElem(int i, int j, double val) { data[i][j] = val; } // 矩阵加法 Matrix operator+(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw "两个矩阵的行列数不匹配"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] + other.data[i][j]; } } return res; } // 矩阵减法 Matrix operator-(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw "两个矩阵的行列数不匹配"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] - other.data[i][j]; } } return res; } // 矩阵乘法 Matrix operator*(const Matrix& other) const { if (cols != other.rows) { throw "两个矩阵的行列数不匹配"; } Matrix res(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) { res.data[i][j] += data[i][k] * other.data[k][j]; } } } return res; } // 矩阵除法 Matrix operator/(const Matrix& other) const { if (other.rows != other.cols || other.rows != 1) { throw "除数必须是1阶方阵"; } double div = other.data[0][0]; if (div == 0) { throw "除数不能为0"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] / div; } } return res; } }; ``` 在上面的代码中,我们定义了一个矩阵类Matrix,包含了矩阵的行列数和元素,以及加减乘除四种运算符重载。其中,矩阵的加减乘法比较基本,需要注意的是除法时,除数必须是1阶方阵,且不能为0。 接下来,我们可以通过以下方式来使用矩阵类: ```c++ // 定义一个2*3的矩阵a Matrix a(2, 3); a.setElem(0, 0, 1); a.setElem(0, 1, 2); a.setElem(0, 2, 3); a.setElem(1, 0, 4); a.setElem(1, 1, 5); a.setElem(1, 2, 6); // 定义一个3*2的矩阵b Matrix b(3, 2); b.setElem(0, 0, 7); b.setElem(0, 1, 8); b.setElem(1, 0, 9); b.setElem(1, 1, 10); b.setElem(2, 0, 11); b.setElem(2, 1, 12); // 矩阵加法 Matrix c = a + b; // 矩阵减法 Matrix d = a - b; // 矩阵乘法 Matrix e = a * b; // 矩阵除法 Matrix f = a / Matrix(1, 1, 2); // 将矩阵a中的每个元素除以2 ``` 以上就是用类来实现矩阵加减乘除的代码示例。希望能够对您有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值