编写一个“解决"函数.一个方程式 [英] Writing a function that "solves" an equation

查看:90
本文介绍了编写一个“解决"函数.一个方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,使我可以在js中求解"方程.

I want to write a function which will allow me to "solve" an equation in js.

我想要什么(不是编程语言):

what I want (not in a programming language):

function f(x) { 1 + x * x }
var z = 2
var y = f(z)  //y will be 5 as a number

我用JS编写的内容:

function P(cfg) { ....
this.equation = "1 + x";
....};
P.prototype.eqn = function(x) {
    var tmp = eval(this.equation);
    return tmp;
};
....
P.prototype.draw = function() {....
for(var i = 0; i < z; i++)
    ctx.lineTo(i, this.eqn(i));
....};

我也读过说,在循环中使用eval可能不是一个好主意,但是我还没有想出另一种方法(JS初学者)...

also I've read that using eval in a loop is probably not a good idea, but I have not figured out another way (yet) (JS beginner)...

此代码的问题在于,至少在FF中,var tmp仍将包含来自this.equation的字符串,而不是计算出的值.

The problem with this code is, that at least in FF the var tmp will STILL contain the string from this.equation instead of the calculated value.

我将不胜感激!

谢谢您的时间:)

因为我的问题表达得不是很好: 行执行后 var tmp = eval(this.equation); var tmp将保留一个STRING,该字符串等于字符串this.equation,而不是所需的解y值. 另外,我不是说要解决,而是要评估,谢谢您的提示:)

because my question was not formulated very well: after the execution of line var tmp = eval(this.equation); the var tmp will hold a STRING which equals the string this.equation, instead of the desired solution y value. Also I do not mean solve but evaluate, thanks for that tip :)

推荐答案

基于您的示例,我想说您想求一个表达式",而不是求解一个方程".为了评估表达式,您可能会找到许多教程.不过,我将简要介绍一下.您需要执行一些步骤.

Based on your example, I'd say that you want to "evaluate an expression", rather than "solve an equation". For evaluating an expression, you can probably find many tutorials. I'll break it down in brief though. You need to do a few steps.

从字符串"1 + x * x"开始,您需要将其分解为令牌.具体来说,将其细分为:"1", "+", "x", "*", "x".此时,您可以将变量("x")替换为其文字值("2"),从而为您提供"1", "+", "2", "*", "2"

Starting with your string "1 + x * x", you need to break it into tokens. Specifically, break it down into: "1", "+", "x", "*", "x". At this point, you can substitute your variables ("x") for their literal values ("2"), giving you "1", "+", "2", "*", "2"

现在您需要解析表达式.根据 PEMDAS的操作顺序,您需要创建一个树形数据结构,其中带括号的子句(被包围先用括号括起来),然后再进行乘法和除法,然后再执行加法和减法.解析通常不是一件容易的事,并且您可能希望将一个更简单的BNF语法组合在一起(尽管您可以通过一些谷歌搜索找到简单数学表达式的语法).

Now you need to parse the expression. Based on order of operations PEMDAS you need to create a tree data structure, where parenthetical clauses (stuff surrounded by parenthesis) are executed first, multiplication and division next, and then additions and subtraction last. Parsing is often not an easy task, and you may want to put together a simpler BNF grammar (though you can probably find a grammar for simple math expressions with some googling).

接下来,沿着树走,首先深入,在上树时评估操作.到达树顶之后,便有了解决方案.

Next, walk the tree, depth first, evaluating the operations as you go up the tree. Once you get to the top of the tree, you have your solution.

如果相反,您想求解方程式",则需要更复杂的东西,例如贤哲

If instead you want to "solve an equation", you're going to need something much more sophisticated, like Sage

这篇关于编写一个“解决"函数.一个方程式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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