以编程方式优化表达式(通过删除冗余计算)
我有一个相当大的方程,我需要用它来求解给定的变量。所以我使用了一个在线工具,它能够根据给定变量重写方程。它给了我一些巨大的 700 个字符的方程。我测试了它,它确实有效。
我可以在方程中看到一些非常明显的冗余,它正在重新计算一个可以保存为临时变量的值。我可以自己检查整个方程并对其进行优化,但我可能需要使用更多方程来完成此操作,因此我想自动化该过程。
有哪些好工具可以帮助优化数学冗余?
(这只是一个个人项目,所以我真的更喜欢免费的东西)
对于所有我认识的人会问这是否真的有必要:这是性能关键的代码,根据我的经验,AS3 编译器将不要自己进行此类优化。删除冗余也会使代码更具可读性。
I had a pretty big equation that I needed to use to solve for a given variable. So I used an online tool that was capable of rewriting an equation in terms of a given variable. It gave me some huge 700 character equation. I tested it, and it does work.
I can see some pretty obvious redundancies in the equation where it's recomputing a value that could be saved as a temporary variable instead. I could go through the entire equation and optimize it myself, but I'm likely to have to do this with many more equations, so I'd like to automate the process instead.
What are some good tools that will help optimize mathematical redundancies?
(It's just for a personal project, so I'd really prefer something free)
To all those people who I know will ask about this really being necessary: This is performance critical code, and from my experience, the AS3 compiler will not do these kind of optimizations on it's own. Removing redundancies will also make the code more readable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑>表达式从 700 个字符减少到以下 20 个字符
尝试在 Wolfram Alpha 或 Mathematica 中使用 FullSimplify。
WolframAlpha FullSimplify(x^2+2 x +1)
编辑 - >
再想一想,Mathematica 不需要简化你的一个 var 方程来求解它......Solve 命令(或 FindRoot、FindInstance ...)会做到这一点。
尝试例如
WolframAlpha Solve(x^2+2*x+1=0 , x)< /a>
编辑 ->
只是为了使答案摆脱对 ideone.com 的依赖,经过一些简化后,您的 700 个字符方程变为
Where
Please check if the Sqrt argument is a Perfect Square, based on other "geometrical"thoughts ...它吠叫并有一条尾巴。 ..是狗吗?
编辑->猜测:
我没有任何证据,但方程的对称性表明在你的问题中
如果是这样的话(请验证它),你的方程就变成了
如果AE+BF > 0(我猜是这样,因为如果不是 t===0)又
短又甜......:)
Edit> Expression reduced form 700 to 20 chars below
Try to use FullSimplify in Wolfram Alpha, or Mathematica.
WolframAlpha FullSimplify(x^2+2 x +1)
Edit ->
Thinking again, Mathematica does not need to simplify your one var equation to solve it ... the Solve command (or FindRoot, or FindInstance ...) will do it.
Try for example
WolframAlpha Solve(x^2+2*x+1=0 , x)
EDIT ->
Just to make the answer free of dependencies from ideone.com, your 700 char equation after some simplifications becomes
Where
Please check if the Sqrt argument is a perfect square, based on other "geometrical" considerations ... it barks and has a tail ... is it a dog?
EDIT -> Guesswork:
I don't have any proof, but the symmetry of the equation suggests that in your problem
If so is the case (please verify it), your equation becomes
If AE+BF > 0 (and I guess it is so, because if not t===0)
short and sweet ... :)
我用过
wxMaxima
。让它进行替换相当容易,而且是免费的。我必须进行大量的拉普拉斯变换,并进行部分分数展开。一旦我学会了如何使用它,速度就非常快了。I've used
wxMaxima
. It's fairly easy to make it do substitutions, and it's free. I had to crank a lot of massive Laplace transforms, with partial fraction expansions. Once I learned how to use it, it was pretty quick.Maxima 有一个有用的函数,称为“优化”:
它将简化您上传到 Ideone 的表达式:
不一定更具可读性,但它不包含更多常见的子表达式。
Maxima has a useful function called
optimize
:It would simplify the expression you uploaded to Ideone to:
Not neccessarily more readable, but it contains no more common subexpressions.
正如 belisarius 所建议的,将方程代入数学像 matlab、mathematica 或 maple 这样的编程语言将允许您使用它们的简化和缩减工具来帮助您。
这是一个免费的类似 matlab 的程序列表 http://www.dspguru.com/dsp /links/matlab-clones 如果您不想花高价购买 matlab 许可证。
As belisarius suggested, putting the equation into a mathematical programming language like matlab, mathematica or maple would allow you to use their simplify and reduction tools to help you.
Here is a list of free matlab like programs http://www.dspguru.com/dsp/links/matlab-clones if you dont want to fork out the high price for a matlab licence.