导数计算器

发布于 2024-09-03 01:59:09 字数 208 浏览 4 评论 0原文

我有兴趣构建一个导数计算器。我绞尽脑汁想解决这个问题,但始终没有找到正确的解决办法。您可以提示一下如何开始吗?谢谢

对不起!我显然想进行象征性的区分。

假设您有函数 f(x) = x^3 + 2x^2 + x

我想显示导数,在本例中 f'(x) = 3x^2 + 4x + 1

我想实现它在 iPhone 的 Objective-C 中。

I'm interested in building a derivative calculator. I've racked my brains over solving the problem, but I haven't found a right solution at all. May you have a hint how to start? Thanks

I'm sorry! I clearly want to make symbolic differentiation.

Let's say you have the function f(x) = x^3 + 2x^2 + x

I want to display the derivative, in this case f'(x) = 3x^2 + 4x + 1

I'd like to implement it in objective-c for the iPhone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

ゃ人海孤独症 2024-09-10 01:59:09

我假设您正在尝试找到函数的精确导数。 (符号微分)

您需要解析数学表达式并将函数中的各个操作存储在树结构中。

例如,x + sin²(x) 将存储为 + 运算,应用于表达式 x^< /code>sin(x) 和 2 的(求幂)运算。

然后,您可以通过将微分规则应用于每个节点来递归地微分树。例如,+ 节点将变为 u' + v'* 节点将变为 uv' + vu'

I assume that you're trying to find the exact derivative of a function. (Symbolic differentiation)

You need to parse the mathematical expression and store the individual operations in the function in a tree structure.

For example, x + sin²(x) would be stored as a + operation, applied to the expression x and a ^ (exponentiation) operation of sin(x) and 2.

You can then recursively differentiate the tree by applying the rules of differentiation to each node. For example, a + node would become the u' + v', and a * node would become uv' + vu'.

香橙ぽ 2024-09-10 01:59:09

你需要记住你的微积分。基本上你需要两件事:基本函数的导数表和如何推导复合表达式的规则(例如 d(f + g)/dx = df/dx + dg/dx )。然后采用表达式解析器并递归地转到树的其他部分。 (http://www.sosmath.com/tables/derivative/derivative.html)

you need to remember your calculus. basically you need two things: table of derivatives of basic functions and rules of how to derivate compound expressions (like d(f + g)/dx = df/dx + dg/dx). Then take expressions parser and recursively go other the tree. (http://www.sosmath.com/tables/derivative/derivative.html)

紫竹語嫣☆ 2024-09-10 01:59:09

将字符串解析为 S-expression (尽管这通常是在 Lisp 上下文中进行的) ,您可以用几乎任何语言做等效的事情),最简单的是使用 lex/yacc 或等效函数,然后编写一个递归“派生”函数。在 OCaml 风格的方言中,类似这样:(

let rec derive var = function
    | Const(_) -> Const(0)
    | Var(x) -> if x = var then Const(1) else Deriv(Var(x), Var(var))
    | Add(x, y) -> Add(derive var x, derive var y)
    | Mul(a, b) -> Add(Mul(a, derive var b), Mul(derive var a, b))
    ...

如果您不知道 OCaml 语法 - derive 是双参数递归函数,第一个参数是变量名称,第二个参数在连续行中进行数学计算; 例如,如果此参数是 Add(x, y) 形式的结构,则返回由两个字段构建的结构 Add,其值为派生的 x 和导出的 y;对于第一个模式中 derive 可能接收的参数的其他情况也类似;匹配任何东西”)

在此之后,您可能有一些清理功能来整理结果表达式(减少分数等),但这会变得复杂,并且对于推导本身来说不是必需的(即,没有它您得到的仍然是正确的答案)。

完成 s-exp 的转换后,再次使用递归函数将生成的 s-exp 重新转换为字符串形式

Parse your string into an S-expression (even though this is usually taken in Lisp context, you can do an equivalent thing in pretty much any language), easiest with lex/yacc or equivalent, then write a recursive "derive" function. In OCaml-ish dialect, something like this:

let rec derive var = function
    | Const(_) -> Const(0)
    | Var(x) -> if x = var then Const(1) else Deriv(Var(x), Var(var))
    | Add(x, y) -> Add(derive var x, derive var y)
    | Mul(a, b) -> Add(Mul(a, derive var b), Mul(derive var a, b))
    ...

(If you don't know OCaml syntax - derive is two-parameter recursive function, with first parameter the variable name, and the second being mathched in successive lines; for example, if this parameter is a structure of form Add(x, y), return the structure Add built from two fields, with values of derived x and derived y; and similarly for other cases of what derive might receive as a parameter; _ in the first pattern means "match anything")

After this you might have some clean-up function to tidy up the resultant expression (reducing fractions etc.) but this gets complicated, and is not necessary for derivation itself (i.e. what you get without it is still a correct answer).

When your transformation of the s-exp is done, reconvert the resultant s-exp into string form, again with a recursive function

梦情居士 2024-09-10 01:59:09

SLAks 已经描述了符号微分的过程。我只想添加一些内容:

  • 符号数学主要是解析和树转换。 ANTLR 对于两者来说都是一个很棒的工具。我建议从这本伟大的书开始 语言实现模式
  • 有开放-执行您想要的操作的源程序(例如 Maxima)。剖析这样的程序可能也很有趣(但如果您首先尝试自己编写它,可能会更容易理解发生了什么)
  • ,您可能还希望对输出进行某种简化。例如,仅将基本导数规则应用于表达式 2 * x 将产生 2 + 0*x。这也可以通过树处理来完成(例如,通过将 0 * [...] 转换为 0[...] + 0[...] 等等)

SLaks already described the procedure for symbolic differentiation. I'd just like to add a few things:

  • Symbolic math is mostly parsing and tree transformations. ANTLR is a great tool for both. I'd suggest starting with this great book Language implementation patterns
  • There are open-source programs that do what you want (e.g. Maxima). Dissecting such a program might be interesting, too (but it's probably easier to understand what's going on if you tried to write it yourself, first)
  • Probably, you also want some kind of simplification for the output. For example, just applying the basic derivative rules to the expression 2 * x would yield 2 + 0*x. This can also be done by tree processing (e.g. by transforming 0 * [...] to 0 and [...] + 0 to [...] and so on)
扶醉桌前 2024-09-10 01:59:09

您想要计算什么类型的导数?如果允许使用正弦、余弦和正切等三角函数,这些函数可能最好存储在表中,而多项式等其他函数可能更容易实现。您是否允许函数有多个输入,例如 f(x,y) 而不仅仅是 f(x)?

我的建议是使用单个变量中的多项式,然后考虑添加三角函数、对数函数、指数函数和其他高级函数来计算导数,这可能更难做到。

For what kinds of operations are you wanting to compute a derivative? If you allow trigonometric functions like sine, cosine and tangent, these are probably best stored in a table while others like polynomials may be much easier to do. Are you allowing for functions to have multiple inputs,e.g. f(x,y) rather than just f(x)?

Polynomials in a single variable would be my suggestion and then consider adding in trigonometric, logarithmic, exponential and other advanced functions to compute derivatives which may be harder to do.

无所谓啦 2024-09-10 01:59:09

对常见函数(+、-、*、/、^、sin、cos 等)进行符号微分,忽略函数或其导数未定义的区域是很容易的。困难的是事后简化结果,这也许是违反直觉的。

要进行微分,请将运算存储在树中(甚至仅使用波兰表示法),并制作每个基本运算的导数表。然后重复应用链式法则和初等导数,并将常数的导数设置为 0。这是快速且易于实现的。

Symbolic differentiation over common functions (+, -, *, /, ^, sin, cos, etc.) ignoring regions where the function or its derivative is undefined is easy. What's difficult, perhaps counterintuitively, is simplifying the result afterward.

To do the differentiation, store the operations in a tree (or even just in Polish notation) and make a table of the derivative of each of the elementary operations. Then repeatedly apply the chain rule and the elementary derivatives, together with setting the derivative of a constant to 0. This is fast and easy to implement.

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