在C / C ++衍生品? [英] Derivatives in C/C++?

查看:130
本文介绍了在C / C ++衍生品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些前pressions如 X ^ 2 + Y ^ 2 ,我想用一些数学计算。其中一个我想要做的事情就是要把前pressions的偏导数。

I have some expressions such as x^2+y^2 that I'd like to use for some math calculations. One of the things I'd like to do is to take partial derivatives of the expressions.

所以,如果 F(X,Y)= X ^ 2 + Y ^ 2 则部分 F的关于 X 2 ,部分相对于 2Y

So if f(x,y) = x^2 + y^2 then the partial of f with respect to x would be 2x, the partial with respect to y would be 2y.

我写了使用有限差分法一个极小的功能,但我遇到大量的浮点precision问题。例如,我结束了 1.99234 而不是 2 的。是否有支持符号分化任何库?任何其他建议?

I wrote a dinky function using a finite differences method but I'm running into lots of problems with floating point precision. For example, I end up with 1.99234 instead of 2. Are there any libraries that support symbolic differentiation? Any other suggestions?

推荐答案

我在几种不同的语言来实现这样的库,但可惜不是C.如果你用多项式(资金,产品,变量,常量只处理和权力)是pretty容易。触发功能也差不太多。任何事情更复杂,你可能会更好些,掌握别人的库的时间。

I've implemented such libraries in several different languages, but unfortunately not C. If you are dealing only with polynomials (sums, products, variables, constants, and powers) it is pretty easy. Trig functions are also not too bad. Anything more complicated and you will probably be better off taking the time to master somebody else's library.

如果您决定推出自己的,我有几个建议,可以简化您的生活:

If you decide to roll your own, I have a few suggestions that will simplify your life:


  • 使用不可变的数据结构(纯功能性数据结构)再present前pressions。

  • Use immutable data structures (purely functional data structures) to represent expressions.

使用汉斯·贝姆的垃圾收集器来管理内存给你。

要重新present的线性和,用有限的地图(如,二叉搜索树)将每个变量映射到其系数。

To represent a linear sum, use a finite map (e.g., a binary search tree) to map each variable to its coefficient.

如果你愿意 Lua的嵌入到你的C code,做你的计算那里,我已经把我的Lua code。在 http://www.cs.tufts.edu/~nr/drop/lua。其中一个更好的特点是,它可以采取象征性的前pression,区分它,结果编译成Lua中。你当然会发现没有任何文件 - (

If you're willing to embed Lua into your C code and do your computations there, I have put my Lua code at http://www.cs.tufts.edu/~nr/drop/lua. One of the nicer features is that it can take a symbolic expression, differentiate it, and compile the results into Lua. You will of course find no documentation whatever :-(

这篇关于在C / C ++衍生品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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