sympy

How to solve symbolically a pair of nonlinear equations using Python (sympy)?

不羁岁月 提交于 2019-12-12 02:07:50
问题 I have the following code for a system of three nonlinear equations with three unknowns: import sympy as sp from sympy import symbols, cos, sin v0, a0, f0 = symbols('v0 a0 f0') v1, a1, f1 = symbols('v1 a1 f1') w, t = symbols('w t') g1 = v0 + a0 * w * cos(w*t + f0) - v1 - a1 * w * cos(f1) g2 = v0**2 + a0**2*w**2 -v1**2 - a1**2*w**2 g3 = a0 * sin(w*t + f0) - a1*sin(f1) sp.solvers.solve((g1,g2,g3), (a1,v1,f1)) The system of equations looks very complicated but actually it is easily solved with

sympy.utilities.iterables.combinations() with replacement?

北慕城南 提交于 2019-12-12 01:26:50
问题 I'm experimenting with the sympy api for combinations. First, combinations without replacement ... from sympy.functions.combinatorial.numbers import nC from sympy.utilities.iterables import combinations nC('abc', 2) # >>> 3 list(combinations('abc', 2)) # >>> [('a', 'b'), ('a', 'c'), ('b', 'c')] I would now like to list the combinations with replacement nC('abc', 2, replacement=True) # >>> 6 However, the combinations() method doesn't seem to support a ' replacements ' argument? Init signature:

Interpreting a Python function from JSON data

▼魔方 西西 提交于 2019-12-12 00:49:55
问题 I am trying to determine the equivalence of two simple functions passed to python via json like so: PHP: $data = array("2*x", "x*2"); $result = shell_exec('python /path/check.py ' . escapeshellarg(json_encode($data))); Python: import sys, json from sympy import * try: data = json.loads(sys.argv[1]) except: sys.exit(1) x = Symbol('x') response = data[0] answer = data[1] result = response==answer print json.dumps(result) My assumption is that result returns false because the response and answer

In SymPy, why is my solution (nonlinsolve) returning the wrong answer?

一曲冷凌霜 提交于 2019-12-11 18:27:37
问题 I have a system of 3 equations, and I'd like to find a solution for the line of intersection, or the nullcline, of dx=dy . from sympy import * x, y, z = symbols('x, y, z') dx = x - x ** 3 / 3 - z + y dy = -y ** 2 * 0.1 + z dz = 0 xy_nullcline = nonlinsolve([dx, dy], [x, y, z]) print(xy_nullcline) # { # (x, -3.16227766016838*sqrt(z), z), # (x, 3.16227766016838*sqrt(z), z) # } In the image below, axes are x, y, z and: orange curve is the dx nullcline, (x,y,z), where dx=0 , a cubic polynomial

Exponential regression function Python

别来无恙 提交于 2019-12-11 18:22:54
问题 I am trying to implement a exponential regression function. sp stands for sympy. I use numpy and sympy. Firstly, in func_exp I tried to use np.exp but it generated an error (attribute error), so I decided to use sympy instead. Well, this is the code import numpy as np from numpy.linalg import matrix_rank import scipy import scipy.integrate import random import matplotlib.pyplot as plt import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D from sympy import integrate import sympy as

How to split a sum into its parts without converting to string in python

纵饮孤独 提交于 2019-12-11 17:08:05
问题 Suppose I have some expression from sympy import * a,b,c,x,y = symbols('a c b x y') eq=a*x + b*x*y + c*y**2 that needs to be split into an array containing the monomials. The current solution I have is parts = str(eq).split(' + ') Then, I use the eval function on each element of the array parts to be interpreted as an expression. What could I do to split the multivariate polynomial into the monomial parts without first converting the expression to a string? 回答1: You can explore a sympy

How to assign properties to symbols in SymPy and have them in the same domain?

為{幸葍}努か 提交于 2019-12-11 17:05:47
问题 I want to extend the Symbols class in SymPy so that I can add a Boolean attribute. I’m able to accomplish this for a single symbol (see my question here, and also someone else’s question here). And the code to accomplish this is repeated below: from sympy.core.symbol import Symbol class State(Symbol): def __init__(self, name, boolean_attr): self.boolean_attr = boolean_attr super(State, self).__init__(name) However, the problem with this solution is that when I am defining a polynomial, or

Conversion of symbolic expression to numeric one for use in quad - use lambdify?

拟墨画扇 提交于 2019-12-11 15:29:45
问题 I want to convert an expression containing symbolic variables to a numeric one so that the expression may be subsequently used in an integration method 'quad'. import numpy import math as m import scipy import sympy #define constants gammaee = 5.55e-6 MJpsi = 3.096916 alphaem = 1/137 lambdasq = 0.09 Ca = 3 qOsq = 2 def qbarsq(qsq): return (qsq+MJpsi**2)/4 def xx(qbarsq, w): return 4*qbarsq/(4*qbarsq-MJpsi**2+w**2) from sympy import * x,NN,a,b,ktsq,qbarsq,w = symbols('x NN a b ktsq qbarsq w')

sympy: PolynomialError: cos(a) contains an element of the generators set

让人想犯罪 __ 提交于 2019-12-11 10:44:42
问题 While using sympy (current version) to solve an polynomial equation (polynom would be d² in this case): from sympy import solve_poly_system solve_poly_system(4*d**2*sin(a)**2*sin(b)/cos(b)**2 - d*cos(a) + 4, d**2*sin(a)**2*sin(b)/cos(b)**2 - d*cos(a) + 8, 3*d**2*sin(a)**2*sin(b)/cos(b)**2 - d*cos(a) + 3 ,d ,a, b) I receive the following error: PolynomialError: cos(a) contains an element of the generators set What is the exact meaning of this error message. And why does it specifically points

sympy simplifying fractional powers of imaginary number

喜夏-厌秋 提交于 2019-12-11 10:26:58
问题 Why doesn't -(-1)**(1/3) + (-1)**(2/3) reduce to -1? wolfram alpha knows it's -1 but sympy gamma only does a float approximation re(_) + I*im(_) produces a NegativeOne object, but none of the other simplification functions I tried did anything to it. 回答1: I'm assuming you really mean -(-1)**Rational(1, 3) + (-1)**Rational(2, 3) , as literally -(-1)**(1/3) + (-1)**(2/3) is all Python (no SymPy), and evaluates numerically. Most SymPy objects do not do any kind of nontrivial simplification