python smypy/matplotlib输入输出,带有符号方程



此程序用于绘制形状图(在I/O问题解决后(。

我使用了之前编写的程序中的模板来制作这个,但我似乎在这里缺少了一些东西。我试着浏览了这个程序并进行了故障排除,但一直没能弄清楚。打印报表纯粹是我在排除故障方面所做的努力。

预期的功能是为函数提供一个numpy数组,它将返回适当的X和Y值,也就是数组。

import sympy as sy
import numpy as np
def rose_func(thetarange):
_theta = thetarange
r, x, y, theta = sy.symbols('r, x, y, theta')
rose_eq = ((((x ** 2 + y ** 2) ** sy.Rational(7 / 2)) + 6 * (
3 * (x ** 5 * y + x * y ** 5) - 10 * x ** 3 * y ** 3)) / (x ** 2 + y ** 2) ** 3) - 1
sub_rose = rose_eq.subs([(x, r * sy.cos(thetarange)),
(y, r * sy.sin(thetarange))])
print("subbed eq:", sub_rose)
print("subbed eq:", sy.latex(sub_rose))
print()
simp_rose = sy.simplify(sub_rose)
print("simplified eq:", simp_rose)
print("simplified eq:", sy.latex(simp_rose))
print()
solv_rose = sy.solve(simp_rose, r)
print("Solved eq:", solv_rose)
print("Solved eq:", sy.latex(solv_rose))
print()
X = sy.lambdify('r', solv_rose*(int(sy.cos(_theta))), "numpy")  #may need to multiply by r(theta) or theta again
Y = sy.lambdify('r', solv_rose*(int(sy.sin(_theta))), "numpy")  #may need to multiply by r(theta) or theta again
return X(_theta), Y(_theta)
thetavar = np.linspace(-3.14 * 2, 3.14 * 2, 250)
rose_func(thetavar)

工作"模板"我基于这个程序:

def mac_ser(x, N):
_x = x
n, z, x = sy.symbols('n, z, x')
my_equation = sy.summation((z ** n / sy.factorial(n)), (n, 0, N))
sub_eq = my_equation.subs([(z, -x ** 2)])
mac_lam = (sy.lambdify('x', sub_eq, "numpy"))
print("Mac test", mac_lam)
print("Type is:", type(mac_lam(_x)))
return (mac_lam(_x))
x = np.linspace(-2, 2, 1000)
test = mac_ser(x, 2)

以下是我当前的错误:

File "C:Users...venvlibsite-packagessympycorecache.py", line 94, in wrapper
retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Users...venvlibsite-packagessympycorecache.py", line 94, in wrapper
retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/.../Roaming/JetBrains/PyCharmCE2020.2/scratches/scratch_21.py", line 51, in <module>
rose_func(thetavar)
File "C:/Users/.../Roaming/JetBrains/PyCharmCE2020.2/scratches/scratch_21.py", line 15, in rose_func
sub_rose = rose_eq.subs([(x, r * sy.cos(thetarange)),
File "C:Users...venvlibsite-packagessympycorecache.py", line 96, in wrapper
retval = func(*args, **kwargs)
File "C:Users...venvlibsite-packagessympycorefunction.py", line 465, in __new__
result = super().__new__(cls, *args, **options)
File "C:Users...venvlibsite-packagessympycorecache.py", line 96, in wrapper
retval = func(*args, **kwargs)
File "C:Users...venvlibsite-packagessympycorefunction.py", line 280, in __new__
evaluated = cls.eval(*args)
File "C:Users...venvlibsite-packagessympyfunctionselementarytrigonometric.py", line 570, in eval
if arg.could_extract_minus_sign():
AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'could_extract_minus_sign'```

人们很难同时使用numpy和sympy,这似乎是一种常见的情况。请记住,numpy不知道sympy的存在,sympy不喜欢基于numpy的输入。除非你是这两方面的专家,否则请不要试图将它们互换使用。请使用标记sympy查看关于堆栈溢出的每三个问题,以了解类似的问题并获得理解。

我发现你的代码有3个主要问题:

  1. sympy函数lieksy.sin不接受numpy数组。您必须为它提供纯python对象(如int(或sympy对象
  2. sy.solve在您的案例中返回2个解决方案。您需要通过执行类似sy.solve(...)[0]的操作来选择其中一个,以获得第一个解决方案
  3. sy.lambdify我认为用字符串作为它的第一个参数是不舒服的。使用您在开始时创建的符号。它也是theta的函数,而不是r的函数

以下是更正,我已经清楚地划分了sympy部分和numpy部分。

import numpy as np
def generate_rose_func():
"""
Generates the lambdifed expressions.
No numpy allowed in this function.
No sympy allowed out of this function.
"""
import sympy as sy
r, x, y, theta = sy.symbols('r, x, y, theta')
rose_eq = ((((x ** 2 + y ** 2) ** sy.Rational(7 / 2)) + 6 * (
3 * (x ** 5 * y + x * y ** 5) - 10 * x ** 3 * y ** 3)) / (x ** 2 + y ** 2) ** 3) - 1
sub_rose = rose_eq.subs([(x, r * sy.cos(theta)),
(y, r * sy.sin(theta))])
print("subbed eq:", sub_rose)
print("subbed eq:", sy.latex(sub_rose))
print()
simp_rose = sy.simplify(sub_rose)
print("simplified eq:", simp_rose)
print("simplified eq:", sy.latex(simp_rose))
print()
solv_rose = sy.solve(simp_rose, r)[0]  # solve returns 2 solutions in this case. Pick the first one
print("Solved eq:", solv_rose)
print("Solved eq:", sy.latex(solv_rose))
print()
X = sy.lambdify(theta, solv_rose * sy.cos(theta), "numpy")  # may need to multiply by r(theta) or theta again
Y = sy.lambdify(theta, solv_rose * sy.sin(theta), "numpy")  # may need to multiply by r(theta) or theta again
return X, Y
def rose_func(thetarange):
X, Y = generate_rose_func()
return X(thetarange), Y(thetarange)
thetavar = np.linspace(-3.14 * 2, 3.14 * 2, 250)
print(rose_func(thetavar))

最新更新