sage_basic
Introduction¶
[!QUOTE]
Sage is free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. Both the Sage development model and the technology in Sage itself are distinguished by an extremely strong emphasis on openness, community, cooperation, and collaboration: we are building the car, not reinventing the wheel. The overall goal of Sage is to create a viable, free, open-source alternative to Maple, Mathematica, Magma, and MATLAB.
Basic use¶
在简单语法上和 python 很类似,函数的定义和使用是一样的,就不讲了;如果想要在 python 中使用,就需要先导入 sage 库 from sage.all import *
即可。
下面的内容基于 ipynb/jupyter 形式实现,使用 sageMath 10.3 。
from sage.all import *
2+2, 2**3, 5*5, 5**2, 2*2**3, 2**2**3**0
(4, 8, 25, 25, 16, 4)
factor(2260305277112127131723251710487571168300606058081777971297059135638227918807397016980532281858292026960397175871392336597693440)
2^10 * 5 * 7^2 * 37 * 193 * 331 * 569 * 5153 * 65381 * 146317 * 155943343 * 9206678941 * 18623046139 * 1290117019611958829 * 6846725164570923688379767 * 575390540597317088017221077
[!QUOTE]
As the last example shows, some mathematical expressions return ‘exact’ values, rather than numerical approximations. To get a numerical approximation, use either the function N or the method n (and both of these have a longer name, numerical_approx, and the function N is the same as n). These take optional arguments prec, which is the requested number of bits of precision, and digits, which is the requested number of decimal digits of precision; the default is 53 bits of precision.
exp(2), n(exp(2)), numerical_approx(pi, prec=200)
(e^2, 7.38905609893065, 3.1415926535897932384626433832795028841971693993751058209749)
x = var('x')
f = 1/((1+x)*(x-1))
f.partial_fraction(x)
-1/2/(x + 1) + 1/2/(x - 1)
get help¶
[!QUOTE]
Sage has extensive built-in documentation, accessible by typing the name of a function or a constant (for example), followed by a question mark (?); alse, we can use the
help()
function.
data structure¶
和 python 一样,sage 也有列表,字典,元组,集合等数据结构。
list(range(2, 20, 3)), vector(range(2, 20, 3))
([2, 5, 8, 11, 14, 17], (2, 5, 8, 11, 14, 17))
不同的是,Sage math 的列表支持不同类型的元素,而 Python 的列表只支持一种类型;同样的,使用 len() 获取列表长度,使用 append() 添加元素,使用 remove() 删除元素。
l = [1, 3.2, "hello", (2, 3), [1, 2, 3]]
for i in l:
print(i, end=" ")
1 3.20000000000000 hello (2, 3) [1, 2, 3]
字典等也是同理,支持不同类型的元素。
d = {1: "one", 'pi': 3.1415 , "admin": "darstib"}
for k in d.keys():
print(k, d[k], end=" # ")
1 one # pi 3.14150000000000 # admin darstib #
x = var('x')
y = sin(x)
diff(y, x), integrate(y, x), limit(y, x=0), limit(y, x=oo), solve(y == 0, x)
(cos(x), -cos(x), 0, ind, [x == 0])
求解器 solve ¶
当然也可以创建多个变量,这和 z3 求解器是类似的。
a, b, c = var('a, b, c')
solve([a + b + c == 1, a + 2*b + 4*c == 2, a + 3*b + 9*c == 3], a, b, c)
[[a == 0, b == 1, c == 0]]
我们可以用这个来求线性方程组等等;对于多解情况,可以直接输出,也可以存起来以便控制输出精度。
var('x y p q')
eq1 = p+q==9
eq2 = q*y+p*x==-6
eq3 = q*y**2+p*x**2==24
solns = solve([eq1,eq2,eq3,p==1],p,q,x,y, solution_dict=True)
[[s[p].n(10), s[q].n(10), s[x].n(10), s[y].n(10)] for s in solns]
[[1.0, 8.0, -4.9, -0.14], [1.0, 8.0, 3.6, -1.2]]
求根 find_root ¶
有时候,solve 能够找到一个解,但是无法给出粗略值,这时候就可以使用 find_root 函数。
var('x')
solve(sin(x) == cos(x), x), find_root(sin(x) == cos(x), 0, pi) # 限制范围在 0 到 pi 之间
([sin(x) == cos(x)], 0.7853981633974484)
微积分 ¶
在前面我们也提到过,在密码学中,我们经常需要对函数进行求导和积分,在 SageMath 中,我们也可以很方便地进行求导和积分。 直接使用 diff 共可接收三个参数,按照我们习惯的说法就是 (f(x), x, n) 其中 n 表示微分阶数。
diff(sin(x^2), x, 4)
16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)
多元微分咋办?
x, y = var('x,y')
f = x^2 + 17*y^2
f.diff(x), f.diff(y)
(2*x, 34*y)
(不)定积分怎么办?
integral(x*sin(x^2), x), integral(x/(x^2+1), x, 0, 1) # 后两个参数表示积分上下限
(-1/2*cos(x^2), 1/2*log(2))