sympy

Converting hard integral to lambda function with lambdify

人走茶凉 提交于 2020-01-02 05:36:09
问题 I would like to lambdify the function Integral(t**t,(t,0,x)) . It works, but my new function, which was returned by lambdify , doesn't return a number but only sympy.integrals.integrals.Integral class. But I don't want that, I want it to return a float number. Here is my code: import sympy as sp import numpy as np f = sp.lambdify(x,sp.integrate(t**t,(t,0,x))) print(f(2)) #return Integral(t**t, (t, 0, 2)) #but i want 2.83387674524687 回答1: lambdify doesn't support scipy.integrate.quad directly

SymPy and square roots of complex numbers

╄→гoц情女王★ 提交于 2020-01-02 04:07:06
问题 When using solve to compute the roots of a quadratic equation, SymPy returns expressions which could be simplified but I can't get it to simplify them. A minimal example looks like so: from sympy import * sqrt(-24-70*I) Here, SymPy just returns sqrt(-24-70*I) while Mathematica or Maple will answer with the equivalent of 5-7*I . I'm aware that there are two square roots, but this behavior entails that SymPy will, for example, return pretty complicated solutions from z = symbols("z") solve(z **

Solving Systems of Equations with SymPy

亡梦爱人 提交于 2020-01-01 04:15:10
问题 I'm trying to solve a set of equations with SymPy 0.7.1: from sympy import * equations = [ Eq(S('vf'), S('vi + a*t')), Eq(S('d'), S('vi*t + 1/2*a*t**2')), Eq(S('a'), S('10')), Eq(S('d'), S('60')), Eq(S('vi'), S('5')) ] print solve(equations) produces the correct result, but in a strange order: [(-4, 10, 60, -35, 5), (3, 10, 60, 35, 5)] How can I identify which value fits which variable? The variable order seems arbitrary. The documentation suggests providing additional arguments: print solve

How can I get a list of the symbols in a sympy expression?

不羁岁月 提交于 2020-01-01 01:11:47
问题 For example, if I run import sympy x, y, z = sympy.symbols('x:z') f = sympy.exp(x + y) - sympy.sqrt(z) is there any method of f that I can use to get a list or tuple of sympy.Symbol objects that the expression contains? I'd rather not have to parse srepr(f) or parse downward through f.args . In this case, g.args[0].args[1].args[0] gives me Symbol("z") , while g.args[1].args[0].args gives me the tuple (Symbol("x"), Symbol("y")) , but obviously these are expression-specific. 回答1: You can use: f

How to find out if the Sympy variable is complex?

怎甘沉沦 提交于 2019-12-31 05:03:10
问题 I am writing a code which involves solving this equation X = solve(Theta_Mod_Eqn*Ramp_Equation/(x+PT) - C, x) I am using sympy library, now the equation has 7 roots few are complex and few are real. I am unable to segregate them because isinstance(i,complex) is always returning true for i in X: if not isinstance(i,complex): if (i>-0.01 and i<maxSheaveDisp): A = i; for one case i = -0.000581431210287302 - 0.2540334478167*I In:i == complex Out[39]: False How to find out if the variable is

Sympy Subs not replacing a symbol when its power is being replaced

走远了吗. 提交于 2019-12-31 04:08:29
问题 I'm trying to make some basic substitutions but SymPy doesn't want to help me out x, y, z, k = symbols("x y z k", positive=True, real=True) exp = x**4 + x**3 + x**2 + x what_im_expecting = simplify(y**(Rational(1/4)) + y**(Rational(3/4)) + sqrt(y) + y) what_i_actually_get = exp.subs(x**4,y) exp, what_i_actually_get, what_im_expecting returns x + y**(Rational(3, 4)) + sqrt(y) + y Can anyone help me out? a more complex example: 回答1: The method subs can be trusted to replace the terms that

sympy hangs when trying to solve a simple algebraic equation

二次信任 提交于 2019-12-31 02:14:15
问题 I recently reinstalled my python environment and a code that used to work very quickly now creeps at best (usually just hangs taking up more and more memory). The point at which the code hangs is: solve(exp(-alpha * x**2) - 0.01, alpha) I've been able to reproduce this problem with a fresh IPython 0.13.1 session: In [1]: from sympy import solve, Symbol, exp In [2]: x = 14.7296138519 In [3]: alpha = Symbol('alpha', real=True) In [4]: solve(exp(-alpha * x**2) - 0.01, alpha) this works for

Any way to solve a system of coupled differential equations in python?

我只是一个虾纸丫 提交于 2019-12-30 03:49:08
问题 I've been working with sympy and scipy, but can't find or figure out how to solve a system of coupled differential equations (non-linear, first-order). So is there any way to solve coupled differential equations? The equations are of the form: V11'(s) = -12*v12(s)**2 v22'(s) = 12*v12(s)**2 v12'(s) = 6*v11(s)*v12(s) - 6*v12(s)*v22(s) - 36*v12(s) with initial conditions for v11(s), v22(s), v12(s). 回答1: For the numerical solution of ODEs with scipy, see scipy.integrate.solve_ivp, scipy.integrate

Convert sympy expressions to function of numpy arrays

放肆的年华 提交于 2019-12-29 05:58:26
问题 I have a system of ODEs written in sympy: from sympy.parsing.sympy_parser import parse_expr xs = symbols('x1 x2') ks = symbols('k1 k2') strs = ['-k1 * x1**2 + k2 * x2', 'k1 * x1**2 - k2 * x2'] syms = [parse_expr(item) for item in strs] I would like to convert this into a vector valued function, accepting a 1D numpy array of the x value, a 1D numpy array of the k values, returning a 1D numpy array of the equations evaluated at those points. The signature would look something like this: import

sympy AttributeError: 'Pow' object has no attribute 'sin'

谁都会走 提交于 2019-12-25 17:17:17
问题 I have read this SO post which says namespace conflict is one reason for this error. I am falling to this error frequently. So, I'd like to learn what exactly is happening here? What is expected by the library? EDIT: fun = lambda x: 4*x*(np.sin(x**2) - 3)*np.cos(x**2) comes from a test case, so practically I am bound to use it as function 'fun'. Sorry for missing that information. Kindly discuss respecting this constraint. EDIT2: This is an error reproducing code, not the full script. Task is