解析Python中的复杂数学函数 [英] Parsing Complex Mathematical Functions in Python

查看:711
本文介绍了解析Python中的复杂数学函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中是否有一种解析描述3D图形的Python中的数学表达式?使用其他数学模块。我似乎找不到一种方法来处理两个输入。



我想要解析的函数的一个例子是持有人表功能



它有多个输入,三角函数函数,我也需要解析abs()部分。



我有两个2D numpy meshgrid作为x1和x2的输入,并且更愿意直接传递谢谢你的帮助,谢谢。

解决方案

直接编写 numpy 代码来评估此公式并不困难。

  def持有者(x1,x2):
f1 = 1 - np.sqrt(x1 ** 2 + x2 ** 2)/np.pi
f2 = np.exp (np.abs(f1))
f3 = np.sin(x1)* np.cos(x2)* f2
return -np.abs(f3)

在一点评估:

  :保持器(10,10) 
出[109]:-15.140223856952055

在网格上评估:

 在[60]中:I,J = np.mgrid [-bd:bd:.01,-bd:bd:.01] 
在[61]:H =持有人(I,J)



快速肮脏的表情



潜入 sympy 而不读取大部分文档:



在[65]中:来自sympy.abc import x,y
在[69]中:将sympy作为sp
在[70]中:x
出[70]:x
在[71]中:x ** 2
输出[71]:x ** 2
在[72]中:(x ** 2 + y ** 2)/sp.pi
输出[72]:(x ** 2 + y ** 2)/ pi
在[73]中:1-(x ** 2 + y ** 2)/sp.pi
出[73]: - (x ** 2 + y ** 2)/ pi + 1
在[75]中:从sympy import Abs
..
在[113]中:h = -Abs(sp.sin(x)* sp.cos(y)* sp.exp(Abs(1-sp.sqrt(x ** 2 + y ** 2 )/
在[114]中:float(h.subs(x,10).subs(y,10))
输出[114]:-15.140223856952053
或者表示DSM建议

  在[117]中:h1 = sp.sympify( -  Abs(sin(x)* cos(y)* exp Abs(1-sqrt(x ** 2 + y ** 2)/ pi)))))
在[118]中:h1
Out [118]:-exp(Abs(sqrt **(b)(b)()()() y,10))
输出[119]:-15.140223856952053
...
在[8]中:h1.subs({x:10,y:10})。
输出[8]:-15.1402238569521



现在我用numpy数组怎么用?



在numpy mesgrid上评估symptom lambdify的结果

 在[22]中:f = sp。在[23]中:z = f(I,J)
在[24]中,[(x,y),h1,[{'ImmutableMatrix':np.array},numpy ]:z.shape
Out [24]:(2000,2000)
在[25]中:np.allclose(z,H)
输出[25]:True
在[26]中:timeit持有者(I,J)
1循环,最好3:898 ms每循环
在[27]中:timeit f(I,J)
1循环,最佳3:899 ms每循环

有趣 - 基本相同的速度。



此行的早期回答:如何从文本文件中读取微分方程系统以使用scipy.codeint来解决系统?


Is there a way in Python to parse a mathematical expression in Python that describes a 3D graph? Using other math modules or not. I couldn't seem to find a way for it to handle two inputs.

An example of a function I would want to parse is Holder Table Function.

It has multiple inputs, trigonometric functions, and I would need to parse the abs() parts too.

I have two 2D numpy meshgrids as input for x1 and x2, and would prefer to pass them directly to the expression for it to evaluate.

Any help would be greatly appreciated, thanks.

解决方案

It's not hard to write straight numpy code that evaluates this formula.

def holder(x1, x2):
    f1 = 1 - np.sqrt(x1**2 + x2**2)/np.pi
    f2 = np.exp(np.abs(f1))
    f3 = np.sin(x1)*np.cos(x2)*f2
    return -np.abs(f3)

Evaluated at a point:

In [109]: holder(10,10)
Out[109]: -15.140223856952055

Evaluated on a grid:

In [60]: I,J = np.mgrid[-bd:bd:.01, -bd:bd:.01]
In [61]: H = holder(I,J)

Quick-n-dirty sympy

Diving into sympy without reading much of the docs:

In [65]: from sympy.abc import x,y
In [69]: import sympy as sp
In [70]: x
Out[70]: x
In [71]: x**2
Out[71]: x**2
In [72]: (x**2 + y**2)/sp.pi
Out[72]: (x**2 + y**2)/pi
In [73]: 1-(x**2 + y**2)/sp.pi
Out[73]: -(x**2 + y**2)/pi + 1
In [75]: from sympy import Abs
...
In [113]: h = -Abs(sp.sin(x)*sp.cos(y)*sp.exp(Abs(1-sp.sqrt(x**2 + y**2)/sp.pi)))
In [114]: float(h.subs(x,10).subs(y,10))
Out[114]: -15.140223856952053

Or with the sympify that DSM suggested

In [117]: h1 = sp.sympify("-Abs(sin(x)*cos(y)*exp(Abs(1-sqrt(x**2 + y**2)/pi)))")
In [118]: h1
Out[118]: -exp(Abs(sqrt(x**2 + y**2)/pi - 1))*Abs(sin(x)*cos(y))
In [119]: float(h1.subs(x,10).subs(y,10))
Out[119]: -15.140223856952053
...
In [8]: h1.subs({x:10, y:10}).n()
Out[8]: -15.1402238569521

Now how do I use this with numpy arrays?

Evaluating the result of sympy lambdify on a numpy mesgrid

In [22]: f = sp.lambdify((x,y), h1,  [{'ImmutableMatrix': np.array}, "numpy"])
In [23]: z = f(I,J)
In [24]: z.shape
Out[24]: (2000, 2000)
In [25]: np.allclose(z,H)
Out[25]: True
In [26]: timeit holder(I,J)
1 loop, best of 3: 898 ms per loop
In [27]: timeit f(I,J)
1 loop, best of 3: 899 ms per loop

Interesting - basically the same speed.

Earlier answer along this line: How to read a system of differential equations from a text file to solve the system with scipy.odeint?

这篇关于解析Python中的复杂数学函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆