数论
Sage对数论有广泛的支持。例如我们可以在$\Zeta/N\Zeta$上进行运算:
sage: R = IntegerModRing(97)sage: a = R(2) / R(3)sage: a33sage: a.rational_reconstruction()2/3sage: b = R(47)sage: b^2005200550sage: b.modulus()97sage: b.is_square()True
Sage包含标准的数论函数。如:
sage: gcd(515,2005)5sage: factor(2005)5 * 401sage: c = factorial(25); c15511210043330985984000000sage:[valuation(c,p) for p in prime_range(2,23)][22, 10, 6, 3, 2, 1, 1, 1]sage: next_prime(2005)2011sage: previous_prime(2005)2003sage: divisors(28); sum(divisors(28)); 2*28[1, 2, 4, 7, 14, 28]5656
完美!
Sage的sigma(n,k)函数求n的所有因子的$k$次幂的和:
sage: sigma(28,0); sigma(28,1); sigma(28,2)6561050
下面展示的是扩展Euclidean算法,Euler的$\phi$函数和中国剩余定理:
sage: d,u,v = xgcd(12,15)sage: d == u*12 + v*15Truesage: n = 2005sage: inverse_mod(3,n)1337sage: 3 * 13374011sage: prime_divisors(n)[5, 401]sage: phi = n*prod([1 - 1/p for p in prime_divisors(n)]); phi1600sage: euler_phi(n)1600sage: prime_to_m_part(n, 5)401
下面验证关于$3n+1$问题的一些内容。
sage: n = 2005sage: for i in range(1000):....: n = 3*odd_part(n) + 1....: if odd_part(n)==1:....: print i....: break38
最后我们展示中国剩余定理。
sage: x = crt(2, 1, 3, 5); x-4sage: x % 3 # x mod 3 = 22sage: x % 5 # x mod 5 = 11sage:[binomial(13,m) for m in range(14)][1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1]sage:[binomial(13,m)%2 for m in range(14)][1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]sage:[kronecker(m,13) for m in range(1,13)][1, -1, 1, 1, -1, -1, -1, -1, 1, 1, -1, 1]sage: n = 10000; sum([moebius(m) for m in range(1,n)])-23Partitions(4).list()[[4],[3, 1],[2, 2],[2, 1, 1],[1, 1, 1, 1]]
$p$-adic数
Sage支持$p$-adic数域。注意,一旦建立了一个$p$-adic 数域,就不能再修改它的精度了。
sage: K = Qp(11); K11-adic Field with capped relative precision 20sage: a = K(211/17); a4 + 4*11 + 11^2 + 7*11^3 + 9*11^5 + 5*11^6 + 4*11^7 + 8*11^8 + 7*11^9+ 9*11^10 + 3*11^11 + 10*11^12 + 11^13 + 5*11^14 + 6*11^15 + 2*11^16+ 3*11^17 + 11^18 + 7*11^19 + O(11^20)sage: b = K(3211/11^2); b10*11^-2 + 5*11^-1 + 4 + 2*11 + O(11^18)
在$p$-adic域或数域上的整数环的实现工作已经完成了很多。感兴趣的读者可以通过阅读[$p$-adics介绍]或到Ssage-supportGoogle群组上向专家们询问相关细节。
许多相关方法已经在NumberField类中实现了。
sage: R.<x> = PolynomialRing(QQ)sage: K = NumberField(x^3 + x^2 - 2*x + 8, 'a')sage: K.integral_basis()[1, 1/2*a^2 + 1/2*a, a^2]
sage: K.galois_group(type="pari")Galois group 3T2 (S3) with order 6 of x^3 + x^2 - 2*x + 8
sage: K.polynomial_quotient_ring()Univariate Quotient Polynomial Ring in a over Rational Field with modulusx^3 + x^2 - 2*x + 8sage: K.units()[3*a^2 + 13*a + 13]sage: K.discriminant()-503sage: K.class_group()Class group of order 1 with structure of Number Field in a withdefining polynomial x^3 + x^2 - 2*x + 8sage: K.class_number()1
