将数学函数转换为 lua
Convert mathematica functions to lua
我想读取使用 Mathematica 函数 'Save' 编写的文件。在里面,它们是我想翻译成 lua 的表达式。
例如:mathematica -> lua
foo[bar_]:= a*bar + b -> function foo(bar) return a*bar + b end
foo[bar_]= a*bar + b -> foo[bar] = a*bar + b
foo = N[bar] -> foo = bar
Pi or \[Pi] or -> math.pi
-7.809692029744407*^-8 -> -7.809692029744407e-8
2.7067*^-8 + 2.268*^-8*I -> 2.7067e-8 + 2.268e-8*math.i
这不一定是难题,我只需要学习 lua 正则表达式即可。但是他们需要考虑很多情况(上面没有提到),我不想 "reinvent the wheel"。
Maybe I should,你会说...
但是无论如何,是否有 lua 库或专门用于此的项目?
我不知道在那个方向上已经做了什么,但我建议你考虑在 Mathematica 中构建一个 Mathematica 到 Lua 的翻译器,比如 "LuaForm" 将其输出保存到文本文件。它将使用现有的砖块,例如 FortranForm / CForm 来转换基本表达式(例如数字和变量的代数组合),并且您可以在使用其他 Mathematica 功能时添加新规则。
CForm /@ {Pi, \[Pi], a b+3x, -7.809692029744407*^-8, 2.7067*^-8 + 2.268*^-8*I}
{Pi,Pi,a*b + 3*x,-7.809692029744407e-8,Complex(2.7067e-8,2.268e-8)}
我以前做过类似的代码(针对其他目标语言),结果令人满意。
我 wrote a translator 可以将 Mathematica 的一个子集转换成 Lua 和其他几种语言。
它仍在进行中,但它已经可以翻译像这样的简单 Mathematica 函数:
doSomething[a_,b_] :=
If[a<3,
(a = a + 1;a),a-1]
这是 Lua 中此函数的输出:
function doSomething(a,b)
if a<3 then
a=a+1
return a
else
return a-1
end
end
我还计划编写一个翻译器,将 Mathematica 的一个子集转换为 symmath-lua 符号。
我想读取使用 Mathematica 函数 'Save' 编写的文件。在里面,它们是我想翻译成 lua 的表达式。
例如:mathematica -> lua
foo[bar_]:= a*bar + b -> function foo(bar) return a*bar + b end
foo[bar_]= a*bar + b -> foo[bar] = a*bar + b
foo = N[bar] -> foo = bar
Pi or \[Pi] or -> math.pi
-7.809692029744407*^-8 -> -7.809692029744407e-8
2.7067*^-8 + 2.268*^-8*I -> 2.7067e-8 + 2.268e-8*math.i
这不一定是难题,我只需要学习 lua 正则表达式即可。但是他们需要考虑很多情况(上面没有提到),我不想 "reinvent the wheel"。 Maybe I should,你会说...
但是无论如何,是否有 lua 库或专门用于此的项目?
我不知道在那个方向上已经做了什么,但我建议你考虑在 Mathematica 中构建一个 Mathematica 到 Lua 的翻译器,比如 "LuaForm" 将其输出保存到文本文件。它将使用现有的砖块,例如 FortranForm / CForm 来转换基本表达式(例如数字和变量的代数组合),并且您可以在使用其他 Mathematica 功能时添加新规则。
CForm /@ {Pi, \[Pi], a b+3x, -7.809692029744407*^-8, 2.7067*^-8 + 2.268*^-8*I}
{Pi,Pi,a*b + 3*x,-7.809692029744407e-8,Complex(2.7067e-8,2.268e-8)}
我以前做过类似的代码(针对其他目标语言),结果令人满意。
我 wrote a translator 可以将 Mathematica 的一个子集转换成 Lua 和其他几种语言。
它仍在进行中,但它已经可以翻译像这样的简单 Mathematica 函数:
doSomething[a_,b_] :=
If[a<3,
(a = a + 1;a),a-1]
这是 Lua 中此函数的输出:
function doSomething(a,b)
if a<3 then
a=a+1
return a
else
return a-1
end
end
我还计划编写一个翻译器,将 Mathematica 的一个子集转换为 symmath-lua 符号。