以编程方式优化表达式(通过删除冗余计算)

发布于 2024-09-06 19:07:41 字数 354 浏览 8 评论 0原文

我有一个相当大的方程,我需要用它来求解给定的变量。所以我使用了一个在线工具,它能够根据给定变量重写方程。它给了我一些巨大的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终弃我 2024-09-13 19:07:41

编辑>表达式从 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 个字符方程变为

   t= -((E*A+B*F+ Sqrt(2*A*E*F*B+ A^2*(I^2-F^2) + B^2*(I^2-E^2))) /(A^2 + B^2))

Where

   E = e - g
   A = a - c
   B = b - d
   F = f - h
   I = i + j

Please check if the Sqrt argument is a Perfect Square, based on other "geometrical"thoughts ...它吠叫并有一条尾巴。 ..是狗吗?

编辑->猜测:

我没有任何证据,但方程的对称性表明在你的问题中

  E^2 = (I^2-F^2)  => (e-g)^2 = (i+j)^2 - (f-h)^2

如果是这样的话(请验证它),你的方程就变成了

  t= -((E*A+B*F+ Abs(E*A+B*F)) /(A^2 + B^2))

如果AE+BF > 0(我猜是这样,因为如果不是 t===0)又

  +-----------------------------------+
  ¦  Your 700 chars equation comes to ¦
  ¦                                   ¦
  ¦ t= -2 * (A*E + B*F) / (A^2 + B^2) ¦
  ¦                                   ¦
  +-----------------------------------+

短又甜......:)

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

   t= -((E*A+B*F+ Sqrt(2*A*E*F*B+ A^2*(I^2-F^2) + B^2*(I^2-E^2))) /(A^2 + B^2))

Where

   E = e - g
   A = a - c
   B = b - d
   F = f - h
   I = i + j

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

  E^2 = (I^2-F^2)  => (e-g)^2 = (i+j)^2 - (f-h)^2

If so is the case (please verify it), your equation becomes

  t= -((E*A+B*F+ Abs(E*A+B*F)) /(A^2 + B^2))

If AE+BF > 0 (and I guess it is so, because if not t===0)

  +-----------------------------------+
  ¦  Your 700 chars equation comes to ¦
  ¦                                   ¦
  ¦ t= -2 * (A*E + B*F) / (A^2 + B^2) ¦
  ¦                                   ¦
  +-----------------------------------+

short and sweet ... :)

生寂 2024-09-13 19:07:41

我用过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.

拿命拼未来 2024-09-13 19:07:41

Maxima 有一个有用的函数,称为“优化”:

功能:优化(expr)

返回一个表达式,该表达式产生与 expr 相同的值和副作用,但通过避免重新计算公共子表达式来提高效率。 Optimize 还具有“折叠”其参数的副作用,以便共享所有公共子表达式。做例子(优化)作为例子。

它将简化您上传到 Ideone 的表达式:

block(
[%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14],
  %1:a^2,
  %2:b^2,
  %3:c^2,
  %4:d^2,
  %5:-%4+2*b*d-%2,
  %6:-%3+2*a*c-%1,
  %7:2*a-2*c,
  %8:2*c-2*a,
  %9:
  %8*d+b*%7,
  %10:%7*d+b*%8,
  %11:i^2,
  %12:j^2,
  %13:-2*%12-4*i*j-2*%11,
  %14:%12+2*i*j+%11,(-sqrt(%4*%14+%3*%14+%2*%14+%1*%14+b*d*%13+a*c*%13+%6*h^2+    (%9*g+2*%3-4*a*c+2*%1)*f+%10*e)*h+%5*g^2+f*(%10*g+%9*e)+(2*%4-4*b*d+2*%2)*e*g+%6*f^2+%5*e^2)-(d-b)*h-(c-a)*g-(b-d)*f-(a-)*e)/(%4-2*b*d+%3-2*a*c+%2+%1))

不一定更具可读性,但它不包含更多常见的子表达式。

Maxima has a useful function called optimize:

Function: optimize (expr)

Returns an expression that produces the same value and side effects as expr but does so more efficiently by avoiding the recomputation of common subexpressions. optimize also has the side effect of "collapsing" its argument so that all common subexpressions are shared. Do example (optimize) for examples.

It would simplify the expression you uploaded to Ideone to:

block(
[%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14],
  %1:a^2,
  %2:b^2,
  %3:c^2,
  %4:d^2,
  %5:-%4+2*b*d-%2,
  %6:-%3+2*a*c-%1,
  %7:2*a-2*c,
  %8:2*c-2*a,
  %9:
  %8*d+b*%7,
  %10:%7*d+b*%8,
  %11:i^2,
  %12:j^2,
  %13:-2*%12-4*i*j-2*%11,
  %14:%12+2*i*j+%11,(-sqrt(%4*%14+%3*%14+%2*%14+%1*%14+b*d*%13+a*c*%13+%6*h^2+    (%9*g+2*%3-4*a*c+2*%1)*f+%10*e)*h+%5*g^2+f*(%10*g+%9*e)+(2*%4-4*b*d+2*%2)*e*g+%6*f^2+%5*e^2)-(d-b)*h-(c-a)*g-(b-d)*f-(a-)*e)/(%4-2*b*d+%3-2*a*c+%2+%1))

Not neccessarily more readable, but it contains no more common subexpressions.

单调的奢华 2024-09-13 19:07:41

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文