将字符串转换为数值方程 [英] Turning String into Numerical Equation

查看:98
本文介绍了将字符串转换为数值方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,希望有人可以帮我弄清楚是否有好的方法可以做到这一点。


我写的允许用户使用预先存在的变量在文本

字段中输入等式的程序。然后他们输入这些变量的数值,或者告诉程序将

中使用的值随机化为某个边界。我的问题是他们在文本字段中写了这个等式,并将它转换成Python可以计算的等式。


我知道的唯一方法要做到这一点是通过解析它,这可以得到相当不错的b $ b令人讨厌的所有不同的数学规则。有没有人比解析从字符串计算方程式更好的想法

表示?


非常感谢!


Brian Kazian

解决方案

Brian Kazian写道:

这里是'我的问题,希望有人可以帮我弄清楚是否有这样做的好方法。

我正在编写一个程序,允许用户在文本中输入方程
字段使用预先存在的变量。然后,他们输入这些变量的数值,或者告诉程序将
中使用的值随机化为某个边界。我的问题在于他们在文本字段中编写的这个等式并将其转换为Python可以计算的等式。

我知道这样做的唯一方法是解析它,所有不同的数学规则都会变得非常糟糕。有没有人比解析从字符串
表示计算方程更好?

非常感谢!

Brian Kazian



eval()


请参阅: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,

-ag

-

Artie Gold - 奥斯汀,德克萨斯州
http://it-matters.blogspot.com (新的发布12/5)
http://www.cafepress.com/goldsays


感谢您的帮助,我甚至没想到。


我在猜那里处理指数或对数

函数并不容易?我也会遇到这两种类型。

" Artie Gold" < AR ******* @ austin.rr.com>在消息中写道

news:39 ************* @ individual.net ...

Brian Kazian写道:

这是我的问题,希望有人可以帮我弄清楚
是否是一个很好的方法。

我正在编写一个程序,允许用户使用预先存在的变量在
文本字段中输入等式。然后,他们为这些变量输入数值,或者可以告诉程序随机化在某个范围内使用的值。我的问题在于他们在文本字段中编写的这个等式,并将其转换为Python可以计算的等式。

我知道这样做的唯一方法是通过解析它,可能会因为所有不同的数学规则而变得非常讨厌。有没有人比解析从字符串
表示计算方程有更好的想法?

非常感谢!

Brian Kazian

eval()

请参阅: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,
- -G

- Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays



Brian Kazian写道:

感谢您的帮助,我甚至没想到。

我猜测处理指数或对数函数没有简单的方法?我也将遇到这两种类型。


嗯,请考虑:


导入数学

eval(" log(pow(x,2) * pow(y,3),2)",{''pow'':math.pow,''og'':math.log},{''x'':1,''y'' :2})


[不,你不想这样写;它只是说明了你能做什么而不用多少钱。


HTH,

--ag


[BTW - 文化问题:我们在这里发帖吗?]

" Artie Gold" < AR ******* @ austin.rr.com>在消息中写道
新闻:39 ************* @ individual.net ...

Brian Kazian写道:

这是我的问题,希望有人可以帮我弄清楚
是否是一个很好的方法。

我正在写一个程序允许用户使用预先存在的变量在
文本字段中输入等式。然后,他们为这些变量输入数值,或者可以告诉程序随机化在某个范围内使用的值。我的问题在于他们在文本字段中编写的这个等式,并将其转换为Python可以计算的等式。

我知道这样做的唯一方法是通过解析它,可能会因为所有不同的数学规则而变得非常讨厌。有没有人比解析从字符串
表示计算方程有更好的想法?

非常感谢!

Brian Kazian


eval()

请参阅: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,
- ag

- Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays




-

Artie Gold - 德克萨斯州奥斯汀
http:/ /it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/go ldsays


Here''s my problem, and hopefully someone can help me figure out if there is
a good way to do this.

I am writing a program that allows the user to enter an equation in a text
field using pre-existing variables. They then enter numerical values for
these variables, or can tell the program to randomize the values used within
a certain bounds. My problem is taking in this equation they have written
in the text field and converting it into an equation Python can calculate.

The only way I know of to do this is by parsing it, which could get pretty
nasty with all of the different mathematical rules. Does anyone have a
better idea than parsing to compute an equation from a string
representation?

Thanks so much!

Brian Kazian

解决方案

Brian Kazian wrote:

Here''s my problem, and hopefully someone can help me figure out if there is
a good way to do this.

I am writing a program that allows the user to enter an equation in a text
field using pre-existing variables. They then enter numerical values for
these variables, or can tell the program to randomize the values used within
a certain bounds. My problem is taking in this equation they have written
in the text field and converting it into an equation Python can calculate.

The only way I know of to do this is by parsing it, which could get pretty
nasty with all of the different mathematical rules. Does anyone have a
better idea than parsing to compute an equation from a string
representation?

Thanks so much!

Brian Kazian


eval()

See: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


Thanks for the help, I didn''t even think of that.

I''m guessing there''s no easy way to handle exponents or logarithmic
functions? I will be running into these two types as well.
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:39*************@individual.net...

Brian Kazian wrote:

Here''s my problem, and hopefully someone can help me figure out if there
is a good way to do this.

I am writing a program that allows the user to enter an equation in a
text field using pre-existing variables. They then enter numerical
values for these variables, or can tell the program to randomize the
values used within a certain bounds. My problem is taking in this
equation they have written in the text field and converting it into an
equation Python can calculate.

The only way I know of to do this is by parsing it, which could get
pretty nasty with all of the different mathematical rules. Does anyone
have a better idea than parsing to compute an equation from a string
representation?

Thanks so much!

Brian Kazian


eval()

See: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays



Brian Kazian wrote:

Thanks for the help, I didn''t even think of that.

I''m guessing there''s no easy way to handle exponents or logarithmic
functions? I will be running into these two types as well.
Well, consider:

import math
eval("log(pow(x,2)*pow(y,3),2)",{''pow'':math.pow,''l og'':math.log},{''x'':1,''y'':2})

[No, you wouldn''t want to write it that way; it''s merely illustrating
what you can do without doing much.]

HTH,
--ag

[BTW -- cultural question: Do we top-post here?]
"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:39*************@individual.net...

Brian Kazian wrote:

Here''s my problem, and hopefully someone can help me figure out if there
is a good way to do this.

I am writing a program that allows the user to enter an equation in a
text field using pre-existing variables. They then enter numerical
values for these variables, or can tell the program to randomize the
values used within a certain bounds. My problem is taking in this
equation they have written in the text field and converting it into an
equation Python can calculate.

The only way I know of to do this is by parsing it, which could get
pretty nasty with all of the different mathematical rules. Does anyone
have a better idea than parsing to compute an equation from a string
representation?

Thanks so much!

Brian Kazian



eval()

See: http://docs.python.org/lib/built-in-funcs.html#l2h-23

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays



--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


这篇关于将字符串转换为数值方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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