如何将 Latex 公式转换为 C/C++代码?

发布于 2024-12-11 15:23:14 字数 510 浏览 0 评论 0原文

我需要将以 Latex 风格编写的数学公式转换为 C/C++ 代码的函数。 例如: y = sin(x)^2 会变成类似

double y = sin(x) * sin(x);

或 的

double y = pow(sin(x), 2);

东西,其中 x 是之前在某处定义的变量。 我的意思是它应该将 Latex 公式转换为 C/C++ 语法。因此,如果存在一个函数y = G(x, y)^F(x),那么G(x,y)是什么并不重要>F(x), 定义它是程序员的问题。它只会生成

double y = pow(G(x, y), F(x)); 

当公式太复杂时,需要一些时间才能将其包含在 C/C++ 公式中。有什么办法可以进行这种转换吗?

I need to convert a math formula written in the Latex style to the function of a C/C++ code.
For example:
y = sin(x)^2 would become something like

double y = sin(x) * sin(x);

or

double y = pow(sin(x), 2);

where x is a variable defined somewhere before.
I mean that it should convert the latex formula to the C/C++ syntax. So that if there is a function y = G(x, y)^F(x) it doesn't matter what is G(x,y) and F(x),
it is a problem of the programmer to define it. It will just generate

double y = pow(G(x, y), F(x)); 

When the formula is too complicated it will take some time to make include it in the C/C++ formula. Is there any way to do this conversion?

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

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

发布评论

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

评论(4

安静被遗忘 2024-12-18 15:23:14

Emacs 的内置计算器 calc-mode 可以做到这一点(以及更多)。您的示例可以这样转换:

将公式放入某个 emacs 缓冲区中

$ y = sin(x)^2 $

将光标放在公式中,激活 calc-embedded 模式

M-x calc-embedded

将显示语言切换为 C:

M-x calc-c-language

就是这样:

$ y == pow(sin(x), 2) $

请注意,它将 Latex 中的“=”符号解释为等式,这会导致 C 为“==”。与 Cs 赋值运算符“=”等效的乳胶将是“\gets”。

有关此主题的更多信息,请访问 Turong 的博客

Emacs' built-in calculator calc-mode can do this (and much more). Your examples can be converted like this:

Put the formula in some emacs buffer

$ y = sin(x)^2 $

With the cursor in the formula, activate calc-embedded mode

M-x calc-embedded

Switch the display language to C:

M-x calc-c-language

There you are:

$ y == pow(sin(x), 2) $

Note that it interprets the '=' sign in latex as an equality, which results in '==' for C. The latex equivalent to Cs assignment operator '=' would be '\gets'.

More on this topic on Turong's blog

玩世 2024-12-18 15:23:14

我知道这个问题太旧了,但无论如何我都会添加一个回复,因为我认为这可能会对其他人有所帮助。在我的搜索中,这个问题经常出现。

我正在开发一个可以做类似事情的工具,在 public git repo

你必须把对乳胶输入的一些人为限制,这是毫无疑问的。
目前,我编写的工具仅支持 mul、div、add、sub、sqrt、pow、frac 和 sum,因为这些是我需要处理的唯一一组操作,并且通过提供预处理器,强加的限制可能会有点宽松(请参阅preproc.l 是一个[可能不太好的]示例),它将清除原始乳胶输入。

I know the question is too old, but I'll just add a reply anyway as a think it might help someone else later. The question popped up a lot for me in my searches.

I'm working on a tool that does something similar, in a public git repo

You'll have to put some artificial limitations on your latex input, that's out of question.
Currently the tool I wrote only supports mul, div, add, sub, sqrt, pow, frac and sum as those are the only set of operations I need to handle, and the imposed limitations can be a bit loose by providing a preprocessor (see preproc.l for an [maybe not-so-good] example) that would clean away the raw latex input.

我最亲爱的 2024-12-18 15:23:14

数学方程(例如 LaTeX 中的方程)和 C 表达式是不可互换的。前者陈述了两个术语之间的关系,后者定义了一个可以被评估的实体,明确地产生一个值。 C 中的 a = b 表示“获取变量 b 中的值并将其存储在变量 a 中”,而在 Math 中,则表示“在当前上下文中,a 和 b 相等'。第一个描述计算过程,第二个描述静态事实。因此,数学方程可以颠倒:a = b 等价于 b = a,但对 C 方程执行相同操作会产生完全不同的结果。

更糟糕的是,LaTeX 公式只包含渲染方程所需的信息;通常,这不足以捕捉它们的含义。

当然,某些 LaTeX 公式(例如您的示例)可以转换为 C 计算,但许多其他公式则不能,因此任何自动化方法的意义都有限。

A mathematical equation, such as the ones in LaTeX, and a C expression are not interchangeable. The former states a relation between two terms, the latter defines an entity that can be evaluated, unambiguously yielding one value. a = b in C means 'take the value in variable b and store it in variable a', wheres in Math, it means 'in the current context, a and b are equal'. The first describes a computation process, the second describes a static fact. Consequently, the Math equation can be reversed: a = b is equivalent to b = a, but doing the same to the C equation yields something quite different.

To make matters worse, LaTeX formulae only contain the information needed to render the equations; often, this is not enough to capture their meaning.

Of course some LaTeX formulae, like your example, can be converted into C computations, but many others cannot, so any automated way of doing so would only make limited sense.

独自←快乐 2024-12-18 15:23:14

我不确定是否有一个简单的答案,因为数学公式(在 LaTeX 文档中)实际上是不明确的,因此要将它们自动翻译为某些代码需要自动理解它们。

MathML 标准有两种形式表示公式(一种用于显示,另一种用于计算),这是有原因的。

I'm not sure there is a simple answer, because mathematical formulaes (in LaTeX documents) are actually ambiguous, so to automate their translation to some code requires automating their understanding.

And the MathML standard has, IIRC, two forms representing formulaes (one for displaying, another for computing) and there is some reason for that.

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