sympy

Deliberately simplifying fractional exponents

血红的双手。 提交于 2019-12-11 03:51:02
问题 I have an expression involving fractional exponents that I want to make into a polynomial recognisable to sympy for solution. I could, if necessary, write the exponents using Rational but can't make that work. What can I do? >>> from sympy import * >>> var('d x') (d, x) >>> (0.125567*(d + 0.04) - d**2.25*(2.51327*d + 6.72929)).subs(d,x**4) 0.125567*x**4 - (2.51327*x**4 + 6.72929)*(x**4)**2.25 + 0.00502268 回答1: SymPy does not combine exponents unless it knows it is safe to do so. For complex

C#, IronPython, and Sympy: unicode_escape_decode() takes no arguments

谁都会走 提交于 2019-12-11 02:58:36
问题 I am trying to create a static method for simplifying mathematical expressions in sympy using IronPython from C#. I have already been able to test and confirm the execution of simple python statements from IronPython. The problem is when I attempt to run the code below, I get the following error: unicode_escape_decode() takes no arguments (1 given) I have looked online for an explanation and managed to find another user with the same problem but it doesn't seem to be resolved: Unicode_escape

Make all symbols commutative in a sympy expression

左心房为你撑大大i 提交于 2019-12-11 02:22:50
问题 Say you have a number of non commutative symbols within a sympy expression, something like a, c = sympy.symbols('a c', commutative=False) b = sympy.Symbol('b') expr = a * c + b * c What is the preferred way to make all symbols in the expression commutative, so that, for example, sympy.simplify(allcommutative(expr)) = c * (a + b) ? In this answer it is stated that there is no way to change the commutativity of a symbol after creation without replacing a symbol, but maybe there is an easy way

Printing the output rounded to 3 decimals in SymPy

北慕城南 提交于 2019-12-11 02:19:33
问题 I got a SymPy matrix M In [1]: from sympy import * In [2]: M = 1/10**6 * Matrix(([1, 10, 100], [1000, 10000, 100000])) In [3]: M Out[3]: Matrix([ [1.0e-6, 1.0e-5, 0.0001], [ 0.001, 0.01, 0.1]]) I want to print the output rounded to 3 decimals, as follows: In [3]: M Out[3]: Matrix([ [ 0.000, 0.000, 0.000], [ 0.001, 0.010, 0.100]]) In normal Python, I would go like this: In [5]: '{:.3f}'.format(1/10**6) Out[5]: '0.000' But how to do in a SymPy matrix? Moreover, a more general case would be an

Defining two random variables that depend on a single condition

半城伤御伤魂 提交于 2019-12-11 02:12:23
问题 In sympy, how can I define two random variables, X and Y, that depend on a common condition? For example, how do I solve a problem such as the following: We throw a dice. If it falls on 1, then X=1 and Y=0. If it falls on 2, then X=0 and Y=1. Otherwise, X=Y=0. What is the covariance of X,Y? 回答1: If X and Y are functions of some Z, then create Z and define X, Y through it. Piecewise helps with this: from sympy.stats import * Z = Die("Z", 6) X = Piecewise((1, Eq(Z, 1)), (0, True)) Y = Piecewise

Sympy fails to integrate the product of a piecewise continuous function and a complex function across the discontinuity

别说谁变了你拦得住时间么 提交于 2019-12-11 01:58:51
问题 If I do: from sympy import * x, L = symbols('x L', real=True) f = Piecewise((1, x<=0), (-1, x<1), (0, True)) g = exp(-x * 1j) integrate(f * g, (x, 0, L)) I get: Piecewise((1.0*I*exp(-1.0*I*L) - 1.0*I, L <= 0), (-1.0*I*exp(-1.0*I*L) + 1.0*I, L < 1), (-1.0*I*exp(-1.0*I) + 1.0*I, True)) But if I change the last line to: integrate(f*g, (x, L/2, L)) I get: Integral(Piecewise((exp(-1.0*I*x), x <= 0), (-exp(-1.0*I*x), x < 1), (0, True)), (x, L/2, L)) Any insight would be appreciated! 回答1: I guess it

Sympy autowrap (cython): defining “helpers” for sympy.Max, sympy.Heaviside

拥有回忆 提交于 2019-12-11 01:48:21
问题 I have a sympy.Matrix, called J_sym, that I would like to autowrap (preferably using the cython backend); the according symbols are stored in the list list_args. However, the problem I run into is that apparently some sympy functions are not supported, in my case specifically sympy.Max and sympy.Heaviside. Specifically, if I try J_num_autowrap = autowrap(J_sym, backend="cython", args=list_args) I get the following: [...] wrapped_code_10.c(4): warning C4013: 'Heaviside' undefined; assuming

get a list of factors in sympy.factor

强颜欢笑 提交于 2019-12-11 00:59:03
问题 I'm implementing an algorithm, and in it I need to get factor a polynomial f(x) = p(x)q(x) with p and q relatively prime. I can use, of course, sympy.factor, but I would like to construct a function that returns perhaps a list of factors in a sympy.factor result. For example, x = sympy.Symbol('x') y = sympy.Symbol('y') g = sympy.Poly(y**4 + x*y**3 + y**2 + x*y, y) sympy.factor(g) throws out y*(x + y)*(y**2 + 1) so perhaps I could build a parser that interprets the string and divides it

How to solve a differential equation in formal power series?

纵饮孤独 提交于 2019-12-11 00:33:29
问题 I would like to have first several coefficients of formal power series defined implicitly by a differential equation. Example. import sympy as sp sp.init_printing() # math as latex from IPython.display import display z = sp.Symbol('z') F = sp.Function('F')(z) F_ = sp.Derivative(F, z) equation = sp.Eq(F**2 + 1 - F_, 0) display(equation) solution = sp.dsolve(equation) display(solution) sp.series(sp.tan(z), n = 8) Question. How to compute formal power series solution of ODE without explicitly

Plotting a mixture distribution in sympy.stats

时光怂恿深爱的人放手 提交于 2019-12-10 23:54:39
问题 ( gist of this Q here ) I'd like create a mixture of two Gamma distributions and plot the result, evaluated over a given range. It would appear that sympy.stats is capable of this because it is able to compute the expectation of the mixture and sample from it. I'm quite new to sympy, so not sure if there is a preferred way for evaluating and plotting in this situation than the one I've been using. %matplotlib inline from matplotlib import pyplot as plt from sympy.stats import Gamma, E,