python/sympy求解矩阵方程的方法

yipeiwu_com6年前Python基础

sympy版本:1.2

假设求解矩阵方程

AX=A+2X

其中

python sympy求解矩阵方程

求解之前对矩阵方程化简为

(A−2E)X=A

B=(A−2E)

使用qtconsole输入下面程序进行求解

In [26]: from sympy import *

In [27]: from sympy.abc import *

In [28]: A=Matrix([[4,2,3],[1,1,0],[-1,2,3]])

In [29]: A
Out[29]: 
Matrix([
[ 4, 2, 3],
[ 1, 1, 0],
[-1, 2, 3]])

In [30]: B=A-2*diag(1,1,1)

In [31]: B
Out[31]: 
Matrix([
[ 2, 2, 3],
[ 1, -1, 0],
[-1, 2, 1]])

In [32]: B.inv()*A
Out[32]: 
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])

将结果验证一下:

In [38]: X=B.inv()*A

In [39]: X
Out[39]: 
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])

In [40]: A*X-A-2*X
Out[40]: 
Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

求解矩阵方程过程中注意的问题是左乘还是右乘问题,在此例中是B.inv()*A ,如果矩阵方程变为

XA=A+2X

那么求解结果为:

In [35]: X=A*B.inv()

In [36]: X
Out[36]: 
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])

将结果验证一下:

X=A*B.inv()

X
Out[36]: 
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])

X*A-A-2*X
Out[37]: 
Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])

以上这篇python/sympy求解矩阵方程的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3连接SQLServer、Oracle、MySql的方法

环境: python3.4 64bit pycharm2018社区版 64bit Oracle 11 64bit SQLServer· Mysql 其中三种不同的数据库安装在不同的服务器...

Python 3.x 判断 dict 是否包含某键值的实例讲解

查询资料得 Python 可以使用两种方式判断字典是否包含某键值 1、(dict.has_key('keyname')) 2、('keyname' in dict) 觉得第二种方式太过丑...

python自定义类并使用的方法

本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下: class Person: def __init__(self, first, middle,...

pytorch中交叉熵损失(nn.CrossEntropyLoss())的计算过程详解

pytorch中交叉熵损失(nn.CrossEntropyLoss())的计算过程详解

公式 首先需要了解CrossEntropyLoss的计算过程,交叉熵的函数是这样的: 其中,其中yi表示真实的分类结果。这里只给出公式,关于CrossEntropyLoss的其他详细细...

matplotlib.pyplot画图 图片的二进制流的获取方法

有些时候,我们需要画图后的二进制数据流,matplotlib没有提供相关的api,通过源码查看与百度,得到下面此方法 import matplotlib.pyplot as plt...