@why-math
2015-04-26T09:29:12.000000Z
字数 1313
阅读 1404
sage
除了少数的例外, Sage 使用 Python 的编程语言, 所以大多数介绍性的 Python 书籍都会帮助你学习 Sage.
Sage 使用 = 表示赋值, ==, <=, >=, < 和 >表示比较:
sage: a = 5sage: a5sage: 2 == 2Truesage: 2 == 3Falsesage: 2 < 3Truesage: a == 5True
Sage 提供所有基础的数学运算:
sage: 2**3 # ** 指数运算8sage: 2^3 # ^ 同 ** (这一点与 Python 不同)8sage: 10 % 3 # 对于整数参数, % 表示求余1sage: 10/45/2sage: 10//4 # 对于整数参数, // 返回整数商2sage: 4 * (10 // 4) + 10 % 4 == 10Truesage: 3^2*4 + 2%538
表达式(如3^2*4 + 2%5)的计算操作应用的顺序.
Sage 也提供很多常见的数学函数, 如下面的几个例子:
sage: sqrt(3.4)1.84390889145858sage: sin(5.135)-0.912021158525540sage: sin(pi/3)1/2*sqrt(3)
上面的最后一个例子表明, 某些数学表达式返回精确的值, 而不是数值逼近. 为了获得数值逼近, 可用函数 n 或者 方法 n (两者都有一个长名字 numerical_approx, 函数 N 和 n 是一样的). 它们都接收可选参数 prec, 用来指定精度的二进制位数, 以及参数 digits指定十进制位数; 默认的二进制精度位数为 53.
sage: exp(2)e^2sage: n(exp(2))7.38905609893065sage: sqrt(pi).numerical_approx()1.77245385090552sage: sin(10).n(digits=5)-0.54402sage: N(sin(10),digits=10)-0.5440211109sage: numerical_approx(pi, prec=200)3.1415926535897932384626433832795028841971693993751058209749
Python 是一种动态类型语言, 所以每个变量的值都有一个类型相伴, 但是一个给定的变量在作用域(scope)内可以是任意 Python 类型的值.
sage: a = 5 # a is an integersage: type(a)<type 'sage.rings.integer.Integer'>sage: a = 5/3 # now a is a rational numbersage: type(a)<type 'sage.rings.rational.Rational'>sage: a = 'hello' # now a is a stringsage: type(a)<type 'str'>
C 是静态类型语言, 所以会非常不同;一个值为整型的变量只能在作用域内保存整型的值.
Python 中一个潜在的可能混淆的地方是, 以 0 开始的数字是表示 8 进制数.
sage: 0119sage: 8 + 19sage: n = 011sage: n.str(8) # string representation of n in base 8'11'
这是和 C 语言是一致的.
