equation

how to handle an asymptote/discontinuity with Matplotlib

微笑、不失礼 提交于 2019-11-26 12:43:38
问题 When plotting a graph with a discontinuity/asymptote/singularity/whatever, is there any automatic way to prevent Matplotlib from \'joining the dots\' across the \'break\'? (please see code/image below). I read that Sage has a [detect_poles] facility that looked good, but I really want it to work with Matplotlib. import matplotlib.pyplot as plt import numpy as np from sympy import sympify, lambdify from sympy.abc import x fig = plt.figure(1) ax = fig.add_subplot(111) # set up axis ax.spines[\

Equation parsing in Python

自闭症网瘾萝莉.ら 提交于 2019-11-26 03:23:26
问题 How can I (easily) take a string such as \"sin(x)*x^2\" which might be entered by a user at runtime and produce a Python function that could be evaluated for any value of x ? 回答1: Python's own internal compiler can parse this, if you use Python notation. If your change the notation slightly, you'll be happier. import compiler eq= "sin(x)*x**2" ast= compiler.parse( eq ) You get an abstract syntax tree that you can work with. 回答2: You can use Python parser : import parser formula = "sin(x)*x**2

Is it possible to plot implicit equations using Matplotlib?

♀尐吖头ヾ 提交于 2019-11-26 02:08:27
I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg. X^y=y^x) in Matplotlib. Is this possible? Steve I don't believe there's very good support for this, but you could try something like import matplotlib.pyplot from numpy import arange from numpy import meshgrid delta = 0.025 xrange = arange(-5.0, 20.0, delta) yrange = arange(-5.0, 20.0, delta) X, Y = meshgrid(xrange,yrange) # F is one side of the equation, G is the other F = Y**X G = X**Y matplotlib.pyplot.contour(X, Y, (F - G), [0]) matplotlib.pyplot.show() See the API docs for contour : if the fourth argument is a

Is it possible to plot implicit equations using Matplotlib?

我们两清 提交于 2019-11-26 01:08:43
问题 I would like to plot implicit equations (of the form f(x, y)=g(x, y) eg. X^y=y^x) in Matplotlib. Is this possible? 回答1: I don't believe there's very good support for this, but you could try something like import matplotlib.pyplot from numpy import arange from numpy import meshgrid delta = 0.025 xrange = arange(-5.0, 20.0, delta) yrange = arange(-5.0, 20.0, delta) X, Y = meshgrid(xrange,yrange) # F is one side of the equation, G is the other F = Y**X G = X**Y matplotlib.pyplot.contour(X, Y, (F

Equation (expression) parser with precedence?

。_饼干妹妹 提交于 2019-11-26 00:13:51
问题 I\'ve developed an equation parser using a simple stack algorithm that will handle binary (+, -, |, &, *, /, etc) operators, unary (!) operators, and parenthesis. Using this method, however, leaves me with everything having the same precedence - it\'s evaluated left to right regardless of operator, although precedence can be enforced using parenthesis. So right now \"1+11*5\" returns 60, not 56 as one might expect. While this is suitable for the current project, I want to have a general

Equation parsing in Python

跟風遠走 提交于 2019-11-25 19:20:50
How can I (easily) take a string such as "sin(x)*x^2" which might be entered by a user at runtime and produce a Python function that could be evaluated for any value of x ? Python's own internal compiler can parse this, if you use Python notation. If your change the notation slightly, you'll be happier. import compiler eq= "sin(x)*x**2" ast= compiler.parse( eq ) You get an abstract syntax tree that you can work with. You can use Python parser : import parser formula = "sin(x)*x**2" code = parser.expr(formula).compile() from math import sin x = 10 print eval(code) It performs better than pure