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

yipeiwu_com5年前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设计】。

相关文章

python生成指定长度的随机数密码

复制代码 代码如下:#!/usr/bin/env python# -*- coding:utf-8 -*- #导入random和string模块import random, string...

浅析python中的迭代与迭代对象

什么是python的迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration)。 (在Python中,迭代是...

Python匿名函数及应用示例

本文实例讲述了Python匿名函数及应用。分享给大家供大家参考,具体如下: lambda关键词能创建?型匿名函数。这种函数得名于省略了def声明函数的标准步骤。 代码如下:...

CentOS6.9 Python环境配置(python2.7、pip、virtualenv)

python2.7 yum install -y zlib zlib-devel openssl openssl-devel mysql-devel gcc gcc-c++ wget...

使用python分析git log日志示例

用git来管理工程的开发,git log是非常有用的‘历史'资料,需求就是来自这里,我们希望能对git log有一个定制性强的过滤。此段脚本就是在完成这种类型的任务。对于一个repo所有...