sagemath 入门笔记
Sagemath 简述
Sage(或称SageMath)是在GPL协议下发布的开源数学软件,并且整合了许多已有的开源软件包到一个基于Python的统一界面下。其目标是创造一个Magma, Maple, Mathematica 和 Matlab 的开源替代品。
Sage 包含了从线性代数、微积分,到密码学、数值计算、组合数学、群论、图论、数论等各种初高等数学的计算功能。
虽然 Sage 想要取代 Matlab,但使用上还是有一定的局限性。
现在的 Sage 版本是基于 Python3 的。
可以使用网页版,但网页版的坏处是没有智能提示、没有注解且运行麻烦。
Windows&Sagemath
安装
在 https://www.sagemath.org/download.html 中选择相应的安装程序下载并安装。
Sage 不在官网中提供 Windows 的二进制文件下载,需要在其 github 官方发布页中下载:
https://github.com/sagemath/sage-windows/releases
选择自己需要的版本,下载:


安装过程中特别注意 home directory
的选择,这将决定 Sage
Shell 的初始工作目录:

还有 Destination Location
即安装目录:

其他选项可以按默认配置进行。
使用
现在在 Sage home directory
目录下编写一个
.sage
脚本:
1 | print("hello world") |
我们打开 SageMath 9.3 Shell
,输入 ls
可以查看到我们刚刚编写的脚本文件:

使用指令 sage hello.sage
尝试运行我们的脚本:

成功运行即可!
同时我们也可以使用另一种方式运行我们的脚本:交互式 Shell
我们运行
SageMath 9.3
,第一次运行可能加载较慢,如下图所示:

其是基于 iPython 而定制的交互式 Shell,我们可以使用 Sage 内置函数
load(filepath)
来运行我们的脚本:

如果对于一些运算量较大的脚本,我们可以使用 Sagecell 在线运行我们的脚本得到结果:
https://sagecell.sagemath.org/
Python3.7
Sage-windows 实际上是基于 Cygwin 运行的 Python3.7 脚本。
想要使用 Python3.7 的其他包,我们需要手动使用 pip3
进行安装,但这里提供一个更加快捷的方式:找到本地的 Python3
根目录,将根目录下的 Lib\site-packages
文件夹复制后粘贴到
Sage 根目录下的 runtime\lib\python3.7\site-packages
这样相当于快速重新安装一遍已有的 Python 库,但推荐更稳妥的方式还是自己手动重新安装一遍所需要的库 (一般 Sage 已经安装了绝大多数需要使用到的库了)。
然后我们再在 SageMath 9.3 Console
中运行以下脚本:
1 | import pkg_resources |
以便快速更新所有脚本。
SageMath Console
SageMath Console
可以认为是 Sagemath 的一个交互式
iPython。
SageMath Shell
SageMath Shell
可以认为是基于 Windows 的 Cygwin
运行的一个终端,内置了许多有用的软件。
SageMath Notebook
SageMath Notebook
是一个基于 jupyter
的交互式 web 终端,打开后会开启一个本地 web 服务器方便使用 Sage。
具体的使用可以参考文章:https://zhuanlan.zhihu.com/p/297736314
Linux&Sagemath
基本上与 WSL&Sagemath一致,唯一区别不需要使用 VSCode 连接 WSL 环境。
WSL&Sagemath(推荐)
环境
必要的环境:
- WSL with Ubuntu 或者 WSL with Kali 等 Debian 系系统(如果是其他非 Debian 系统不保证可适用)
- VSCode
以上软件的安装均不作赘述,在网上有很多资料可供参考。
安装
进入 WSL 终端(如果不知道如何打开,可以使用 Powershell 直接输入 bash 命令调用),使用 apt 安装即可
1 | apt install sagemath |
大概有 1G ~ 2G 左右大小。
安装完后即可,如果 Python 版本和系统版本合适,它将依赖于系统的 Python 运行。
不同系统、不同 Python 版本可能会影响 Sagemath 的具体运行。
使用
Python
Sagemath 依赖于 Python 运行环境,同时在 Python 中也可以直接调用
Sagemath 的内容,仅需通过模块 sage
导入相应的内容即可。
最无脑的方式是
from sage.all import *
。
pip
换源问题请参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
Sage
可以直接在 WSL 中调用软件 sage
,进入 Sagemath
交互界面(类似 Python Idle、IPython),可以直接运行 Sagemath 代码。
VSCode & Jupyter(推荐)
点击 VSCode 左下角的蓝色按钮,如下图所示

会弹出窗口,选择 连接到WSL

可能会让你选择 WSL 的版本,选择即可,连接成功后,左下角将变为
这里我本人使用的是 WSL with Kali,如果是其他版本将显示其他版本的信息。

按下快捷键 Ctrl+Shift+`,或者手动点击菜单、终端、新建终端。

将会弹出类似下图的窗口(不同系统版本可能不一样的显示)

输入命令 mkdir sage
,这是新建文件夹的命令,如果不喜欢
sage
这个文件夹名,可以替换成其他的名字,例如
mkdir qsdzyyds
。
之后,请打开资源管理器选项卡,选择打开文件夹,选择你刚刚新建的文件夹即可,正常来说不会出现错误。

此时窗口可能会发生刷新,然后回进入界面

悬浮在
无标题(工作区)
,点击新建文件图标,输入文件名,需要注意后缀名一定是
ipynb
(包括以后需要新建文件的话最好也使用
ipynb
)

新建完文件后,可能 VSCode 会推荐你安装什么什么扩展,或者弹出什么什么提示,无论如何一律点同意、安装、允许等,最终界面应该如下所示

点击选择内核,会弹出 WSL 系统自带的 Python 和 Sagemath,建议选择 Python,如果你喜欢使用 Sagemath 也可以选择 Sagemath。

选择 Python 后,输入代码 from sage.all import *
,点击
全部运行
,如果没有报错,那么环境至此安装完成。
如果报错请从头开始,可能是你有什么环境或者扩展没有安装成功。

在这之后,使用 VSCode + WSL + Jupyter Notebook 的工作环境编写 Sagemath、Python 代码即可。
Jupyter Notebook 示例
点击 +代码
可以新建一个单元格(代码块),每一个单元格的代码都可以上下文有关地运行。
上下文有关的意思是,你现在运行的单元格的代码会依赖于在此之前所运行代码的结果。
输入代码,点击快捷键 Ctrl+Enter
即可运行当前光标所在单元格的代码。
需要注意,Jupyter Nodebook 会默认输出最后一行代码的结果(不需要使用
print
)

如果有需要,也可以使用 help()
函数获取 Sagemath
函数的帮助文档。

其他的,可以参考网络上的文章和 Sagemath 官方文档。
注意事项
pip 安装第三方模块
受不同环境的影响,需要注意的是:
有的系统中 Sagemath 可以直接调用系统中的 Python 第三方库,即全局
pip
安装的第三方库是 Python 与 Sagemath 混用的;
但有的系统的 Sagemath 可能依赖的 Python 版本不对,需要使用
sage-python -m pip
才能为 Sagemath 安装第三方 Python
库。