sympy

sympy.solve() doesn't give one of the solutions with LambertW

僤鯓⒐⒋嵵緔 提交于 2019-12-10 09:44:24
问题 Background: I am trying to implement a function doing an inverse transform sampling. I use sympy for calculating CDF and getting its inverse function. While for some simple PDFs I get correct results, for a PDF which CDF's inverse function includes Lambert-W function, results are wrong. Example: Consider following example CDF: import sympy as sym y = sym.Symbol('y') cdf = (-y - 1) * sym.exp(-y) + 1 # derived from `pdf = x * sym.exp(-x)` sym.plot(cdf, (y, -1, 5)) Now calculating inverse of

SymPy cannot lambdify Product

自作多情 提交于 2019-12-10 07:46:54
问题 I'm using SymPy 1.0 and Python 2.7. I want to compute the sum of first 100 integer numbers: This code runs succesfully import sympy as sy from sympy.tensor import IndexedBase, Idx import numpy as np x = sy.IndexedBase('x') i = sy.symbols('i', cls=Idx) s = sy.Sum(x[i], (i, 0, 100)) s_lambda = sy.lambdify(sy.DeferredVector('x'), s, 'numpy') s_lambda(np.arange(101)) And gives 5050 as expected. But when I try to do the same with a Product instead of a Sum : import sympy as sy from sympy.tensor

Create a formal linear function in Sympy

帅比萌擦擦* 提交于 2019-12-10 03:05:28
问题 I have an expression in Sympy (like -M - n + x(n) ) and I would like to create a formal linear function, says f, and apply it to my expression, in order to get, after simplification: -f(M) - f(n) + f(x(n)) Is it possible to tell sympy that a property such as linearity is verified? A very hacky way to do it would be to apply the function f to every subexpression which is in a sum. For instance when given an expressions like the first one I gave, it would be nice to simply access the terms

Why doesn't my Rician in Sympy match the Rician in Scipy?

坚强是说给别人听的谎言 提交于 2019-12-09 23:50:24
问题 I've tried to create a Sympy continuous random variable with a Rician distribution. With thanks to help from an earlier question, it seems that the best approach is to subclass SingleContinuousDistribution . I've implemented a distribution that appears to be in agreement between Wikipedia and Scipy, however I am not getting the same results as Scipy. What follows is code that implements the random variable, extracts its symbolic distribution and converts it to a Numpy representation through

How to solve matrix equation with sympy?

风格不统一 提交于 2019-12-09 18:08:29
问题 In sympy, given a matrix equation M * x + N * y = 0 (or more complicated..) how to solve this for x? (M,N = matrices, x,y = vectors) I tried this with normal symbols, but obviously this failed. Using MatrixSymbol was not working as well. Is there some way to do it, or is sympy not capable of doing it? 回答1: As MRocklin noted, MatrixExpressions don't support this yet, but noncommutative symbols do: In [13]: M, N, x, y = symbols('M N x y', commutative=False) In [15]: solve(M*x + N*y, x) Out[15]:

How to do a symbolic taylor expansion of an unknown function $f(x)$ using sympy

纵饮孤独 提交于 2019-12-09 15:59:04
问题 In sage it is fairly easy to do a Taylor expansion of an unknown function f(x) , x = var('x') h = var('h') f = function('f',x) g1 = taylor(f,x,h,2) How can this be done in sympy? Update asmeurer points out that this is a feature which will be available soon in sympy from the pull request http://github.com/sympy/sympy/pull/1888. I installed the branch using pip, pip install -e git+git@github.com:renatocoutinho/sympy.git@897b#egg=sympy --upgrade However, when I try to calculate the series of f

Plot/Convert an expression coming from sympy: Taylor series with matplotlib

限于喜欢 提交于 2019-12-09 13:46:24
问题 i´am trying to plot the function sin(x)/x and a taylor approximation of it. i use python 3 and pyzo - the first plot works but i have problems converting the series coming from the sympy module to an numpy expression that would work. import numpy as np import matplotlib.pyplot as plt import sympy as sp from sympy.abc import x x = np.linspace(-10, 10, 100) y = np.sin(x)/x #first function plt.plot(x, y, 'k') #this is working fine ### this is a code that removes the "0(x**something)" part of the

How to plot grad(f(x,y))?

扶醉桌前 提交于 2019-12-09 13:36:43
问题 I want to calculate and plot a gradient of any scalar function of two variables. If you really want a concrete example, lets say f=x^2+y^2 where x goes from -10 to 10 and same for y. How do I calculate and plot grad(f)? The solution should be vector and I should see vector lines. I am new to python so please use simple words. EDIT: @Andras Deak: thank you for your post, i tried what you suggested and instead of your test function (fun=3*x^2-5*y^2) I used function that i defined as V(x,y);

Python3 - Sympy: expand products of trig functions

孤街醉人 提交于 2019-12-09 11:37:56
问题 I cannot find a way to have SymPy expand products like cos(a)*cos(b) into sum of trig functions of sum of angles. from sympy import * init_printing() wrf,wlo,t = symbols('\omega_RF \omega_LO t') c = cos(wrf*t)*cos(wlo*t) expand_trig(c) Keeps the product intact. simplify(c) or trigsimp(c) also do not give any alternative form. I would like to have cos(a)*cos(b) to be expanded to 1/2*(cos(a+b) + cos(a-b)) ...any hints? 回答1: Per the docstring, help(sympy.fu) , fu will try to minimize the

How to get a fast lambda function from an sympy expression in 3 dimensions?

扶醉桌前 提交于 2019-12-08 19:21:25
问题 I am using sympy to generate different expressions for cfd-simulations. Mostly these expressions are of the kind exp = f(x,y,z) for example f(x,y,z) = sin(x)*cos(y)*sin(z). To get values on a grid I use simpy.lambdify. For example: import numpy as np import sympy as sp from sympy.abc import x,y,z xg, yg, zg = np.mgrid[0:1:50*1j, 0:1:50*1j, 0:1:50*1j] f = sp.sin(x)*sp.cos(y)*sp.sin(z) lambda_f = sp.lambdify([x,y,z], f, "numpy") fn = lambda_f(xg, yg, zg) print fn This seems to work pretty good