sympy

sympy unknown assignment

戏子无情 提交于 2020-03-28 06:39:13
问题 I'm trying to create a python program for solving equations: from sympy import symbols, Eq, solve list1 = ['x', 'y'] #<--- Letters of the user input list1[0] = symbols(list1[0]) #<--- Assignment of the unknown x list1[1] = symbols(list1[1]) #<--- Assignment of the unknown y eq1 = Eq(x*2 - 5*x + 6 + y, 0) #<--- Equation res = solve(eq1, dict=True) #<--- Solving print(res) I want assign a value to the first and the second object of the 'list1', in this case 'x = symbols('x')' and 'y = symbols(

Having a list and trying to make a polynomial in Python (symbolic calculation)

大憨熊 提交于 2020-03-26 05:19:02
问题 Suppose I have the following list: f=[1, 3, 4, 5, 3, 4, 5, 6, 6, 3, 3, 1, 1, 4, 2] I can count how many of each integers exists in the list using two methods. First: from collections import Counter Counter(f) #Counter({1: 3, 2: 1, 3: 4, 4: 3, 5: 2, 6: 2}) or second: [[x,f.count(x)] for x in set(f)] #[[1, 3], [2, 1], [3, 4], [4, 3], [5, 2], [6, 2]] the second method is preferable as it throws me with a list. Now I want to turn the output into polynomial where the first element of sublists

Having a list and trying to make a polynomial in Python (symbolic calculation)

那年仲夏 提交于 2020-03-26 05:18:02
问题 Suppose I have the following list: f=[1, 3, 4, 5, 3, 4, 5, 6, 6, 3, 3, 1, 1, 4, 2] I can count how many of each integers exists in the list using two methods. First: from collections import Counter Counter(f) #Counter({1: 3, 2: 1, 3: 4, 4: 3, 5: 2, 6: 2}) or second: [[x,f.count(x)] for x in set(f)] #[[1, 3], [2, 1], [3, 4], [4, 3], [5, 2], [6, 2]] the second method is preferable as it throws me with a list. Now I want to turn the output into polynomial where the first element of sublists

Run through the AST of an expression in sympy

别等时光非礼了梦想. 提交于 2020-03-26 03:50:45
问题 I'm using sympy to compute some higher order derivatives of a complicated function expression. I'd like to traverse the AST of the expression, e.g. go through the nodes depth first. How do I do that ? 回答1: A simple depth-first travel would be like this: from sympy import pi, sin from sympy.abc import a,x,y def depth_first_traverse(expr): for arg in expr.args: depth_first_traverse(arg, depth+1, new_marks+'+---', parent_ind=ind) if len(expr.args) == 0: # we reached a leaf of the tree pass # do

Best way to isolate one coefficient of a multivariate polynomial in sympy

那年仲夏 提交于 2020-03-18 04:24:08
问题 I have a multivariate polynomial (which in the general case many many variables) whose coefficients list some data that I need to read off, but it doesn't seem like sympy gives a good way to do this. The collect function seemed like the right idea, but when you use it with several variables, it doesn't actually give you the individual monomials, but rather strange groupings of monomials that depend on the order you listed the variables. Does anyone know of a way to do this? 回答1: The

使用python解方程组

无人久伴 提交于 2020-03-16 18:57:21
某厂面试归来,发现自己落伍了!>>> 知乎上有人列了个问题, a平方 + a三次方 = 392 ,之前用了凑数的办法,答案是 7 ,土办法,但最简单 今天有人回帖,用编程的方案解决,心血来潮,试了一把用python (使用了python 的sympy 库) from sympy import * from sympy.abc import x aa = solve([x**2 + x**3 -392], [x]) print(aa) 代码很简单,就是把等式做一下形变,把等号的右边编程 0 ,然后使用 sympy的 solve 函数进行求解 得到结果如下 [(7,), (-4 - 2*sqrt(10)*I,), (-4 + 2*sqrt(10)*I,)] 也就是这个方程有3个解,说明之前的凑数法有点武断了,7是正解,但只是正解之一 [手工捂脸] 于是又尝试了解方程组 x + y + z = 2 2x - y + z = -3 x + 2y + 3z = 5 原理差不多,第一步也需要作形变,将等式右边变成 0 ,测试代码如下 from sympy import * x, y , z = symbols("x y z") eq = [x + y + z -2 , 2*x - y + z + 3, x + 2*y + 3*z -5] result = linsolve(eq, [x, y,

'Float' object has no attribute 'sin'

孤街醉人 提交于 2020-03-06 09:54:20
问题 When I go to print my matrix, it prints an error that says 'Float' object has no attribute 'sin'. While coding in Python, I am attempting to print a matrix in symbolic form using sym.matrix but I am defining my matrix numerically. When I go to print my matrix, it prints an error that says 'Float' object has no attribute 'sin'. It appears that for some reason, my sin function is not being read or outputted properly. import numpy as np from scipy.integrate import quad from numpy import sin, cos

Define gradient and hessian function in Python

≯℡__Kan透↙ 提交于 2020-03-06 09:31:28
问题 I would like the compute the Gradient and Hessian of the following function with respect to the variables x and y . Anyone could help? Thanks a lot. I find a code relevant from github for calculation of Rosenbrock function. def objfun(x,y): return 10*(y-x**2)**2 + (1-x)**2 def gradient(x,y): return np.array([-40*x*y + 40*x**3 -2 + 2*x, 20*(y-x**2)]) def hessian(x,y): return np.array([[120*x*x - 40*y+2, -40*x],[-40*x, 20]]) Update: from sympy import symbols, hessian, Function, N x, y = symbols

Cannot resolve ModuleNotFoundError: No module named 'sympy'

大城市里の小女人 提交于 2020-03-05 03:05:02
问题 Can someone help me resolve the following issue? I'm getting extremely frustrated. I've installed Octave with the symbolic package before, but I forgot how I did it, and I'm getting very upset at myself currently. I've installed Anaconda, Python, and Octave and I keep getting the following error message: Symbolic pkg v2.8.0: Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'sympy' error: Python cannot import SymPy: have you installed

Cannot resolve ModuleNotFoundError: No module named 'sympy'

主宰稳场 提交于 2020-03-05 03:04:29
问题 Can someone help me resolve the following issue? I'm getting extremely frustrated. I've installed Octave with the symbolic package before, but I forgot how I did it, and I'm getting very upset at myself currently. I've installed Anaconda, Python, and Octave and I keep getting the following error message: Symbolic pkg v2.8.0: Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'sympy' error: Python cannot import SymPy: have you installed