Mathics 3 体验 : Wolfram 语言的 Python 实现

【本文同步发表于本人的知乎专栏
Mathics 3 是什么?官网 上是这么说的:

Mathics is a free, open-source general-purpose computer algebra system featuring Mathematica®-compatible syntax and functions. It relies on a number of other Python libraries in the Python ecosystem.

简而言之,Mathics 使用 Python 构建,是 Wolfram 语言 的一种开源实现,包含了 Wolfram 语言的部分核心语法和主要功能(详见其官方 PDF 文档),并且该项目也有人正在维护。本文简要介绍了 Mathics 在 Windows 平台的安装和简单使用。

下载 Mathics 3

安装(Windows 平台)

Mathics 3 的下载可以参考 官方的安装指南 。安装过程也比较简单,在终端中依次执行下面命令即可(Conda中是没有这些包的,需要通过 pip 直接从PyPI下载):

pip install Mathics3             # this is the core engine. It is a dependency of some of the below too
pip install Mathics-Django[full] # web front-end with extras
pip install mathicsscript[full]  # the command-line interface with extras
pip install pymathics-natlang    # the Natural-language Python module (可选安装)
pip install pymathics-graph      # the Python module for working with Graphs and Networks (可选安装)

注意事项

  1. Mathics 的安装过程需要使用 Cython。因此,如果在 Windows 下安装 Mathics ,可能需要提前安装 VC 编译环境。可以根据 Python WiKi 的介绍 下载合适的编译环境。
  2. 如果 Mathics-Django 或者 mathicsscript 下载失败,则可以把 [full] 去除后再次尝试。
  3. 如果在运行 mathicsscript 时出现了找不到 cario 的错误,请去Github下载 Windows 下的 GTK+ 运行时 GTK-for-Windows-Runtime-Environment-Installer
  4. 如果需要使用 Mathics 3 构建 PDF 文档,则需要安装 xetex , asymptoteghostscript 三个额外依赖。具体的版本要求可见官网的下载指南。

Try Mathics 3

Mathics 分为 mathics , mathicsscriptmathicsserver 三个命令行程序。其中 mathics 支持运行 .m 的 Wolfram 语言脚本;mathicsscript 是支持代码补全的 REPL ;mathicsserver 则是在本地打开网页,可以像用 jupyter 一样使用 Mathics 。

交互模式

mathicsmathicsscript 两个命令均可进入 REPL 模式,如果需要代码补全可以使用 mathicsscript

笔记本模式

Mathics 的笔记本模式通过 mathicsserver 打开。

脚本模式

终端程序 mathics 支持直接运行 .m 的 Wolfram 语言脚本。这里我们举例简要说明。

创建一个 fib_test.m 的脚本,内容如下:

(* fib_test.m *)
fib[n_Integer] := Module[
    {f},
	f[0] = f[1] = 1;
	f[i_Integer] := f[i] = f[i-1] + f[i-2];
	f[n] (* return the function `f` *)
]
Print[ Table[{i, fib[i]}, {i, 0, 10}] ]

fib_test.m 所在文件夹打开终端,运行

mathics fib_test.m

这一脚本会输出如下结果,也就是斐波那契数列的前10项。

{{0, 1}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {5, 8}, {6, 13}, {7, 21}, {8, 34}, {9, 55}, {10, 89}}

Autoload Script

当 Mathics3 启动时,它会读取以 Wolfram 语言的 Mathics3 方言编写的文件,这些文件可在 mathics/autoload 文件夹中找到。

这里以我在 Windows 平台的的安装为例:
我在名为 mathics 的 conda 虚拟环境中安装了 Mathics,
虚拟环境位置是 D:\\ProgramData\\PyEnv\\mathics
则预启动脚本为 D:\\ProgramData\\PyEnv\\mathics\\Lib\\site-packages\\mathics\\autoload\\settings.m

绘图和图像的输出

截至本文投出之时,Mathics 对于绘图的选项设置、交互和导出仍然非常有限。

  1. Mathics 目前不支持使用 Manipulate 函数生成交互界面。
    其实在它的 mathics/builtin/manipulate.py 里可以看到 Manipulate 函数的定义,但全被注释掉了,也即是说目前处于弃用状态

  2. 仅在安装了 GTK+ 运行时的情况下,使用 Plot 之类的函数绘制出的图像可以被导出为 .svg 格式(其他格式暂不支持)。

例1:在下面例子中,Plot 函数生成的绘图对象目前仅可导出为 .svg 矢量图格式。
mathics的交互式输入和输出
这是由 mathics 生成的 D:\\Codes\\Sin[x].svg 图片。由于csdn不支持上传svg,我将其转成了 webp 图片。

Mathics 的 Plot 函数目前仅支持以下属性: PlotRange , Mesh , MaxRecursion

  1. 此外,Mathics 目前只支持将形状为 {x, y, 3} 的矩阵作为图像导出(需要将矩阵值归一化),若想将形状为 {x, y}的矩阵作为黑白图像导出,就得自行扩展成 {x, y, 3} 的形状。下面便是一个将形如 {x, y}的矩阵转成 {x, y, 3} 的灰度图的函数。
Matrix2GrayImage[matrix_] := Module[
    (* Declare local variables *)
    {arr = matrix, ndim, grayMin, grayMax, imgdata},
    (* Function body *)
    ndim = Length[Dimensions[arr]];  (* Check shape of arr *)
    If[(ndim == 2) ,
        (* Normalize arr when ndim == 2 is True*)
        grayMin = Min[arr];
        grayMax = Max[arr];
        arr = (arr - grayMin) / (grayMax - grayMin);
        (* Generate image *)
        imgdata = Map[ Function[{x}, {x,x,x}], arr, {2} ];
        Return[ Image[imgdata] ] ,
		(* else : raise failure *)
        Failure["InvalidInput",<||>]
    ]
]

关于 Complie 函数

Mathics 实现的 Complie 函数基于 llvmlite 或者 Python 内置的 compile 函数。当编译的表达式含有循环、变量赋值时,使用Python内置 compile 函数处理。

总结

相对于多数不再维护的 Wolfram 语言开源实现,Mathics 项目一直处于开发状态。但它相比起 Matlab 的开源替代 GNU Octave 来说还是不够活跃。虽然 Mathics 实现了 Wolfram 语言的部分主要功能,但 Mathics 实现的部分函数与 Mathematica 的对应版本存在差异,部分函数甚至尚未实现,比如:

  1. 实现了 FractionalPart 却没有 IntegerPart。在 Mathics 里,需要先自己定义 IntegerPart[x_] := x - FractionalPart[x] 。但是 FractionalPartMathics 的实现里已经包含了 IntegerPart 的计算,这样定义影响性能。

  2. 实现了 MinMax 却没有 MinMax。 在 Mathics 里,需要先自己定义

(* simple function docstring *)
MinMax::usage = "gives the minimum and maximum value of a list"
(* function body : pattern matching*)
MinMax[list_] := {Min[list], Max[list]};
MinMax[list_, delta_] := {Min[list] - delta, Max[list] + delta};
(* NOTE: `Scaled` function haven't implemented in Mathics. 
    Writing it into autoload would leads to a startup error! *)
MinMax[list_, Scaled[s_]] := {(1 + s) Min[list] - s Max[list], (1 + s) Max[list] - s Min[list]};
MinMax[list_, {dmin_, dmax_}] := {Min[list] - dmin, Max[list] + dmax};

同样地,MinMaxMathics 的实现里是对 list 里所有值的遍历,使用这种脚本方式声明一个 MinMax 本质上对一个 list 遍历了两次。

  1. 还有很多类似的情况,这里就不一一列举。

并且,基于 Python 开发的符号计算系统还有 SageMath 这一强力竞争者,显得还在不断开发的 Mathics 并不是那么有竞争力。

总的来说,考虑到其功能的不完善以及与 Mathematica 的差异, Mathics 更适合用于运行使用了 Wolfram 语言核心控制流的 .m 脚本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值