python - 求解线性方程组。使用 numpy 的三个变量

标签 python python-3.x numpy linear-algebra equation-solving

<分区>

我目前需要一个类,它必须能够显示和求解像这样的方程系统:

| 2x-4y+4z=8  |
| 34x+3y-z=30 |
| x+y+z=108   |

我认为编写一个类将方程系统的左侧事物转换为类似矩阵的对象是个好主意,这是该系统的自制矩阵:

/2  -4  4\
|34 3  -1|
\1  1   1/

我目前写的是:

class mymatrix(object):
    def __init__(self):
        o11 = None
        o12 = None
        o12 = None
        o21 = None
        o22 = None
        o23 = None
        o31 = None
        o32 = None
        o33 = None

    def set(row, column, value):
        string = 'o'+str(row)+str(column)+' = '+str(value)
        exec(string)

    def solve(self, listwithrightsidethings):
        #Here I want to solve the system. This code should read  the three    
        #values out of the list and solves the system It should return the
        #values for x, y and z in a tuple: (x, y, z)
        pass

我搜索了一个解决线性代数问题的模块,然后找到了 numpy。我在手册中进行了搜索,但没有找到完全适合我的问题的解决方案

如何编写 solve 函数?

编辑:

python应该这样解释

/o11, o21, o31\   123
|o21, o22, o32| = 456
\o31, o32, o33/   789

编辑:我想用恰好 3 个变量解决它,并将其作为 tuple

返回

最佳答案

你可以使用numpy.linalg.solve:

import numpy as np
a = np.array([[2, -4, 4], [34, 3, -1], [1, 1, 1]])
b = np.array([8, 30, 108])
x = np.linalg.solve(a, b)
print x # [ -2.17647059  53.54411765  56.63235294]

关于python - 求解线性方程组。使用 numpy 的三个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36354807/

相关文章:

python - Django渲染无法找到实际存在的模板

python - 在 Py_BuildValue 中返回变量字符串

python-3.x - 如何在不使用 Pandas 的情况下创建相当于 numpy.nan 的日期时间对象?

python - 检查所有列表元素是否相同的代码返回 IndexError?

python - 在Python中将月份和年份的列一起变成季度和年份的列

python - 使用Python对象创建表

python - 小部件中的 tkinter 默认按钮

python-3.x - 在嵌套函数中,如何让当前函数只粘附前一个函数的返回值?

Python:将 numpy 数组保存到 ROOT 文件

numpy - 检查一个 numpy 数组是否是一个 numpy 掩码数组