求解同情线性方程组

时间:2022-05-12 12:37:36

I'm learning to use sympy.solvers.solveset.linsolve to solve a system of linear equations. I can solve the system all right, but I'm having trouble getting the results assigned to my variables. It doesn't seem to do the assignment automatically, as I would expect, but returns a one-element set set containing a tuple of the values, and I have to go through some silly contortions to get the values assigned. Here's my code:

我正在学习使用sympy.solvers.solveset.linsolve来解决线性方程组。我可以很好地解决系统,但是我无法将结果分配给我的变量。它似乎没有像我期望的那样自动进行赋值,但返回包含值元组的单元素集集,并且我必须通过一些愚蠢的扭曲来获取赋值。这是我的代码:

from sympy import symbols, ones
from sympy.solvers.solveset import linsolve
A= ones(6)
A[0,0]=7
for k in range(1,6):
    A[k,k]=6
b=6*ones(6,1)
p1,p2,p3,p4,p5,p6 = symbols('p1 p2 p3 p4 p5 p6')
system = (A,b)
p=linsolve(system,p1,p2,p3,p4,p5,p6)
p1,p2,p3,p4,p5,p6=tuple(p)[0]

Surely there's a more pythonic way of doing this, isn't there?

当然有更多的pythonic方式这样做,不是吗?

1 个解决方案

#1


1  

Returning a set is a central idea of solveset module (to which `linsolve' belongs). See Why do we use Sets as an output type? which includes "Why not use dicts as output?". Yes, it's awkward to deal with: one of open SymPy issues is The use of sets in solveset makes it very clumsy to get a dictionary of solutions.

返回集合是solveset模块(linsolve'所属的)的核心思想。请参阅为什么我们使用集合作为输出类型?其中包括“为什么不使用dicts作为输出?”。是的,处理起来很尴尬:一个开放的SymPy问题是在solveset中使用集合使得获取解决方案字典非常笨拙。

I usually apply next(iter(...)) to the output of solveset and its analogues.

我通常将next(iter(...))应用于solveset及其类似物的输出。

Your code could be simplified by not listing p1,...p6 individually, keeping it as a tuple of symbols.

您可以通过不单独列出p1,... p6来简化代码,将其保留为符号元组。

syms = symbols('p1:7')
system = (A, b)
p = linsolve(system, syms)
p = next(iter(p))

Now p is the tuple (6/13, 36/65, 36/65, 36/65, 36/65, 36/65).

现在p是元组(6 / 13,36 / 65,36 / 65,36 / 65,36 / 65,36 / 65)。

The older solve method can return a dict directly, but I would not recommend using it to handle a system of linear equations. Besides, SymPy development plan is to transition toward solveset, leaving solve behind.

较旧的求解方法可以直接返回dict,但我不建议使用它来处理线性方程组。此外,SymPy的发展计划是向解决方案过渡,留下解决方案。

#1


1  

Returning a set is a central idea of solveset module (to which `linsolve' belongs). See Why do we use Sets as an output type? which includes "Why not use dicts as output?". Yes, it's awkward to deal with: one of open SymPy issues is The use of sets in solveset makes it very clumsy to get a dictionary of solutions.

返回集合是solveset模块(linsolve'所属的)的核心思想。请参阅为什么我们使用集合作为输出类型?其中包括“为什么不使用dicts作为输出?”。是的,处理起来很尴尬:一个开放的SymPy问题是在solveset中使用集合使得获取解决方案字典非常笨拙。

I usually apply next(iter(...)) to the output of solveset and its analogues.

我通常将next(iter(...))应用于solveset及其类似物的输出。

Your code could be simplified by not listing p1,...p6 individually, keeping it as a tuple of symbols.

您可以通过不单独列出p1,... p6来简化代码,将其保留为符号元组。

syms = symbols('p1:7')
system = (A, b)
p = linsolve(system, syms)
p = next(iter(p))

Now p is the tuple (6/13, 36/65, 36/65, 36/65, 36/65, 36/65).

现在p是元组(6 / 13,36 / 65,36 / 65,36 / 65,36 / 65,36 / 65)。

The older solve method can return a dict directly, but I would not recommend using it to handle a system of linear equations. Besides, SymPy development plan is to transition toward solveset, leaving solve behind.

较旧的求解方法可以直接返回dict,但我不建议使用它来处理线性方程组。此外,SymPy的发展计划是向解决方案过渡,留下解决方案。