sympy

Spherical coordinates plot in matplotlib

天大地大妈咪最大 提交于 2019-12-18 12:38:54
问题 R(teta, phi) = cos(phi^2), teta[0, 2*pi], phi[0,pi] How to draw a graph of this function (R(teta, phi)) in spherical coordinates with the help of matplotlib? The documentation I have not found Spherical coordinates. 回答1: The code below is very much like the 3D polar plot from the Matplotlib gallery. The only difference is that you use np.meshgrid to make 2D arrays for PHI and THETA instead of R and THETA (or what the 3D polar plot example calls P ). The moral of the story is that as long as X

TypeError: can't convert expression to float

大憨熊 提交于 2019-12-18 09:37:02
问题 I am a python newbie. I am trying to evaluate Planck equation using python. I have written a simple program. But when I give input to it is giving me an error. can anyone hep me where I am going wrong? Here is the program and the error: Program: from __future__ import division from sympy.physics.units import * from math import * import numpy from scipy.interpolate import interp1d #Planck's Law evaluation at a single wavelength and temperature def planck_law(wavelength,temperature): T

Get all positive integral solutions for a linear equation

那年仲夏 提交于 2019-12-18 08:48:39
问题 A game I played has a riddle that involves solving the following equation: x*411 + y*295 + z*161 = 3200 Not wanting to think I just slapped it into sympy , which I haven’t really used up to that point: >>> from sympy import * >>> x, y, z = symbols('x y z', integer=True, positive=True) >>> solve(x*411 + y*295 + z*161 - 3200, [x, y, z]) [{x: -295*y/411 - 161*z/411 + 3200/411}] Hmm, this only gave me a dependent solution, but I want all possible solutions in the domain I constrained the

How to evaluate the constants SymPy gives with initial condition?

让人想犯罪 __ 提交于 2019-12-18 05:12:43
问题 How can I evaluate the constants C1 and C2 from a solution of a differential equation SymPy gives me? There are the initial condition f(0)=0 and f(pi/2)=3. >>> from sympy import * >>> f = Function('f') >>> x = Symbol('x') >>> dsolve(f(x).diff(x,2)+f(x),f(x)) f(x) == C1*sin(x) + C2*cos(x) I tried some ics stuff but it's not working. Example: >>> dsolve(f(x).diff(x,2)+f(x),f(x), ics={f(0):0, f(pi/2):3}) f(x) == C1*sin(x) + C2*cos(x) By the way: C2 = 0 and C1 = 3. 回答1: There's a pull request

How to serialize sympy lambdified function?

南笙酒味 提交于 2019-12-18 04:50:11
问题 The title says it all. Is there any way to serialize a function generated by sympy.lambdify?: import sympy as sym import pickle import dill a, b = sym.symbols("a, b") expr = sym.sin(a) + sym.cos(b) lambdified_expr = sym.lambdify((a, b), expr, modules="numpy") pickle.dumps(lambdified_expr) # won't work dill.dumps(lambdified_expr) # won't work either ... The reason I want to do this is because my code generates so many lambdified functions but I found it takes too long every time. 回答1: You

Setting Assumptions on Variables in Sympy Relative to Other Variables

廉价感情. 提交于 2019-12-18 04:10:39
问题 I know that sympy in python can set assumptions on variables, such as x is positive, negative, real, complex, etc. I was wondering if sympy can set assumptions on variables relative to other variables. For example, if I have variables x and y, can I set sympy to assume that x > y in its solutions. Or, alternatively, if I have two variables, a and B, can I set sympy to assume that a + 2B < 1? These sorts of assumptions would possibly help sympy simplify complicated solutions to solve() and

How to pretty print in ipython notebook via sympy?

主宰稳场 提交于 2019-12-17 22:25:54
问题 I tried pprint , print , the former only prints Unicode version, and the latter doesn't do pretty prints. from sympy import symbols, Function import sympy.functions as sym from sympy import init_printing init_printing(use_latex=True) from sympy import pprint from sympy import Symbol x = Symbol('x') # If a cell contains only the following, it will render perfectly. (pi + x)**2 # However I would like to control what to print in a function, # so that multiple expressions can be printed from a

Sympy - Comparing expressions

大城市里の小女人 提交于 2019-12-17 19:34:56
问题 Is there a way to check if two expressions are mathematically equal? I expected tg(x)cos(x) == sin(x) to output True , but it outputs False . Is there a way to make such comparisons with sympy? Another example is (a+b)**2 == a**2 + 2*a*b + b**2 which surprisingly also outputs False . I found some similar questions, but none covered this exact problem. 回答1: From the SymPy documentation == represents exact structural equality testing. “Exact” here means that two expressions will compare equal

Using Sympy Equations for Plotting

时光毁灭记忆、已成空白 提交于 2019-12-17 18:29:31
问题 What is the best way to create a Sympy equation, do something like take the derivative, and then plot the results of that equation? I have my symbolic equation, but can't figure out how to make an array of values for plotting. Here's my code: from sympy import symbols import matplotlib.pyplot as mpl t = symbols('t') x = 0.05*t + 0.2/((t - 5)**2 + 2) nums = [] for i in range(1000): nums.append(t) t += 0.02 plotted = [x for t in nums] mpl.plot(plotted) mpl.ylabel("Speed") mpl.show() In my case

Evaluate sympy expression from an array of values

情到浓时终转凉″ 提交于 2019-12-17 10:25:37
问题 I'm experimenting with sympy and I've hit upon an issue I can't work out. Using scipy I can write an expression and evaluate it for an array of x values as follows: import scipy xvals = scipy.arange(-100,100,0.1) f = lambda x: x**2 f(xvals) Using sympy I can write the same expression as follows: import sympy x = sympy.symbols('x') g = x**2 I can evaluate this expression for a single value by doing the following: g.evalf(subs={x:10}) However I can't work out how to evaluate it for an array of