用c#减少等式 [英] Reduce equation with c#

查看:89
本文介绍了用c#减少等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们!



我尝试设置一种减少等式的方法。我将提供一个示例:



用户输入:(4x + 7x)/(xy)

其中x和y是未知变量在计算过程中填写。


$ RP $ b在RPN上面的公式将是:



4 x * 7 x * + xy * /



要计算表达式,计算器必须进行11次令牌。想象一下,我用x和y的不同值来解决这个公式1000万次。这可行,但它不是最理想的,因为表达式可以降低到



11 / y(或RPN 11 y /)



现在,我的问题是如何以编程方式将公式减少到上面提到的值。有什么建议吗?



非常感谢提前



关心

Hi folks!

I try to set up a method, that reduces an equation. I'll provide a sample:

User Input: (4x + 7x) / (xy)
where x and y are unknown variables which are filled during the calculation process.

in RPN the formula above would be:

4 x * 7 x * + x y * /

To calculate the expression, the calculator would have to proceed 11 token. Imagine me solving this formula 10 million times with different values for x and y. This would work, but it is suboptimal since the expression could be reduced to

11 / y (or RPN 11 y /)

Now, my question is how to reduce the formula programatically to the value mentioned above. Any suggesions?

Thanks a lot in advance

Regards

推荐答案

这是一项相当复杂的任务。你需要不使用数字,而是使用符号计算。

计算领域称为计算机代数系统 CAS ,见< a href =http://en.wikipedia.org/wiki/Computer_algebra_system> http://en.wikipedia.org/wiki/Computer_algebra_system [ ^ ])。



你需要象征性地解析你的表达式表达树。您的操作应表示为节点,您的子表达式应该是节点子节点,终端节点应该是表示表达式参数或常量的对象。



好新闻:使用v.4.0,您可以通过lambda表达式从C#代码中获取表达式树: http://msdn.microsoft.com/en-us/library/bb397951.aspx [ ^ ]。



除此之外的一切都要困难得多:你需要开发一个系统在树上执行数学运算。你的代数运算应该是对树表达式的操作;它们将表达式树作为参数,并为返回值构建结果表达式树。例如,微分函数有两个参数:对要区分的参数的引用和表示要区分的函数的表达式树,结果将是表示派生的新表达式树;由于衍生物的属性,函数应该是递归的。



为什么我要解释衍生的例子?仅仅因为这是所有计算机代数中最简单的非平凡操作!简化表达式已经困难得多了。你可以为一些局部情况开发简化算法,但一般情况下很难。



祝你好运,

-SA
This is a pretty complex task. You need to work not with the digital, but with symbolic calculations.
The field of computing is called Computer Algebra System (CAS, see http://en.wikipedia.org/wiki/Computer_algebra_system[^]).

You need to parse your expression symbolically into an expression tree. Your operations should be represented as the nodes, your sub-expressions should be the node children, and your terminal nodes should be the objects representing expression arguments or constants.

Good news: with your v.4.0 you can get expression trees right from your C# code in the form of lambda expression: http://msdn.microsoft.com/en-us/library/bb397951.aspx[^].

Everything beyond that is much more difficult: you will need to develop a system which performs mathematical operations over a tree. Your algebra operations should be the operations over an expression of the tree; they will takes expression tree(s) as an argument and build resulting expression tree for return value. For example, differentiation function takes two parameters: a reference to argument to differentiate by and the expression tree representing a function to be differentiate, the result will be a new expression tree representing the derivative; due to the property of the derivative the function should be recursive.

Why did I explain the example of derivative? Simply because this is the simplest non-trivial operation in all Computer Algebra! Simplification of expressions is already much more difficult. You can develop simplification algorithm for some partial cases, but a general case is quite difficult.

Good luck,
—SA


SA是对的。



由于看起来非常自然,简化可能看起来很简单。但它根本不存在。



首先,人们应该同意表达式的最简单形式。例如,你应该保留:

1 / sqrt(x)

或者更喜欢

sqrt (x)/ x



有些代数程序会给你第二个表达式,因为它们不喜欢比率下的根。



即使你自己的例子也可能带来问题:

简化(4x + 7x)/(xy)只要 x 不等于 0 ,进入 11 / y 即可。在数学上,两个表达式都不相等(第一个没有为 x = 0 定义,但第二个是。)



这是绝对不是一个小问题...
SA is right.

Simplication might appear as a simple task at first sight since it looks very natural. But it is not at all.

First of all, one should agree on the simplest form of an expression. For example, should you keep:
1 / sqrt(x)
or prefer
sqrt(x) / x?

Some algebra programs will give you the second expression as the simplest one since they don't like roots under a ratio.

And even your own example might bring problems:
Simplifying (4x + 7x) / (xy) into 11/y is OK as long as x doesn't equal 0. Mathematically, both expressions are not equivalent (the first one is not defined for x=0, but the second is).

This is definitely not a trivial problem…


我想你应该看看 Math.NET:关于&安培;功能 [ ^ ]



Math.NET LinqAlegebra直接在纯Linq表达式之上提供通用计算机代数系统的元素,包括自动简化,区分和MathML I / O.



问候

Espen Harlinn
I guess you should take a look at Math.NET : About & Features[^]

Math.NET LinqAlegebra provides elements of general purpose computer algebra systems directly on top of pure Linq Expressions, including automatic simplification, differentiation and MathML I/O.

Regards
Espen Harlinn


这篇关于用c#减少等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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