diff --git a/VERSION.txt b/VERSION.txt index d4d5de2d5af..c152a49d9d0 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -Sage version 7.0.rc1, released 2016-01-14 +Sage version 7.0, released 2016-01-19 diff --git a/build/pkgs/configure/checksums.ini b/build/pkgs/configure/checksums.ini index 952cc1eab82..0482c018bb4 100644 --- a/build/pkgs/configure/checksums.ini +++ b/build/pkgs/configure/checksums.ini @@ -1,4 +1,4 @@ tarball=configure-VERSION.tar.gz -sha1=5718238d9da2cc8a70a66d9dff3f6da33d0cb579 -md5=3f84f3806e725dbfe742d8b6a22750f9 -cksum=7625246 +sha1=631a348f0ac864b3ec37dfcb1d7fab79e213a075 +md5=5759d84c96102aee89c7eaea0aadf842 +cksum=2510923339 diff --git a/build/pkgs/configure/package-version.txt b/build/pkgs/configure/package-version.txt index 897bdc8200c..dee261df401 100644 --- a/build/pkgs/configure/package-version.txt +++ b/build/pkgs/configure/package-version.txt @@ -1 +1 @@ -139 +140 diff --git a/src/bin/sage-banner b/src/bin/sage-banner index e56222a0a7a..1d71aa46db5 100644 --- a/src/bin/sage-banner +++ b/src/bin/sage-banner @@ -1,8 +1,5 @@ ┌────────────────────────────────────────────────────────────────────┐ -│ SageMath Version 7.0.rc1, Release Date: 2016-01-14 │ +│ SageMath Version 7.0, Release Date: 2016-01-19 │ │ Type "notebook()" for the browser-based notebook interface. │ │ Type "help()" for help. │ └────────────────────────────────────────────────────────────────────┘ -┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ -┃ Warning: this is a prerelease version, and it may be unstable. ┃ -┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ diff --git a/src/bin/sage-location b/src/bin/sage-location index 03a3d12eca3..1315b2b5342 100755 --- a/src/bin/sage-location +++ b/src/bin/sage-location @@ -275,8 +275,36 @@ def sage_relocate(): write_location_file() +RELOCATION_ERROR = """ +ERROR: The Sage installation tree has moved + +from {OLD_SAGE_ROOT} + to {SAGE_ROOT} + +This is not supported, and Sage will not work. To install Sage from a +binary package: + +1. Open the .tar.bz2 archive (or .dmg on OSX) + +2. Move the SageMath folder/app to where you want it to be. You can + also rename the directory now. + +3. Start sage for the first time. This will then automatically patch + paths in binaries. + +After starting Sage for the first time you cannot change the +installation any more. To install Sage elsewhere, start over from the +binary package. Or recompile Sage from scratch in the new location +("make distclean && make") +""" + + if __name__ == '__main__': + if SAGE_ROOT is None: + print('You must run this script from within a Sage shell') + sys.exit(1) + # Previous SAGE_ROOT, read from "sage-location.txt". # OLD_SAGE_ROOT is None if this is a first-time install. OLD_SAGE_ROOT = read_location_file() @@ -292,12 +320,16 @@ if __name__ == '__main__': if OLD_SAGE_ROOT != SAGE_ROOT or force_relocate: if OLD_SAGE_ROOT is None: print("This looks like the first time you are running Sage.") + elif OLD_SAGE_ROOT != SAGE_ROOT: + print(RELOCATION_ERROR.format(OLD_SAGE_ROOT=OLD_SAGE_ROOT, SAGE_ROOT=SAGE_ROOT)) + sys.exit(1) elif force_relocate: print("Forcing sage-location, probably because a new package was installed.") else: print("The Sage installation tree has moved") print("from %s" % OLD_SAGE_ROOT) print(" to %s" % SAGE_ROOT) + assert(False) print("Updating various hardcoded paths...") print("(Please wait at most a few minutes.)") print("DO NOT INTERRUPT THIS.") diff --git a/src/bin/sage-version.sh b/src/bin/sage-version.sh index a7baec7e945..0853b4fc51d 100644 --- a/src/bin/sage-version.sh +++ b/src/bin/sage-version.sh @@ -1,4 +1,4 @@ # Sage version information for shell scripts # This file is auto-generated by the sage-update-version script, do not edit! -SAGE_VERSION='7.0.rc1' -SAGE_RELEASE_DATE='2016-01-14' +SAGE_VERSION='7.0' +SAGE_RELEASE_DATE='2016-01-19' diff --git a/src/doc/en/reference/rings_standard/index.rst b/src/doc/en/reference/rings_standard/index.rst index 3ff6764c9b6..4e8cb50e828 100644 --- a/src/doc/en/reference/rings_standard/index.rst +++ b/src/doc/en/reference/rings_standard/index.rst @@ -15,7 +15,7 @@ Integers sage/rings/fast_arith sage/rings/sum_of_squares sage/ext/multi_modular - sage/rings/arith + sage/arith/misc .. SEEALSO:: diff --git a/src/doc/en/thematic_tutorials/profiling.rst b/src/doc/en/thematic_tutorials/profiling.rst index afb68ab7633..384ab77d362 100644 --- a/src/doc/en/thematic_tutorials/profiling.rst +++ b/src/doc/en/thematic_tutorials/profiling.rst @@ -99,7 +99,7 @@ when ``code_to_run`` is executed:: 1193 def random_prime(n, proof=None, lbound=2): ... ... 1251 # since we don't want current_randstate to get - 1252 # pulled when you say "from sage.arith import *". + 1252 # pulled when you say "from sage.arith.all import *". 1253 1 11 11.0 0.0 from sage.misc.randstate import current_randstate 1254 1 7 7.0 0.0 from sage.structure.proof.proof import get_flag 1255 1 6 6.0 0.0 proof = get_flag(proof, "arithmetic") diff --git a/src/sage/algebras/iwahori_hecke_algebra.py b/src/sage/algebras/iwahori_hecke_algebra.py index a934ad7eb15..81d71c94a10 100644 --- a/src/sage/algebras/iwahori_hecke_algebra.py +++ b/src/sage/algebras/iwahori_hecke_algebra.py @@ -29,7 +29,7 @@ from sage.rings.all import ZZ from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing from sage.rings.polynomial.polydict import ETuple -from sage.rings.arith import is_square +from sage.arith.all import is_square from sage.combinat.root_system.weyl_group import WeylGroup from sage.combinat.family import Family from sage.combinat.free_module import CombinatorialFreeModule, CombinatorialFreeModuleElement diff --git a/src/sage/algebras/quatalg/quaternion_algebra.py b/src/sage/algebras/quatalg/quaternion_algebra.py index a08d256f16b..331b02ab6b8 100644 --- a/src/sage/algebras/quatalg/quaternion_algebra.py +++ b/src/sage/algebras/quatalg/quaternion_algebra.py @@ -33,9 +33,8 @@ #***************************************************************************** -from sage.rings.arith import (GCD, - hilbert_conductor_inverse, hilbert_conductor, - factor, gcd, lcm, kronecker_symbol, valuation) +from sage.arith.all import (hilbert_conductor_inverse, hilbert_conductor, + factor, gcd, lcm, kronecker_symbol, valuation) from sage.rings.all import RR, Integer from sage.rings.integer_ring import ZZ from sage.rings.rational import Rational diff --git a/src/sage/algebras/quatalg/quaternion_algebra_element.pyx b/src/sage/algebras/quatalg/quaternion_algebra_element.pyx index fa5e0278fae..849bb1bf6b3 100644 --- a/src/sage/algebras/quatalg/quaternion_algebra_element.pyx +++ b/src/sage/algebras/quatalg/quaternion_algebra_element.pyx @@ -22,7 +22,6 @@ from sage.structure.element cimport AlgebraElement, RingElement, ModuleElement, from sage.algebras.quatalg.quaternion_algebra_element cimport QuaternionAlgebraElement_abstract from sage.rings.rational cimport Rational from sage.rings.integer cimport Integer -from sage.rings.arith import lcm from sage.rings.polynomial.polynomial_integer_dense_flint cimport Polynomial_integer_dense_flint from sage.rings.number_field.number_field_element cimport NumberFieldElement from sage.rings.all import PolynomialRing diff --git a/src/sage/algebras/steenrod/steenrod_algebra.py b/src/sage/algebras/steenrod/steenrod_algebra.py index f1572057139..47bd271387b 100644 --- a/src/sage/algebras/steenrod/steenrod_algebra.py +++ b/src/sage/algebras/steenrod/steenrod_algebra.py @@ -578,7 +578,7 @@ def __init__(self, p=2, basis='milnor', **kwds): sage: TestSuite(SteenrodAlgebra(basis='comm_deg', p=5)).run() # long time sage: TestSuite(SteenrodAlgebra(p=2,generic=True)).run() """ - from sage.rings.arith import is_prime + from sage.arith.all import is_prime from sage.categories.graded_hopf_algebras_with_basis import GradedHopfAlgebrasWithBasis from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets diff --git a/src/sage/algebras/steenrod/steenrod_algebra_mult.py b/src/sage/algebras/steenrod/steenrod_algebra_mult.py index 6e1e29e9198..516738edb65 100644 --- a/src/sage/algebras/steenrod/steenrod_algebra_mult.py +++ b/src/sage/algebras/steenrod/steenrod_algebra_mult.py @@ -622,7 +622,7 @@ def multinomial_odd(list,p): 105 """ from sage.rings.all import GF, Integer - from sage.rings.arith import binomial + from sage.arith.all import binomial n = sum(list) answer = 1 F = GF(p) diff --git a/src/sage/all.py b/src/sage/all.py index 3ddfe089bff..658a3451a36 100644 --- a/src/sage/all.py +++ b/src/sage/all.py @@ -97,6 +97,7 @@ from sage.structure.all import * from sage.rings.all import * +from sage.arith.all import * from sage.matrix.all import * # This must come before Calculus -- it initializes the Pynac library. diff --git a/src/sage/arith/__init__.py b/src/sage/arith/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/sage/arith/all.py b/src/sage/arith/all.py new file mode 100644 index 00000000000..2fbd0c0cb66 --- /dev/null +++ b/src/sage/arith/all.py @@ -0,0 +1,23 @@ +from .misc import (algdep, bernoulli, is_prime, is_prime_power, + is_pseudoprime, is_pseudoprime_power, is_pseudoprime_small_power, + prime_powers, primes_first_n, eratosthenes, primes, + next_prime_power, next_probable_prime, next_prime, + previous_prime, previous_prime_power, random_prime, + divisors, sigma, gcd, GCD, lcm, LCM, xlcm, xgcd, xkcd, + inverse_mod, get_gcd, get_inverse_mod, power_mod, + rational_reconstruction, mqrr_rational_reconstruction, + trial_division, factor, prime_divisors, odd_part, prime_to_m_part, + is_square, is_squarefree, euler_phi, crt, CRT, CRT_list, CRT_basis, + CRT_vectors, multinomial, multinomial_coefficients, + binomial, factorial, kronecker_symbol, kronecker, legendre_symbol, + primitive_root, nth_prime, quadratic_residues, moebius, + continuant, number_of_divisors, hilbert_symbol, hilbert_conductor, + hilbert_conductor_inverse, falling_factorial, rising_factorial, + integer_ceil, integer_floor, + two_squares, three_squares, four_squares, sum_of_k_squares, + subfactorial, is_power_of_two, differences, + sort_complex_numbers_for_display, + fundamental_discriminant, squarefree_divisors, + Sigma, radical, Euler_Phi, binomial_coefficients, jacobi_symbol, + Moebius, dedekind_sum, + prime_factors, prime_range, valuation) diff --git a/src/sage/arith/misc.py b/src/sage/arith/misc.py new file mode 100644 index 00000000000..6690669d8ac --- /dev/null +++ b/src/sage/arith/misc.py @@ -0,0 +1,5313 @@ +""" +Miscellaneous arithmetic functions +""" + +#***************************************************************************** +# Copyright (C) 2006 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** + +from __future__ import absolute_import + +import math + +from sage.misc.misc import powerset +from sage.misc.misc_c import prod + +from sage.libs.pari.all import pari +import sage.libs.flint.arith as flint_arith + +from sage.structure.element import parent +from sage.structure.coerce import py_scalar_to_element + +from sage.rings.rational_field import QQ +from sage.rings.integer_ring import ZZ +from sage.rings.integer import Integer, GCD_list, LCM_list +from sage.rings.rational import Rational +from sage.rings.real_mpfr import RealNumber +from sage.rings.complex_number import ComplexNumber + +import sage.rings.fast_arith as fast_arith +prime_range = fast_arith.prime_range + + +################################################################## +# Elementary Arithmetic +################################################################## + +def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None, use_digits=None, height_bound=None, proof=False): + """ + Returns a polynomial of degree at most `degree` which is + approximately satisfied by the number `z`. Note that the returned + polynomial need not be irreducible, and indeed usually won't be if + `z` is a good approximation to an algebraic number of degree less + than `degree`. + + You can specify the number of known bits or digits of `z` with + ``known_bits=k`` or ``known_digits=k``. PARI is then told to + compute the result using `0.8k` of these bits/digits. Or, you can + specify the precision to use directly with ``use_bits=k`` or + ``use_digits=k``. If none of these are specified, then the precision + is taken from the input value. + + A height bound may be specified to indicate the maximum coefficient + size of the returned polynomial; if a sufficiently small polynomial + is not found, then ``None`` will be returned. If ``proof=True`` then + the result is returned only if it can be proved correct (i.e. the + only possible minimal polynomial satisfying the height bound, or no + such polynomial exists). Otherwise a ``ValueError`` is raised + indicating that higher precision is required. + + ALGORITHM: Uses LLL for real/complex inputs, PARI C-library + ``algdep`` command otherwise. + + Note that ``algebraic_dependency`` is a synonym for ``algdep``. + + INPUT: + + + - ``z`` - real, complex, or `p`-adic number + + - ``degree`` - an integer + + - ``height_bound`` - an integer (default: ``None``) specifying the maximum + coefficient size for the returned polynomial + + - ``proof`` - a boolean (default: ``False``), requires height_bound to be set + + + EXAMPLES:: + + sage: algdep(1.888888888888888, 1) + 9*x - 17 + sage: algdep(0.12121212121212,1) + 33*x - 4 + sage: algdep(sqrt(2),2) + x^2 - 2 + + This example involves a complex number:: + + sage: z = (1/2)*(1 + RDF(sqrt(3)) *CC.0); z + 0.500000000000000 + 0.866025403784439*I + sage: p = algdep(z, 6); p + x^3 + 1 + sage: p.factor() + (x + 1) * (x^2 - x + 1) + sage: z^2 - z + 1 # abs tol 2e-16 + 0.000000000000000 + + This example involves a `p`-adic number:: + + sage: K = Qp(3, print_mode = 'series') + sage: a = K(7/19); a + 1 + 2*3 + 3^2 + 3^3 + 2*3^4 + 2*3^5 + 3^8 + 2*3^9 + 3^11 + 3^12 + 2*3^15 + 2*3^16 + 3^17 + 2*3^19 + O(3^20) + sage: algdep(a, 1) + 19*x - 7 + + These examples show the importance of proper precision control. We + compute a 200-bit approximation to `sqrt(2)` which is wrong in the + 33'rd bit:: + + sage: z = sqrt(RealField(200)(2)) + (1/2)^33 + sage: p = algdep(z, 4); p + 227004321085*x^4 - 216947902586*x^3 - 99411220986*x^2 + 82234881648*x - 211871195088 + sage: factor(p) + 227004321085*x^4 - 216947902586*x^3 - 99411220986*x^2 + 82234881648*x - 211871195088 + sage: algdep(z, 4, known_bits=32) + x^2 - 2 + sage: algdep(z, 4, known_digits=10) + x^2 - 2 + sage: algdep(z, 4, use_bits=25) + x^2 - 2 + sage: algdep(z, 4, use_digits=8) + x^2 - 2 + + Using the ``height_bound`` and ``proof`` parameters, we can see that + `pi` is not the root of an integer polynomial of degree at most 5 + and coefficients bounded above by 10:: + + sage: algdep(pi.n(), 5, height_bound=10, proof=True) is None + True + + For stronger results, we need more precicion:: + + sage: algdep(pi.n(), 5, height_bound=100, proof=True) is None + Traceback (most recent call last): + ... + ValueError: insufficient precision for non-existence proof + sage: algdep(pi.n(200), 5, height_bound=100, proof=True) is None + True + + sage: algdep(pi.n(), 10, height_bound=10, proof=True) is None + Traceback (most recent call last): + ... + ValueError: insufficient precision for non-existence proof + sage: algdep(pi.n(200), 10, height_bound=10, proof=True) is None + True + + We can also use ``proof=True`` to get positive results:: + + sage: a = sqrt(2) + sqrt(3) + sqrt(5) + sage: algdep(a.n(), 8, height_bound=1000, proof=True) + Traceback (most recent call last): + ... + ValueError: insufficient precision for uniqueness proof + sage: f = algdep(a.n(1000), 8, height_bound=1000, proof=True); f + x^8 - 40*x^6 + 352*x^4 - 960*x^2 + 576 + sage: f(a).expand() + 0 + + TESTS:: + + sage: algdep(complex("1+2j"), 4) + x^2 - 2*x + 5 + """ + if proof and not height_bound: + raise ValueError("height_bound must be given for proof=True") + + x = ZZ['x'].gen() + + z = py_scalar_to_element(z) + + if isinstance(z, Integer): + if height_bound and abs(z) >= height_bound: + return None + return x - ZZ(z) + + degree = ZZ(degree) + + if isinstance(z, Rational): + if height_bound and max(abs(z.denominator()), abs(z.numerator())) >= height_bound: + return None + return z.denominator()*x - z.numerator() + + if isinstance(z, (RealNumber, ComplexNumber)): + + log2_10 = math.log(10,2) + + prec = z.prec() - 6 + if known_digits is not None: + known_bits = known_digits * log2_10 + if known_bits is not None: + use_bits = known_bits * 0.8 + if use_digits is not None: + use_bits = use_digits * log2_10 + if use_bits is not None: + prec = int(use_bits) + + is_complex = isinstance(z, ComplexNumber) + n = degree+1 + from sage.matrix.all import matrix + M = matrix(ZZ, n, n+1+int(is_complex)) + r = ZZ.one() << prec + M[0, 0] = 1 + M[0, -1] = r + for k in range(1, degree+1): + M[k, k] = 1 + r *= z + if is_complex: + M[k, -1] = r.real().round() + M[k, -2] = r.imag().round() + else: + M[k, -1] = r.round() + LLL = M.LLL(delta=.75) + coeffs = LLL[0][:n] + if height_bound: + def norm(v): + # norm on an integer vector invokes Integer.sqrt() which tries to factor... + from sage.rings.real_mpfi import RIF + return v.change_ring(RIF).norm() + if max(abs(a) for a in coeffs) > height_bound: + if proof: + # Given an LLL reduced basis $b_1, ..., b_n$, we only + # know that $|b_1| <= 2^((n-1)/2) |x|$ for non-zero $x \in L$. + if norm(LLL[0]) <= 2**((n-1)/2) * n.sqrt() * height_bound: + raise ValueError("insufficient precision for non-existence proof") + return None + elif proof and norm(LLL[1]) < 2**((n-1)/2) * max(norm(LLL[0]), n.sqrt()*height_bound): + raise ValueError("insufficient precision for uniqueness proof") + if coeffs[degree] < 0: + coeffs = -coeffs + f = list(coeffs) + + elif proof or height_bound: + raise NotImplementedError("proof and height bound only implemented for real and complex numbers") + + else: + y = pari(z) + f = y.algdep(degree) + + return x.parent()(f) + + +algebraic_dependency = algdep + +def bernoulli(n, algorithm='default', num_threads=1): + r""" + Return the n-th Bernoulli number, as a rational number. + + INPUT: + + - ``n`` - an integer + - ``algorithm``: + + - ``'default'`` -- use 'flint' for n <= 300000, and 'bernmm' + otherwise (this is just a heuristic, and not guaranteed to be + optimal on all hardware) + - ``'flint'`` -- use the FLINT library + - ``'pari'`` -- use the PARI C library + - ``'gap'`` -- use GAP + - ``'gp'`` -- use PARI/GP interpreter + - ``'magma'`` -- use MAGMA (optional) + - ``'bernmm'`` -- use bernmm package (a multimodular algorithm) + + - ``num_threads`` - positive integer, number of + threads to use (only used for bernmm algorithm) + + EXAMPLES:: + + sage: bernoulli(12) + -691/2730 + sage: bernoulli(50) + 495057205241079648212477525/66 + + We demonstrate each of the alternative algorithms:: + + sage: bernoulli(12, algorithm='flint') + -691/2730 + sage: bernoulli(12, algorithm='gap') + -691/2730 + sage: bernoulli(12, algorithm='gp') + -691/2730 + sage: bernoulli(12, algorithm='magma') # optional - magma + -691/2730 + sage: bernoulli(12, algorithm='pari') + -691/2730 + sage: bernoulli(12, algorithm='bernmm') + -691/2730 + sage: bernoulli(12, algorithm='bernmm', num_threads=4) + -691/2730 + + TESTS:: + + sage: algs = ['gap','gp','pari','bernmm','flint'] + sage: test_list = [ZZ.random_element(2, 2255) for _ in range(500)] + sage: vals = [[bernoulli(i,algorithm = j) for j in algs] for i in test_list] # long time (up to 21s on sage.math, 2011) + sage: union([len(union(x))==1 for x in vals]) # long time (depends on previous line) + [True] + sage: algs = ['gp','pari','bernmm'] + sage: test_list = [ZZ.random_element(2256, 5000) for _ in range(500)] + sage: vals = [[bernoulli(i,algorithm = j) for j in algs] for i in test_list] # long time (up to 30s on sage.math, 2011) + sage: union([len(union(x))==1 for x in vals]) # long time (depends on previous line) + [True] + + AUTHOR: + + - David Joyner and William Stein + """ + n = ZZ(n) + + if algorithm == 'default': + algorithm = 'flint' if n <= 300000 else 'bernmm' + + if algorithm == 'flint': + return flint_arith.bernoulli_number(n) + elif algorithm == 'pari': + x = pari(n).bernfrac() # Use the PARI C library + return Rational(x) + elif algorithm == 'gap': + import sage.interfaces.gap + x = sage.interfaces.gap.gap('Bernoulli(%s)'%n) + return Rational(x) + elif algorithm == 'magma': + import sage.interfaces.magma + x = sage.interfaces.magma.magma('Bernoulli(%s)'%n) + return Rational(x) + elif algorithm == 'gp': + import sage.interfaces.gp + x = sage.interfaces.gp.gp('bernfrac(%s)'%n) + return Rational(x) + elif algorithm == 'bernmm': + import sage.rings.bernmm + return sage.rings.bernmm.bernmm_bern_rat(n, num_threads) + else: + raise ValueError("invalid choice of algorithm") + + +def factorial(n, algorithm='gmp'): + r""" + Compute the factorial of `n`, which is the product + `1\cdot 2\cdot 3 \cdots (n-1)\cdot n`. + + INPUT: + + - ``n`` - an integer + + - ``algorithm`` - string (default: 'gmp'): + + - ``'gmp'`` - use the GMP C-library factorial function + + - ``'pari'`` - use PARI's factorial function + + OUTPUT: an integer + + EXAMPLES:: + + sage: from sage.arith.misc import factorial + sage: factorial(0) + 1 + sage: factorial(4) + 24 + sage: factorial(10) + 3628800 + sage: factorial(1) == factorial(0) + True + sage: factorial(6) == 6*5*4*3*2 + True + sage: factorial(1) == factorial(0) + True + sage: factorial(71) == 71* factorial(70) + True + sage: factorial(-32) + Traceback (most recent call last): + ... + ValueError: factorial -- must be nonnegative + + PERFORMANCE: This discussion is valid as of April 2006. All timings + below are on a Pentium Core Duo 2Ghz MacBook Pro running Linux with + a 2.6.16.1 kernel. + + + - It takes less than a minute to compute the factorial of + `10^7` using the GMP algorithm, and the factorial of + `10^6` takes less than 4 seconds. + + - The GMP algorithm is faster and more memory efficient than the + PARI algorithm. E.g., PARI computes `10^7` factorial in 100 + seconds on the core duo 2Ghz. + + - For comparison, computation in Magma `\leq` 2.12-10 of + `n!` is best done using ``*[1..n]``. It takes + 113 seconds to compute the factorial of `10^7` and 6 + seconds to compute the factorial of `10^6`. Mathematica + V5.2 compute the factorial of `10^7` in 136 seconds and the + factorial of `10^6` in 7 seconds. (Mathematica is notably + very efficient at memory usage when doing factorial + calculations.) + """ + if n < 0: + raise ValueError("factorial -- must be nonnegative") + if algorithm == 'gmp': + return ZZ(n).factorial() + elif algorithm == 'pari': + return pari.factorial(n) + else: + raise ValueError('unknown algorithm') + +def is_prime(n): + r""" + Return ``True`` if `n` is a prime number, and ``False`` otherwise. + + Use a provable primality test or a strong pseudo-primality test depending + on the global :mod:`arithmetic proof flag `. + + INPUT: + + - ``n`` - the object for which to determine primality + + .. SEEALSO:: + + - :meth:`is_pseudoprime` + - :meth:`sage.rings.integer.Integer.is_prime` + + AUTHORS: + + - Kevin Stueve kstueve@uw.edu (2010-01-17): + delegated calculation to ``n.is_prime()`` + + EXAMPLES:: + + sage: is_prime(389) + True + sage: is_prime(2000) + False + sage: is_prime(2) + True + sage: is_prime(-1) + False + sage: is_prime(1) + False + sage: is_prime(-2) + False + + sage: a = 2**2048 + 981 + sage: is_prime(a) # not tested - takes ~ 1min + sage: proof.arithmetic(False) + sage: is_prime(a) # instantaneous! + True + sage: proof.arithmetic(True) + """ + try: + return n.is_prime() + except (AttributeError, NotImplementedError): + return ZZ(n).is_prime() + +def is_pseudoprime(n, flag=None): + r""" + Test whether ``n`` is a pseudo-prime + + The result is *NOT* proven correct - *this is a pseudo-primality test!*. + + INPUT: + + - ``n`` -- an integer + + .. note:: + + We do not consider negatives of prime numbers as prime. + + EXAMPLES:: + + sage: is_pseudoprime(389) + True + sage: is_pseudoprime(2000) + False + sage: is_pseudoprime(2) + True + sage: is_pseudoprime(-1) + False + sage: factor(-6) + -1 * 2 * 3 + sage: is_pseudoprime(1) + False + sage: is_pseudoprime(-2) + False + + TESTS: + + Deprecation warning from :trac:`16878`:: + + sage: is_pseudoprime(127, flag=0) + doctest:...: DeprecationWarning: the keyword 'flag' is deprecated and no longer used + See http://trac.sagemath.org/16878 for details. + True + """ + if flag is not None: + from sage.misc.superseded import deprecation + deprecation(16878, "the keyword 'flag' is deprecated and no longer used") + return ZZ(n).is_pseudoprime() + +def is_prime_power(n, flag=None, get_data=False): + r""" + Test whether ``n`` is a positive power of a prime number + + This function simply calls the method :meth:`Integer.is_prime_power() + ` of Integers. + + INPUT: + + - ``n`` -- an integer + + - ``get_data`` -- if set to ``True``, return a pair ``(p,k)`` such that + this integer equals ``p^k`` instead of ``True`` or ``(self,0)`` instead of + ``False`` + + EXAMPLES:: + + sage: is_prime_power(389) + True + sage: is_prime_power(2000) + False + sage: is_prime_power(2) + True + sage: is_prime_power(1024) + True + sage: is_prime_power(1024, get_data=True) + (2, 10) + + The same results can be obtained with:: + + sage: 389.is_prime_power() + True + sage: 2000.is_prime_power() + False + sage: 2.is_prime_power() + True + sage: 1024.is_prime_power() + True + sage: 1024.is_prime_power(get_data=True) + (2, 10) + + TESTS:: + + sage: is_prime_power(-1) + False + sage: is_prime_power(1) + False + sage: is_prime_power(QQ(997^100)) + True + sage: is_prime_power(1/2197) + Traceback (most recent call last): + ... + TypeError: no conversion of this rational to integer + sage: is_prime_power("foo") + Traceback (most recent call last): + ... + TypeError: unable to convert 'foo' to an integer + """ + if flag is not None: + from sage.misc.superseded import deprecation + deprecation(16878, "the keyword 'flag' is deprecated and no longer used") + return ZZ(n).is_prime_power(get_data=get_data) + +def is_pseudoprime_power(n, get_data=False): + r""" + Test if ``n`` is a power of a pseudoprime. + + The result is *NOT* proven correct - *this IS a pseudo-primality test!*. + Note that a prime power is a positive power of a prime number so that 1 is + not a prime power. + + INPUT: + + - ``n`` - an integer + + - ``get_data`` - (boolean) instead of a boolean return a pair `(p,k)` so + that ``n`` equals `p^k` and `p` is a pseudoprime or `(n,0)` otherwise. + + EXAMPLES:: + + sage: is_pseudoprime_power(389) + True + sage: is_pseudoprime_power(2000) + False + sage: is_pseudoprime_power(2) + True + sage: is_pseudoprime_power(1024) + True + sage: is_pseudoprime_power(-1) + False + sage: is_pseudoprime_power(1) + False + sage: is_pseudoprime_power(997^100) + True + + Use of the get_data keyword:: + + sage: is_pseudoprime_power(3^1024, get_data=True) + (3, 1024) + sage: is_pseudoprime_power(2^256, get_data=True) + (2, 256) + sage: is_pseudoprime_power(31, get_data=True) + (31, 1) + sage: is_pseudoprime_power(15, get_data=True) + (15, 0) + """ + return ZZ(n).is_prime_power(proof=False, get_data=get_data) + +def is_pseudoprime_small_power(n, bound=None, get_data=False): + """ + Deprecated version of ``is_pseudoprime_power``. + + EXAMPLES:: + + sage: is_pseudoprime_small_power(1234) + doctest:...: DeprecationWarning: the function is_pseudoprime_small_power() is deprecated, use is_pseudoprime_power() instead. + See http://trac.sagemath.org/16878 for details. + False + sage: is_pseudoprime_small_power(3^1024, get_data=True) + [(3, 1024)] + """ + from sage.misc.superseded import deprecation + deprecation(16878, "the function is_pseudoprime_small_power() is deprecated, use is_pseudoprime_power() instead.") + if get_data: + return [ZZ(n).is_prime_power(proof=False, get_data=True)] + else: + return ZZ(n).is_prime_power(proof=False) + + +def valuation(m, *args, **kwds): + """ + Return the valuation of ``m``. + + This function simply calls the m.valuation() method. + See the documentation of m.valuation() for a more precise description. + + Note that the use of this functions is discouraged as it is better to use + m.valuation() directly. + + .. NOTE:: + + This is not always a valuation in the mathematical sense. + For more information see: + sage.rings.finite_rings.integer_mod.IntegerMod_int.valuation + + EXAMPLES:: + + sage: valuation(512,2) + 9 + sage: valuation(1,2) + 0 + sage: valuation(5/9, 3) + -2 + + Valuation of 0 is defined, but valuation with respect to 0 is not:: + + sage: valuation(0,7) + +Infinity + sage: valuation(3,0) + Traceback (most recent call last): + ... + ValueError: You can only compute the valuation with respect to a integer larger than 1. + + Here are some other examples:: + + sage: valuation(100,10) + 2 + sage: valuation(200,10) + 2 + sage: valuation(243,3) + 5 + sage: valuation(243*10007,3) + 5 + sage: valuation(243*10007,10007) + 1 + sage: y = QQ['y'].gen() + sage: valuation(y^3, y) + 3 + sage: x = QQ[['x']].gen() + sage: valuation((x^3-x^2)/(x-4)) + 2 + sage: valuation(4r,2r) + 2 + sage: valuation(1r,1r) + Traceback (most recent call last): + ... + ValueError: You can only compute the valuation with respect to a integer larger than 1. + """ + try: + return m.valuation(*args, **kwds) + except AttributeError: + return ZZ(m).valuation(*args, **kwds) + +def prime_powers(start, stop=None): + r""" + List of all positive primes powers between ``start`` and + ``stop``-1, inclusive. If the second argument is omitted, returns + the prime powers up to the first argument. + + INPUT: + + - ``start`` - an integer. If two inputs are given, a lower bound + for the returned set of prime powers. If this is the only input, + then it is an upper bound. + + - ``stop`` - an integer (default: ``None``). An upper bound for the + returned set of prime powers. + + OUTPUT: + + The set of all prime powers between ``start`` and ``stop`` or, if + only one argument is passed, the set of all prime powers between 1 + and ``start``. The number `n` is a prime power if `n=p^k`, where + `p` is a prime number and `k` is a positive integer. Thus, `1` is + not a prime power. + + EXAMPLES:: + + sage: prime_powers(20) + [2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 17, 19] + sage: len(prime_powers(1000)) + 193 + sage: len(prime_range(1000)) + 168 + + sage: a = [z for z in range(95,1234) if is_prime_power(z)] + sage: b = prime_powers(95,1234) + sage: len(b) + 194 + sage: len(a) + 194 + sage: a[:10] + [97, 101, 103, 107, 109, 113, 121, 125, 127, 128] + sage: b[:10] + [97, 101, 103, 107, 109, 113, 121, 125, 127, 128] + sage: a == b + True + + sage: prime_powers(100) == [i for i in range(100) if is_prime_power(i)] + True + + sage: prime_powers(10,7) + [] + sage: prime_powers(-5) + [] + sage: prime_powers(-1,3) + [2] + + TESTS: + + Check that output are always Sage integers (:trac:`922`):: + + sage: v = prime_powers(10) + sage: type(v[0]) + + + sage: prime_powers(0,1) + [] + sage: prime_powers(2) + [] + sage: prime_powers(3) + [2] + + sage: prime_powers("foo") + Traceback (most recent call last): + ... + TypeError: unable to convert 'foo' to an integer + + sage: prime_powers(6, "bar") + Traceback (most recent call last): + ... + TypeError: unable to convert 'bar' to an integer + + Check that long input are accepted (:trac:`17852`):: + + sage: prime_powers(6l) + [2, 3, 4, 5] + sage: prime_powers(6l,10l) + [7, 8, 9] + """ + start = ZZ(start) + + ZZ_2 = Integer(2) + if stop is None: + stop = start + start = ZZ_2 + else: + stop = ZZ(stop) + + if stop <= ZZ_2 or start >= stop: + return [] + + output = [] + for p in prime_range(stop): + q = p + while q < start: + q *= p + while q < stop: + output.append(q) + q *= p + + output.sort() + return output + +def primes_first_n(n, leave_pari=False): + r""" + Return the first `n` primes. + + INPUT: + + - `n` - a nonnegative integer + + OUTPUT: + + - a list of the first `n` prime numbers. + + EXAMPLES:: + + sage: primes_first_n(10) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] + sage: len(primes_first_n(1000)) + 1000 + sage: primes_first_n(0) + [] + """ + if n < 0: + raise ValueError("n must be nonnegative") + if n < 1: + return [] + return prime_range(nth_prime(n) + 1) + +# +# This is from +# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366178 +# It's impressively fast given that it's in Pure Python. +# +def eratosthenes(n): + r""" + Return a list of the primes `\leq n`. + + This is extremely slow and is for educational purposes only. + + INPUT: + + - ``n`` - a positive integer + + OUTPUT: + + - a list of primes less than or equal to n. + + + EXAMPLES:: + + sage: eratosthenes(3) + [2, 3] + sage: eratosthenes(50) + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] + sage: len(eratosthenes(100)) + 25 + sage: eratosthenes(213) == prime_range(213) + True + """ + n = int(n) + + if n < 2: + return [] + elif n == 2: + return [ZZ(2)] + + s = range(3, n+3, 2) + mroot = int(n ** 0.5) + half = (n+1) // 2 + i = 0 + m = 3 + while m <= mroot: + if s[i]: + j = (m*m-3) // 2 + s[j] = 0 + while j < half: + s[j] = 0 + j += m + i = i+1 + m = 2*i+3 + + return [ZZ(2)] + [ZZ(x) for x in s if x and x <= n] + +def primes(start, stop=None, proof=None): + r""" + Returns an iterator over all primes between start and stop-1, + inclusive. This is much slower than ``prime_range``, but + potentially uses less memory. As with :func:`next_prime`, the optional + argument proof controls whether the numbers returned are + guaranteed to be prime or not. + + This command is like the xrange command, except it only iterates + over primes. In some cases it is better to use primes than + ``prime_range``, because primes does not build a list of all primes in + the range in memory all at once. However, it is potentially much + slower since it simply calls the :func:`next_prime` function + repeatedly, and :func:`next_prime` is slow. + + INPUT: + + - ``start`` - an integer - lower bound for the primes + + - ``stop`` - an integer (or infinity) optional argument - + giving upper (open) bound for the primes + + - ``proof`` - bool or None (default: None) If True, the function + yields only proven primes. If False, the function uses a + pseudo-primality test, which is much faster for really big + numbers but does not provide a proof of primality. If None, + uses the global default (see :mod:`sage.structure.proof.proof`) + + OUTPUT: + + - an iterator over primes from start to stop-1, inclusive + + + EXAMPLES:: + + sage: for p in primes(5,10): + ....: print p + 5 + 7 + sage: list(primes(13)) + [2, 3, 5, 7, 11] + sage: list(primes(10000000000, 10000000100)) + [10000000019, 10000000033, 10000000061, 10000000069, 10000000097] + sage: max(primes(10^100, 10^100+10^4, proof=False)) + 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009631 + sage: next(p for p in primes(10^20, infinity) if is_prime(2*p+1)) + 100000000000000001243 + + + TESTS:: + + sage: for a in range(-10, 50): + ....: for b in range(-10, 50): + ....: assert list(primes(a,b)) == list(filter(is_prime, xrange(a,b))) + sage: sum(primes(-10, 9973, proof=False)) == sum(filter(is_prime, range(-10, 9973))) + True + sage: for p in primes(10, infinity): + ....: if p > 20: break + ....: print p + 11 + 13 + 17 + 19 + sage: next(p for p in primes(10,oo)) # checks alternate infinity notation + 11 + """ + from sage.rings.infinity import infinity + + start = ZZ(start) + if stop is None: + stop = start + start = ZZ(2) + elif stop != infinity: + stop = ZZ(stop) + n = start - 1 + while True: + n = n.next_prime(proof) + if n < stop: + yield n + else: + return + +def next_prime_power(n): + """ + Return the smallest prime power greater than ``n``. + + Note that if ``n`` is a prime power, then this function does not return + ``n``, but the next prime power after ``n``. + + This function just calls the method + :meth:`Integer.next_prime_power() ` + of Integers. + + .. SEEALSO:: + + - :func:`is_prime_power` (and + :meth:`Integer.is_prime_power() `) + - :func:`previous_prime_power` (and + :meth:`Integer.previous_prime_power() `) + + EXAMPLES:: + + sage: next_prime_power(1) + 2 + sage: next_prime_power(2) + 3 + sage: next_prime_power(10) + 11 + sage: next_prime_power(7) + 8 + sage: next_prime_power(99) + 101 + + The same results can be obtained with:: + + sage: 1.next_prime_power() + 2 + sage: 2.next_prime_power() + 3 + sage: 10.next_prime_power() + 11 + + Note that `2` is the smallest prime power:: + + sage: next_prime_power(-10) + 2 + sage: next_prime_power(0) + 2 + """ + return ZZ(n).next_prime_power() + +def next_probable_prime(n): + """ + Returns the next probable prime after self, as determined by PARI. + + INPUT: + + + - ``n`` - an integer + + + EXAMPLES:: + + sage: next_probable_prime(-100) + 2 + sage: next_probable_prime(19) + 23 + sage: next_probable_prime(int(999999999)) + 1000000007 + sage: next_probable_prime(2^768) + 1552518092300708935148979488462502555256886017116696611139052038026050952686376886330878408828646477950487730697131073206171580044114814391444287275041181139204454976020849905550265285631598444825262999193716468750892846853816058039 + """ + return ZZ(n).next_probable_prime() + +def next_prime(n, proof=None): + """ + The next prime greater than the integer n. If n is prime, then this + function does not return n, but the next prime after n. If the + optional argument proof is False, this function only returns a + pseudo-prime, as defined by the PARI nextprime function. If it is + None, uses the global default (see :mod:`sage.structure.proof.proof`) + + INPUT: + + + - ``n`` - integer + + - ``proof`` - bool or None (default: None) + + + EXAMPLES:: + + sage: next_prime(-100) + 2 + sage: next_prime(1) + 2 + sage: next_prime(2) + 3 + sage: next_prime(3) + 5 + sage: next_prime(4) + 5 + + Notice that the next_prime(5) is not 5 but 7. + + :: + + sage: next_prime(5) + 7 + sage: next_prime(2004) + 2011 + """ + return ZZ(n).next_prime(proof) + +def previous_prime(n): + """ + The largest prime < n. The result is provably correct. If n <= 1, + this function raises a ValueError. + + EXAMPLES:: + + sage: previous_prime(10) + 7 + sage: previous_prime(7) + 5 + sage: previous_prime(8) + 7 + sage: previous_prime(7) + 5 + sage: previous_prime(5) + 3 + sage: previous_prime(3) + 2 + sage: previous_prime(2) + Traceback (most recent call last): + ... + ValueError: no previous prime + sage: previous_prime(1) + Traceback (most recent call last): + ... + ValueError: no previous prime + sage: previous_prime(-20) + Traceback (most recent call last): + ... + ValueError: no previous prime + """ + n = ZZ(n)-1 + if n <= 1: + raise ValueError("no previous prime") + if n <= 3: + return ZZ(n) + if n%2 == 0: + n -= 1 + while not is_prime(n): + n -= 2 + return ZZ(n) + +def previous_prime_power(n): + r""" + Return the largest prime power smaller than ``n``. + + The result is provably correct. If ``n`` is smaller or equal than ``2`` this + function raises an error. + + This function simply call the method + :meth:`Integer.previous_prime_power() ` + of Integers. + + .. SEEALSO:: + + - :func:`is_prime_power` (and :meth:`Integer.is_prime_power() + `) + - :func:`next_prime_power` (and :meth:`Integer.next_prime_power() + `) + + EXAMPLES:: + + sage: previous_prime_power(3) + 2 + sage: previous_prime_power(10) + 9 + sage: previous_prime_power(7) + 5 + sage: previous_prime_power(127) + 125 + + The same results can be obtained with:: + + sage: 3.previous_prime_power() + 2 + sage: 10.previous_prime_power() + 9 + sage: 7.previous_prime_power() + 5 + sage: 127.previous_prime_power() + 125 + + Input less than or equal to `2` raises errors:: + + sage: previous_prime_power(2) + Traceback (most recent call last): + ... + ValueError: no prime power less than 2 + sage: previous_prime_power(-10) + Traceback (most recent call last): + ... + ValueError: no prime power less than 2 + + :: + + sage: n = previous_prime_power(2^16 - 1) + sage: while is_prime(n): + ....: n = previous_prime_power(n) + sage: factor(n) + 251^2 + """ + return ZZ(n).previous_prime_power() + +def random_prime(n, proof=None, lbound=2): + """ + Returns a random prime p between `lbound` and n (i.e. `lbound <= p <= n`). + The returned prime is chosen uniformly at random from the set of prime + numbers less than or equal to n. + + INPUT: + + - ``n`` - an integer >= 2. + + - ``proof`` - bool or None (default: None) If False, the function uses a + pseudo-primality test, which is much faster for really big numbers but + does not provide a proof of primality. If None, uses the global default + (see :mod:`sage.structure.proof.proof`) + + - ``lbound`` - an integer >= 2 + lower bound for the chosen primes + + EXAMPLES:: + + sage: random_prime(100000) + 88237 + sage: random_prime(2) + 2 + + Here we generate a random prime between 100 and 200:: + + sage: random_prime(200, lbound=100) + 149 + + If all we care about is finding a pseudo prime, then we can pass + in ``proof=False`` :: + + sage: random_prime(200, proof=False, lbound=100) + 149 + + TESTS:: + + sage: type(random_prime(2)) + + sage: type(random_prime(100)) + + sage: random_prime(1, lbound=-2) #caused Sage hang #10112 + Traceback (most recent call last): + ... + ValueError: n must be greater than or equal to 2 + sage: random_prime(126, lbound=114) + Traceback (most recent call last): + ... + ValueError: There are no primes between 114 and 126 (inclusive) + + + AUTHORS: + + - Jon Hanke (2006-08-08): with standard Stein cleanup + + - Jonathan Bober (2007-03-17) + """ + # since we don't want current_randstate to get + # pulled when you say "from sage.arith.misc import *". + from sage.misc.randstate import current_randstate + from sage.structure.proof.proof import get_flag + proof = get_flag(proof, "arithmetic") + n = ZZ(n) + if n < 2: + raise ValueError("n must be greater than or equal to 2") + if n < lbound: + raise ValueError("n must be at least lbound: %s"%(lbound)) + elif n == 2: + return n + lbound = max(2, lbound) + if lbound > 2: + if lbound == 3 or n <= 2*lbound - 2: + # check for Betrand's postulate (proved by Chebyshev) + if lbound < 25 or n <= 6*lbound/5: + # see J. Nagura, Proc. Japan Acad. 28, (1952). 177-181. + if lbound < 2010760 or n <= 16598*lbound/16597: + # see L. Schoenfeld, Math. Comp. 30 (1976), no. 134, 337-360. + if proof: + smallest_prime = ZZ(lbound-1).next_prime() + else: + smallest_prime = ZZ(lbound-1).next_probable_prime() + if smallest_prime > n: + raise ValueError("There are no primes between %s and %s (inclusive)" % (lbound, n)) + + if proof: + prime_test = is_prime + else: + prime_test = is_pseudoprime + randint = current_randstate().python_random().randint + while True: + # In order to ensure that the returned prime is chosen + # uniformly from the set of primes it is necessary to + # choose a random number and then test for primality. + # The method of choosing a random number and then returning + # the closest prime smaller than it would typically not, + # for example, return the first of a pair of twin primes. + p = randint(lbound, n) + if prime_test(p): + return ZZ(p) + + +def divisors(n): + """ + Returns a list of all positive integer divisors of the nonzero + integer n. + + INPUT: + + + - ``n`` - the element + + + EXAMPLES:: + + sage: divisors(-3) + [1, 3] + sage: divisors(6) + [1, 2, 3, 6] + sage: divisors(28) + [1, 2, 4, 7, 14, 28] + sage: divisors(2^5) + [1, 2, 4, 8, 16, 32] + sage: divisors(100) + [1, 2, 4, 5, 10, 20, 25, 50, 100] + sage: divisors(1) + [1] + sage: divisors(0) + Traceback (most recent call last): + ... + ValueError: n must be nonzero + sage: divisors(2^3 * 3^2 * 17) + [1, 2, 3, 4, 6, 8, 9, 12, 17, 18, 24, 34, 36, 51, 68, 72, 102, 136, 153, 204, 306, 408, 612, 1224] + + This function works whenever one has unique factorization:: + + sage: K. = QuadraticField(7) + sage: divisors(K.ideal(7)) + [Fractional ideal (1), Fractional ideal (a), Fractional ideal (7)] + sage: divisors(K.ideal(3)) + [Fractional ideal (1), Fractional ideal (3), Fractional ideal (-a + 2), Fractional ideal (-a - 2)] + sage: divisors(K.ideal(35)) + [Fractional ideal (1), Fractional ideal (5), Fractional ideal (a), Fractional ideal (7), Fractional ideal (5*a), Fractional ideal (35)] + + TESTS:: + + sage: divisors(int(300)) + [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300] + """ + if not n: + raise ValueError("n must be nonzero") + + if isinstance(n, (int, long)): + n = ZZ(n) # we have specialized code for this case, make sure it gets used + + try: + return n.divisors() + except AttributeError: + pass + + f = factor(n) + one = parent(n)(1) + output = [one] + for p, e in f: + prev = output[:] + pn = one + for i in range(e): + pn *= p + output.extend(a*pn for a in prev) + output.sort() + return output + +class Sigma: + """ + Return the sum of the k-th powers of the divisors of n. + + INPUT: + + + - ``n`` - integer + + - ``k`` - integer (default: 1) + + + OUTPUT: integer + + EXAMPLES:: + + sage: sigma(5) + 6 + sage: sigma(5,2) + 26 + + The sigma function also has a special plotting method. + + :: + + sage: P = plot(sigma, 1, 100) + + This method also works with k-th powers. + + :: + + sage: P = plot(sigma, 1, 100, k=2) + + AUTHORS: + + - William Stein: original implementation + + - Craig Citro (2007-06-01): rewrote for huge speedup + + TESTS:: + + sage: sigma(100,4) + 106811523 + sage: sigma(factorial(100),3).mod(144169) + 3672 + sage: sigma(factorial(150),12).mod(691) + 176 + sage: RR(sigma(factorial(133),20)) + 2.80414775675747e4523 + sage: sigma(factorial(100),0) + 39001250856960000 + sage: sigma(factorial(41),1) + 229199532273029988767733858700732906511758707916800 + """ + def __repr__(self): + """ + A description of this class, which computes the sum of the + k-th powers of the divisors of n. + + EXAMPLES:: + + sage: Sigma().__repr__() + 'Function that adds up (k-th powers of) the divisors of n' + """ + return "Function that adds up (k-th powers of) the divisors of n" + + def __call__(self, n, k=1): + """ + Computes the sum of (the k-th powers of) the divisors of n. + + EXAMPLES:: + + sage: q = Sigma() + sage: q(10) + 18 + sage: q(10,2) + 130 + """ + n = ZZ(n) + k = ZZ(k) + one = ZZ(1) + + if (k == ZZ(0)): + return prod(expt+one for p, expt in factor(n)) + elif (k == one): + return prod((p**(expt+one) - one).divide_knowing_divisible_by(p - one) + for p, expt in factor(n)) + else: + return prod((p**((expt+one)*k)-one).divide_knowing_divisible_by(p**k-one) + for p,expt in factor(n)) + + def plot(self, xmin=1, xmax=50, k=1, pointsize=30, rgbcolor=(0,0,1), join=True, + **kwds): + """ + Plot the sigma (sum of k-th powers of divisors) function. + + INPUT: + + + - ``xmin`` - default: 1 + + - ``xmax`` - default: 50 + + - ``k`` - default: 1 + + - ``pointsize`` - default: 30 + + - ``rgbcolor`` - default: (0,0,1) + + - ``join`` - default: True; whether to join the + points. + + - ``**kwds`` - passed on + + EXAMPLES:: + + sage: p = Sigma().plot() + sage: p.ymax() + 124.0 + """ + v = [(n,sigma(n,k)) for n in range(xmin,xmax + 1)] + from sage.plot.all import list_plot + P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) + if join: + P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) + return P + +sigma = Sigma() + +def gcd(a, b=None, **kwargs): + r""" + The greatest common divisor of a and b, or if a is a list and b is + omitted the greatest common divisor of all elements of a. + + INPUT: + + + - ``a,b`` - two elements of a ring with gcd or + + - ``a`` - a list or tuple of elements of a ring with + gcd + + + Additional keyword arguments are passed to the respectively called + methods. + + OUTPUT: + + The given elements are first coerced into a common parent. Then, + their greatest common divisor *in that common parent* is returned. + + EXAMPLES:: + + sage: GCD(97,100) + 1 + sage: GCD(97*10^15, 19^20*97^2) + 97 + sage: GCD(2/3, 4/5) + 2/15 + sage: GCD([2,4,6,8]) + 2 + sage: GCD(srange(0,10000,10)) # fast !! + 10 + + Note that to take the gcd of `n` elements for `n \not= 2` you must + put the elements into a list by enclosing them in ``[..]``. Before + #4988 the following wrongly returned 3 since the third parameter + was just ignored:: + + sage: gcd(3,6,2) + Traceback (most recent call last): + ... + TypeError: gcd() takes at most 2 arguments (3 given) + sage: gcd([3,6,2]) + 1 + + Similarly, giving just one element (which is not a list) gives an error:: + + sage: gcd(3) + Traceback (most recent call last): + ... + TypeError: 'sage.rings.integer.Integer' object is not iterable + + By convention, the gcd of the empty list is (the integer) 0:: + + sage: gcd([]) + 0 + sage: type(gcd([])) + + + TESTS: + + The following shows that indeed coercion takes place before computing + the gcd. This behaviour was introduced in :trac:`10771`:: + + sage: R.=QQ[] + sage: S.=ZZ[] + sage: p = S.random_element(degree=(0,10)) + sage: q = R.random_element(degree=(0,10)) + sage: parent(gcd(1/p,q)) + Fraction Field of Univariate Polynomial Ring in x over Rational Field + sage: parent(gcd([1/p,q])) + Fraction Field of Univariate Polynomial Ring in x over Rational Field + + Make sure we try QQ and not merely ZZ (:trac:`13014`):: + + sage: bool(gcd(2/5, 3/7) == gcd(SR(2/5), SR(3/7))) + True + + Make sure that the gcd of Expressions stays symbolic:: + + sage: parent(gcd(2, 4)) + Integer Ring + sage: parent(gcd(SR(2), 4)) + Symbolic Ring + sage: parent(gcd(2, SR(4))) + Symbolic Ring + sage: parent(gcd(SR(2), SR(4))) + Symbolic Ring + + Verify that objects without gcd methods but which can't be + coerced to ZZ or QQ raise an error:: + + sage: F. = FreeMonoid(2) + sage: gcd(a,b) + Traceback (most recent call last): + ... + TypeError: unable to find gcd + + """ + # Most common use case first: + if b is not None: + try: + return a.gcd(b, **kwargs) + except (AttributeError, TypeError): + pass + try: + return ZZ(a).gcd(ZZ(b)) + except TypeError: + raise TypeError("unable to find gcd") + + from sage.structure.sequence import Sequence + seq = Sequence(a) + U = seq.universe() + if U is ZZ or U is int or U is long:# ZZ.has_coerce_map_from(U): + return GCD_list(a) + return __GCD_sequence(seq, **kwargs) + +GCD = gcd + +def __GCD_sequence(v, **kwargs): + """ + Internal function returning the gcd of the elements of a sequence + + INPUT: + + + - ``v`` - A sequence (possibly empty) + + + OUTPUT: The gcd of the elements of the sequence as an element of + the sequence's universe, or the integer 0 if the sequence is + empty. + + EXAMPLES:: + + sage: from sage.arith.misc import __GCD_sequence + sage: from sage.structure.sequence import Sequence + sage: l = () + sage: __GCD_sequence(l) + 0 + sage: __GCD_sequence(Sequence(srange(10))) + 1 + sage: X=polygen(QQ) + sage: __GCD_sequence(Sequence((2*X+4,2*X^2,2))) + 1 + sage: X=polygen(ZZ) + sage: __GCD_sequence(Sequence((2*X+4,2*X^2,2))) + 2 + """ + if len(v) == 0: + return ZZ(0) + if hasattr(v,'universe'): + g = v.universe()(0) + else: + g = ZZ(0) + one = v.universe()(1) + for vi in v: + g = vi.gcd(g, **kwargs) + if g == one: + return g + return g + +def lcm(a, b=None): + """ + The least common multiple of a and b, or if a is a list and b is + omitted the least common multiple of all elements of a. + + Note that LCM is an alias for lcm. + + INPUT: + + + - ``a,b`` - two elements of a ring with lcm or + + - ``a`` - a list or tuple of elements of a ring with + lcm + + OUTPUT: + + First, the given elements are coerced into a common parent. Then, + their least common multiple *in that parent* is returned. + + EXAMPLES:: + + sage: lcm(97,100) + 9700 + sage: LCM(97,100) + 9700 + sage: LCM(0,2) + 0 + sage: LCM(-3,-5) + 15 + sage: LCM([1,2,3,4,5]) + 60 + sage: v = LCM(range(1,10000)) # *very* fast! + sage: len(str(v)) + 4349 + + + TESTS: + + The following tests against a bug that was fixed in :trac:`10771`:: + + sage: lcm(4/1,2) + 4 + + The following shows that indeed coercion takes place before + computing the least common multiple:: + + sage: R.=QQ[] + sage: S.=ZZ[] + sage: p = S.random_element(degree=(0,5)) + sage: q = R.random_element(degree=(0,5)) + sage: parent(lcm([1/p,q])) + Fraction Field of Univariate Polynomial Ring in x over Rational Field + + Make sure we try QQ and not merely ZZ (:trac:`13014`):: + + sage: bool(lcm(2/5, 3/7) == lcm(SR(2/5), SR(3/7))) + True + + Make sure that the lcm of Expressions stays symbolic:: + + sage: parent(lcm(2, 4)) + Integer Ring + sage: parent(lcm(SR(2), 4)) + Symbolic Ring + sage: parent(lcm(2, SR(4))) + Symbolic Ring + sage: parent(lcm(SR(2), SR(4))) + Symbolic Ring + + Verify that objects without lcm methods but which can't be + coerced to ZZ or QQ raise an error:: + + sage: F. = FreeMonoid(2) + sage: lcm(a,b) + Traceback (most recent call last): + ... + TypeError: unable to find lcm + + Check rational and integers (:trac:`17852`):: + + sage: lcm(1/2, 4) + 4 + sage: lcm(4, 1/2) + 4 + """ + # Most common use case first: + if b is not None: + try: + return a.lcm(b) + except (AttributeError,TypeError): + pass + try: + return ZZ(a).lcm(ZZ(b)) + except TypeError: + raise TypeError("unable to find lcm") + + from sage.structure.sequence import Sequence + seq = Sequence(a) + U = seq.universe() + if U is ZZ or U is int or U is long: + return LCM_list(a) + return __LCM_sequence(seq) + +LCM = lcm + +def __LCM_sequence(v): + """ + Internal function returning the lcm of the elements of a sequence + + INPUT: + + + - ``v`` - A sequence (possibly empty) + + + OUTPUT: The lcm of the elements of the sequence as an element of + the sequence's universe, or the integer 1 if the sequence is + empty. + + EXAMPLES:: + + sage: from sage.structure.sequence import Sequence + sage: from sage.arith.misc import __LCM_sequence + sage: l = Sequence(()) + sage: __LCM_sequence(l) + 1 + + This is because lcm(0,x)=0 for all x (by convention) + + :: + + sage: __LCM_sequence(Sequence(srange(100))) + 0 + + So for the lcm of all integers up to 10 you must do this:: + + sage: __LCM_sequence(Sequence(srange(1,100))) + 69720375229712477164533808935312303556800 + + Note that the following example did not work in QQ[] as of 2.11, + but does in 3.1.4; the answer is different, though equivalent:: + + sage: R.=ZZ[] + sage: __LCM_sequence(Sequence((2*X+4,2*X^2,2))) + 2*X^3 + 4*X^2 + sage: R.=QQ[] + sage: __LCM_sequence(Sequence((2*X+4,2*X^2,2))) + X^3 + 2*X^2 + """ + if len(v) == 0: + return ZZ(1) + try: + g = v.universe()(1) + except AttributeError: + g = ZZ(1) + for vi in v: + g = vi.lcm(g) + if not g: + return g + return g + +def xlcm(m, n): + r""" + Extended lcm function: given two positive integers `m,n`, returns + a triple `(l,m_1,n_1)` such that `l=\mathop{\mathrm{lcm}}(m,n)=m_1 + \cdot n_1` where `m_1|m`, `n_1|n` and `\gcd(m_1,n_1)=1`, all with no + factorization. + + Used to construct an element of order `l` from elements of orders `m,n` + in any group: see sage/groups/generic.py for examples. + + EXAMPLES:: + + sage: xlcm(120,36) + (360, 40, 9) + """ + g = gcd(m, n) + l = m*n//g # = lcm(m, n) + g = gcd(m, n//g) # divisible by those primes which divide n to a + # higher power than m + + while not g==1: + m //= g + g = gcd(m, g) + + n = l//m + return (l, m, n) + +def xgcd(a, b): + r""" + Return a triple ``(g,s,t)`` such that `g = s\cdot a+t\cdot b = \gcd(a,b)`. + + .. NOTE:: + + One exception is if `a` and `b` are not in a principal ideal domain (see + :wikipedia:`Principal_ideal_domain`), e.g., they are both polynomials + over the integers. Then this function can't in general return ``(g,s,t)`` + as above, since they need not exist. Instead, over the integers, we + first multiply `g` by a divisor of the resultant of `a/g` and `b/g`, up + to sign. + + INPUT: + + - ``a, b`` - integers or more generally, element of a ring for which the + xgcd make sense (e.g. a field or univariate polynomials). + + OUTPUT: + + - ``g, s, t`` - such that `g = s\cdot a + t\cdot b` + + .. NOTE:: + + There is no guarantee that the returned cofactors (s and t) are + minimal. + + EXAMPLES:: + + sage: xgcd(56, 44) + (4, 4, -5) + sage: 4*56 + (-5)*44 + 4 + + sage: g, a, b = xgcd(5/1, 7/1); g, a, b + (1, 3, -2) + sage: a*(5/1) + b*(7/1) == g + True + + sage: x = polygen(QQ) + sage: xgcd(x^3 - 1, x^2 - 1) + (x - 1, 1, -x) + + sage: K. = NumberField(x^2-3) + sage: g.xgcd(g+2) + (1, 1/3*g, 0) + + sage: R. = K[] + sage: S. = R.fraction_field()[] + sage: xgcd(y^2, a*y+b) + (1, a^2/b^2, ((-a)/b^2)*y + 1/b) + sage: xgcd((b+g)*y^2, (a+g)*y+b) + (1, (a^2 + (2*g)*a + 3)/(b^3 + (g)*b^2), ((-a + (-g))/b^2)*y + 1/b) + + Here is an example of a xgcd for two polynomials over the integers, where the linear + combination is not the gcd but the gcd multiplied by the resultant:: + + sage: R. = ZZ[] + sage: gcd(2*x*(x-1), x^2) + x + sage: xgcd(2*x*(x-1), x^2) + (2*x, -1, 2) + sage: (2*(x-1)).resultant(x) + 2 + """ + try: + return a.xgcd(b) + except AttributeError: + pass + return ZZ(a).xgcd(ZZ(b)) + +XGCD = xgcd + +## def XGCD_python(a, b): +## """ +## Returns triple (g,p,q) such that g = p*a+b*q = GCD(a,b). +## This function should behave exactly the same as XGCD, +## but is implemented in pure python. +## """ +## if a == 0 and b == 0: +## return (0,0,1) +## if a == 0: +## return (abs(b), 0, b/abs(b)) +## if b == 0: +## return (abs(a), a/abs(a), 0) +## psign = 1 +## qsign = 1 +## if a < 0: +## a = -a +## psign = -1 +## if b < 0: +## b = -b +## qsign = -1 +## p = 1; q = 0; r = 0; s = 1 +## while b != 0: +## c = a % b +## quot = a/b +## a = b; b = c +## new_r = p - quot*r +## new_s = q - quot*s +## p = r; q = s +## r = new_r; s = new_s +## return (a, p*psign, q*qsign) + + +def xkcd(n=""): + r""" + This function is similar to the xgcd function, but behaves + in a completely different way. + + INPUT: + + - ``n`` - an integer (optional) + + OUTPUT: + + This function outputs nothing it just prints something. Note that this + function does not feel itself at ease in a html deprived environment. + + EXAMPLES:: + + sage: xkcd(353) # optional - internet +

Python

+ """ + import contextlib + import json + from sage.misc.html import html + + # import compatible with py2 and py3 + from six.moves.urllib.request import urlopen + from six.moves.urllib.error import HTTPError, URLError + + data = None + url = "http://dynamic.xkcd.com/api-0/jsonp/comic/{}".format(n) + + try: + with contextlib.closing(urlopen(url)) as f: + data = f.read() + except HTTPError as error: + if error.getcode() == 400: # this error occurs when asking for a non valid comic number + raise RuntimeError("Could not obtain comic data from {}. Maybe you should enable time travel!".format(url)) + except URLError: + pass + + if n == 1024: + data = None + + if data: + data = json.loads(data) + img = data['img'] + alt = data['alt'] + title = data['safe_title'] + link = "http://xkcd.com/{}".format(data['num']) + html('

{}

'.format(title, img, alt) + + '
Source: {0}
'.format(link)) + return + + # TODO: raise this error in such a way that it's not clear that + # it is produced by sage, see http://xkcd.com/1024/ + html('') + + +def inverse_mod(a, m): + """ + The inverse of the ring element a modulo m. + + If no special inverse_mod is defined for the elements, it tries to + coerce them into integers and perform the inversion there + + :: + + sage: inverse_mod(7,1) + 0 + sage: inverse_mod(5,14) + 3 + sage: inverse_mod(3,-5) + 2 + """ + try: + return a.inverse_mod(m) + except AttributeError: + return Integer(a).inverse_mod(m) + +####################################################### +# Functions to find the fastest available commands +# for gcd and inverse_mod +####################################################### + +def get_gcd(order): + """ + Return the fastest gcd function for integers of size no larger than + order. + + EXAMPLES:: + + sage: sage.arith.misc.get_gcd(4000) + + sage: sage.arith.misc.get_gcd(400000) + + sage: sage.arith.misc.get_gcd(4000000000) + + """ + if order <= 46340: # todo: don't hard code + return fast_arith.arith_int().gcd_int + elif order <= 2147483647: # todo: don't hard code + return fast_arith.arith_llong().gcd_longlong + else: + return gcd + +def get_inverse_mod(order): + """ + Return the fastest inverse_mod function for integers of size no + larger than order. + + EXAMPLES:: + + sage: sage.arith.misc.get_inverse_mod(6000) + + sage: sage.arith.misc.get_inverse_mod(600000) + + sage: sage.arith.misc.get_inverse_mod(6000000000) + + """ + if order <= 46340: # todo: don't hard code + return fast_arith.arith_int().inverse_mod_int + elif order <= 2147483647: # todo: don't hard code + return fast_arith.arith_llong().inverse_mod_longlong + else: + return inverse_mod + +# def sqrt_mod(a, m): +# """A square root of a modulo m.""" + +# def xxx_inverse_mod(a, m): +# """The inverse of a modulo m.""" +# g,s,t = XGCD(a,m) +# if g != 1: +# raise "inverse_mod(a=%s,m=%s), error since GCD=%s"%(a,m,g) +# return s + +def power_mod(a,n,m): + """ + The n-th power of a modulo the integer m. + + EXAMPLES:: + + sage: power_mod(0,0,5) + Traceback (most recent call last): + ... + ArithmeticError: 0^0 is undefined. + sage: power_mod(2,390,391) + 285 + sage: power_mod(2,-1,7) + 4 + sage: power_mod(11,1,7) + 4 + sage: R. = ZZ[] + sage: power_mod(3*x, 10, 7) + 4*x^10 + + sage: power_mod(11,1,0) + Traceback (most recent call last): + ... + ZeroDivisionError: modulus must be nonzero. + """ + if m==0: + raise ZeroDivisionError("modulus must be nonzero.") + if m==1: + return 0 + if n < 0: + ainv = inverse_mod(a,m) + return power_mod(ainv, -n, m) + if n==0: + if a == 0: + raise ArithmeticError("0^0 is undefined.") + return 1 + + apow = a % m + while n&1 == 0: + apow = (apow*apow) % m + n = n >> 1 + power = apow + n = n >> 1 + while n != 0: + apow = (apow*apow) % m + if n&1 != 0: + power = (power*apow) % m + n = n >> 1 + + return power + + +def rational_reconstruction(a, m, algorithm='fast'): + r""" + This function tries to compute `x/y`, where `x/y` is a rational number in + lowest terms such that the reduction of `x/y` modulo `m` is equal to `a` and + the absolute values of `x` and `y` are both `\le \sqrt{m/2}`. If such `x/y` + exists, that pair is unique and this function returns it. If no + such pair exists, this function raises ZeroDivisionError. + + An efficient algorithm for computing rational reconstruction is + very similar to the extended Euclidean algorithm. For more details, + see Knuth, Vol 2, 3rd ed, pages 656-657. + + INPUT: + + - ``a`` -- an integer + + - ``m`` -- a modulus + + - ``algorithm`` -- (default: 'fast') + + - ``'fast'`` - a fast implementation using direct MPIR calls + in Cython. + + OUTPUT: + + Numerator and denominator `n`, `d` of the unique rational number + `r=n/d`, if it exists, with `n` and `|d| \le \sqrt{N/2}`. Return + `(0,0)` if no such number exists. + + The algorithm for rational reconstruction is described (with a + complete nontrivial proof) on pages 656-657 of Knuth, Vol 2, 3rd + ed. as the solution to exercise 51 on page 379. See in particular + the conclusion paragraph right in the middle of page 657, which + describes the algorithm thus: + + This discussion proves that the problem can be solved + efficiently by applying Algorithm 4.5.2X with `u=m` and `v=a`, + but with the following replacement for step X2: If + `v3 \le \sqrt{m/2}`, the algorithm terminates. The pair + `(x,y)=(|v2|,v3*\mathrm{sign}(v2))` is then the unique + solution, provided that `x` and `y` are coprime and + `x \le \sqrt{m/2}`; otherwise there is no solution. (Alg 4.5.2X is + the extended Euclidean algorithm.) + + Knuth remarks that this algorithm is due to Wang, Kornerup, and + Gregory from around 1983. + + EXAMPLES:: + + sage: m = 100000 + sage: (119*inverse_mod(53,m))%m + 11323 + sage: rational_reconstruction(11323,m) + 119/53 + + :: + + sage: rational_reconstruction(400,1000) + Traceback (most recent call last): + ... + ArithmeticError: rational reconstruction of 400 (mod 1000) does not exist + + :: + + sage: rational_reconstruction(3, 292393) + 3 + sage: a = Integers(292393)(45/97); a + 204977 + sage: rational_reconstruction(a, 292393, algorithm='fast') + 45/97 + sage: rational_reconstruction(293048, 292393) + Traceback (most recent call last): + ... + ArithmeticError: rational reconstruction of 655 (mod 292393) does not exist + sage: rational_reconstruction(0, 0) + Traceback (most recent call last): + ... + ZeroDivisionError: rational reconstruction with zero modulus + sage: rational_reconstruction(0, 1, algorithm="foobar") + Traceback (most recent call last): + ... + ValueError: unknown algorithm 'foobar' + """ + if algorithm == 'fast': + return ZZ(a).rational_reconstruction(m) + elif algorithm == 'python': + from sage.misc.superseded import deprecation + deprecation(17180, 'The %r algorithm for rational_reconstruction is deprecated' % algorithm) + return ZZ(a).rational_reconstruction(m) + else: + raise ValueError("unknown algorithm %r" % algorithm) + +def mqrr_rational_reconstruction(u, m, T): + r""" + Maximal Quotient Rational Reconstruction. + + For research purposes only - this is pure Python, so slow. + + INPUT: + + - ``u, m, T`` - integers such that `m > u \ge 0`, `T > 0`. + + OUTPUT: + + Either integers `n,d` such that `d>0`, `\mathop{\mathrm{gcd}}(n,d)=1`, `n/d=u \bmod m`, and + `T \cdot d \cdot |n| < m`, or ``None``. + + Reference: Monagan, Maximal Quotient Rational Reconstruction: An + Almost Optimal Algorithm for Rational Reconstruction (page 11) + + This algorithm is probabilistic. + + EXAMPLES:: + + sage: mqrr_rational_reconstruction(21,3100,13) + (21, 1) + """ + if u == 0: + if m > T: + return (0,1) + else: + return None + n, d = 0, 0 + t0, r0 = 0, m + t1, r1 = 1, u + while r1 != 0 and r0 > T: + q = r0/r1 # C division implicit floor + if q > T: + n, d, T = r1, t1, q + r0, r1 = r1, r0 - q*r1 + t0, t1 = t1, t0 - q*t1 + if d != 0 and GCD(n,d) == 1: + return (n,d) + return None + + +###################### + + +def trial_division(n, bound=None): + """ + Return the smallest prime divisor <= bound of the positive integer + n, or n if there is no such prime. If the optional argument bound + is omitted, then bound <= n. + + INPUT: + + - ``n`` - a positive integer + + - ``bound`` - (optional) a positive integer + + OUTPUT: + + - ``int`` - a prime p=bound that divides n, or n if + there is no such prime. + + + EXAMPLES:: + + sage: trial_division(15) + 3 + sage: trial_division(91) + 7 + sage: trial_division(11) + 11 + sage: trial_division(387833, 300) + 387833 + sage: # 300 is not big enough to split off a + sage: # factor, but 400 is. + sage: trial_division(387833, 400) + 389 + """ + if bound is None: + return ZZ(n).trial_division() + else: + return ZZ(n).trial_division(bound) + +def factor(n, proof=None, int_=False, algorithm='pari', verbose=0, **kwds): + """ + Returns the factorization of ``n``. The result depends on the + type of ``n``. + + If ``n`` is an integer, returns the factorization as an object + of type ``Factorization``. + + If n is not an integer, ``n.factor(proof=proof, **kwds)`` gets called. + See ``n.factor??`` for more documentation in this case. + + .. warning:: + + This means that applying ``factor`` to an integer result of + a symbolic computation will not factor the integer, because it is + considered as an element of a larger symbolic ring. + + EXAMPLE:: + + sage: f(n)=n^2 + sage: is_prime(f(3)) + False + sage: factor(f(3)) + 9 + + INPUT: + + - ``n`` - an nonzero integer + + - ``proof`` - bool or None (default: None) + + - ``int_`` - bool (default: False) whether to return + answers as Python ints + + - ``algorithm`` - string + + - ``'pari'`` - (default) use the PARI c library + + - ``'kash'`` - use KASH computer algebra system (requires the + optional kash package be installed) + + - ``'magma'`` - use Magma (requires magma be installed) + + - ``verbose`` - integer (default: 0); PARI's debug + variable is set to this; e.g., set to 4 or 8 to see lots of output + during factorization. + + OUTPUT: + + - factorization of n + + The qsieve and ecm commands give access to highly optimized + implementations of algorithms for doing certain integer + factorization problems. These implementations are not used by the + generic factor command, which currently just calls PARI (note that + PARI also implements sieve and ecm algorithms, but they aren't as + optimized). Thus you might consider using them instead for certain + numbers. + + The factorization returned is an element of the class + :class:`~sage.structure.factorization.Factorization`; see Factorization?? + for more details, and examples below for usage. A Factorization contains + both the unit factor (+1 or -1) and a sorted list of (prime, exponent) + pairs. + + The factorization displays in pretty-print format but it is easy to + obtain access to the (prime,exponent) pairs and the unit, to + recover the number from its factorization, and even to multiply two + factorizations. See examples below. + + EXAMPLES:: + + sage: factor(500) + 2^2 * 5^3 + sage: factor(-20) + -1 * 2^2 * 5 + sage: f=factor(-20) + sage: list(f) + [(2, 2), (5, 1)] + sage: f.unit() + -1 + sage: f.value() + -20 + sage: factor( -next_prime(10^2) * next_prime(10^7) ) + -1 * 101 * 10000019 + + :: + + sage: factor(-500, algorithm='kash') # optional - kash + -1 * 2^2 * 5^3 + + :: + + sage: factor(-500, algorithm='magma') # optional - magma + -1 * 2^2 * 5^3 + + :: + + sage: factor(0) + Traceback (most recent call last): + ... + ArithmeticError: Prime factorization of 0 not defined. + sage: factor(1) + 1 + sage: factor(-1) + -1 + sage: factor(2^(2^7)+1) + 59649589127497217 * 5704689200685129054721 + + Sage calls PARI's factor, which has proof False by default. + Sage has a global proof flag, set to True by default (see + :mod:`sage.structure.proof.proof`, or proof.[tab]). To override + the default, call this function with proof=False. + + :: + + sage: factor(3^89-1, proof=False) + 2 * 179 * 1611479891519807 * 5042939439565996049162197 + + :: + + sage: factor(2^197 + 1) # long time (2s) + 3 * 197002597249 * 1348959352853811313 * 251951573867253012259144010843 + + Any object which has a factor method can be factored like this:: + + sage: K. = QuadraticField(-1) + sage: factor(122 - 454*i) + (-3*i - 2) * (-i - 2)^3 * (i + 1)^3 * (i + 4) + + To access the data in a factorization:: + + sage: f = factor(420); f + 2^2 * 3 * 5 * 7 + sage: [x for x in f] + [(2, 2), (3, 1), (5, 1), (7, 1)] + sage: [p for p,e in f] + [2, 3, 5, 7] + sage: [e for p,e in f] + [2, 1, 1, 1] + sage: [p^e for p,e in f] + [4, 3, 5, 7] + + """ + if isinstance(n, (int, long)): + n = ZZ(n) + + if isinstance(n, Integer): + return n.factor(proof=proof, algorithm=algorithm, + int_ = int_, verbose=verbose) + else: + # e.g. n = x**2 + y**2 + 2*x*y + try: + return n.factor(proof=proof, **kwds) + except AttributeError: + raise TypeError("unable to factor n") + except TypeError: + # Just in case factor method doesn't have a proof option. + try: + return n.factor(**kwds) + except AttributeError: + raise TypeError("unable to factor n") + +def radical(n, *args, **kwds): + """ + Return the product of the prime divisors of n. + + This calls ``n.radical(*args, **kwds)``. If that doesn't work, it + does ``n.factor(*args, **kwds)`` and returns the product of the prime + factors in the resulting factorization. + + EXAMPLES:: + + sage: radical(2 * 3^2 * 5^5) + 30 + sage: radical(0) + Traceback (most recent call last): + ... + ArithmeticError: Radical of 0 not defined. + sage: K. = QuadraticField(-1) + sage: radical(K(2)) + i + 1 + + The next example shows how to compute the radical of a number, + assuming no prime > 100000 has exponent > 1 in the factorization:: + + sage: n = 2^1000-1; n / radical(n, limit=100000) + 125 + """ + try: + return n.radical(*args, **kwds) + except AttributeError: + return n.factor(*args, **kwds).radical_value() + +def prime_divisors(n): + """ + The prime divisors of ``n``. + + INPUT: + + - ``n`` -- any object which can be factored + + OUTPUT: + + A list of prime factors of ``n``. For integers, this list is sorted + in increasing order. + + EXAMPLES:: + + sage: prime_divisors(1) + [] + sage: prime_divisors(100) + [2, 5] + sage: prime_divisors(2004) + [2, 3, 167] + + If ``n`` is negative, we do *not* include -1 among the prime + divisors, since -1 is not a prime number:: + + sage: prime_divisors(-100) + [2, 5] + + For polynomials we get all irreducible factors:: + + sage: R. = PolynomialRing(QQ) + sage: prime_divisors(x^12 - 1) + [x - 1, x + 1, x^2 - x + 1, x^2 + 1, x^2 + x + 1, x^4 - x^2 + 1] + """ + try: + return n.prime_divisors() + except AttributeError: + pass + return [p for p,_ in factor(n)] + +prime_factors = prime_divisors + +def odd_part(n): + r""" + The odd part of the integer `n`. This is `n / 2^v`, + where `v = \mathrm{valuation}(n,2)`. + + EXAMPLES:: + + sage: odd_part(5) + 5 + sage: odd_part(4) + 1 + sage: odd_part(factorial(31)) + 122529844256906551386796875 + """ + if not isinstance(n, Integer): + n = ZZ(n) + return n.odd_part() + +def prime_to_m_part(n,m): + """ + Returns the prime-to-m part of n, i.e., the largest divisor of n + that is coprime to m. + + INPUT: + + - ``n`` - Integer (nonzero) + + - ``m`` - Integer + + OUTPUT: Integer + + EXAMPLES:: + + sage: 240.prime_to_m_part(2) + 15 + sage: 240.prime_to_m_part(3) + 80 + sage: 240.prime_to_m_part(5) + 48 + + sage: 43434.prime_to_m_part(20) + 21717 + """ + return ZZ(n).prime_to_m_part(m) + +def is_square(n, root=False): + """ + Returns whether or not n is square, and if n is a square also + returns the square root. If n is not square, also returns None. + + INPUT: + + + - ``n`` - an integer + + - ``root`` - whether or not to also return a square + root (default: False) + + + OUTPUT: + + + - ``bool`` - whether or not a square + + - ``object`` - (optional) an actual square if found, + and None otherwise. + + + EXAMPLES:: + + sage: is_square(2) + False + sage: is_square(4) + True + sage: is_square(2.2) + True + sage: is_square(-2.2) + False + sage: is_square(CDF(-2.2)) + True + sage: is_square((x-1)^2) + True + + :: + + sage: is_square(4, True) + (True, 2) + """ + if isinstance(n, (int,long)): + n = ZZ(n) + try: + if root: + try: + return n.is_square(root) + except TypeError: + if n.is_square(): + return True, n.sqrt() + else: + return False, None + return n.is_square() + except (AttributeError, NotImplementedError): + pass + t, x = pari(n).issquare(find_root=True) + if root: + if t: + x = parent(n)(x) + return t, x + return t + +def is_squarefree(n): + """ + Test whether ``n`` is square free. + + EXAMPLES:: + + sage: is_squarefree(100) + False + sage: is_squarefree(101) + True + + sage: R = ZZ['x'] + sage: x = R.gen() + sage: is_squarefree((x^2+x+1) * (x-2)) + True + sage: is_squarefree((x-1)**2 * (x-3)) + False + + sage: O = ZZ[sqrt(-1)] + sage: I = O.gen(1) + sage: is_squarefree(I+1) + True + sage: is_squarefree(O(2)) + False + sage: O(2).factor() + (-I) * (I + 1)^2 + + This method fails on domains which are not Unique Factorization Domains:: + + sage: O = ZZ[sqrt(-5)] + sage: a = O.gen(1) + sage: is_squarefree(a - 3) + Traceback (most recent call last): + ... + ArithmeticError: non-principal ideal in factorization + """ + if isinstance(n, (int,long)): + n = Integer(n) + + try: + return n.is_squarefree() + except AttributeError: + pass + + if n == 0: + return False + return all(r[1] == 1 for r in factor(n)) + + +################################################################# +# Euler phi function +################################################################# +class Euler_Phi: + r""" + Return the value of the Euler phi function on the integer n. We + defined this to be the number of positive integers <= n that are + relatively prime to n. Thus if n<=0 then + ``euler_phi(n)`` is defined and equals 0. + + INPUT: + + + - ``n`` - an integer + + + EXAMPLES:: + + sage: euler_phi(1) + 1 + sage: euler_phi(2) + 1 + sage: euler_phi(3) + 2 + sage: euler_phi(12) + 4 + sage: euler_phi(37) + 36 + + Notice that euler_phi is defined to be 0 on negative numbers and + 0. + + :: + + sage: euler_phi(-1) + 0 + sage: euler_phi(0) + 0 + sage: type(euler_phi(0)) + + + We verify directly that the phi function is correct for 21. + + :: + + sage: euler_phi(21) + 12 + sage: [i for i in range(21) if gcd(21,i) == 1] + [1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20] + + The length of the list of integers 'i' in range(n) such that the + gcd(i,n) == 1 equals euler_phi(n). + + :: + + sage: len([i for i in range(21) if gcd(21,i) == 1]) == euler_phi(21) + True + + The phi function also has a special plotting method. + + :: + + sage: P = plot(euler_phi, -3, 71) + + AUTHORS: + + - William Stein + + - Alex Clemesha (2006-01-10): some examples + """ + def __repr__(self): + """ + Returns a string describing this class. + + EXAMPLES:: + + sage: Euler_Phi().__repr__() + 'Number of positive integers <=n but relatively prime to n' + """ + return "Number of positive integers <=n but relatively prime to n" + + def __call__(self, n): + """ + Calls the euler_phi function. + + EXAMPLES:: + + sage: Euler_Phi()(10) + 4 + sage: Euler_Phi()(720) + 192 + """ + if n<=0: + return ZZ(0) + if n<=2: + return ZZ(1) + return ZZ(pari(n).phi()) + + def plot(self, xmin=1, xmax=50, pointsize=30, rgbcolor=(0,0,1), join=True, + **kwds): + """ + Plot the Euler phi function. + + INPUT: + + + - ``xmin`` - default: 1 + + - ``xmax`` - default: 50 + + - ``pointsize`` - default: 30 + + - ``rgbcolor`` - default: (0,0,1) + + - ``join`` - default: True; whether to join the + points. + + - ``**kwds`` - passed on + + EXAMPLES:: + + sage: p = Euler_Phi().plot() + sage: p.ymax() + 46.0 + """ + v = [(n,euler_phi(n)) for n in range(xmin,xmax + 1)] + from sage.plot.all import list_plot + P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) + if join: + P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) + return P + +euler_phi = Euler_Phi() + +def crt(a,b,m=None,n=None): + r""" + Returns a solution to a Chinese Remainder Theorem problem. + + INPUT: + + - ``a``, ``b`` - two residues (elements of some ring for which + extended gcd is available), or two lists, one of residues and + one of moduli. + + - ``m``, ``n`` - (default: ``None``) two moduli, or ``None``. + + OUTPUT: + + If ``m``, ``n`` are not ``None``, returns a solution `x` to the + simultaneous congruences `x\equiv a \bmod m` and `x\equiv b \bmod + n`, if one exists. By the Chinese Remainder Theorem, a solution to the + simultaneous congruences exists if and only if + `a\equiv b\pmod{\gcd(m,n)}`. The solution `x` is only well-defined modulo + `\text{lcm}(m,n)`. + + If ``a`` and ``b`` are lists, returns a simultaneous solution to + the congruences `x\equiv a_i\pmod{b_i}`, if one exists. + + .. SEEALSO:: + + - :func:`CRT_list` + + EXAMPLES: + + Using ``crt`` by giving it pairs of residues and moduli:: + + sage: crt(2, 1, 3, 5) + 11 + sage: crt(13, 20, 100, 301) + 28013 + sage: crt([2, 1], [3, 5]) + 11 + sage: crt([13, 20], [100, 301]) + 28013 + + You can also use upper case:: + + sage: c = CRT(2,3, 3, 5); c + 8 + sage: c % 3 == 2 + True + sage: c % 5 == 3 + True + + Note that this also works for polynomial rings:: + + sage: K. = NumberField(x^3 - 7) + sage: R. = K[] + sage: f = y^2 + 3 + sage: g = y^3 - 5 + sage: CRT(1,3,f,g) + -3/26*y^4 + 5/26*y^3 + 15/26*y + 53/26 + sage: CRT(1,a,f,g) + (-3/52*a + 3/52)*y^4 + (5/52*a - 5/52)*y^3 + (15/52*a - 15/52)*y + 27/52*a + 25/52 + + You can also do this for any number of moduli:: + + sage: K. = NumberField(x^3 - 7) + sage: R. = K[] + sage: CRT([], []) + 0 + sage: CRT([a], [x]) + a + sage: f = x^2 + 3 + sage: g = x^3 - 5 + sage: h = x^5 + x^2 - 9 + sage: k = CRT([1, a, 3], [f, g, h]); k + (127/26988*a - 5807/386828)*x^9 + (45/8996*a - 33677/1160484)*x^8 + (2/173*a - 6/173)*x^7 + (133/6747*a - 5373/96707)*x^6 + (-6/2249*a + 18584/290121)*x^5 + (-277/8996*a + 38847/386828)*x^4 + (-135/4498*a + 42673/193414)*x^3 + (-1005/8996*a + 470245/1160484)*x^2 + (-1215/8996*a + 141165/386828)*x + 621/8996*a + 836445/386828 + sage: k.mod(f) + 1 + sage: k.mod(g) + a + sage: k.mod(h) + 3 + + If the moduli are not coprime, a solution may not exist:: + + sage: crt(4,8,8,12) + 20 + sage: crt(4,6,8,12) + Traceback (most recent call last): + ... + ValueError: No solution to crt problem since gcd(8,12) does not divide 4-6 + + sage: x = polygen(QQ) + sage: crt(2,3,x-1,x+1) + -1/2*x + 5/2 + sage: crt(2,x,x^2-1,x^2+1) + -1/2*x^3 + x^2 + 1/2*x + 1 + sage: crt(2,x,x^2-1,x^3-1) + Traceback (most recent call last): + ... + ValueError: No solution to crt problem since gcd(x^2 - 1,x^3 - 1) does not divide 2-x + + sage: crt(int(2), int(3), int(7), int(11)) + 58 + """ + if isinstance(a, list): + return CRT_list(a, b) + if isinstance(a, (int, long)): + a = Integer(a) # otherwise we get an error at (b-a).quo_rem(g) + g, alpha, beta = XGCD(m, n) + q, r = (b - a).quo_rem(g) + if r != 0: + raise ValueError("No solution to crt problem since gcd(%s,%s) does not divide %s-%s" % (m, n, a, b)) + return (a + q*alpha*m) % lcm(m, n) + +CRT = crt + +def CRT_list(v, moduli): + r""" Given a list ``v`` of elements and a list of corresponding + ``moduli``, find a single element that reduces to each element of + ``v`` modulo the corresponding moduli. + + .. SEEALSO:: + + - :func:`crt` + + EXAMPLES:: + + sage: CRT_list([2,3,2], [3,5,7]) + 23 + sage: x = polygen(QQ) + sage: c = CRT_list([3], [x]); c + 3 + sage: c.parent() + Univariate Polynomial Ring in x over Rational Field + + It also works if the moduli are not coprime:: + + sage: CRT_list([32,2,2],[60,90,150]) + 452 + + But with non coprime moduli there is not always a solution:: + + sage: CRT_list([32,2,1],[60,90,150]) + Traceback (most recent call last): + ... + ValueError: No solution to crt problem since gcd(180,150) does not divide 92-1 + + The arguments must be lists:: + + sage: CRT_list([1,2,3],"not a list") + Traceback (most recent call last): + ... + ValueError: Arguments to CRT_list should be lists + sage: CRT_list("not a list",[2,3]) + Traceback (most recent call last): + ... + ValueError: Arguments to CRT_list should be lists + + The list of moduli must have the same length as the list of elements:: + + sage: CRT_list([1,2,3],[2,3,5]) + 23 + sage: CRT_list([1,2,3],[2,3]) + Traceback (most recent call last): + ... + ValueError: Arguments to CRT_list should be lists of the same length + sage: CRT_list([1,2,3],[2,3,5,7]) + Traceback (most recent call last): + ... + ValueError: Arguments to CRT_list should be lists of the same length + + TESTS:: + + sage: CRT([32r,2r,2r],[60r,90r,150r]) + 452 + + """ + if not isinstance(v,list) or not isinstance(moduli,list): + raise ValueError("Arguments to CRT_list should be lists") + if len(v) != len(moduli): + raise ValueError("Arguments to CRT_list should be lists of the same length") + if len(v) == 0: + return ZZ(0) + if len(v) == 1: + return moduli[0].parent()(v[0]) + x = v[0] + m = moduli[0] + for i in range(1,len(v)): + x = CRT(x,v[i],m,moduli[i]) + m = lcm(m,moduli[i]) + return x%m + +def CRT_basis(moduli): + r""" + Returns a CRT basis for the given moduli. + + INPUT: + + - ``moduli`` - list of pairwise coprime moduli `m` which admit an + extended Euclidean algorithm + + OUTPUT: + + - a list of elements `a_i` of the same length as `m` such that + `a_i` is congruent to 1 modulo `m_i` and to 0 modulo `m_j` for + `j\not=i`. + + .. note:: + + The pairwise coprimality of the input is not checked. + + EXAMPLES:: + + sage: a1 = ZZ(mod(42,5)) + sage: a2 = ZZ(mod(42,13)) + sage: c1,c2 = CRT_basis([5,13]) + sage: mod(a1*c1+a2*c2,5*13) + 42 + + A polynomial example:: + + sage: x=polygen(QQ) + sage: mods = [x,x^2+1,2*x-3] + sage: b = CRT_basis(mods) + sage: b + [-2/3*x^3 + x^2 - 2/3*x + 1, 6/13*x^3 - x^2 + 6/13*x, 8/39*x^3 + 8/39*x] + sage: [[bi % mj for mj in mods] for bi in b] + [[1, 0, 0], [0, 1, 0], [0, 0, 1]] + """ + n = len(moduli) + if n == 0: + return [] + M = prod(moduli) + return [((xgcd(m,M//m)[2])*(M//m))%M for m in moduli] + +def CRT_vectors(X, moduli): + r""" + Vector form of the Chinese Remainder Theorem: given a list of integer + vectors `v_i` and a list of coprime moduli `m_i`, find a vector `w` such + that `w = v_i \pmod m_i` for all `i`. This is more efficient than applying + :func:`CRT` to each entry. + + INPUT: + + - ``X`` - list or tuple, consisting of lists/tuples/vectors/etc of + integers of the same length + - ``moduli`` - list of len(X) moduli + + OUTPUT: + + - ``list`` - application of CRT componentwise. + + EXAMPLES:: + + sage: CRT_vectors([[3,5,7],[3,5,11]], [2,3]) + [3, 5, 5] + + sage: CRT_vectors([vector(ZZ, [2,3,1]), Sequence([1,7,8],ZZ)], [8,9]) + [10, 43, 17] + """ + # First find the CRT basis: + if len(X) == 0 or len(X[0]) == 0: + return [] + n = len(X) + if n != len(moduli): + raise ValueError("number of moduli must equal length of X") + a = CRT_basis(moduli) + modulus = prod(moduli) + return [sum(a[i]*X[i][j] for i in range(n)) % modulus for j in range(len(X[0]))] + +def binomial(x, m, **kwds): + r""" + Return the binomial coefficient + + .. math:: + + \binom{x}{m} = x (x-1) \cdots (x-m+1) / m! + + which is defined for `m \in \ZZ` and any + `x`. We extend this definition to include cases when + `x-m` is an integer but `m` is not by + + .. math:: + + \binom{x}{m} = \binom{x}{x-m} + + If `m < 0`, return `0`. + + INPUT: + + - ``x``, ``m`` - numbers or symbolic expressions. Either ``m`` + or ``x-m`` must be an integer. + + OUTPUT: number or symbolic expression (if input is symbolic) + + EXAMPLES:: + + sage: from sage.arith.misc import binomial + sage: binomial(5,2) + 10 + sage: binomial(2,0) + 1 + sage: binomial(1/2, 0) + 1 + sage: binomial(3,-1) + 0 + sage: binomial(20,10) + 184756 + sage: binomial(-2, 5) + -6 + sage: binomial(-5, -2) + 0 + sage: binomial(RealField()('2.5'), 2) + 1.87500000000000 + sage: n=var('n'); binomial(n,2) + 1/2*(n - 1)*n + sage: n=var('n'); binomial(n,n) + 1 + sage: n=var('n'); binomial(n,n-1) + n + sage: binomial(2^100, 2^100) + 1 + + sage: x = polygen(ZZ) + sage: binomial(x, 3) + 1/6*x^3 - 1/2*x^2 + 1/3*x + sage: binomial(x, x-3) + 1/6*x^3 - 1/2*x^2 + 1/3*x + + If `x \in \ZZ`, there is an optional 'algorithm' parameter, which + can be 'mpir' (faster for small values) or 'pari' (faster for + large values):: + + sage: a = binomial(100, 45, algorithm='mpir') + sage: b = binomial(100, 45, algorithm='pari') + sage: a == b + True + + TESTS: + + We test that certain binomials are very fast (this should be + instant) -- see :trac:`3309`:: + + sage: a = binomial(RR(1140000.78), 23310000) + + We test conversion of arguments to Integers -- see :trac:`6870`:: + + sage: binomial(1/2,1/1) + 1/2 + sage: binomial(10^20+1/1,10^20) + 100000000000000000001 + sage: binomial(SR(10**7),10**7) + 1 + sage: binomial(3/2,SR(1/1)) + 3/2 + + Some floating point cases -- see :trac:`7562`, :trac:`9633`, and + :trac:`12448`:: + + sage: binomial(1.,3) + 0.000000000000000 + sage: binomial(-2.,3) + -4.00000000000000 + sage: binomial(0.5r, 5) + 0.02734375 + sage: a = binomial(float(1001), float(1)); a + 1001.0 + sage: type(a) + + sage: binomial(float(1000), 1001) + 0.0 + + Test more output types:: + + sage: type(binomial(5r, 2)) + + sage: type(binomial(5, 2r)) + + + sage: type(binomial(5.0r, 2)) + + + sage: type(binomial(5/1, 2)) + + + sage: R = Integers(11) + sage: b = binomial(R(7), R(3)) + sage: b + 2 + sage: b.parent() + Ring of integers modulo 11 + + Test symbolic and uni/multivariate polynomials:: + + sage: x = polygen(ZZ) + sage: binomial(x, 3) + 1/6*x^3 - 1/2*x^2 + 1/3*x + sage: binomial(x, 3).parent() + Univariate Polynomial Ring in x over Rational Field + + sage: K. = Integers(7)[] + sage: binomial(y,3) + -y^3 + 3*y^2 - 2*y + sage: binomial(y,3).parent() + Multivariate Polynomial Ring in x, y over Ring of integers modulo 7 + + sage: n = var('n') + sage: binomial(n,2) + 1/2*(n - 1)*n + + Invalid inputs:: + + sage: x = polygen(ZZ) + sage: binomial(x, x^2) + Traceback (most recent call last): + ... + TypeError: either m or x-m must be an integer + + sage: k, i = var('k,i') + sage: binomial(k,i) + Traceback (most recent call last): + ... + TypeError: either m or x-m must be an integer + + sage: R6 = Zmod(6) + sage: binomial(R6(5), 2) + Traceback (most recent call last): + ... + ZeroDivisionError: factorial(2) not invertible in Ring of integers modulo 6 + + sage: R7 = Zmod(7) + sage: binomial(R7(10), 7) + Traceback (most recent call last): + ... + ZeroDivisionError: factorial(7) not invertible in Ring of integers modulo 7 + + The last two examples failed to execute since `2!` and `7!` are respectively + not invertible in `\ZZ/6\ZZ` and `\ZZ/7\ZZ`. One can check that there + is no well defined value for that binomial coefficient in the quotient:: + + sage: R6(binomial(5,2)) + 4 + sage: R6(binomial(5+6,2)) + 1 + + sage: R7(binomial(3, 7)) + 0 + sage: R7(binomial(10, 7)) + 1 + sage: R7(binomial(17, 7)) + 2 + + For symbolic manipulation, you should use the function + :func:`~sage.functions.other.binomial` from the module + :mod:`sage.functions.other`:: + + sage: from sage.functions.other import binomial + sage: binomial(k, i) + binomial(k, i) + """ + try: + m = ZZ(m) + except TypeError: + try: + m = ZZ(x-m) + except TypeError: + raise TypeError("either m or x-m must be an integer") + + P = parent(x) + x = py_scalar_to_element(x) + + # case 1: native binomial implemented on x + try: + return P(x.binomial(m, **kwds)) + except (AttributeError,TypeError): + pass + + # case 2: conversion to integers + try: + x = ZZ(x) + except TypeError: + pass + else: + # Check invertibility of factorial(m) in P + try: + c = P.characteristic() + except AttributeError: + # Assume that P has characteristic zero (can be int, float, ...) + pass + else: + if c > 0 and any(c.gcd(k) > 1 for k in range(2, m+1)): + raise ZeroDivisionError("factorial({}) not invertible in {}".format(m, P)) + return P(x.binomial(m, **kwds)) + + # case 3: rational, real numbers, complex numbers -> use pari + if isinstance(x, (Rational, RealNumber, ComplexNumber)): + return P(x._pari_().binomial(m)) + + # case 4: naive method + if m < ZZ.zero(): + return P(0) + return P(prod(x-i for i in xrange(m))) / m.factorial() + +def multinomial(*ks): + r""" + Return the multinomial coefficient + + INPUT: + + - An arbitrary number of integer arguments `k_1,\dots,k_n` + - A list of integers `[k_1,\dots,k_n]` + + OUTPUT: + + Returns the integer: + + .. math:: + + \binom{k_1 + \cdots + k_n}{k_1, \cdots, k_n} + =\frac{\left(\sum_{i=1}^n k_i\right)!}{\prod_{i=1}^n k_i!} + = \prod_{i=1}^n \binom{\sum_{j=1}^i k_j}{k_i} + + EXAMPLES:: + + sage: multinomial(0, 0, 2, 1, 0, 0) + 3 + sage: multinomial([0, 0, 2, 1, 0, 0]) + 3 + sage: multinomial(3, 2) + 10 + sage: multinomial(2^30, 2, 1) + 618970023101454657175683075 + sage: multinomial([2^30, 2, 1]) + 618970023101454657175683075 + + AUTHORS: + + - Gabriel Ebner + """ + if isinstance(ks[0],list): + if len(ks) >1: + raise ValueError("multinomial takes only one list argument") + ks=ks[0] + + s, c = 0, 1 + for k in ks: + s += k + c *= binomial(s, k) + return c + +def binomial_coefficients(n): + r""" + Return a dictionary containing pairs + `\{(k_1,k_2) : C_{k,n}\}` where `C_{k_n}` are + binomial coefficients and `n = k_1 + k_2`. + + INPUT: + + + - ``n`` - an integer + + + OUTPUT: dict + + EXAMPLES:: + + sage: sorted(binomial_coefficients(3).items()) + [((0, 3), 1), ((1, 2), 3), ((2, 1), 3), ((3, 0), 1)] + + Notice the coefficients above are the same as below:: + + sage: R. = QQ[] + sage: (x+y)^3 + x^3 + 3*x^2*y + 3*x*y^2 + y^3 + + AUTHORS: + + - Fredrik Johansson + """ + d = {(0, n):1, (n, 0):1} + a = 1 + for k in xrange(1, n//2+1): + a = (a * (n-k+1))//k + d[k, n-k] = d[n-k, k] = a + return d + +def multinomial_coefficients(m, n): + r""" + Return a dictionary containing pairs + `\{(k_1, k_2, ..., k_m) : C_{k, n}\}` where + `C_{k, n}` are multinomial coefficients such that + `n = k_1 + k_2 + ...+ k_m`. + + INPUT: + + - ``m`` - integer + - ``n`` - integer + + OUTPUT: dict + + EXAMPLES:: + + sage: sorted(multinomial_coefficients(2, 5).items()) + [((0, 5), 1), ((1, 4), 5), ((2, 3), 10), ((3, 2), 10), ((4, 1), 5), ((5, 0), 1)] + + Notice that these are the coefficients of `(x+y)^5`:: + + sage: R. = QQ[] + sage: (x+y)^5 + x^5 + 5*x^4*y + 10*x^3*y^2 + 10*x^2*y^3 + 5*x*y^4 + y^5 + + :: + + sage: sorted(multinomial_coefficients(3, 2).items()) + [((0, 0, 2), 1), ((0, 1, 1), 2), ((0, 2, 0), 1), ((1, 0, 1), 2), ((1, 1, 0), 2), ((2, 0, 0), 1)] + + ALGORITHM: The algorithm we implement for computing the multinomial + coefficients is based on the following result: + + ..math:: + + \binom{n}{k_1, \cdots, k_m} = + \frac{k_1+1}{n-k_1}\sum_{i=2}^m \binom{n}{k_1+1, \cdots, k_i-1, \cdots} + + e.g.:: + + sage: k = (2, 4, 1, 0, 2, 6, 0, 0, 3, 5, 7, 1) # random value + sage: n = sum(k) + sage: s = 0 + sage: for i in range(1, len(k)): + ....: ki = list(k) + ....: ki[0] += 1 + ....: ki[i] -= 1 + ....: s += multinomial(n, *ki) + sage: multinomial(n, *k) == (k[0] + 1) / (n - k[0]) * s + True + + TESTS:: + + sage: multinomial_coefficients(0, 0) + {(): 1} + sage: multinomial_coefficients(0, 3) + {} + + """ + if not m: + if n: + return {} + else: + return {(): 1} + if m == 2: + return binomial_coefficients(n) + t = [n] + [0] * (m - 1) + r = {tuple(t): 1} + if n: + j = 0 # j will be the leftmost nonzero position + else: + j = m + # enumerate tuples in co-lex order + while j < m - 1: + # compute next tuple + tj = t[j] + if j: + t[j] = 0 + t[0] = tj + if tj > 1: + t[j + 1] += 1 + j = 0 + start = 1 + v = 0 + else: + j += 1 + start = j + 1 + v = r[tuple(t)] + t[j] += 1 + # compute the value + # NB: the initialization of v was done above + for k in xrange(start, m): + if t[k]: + t[k] -= 1 + v += r[tuple(t)] + t[k] += 1 + t[0] -= 1 + r[tuple(t)] = (v * tj) // (n - t[0]) + return r + + +def kronecker_symbol(x,y): + """ + The Kronecker symbol `(x|y)`. + + INPUT: + + - ``x`` -- integer + + - ``y`` -- integer + + OUTPUT: + + - an integer + + EXAMPLES:: + + sage: kronecker_symbol(13,21) + -1 + sage: kronecker_symbol(101,4) + 1 + + This is also available as :func:`kronecker`:: + + sage: kronecker(3,5) + -1 + sage: kronecker(3,15) + 0 + sage: kronecker(2,15) + 1 + sage: kronecker(-2,15) + -1 + sage: kronecker(2/3,5) + 1 + """ + x = QQ(x).numerator() * QQ(x).denominator() + return ZZ(x.kronecker(y)) + +kronecker = kronecker_symbol + + +def legendre_symbol(x,p): + r""" + The Legendre symbol `(x|p)`, for `p` prime. + + .. note:: + + The :func:`kronecker_symbol` command extends the Legendre + symbol to composite moduli and `p=2`. + + INPUT: + + + - ``x`` - integer + + - ``p`` - an odd prime number + + + EXAMPLES:: + + sage: legendre_symbol(2,3) + -1 + sage: legendre_symbol(1,3) + 1 + sage: legendre_symbol(1,2) + Traceback (most recent call last): + ... + ValueError: p must be odd + sage: legendre_symbol(2,15) + Traceback (most recent call last): + ... + ValueError: p must be a prime + sage: kronecker_symbol(2,15) + 1 + sage: legendre_symbol(2/3,7) + -1 + """ + x = QQ(x).numerator() * QQ(x).denominator() + p = ZZ(p) + if not p.is_prime(): + raise ValueError("p must be a prime") + if p == 2: + raise ValueError("p must be odd") + return x.kronecker(p) + +def jacobi_symbol(a,b): + r""" + The Jacobi symbol of integers a and b, where b is odd. + + .. note:: + + The :func:`kronecker_symbol` command extends the Jacobi + symbol to all integers b. + + If + + `b = p_1^{e_1} * ... * p_r^{e_r}` + + then + + `(a|b) = (a|p_1)^{e_1} ... (a|p_r)^{e_r}` + + where `(a|p_j)` are Legendre Symbols. + + + + INPUT: + + - ``a`` - an integer + + - ``b`` - an odd integer + + EXAMPLES:: + + sage: jacobi_symbol(10,777) + -1 + sage: jacobi_symbol(10,5) + 0 + sage: jacobi_symbol(10,2) + Traceback (most recent call last): + ... + ValueError: second input must be odd, 2 is not odd + """ + + if b%2==0: + raise ValueError("second input must be odd, %s is not odd"%b) + + return kronecker_symbol(a,b) + +def primitive_root(n, check=True): + """ + Return a positive integer that generates the multiplicative group + of integers modulo `n`, if one exists; otherwise, raise a + ``ValueError``. + + A primitive root exists if `n=4` or `n=p^k` or `n=2p^k`, where `p` + is an odd prime and `k` is a nonnegative number. + + INPUT: + + - ``n`` -- a non-zero integer + - ``check`` -- bool (default: True); if False, then `n` is assumed + to be a positive integer possessing a primitive root, and behavior + is undefined otherwise. + + OUTPUT: + + A primitive root of `n`. If `n` is prime, this is the smallest + primitive root. + + EXAMPLES:: + + sage: primitive_root(23) + 5 + sage: primitive_root(-46) + 5 + sage: primitive_root(25) + 2 + sage: print [primitive_root(p) for p in primes(100)] + [1, 2, 2, 3, 2, 2, 3, 2, 5, 2, 3, 2, 6, 3, 5, 2, 2, 2, 2, 7, 5, 3, 2, 3, 5] + sage: primitive_root(8) + Traceback (most recent call last): + ... + ValueError: no primitive root + + .. NOTE:: + + It takes extra work to check if `n` has a primitive root; to + avoid this, use ``check=False``, which may slightly speed things + up (but could also result in undefined behavior). For example, + the second call below is an order of magnitude faster than the + first: + + :: + + sage: n = 10^50 + 151 # a prime + sage: primitive_root(n) + 11 + sage: primitive_root(n, check=False) + 11 + + TESTS: + + Various special cases:: + + sage: primitive_root(-1) + 0 + sage: primitive_root(0) + Traceback (most recent call last): + ... + ValueError: no primitive root + sage: primitive_root(1) + 0 + sage: primitive_root(2) + 1 + sage: primitive_root(3) + 2 + sage: primitive_root(4) + 3 + + We test that various numbers without primitive roots give + an error - see :trac:`10836`:: + + sage: primitive_root(15) + Traceback (most recent call last): + ... + ValueError: no primitive root + sage: primitive_root(16) + Traceback (most recent call last): + ... + ValueError: no primitive root + sage: primitive_root(1729) + Traceback (most recent call last): + ... + ValueError: no primitive root + sage: primitive_root(4*7^8) + Traceback (most recent call last): + ... + ValueError: no primitive root + """ + if not check: + return ZZ(pari(n).znprimroot()) + n = ZZ(n).abs() + if n <= 4: + if n: + # n-1 is a primitive root for n in {1,2,3,4} + return n-1 + elif n%2: # n odd + if n.is_prime_power(): + return ZZ(pari(n).znprimroot()) + else: # n even + m = n // 2 + if m%2 and m.is_prime_power(): + return ZZ(pari(n).znprimroot()) + raise ValueError("no primitive root") + +def nth_prime(n): + """ + + Return the n-th prime number (1-indexed, so that 2 is the 1st prime.) + + INPUT: + + - ``n`` -- a positive integer + + OUTPUT: + + - the n-th prime number + + EXAMPLES:: + + sage: nth_prime(3) + 5 + sage: nth_prime(10) + 29 + + :: + + sage: nth_prime(0) + Traceback (most recent call last): + ... + ValueError: nth prime meaningless for non-positive n (=0) + + TESTS:: + + sage: all(prime_pi(nth_prime(j)) == j for j in range(1, 1000, 10)) + True + + """ + return ZZ(pari.nth_prime(n)) + +def quadratic_residues(n): + r""" + Return a sorted list of all squares modulo the integer `n` + in the range `0\leq x < |n|`. + + EXAMPLES:: + + sage: quadratic_residues(11) + [0, 1, 3, 4, 5, 9] + sage: quadratic_residues(1) + [0] + sage: quadratic_residues(2) + [0, 1] + sage: quadratic_residues(8) + [0, 1, 4] + sage: quadratic_residues(-10) + [0, 1, 4, 5, 6, 9] + sage: v = quadratic_residues(1000); len(v); + 159 + """ + n = abs(int(n)) + X = sorted(set(ZZ((a*a)%n) for a in range(n//2+1))) + return X + +class Moebius: + r""" + Returns the value of the Moebius function of abs(n), where n is an + integer. + + DEFINITION: `\mu(n)` is 0 if `n` is not square + free, and otherwise equals `(-1)^r`, where `n` has + `r` distinct prime factors. + + For simplicity, if `n=0` we define `\mu(n) = 0`. + + IMPLEMENTATION: Factors or - for integers - uses the PARI C + library. + + INPUT: + + + - ``n`` - anything that can be factored. + + + OUTPUT: 0, 1, or -1 + + EXAMPLES:: + + sage: moebius(-5) + -1 + sage: moebius(9) + 0 + sage: moebius(12) + 0 + sage: moebius(-35) + 1 + sage: moebius(-1) + 1 + sage: moebius(7) + -1 + + :: + + sage: moebius(0) # potentially nonstandard! + 0 + + The moebius function even makes sense for non-integer inputs. + + :: + + sage: x = GF(7)['x'].0 + sage: moebius(x+2) + -1 + """ + def __call__(self, n): + """ + EXAMPLES:: + + sage: Moebius().__call__(7) + -1 + """ + if isinstance(n, (int, long)): + n = ZZ(n) + elif not isinstance(n, Integer): + # Use a generic algorithm. + if n < 0: + n = -n + F = factor(n) + for _, e in F: + if e >= 2: + return 0 + return (-1)**len(F) + + # Use fast PARI algorithm + if n == 0: + return ZZ.zero() + return ZZ(pari(n).moebius()) + + + def __repr__(self): + """ + Returns a description of this function. + + EXAMPLES:: + + sage: q = Moebius() + sage: q.__repr__() + 'The Moebius function' + """ + return "The Moebius function" + + def plot(self, xmin=0, xmax=50, pointsize=30, rgbcolor=(0,0,1), join=True, + **kwds): + """ + Plot the Moebius function. + + INPUT: + + + - ``xmin`` - default: 0 + + - ``xmax`` - default: 50 + + - ``pointsize`` - default: 30 + + - ``rgbcolor`` - default: (0,0,1) + + - ``join`` - default: True; whether to join the points + (very helpful in seeing their order). + + - ``**kwds`` - passed on + + EXAMPLES:: + + sage: p = Moebius().plot() + sage: p.ymax() + 1.0 + """ + values = self.range(xmin, xmax + 1) + v = [(n,values[n-xmin]) for n in range(xmin,xmax + 1)] + from sage.plot.all import list_plot + P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) + if join: + P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) + return P + + def range(self, start, stop=None, step=None): + """ + Return the Moebius function evaluated at the given range of values, + i.e., the image of the list range(start, stop, step) under the + Mobius function. + + This is much faster than directly computing all these values with a + list comprehension. + + EXAMPLES:: + + sage: v = moebius.range(-10,10); v + [1, 0, 0, -1, 1, -1, 0, -1, -1, 1, 0, 1, -1, -1, 0, -1, 1, -1, 0, 0] + sage: v == [moebius(n) for n in range(-10,10)] + True + sage: v = moebius.range(-1000, 2000, 4) + sage: v == [moebius(n) for n in range(-1000,2000, 4)] + True + """ + if stop is None: + start, stop = 1, int(start) + else: + start = int(start) + stop = int(stop) + if step is None: + step = 1 + else: + step = int(step) + + if start <= 0 and 0 < stop and start % step == 0: + return self.range(start, 0, step) + [ZZ.zero()] +\ + self.range(step, stop, step) + + if step == 1: + v = pari('vector(%s, i, moebius(i-1+%s))'%( + stop-start, start)) + else: + n = len(range(start, stop, step)) # stupid + v = pari('vector(%s, i, moebius(%s*(i-1) + %s))'%( + n, step, start)) + return [Integer(x) for x in v] + +moebius = Moebius() + + +## Note: farey, convergent, continued_fraction_list and convergents have been moved to +## sage.rings.continued_fraction + +def continuant(v, n=None): + r""" + Function returns the continuant of the sequence `v` (list + or tuple). + + Definition: see Graham, Knuth and Patashnik, *Concrete Mathematics*, + section 6.7: Continuants. The continuant is defined by + + - `K_0() = 1` + - `K_1(x_1) = x_1` + - `K_n(x_1, \cdots, x_n) = K_{n-1}(x_n, \cdots x_{n-1})x_n + K_{n-2}(x_1, \cdots, x_{n-2})` + + If ``n = None`` or ``n > len(v)`` the default + ``n = len(v)`` is used. + + INPUT: + + - ``v`` - list or tuple of elements of a ring + - ``n`` - optional integer + + OUTPUT: element of ring (integer, polynomial, etcetera). + + EXAMPLES:: + + sage: continuant([1,2,3]) + 10 + sage: p = continuant([2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]) + sage: q = continuant([1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]) + sage: p/q + 517656/190435 + sage: continued_fraction([2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]).convergent(14) + 517656/190435 + sage: x = PolynomialRing(RationalField(),'x',5).gens() + sage: continuant(x) + x0*x1*x2*x3*x4 + x0*x1*x2 + x0*x1*x4 + x0*x3*x4 + x2*x3*x4 + x0 + x2 + x4 + sage: continuant(x, 3) + x0*x1*x2 + x0 + x2 + sage: continuant(x,2) + x0*x1 + 1 + + We verify the identity + + .. math:: + + K_n(z,z,\cdots,z) = \sum_{k=0}^n \binom{n-k}{k} z^{n-2k} + + for `n = 6` using polynomial arithmetic:: + + sage: z = QQ['z'].0 + sage: continuant((z,z,z,z,z,z,z,z,z,z,z,z,z,z,z),6) + z^6 + 5*z^4 + 6*z^2 + 1 + + sage: continuant(9) + Traceback (most recent call last): + ... + TypeError: object of type 'sage.rings.integer.Integer' has no len() + + AUTHORS: + + - Jaap Spies (2007-02-06) + """ + m = len(v) + if n is None or m < n: + n = m + if n == 0: + return 1 + if n == 1: + return v[0] + a, b = 1, v[0] + for k in range(1,n): + a, b = b, a + b*v[k] + return b + +def number_of_divisors(n): + """ + Return the number of divisors of the integer n. + + INPUT: + + - ``n`` - a nonzero integer + + OUTPUT: + + - an integer, the number of divisors of n + + EXAMPLES:: + + sage: number_of_divisors(100) + 9 + sage: number_of_divisors(-720) + 30 + """ + m = ZZ(n) + if m.is_zero(): + raise ValueError("input must be nonzero") + return ZZ(pari(m).numdiv()) + + + +def hilbert_symbol(a, b, p, algorithm="pari"): + """ + Returns 1 if `ax^2 + by^2` `p`-adically represents + a nonzero square, otherwise returns `-1`. If either a or b + is 0, returns 0. + + INPUT: + + + - ``a, b`` - integers + + - ``p`` - integer; either prime or -1 (which + represents the archimedean place) + + - ``algorithm`` - string + + - ``'pari'`` - (default) use the PARI C library + + - ``'direct'`` - use a Python implementation + + - ``'all'`` - use both PARI and direct and check that + the results agree, then return the common answer + + + OUTPUT: integer (0, -1, or 1) + + EXAMPLES:: + + sage: hilbert_symbol (-1, -1, -1, algorithm='all') + -1 + sage: hilbert_symbol (2,3, 5, algorithm='all') + 1 + sage: hilbert_symbol (4, 3, 5, algorithm='all') + 1 + sage: hilbert_symbol (0, 3, 5, algorithm='all') + 0 + sage: hilbert_symbol (-1, -1, 2, algorithm='all') + -1 + sage: hilbert_symbol (1, -1, 2, algorithm='all') + 1 + sage: hilbert_symbol (3, -1, 2, algorithm='all') + -1 + + sage: hilbert_symbol(QQ(-1)/QQ(4), -1, 2) == -1 + True + sage: hilbert_symbol(QQ(-1)/QQ(4), -1, 3) == 1 + True + + AUTHORS: + + - William Stein and David Kohel (2006-01-05) + """ + p = ZZ(p) + if p != -1 and not p.is_prime(): + raise ValueError("p must be prime or -1") + a = QQ(a).numerator() * QQ(a).denominator() + b = QQ(b).numerator() * QQ(b).denominator() + + if algorithm == "pari": + if p == -1: + p = 0 + return ZZ(pari(a).hilbert(b,p)) + + elif algorithm == 'direct': + if a == 0 or b == 0: + return ZZ(0) + + p = ZZ(p) + one = ZZ(1) + + if p != -1: + p_sqr = p**2 + while a%p_sqr == 0: a //= p_sqr + while b%p_sqr == 0: b //= p_sqr + + if p != 2 and True in ( kronecker(x,p) == 1 for x in (a,b,a+b) ): + return one + if a%p == 0: + if b%p == 0: + return hilbert_symbol(p,-(b//p),p)*hilbert_symbol(a//p,b,p) + elif p == 2 and (b%4) == 3: + if kronecker(a+b,p) == -1: + return -one + elif kronecker(b,p) == -1: + return -one + elif b%p == 0: + if p == 2 and (a%4) == 3: + if kronecker(a+b,p) == -1: + return -one + elif kronecker(a,p) == -1: + return -one + elif p == 2 and (a%4) == 3 and (b%4) == 3: + return -one + return one + elif algorithm == 'all': + ans_pari = hilbert_symbol(a,b,p,algorithm='pari') + ans_direct = hilbert_symbol(a,b,p,algorithm='direct') + if ans_pari != ans_direct: + raise RuntimeError("There is a bug in hilbert_symbol; two ways of computing the Hilbert symbol (%s,%s)_%s disagree"%(a,b,p)) + return ans_pari + else: + raise ValueError("Algorithm %s not defined"%algorithm) + + +def hilbert_conductor(a, b): + """ + This is the product of all (finite) primes where the Hilbert symbol is -1. + What is the same, this is the (reduced) discriminant of the quaternion + algebra `(a,b)` over `\QQ`. + + INPUT: + + - ``a``, ``b`` -- integers + + OUTPUT: + + - squarefree positive integer + + EXAMPLES:: + + sage: hilbert_conductor(-1, -1) + 2 + sage: hilbert_conductor(-1, -11) + 11 + sage: hilbert_conductor(-2, -5) + 5 + sage: hilbert_conductor(-3, -17) + 17 + + AUTHOR: + + - Gonzalo Tornaria (2009-03-02) + """ + a, b = ZZ(a), ZZ(b) + d = ZZ(1) + for p in set().union([2], prime_divisors(a), prime_divisors(b)): + if hilbert_symbol(a, b, p) == -1: + d *= p + return d + +def hilbert_conductor_inverse(d): + """ + Finds a pair of integers `(a,b)` such that ``hilbert_conductor(a,b) == d``. + The quaternion algebra `(a,b)` over `\QQ` will then have (reduced) + discriminant `d`. + + INPUT: + + - ``d`` -- square-free positive integer + + OUTPUT: pair of integers + + EXAMPLES:: + + sage: hilbert_conductor_inverse(2) + (-1, -1) + sage: hilbert_conductor_inverse(3) + (-1, -3) + sage: hilbert_conductor_inverse(6) + (-1, 3) + sage: hilbert_conductor_inverse(30) + (-3, -10) + sage: hilbert_conductor_inverse(4) + Traceback (most recent call last): + ... + ValueError: d needs to be squarefree + sage: hilbert_conductor_inverse(-1) + Traceback (most recent call last): + ... + ValueError: d needs to be positive + + AUTHOR: + + - Gonzalo Tornaria (2009-03-02) + + TESTS:: + + sage: for i in xrange(100): + ....: d = ZZ.random_element(2**32).squarefree_part() + ....: if hilbert_conductor(*hilbert_conductor_inverse(d)) != d: + ....: print "hilbert_conductor_inverse failed for d =", d + """ + Z = ZZ + d = Z(d) + if d <= 0: + raise ValueError("d needs to be positive") + if d == 1: + return (Z(-1), Z(1)) + if d == 2: + return (Z(-1), Z(-1)) + if d.is_prime(): + if d%4 == 3: + return (Z(-1), -d) + if d%8 == 5: + return (Z(-2), -d) + q = 3 + while q%4 != 3 or kronecker_symbol(d,q) != -1: + q = next_prime(q) + return (Z(-q), -d) + else: + mo = moebius(d) + if mo == 0: + raise ValueError("d needs to be squarefree") + if d % 2 == 0 and mo*d % 16 != 2: + dd = mo * d / 2 + else: + dd = mo * d + q = 1 + while hilbert_conductor(-q, dd) != d: + q+=1; + if dd%q == 0: + dd /= q + return (Z(-q), Z(dd)) + + +############################################################################## +## falling and rising factorials +## By Jaap Spies +## +## Copyright (C) 2006 Jaap Spies +## Copyright (C) 2006 William Stein +## +## Distributed under the terms of the GNU General Public License (GPL) +## http://www.gnu.org/licenses/ +############################################################################## + + +def falling_factorial(x, a): + r""" + Returns the falling factorial `(x)_a`. + + The notation in the literature is a mess: often `(x)_a`, + but there are many other notations: GKP: Concrete Mathematics uses + `x^{\underline{a}}`. + + Definition: for integer `a \ge 0` we have + `x(x-1) \cdots (x-a+1)`. In all other cases we use the + GAMMA-function: `\frac {\Gamma(x+1)} {\Gamma(x-a+1)}`. + + INPUT: + + - ``x`` - element of a ring + + - ``a`` - a non-negative integer or + + OR + + - ``x and a`` - any numbers + + OUTPUT: the falling factorial + + EXAMPLES:: + + sage: falling_factorial(10, 3) + 720 + sage: falling_factorial(10, RR('3.0')) + 720.000000000000 + sage: falling_factorial(10, RR('3.3')) + 1310.11633396601 + sage: falling_factorial(10, 10) + 3628800 + sage: factorial(10) + 3628800 + sage: a = falling_factorial(1+I, I); a + gamma(I + 2) + sage: CC(a) + 0.652965496420167 + 0.343065839816545*I + sage: falling_factorial(1+I, 4) + 4*I + 2 + sage: falling_factorial(I, 4) + -10 + + :: + + sage: M = MatrixSpace(ZZ, 4, 4) + sage: A = M([1,0,1,0,1,0,1,0,1,0,10,10,1,0,1,1]) + sage: falling_factorial(A, 2) # A(A - I) + [ 1 0 10 10] + [ 1 0 10 10] + [ 20 0 101 100] + [ 2 0 11 10] + + :: + + sage: x = ZZ['x'].0 + sage: falling_factorial(x, 4) + x^4 - 6*x^3 + 11*x^2 - 6*x + + TESTS: + + Check that :trac:`14858` is fixed:: + + sage: falling_factorial(-4, SR(2)) + 20 + + Check that :trac:`16770` is fixed:: + + sage: d = var('d') + sage: type(falling_factorial(d, 0)) + + + AUTHORS: + + - Jaap Spies (2006-03-05) + """ + from sage.symbolic.expression import Expression + + if (isinstance(a, (Integer, int, long)) or + (isinstance(a, Expression) and + a.is_integer())) and a >= 0: + return prod(((x - i) for i in range(a)), z=x.parent().one()) + from sage.functions.all import gamma + return gamma(x+1) / gamma(x-a+1) + +def rising_factorial(x, a): + r""" + Returns the rising factorial `(x)^a`. + + The notation in the literature is a mess: often `(x)^a`, + but there are many other notations: GKP: Concrete Mathematics uses + `x^{\overline{a}}`. + + The rising factorial is also known as the Pochhammer symbol, see + Maple and Mathematica. + + Definition: for integer `a \ge 0` we have + `x(x+1) \cdots (x+a-1)`. In all other cases we use the + GAMMA-function: `\frac {\Gamma(x+a)} {\Gamma(x)}`. + + INPUT: + + + - ``x`` - element of a ring + + - ``a`` - a non-negative integer or + + - ``x and a`` - any numbers + + + OUTPUT: the rising factorial + + EXAMPLES:: + + sage: rising_factorial(10,3) + 1320 + + :: + + sage: rising_factorial(10,RR('3.0')) + 1320.00000000000 + + :: + + sage: rising_factorial(10,RR('3.3')) + 2826.38895824964 + + :: + + sage: a = rising_factorial(1+I, I); a + gamma(2*I + 1)/gamma(I + 1) + sage: CC(a) + 0.266816390637832 + 0.122783354006372*I + + :: + + sage: a = rising_factorial(I, 4); a + -10 + + See falling_factorial(I, 4). + + :: + + sage: x = polygen(ZZ) + sage: rising_factorial(x, 4) + x^4 + 6*x^3 + 11*x^2 + 6*x + + TESTS: + + Check that :trac:`14858` is fixed:: + + sage: bool(rising_factorial(-4, 2) == + ....: rising_factorial(-4, SR(2)) == + ....: rising_factorial(SR(-4), SR(2))) + True + + Check that :trac:`16770` is fixed:: + + sage: d = var('d') + sage: type(rising_factorial(d, 0)) + + + AUTHORS: + + - Jaap Spies (2006-03-05) + """ + from sage.symbolic.expression import Expression + + if (isinstance(a, (Integer, int, long)) or + (isinstance(a, Expression) and + a.is_integer())) and a >= 0: + return prod(((x + i) for i in range(a)), z=x.parent().one()) + from sage.functions.all import gamma + return gamma(x+a) / gamma(x) + + +def integer_ceil(x): + """ + Return the ceiling of x. + + EXAMPLES:: + + sage: integer_ceil(5.4) + 6 + sage: integer_ceil(x) + Traceback (most recent call last): + ... + NotImplementedError: computation of ceil of x not implemented + """ + try: + return ZZ(x.ceil()) + except AttributeError: + try: + return ZZ(math.ceil(float(x))) + except TypeError: + pass + raise NotImplementedError("computation of ceil of %s not implemented"%x) + +def integer_floor(x): + r""" + Return the largest integer `\leq x`. + + INPUT: + + - ``x`` - an object that has a floor method or is + coercible to int + + OUTPUT: an Integer + + EXAMPLES:: + + sage: integer_floor(5.4) + 5 + sage: integer_floor(float(5.4)) + 5 + sage: integer_floor(-5/2) + -3 + sage: integer_floor(RDF(-5/2)) + -3 + + sage: integer_floor(x) + Traceback (most recent call last): + ... + NotImplementedError: computation of floor of x not implemented + """ + try: + return ZZ(x.floor()) + except AttributeError: + try: + return ZZ(math.floor(float(x))) + except TypeError: + pass + raise NotImplementedError("computation of floor of %s not implemented"%x) + + +def two_squares(n): + """ + Write the integer `n` as a sum of two integer squares if possible; + otherwise raise a ``ValueError``. + + INPUT: + + - ``n`` -- an integer + + OUTPUT: a tuple `(a,b)` of non-negative integers such that + `n = a^2 + b^2` with `a <= b`. + + EXAMPLES:: + + sage: two_squares(389) + (10, 17) + sage: two_squares(21) + Traceback (most recent call last): + ... + ValueError: 21 is not a sum of 2 squares + sage: two_squares(21^2) + (0, 21) + sage: a,b = two_squares(100000000000000000129); a,b + (4418521500, 8970878873) + sage: a^2 + b^2 + 100000000000000000129 + sage: two_squares(2^222+1) + (253801659504708621991421712450521, 2583712713213354898490304645018692) + sage: two_squares(0) + (0, 0) + sage: two_squares(-1) + Traceback (most recent call last): + ... + ValueError: -1 is not a sum of 2 squares + + TESTS:: + + sage: for _ in xrange(100): + ....: a = ZZ.random_element(2**16, 2**20) + ....: b = ZZ.random_element(2**16, 2**20) + ....: n = a**2 + b**2 + ....: aa,bb = two_squares(n) + ....: assert aa**2 + bb**2 == n + + ALGORITHM: + + See http://www.schorn.ch/howto.html + """ + n = ZZ(n) + + if n <= 0: + if n == 0: + z = ZZ.zero() + return (z, z) + raise ValueError("%s is not a sum of 2 squares"%n) + + if n.nbits() <= 32: + from sage.rings import sum_of_squares + return sum_of_squares.two_squares_pyx(n) + + # Start by factoring n (which seems to be unavoidable) + F = n.factor(proof=False) + + # First check whether it is possible to write n as a sum of two + # squares: all prime powers p^e must have p = 2 or p = 1 mod 4 + # or e even. + for (p,e) in F: + if e % 2 == 1 and p % 4 == 3: + raise ValueError("%s is not a sum of 2 squares"%n) + + # We run over all factors of n, write each factor p^e as + # a sum of 2 squares and accumulate the product + # (using multiplication in Z[I]) in a^2 + b^2. + from sage.rings.finite_rings.integer_mod import Mod + a = ZZ.one() + b = ZZ.zero() + for (p,e) in F: + if e >= 2: + m = p ** (e//2) + a *= m + b *= m + if e % 2 == 1: + if p == 2: + # (a + bi) *= (1 + I) + a,b = a - b, a + b + else: # p = 1 mod 4 + # Find a square root of -1 mod p. + # If y is a non-square, then y^((p-1)/4) is a square root of -1. + y = Mod(2,p) + while True: + s = y**((p-1)/4) + if not s*s + 1: + s = s.lift() + break + y += 1 + # Apply Cornacchia's algorithm to write p as r^2 + s^2. + r = p + while s*s > p: + r,s = s, r % s + r %= s + + # Multiply (a + bI) by (r + sI) + a,b = a*r - b*s, b*r + a*s + + a = a.abs() + b = b.abs() + assert a*a + b*b == n + if a <= b: + return (a,b) + else: + return (b,a) + +def three_squares(n): + """ + Write the integer `n` as a sum of three integer squares if possible; + otherwise raise a ``ValueError``. + + INPUT: + + - ``n`` -- an integer + + OUTPUT: a tuple `(a,b,c)` of non-negative integers such that + `n = a^2 + b^2 + c^2` with `a <= b <= c`. + + EXAMPLES:: + + sage: three_squares(389) + (1, 8, 18) + sage: three_squares(946) + (9, 9, 28) + sage: three_squares(2986) + (3, 24, 49) + sage: three_squares(7^100) + (0, 0, 1798465042647412146620280340569649349251249) + sage: three_squares(11^111-1) + (616274160655975340150706442680, 901582938385735143295060746161, 6270382387635744140394001363065311967964099981788593947233) + sage: three_squares(7 * 2^41) + (1048576, 2097152, 3145728) + sage: three_squares(7 * 2^42) + Traceback (most recent call last): + ... + ValueError: 30786325577728 is not a sum of 3 squares + sage: three_squares(0) + (0, 0, 0) + sage: three_squares(-1) + Traceback (most recent call last): + ... + ValueError: -1 is not a sum of 3 squares + + TESTS:: + + sage: for _ in xrange(100): + ....: a = ZZ.random_element(2**16, 2**20) + ....: b = ZZ.random_element(2**16, 2**20) + ....: c = ZZ.random_element(2**16, 2**20) + ....: n = a**2 + b**2 + c**2 + ....: aa,bb,cc = three_squares(n) + ....: assert aa**2 + bb**2 + cc**2 == n + + ALGORITHM: + + See http://www.schorn.ch/howto.html + """ + n = ZZ(n) + + if n <= 0: + if n == 0: + z = ZZ.zero() + return (z, z, z) + raise ValueError("%s is not a sum of 3 squares"%n) + + if n.nbits() <= 32: + from sage.rings import sum_of_squares + return sum_of_squares.three_squares_pyx(n) + + # First, remove all factors 4 from n + e = n.valuation(2)//2 + m = ZZ.one() << e + N = n >> (2*e) + + # Let x be the largest integer at most sqrt(N) + x, r = N.sqrtrem() + # We need to check for this special case, + # otherwise N - x^2 will always factor. + if not r: + z = ZZ.zero() + return (z, z, x*m) + + # Consider different cases to find an x such that N - x^2 is easily + # written as the sum of 2 squares, because it is either p or 2p, + # with p a prime which is 1 mod 4. + if N % 4 == 1: + # Write N = x^2 + p with x even, p = 1 mod 4 prime + if x % 2 == 1: + x -= 1 + while x >= 0: + p = N - x*x + if p.is_pseudoprime(): + break + x -= 2 + elif N % 4 == 2: + # Write N = x^2 + p with x odd, p = 1 mod 4 prime + if x % 2 == 0: + x -= 1 + while x >= 0: + p = N - x*x + if p.is_pseudoprime(): + break + x -= 2 + elif N % 8 == 3: + # Write N = x^2 + 2p with x odd, p = 1 mod 4 prime + if x % 2 == 0: + x -= 1 + while x >= 0: + p = (N - x*x) >> 1 + if p.is_pseudoprime(): + break + x -= 2 + else: # 7 mod 8 + raise ValueError("%s is not a sum of 3 squares"%n) + + if x < 0: + # We found no good x, brute force instead. + # Normally, this should only happen for small values of N. + if N > 10000: + from warnings import warn + warn("Brute forcing sum of 3 squares for large N = %s"%N, RuntimeWarning) + x = N.isqrt() + + # In the usual case, this loop will only be executed once, since + # we already know the "right" value of x. + # This will only really loop if we hit the "x < 0" case above. + while True: + try: + a,b = two_squares(N - x*x) + break + except ValueError: + x -= 1 + assert x >= 0 + + if x >= b: + return (a*m, b*m, x*m) + elif x >= a: + return (a*m, x*m, b*m) + else: + return (x*m, a*m, b*m) + +def four_squares(n): + """ + Write the integer `n` as a sum of four integer squares. + + INPUT: + + - ``n`` -- an integer + + OUTPUT: a tuple `(a,b,c,d)` of non-negative integers such that + `n = a^2 + b^2 + c^2 + d^2` with `a <= b <= c <= d`. + + EXAMPLES:: + + sage: four_squares(3) + (0, 1, 1, 1) + sage: four_squares(13) + (0, 0, 2, 3) + sage: four_squares(130) + (0, 0, 3, 11) + sage: four_squares(1101011011004) + (90, 102, 1220, 1049290) + sage: four_squares(10^100-1) + (155024616290, 2612183768627, 14142135623730950488016887, 99999999999999999999999999999999999999999999999999) + sage: for i in range(2^129, 2^129+10000): # long time + ....: S = four_squares(i) + ....: assert sum(x^2 for x in S) == i + + TESTS:: + + sage: for _ in xrange(100): + ....: n = ZZ.random_element(2**32,2**34) + ....: aa,bb,cc,dd = four_squares(n) + ....: assert aa**2 + bb**2 + cc**2 + dd**2 == n + """ + n = ZZ(n) + + if n <= 0: + if n == 0: + z = ZZ.zero() + return (z, z, z, z) + raise ValueError("%s is not a sum of 4 squares"%n) + + if n.nbits() <= 32: + from sage.rings import sum_of_squares + return sum_of_squares.four_squares_pyx(n) + + # First, remove all factors 4 from n + e = n.valuation(2) // 2 + m = ZZ.one() << e + N = n >> (2*e) + + # Subtract a suitable x^2 such that N - x^2 is 1,2,3,5,6 mod 8, + # which can then be written as a sum of 3 squares. + x = N.isqrt() + y = N - x*x + if y >= 7 and (y % 4 == 0 or y % 8 == 7): + x -= 1 + y += 2*x + 1 + + a,b,c = three_squares(y) + + # Correct sorting is guaranteed by construction + return (a*m, b*m, c*m, x*m) + +def sum_of_k_squares(k,n): + """ + Write the integer `n` as a sum of `k` integer squares if possible; + otherwise raise a ``ValueError``. + + INPUT: + + - ``k`` -- a non-negative integer + + - ``n`` -- an integer + + OUTPUT: a tuple `(x_1, ..., x_k)` of non-negative integers such that + their squares sum to `n`. + + EXAMPLES:: + + sage: sum_of_k_squares(2, 9634) + (15, 97) + sage: sum_of_k_squares(3, 9634) + (0, 15, 97) + sage: sum_of_k_squares(4, 9634) + (1, 2, 5, 98) + sage: sum_of_k_squares(5, 9634) + (0, 1, 2, 5, 98) + sage: sum_of_k_squares(6, 11^1111-1) + (19215400822645944253860920437586326284, 37204645194585992174252915693267578306, 3473654819477394665857484221256136567800161086815834297092488779216863122, 5860191799617673633547572610351797996721850737768032876360978911074629287841061578270832330322236796556721252602860754789786937515870682024273948, 20457423294558182494001919812379023992538802203730791019728543439765347851316366537094696896669915675685581905102118246887673397020172285247862426612188418787649371716686651256443143210952163970564228423098202682066311189439731080552623884051737264415984619097656479060977602722566383385989, 311628095411678159849237738619458396497534696043580912225334269371611836910345930320700816649653412141574887113710604828156159177769285115652741014638785285820578943010943846225597311231847997461959204894255074229895666356909071243390280307709880906261008237873840245959883405303580405277298513108957483306488193844321589356441983980532251051786704380984788999660195252373574924026139168936921591652831237741973242604363696352878914129671292072201700073286987126265965322808664802662993006926302359371379531571194266134916767573373504566621665949840469229781956838744551367172353) + sage: sum_of_k_squares(7, 0) + (0, 0, 0, 0, 0, 0, 0) + sage: sum_of_k_squares(30,999999) + (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7, 44, 999) + sage: sum_of_k_squares(1, 9) + (3,) + sage: sum_of_k_squares(1, 10) + Traceback (most recent call last): + ... + ValueError: 10 is not a sum of 1 square + sage: sum_of_k_squares(1, -10) + Traceback (most recent call last): + ... + ValueError: -10 is not a sum of 1 square + sage: sum_of_k_squares(0, 9) + Traceback (most recent call last): + ... + ValueError: 9 is not a sum of 0 squares + sage: sum_of_k_squares(0, 0) + () + sage: sum_of_k_squares(7, -1) + Traceback (most recent call last): + ... + ValueError: -1 is not a sum of 7 squares + sage: sum_of_k_squares(-1, 0) + Traceback (most recent call last): + ... + ValueError: k = -1 must be non-negative + """ + n = ZZ(n) + k = int(k) + + if k <= 4: + if k == 4: + return four_squares(n) + if k == 3: + return three_squares(n) + if k == 2: + return two_squares(n) + if k == 1: + if n >= 0: + x, r = n.sqrtrem() + if not r: + return (x,) + raise ValueError("%s is not a sum of 1 square"%n) + if k == 0: + if n == 0: + return tuple() + raise ValueError("%s is not a sum of 0 squares"%n) + raise ValueError("k = %s must be non-negative"%k) + + if n < 0: + raise ValueError("%s is not a sum of %s squares"%(n,k)) + + # Recursively subtract the largest square + t = [] + while k > 4: + x = n.isqrt() + t.insert(0, x) + n -= x*x + k -= 1 + + t = list(four_squares(n)) + t + return tuple(t) + +def subfactorial(n): + r""" + Subfactorial or rencontres numbers, or derangements: number of + permutations of `n` elements with no fixed points. + + INPUT: + + + - ``n`` - non negative integer + + + OUTPUT: + + + - ``integer`` - function value + + + EXAMPLES:: + + sage: subfactorial(0) + 1 + sage: subfactorial(1) + 0 + sage: subfactorial(8) + 14833 + + AUTHORS: + + - Jaap Spies (2007-01-23) + """ + return factorial(n)*sum(((-1)**k)/factorial(k) for k in range(n+1)) + +def is_power_of_two(n): + r""" + This function returns True if and only if ``n`` is a power of + 2 + + INPUT: + + - ``n`` - integer + + OUTPUT: + + - ``True`` - if n is a power of 2 + + - ``False`` - if not + + EXAMPLES:: + + sage: is_power_of_two(1024) + True + sage: is_power_of_two(1) + True + sage: is_power_of_two(24) + False + sage: is_power_of_two(0) + False + sage: is_power_of_two(-4) + False + """ + return ZZ(n).popcount() == 1 + +def differences(lis, n=1): + """ + Returns the `n` successive differences of the elements in + `lis`. + + EXAMPLES:: + + sage: differences(prime_range(50)) + [1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4] + sage: differences([i^2 for i in range(1,11)]) + [3, 5, 7, 9, 11, 13, 15, 17, 19] + sage: differences([i^3 + 3*i for i in range(1,21)]) + [10, 22, 40, 64, 94, 130, 172, 220, 274, 334, 400, 472, 550, 634, 724, 820, 922, 1030, 1144] + sage: differences([i^3 - i^2 for i in range(1,21)], 2) + [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 100, 106, 112] + sage: differences([p - i^2 for i, p in enumerate(prime_range(50))], 3) + [-1, 2, -4, 4, -4, 4, 0, -6, 8, -6, 0, 4] + + AUTHORS: + + - Timothy Clemans (2008-03-09) + """ + n = ZZ(n) + if n < 1: + raise ValueError('n must be greater than 0') + lis = [lis[i + 1] - num for i, num in enumerate(lis[:-1])] + if n == 1: + return lis + return differences(lis, n - 1) + +def _cmp_complex_for_display(a, b): + r""" + Compare two complex numbers in a "pretty" (but mathematically + meaningless) fashion, for display only. + + Real numbers (with a zero imaginary part) come before complex numbers, + and are sorted. Complex numbers are sorted by their real part + unless their real parts are quite close, in which case they are + sorted by their imaginary part. + + EXAMPLES:: + + sage: import sage.arith.misc + sage: cmp_c = sage.arith.misc._cmp_complex_for_display + sage: teeny = 3e-11 + sage: cmp_c(CC(5), CC(3, 3)) + -1 + sage: cmp_c(CC(3), CC(5, 5)) + -1 + sage: cmp_c(CC(5), CC(3)) + 1 + sage: cmp_c(CC(teeny, -1), CC(-teeny, 1)) + -1 + sage: cmp_c(CC(teeny, 1), CC(-teeny, -1)) + 1 + sage: cmp_c(CC(0, 1), CC(1, 0.5)) + -1 + sage: cmp_c(CC(3+teeny, -1), CC(3-teeny, 1)) + -1 + sage: CIF200 = ComplexIntervalField(200) + sage: cmp_c(CIF200(teeny, -1), CIF200(-teeny, 1)) + -1 + sage: cmp_c(CIF200(teeny, 1), CIF200(-teeny, -1)) + 1 + sage: cmp_c(CIF200(0, 1), CIF200(1, 0.5)) + -1 + sage: cmp_c(CIF200(3+teeny, -1), CIF200(3-teeny, 1)) + -1 + """ + ar = a.real(); br = b.real() + ai = a.imag(); bi = b.imag() + epsilon = ar.parent()(1e-10) + if ai: + if bi: + if abs(br) < epsilon: + if abs(ar) < epsilon: + return cmp(ai, bi) + return cmp(ar, 0) + if abs((ar - br) / br) < epsilon: + return cmp(ai, bi) + return cmp(ar, br) + else: + return 1 + else: + if bi: + return -1 + else: + return cmp(ar, br) + +def sort_complex_numbers_for_display(nums): + r""" + Given a list of complex numbers (or a list of tuples, where the + first element of each tuple is a complex number), we sort the list + in a "pretty" order. First come the real numbers (with zero + imaginary part), then the complex numbers sorted according to + their real part. If two complex numbers have a real part which is + sufficiently close, then they are sorted according to their + imaginary part. + + This is not a useful function mathematically (not least because + there's no principled way to determine whether the real components + should be treated as equal or not). It is called by various + polynomial root-finders; its purpose is to make doctest printing + more reproducible. + + We deliberately choose a cumbersome name for this function to + discourage use, since it is mathematically meaningless. + + EXAMPLES:: + + sage: import sage.arith.misc + sage: sort_c = sort_complex_numbers_for_display + sage: nums = [CDF(i) for i in range(3)] + sage: for i in range(3): + ....: nums.append(CDF(i + RDF.random_element(-3e-11, 3e-11), + ....: RDF.random_element())) + ....: nums.append(CDF(i + RDF.random_element(-3e-11, 3e-11), + ....: RDF.random_element())) + sage: shuffle(nums) + sage: sort_c(nums) + [0.0, 1.0, 2.0, -2.862406201002009e-11 - 0.7088740263015161*I, 2.2108362706985576e-11 - 0.43681052967509904*I, 1.0000000000138833 - 0.7587654737635712*I, 0.9999999999760288 - 0.7238965893336062*I, 1.9999999999874383 - 0.4560801012073723*I, 1.9999999999869107 + 0.6090836283134269*I] + """ + if len(nums) == 0: + return nums + + if isinstance(nums[0], tuple): + return sorted(nums, cmp=_cmp_complex_for_display, key=lambda t: t[0]) + else: + return sorted(nums, cmp=_cmp_complex_for_display) + +def fundamental_discriminant(D): + r""" + Return the discriminant of the quadratic extension + `K=Q(\sqrt{D})`, i.e. an integer d congruent to either 0 or + 1, mod 4, and such that, at most, the only square dividing it is + 4. + + INPUT: + + - ``D`` - an integer + + OUTPUT: + + - an integer, the fundamental discriminant + + EXAMPLES:: + + sage: fundamental_discriminant(102) + 408 + sage: fundamental_discriminant(720) + 5 + sage: fundamental_discriminant(2) + 8 + + """ + D = ZZ(D) + D = D.squarefree_part() + if D%4 == 1: + return D + return 4*D + +def squarefree_divisors(x): + """ + Iterator over the squarefree divisors (up to units) of the element x. + + Depends on the output of the prime_divisors function. + + INPUT: + + - x -- an element of any ring for which the prime_divisors + function works. + + EXAMPLES:: + + sage: list(squarefree_divisors(7)) + [1, 7] + sage: list(squarefree_divisors(6)) + [1, 2, 3, 6] + sage: list(squarefree_divisors(12)) + [1, 2, 3, 6] + + TESTS: + + Check that the first divisor (i.e. `1`) is a Sage integer (see + :trac:`17852`):: + + sage: a = next(squarefree_divisors(14)) + sage: a + 1 + sage: type(a) + + """ + for a in powerset(prime_divisors(x)): + yield prod(a, ZZ.one()) + +def dedekind_sum(p, q, algorithm='default'): + r""" + Return the Dedekind sum `s(p,q)` defined for integers `p`, `q` as + + .. MATH:: + + s(p,q) = \sum_{i=0}^{q-1} \left(\!\left(\frac{i}{q}\right)\!\right) + \left(\!\left(\frac{pi}{q}\right)\!\right) + + where + + .. MATH:: + + ((x))=\begin{cases} + x-\lfloor x \rfloor - \frac{1}{2} &\mbox{if } + x \in \QQ \setminus \ZZ \\ + 0 & \mbox{if } x \in \ZZ. + \end{cases} + + .. WARNING:: + + Caution is required as the Dedekind sum sometimes depends on the + algorithm or is left undefined when `p` and `q` are not coprime. + + INPUT: + + - ``p``, ``q`` -- integers + - ``algorithm`` -- must be one of the following + + - ``'default'`` - (default) use FLINT + - ``'flint'`` - use FLINT + - ``'pari'`` - use PARI (gives different results if `p` and `q` + are not coprime) + + OUTPUT: a rational number + + EXAMPLES: + + Several small values:: + + sage: for q in range(10): print [dedekind_sum(p,q) for p in range(q+1)] + [0] + [0, 0] + [0, 0, 0] + [0, 1/18, -1/18, 0] + [0, 1/8, 0, -1/8, 0] + [0, 1/5, 0, 0, -1/5, 0] + [0, 5/18, 1/18, 0, -1/18, -5/18, 0] + [0, 5/14, 1/14, -1/14, 1/14, -1/14, -5/14, 0] + [0, 7/16, 1/8, 1/16, 0, -1/16, -1/8, -7/16, 0] + [0, 14/27, 4/27, 1/18, -4/27, 4/27, -1/18, -4/27, -14/27, 0] + + Check relations for restricted arguments:: + + sage: q = 23; dedekind_sum(1, q); (q-1)*(q-2)/(12*q) + 77/46 + 77/46 + sage: p, q = 100, 723 # must be coprime + sage: dedekind_sum(p, q) + dedekind_sum(q, p) + 31583/86760 + sage: -1/4 + (p/q + q/p + 1/(p*q))/12 + 31583/86760 + + We check that evaluation works with large input:: + + sage: dedekind_sum(3^54 - 1, 2^93 + 1) + 459340694971839990630374299870/29710560942849126597578981379 + sage: dedekind_sum(3^54 - 1, 2^93 + 1, algorithm='pari') + 459340694971839990630374299870/29710560942849126597578981379 + + We check consistency of the results:: + + sage: dedekind_sum(5, 7, algorithm='default') + -1/14 + sage: dedekind_sum(5, 7, algorithm='flint') + -1/14 + sage: dedekind_sum(5, 7, algorithm='pari') + -1/14 + sage: dedekind_sum(6, 8, algorithm='default') + -1/8 + sage: dedekind_sum(6, 8, algorithm='flint') + -1/8 + sage: dedekind_sum(6, 8, algorithm='pari') + -1/8 + + REFERENCES: + + .. [Apostol] T. Apostol, Modular functions and Dirichlet series + in number theory, Springer, 1997 (2nd ed), section 3.7--3.9. + + - :wikipedia:`Dedekind\_sum` + """ + if algorithm == 'default' or algorithm == 'flint': + return flint_arith.dedekind_sum(p, q) + + if algorithm == 'pari': + import sage.interfaces.gp + x = sage.interfaces.gp.gp('sumdedekind(%s,%s)' % (p, q)) + return Rational(x) + + raise ValueError('unknown algorithm') + diff --git a/src/sage/calculus/calculus.py b/src/sage/calculus/calculus.py index ac1040594be..535c85812ab 100644 --- a/src/sage/calculus/calculus.py +++ b/src/sage/calculus/calculus.py @@ -382,22 +382,32 @@ sage: map(lambda f:f[0].n(), _.coefficients()) # numerical coefficients to make comparison easier; Maple 12 gives same answer [2.6789385347..., -8.3905259853..., 26.662447494..., -80.683148377...] -Ensure that ticket #8582 is fixed:: +Ensure that :trac:`8582` is fixed:: sage: k = var("k") sage: sum(1/(1+k^2), k, -oo, oo) -1/2*I*psi(I + 1) + 1/2*I*psi(-I + 1) - 1/2*I*psi(I) + 1/2*I*psi(-I) -Ensure that ticket #8624 is fixed:: +Ensure that :trac:`8624` is fixed:: sage: integrate(abs(cos(x)) * sin(x), x, pi/2, pi) 1/2 sage: integrate(sqrt(cos(x)^2 + sin(x)^2), x, 0, 2*pi) 2*pi + +Check if maxima has redundant variables defined after initialization, +see :trac:`9538`:: + + sage: maxima = sage.interfaces.maxima.maxima + sage: maxima('f1') + f1 + sage: sage.calculus.calculus.maxima('f1') + f1 """ import re -from sage.rings.all import RR, Integer, CC, QQ, RealDoubleElement, algdep +from sage.arith.all import algdep +from sage.rings.all import RR, Integer, CC, QQ, RealDoubleElement from sage.rings.real_mpfr import create_RealNumber from sage.misc.latex import latex @@ -407,27 +417,13 @@ from sage.symbolic.expression import Expression from sage.symbolic.function import Function from sage.symbolic.function_factory import function_factory -from sage.symbolic.integration.integral import indefinite_integral, \ - definite_integral +from sage.symbolic.integration.integral import (indefinite_integral, + definite_integral) import sage.symbolic.pynac -""" -Check if maxima has redundant variables defined after initialization #9538:: - - sage: maxima = sage.interfaces.maxima.maxima - sage: maxima('f1') - f1 - sage: sage.calculus.calculus.maxima('f1') - f1 -""" from sage.misc.lazy_import import lazy_import lazy_import('sage.interfaces.maxima_lib','maxima') -# This is not the same instance of Maxima as the general purpose one -#from sage.interfaces.maxima import Maxima -#maxima = Maxima(init_code = ['display2d : false', 'domain : complex', -# 'keepfloat : true', 'load(to_poly_solver)', -# 'load(simplify_sum)'], -# script_subdirectory=None) + ######################################################## def symbolic_sum(expression, v, a, b, algorithm='maxima'): diff --git a/src/sage/categories/examples/sets_cat.py b/src/sage/categories/examples/sets_cat.py index 139d01f897b..60f7493c8a3 100644 --- a/src/sage/categories/examples/sets_cat.py +++ b/src/sage/categories/examples/sets_cat.py @@ -13,7 +13,7 @@ from sage.categories.sets_cat import Sets from sage.rings.integer import Integer, IntegerWrapper from sage.rings.integer_ring import IntegerRing -from sage.rings.arith import is_prime +from sage.arith.all import is_prime from sage.structure.unique_representation import UniqueRepresentation diff --git a/src/sage/coding/binary_code.pyx b/src/sage/coding/binary_code.pyx index 0ed279b51fb..7cb979bd6b3 100644 --- a/src/sage/coding/binary_code.pyx +++ b/src/sage/coding/binary_code.pyx @@ -4183,7 +4183,7 @@ cdef class BinaryCodeClassifier: # print 'm:' # print m m_aut_gp_gens, m_labeling, m_size, m_base = self._aut_gp_and_can_label(m) - from sage.rings.arith import factorial + from sage.arith.all import factorial if True:#size*factorial(n-B.ncols) == m_size: # print 'in if' # print 'm_aut_gp_gens:', m_aut_gp_gens diff --git a/src/sage/coding/code_bounds.py b/src/sage/coding/code_bounds.py index 0a9a16e3335..6ba3b08b2f0 100644 --- a/src/sage/coding/code_bounds.py +++ b/src/sage/coding/code_bounds.py @@ -188,7 +188,7 @@ from sage.interfaces.all import gap from sage.rings.all import QQ, RR, ZZ, RDF -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.functions.all import log, sqrt from sage.misc.decorators import rename_keyword from delsarte_bounds import delsarte_bound_hamming_space, \ diff --git a/src/sage/coding/code_constructions.py b/src/sage/coding/code_constructions.py index 46704110993..7d44067ee47 100644 --- a/src/sage/coding/code_constructions.py +++ b/src/sage/coding/code_constructions.py @@ -139,11 +139,16 @@ --------- """ -############################################################################ -## Copyright David Joyner, 2007. wdjoyner@gmail.com. -## This is released under the GPL, version 2 or later (www.fsf.org). -############################################################################# +#***************************************************************************** +# Copyright (C) 2007 David Joyner +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.matrix.matrix_space import MatrixSpace from sage.matrix.constructor import matrix @@ -154,7 +159,7 @@ from sage.modules.free_module import span from sage.schemes.projective.projective_space import ProjectiveSpace from sage.structure.sequence import Sequence, Sequence_generic -from sage.rings.arith import GCD,LCM,divisors,quadratic_residues +from sage.arith.all import GCD, LCM, divisors, quadratic_residues from sage.rings.finite_rings.integer_mod_ring import IntegerModRing from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.integer import Integer diff --git a/src/sage/coding/delsarte_bounds.py b/src/sage/coding/delsarte_bounds.py index d3148b8492e..bfc864a6ab1 100644 --- a/src/sage/coding/delsarte_bounds.py +++ b/src/sage/coding/delsarte_bounds.py @@ -69,7 +69,7 @@ def Krawtchouk(n,q,l,x,check=True): ... TypeError: no conversion of this rational to integer """ - from sage.rings.arith import binomial + from sage.arith.all import binomial from sage.misc.misc import srange # Use the expression in equation (55) of MacWilliams & Sloane, pg 151 # We write jth term = some_factor * (j-1)th term diff --git a/src/sage/coding/linear_code.py b/src/sage/coding/linear_code.py index b449116992d..2cd126d3b04 100644 --- a/src/sage/coding/linear_code.py +++ b/src/sage/coding/linear_code.py @@ -222,7 +222,7 @@ from sage.matrix.matrix_space import MatrixSpace from sage.matrix.constructor import Matrix from sage.modules.free_module_element import vector -from sage.rings.arith import GCD, rising_factorial, binomial +from sage.arith.all import GCD, rising_factorial, binomial from sage.groups.all import SymmetricGroup from sage.misc.all import prod from sage.misc.functional import log, is_even diff --git a/src/sage/combinat/affine_permutation.py b/src/sage/combinat/affine_permutation.py index b9ad2739432..61dd29a2226 100644 --- a/src/sage/combinat/affine_permutation.py +++ b/src/sage/combinat/affine_permutation.py @@ -22,7 +22,7 @@ from sage.structure.parent import Parent from sage.groups.perm_gps.permgroup_named import SymmetricGroup -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.combinat.root_system.cartan_type import CartanType from sage.combinat.root_system.weyl_group import WeylGroup from sage.combinat.composition import Composition diff --git a/src/sage/combinat/alternating_sign_matrix.py b/src/sage/combinat/alternating_sign_matrix.py index c973d6f2dcb..040b97d6ce7 100644 --- a/src/sage/combinat/alternating_sign_matrix.py +++ b/src/sage/combinat/alternating_sign_matrix.py @@ -42,7 +42,7 @@ from sage.matrix.constructor import matrix from sage.misc.all import cached_method from sage.rings.all import ZZ -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.rings.integer import Integer from sage.combinat.posets.lattices import LatticePoset from sage.combinat.gelfand_tsetlin_patterns import GelfandTsetlinPatternsTopRow diff --git a/src/sage/combinat/baxter_permutations.py b/src/sage/combinat/baxter_permutations.py index 4a0c5a1f9af..96c927e90de 100644 --- a/src/sage/combinat/baxter_permutations.py +++ b/src/sage/combinat/baxter_permutations.py @@ -241,7 +241,7 @@ def cardinality(self): """ if self._n == 0: return 1 - from sage.rings.arith import binomial + from sage.arith.all import binomial return sum((binomial(self._n + 1, k) * binomial(self._n + 1, k + 1) * binomial(self._n + 1, k + 2)) // diff --git a/src/sage/combinat/binary_recurrence_sequences.py b/src/sage/combinat/binary_recurrence_sequences.py index 29e28b613a7..b82c52c9e8d 100644 --- a/src/sage/combinat/binary_recurrence_sequences.py +++ b/src/sage/combinat/binary_recurrence_sequences.py @@ -69,7 +69,7 @@ from sage.rings.finite_rings.integer_mod_ring import Integers from sage.rings.finite_rings.constructor import GF from sage.rings.integer import Integer -from sage.rings.arith import gcd, lcm, next_prime, is_prime, next_prime_power, legendre_symbol +from sage.arith.all import gcd, lcm, next_prime, is_prime, next_prime_power, legendre_symbol from sage.functions.log import log from sage.functions.other import sqrt diff --git a/src/sage/combinat/cluster_algebra_quiver/cluster_seed.py b/src/sage/combinat/cluster_algebra_quiver/cluster_seed.py index 766eab07e97..873db468f00 100644 --- a/src/sage/combinat/cluster_algebra_quiver/cluster_seed.py +++ b/src/sage/combinat/cluster_algebra_quiver/cluster_seed.py @@ -4043,7 +4043,7 @@ def _bino(n, k): 0 """ if n >= 0: - from sage.rings.arith import binomial + from sage.arith.all import binomial return binomial(n, k) else: return 0 diff --git a/src/sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py b/src/sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py index 649d7a9c93f..45626c6b596 100644 --- a/src/sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py +++ b/src/sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py @@ -21,7 +21,7 @@ from sage.misc.all import cached_method from sage.rings.all import ZZ, infinity from sage.graphs.all import Graph, DiGraph -from sage.rings.arith import binomial, Euler_Phi +from sage.arith.all import binomial, Euler_Phi from sage.all import prod from sage.matrix.all import matrix diff --git a/src/sage/combinat/combinat.py b/src/sage/combinat/combinat.py index 1aeb647acc3..403121dbe4e 100644 --- a/src/sage/combinat/combinat.py +++ b/src/sage/combinat/combinat.py @@ -57,7 +57,7 @@ **Implemented in other modules (listed for completeness):** -The ``sage.rings.arith`` module contains the following +The ``sage.arith.all`` module contains the following combinatorial functions: - binomial the binomial coefficient (wrapped from PARI) @@ -146,7 +146,7 @@ from sage.interfaces.all import maxima from sage.rings.all import ZZ, QQ, Integer, infinity -from sage.rings.arith import bernoulli, binomial +from sage.arith.all import bernoulli, binomial, factorial from sage.rings.polynomial.polynomial_element import Polynomial from sage.libs.all import pari from sage.misc.prandom import randint @@ -2685,7 +2685,6 @@ def bell_polynomial(n, k): """ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.combinat.partition import Partitions - from sage.rings.arith import factorial R = PolynomialRing(ZZ, 'x', n-k+1) vars = R.gens() result = R.zero() diff --git a/src/sage/combinat/combination.py b/src/sage/combinat/combination.py index 9b1680f2302..95fdaaafa35 100644 --- a/src/sage/combinat/combination.py +++ b/src/sage/combinat/combination.py @@ -25,7 +25,7 @@ from sage.interfaces.all import gap from sage.rings.all import ZZ, Integer -from sage.rings.arith import binomial +from sage.arith.all import binomial from combinat import CombinatorialClass from integer_vector import IntegerVectors from sage.misc.misc import uniq diff --git a/src/sage/combinat/composition_signed.py b/src/sage/combinat/composition_signed.py index eff46c83d51..e36c9f01abc 100644 --- a/src/sage/combinat/composition_signed.py +++ b/src/sage/combinat/composition_signed.py @@ -20,7 +20,7 @@ from composition import Compositions_n, Composition from sage.rings.all import Integer -from sage.rings.arith import binomial +from sage.arith.all import binomial import __builtin__ class SignedCompositions(Compositions_n): diff --git a/src/sage/combinat/cyclic_sieving_phenomenon.py b/src/sage/combinat/cyclic_sieving_phenomenon.py index 880035a4d1e..56c7af62a80 100644 --- a/src/sage/combinat/cyclic_sieving_phenomenon.py +++ b/src/sage/combinat/cyclic_sieving_phenomenon.py @@ -20,7 +20,7 @@ from copy import copy from sage.rings.all import ZZ, QQ -from sage.rings.arith import lcm +from sage.arith.all import lcm def CyclicSievingPolynomial( L, cyc_act=None, order=None, get_order=False): """ diff --git a/src/sage/combinat/designs/bibd.py b/src/sage/combinat/designs/bibd.py index 3f2edba17e9..99e14ad8651 100644 --- a/src/sage/combinat/designs/bibd.py +++ b/src/sage/combinat/designs/bibd.py @@ -54,7 +54,7 @@ from sage.misc.unknown import Unknown from design_catalog import transversal_design from block_design import BlockDesign -from sage.rings.arith import binomial, is_prime_power +from sage.arith.all import binomial, is_prime_power from group_divisible_designs import GroupDivisibleDesign from designs_pyx import is_pairwise_balanced_design diff --git a/src/sage/combinat/designs/block_design.py b/src/sage/combinat/designs/block_design.py index 523d25953de..51d0dc3171d 100644 --- a/src/sage/combinat/designs/block_design.py +++ b/src/sage/combinat/designs/block_design.py @@ -42,23 +42,22 @@ Functions and methods --------------------- """ -#*************************************************************************** -# Copyright (C) 2007 # -# # -# Peter Dobcsanyi and David Joyner # -# # -# # -# # -# Distributed under the terms of the GNU General Public License (GPL) # -# as published by the Free Software Foundation; either version 2 of # -# the License, or (at your option) any later version. # -# http://www.gnu.org/licenses/ # -#*************************************************************************** + +#***************************************************************************** +# Copyright (C) 2007 Peter Dobcsanyi +# Copyright (C) 2007 David Joyner +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.modules.free_module import VectorSpace from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ -from sage.rings.arith import binomial, integer_floor +from sage.arith.all import binomial, integer_floor, is_prime_power from incidence_structures import IncidenceStructure from sage.misc.decorators import rename_keyword from sage.rings.finite_rings.constructor import FiniteField @@ -144,7 +143,7 @@ def are_hyperplanes_in_projective_geometry_parameters(v, k, lmbda, return_parame ....: assert are_hyperplanes_in_projective_geometry_parameters(v,k,l+1) is False ....: assert are_hyperplanes_in_projective_geometry_parameters(v,k,l-1) is False """ - import sage.rings.arith as arith + import sage.arith.all as arith q1 = Integer(v - k) q2 = Integer(k - lmbda) @@ -703,7 +702,6 @@ def projective_plane(n, check=True, existence=False): sage: designs.projective_plane(12, existence=True) Unknown """ - from sage.rings.arith import is_prime_power from sage.rings.sum_of_squares import is_sum_of_two_squares_pyx if n <= 1: diff --git a/src/sage/combinat/designs/covering_design.py b/src/sage/combinat/designs/covering_design.py index b0dff359f88..8249b3ebe2f 100644 --- a/src/sage/combinat/designs/covering_design.py +++ b/src/sage/combinat/designs/covering_design.py @@ -46,7 +46,7 @@ from sage.misc.sage_eval import sage_eval from sage.structure.sage_object import SageObject from sage.rings.rational import Rational -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.combinat.combination import Combinations from sage.combinat.designs.incidence_structures import IncidenceStructure diff --git a/src/sage/combinat/designs/difference_family.py b/src/sage/combinat/designs/difference_family.py index 637dd579279..52fd4ffdd64 100644 --- a/src/sage/combinat/designs/difference_family.py +++ b/src/sage/combinat/designs/difference_family.py @@ -43,7 +43,7 @@ #***************************************************************************** from sage.categories.sets_cat import EmptySetError -import sage.rings.arith as arith +import sage.arith.all as arith from sage.misc.unknown import Unknown from sage.rings.integer import Integer diff --git a/src/sage/combinat/designs/difference_matrices.py b/src/sage/combinat/designs/difference_matrices.py index 255a68c4050..6a25fb5f11c 100644 --- a/src/sage/combinat/designs/difference_matrices.py +++ b/src/sage/combinat/designs/difference_matrices.py @@ -13,7 +13,7 @@ from sage.misc.cachefunc import cached_function from sage.categories.sets_cat import EmptySetError from sage.rings.finite_rings.constructor import FiniteField -from sage.rings.arith import is_prime_power, divisors +from sage.arith.all import is_prime_power, divisors from designs_pyx import is_difference_matrix from database import DM as DM_constructions diff --git a/src/sage/combinat/designs/group_divisible_designs.py b/src/sage/combinat/designs/group_divisible_designs.py index 0cd68793040..1a9859adf71 100644 --- a/src/sage/combinat/designs/group_divisible_designs.py +++ b/src/sage/combinat/designs/group_divisible_designs.py @@ -22,7 +22,16 @@ Functions --------- """ -from sage.rings.arith import is_prime_power + +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** + +from sage.arith.all import is_prime_power from sage.misc.unknown import Unknown from incidence_structures import IncidenceStructure diff --git a/src/sage/combinat/designs/incidence_structures.py b/src/sage/combinat/designs/incidence_structures.py index c5e07ffc1c0..f83dd30aa93 100644 --- a/src/sage/combinat/designs/incidence_structures.py +++ b/src/sage/combinat/designs/incidence_structures.py @@ -1535,7 +1535,7 @@ def is_t_design(self, t=None, v=None, k=None, l=None, return_parameters=False): sage: I.is_t_design(return_parameters=True) (False, (0, 0, 0, 0)) """ - from sage.rings.arith import binomial + from sage.arith.all import binomial # Missing parameters ? if v is None: diff --git a/src/sage/combinat/designs/latin_squares.py b/src/sage/combinat/designs/latin_squares.py index 0364ce14509..748b331b507 100644 --- a/src/sage/combinat/designs/latin_squares.py +++ b/src/sage/combinat/designs/latin_squares.py @@ -347,7 +347,7 @@ def mutually_orthogonal_latin_squares(k,n, partitions = False, check = True, exi """ from sage.combinat.designs.orthogonal_arrays import orthogonal_array from sage.matrix.constructor import Matrix - from sage.rings.arith import factor + from sage.arith.all import factor from database import MOLS_constructions # Is k is None we find the largest available diff --git a/src/sage/combinat/designs/orthogonal_arrays_build_recursive.py b/src/sage/combinat/designs/orthogonal_arrays_build_recursive.py index fc75fb6d961..f95bb553b62 100644 --- a/src/sage/combinat/designs/orthogonal_arrays_build_recursive.py +++ b/src/sage/combinat/designs/orthogonal_arrays_build_recursive.py @@ -353,7 +353,7 @@ def OA_and_oval(q): sage: _ = OA_and_oval """ - from sage.rings.arith import is_prime_power + from sage.arith.all import is_prime_power from sage.combinat.designs.block_design import projective_plane from orthogonal_arrays import OA_relabel @@ -670,7 +670,7 @@ def thwart_lemma_3_5(k,n,m,a,b,c,d=0,complement=False,explain_construction=False Charles J.Colbourn, Jeffrey H. Dinitz, Mieczyslaw Wojtas. Designs, Codes and Cryptography 5, no. 3 (1995): 189-197. """ - from sage.rings.arith import is_prime_power + from sage.arith.all import is_prime_power from sage.rings.finite_rings.constructor import FiniteField as GF if complement: @@ -785,7 +785,7 @@ def thwart_lemma_4_1(k,n,m,explain_construction=False): """ from sage.combinat.designs.designs_pyx import is_orthogonal_array from sage.rings.finite_rings.constructor import FiniteField - from sage.rings.arith import is_prime_power + from sage.arith.all import is_prime_power from block_design import DesarguesianProjectivePlaneDesign from itertools import chain @@ -1377,7 +1377,7 @@ def brouwer_separable_design(k,t,q,x,check=False,verbose=False,explain_construct from sage.combinat.designs.orthogonal_arrays import OA_from_PBD from difference_family import difference_family from orthogonal_arrays import incomplete_orthogonal_array - from sage.rings.arith import is_prime_power + from sage.arith.all import is_prime_power if explain_construction: return ("Brouwer's separable design construction with t={},q={},x={} from:\n"+ diff --git a/src/sage/combinat/designs/orthogonal_arrays_find_recursive.pyx b/src/sage/combinat/designs/orthogonal_arrays_find_recursive.pyx index 2d8102f4011..ecc1bfb70d9 100644 --- a/src/sage/combinat/designs/orthogonal_arrays_find_recursive.pyx +++ b/src/sage/combinat/designs/orthogonal_arrays_find_recursive.pyx @@ -47,7 +47,7 @@ Functions from sage.misc.cachefunc import cached_function from orthogonal_arrays import orthogonal_array from sage.rings.integer cimport Integer, smallInteger -from sage.rings.arith import prime_powers +from sage.arith.all import prime_powers @cached_function def find_recursive_construction(k, n): diff --git a/src/sage/combinat/designs/resolvable_bibd.py b/src/sage/combinat/designs/resolvable_bibd.py index 68f76856965..0ab02ce8cd6 100644 --- a/src/sage/combinat/designs/resolvable_bibd.py +++ b/src/sage/combinat/designs/resolvable_bibd.py @@ -47,7 +47,7 @@ Functions --------- """ -from sage.rings.arith import is_prime_power +from sage.arith.all import is_prime_power from sage.combinat.designs.bibd import BalancedIncompleteBlockDesign from sage.categories.sets_cat import EmptySetError from bibd import balanced_incomplete_block_design diff --git a/src/sage/combinat/dyck_word.py b/src/sage/combinat/dyck_word.py index 73e9ef56a2f..eafac50a1d9 100644 --- a/src/sage/combinat/dyck_word.py +++ b/src/sage/combinat/dyck_word.py @@ -1762,7 +1762,7 @@ def number_of_parking_functions(self): sage: DyckWord(area_sequence=[0,0,0]).number_of_parking_functions() 6 """ - from sage.rings.arith import multinomial + from sage.arith.all import multinomial return multinomial(list(self.rise_composition())) def list_parking_functions(self): @@ -3511,7 +3511,7 @@ def cardinality(self): ....: for p in range(7)) True """ - from sage.rings.arith import binomial + from sage.arith.all import binomial return (self.k1 - self.k2 + 1) * binomial(self.k1 + self.k2, self.k2) // (self.k1 + 1) ################################################################ diff --git a/src/sage/combinat/finite_state_machine.py b/src/sage/combinat/finite_state_machine.py index f6b87b36435..c95d19d4f1b 100644 --- a/src/sage/combinat/finite_state_machine.py +++ b/src/sage/combinat/finite_state_machine.py @@ -10046,7 +10046,7 @@ def number_of_words(self, variable=sage.symbolic.ring.SR.var('n'), """ from sage.matrix.constructor import matrix from sage.modules.free_module_element import vector - from sage.rings.arith import falling_factorial + from sage.arith.all import falling_factorial from sage.rings.integer_ring import ZZ from sage.symbolic.ring import SR diff --git a/src/sage/combinat/fully_packed_loop.py b/src/sage/combinat/fully_packed_loop.py index a2c3ad3d1da..686b805d1d3 100644 --- a/src/sage/combinat/fully_packed_loop.py +++ b/src/sage/combinat/fully_packed_loop.py @@ -14,7 +14,7 @@ from sage.matrix.constructor import matrix from sage.plot.line import line from sage.combinat.perfect_matching import PerfectMatching -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.rings.integer import Integer from sage.misc.all import prod from sage.misc.lazy_attribute import lazy_attribute diff --git a/src/sage/combinat/integer_list_old.py b/src/sage/combinat/integer_list_old.py index 54024eab27f..8b9f191b373 100644 --- a/src/sage/combinat/integer_list_old.py +++ b/src/sage/combinat/integer_list_old.py @@ -31,7 +31,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.integer_ring import ZZ from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets from sage.structure.parent import Parent diff --git a/src/sage/combinat/integer_vector.py b/src/sage/combinat/integer_vector.py index 6ca77961adc..fc52464cf88 100644 --- a/src/sage/combinat/integer_vector.py +++ b/src/sage/combinat/integer_vector.py @@ -33,7 +33,7 @@ from sage.categories.enumerated_sets import EnumeratedSets from sage.combinat.combinat import CombinatorialClass from sage.rings.integer import Integer -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.infinity import PlusInfinity import functools from sage.combinat.integer_lists import IntegerListsLex diff --git a/src/sage/combinat/interval_posets.py b/src/sage/combinat/interval_posets.py index dac88468819..d67af0aca55 100644 --- a/src/sage/combinat/interval_posets.py +++ b/src/sage/combinat/interval_posets.py @@ -2654,7 +2654,7 @@ def cardinality(self): sage: [TamariIntervalPosets(i).cardinality() for i in range(6)] [1, 1, 3, 13, 68, 399] """ - from sage.rings.arith import binomial + from sage.arith.all import binomial n = self._size if n == 0: return Integer(1) diff --git a/src/sage/combinat/lyndon_word.py b/src/sage/combinat/lyndon_word.py index edab7ab2f73..d59ae7d3092 100644 --- a/src/sage/combinat/lyndon_word.py +++ b/src/sage/combinat/lyndon_word.py @@ -2,18 +2,14 @@ """ Lyndon words """ + #***************************************************************************** -# Copyright (C) 2007 Mike Hansen , -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +# Copyright (C) 2007 Mike Hansen # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -21,8 +17,8 @@ from sage.structure.parent import Parent from sage.combinat.composition import Composition, Compositions -from sage.rings.all import divisors, gcd, moebius, Integer -from sage.rings.arith import factorial +from sage.rings.all import Integer +from sage.arith.all import factorial, divisors, gcd, moebius from sage.misc.all import prod import __builtin__ import necklace diff --git a/src/sage/combinat/matrices/hadamard_matrix.py b/src/sage/combinat/matrices/hadamard_matrix.py index f5505cfe3a6..f966e4e166a 100644 --- a/src/sage/combinat/matrices/hadamard_matrix.py +++ b/src/sage/combinat/matrices/hadamard_matrix.py @@ -45,12 +45,20 @@ .. [Hora] K. J. Horadam, Hadamard Matrices and Their Applications, Princeton University Press, 2006. """ -from sage.rings.arith import kronecker_symbol + +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** + from sage.rings.integer_ring import ZZ from sage.rings.integer import Integer from sage.matrix.constructor import matrix, block_matrix, block_diagonal_matrix, diagonal_matrix from urllib import urlopen -from sage.rings.arith import is_prime, is_square, is_prime_power, divisors +from sage.arith.all import is_prime, is_square, is_prime_power, divisors, kronecker_symbol from math import sqrt from sage.matrix.constructor import identity_matrix as I from sage.matrix.constructor import ones_matrix as J diff --git a/src/sage/combinat/matrices/latin.py b/src/sage/combinat/matrices/latin.py index 814e2c0fbe1..8bcab6bf107 100644 --- a/src/sage/combinat/matrices/latin.py +++ b/src/sage/combinat/matrices/latin.py @@ -139,7 +139,7 @@ from sage.combinat.permutation import Permutation from sage.interfaces.gap import gap from sage.groups.perm_gps.permgroup import PermutationGroup -from sage.rings.arith import is_prime +from sage.arith.all import is_prime from sage.rings.finite_rings.constructor import FiniteField from sage.misc.misc import uniq from sage.misc.flatten import flatten diff --git a/src/sage/combinat/multichoose_nk.py b/src/sage/combinat/multichoose_nk.py index 4c60ca24420..7de8cf504ce 100644 --- a/src/sage/combinat/multichoose_nk.py +++ b/src/sage/combinat/multichoose_nk.py @@ -16,7 +16,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** from combinat import CombinatorialClass -from sage.rings.arith import binomial +from sage.arith.all import binomial import sage.misc.prandom as rnd class MultichooseNK(CombinatorialClass): diff --git a/src/sage/combinat/ncsf_qsym/qsym.py b/src/sage/combinat/ncsf_qsym/qsym.py index 2434430c93e..7ffc73c8b85 100644 --- a/src/sage/combinat/ncsf_qsym/qsym.py +++ b/src/sage/combinat/ncsf_qsym/qsym.py @@ -3295,7 +3295,7 @@ def _precompute_M(self, n): M = self.realization_of().M() if l <= n: from sage.misc.cachefunc import cached_function - from sage.rings.arith import gcd + from sage.arith.all import gcd @cached_function def monolambda(I): # expansion of self[I] in monomial basis, diff --git a/src/sage/combinat/necklace.py b/src/sage/combinat/necklace.py index 83dab49045b..6c14196ae68 100644 --- a/src/sage/combinat/necklace.py +++ b/src/sage/combinat/necklace.py @@ -25,7 +25,7 @@ from sage.misc.lazy_attribute import lazy_attribute from sage.combinat.composition import Composition from sage.combinat.combinat import CombinatorialClass -from sage.rings.arith import euler_phi,factorial, divisors, gcd +from sage.arith.all import euler_phi,factorial, divisors, gcd from sage.rings.integer import Integer from sage.misc.all import prod from sage.combinat.misc import DoublyLinkedList diff --git a/src/sage/combinat/partition.py b/src/sage/combinat/partition.py index b9856497d37..21e62bf3809 100644 --- a/src/sage/combinat/partition.py +++ b/src/sage/combinat/partition.py @@ -298,8 +298,8 @@ from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets from sage.sets.non_negative_integers import NonNegativeIntegers -from sage.rings.all import QQ, ZZ, NN, gcd -from sage.rings.arith import factorial +from sage.rings.all import QQ, ZZ, NN +from sage.arith.all import factorial, gcd from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.integer import Integer from sage.rings.infinity import infinity diff --git a/src/sage/combinat/partition_algebra.py b/src/sage/combinat/partition_algebra.py index 93840de12c3..11a0e874d6b 100644 --- a/src/sage/combinat/partition_algebra.py +++ b/src/sage/combinat/partition_algebra.py @@ -21,7 +21,7 @@ from sage.combinat.set_partition import SetPartition, SetPartitions, SetPartitions_set from sage.sets.set import Set, is_Set from sage.graphs.graph import Graph -from sage.rings.arith import factorial, binomial +from sage.arith.all import factorial, binomial from permutation import Permutations from sage.rings.all import Integer from sage.rings.real_mpfr import is_RealNumber diff --git a/src/sage/combinat/permutation.py b/src/sage/combinat/permutation.py index 21f5adb432f..ecdca194fa7 100644 --- a/src/sage/combinat/permutation.py +++ b/src/sage/combinat/permutation.py @@ -237,7 +237,7 @@ from sage.interfaces.all import gap from sage.rings.all import ZZ, Integer, PolynomialRing -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.matrix.all import matrix from sage.combinat.tools import transitive_ideal from sage.combinat.composition import Composition diff --git a/src/sage/combinat/rigged_configurations/kleber_tree.py b/src/sage/combinat/rigged_configurations/kleber_tree.py index 031c85902b3..137b1cac183 100644 --- a/src/sage/combinat/rigged_configurations/kleber_tree.py +++ b/src/sage/combinat/rigged_configurations/kleber_tree.py @@ -70,7 +70,7 @@ from sage.misc.lazy_attribute import lazy_attribute from sage.misc.cachefunc import cached_method from sage.misc.latex import latex -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.integer import Integer from sage.structure.parent import Parent diff --git a/src/sage/combinat/root_system/cartan_matrix.py b/src/sage/combinat/root_system/cartan_matrix.py index 6d152d57376..3f287e2ece5 100644 --- a/src/sage/combinat/root_system/cartan_matrix.py +++ b/src/sage/combinat/root_system/cartan_matrix.py @@ -411,7 +411,7 @@ def symmetrizer(self): iset = self.index_set() # The result from is_symmetrizable needs to be scaled # to integer coefficients - from sage.rings.arith import LCM + from sage.arith.all import LCM from sage.rings.all import QQ scalar = LCM([QQ(x).denominator() for x in sym]) return Family( {iset[i]: ZZ(val*scalar) for i, val in enumerate(sym)} ) diff --git a/src/sage/combinat/root_system/pieri_factors.py b/src/sage/combinat/root_system/pieri_factors.py index 96dab17e5c0..bfffa519a74 100644 --- a/src/sage/combinat/root_system/pieri_factors.py +++ b/src/sage/combinat/root_system/pieri_factors.py @@ -19,7 +19,7 @@ from sage.rings.integer import Integer from sage.rings.rational_field import QQ from sage.rings.infinity import infinity -from sage.rings.arith import binomial +from sage.arith.all import binomial import sage.combinat.ranker from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet from sage.combinat.root_system.root_system import RootSystem diff --git a/src/sage/combinat/set_partition_ordered.py b/src/sage/combinat/set_partition_ordered.py index 07e05ffb64a..efd5fd0bc87 100644 --- a/src/sage/combinat/set_partition_ordered.py +++ b/src/sage/combinat/set_partition_ordered.py @@ -25,7 +25,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** -from sage.rings.arith import factorial +from sage.arith.all import factorial import sage.rings.integer from sage.sets.set import Set, is_Set from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets diff --git a/src/sage/combinat/sf/character.py b/src/sage/combinat/sf/character.py index e1c1b83ad4b..921319ea3e1 100644 --- a/src/sage/combinat/sf/character.py +++ b/src/sage/combinat/sf/character.py @@ -32,7 +32,7 @@ from sage.categories.homset import Hom from sage.categories.morphism import SetMorphism from sage.combinat.partition import Partition -from sage.rings.arith import divisors, moebius +from sage.arith.all import divisors, moebius from sage.functions.other import binomial from sage.rings.integer import Integer diff --git a/src/sage/combinat/sf/jack.py b/src/sage/combinat/sf/jack.py index 3adc8275944..389797a3de6 100644 --- a/src/sage/combinat/sf/jack.py +++ b/src/sage/combinat/sf/jack.py @@ -18,25 +18,22 @@ The Clarendon Press, Oxford University Press, New York, 1995, With contributions by A. Zelevinsky, Oxford Science Publications. """ + #***************************************************************************** # Copyright (C) 2007 Mike Hansen # 2012 Mike Zabrocki # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + from sage.structure.unique_representation import UniqueRepresentation import sage.categories.all -from sage.rings.all import Integer, gcd, lcm, QQ +from sage.rings.all import Integer, QQ +from sage.arith.all import gcd, lcm from sage.rings.fraction_field import is_FractionField from sage.misc.all import prod from sage.categories.morphism import SetMorphism diff --git a/src/sage/combinat/sf/powersum.py b/src/sage/combinat/sf/powersum.py index b97176ab8cc..0892f5570b8 100644 --- a/src/sage/combinat/sf/powersum.py +++ b/src/sage/combinat/sf/powersum.py @@ -19,7 +19,7 @@ #***************************************************************************** import sfa, multiplicative, classical from sage.combinat.partition import Partition -from sage.rings.arith import divisors +from sage.arith.all import divisors class SymmetricFunctionAlgebra_power(multiplicative.SymmetricFunctionAlgebra_multiplicative): def __init__(self, Sym): diff --git a/src/sage/combinat/sf/sfa.py b/src/sage/combinat/sf/sfa.py index beafb5c49c3..5a2d6b15c14 100644 --- a/src/sage/combinat/sf/sfa.py +++ b/src/sage/combinat/sf/sfa.py @@ -961,7 +961,7 @@ def gessel_reutenauer(self, lam): m = lam.to_exp_dict() # == {i: m_i | i occurs in lam} p = self.realization_of().power() h = self.realization_of().complete() - from sage.rings.arith import Moebius, squarefree_divisors + from sage.arith.all import Moebius, squarefree_divisors mu = Moebius() def component(i, g): # == h_g[L_i] L_i = p.sum_of_terms([(_Partitions([d] * (i//d)), R(mu(d))) @@ -4138,7 +4138,7 @@ def arithmetic_product(self, x): parent = self.parent() if parent.has_coerce_map_from(QQ): from sage.combinat.partition import Partition - from sage.rings.arith import gcd, lcm + from sage.arith.all import gcd, lcm from itertools import product, repeat, chain p = parent.realization_of().power() def f(lam, mu): diff --git a/src/sage/combinat/sf/witt.py b/src/sage/combinat/sf/witt.py index eb58afcbde4..7af7a2ffd67 100644 --- a/src/sage/combinat/sf/witt.py +++ b/src/sage/combinat/sf/witt.py @@ -751,7 +751,7 @@ def _precompute_p(self, n): """ l = len(self._p_transition_matrices) if l <= n: - from sage.rings.arith import divisors + from sage.arith.all import divisors from sage.combinat.partition import Partition from sage.misc.cachefunc import cached_function @cached_function @@ -1141,7 +1141,7 @@ def wsum(m): # expansion of h_m in w-basis, for m > 0 return result if parent_name == "powersum": - from sage.rings.arith import divisors + from sage.arith.all import divisors from sage.combinat.partition import Partition @cached_function def wsum_p(m): # expansion of p_m in w-basis, for m > 0 diff --git a/src/sage/combinat/shuffle.py b/src/sage/combinat/shuffle.py index 47f0896cc11..65aa5c1896f 100644 --- a/src/sage/combinat/shuffle.py +++ b/src/sage/combinat/shuffle.py @@ -57,7 +57,7 @@ import collections import itertools -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.structure.sage_object import SageObject ## TODO: Think about Parent/Element for this and the category diff --git a/src/sage/combinat/similarity_class_type.py b/src/sage/combinat/similarity_class_type.py index 2231480778b..a4f24e38133 100644 --- a/src/sage/combinat/similarity_class_type.py +++ b/src/sage/combinat/similarity_class_type.py @@ -179,7 +179,7 @@ class type, it is also possible to compute the number of classes of that type from itertools import chain, product from sage.misc.all import prod from sage.functions.all import factorial -from sage.rings.arith import moebius +from sage.arith.all import moebius, divisors from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass from sage.structure.element import Element, parent from sage.structure.parent import Parent @@ -187,7 +187,7 @@ class type, it is also possible to compute the number of classes of that type from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets from sage.combinat.combinat import CombinatorialElement from sage.combinat.partition import Partitions, Partition -from sage.rings.all import ZZ, QQ, FractionField, divisors +from sage.rings.all import ZZ, QQ, FractionField from sage.misc.cachefunc import cached_in_parent_method, cached_function from sage.combinat.misc import IterableFunctionCall from functools import reduce diff --git a/src/sage/combinat/skew_tableau.py b/src/sage/combinat/skew_tableau.py index d8c3c3604f8..e72d32904da 100644 --- a/src/sage/combinat/skew_tableau.py +++ b/src/sage/combinat/skew_tableau.py @@ -33,7 +33,7 @@ from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets from sage.rings.all import Integer, QQ, ZZ -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.rings.infinity import PlusInfinity from sage.matrix.all import zero_matrix import itertools diff --git a/src/sage/combinat/sloane_functions.py b/src/sage/combinat/sloane_functions.py index d22cee93fad..7c9f8105ba6 100644 --- a/src/sage/combinat/sloane_functions.py +++ b/src/sage/combinat/sloane_functions.py @@ -300,7 +300,7 @@ def __getitem__(self, n): ######################################################################## # You may have to import more here when defining new sequences -import sage.rings.arith as arith +import sage.arith.all as arith from sage.matrix.matrix_space import MatrixSpace from sage.rings.rational_field import QQ from sage.combinat import combinat diff --git a/src/sage/combinat/species/cycle_species.py b/src/sage/combinat/species/cycle_species.py index 6a366abc82e..2affde82cdb 100644 --- a/src/sage/combinat/species/cycle_species.py +++ b/src/sage/combinat/species/cycle_species.py @@ -1,25 +1,23 @@ """ Cycle Species """ + #***************************************************************************** # Copyright (C) 2008 Mike Hansen , # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + from species import GenericCombinatorialSpecies from structure import GenericSpeciesStructure from generating_series import _integers_from from sage.structure.unique_representation import UniqueRepresentation -from sage.rings.all import ZZ, divisors, euler_phi +from sage.rings.all import ZZ +from sage.arith.all import divisors, euler_phi from sage.misc.cachefunc import cached_function from sage.combinat.species.misc import accept_size diff --git a/src/sage/combinat/species/generating_series.py b/src/sage/combinat/species/generating_series.py index c83d750b697..b2a16703adc 100644 --- a/src/sage/combinat/species/generating_series.py +++ b/src/sage/combinat/species/generating_series.py @@ -66,23 +66,21 @@ .. [BLL-Intro] Francois Bergeron, Gilbert Labelle, and Pierre Leroux. "Introduction to the Theory of Species of Structures", March 14, 2008. """ + #***************************************************************************** -# Copyright (C) 2008 Mike Hansen , -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +# Copyright (C) 2008 Mike Hansen # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + from series import LazyPowerSeriesRing, LazyPowerSeries from stream import Stream, _integers_from -from sage.rings.all import Integer, moebius, lcm, divisors, RationalField +from sage.rings.all import Integer, RationalField +from sage.arith.all import moebius, gcd, lcm, divisors from sage.combinat.partition import Partition, Partitions from functools import partial from sage.combinat.sf.sf import SymmetricFunctions @@ -759,7 +757,6 @@ def arithmetic_product(self, g, check_input = True): :arXiv:`math/0503436v2`. """ - from sage.rings.arith import gcd, lcm, divisors from itertools import product, repeat, chain p = self.base_ring() diff --git a/src/sage/combinat/subset.py b/src/sage/combinat/subset.py index 24816a9a35b..29f407c4b15 100644 --- a/src/sage/combinat/subset.py +++ b/src/sage/combinat/subset.py @@ -38,7 +38,7 @@ from sage.structure.element import Element from sage.sets.set import Set, Set_object_enumerated -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.integer_ring import ZZ from sage.rings.integer import Integer import combination diff --git a/src/sage/combinat/subword.py b/src/sage/combinat/subword.py index a1f7f45d077..2e5ec1d835f 100644 --- a/src/sage/combinat/subword.py +++ b/src/sage/combinat/subword.py @@ -64,7 +64,7 @@ from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets -import sage.rings.arith as arith +import sage.arith.all as arith import sage.misc.prandom as prandom from sage.rings.integer import Integer from sage.sets.finite_enumerated_set import FiniteEnumeratedSet diff --git a/src/sage/combinat/symmetric_group_algebra.py b/src/sage/combinat/symmetric_group_algebra.py index f32bfd20e33..0da0758fe1e 100644 --- a/src/sage/combinat/symmetric_group_algebra.py +++ b/src/sage/combinat/symmetric_group_algebra.py @@ -17,7 +17,7 @@ from tableau import Tableau, StandardTableaux_size, StandardTableaux_shape, StandardTableaux from sage.interfaces.all import gap from sage.rings.all import QQ, PolynomialRing -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.matrix.all import matrix from sage.modules.all import vector from sage.groups.perm_gps.permgroup_named import SymmetricGroup diff --git a/src/sage/combinat/tableau.py b/src/sage/combinat/tableau.py index 67ba8b3fcda..3cc12c796e8 100644 --- a/src/sage/combinat/tableau.py +++ b/src/sage/combinat/tableau.py @@ -55,21 +55,18 @@ - Add a class for tableaux of a given shape (eg Tableaux_shape) """ + #***************************************************************************** # Copyright (C) 2007 Mike Hansen , # 2011 Jason Bandlow # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + from sage.sets.disjoint_union_enumerated_sets import DisjointUnionEnumeratedSets from sage.sets.family import Family from sage.sets.non_negative_integers import NonNegativeIntegers @@ -79,7 +76,7 @@ from sage.structure.parent import Parent from sage.misc.inherit_comparison import InheritComparisonClasscallMetaclass from sage.rings.infinity import PlusInfinity -from sage.rings.arith import factorial +from sage.arith.all import factorial, binomial from sage.rings.integer import Integer from sage.combinat.composition import Composition, Compositions from integer_vector import IntegerVectors @@ -5526,7 +5523,6 @@ def random_element(self): [[2, 4, 4, 6, 6, 6]] """ from sage.rings.all import ZZ - from sage.rings.arith import binomial from sage.matrix.constructor import diagonal_matrix from sage.combinat.rsk import RSK kchoose2m1 = self.max_entry * (self.max_entry - 1) / 2 - 1 @@ -6401,7 +6397,6 @@ def random_element(self): True """ from sage.misc.prandom import randrange - from sage.rings.arith import binomial from sage.misc.prandom import sample from sage.combinat.perfect_matching import PerfectMatchings from sage.combinat.permutation import from_cycles diff --git a/src/sage/combinat/tableau_tuple.py b/src/sage/combinat/tableau_tuple.py index 4c23db9ac0f..6e1af7a1cc9 100644 --- a/src/sage/combinat/tableau_tuple.py +++ b/src/sage/combinat/tableau_tuple.py @@ -221,7 +221,7 @@ from sage.misc.misc_c import prod from sage.misc.prandom import random from sage.misc.sage_unittest import TestSuite -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.rings.finite_rings.integer_mod_ring import IntegerModRing from sage.rings.integer import Integer from sage.rings.all import NN diff --git a/src/sage/combinat/tamari_lattices.py b/src/sage/combinat/tamari_lattices.py index 872578e54ff..2aad873edff 100644 --- a/src/sage/combinat/tamari_lattices.py +++ b/src/sage/combinat/tamari_lattices.py @@ -46,7 +46,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** from sage.combinat.posets.lattices import LatticePoset -from sage.rings.arith import gcd +from sage.arith.all import gcd def paths_in_triangle(i, j, a, b): diff --git a/src/sage/combinat/words/shuffle_product.py b/src/sage/combinat/words/shuffle_product.py index 83f47dec92a..5dfa7f0caae 100644 --- a/src/sage/combinat/words/shuffle_product.py +++ b/src/sage/combinat/words/shuffle_product.py @@ -23,7 +23,7 @@ #***************************************************************************** from sage.combinat.words.word import Word_class from sage.combinat.combinat import CombinatorialClass -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.combinat.integer_vector import IntegerVectors from sage.combinat.subset import Subsets from sage.combinat.composition import Compositions_n, Compositions diff --git a/src/sage/combinat/words/word_generators.py b/src/sage/combinat/words/word_generators.py index 185c30b95fa..34e27aaf7a2 100644 --- a/src/sage/combinat/words/word_generators.py +++ b/src/sage/combinat/words/word_generators.py @@ -63,7 +63,7 @@ from sage.combinat.words.finite_word import FiniteWord_class, Factorization from sage.combinat.words.words import Words, FiniteWords, InfiniteWords from sage.combinat.words.morphism import WordMorphism -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.misc.decorators import rename_keyword def _build_tab(sym, tab, W): diff --git a/src/sage/crypto/boolean_function.pyx b/src/sage/crypto/boolean_function.pyx index d44f5686113..4ecc77ccac9 100644 --- a/src/sage/crypto/boolean_function.pyx +++ b/src/sage/crypto/boolean_function.pyx @@ -963,7 +963,7 @@ cdef class BooleanFunction(SageObject): from sage.misc.all import prod from sage.matrix.constructor import Matrix - from sage.rings.arith import binomial + from sage.arith.all import binomial M = Matrix(GF(2),sum([binomial(self._nvariables,i) for i in xrange(d+1)]),len(s)) for i in xrange(1,d+1): diff --git a/src/sage/crypto/classical.py b/src/sage/crypto/classical.py index ee21cf94133..3eb308cc089 100644 --- a/src/sage/crypto/classical.py +++ b/src/sage/crypto/classical.py @@ -34,8 +34,10 @@ #***************************************************************************** # Copyright (C) 2007 David Kohel # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -53,7 +55,7 @@ from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ from sage.rings.finite_rings.integer_mod_ring import IntegerModRing -from sage.rings.arith import xgcd +from sage.arith.all import xgcd, gcd, inverse_mod from random import randint from sage.matrix.matrix_space import MatrixSpace @@ -273,7 +275,6 @@ def __init__(self, A): raise TypeError("A (= %s) is not supported as a cipher domain of this affine cryptosystem." % A) # List L of invertible linear coefficients modulo n, where n is the # alphabet size. Each e in L satisfies gcd(e, n) = 1. - from sage.rings.arith import gcd n = A.ngens() self._invertible_A = [i for i in xrange(n) if gcd(i, n) == 1] # Initialize the affine cryptosystem with the plaintext, ciphertext, @@ -1236,7 +1237,6 @@ def inverse_key(self, a, b): ValueError: (a, b) = (0, 1) is outside the range of acceptable values for a key of this affine cipher. """ try: - from sage.rings.arith import inverse_mod from sage.rings.finite_rings.integer_mod import Mod n = self.alphabet_size() aInv = inverse_mod(a, n) diff --git a/src/sage/crypto/lattice.py b/src/sage/crypto/lattice.py index 9a9c54ab3a3..885359b561c 100644 --- a/src/sage/crypto/lattice.py +++ b/src/sage/crypto/lattice.py @@ -271,7 +271,7 @@ def gen_lattice(type='modular', n=4, m=8, q=11, seed=None, A = A.stack(R.random_element().matrix()) elif type == 'cyclotomic': - from sage.rings.arith import euler_phi + from sage.arith.all import euler_phi from sage.misc.functional import cyclotomic_polynomial # we assume that n+1 <= min( euler_phi^{-1}(n) ) <= 2*n diff --git a/src/sage/crypto/lwe.py b/src/sage/crypto/lwe.py index 83fc8892f44..7b6a77bd589 100644 --- a/src/sage/crypto/lwe.py +++ b/src/sage/crypto/lwe.py @@ -106,7 +106,7 @@ from sage.modules.free_module_element import random_vector, vector from sage.numerical.optimize import find_root from sage.rings.all import ZZ, RealField, IntegerModRing, RR -from sage.rings.arith import next_prime, euler_phi +from sage.arith.all import next_prime, euler_phi from sage.structure.element import parent from sage.structure.sage_object import SageObject from sage.symbolic.constants import pi diff --git a/src/sage/crypto/public_key/blum_goldwasser.py b/src/sage/crypto/public_key/blum_goldwasser.py index 985818db1e4..1bc04984a83 100644 --- a/src/sage/crypto/public_key/blum_goldwasser.py +++ b/src/sage/crypto/public_key/blum_goldwasser.py @@ -27,24 +27,17 @@ the description contained in [MenezesEtAl1996]_. """ -########################################################################### -# Copyright (c) 2009, 2010 -# Mike Hogan -# David Joyner -# Minh Van Nguyen +#***************************************************************************** +# Copyright (c) 2009, 2010 Mike Hogan +# Copyright (c) 2009, 2010 David Joyner +# Copyright (c) 2009, 2010 Minh Van Nguyen # -# This program is free software; you can redistribute it and/or modify +# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# http://www.gnu.org/licenses/ -########################################################################### +# http://www.gnu.org/licenses/ +#***************************************************************************** from operator import xor @@ -55,9 +48,7 @@ from sage.functions.log import log from sage.functions.other import Function_floor from sage.monoids.string_monoid import BinaryStrings -from sage.rings.arith import gcd -from sage.rings.arith import power_mod -from sage.rings.arith import xgcd +from sage.arith.all import gcd, power_mod, xgcd from sage.rings.finite_rings.integer_mod import Mod as mod from sage.rings.finite_rings.integer_mod_ring import IntegerModFactory diff --git a/src/sage/crypto/stream.py b/src/sage/crypto/stream.py index 53b64980eb3..2964eb578d3 100644 --- a/src/sage/crypto/stream.py +++ b/src/sage/crypto/stream.py @@ -5,8 +5,10 @@ #***************************************************************************** # Copyright (C) 2007 David Kohel # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -15,8 +17,7 @@ from sage.crypto.util import random_blum_prime from sage.monoids.string_monoid import BinaryStrings -from sage.rings.arith import gcd -from sage.rings.arith import power_mod +from sage.arith.all import gcd, power_mod from sage.rings.finite_rings.constructor import FiniteField from sage.rings.finite_rings.integer_mod_ring import IntegerModFactory from sage.rings.polynomial.polynomial_element import is_Polynomial diff --git a/src/sage/crypto/util.py b/src/sage/crypto/util.py index 6ecbca5a543..2bde594782d 100644 --- a/src/sage/crypto/util.py +++ b/src/sage/crypto/util.py @@ -10,27 +10,18 @@ ``is_blum_prime``, ``least_significant_bits``, ``random_blum_prime``. """ -########################################################################### -# Copyright (c) 2009, 2010 Minh Van Nguyen +#***************************************************************************** +# Copyright (c) 2009, 2010 Minh Van Nguyen # -# This program is free software; you can redistribute it and/or modify +# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or +# the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# http://www.gnu.org/licenses/ -########################################################################### +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.monoids.string_monoid import BinaryStrings -from sage.rings.arith import is_prime -from sage.rings.arith import lcm -from sage.rings.arith import primes -from sage.rings.arith import random_prime +from sage.arith.all import is_prime, lcm, primes, random_prime from sage.rings.integer import Integer from sage.rings.finite_rings.integer_mod import Mod as mod diff --git a/src/sage/ext/multi_modular.pyx b/src/sage/ext/multi_modular.pyx index fd6d162c9b3..a58b945eedd 100644 --- a/src/sage/ext/multi_modular.pyx +++ b/src/sage/ext/multi_modular.pyx @@ -17,7 +17,7 @@ include "sage/ext/stdsage.pxi" from sage.libs.gmp.mpz cimport * from sage.rings.integer_ring import ZZ -from sage.rings.arith import random_prime +from sage.arith.all import random_prime from types import GeneratorType # should I have mod_int versions of these functions? diff --git a/src/sage/functions/hypergeometric.py b/src/sage/functions/hypergeometric.py index 602f9d24e2f..5dd02433d66 100644 --- a/src/sage/functions/hypergeometric.py +++ b/src/sage/functions/hypergeometric.py @@ -126,13 +126,15 @@ sage: nest(lambda y: hypergeometric([y], [], x), 3, 1)._mathematica_init_() 'HypergeometricPFQ[{HypergeometricPFQ[{HypergeometricPFQ[{1},{},x]},... """ + #***************************************************************************** # Copyright (C) 2010 Fredrik Johansson # Copyright (C) 2013 Eviatar Bach # -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -140,7 +142,7 @@ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ from sage.rings.infinity import Infinity -from sage.rings.arith import (binomial, rising_factorial, factorial) +from sage.arith.all import binomial, rising_factorial, factorial from sage.functions.other import sqrt, gamma, real_part from sage.functions.log import exp, log from sage.functions.trig import cos, sin diff --git a/src/sage/functions/orthogonal_polys.py b/src/sage/functions/orthogonal_polys.py index d84d9a90ee4..2532a85564a 100644 --- a/src/sage/functions/orthogonal_polys.py +++ b/src/sage/functions/orthogonal_polys.py @@ -1662,7 +1662,7 @@ def _eval_special_values_(self, n, a, x): if a == 0: return laguerre(n, x) if x == 0: - from sage.rings.arith import binomial + from sage.arith.all import binomial return binomial(n+a, n) def _pol_gen_laguerre(self, n, a, x): diff --git a/src/sage/functions/other.py b/src/sage/functions/other.py index 9e107352b5a..417f8ea7cbe 100644 --- a/src/sage/functions/other.py +++ b/src/sage/functions/other.py @@ -1651,7 +1651,7 @@ def _evalf_(self, n, k, parent=None, algorithm=None): sage: binomial._evalf_(3/2,SR(1/1)) 3/2 """ - return sage.rings.arith.binomial(n, k) + return sage.arith.all.binomial(n, k) binomial = Function_binomial() diff --git a/src/sage/geometry/cone.py b/src/sage/geometry/cone.py index 9c78587b7bf..db59d20e9ce 100644 --- a/src/sage/geometry/cone.py +++ b/src/sage/geometry/cone.py @@ -211,7 +211,8 @@ from sage.misc.all import cached_method, flatten, latex from sage.misc.superseded import deprecation from sage.modules.all import span, vector, VectorSpace -from sage.rings.all import QQ, RR, ZZ, gcd +from sage.rings.all import QQ, RR, ZZ +from sage.arith.all import gcd from sage.structure.all import SageObject, parent from sage.libs.ppl import C_Polyhedron, Generator_System, Constraint_System, \ Linear_Expression, ray as PPL_ray, point as PPL_point, \ diff --git a/src/sage/geometry/hyperplane_arrangement/hyperplane.py b/src/sage/geometry/hyperplane_arrangement/hyperplane.py index d73bc840925..15e4faa852c 100644 --- a/src/sage/geometry/hyperplane_arrangement/hyperplane.py +++ b/src/sage/geometry/hyperplane_arrangement/hyperplane.py @@ -544,7 +544,7 @@ def primitive(self, signed=True): sage: (4*x - y - 8).primitive(signed=False) Hyperplane -4*x + y + 8 """ - from sage.rings.all import lcm, gcd + from sage.arith.all import lcm, gcd coeffs = self.coefficients() try: d = lcm([x.denom() for x in coeffs]) diff --git a/src/sage/geometry/hyperplane_arrangement/library.py b/src/sage/geometry/hyperplane_arrangement/library.py index b22612457e4..ed2eb245ed3 100644 --- a/src/sage/geometry/hyperplane_arrangement/library.py +++ b/src/sage/geometry/hyperplane_arrangement/library.py @@ -18,7 +18,7 @@ from sage.misc.misc_c import prod from sage.combinat.combinat import stirling_number2 -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.polynomial.polynomial_ring import polygen from sage.geometry.hyperplane_arrangement.arrangement import HyperplaneArrangements diff --git a/src/sage/geometry/integral_points.pyx b/src/sage/geometry/integral_points.pyx index d5f49bbe0fb..d13494b2bdb 100644 --- a/src/sage/geometry/integral_points.pyx +++ b/src/sage/geometry/integral_points.pyx @@ -5,8 +5,10 @@ Cython helper methods to compute integral points in polyhedra. #***************************************************************************** # Copyright (C) 2010 Volker Braun # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -14,8 +16,9 @@ import copy import itertools from sage.matrix.constructor import matrix, column_matrix, vector, diagonal_matrix -from sage.rings.all import QQ, RR, ZZ, gcd, lcm +from sage.rings.all import QQ, RR, ZZ from sage.rings.integer cimport Integer +from sage.arith.all import gcd, lcm from sage.combinat.permutation import Permutation from sage.misc.all import prod, uniq diff --git a/src/sage/geometry/lattice_polytope.py b/src/sage/geometry/lattice_polytope.py index 4b6c901867e..6d8c6e42de0 100644 --- a/src/sage/geometry/lattice_polytope.py +++ b/src/sage/geometry/lattice_polytope.py @@ -118,7 +118,8 @@ from sage.plot.plot3d.index_face_set import IndexFaceSet from sage.plot.plot3d.all import line3d, point3d from sage.plot.plot3d.shapes2 import text3d -from sage.rings.all import Integer, ZZ, QQ, gcd, lcm +from sage.rings.all import Integer, ZZ, QQ +from sage.arith.all import gcd, lcm from sage.sets.set import Set_generic from sage.structure.all import Sequence from sage.structure.sequence import Sequence_generic diff --git a/src/sage/geometry/polyhedron/base_ZZ.py b/src/sage/geometry/polyhedron/base_ZZ.py index 8de9c6e5581..7c43ded7906 100644 --- a/src/sage/geometry/polyhedron/base_ZZ.py +++ b/src/sage/geometry/polyhedron/base_ZZ.py @@ -2,24 +2,24 @@ Base class for polyhedra over `\ZZ` """ -######################################################################## +#***************************************************************************** # Copyright (C) 2011 Volker Braun # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################## - +#***************************************************************************** - -from sage.rings.all import ZZ, QQ, gcd +from sage.rings.all import ZZ, QQ from sage.misc.all import cached_method from sage.modules.free_module_element import vector +from sage.arith.all import gcd from constructor import Polyhedron from base import Polyhedron_base - ######################################################################### class Polyhedron_ZZ(Polyhedron_base): """ diff --git a/src/sage/geometry/polyhedron/plot.py b/src/sage/geometry/polyhedron/plot.py index 19619849e25..b326fbfcca5 100644 --- a/src/sage/geometry/polyhedron/plot.py +++ b/src/sage/geometry/polyhedron/plot.py @@ -627,7 +627,7 @@ def schlegel(self, projection_direction=None, height=1.1): if self.parent_polyhedron.is_full_dimensional(): projection_direction = next(self.parent_polyhedron.inequality_generator()).A() else: - from sage.rings.arith import primes_first_n + from sage.arith.all import primes_first_n projection_direction = primes_first_n(self.polyhedron_ambient_dim) return self(ProjectionFuncSchlegel( projection_direction, height=height, center=center)) diff --git a/src/sage/geometry/triangulation/element.py b/src/sage/geometry/triangulation/element.py index 7189b9fb391..5d907168b6c 100644 --- a/src/sage/geometry/triangulation/element.py +++ b/src/sage/geometry/triangulation/element.py @@ -782,7 +782,7 @@ def normal_cone(self): from sage.libs.ppl import Variable, Constraint, Constraint_System, Linear_Expression, C_Polyhedron from sage.matrix.constructor import matrix from sage.misc.misc import uniq - from sage.rings.arith import lcm + from sage.arith.all import lcm pc = self.point_configuration() cs = Constraint_System() for facet in self.interior_facets(): diff --git a/src/sage/graphs/digraph.py b/src/sage/graphs/digraph.py index 52541e54808..9bc69ac8b3a 100644 --- a/src/sage/graphs/digraph.py +++ b/src/sage/graphs/digraph.py @@ -3242,7 +3242,7 @@ def period(self): :meth:`is_aperiodic` """ - from sage.rings.arith import gcd + from sage.arith.all import gcd g = 0 diff --git a/src/sage/graphs/generators/classical_geometries.py b/src/sage/graphs/generators/classical_geometries.py index 4e908f156a2..d67dd828664 100644 --- a/src/sage/graphs/generators/classical_geometries.py +++ b/src/sage/graphs/generators/classical_geometries.py @@ -21,7 +21,7 @@ from math import sin, cos, pi from sage.graphs.graph import Graph from sage.graphs import graph -from sage.rings.arith import is_prime_power +from sage.arith.all import is_prime_power from sage.rings.finite_rings.constructor import FiniteField def SymplecticPolarGraph(d, q, algorithm=None): diff --git a/src/sage/graphs/generators/families.py b/src/sage/graphs/generators/families.py index d1e0de51efd..74810ca98fa 100644 --- a/src/sage/graphs/generators/families.py +++ b/src/sage/graphs/generators/families.py @@ -1588,7 +1588,7 @@ def PaleyGraph(q): """ from sage.rings.finite_rings.integer_mod import mod from sage.rings.finite_rings.constructor import FiniteField - from sage.rings.arith import is_prime_power + from sage.arith.all import is_prime_power assert is_prime_power(q), "Parameter q must be a prime power" assert mod(q,4)==1, "Parameter q must be congruent to 1 mod 4" g = Graph([FiniteField(q,'a'), lambda i,j: (i-j).is_square()], @@ -2563,7 +2563,7 @@ def MathonPseudocyclicStronglyRegularGraph(t, G=None, L=None): from sage.matrix.constructor import matrix, block_matrix, \ ones_matrix, identity_matrix from itertools import product - from sage.rings.arith import two_squares + from sage.arith.all import two_squares p = 4*t+1 try: x = two_squares(p) diff --git a/src/sage/graphs/hyperbolicity.pyx b/src/sage/graphs/hyperbolicity.pyx index a834da52815..8bd35726be9 100644 --- a/src/sage/graphs/hyperbolicity.pyx +++ b/src/sage/graphs/hyperbolicity.pyx @@ -173,7 +173,7 @@ Methods from libc.string cimport memset from sage.graphs.graph import Graph from sage.graphs.distances_all_pairs cimport c_distances_all_pairs -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.integer_ring import ZZ from sage.rings.real_mpfr import RR from sage.functions.other import floor diff --git a/src/sage/graphs/strongly_regular_db.pyx b/src/sage/graphs/strongly_regular_db.pyx index 918eeac41aa..c7c9dc9c616 100644 --- a/src/sage/graphs/strongly_regular_db.pyx +++ b/src/sage/graphs/strongly_regular_db.pyx @@ -31,8 +31,7 @@ Functions """ from sage.categories.sets_cat import EmptySetError from sage.misc.unknown import Unknown -from sage.rings.arith import is_square -from sage.rings.arith import is_prime_power +from sage.arith.all import is_square, is_prime_power, divisors from sage.misc.cachefunc import cached_function from sage.combinat.designs.orthogonal_arrays import orthogonal_array from sage.combinat.designs.bibd import balanced_incomplete_block_design @@ -318,7 +317,6 @@ def is_affine_polar(int v,int k,int l,int mu): sage: t = is_affine_polar(5,5,5,5); t """ - from sage.rings.arith import divisors # Using notations from http://www.win.tue.nl/~aeb/graphs/VO.html # # VO+(2e,q) has parameters: v = q^(2e), k = (q^(e−1) + 1)(q^e − 1), λ = @@ -386,7 +384,6 @@ def is_orthogonal_polar(int v,int k,int l,int mu): (, 6, 3, '+') """ - from sage.rings.arith import divisors r,s = eigenvalues(v,k,l,mu) if r is None: return diff --git a/src/sage/groups/abelian_gps/abelian_group.py b/src/sage/groups/abelian_gps/abelian_group.py index 52cacad0404..561998bc5c0 100644 --- a/src/sage/groups/abelian_gps/abelian_group.py +++ b/src/sage/groups/abelian_gps/abelian_group.py @@ -191,15 +191,17 @@ immutables. Rename invariants to gens_orders. """ -########################################################################## -# Copyright (C) 2006 William Stein -# Copyright (C) 2006 David Joyner -# Copyright (C) 2012 Volker Braun -# -# Distributed under the terms of the GNU General Public License (GPL): +#***************************************************************************** +# Copyright (C) 2006 William Stein +# Copyright (C) 2006 David Joyner +# Copyright (C) 2012 Volker Braun # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -########################################################################## +#***************************************************************************** import six from sage.rings.integer import Integer @@ -207,12 +209,11 @@ from sage.structure.category_object import normalize_names from sage.structure.unique_representation import UniqueRepresentation from sage.rings.infinity import infinity -from sage.rings.arith import divisors, gcd +from sage.arith.all import divisors, gcd, lcm from sage.groups.abelian_gps.abelian_group_element import AbelianGroupElement from sage.misc.cachefunc import cached_method from sage.misc.all import prod from sage.misc.mrange import mrange, cartesian_product_iterator -from sage.rings.arith import lcm from sage.groups.group import AbelianGroup as AbelianGroupBase from sage.categories.groups import Groups @@ -737,8 +738,7 @@ def dual_group(self, names="X", base_ring=None): raise ValueError('the group must be finite') if base_ring is None: from sage.rings.number_field.number_field import CyclotomicField - from sage.rings.arith import LCM - base_ring = CyclotomicField(LCM(self.gens_orders())) + base_ring = CyclotomicField(lcm(self.gens_orders())) return DualAbelianGroup_class(self, names=names, base_ring=base_ring) @cached_method diff --git a/src/sage/groups/abelian_gps/abelian_group_element.py b/src/sage/groups/abelian_gps/abelian_group_element.py index d1d61c93cc0..c36fb722856 100644 --- a/src/sage/groups/abelian_gps/abelian_group_element.py +++ b/src/sage/groups/abelian_gps/abelian_group_element.py @@ -47,7 +47,7 @@ from sage.rings.integer import Integer from sage.rings.infinity import infinity -from sage.rings.arith import LCM, GCD +from sage.arith.all import LCM, GCD from sage.groups.abelian_gps.element_base import AbelianGroupElementBase def is_AbelianGroupElement(x): diff --git a/src/sage/groups/abelian_gps/dual_abelian_group_element.py b/src/sage/groups/abelian_gps/dual_abelian_group_element.py index 104e6d16763..fe888ea844f 100644 --- a/src/sage/groups/abelian_gps/dual_abelian_group_element.py +++ b/src/sage/groups/abelian_gps/dual_abelian_group_element.py @@ -42,20 +42,21 @@ Default to cyclotomic base ring. """ -########################################################################### -# Copyright (C) 2006 William Stein -# Copyright (C) 2006 David Joyner -# Copyright (C) 2012 Volker Braun +#***************************************************************************** +# Copyright (C) 2006 William Stein +# Copyright (C) 2006 David Joyner +# Copyright (C) 2012 Volker Braun # -# Distributed under the terms of the GNU General Public License (GPL) +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -########################################################################### +#***************************************************************************** import operator -from sage.rings.integer import Integer -from sage.rings.infinity import infinity -from sage.rings.arith import * +from sage.arith.all import LCM from sage.misc.all import prod from sage.rings.complex_field import is_ComplexField from sage.groups.abelian_gps.element_base import AbelianGroupElementBase diff --git a/src/sage/groups/abelian_gps/element_base.py b/src/sage/groups/abelian_gps/element_base.py index 1cf9c4a5a62..657ee1d2c2a 100644 --- a/src/sage/groups/abelian_gps/element_base.py +++ b/src/sage/groups/abelian_gps/element_base.py @@ -20,7 +20,7 @@ from sage.structure.element import MultiplicativeGroupElement from sage.misc.cachefunc import cached_method -from sage.rings.arith import GCD, LCM +from sage.arith.all import GCD, LCM from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ from sage.rings.infinity import infinity diff --git a/src/sage/groups/generic.py b/src/sage/groups/generic.py index d804a988e3b..460cda6a658 100644 --- a/src/sage/groups/generic.py +++ b/src/sage/groups/generic.py @@ -812,7 +812,7 @@ def discrete_log(a, base, ord=None, bounds=None, operation='*', identity=None, i elif operation in addition_names: c=bsgs(base*(ord//pi),(a-base*l[i])*(ord//pi**(j+1)),(0,pi),operation=operation) l[i] += c*(pi**j) - from sage.rings.arith import CRT_list + from sage.arith.all import CRT_list return CRT_list(l,[pi**ri for pi,ri in f]) except ValueError: raise ValueError("No discrete log of %s found to base %s"%(a,base)) @@ -999,7 +999,7 @@ def linear_relation(P, Q, operation='+', identity=None, inverse=None, op=None): n = P.order() m = Q.order() - g = sage.rings.arith.gcd(n,m) + g = sage.arith.all.gcd(n,m) if g==1: return (m,Z(0)) n1 = n//g m1 = m//g @@ -1233,8 +1233,8 @@ def order_from_bounds(P, bounds, d=None, operation='+', if d > 1: Q = multiple(P,d,operation=operation) lb, ub = bounds - bounds = ( sage.rings.arith.integer_ceil(lb/d), - sage.rings.arith.integer_floor(ub/d) ) + bounds = ( sage.arith.all.integer_ceil(lb/d), + sage.arith.all.integer_floor(ub/d) ) # Use generic bsgs to find n=d*m with lb<=n<=ub and n*P=0 @@ -1321,7 +1321,7 @@ def merge_points(P1,P2, operation='+', if n2.divides(n1): return (g1,n1) - m,k1,k2 = sage.rings.arith.xlcm(n1,n2); + m,k1,k2 = sage.arith.all.xlcm(n1,n2) m1 = n1//k1 m2 = n2//k2 g1 = multiple(g1,m1,operation=operation) diff --git a/src/sage/groups/perm_gps/partn_ref/data_structures_pyx.pxi b/src/sage/groups/perm_gps/partn_ref/data_structures_pyx.pxi index afff203627b..8638dab5acb 100644 --- a/src/sage/groups/perm_gps/partn_ref/data_structures_pyx.pxi +++ b/src/sage/groups/perm_gps/partn_ref/data_structures_pyx.pxi @@ -1786,7 +1786,7 @@ def SC_test_list_perms(list L, int n, int limit, bint gap, bint limit_complain, if SC_is_giant(n, len(L), perm, 0.9, giant_support): giant = True m = bitset_len(giant_support) - from sage.rings.arith import factorial + from sage.arith.all import factorial if not (order == factorial(m) or order == factorial(m)/2): print "SC_is_giant failed: %s %s"%(str(L), order) raise AssertionError @@ -1905,7 +1905,7 @@ def SC_test_list_perms(list L, int n, int limit, bint gap, bint limit_complain, perm[n*j + i] = Lperm[i] j += 1 if SC_is_giant(n, len(L), perm, 0.9, giant_support): - from sage.rings.arith import factorial + from sage.arith.all import factorial m = bitset_len(giant_support) if order != factorial(m) and order != factorial(m)/2: print "SC_is_giant failed: %s %s"%(str(L), order) diff --git a/src/sage/groups/perm_gps/partn_ref/refinement_matrices.pyx b/src/sage/groups/perm_gps/partn_ref/refinement_matrices.pyx index b852ff60035..1776a179b0d 100644 --- a/src/sage/groups/perm_gps/partn_ref/refinement_matrices.pyx +++ b/src/sage/groups/perm_gps/partn_ref/refinement_matrices.pyx @@ -318,7 +318,7 @@ def random_tests(n=10, nrows_max=50, ncols_max=50, nsymbols_max=10, perms_per_ma from sage.combinat.permutation import Permutations from sage.matrix.constructor import random_matrix, matrix from sage.rings.finite_rings.constructor import FiniteField as GF - from sage.rings.arith import next_prime + from sage.arith.all import next_prime cdef int h, i, j, nrows, k, num_tests = 0, num_matrices = 0 cdef MatrixStruct M, N for m in range(n): diff --git a/src/sage/groups/perm_gps/permgroup.py b/src/sage/groups/perm_gps/permgroup.py index 3f882a5c26b..336f0d400a0 100644 --- a/src/sage/groups/perm_gps/permgroup.py +++ b/src/sage/groups/perm_gps/permgroup.py @@ -184,7 +184,7 @@ def wrapped(self, n, p=0): if not is_package_installed('gap_packages'): raise RuntimeError("You must install the optional gap_packages package.") load_hap() - from sage.rings.arith import is_prime + from sage.arith.all import is_prime if not (p == 0 or is_prime(p)): raise ValueError("p must be 0 or prime") @@ -4001,7 +4001,7 @@ def poincare_series(self, p=2, n=10): if not is_package_installed('gap_packages'): raise RuntimeError("You must install the optional gap_packages package.") load_hap() - from sage.rings.arith import is_prime + from sage.arith.all import is_prime if not (p == 0 or is_prime(p)): raise ValueError("p must be 0 or prime") diff --git a/src/sage/groups/perm_gps/permgroup_named.py b/src/sage/groups/perm_gps/permgroup_named.py index 75ac680ba0a..5b5eb7f93d0 100644 --- a/src/sage/groups/perm_gps/permgroup_named.py +++ b/src/sage/groups/perm_gps/permgroup_named.py @@ -86,7 +86,7 @@ from sage.rings.all import Integer from sage.interfaces.all import gap from sage.rings.finite_rings.constructor import FiniteField as GF -from sage.rings.arith import factor +from sage.arith.all import factor, valuation from sage.groups.abelian_gps.abelian_group import AbelianGroup from sage.misc.functional import is_even from sage.misc.cachefunc import cached_method, weak_cached_function @@ -2956,7 +2956,6 @@ def __init__(self, q, name='a'): - http://en.wikipedia.org/wiki/Group_of_Lie_type\#Suzuki-Ree_groups """ q = Integer(q) - from sage.rings.arith import valuation t = valuation(q, 2) if 2**t != q or is_even(t): raise ValueError("The ground field size %s must be an odd power of 2." % q) diff --git a/src/sage/gsl/dft.py b/src/sage/gsl/dft.py index b165761bb23..3c3c7355565 100644 --- a/src/sage/gsl/dft.py +++ b/src/sage/gsl/dft.py @@ -79,7 +79,7 @@ from sage.groups.perm_gps.permgroup_element import is_PermutationGroupElement from sage.rings.integer_ring import ZZ from sage.rings.integer import Integer -from sage.rings.arith import factor +from sage.arith.all import factor from sage.rings.rational_field import QQ from sage.rings.real_mpfr import RR from sage.functions.all import sin, cos diff --git a/src/sage/homology/delta_complex.py b/src/sage/homology/delta_complex.py index 45e0abf95fa..6f3f68adf27 100644 --- a/src/sage/homology/delta_complex.py +++ b/src/sage/homology/delta_complex.py @@ -63,7 +63,7 @@ from sage.homology.simplicial_complex import Simplex, lattice_paths, SimplicialComplex from sage.homology.chain_complex import ChainComplex from sage.graphs.graph import Graph -from sage.rings.arith import binomial +from sage.arith.all import binomial class DeltaComplex(GenericCellComplex): r""" diff --git a/src/sage/homology/koszul_complex.py b/src/sage/homology/koszul_complex.py index 592c2182c5c..d7b9ecf0435 100644 --- a/src/sage/homology/koszul_complex.py +++ b/src/sage/homology/koszul_complex.py @@ -15,7 +15,7 @@ from sage.structure.unique_representation import UniqueRepresentation from sage.structure.parent import Parent from sage.combinat.combination import rank -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.all import ZZ from sage.matrix.constructor import matrix from sage.homology.chain_complex import ChainComplex_class diff --git a/src/sage/homology/simplicial_complex.py b/src/sage/homology/simplicial_complex.py index a0ed1d2e32d..988f0113301 100644 --- a/src/sage/homology/simplicial_complex.py +++ b/src/sage/homology/simplicial_complex.py @@ -1352,7 +1352,7 @@ def h_vector(self): sage: octa.h_vector() [1, 3, 3, 1] """ - from sage.rings.arith import binomial + from sage.arith.all import binomial d = self.dimension() f = self.f_vector() # indexed starting at 0, since it's a Python list h = [] diff --git a/src/sage/lfunctions/zero_sums.pyx b/src/sage/lfunctions/zero_sums.pyx index 6c4535c3d9c..0735ca01423 100644 --- a/src/sage/lfunctions/zero_sums.pyx +++ b/src/sage/lfunctions/zero_sums.pyx @@ -8,27 +8,22 @@ AUTHORS: """ -############################################################################## +#***************************************************************************** # Copyright (C) 2014 Simon Spicer # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -############################################################################## +#***************************************************************************** from sage.structure.sage_object cimport SageObject from sage.rings.integer_ring import ZZ from sage.rings.real_double import RDF from sage.rings.complex_double import CDF from sage.rings.infinity import PlusInfinity -from sage.rings.arith import prime_powers +from sage.arith.all import prime_powers, next_prime from sage.functions.log import log, exp from sage.functions.other import real, imag from sage.symbolic.constants import pi, euler_gamma @@ -1318,7 +1313,6 @@ cdef class LFunctionZeroSum_EllipticCurve(LFunctionZeroSum_abstract): # General case for n > 480 else: from sage.rings.finite_rings.integer_mod import mod - from sage.rings.arith import next_prime modulus,p = 2,2 small_primes,residue_list = [2],[1] diff --git a/src/sage/libs/fes.pyx b/src/sage/libs/fes.pyx index 40b9c23bfbb..842ae99d680 100644 --- a/src/sage/libs/fes.pyx +++ b/src/sage/libs/fes.pyx @@ -86,7 +86,7 @@ from sage.structure.sequence import Sequence from sage.rings.polynomial.multi_polynomial cimport MPolynomial from sage.rings.polynomial.term_order import TermOrder from sage.rings.polynomial.pbori import BooleanPolynomial, BooleanPolynomialRing -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.combinat.subset import Subsets from sage.matrix.all import * diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index f3623328eb3..f418222ebe3 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -14215,7 +14215,7 @@ explicitly setting the argument to `True` or `False` will avoid this message.""" - Rob Beezer (2011-06-09) """ - from sage.rings.arith import gcd # remove if translated to object-oriented calls + from sage.arith.all import gcd import sage.rings.polynomial.polynomial_ring_constructor import sage.matrix.constructor diff --git a/src/sage/matrix/matrix_cyclo_dense.pyx b/src/sage/matrix/matrix_cyclo_dense.pyx index 9fa1346fe5f..c932852131c 100644 --- a/src/sage/matrix/matrix_cyclo_dense.pyx +++ b/src/sage/matrix/matrix_cyclo_dense.pyx @@ -56,7 +56,7 @@ from misc import matrix_integer_dense_rational_reconstruction from sage.rings.rational_field import QQ from sage.rings.integer_ring import ZZ -from sage.rings.arith import previous_prime, binomial +from sage.arith.all import previous_prime, binomial from sage.rings.all import RealNumber from sage.rings.integer cimport Integer from sage.rings.rational cimport Rational diff --git a/src/sage/matrix/matrix_integer_dense.pyx b/src/sage/matrix/matrix_integer_dense.pyx index eeaa965d441..f582217e075 100644 --- a/src/sage/matrix/matrix_integer_dense.pyx +++ b/src/sage/matrix/matrix_integer_dense.pyx @@ -60,7 +60,7 @@ from sage.modules.vector_integer_dense cimport Vector_integer_dense from sage.misc.misc import verbose, get_verbose, cputime -from sage.rings.arith import previous_prime +from sage.arith.all import previous_prime from sage.structure.element cimport Element, generic_power_c from sage.structure.proof.proof import get_flag as get_proof_flag from sage.misc.randstate cimport randstate, current_randstate diff --git a/src/sage/matrix/matrix_integer_dense_hnf.py b/src/sage/matrix/matrix_integer_dense_hnf.py index 3460cb405c2..54fb4f31d60 100644 --- a/src/sage/matrix/matrix_integer_dense_hnf.py +++ b/src/sage/matrix/matrix_integer_dense_hnf.py @@ -11,7 +11,8 @@ from sage.misc.misc import verbose, cputime from sage.matrix.constructor import random_matrix, matrix, matrix, identity_matrix -from sage.rings.all import ZZ, Integer, previous_prime, next_prime, CRT_list, RR +from sage.rings.all import ZZ, Integer, RR +from sage.arith.all import previous_prime, next_prime, CRT_list def max_det_prime(n): """ diff --git a/src/sage/matrix/matrix_integer_dense_saturation.py b/src/sage/matrix/matrix_integer_dense_saturation.py index 9dd83a6345e..bad47770101 100644 --- a/src/sage/matrix/matrix_integer_dense_saturation.py +++ b/src/sage/matrix/matrix_integer_dense_saturation.py @@ -2,8 +2,8 @@ Saturation over ZZ """ -from sage.rings.all import ZZ, gcd, GF -from sage.rings.arith import binomial +from sage.rings.all import ZZ, GF +from sage.arith.all import binomial, gcd from sage.matrix.constructor import identity_matrix, random_matrix from sage.misc.misc import verbose from sage.misc.randstate import current_randstate diff --git a/src/sage/matrix/matrix_misc.py b/src/sage/matrix/matrix_misc.py index dc094089c4d..41f01b0aee7 100644 --- a/src/sage/matrix/matrix_misc.py +++ b/src/sage/matrix/matrix_misc.py @@ -101,7 +101,7 @@ def row_reduced_form(M,transformation=False): # calculate least-common denominator of matrix entries and clear # denominators. The result lies in R - from sage.rings.arith import lcm + from sage.arith.all import lcm from sage.matrix.constructor import matrix from sage.misc.functional import numerator if R0 in _Fields: diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index 9104aaec5ff..b2d510726b8 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -103,7 +103,7 @@ from libc.stdio cimport snprintf from sage.modules.vector_modn_dense cimport Vector_modn_dense -from sage.rings.arith import is_prime +from sage.arith.all import is_prime from sage.structure.element cimport ModuleElement cimport matrix_dense diff --git a/src/sage/matrix/matrix_modn_sparse.pyx b/src/sage/matrix/matrix_modn_sparse.pyx index 5a2c14c8ffb..c56684cc324 100644 --- a/src/sage/matrix/matrix_modn_sparse.pyx +++ b/src/sage/matrix/matrix_modn_sparse.pyx @@ -91,7 +91,7 @@ from sage.misc.misc import verbose, get_verbose import sage.rings.all as rings from sage.matrix.matrix2 import Matrix as Matrix2 -from sage.rings.arith import is_prime +from sage.arith.all import is_prime from sage.structure.element import is_Vector diff --git a/src/sage/matrix/matrix_rational_dense.pyx b/src/sage/matrix/matrix_rational_dense.pyx index ad3ffe0ea26..717fd3ac525 100644 --- a/src/sage/matrix/matrix_rational_dense.pyx +++ b/src/sage/matrix/matrix_rational_dense.pyx @@ -75,7 +75,7 @@ from sage.rings.integer_ring import ZZ, is_IntegerRing from sage.rings.finite_rings.constructor import FiniteField as GF from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing from sage.rings.rational_field import QQ -from sage.rings.arith import gcd +from sage.arith.all import gcd from matrix2 import cmp_pivots, decomp_seq from matrix0 import Matrix as Matrix_base diff --git a/src/sage/matrix/misc.pyx b/src/sage/matrix/misc.pyx index be9d5eaafb3..bcaa939028c 100644 --- a/src/sage/matrix/misc.pyx +++ b/src/sage/matrix/misc.pyx @@ -35,7 +35,7 @@ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ from sage.rings.integer cimport Integer -from sage.rings.arith import previous_prime, CRT_basis +from sage.arith.all import previous_prime, CRT_basis from sage.rings.real_mpfr import is_RealField from sage.rings.real_mpfr cimport RealNumber diff --git a/src/sage/matroids/basis_matroid.pyx b/src/sage/matroids/basis_matroid.pyx index a582a88fa9d..fc7dc2e0b9e 100644 --- a/src/sage/matroids/basis_matroid.pyx +++ b/src/sage/matroids/basis_matroid.pyx @@ -75,7 +75,7 @@ include 'sage/data_structures/bitset.pxi' from matroid cimport Matroid from basis_exchange_matroid cimport BasisExchangeMatroid from itertools import permutations -from sage.rings.arith import binomial +from sage.arith.all import binomial from set_system cimport SetSystem from itertools import combinations diff --git a/src/sage/matroids/extension.pyx b/src/sage/matroids/extension.pyx index 39ad42b246c..1b4ccc65445 100644 --- a/src/sage/matroids/extension.pyx +++ b/src/sage/matroids/extension.pyx @@ -30,7 +30,7 @@ Methods #***************************************************************************** include 'sage/data_structures/bitset.pxi' from basis_matroid cimport BasisMatroid -from sage.rings.arith import binomial +from sage.arith.all import binomial cdef class CutNode: """ diff --git a/src/sage/misc/dev_tools.py b/src/sage/misc/dev_tools.py index 43c0b379170..2bdae9bedcd 100644 --- a/src/sage/misc/dev_tools.py +++ b/src/sage/misc/dev_tools.py @@ -395,7 +395,7 @@ def import_statements(*objects, **kwds): from sage.rings.integer_ring import Z sage: import_statements(euler_phi) - from sage.rings.arith import euler_phi + from sage.arith.misc import euler_phi sage: import_statements(x) from sage.calculus.predefined import x diff --git a/src/sage/misc/functional.py b/src/sage/misc/functional.py index 5920e8e9c7b..dbcd382f6b1 100644 --- a/src/sage/misc/functional.py +++ b/src/sage/misc/functional.py @@ -1575,7 +1575,7 @@ def squarefree_part(x): return x.squarefree_part() except AttributeError: pass - from sage.rings.arith import factor + from sage.arith.all import factor from sage.structure.all import parent F = factor(x) n = parent(x)(1) diff --git a/src/sage/misc/superseded.py b/src/sage/misc/superseded.py index c676ee410c2..f7a3234b958 100644 --- a/src/sage/misc/superseded.py +++ b/src/sage/misc/superseded.py @@ -517,10 +517,10 @@ def deprecated_callable_import(trac_number, module_name, globs, locs, fromlist, See http://trac.sagemath.org/13109 for details. True sage: del is_prime - sage: deprecated_callable_import(13109, 'sage.rings.arith', globals(), locals(), ['is_prime']) + sage: deprecated_callable_import(13109, 'sage.arith.all', globals(), locals(), ['is_prime']) sage: is_prime(3) doctest:...: DeprecationWarning: - Using is_prime from here is deprecated. If you need to use it, please import it directly from sage.rings.arith. + Using is_prime from here is deprecated. If you need to use it, please import it directly from sage.arith.all. See http://trac.sagemath.org/13109 for details. True """ diff --git a/src/sage/modular/abvar/abvar.py b/src/sage/modular/abvar/abvar.py index af44440e0e4..049c472de6b 100644 --- a/src/sage/modular/abvar/abvar.py +++ b/src/sage/modular/abvar/abvar.py @@ -20,11 +20,15 @@ True """ -########################################################################### -# Copyright (C) 2007 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) # -# http://www.gnu.org/licenses/ # -########################################################################### +#***************************************************************************** +# Copyright (C) 2007 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.categories.all import ModularAbelianVarieties from sage.structure.sequence import Sequence, Sequence_generic @@ -33,8 +37,8 @@ from torsion_subgroup import RationalTorsionSubgroup, QQbarTorsionSubgroup from finite_subgroup import (FiniteSubgroup_lattice, FiniteSubgroup, TorsionPoint) from cuspidal_subgroup import CuspidalSubgroup, RationalCuspidalSubgroup, RationalCuspSubgroup -from sage.rings.all import (ZZ, QQ, QQbar, LCM, - divisors, Integer, prime_range) +from sage.rings.all import ZZ, QQ, QQbar, Integer +from sage.arith.all import LCM, divisors, prime_range, next_prime from sage.rings.ring import is_Ring from sage.modules.free_module import is_FreeModule from sage.modular.arithgroup.all import is_CongruenceSubgroup, is_Gamma0, is_Gamma1, is_GammaH @@ -4513,7 +4517,6 @@ def sqrt_poly(f): #################################################################################################### # Useful for decomposing exactly the sort of modular symbols spaces that come up here. from random import randrange -from sage.rings.arith import next_prime def random_hecke_operator(M, t=None, p=2): """ diff --git a/src/sage/modular/abvar/cuspidal_subgroup.py b/src/sage/modular/abvar/cuspidal_subgroup.py index d1ca1f30d26..f41fd828664 100644 --- a/src/sage/modular/abvar/cuspidal_subgroup.py +++ b/src/sage/modular/abvar/cuspidal_subgroup.py @@ -59,17 +59,22 @@ True """ -########################################################################### -# Copyright (C) 2007 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) # -# http://www.gnu.org/licenses/ # -########################################################################### +#***************************************************************************** +# Copyright (C) 2007 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from finite_subgroup import FiniteSubgroup -from sage.rings.all import infinity, QQ, gcd, ZZ +from sage.rings.all import infinity, QQ, ZZ from sage.matrix.all import matrix from sage.modular.arithgroup.all import is_Gamma0 from sage.modular.cusps import Cusp +from sage.arith.all import gcd class CuspidalSubgroup_generic(FiniteSubgroup): def _compute_lattice(self, rational_only=False, rational_subgroup=False): diff --git a/src/sage/modular/abvar/finite_subgroup.py b/src/sage/modular/abvar/finite_subgroup.py index 161849b88bd..ea3030ff88a 100644 --- a/src/sage/modular/abvar/finite_subgroup.py +++ b/src/sage/modular/abvar/finite_subgroup.py @@ -87,11 +87,15 @@ True """ -########################################################################### -# Copyright (C) 2007 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) # -# http://www.gnu.org/licenses/ # -########################################################################### +#***************************************************************************** +# Copyright (C) 2007 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.modular.abvar.torsion_point import TorsionPoint from sage.modules.module import Module @@ -99,8 +103,10 @@ from sage.structure.element import ModuleElement from sage.structure.gens_py import abelian_iterator from sage.structure.sequence import Sequence -from sage.rings.all import gcd, lcm, QQ, ZZ, QQbar, Integer, composite_field +from sage.rings.all import QQ, ZZ, QQbar, Integer +from sage.arith.all import gcd, lcm from sage.misc.all import prod +from sage.structure.element import get_coercion_model import abvar as abelian_variety @@ -298,7 +304,7 @@ def __add__(self, other): B = other.abelian_variety() if not A.in_same_ambient_variety(B): raise ValueError("self and other must be in the same ambient Jacobian") - K = composite_field(self.field_of_definition(), other.field_of_definition()) + K = get_coercion_model().common_parent(self.field_of_definition(), other.field_of_definition()) lattice = self.lattice() + other.lattice() if A != B: lattice += C.lattice() @@ -384,7 +390,7 @@ def intersection(self, other): amb = other B = other M = B.lattice().scale(Integer(1)/self.exponent()) - K = composite_field(self.field_of_definition(), other.base_field()) + K = get_coercion_model().common_parent(self.field_of_definition(), other.base_field()) else: amb = A if not isinstance(other, FiniteSubgroup): @@ -393,7 +399,7 @@ def intersection(self, other): if A.ambient_variety() != B.ambient_variety(): raise TypeError("finite subgroups must be in the same ambient product Jacobian") M = other.lattice() - K = composite_field(self.field_of_definition(), other.field_of_definition()) + K = get_coercion_model().common_parent(self.field_of_definition(), other.field_of_definition()) L = self.lattice() if A != B: diff --git a/src/sage/modular/abvar/torsion_subgroup.py b/src/sage/modular/abvar/torsion_subgroup.py index 0461f4edae9..f051a1e6f78 100644 --- a/src/sage/modular/abvar/torsion_subgroup.py +++ b/src/sage/modular/abvar/torsion_subgroup.py @@ -78,18 +78,23 @@ True """ -########################################################################### -# Copyright (C) 2007 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) # -# http://www.gnu.org/licenses/ # -########################################################################### +#***************************************************************************** +# Copyright (C) 2007 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** from sage.modular.abvar.torsion_point import TorsionPoint from sage.modules.module import Module from finite_subgroup import FiniteSubgroup -from sage.rings.all import divisors, gcd, ZZ, prime_range +from sage.rings.all import ZZ from sage.sets.primes import Primes from sage.modular.arithgroup.all import is_Gamma0 +from sage.arith.all import divisors, gcd, prime_range class RationalTorsionSubgroup(FiniteSubgroup): """ diff --git a/src/sage/modular/arithgroup/arithgroup_generic.py b/src/sage/modular/arithgroup/arithgroup_generic.py index 51f79004585..153268152e3 100644 --- a/src/sage/modular/arithgroup/arithgroup_generic.py +++ b/src/sage/modular/arithgroup/arithgroup_generic.py @@ -17,7 +17,7 @@ import sage.groups.old as group from sage.rings.all import ZZ -import sage.rings.arith as arith +import sage.arith.all as arith from sage.misc.cachefunc import cached_method from copy import copy # for making copies of lists of cusps from sage.modular.modsym.p1list import lift_to_sl2z diff --git a/src/sage/modular/arithgroup/arithgroup_perm.py b/src/sage/modular/arithgroup/arithgroup_perm.py index 8b46b784045..efa7a120239 100644 --- a/src/sage/modular/arithgroup/arithgroup_perm.py +++ b/src/sage/modular/arithgroup/arithgroup_perm.py @@ -131,7 +131,7 @@ from sage.rings.all import ZZ from sage.misc.cachefunc import cached_method from sage.misc.misc import verbose -import sage.rings.arith as arith +import sage.arith.all as arith from sage.groups.perm_gps.permgroup_element import PermutationGroupElement diff --git a/src/sage/modular/arithgroup/congroup_gamma.py b/src/sage/modular/arithgroup/congroup_gamma.py index fd82443a00c..cc2e7be1d25 100644 --- a/src/sage/modular/arithgroup/congroup_gamma.py +++ b/src/sage/modular/arithgroup/congroup_gamma.py @@ -2,25 +2,23 @@ Congruence Subgroup `\Gamma(N)` """ -################################################################################ -# -# Copyright (C) 2009, The Sage Group -- http://www.sagemath.org/ -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# The full text of the GPL is available at: -# +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -# -################################################################################ +#***************************************************************************** + from congroup_generic import CongruenceSubgroup from sage.misc.all import prod -from sage.rings.all import ZZ, Zmod, gcd, QQ +from sage.rings.all import ZZ, Zmod, QQ from sage.rings.integer import GCD_list from sage.groups.matrix_gps.finitely_generated import MatrixGroup from sage.matrix.constructor import matrix from sage.modular.cusps import Cusp +from sage.arith.all import gcd from congroup_sl2z import SL2Z diff --git a/src/sage/modular/arithgroup/congroup_gamma0.py b/src/sage/modular/arithgroup/congroup_gamma0.py index 079ac2b8c45..2ccd2c60eee 100644 --- a/src/sage/modular/arithgroup/congroup_gamma0.py +++ b/src/sage/modular/arithgroup/congroup_gamma0.py @@ -2,17 +2,13 @@ Congruence Subgroup `\Gamma_0(N)` """ -################################################################################ -# -# Copyright (C) 2009, The Sage Group -- http://www.sagemath.org/ -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# The full text of the GPL is available at: -# +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -# -################################################################################ +#***************************************************************************** from congroup_gammaH import GammaH_class from congroup_gamma1 import is_Gamma1 @@ -21,10 +17,11 @@ from sage.modular.cusps import Cusp from sage.misc.cachefunc import cached_method -from sage.rings.all import (IntegerModRing, kronecker_symbol, ZZ) +from sage.rings.all import IntegerModRing, ZZ +from sage.arith.all import kronecker_symbol from sage.misc.all import prod import sage.modular.modsym.p1list -import sage.rings.arith as arith +import sage.arith.all as arith def is_Gamma0(x): diff --git a/src/sage/modular/arithgroup/congroup_gamma1.py b/src/sage/modular/arithgroup/congroup_gamma1.py index c20d8fa829d..8217285bbf0 100644 --- a/src/sage/modular/arithgroup/congroup_gamma1.py +++ b/src/sage/modular/arithgroup/congroup_gamma1.py @@ -2,23 +2,21 @@ Congruence Subgroup `\Gamma_1(N)` """ -################################################################################ -# -# Copyright (C) 2009, The Sage Group -- http://www.sagemath.org/ -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# The full text of the GPL is available at: -# +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -# -################################################################################ +#***************************************************************************** + from sage.misc.cachefunc import cached_method from sage.misc.all import prod from congroup_gammaH import GammaH_class, is_GammaH, GammaH_constructor -from sage.rings.all import ZZ, euler_phi as phi, moebius, divisors +from sage.rings.all import ZZ +from sage.arith.all import euler_phi as phi, moebius, divisors from sage.modular.dirichlet import DirichletGroup diff --git a/src/sage/modular/arithgroup/congroup_gammaH.py b/src/sage/modular/arithgroup/congroup_gammaH.py index 388e48eec28..35a70502ccd 100644 --- a/src/sage/modular/arithgroup/congroup_gammaH.py +++ b/src/sage/modular/arithgroup/congroup_gammaH.py @@ -19,7 +19,7 @@ # ################################################################################ -from sage.rings.arith import euler_phi, lcm, gcd, divisors, get_inverse_mod, get_gcd, factor +from sage.arith.all import euler_phi, lcm, gcd, divisors, get_inverse_mod, get_gcd, factor from sage.modular.modsym.p1list import lift_to_sl2z from congroup_generic import CongruenceSubgroup from sage.modular.cusps import Cusp diff --git a/src/sage/modular/arithgroup/congroup_generic.py b/src/sage/modular/arithgroup/congroup_generic.py index 643c0530cec..afcb79b6152 100644 --- a/src/sage/modular/arithgroup/congroup_generic.py +++ b/src/sage/modular/arithgroup/congroup_generic.py @@ -23,7 +23,7 @@ ################################################################################ from sage.rings.all import QQ, ZZ, Zmod -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.sets.set import Set from sage.groups.matrix_gps.all import MatrixGroup from sage.matrix.matrix_space import MatrixSpace diff --git a/src/sage/modular/arithgroup/congroup_sl2z.py b/src/sage/modular/arithgroup/congroup_sl2z.py index 54d3e50e8ed..82da1d2cf53 100644 --- a/src/sage/modular/arithgroup/congroup_sl2z.py +++ b/src/sage/modular/arithgroup/congroup_sl2z.py @@ -23,7 +23,7 @@ from arithgroup_element import ArithmeticSubgroupElement from sage.rings.integer_ring import ZZ from sage.modular.cusps import Cusp -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.modular.modsym.p1list import lift_to_sl2z def is_SL2Z(x): diff --git a/src/sage/modular/arithgroup/farey_symbol.pyx b/src/sage/modular/arithgroup/farey_symbol.pyx index 1f8dd7b3d51..bfc1c699c01 100644 --- a/src/sage/modular/arithgroup/farey_symbol.pyx +++ b/src/sage/modular/arithgroup/farey_symbol.pyx @@ -24,7 +24,6 @@ include 'sage/ext/interrupt.pxi' include 'sage/ext/cdefs.pxi' from .farey cimport * -import sage.rings.arith from sage.rings.all import CC, RR from sage.rings.integer cimport Integer from sage.rings.infinity import infinity diff --git a/src/sage/modular/cusps_nf.py b/src/sage/modular/cusps_nf.py index b8f9ccdd105..5b9c6a35343 100644 --- a/src/sage/modular/cusps_nf.py +++ b/src/sage/modular/cusps_nf.py @@ -1152,7 +1152,7 @@ def Gamma0_NFCusps(N): g = (A*B).gens_reduced()[0] #for every divisor of N we have to find cusps - from sage.rings.arith import divisors + from sage.arith.all import divisors for d in divisors(N): #find delta prime coprime to B in inverse class of d*A #by searching in our list of auxiliary prime ideals @@ -1207,7 +1207,7 @@ def number_of_Gamma0_NFCusps(N): """ k = N.number_field() # The number of Gamma0(N)-sub-orbits for each Gamma-orbit: - from sage.rings.arith import divisors + from sage.arith.all import divisors s = sum([len(list((d+N/d).invertible_residues_mod(k.unit_group().gens()))) \ for d in divisors(N)]) # There are h Gamma-orbits, with h class number of underlying number field. diff --git a/src/sage/modular/dims.py b/src/sage/modular/dims.py index 0bcac125b44..b931afeb550 100644 --- a/src/sage/modular/dims.py +++ b/src/sage/modular/dims.py @@ -30,19 +30,18 @@ classes. """ -########################################################################## -# Copyright (C) 2004,2005,2006,2007,2008 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# The full text of the GPL is available at: +#***************************************************************************** +# Copyright (C) 2004-2008 William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -########################################################################## - +#***************************************************************************** -from sage.rings.arith import (factor, is_prime, - valuation, kronecker_symbol, gcd, euler_phi, lcm) +from sage.arith.all import (factor, is_prime, valuation, kronecker_symbol, + gcd, euler_phi, lcm) from sage.misc.all import prod as mul from sage.rings.all import Mod, Integer, IntegerModRing, ZZ diff --git a/src/sage/modular/dirichlet.py b/src/sage/modular/dirichlet.py index e36790a7f3a..12d2115784a 100644 --- a/src/sage/modular/dirichlet.py +++ b/src/sage/modular/dirichlet.py @@ -46,16 +46,16 @@ """ -######################################################################## -# Copyright (C) 2004,2005,2006 William Stein -# 2014 Julian Rueth -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# The full text of the GPL is available at: +#***************************************************************************** +# Copyright (C) 2004-2006 William Stein +# Copyright (C) 2014 Julian Rueth # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################## +#***************************************************************************** import sage.categories.all as cat from sage.misc.all import prod @@ -63,7 +63,6 @@ import sage.modules.free_module as free_module import sage.modules.free_module_element as free_module_element import sage.rings.all as rings -import sage.rings.arith as arith import sage.rings.number_field.number_field as number_field from sage.categories.map import Map @@ -74,12 +73,13 @@ from sage.misc.cachefunc import cached_method from sage.misc.fast_methods import WithEqualityById -from sage.rings.arith import binomial, bernoulli from sage.structure.element import MultiplicativeGroupElement from sage.structure.gens_py import multiplicative_iterator from sage.structure.parent import Parent from sage.structure.sequence import Sequence from sage.structure.factory import UniqueFactory +from sage.arith.all import (binomial, bernoulli, kronecker, factor, gcd, + lcm, fundamental_discriminant, euler_phi, factorial, valuation) def trivial_character(N, base_ring=rings.RationalField()): r""" @@ -125,9 +125,9 @@ def kronecker_character(d): if d == 0: raise ValueError("d must be nonzero") - D = arith.fundamental_discriminant(d) + D = fundamental_discriminant(d) G = DirichletGroup(abs(D), rings.RationalField()) - return G([arith.kronecker(D,u) for u in G.unit_gens()]) + return G([kronecker(D,u) for u in G.unit_gens()]) def kronecker_character_upside_down(d): @@ -149,7 +149,7 @@ def kronecker_character_upside_down(d): raise ValueError("d must be positive") G = DirichletGroup(d, rings.RationalField()) - return G([arith.kronecker(u.lift(),d) for u in G.unit_gens()]) + return G([kronecker(u.lift(),d) for u in G.unit_gens()]) def is_DirichletCharacter(x): @@ -292,7 +292,7 @@ def __eval_at_minus_one(self): if e.modulus() % 2 == 0: if e.modulus() % 4 == 0: val *= e.values_on_gens()[0] # first gen is -1 for 2-power modulus - elif (arith.euler_phi(e.parent().modulus()) / e.order()) % 2 != 0: + elif (euler_phi(e.parent().modulus()) / e.order()) % 2 != 0: val *= -1 return val @@ -704,7 +704,7 @@ def bernoulli(self, k, algorithm='recurrence', cache=True, **opts): g = t/((N*t).exp(prec) - 1) # h(n) = g(t)*e^{nt} h = [0] + [g * ((n*t).exp(prec)) for n in range(1,N+1)] - ber = sum([self(a)*h[a][k] for a in range(1,N+1)]) * arith.factorial(k) + ber = sum([self(a)*h[a][k] for a in range(1,N+1)]) * factorial(k) else: raise ValueError("algorithm = '%s' unknown"%algorithm) @@ -735,7 +735,7 @@ def conductor(self): """ if self.modulus() == 1 or self.is_trivial(): return rings.Integer(1) - F = arith.factor(self.modulus()) + F = factor(self.modulus()) if len(F) > 1: return prod([d.conductor() for d in self.decomposition()]) p = F[0][0] @@ -745,7 +745,7 @@ def conductor(self): # depends only on the factor of p**(r-1) on the right hand side. # Since p-1 is coprime to p, this smallest r such that the # divisibility holds equals Valuation(Order(x),p)+1. - cond = p**(arith.valuation(self.order(),p) + 1) + cond = p**(valuation(self.order(),p) + 1) if p == 2 and F[0][1] > 2 and self.values_on_gens()[1].multiplicative_order() != 1: cond *= 2; return rings.Integer(cond) @@ -940,7 +940,7 @@ def gauss_sum(self, a=1): zeta = L.zeta(m) elif number_field.is_CyclotomicField(K) or is_RationalField(K): chi = chi.minimize_base_ring() - n = arith.lcm(m, G.zeta_order()) + n = lcm(m, G.zeta_order()) L = rings.CyclotomicField(n) zeta = L.gen(0) ** (n // m) else: @@ -1175,12 +1175,12 @@ def kloosterman_sum(self, a=1,b=0): raise NotImplementedError("Kloosterman sums only currently implemented when the base ring is a cyclotomic field or QQ.") g = 0 m = G.modulus() - L = rings.CyclotomicField(arith.lcm(m,G.zeta_order())) + L = rings.CyclotomicField(lcm(m,G.zeta_order())) zeta = L.gen(0) n = zeta.multiplicative_order() zeta = zeta ** (n // m) for c in range(1,m): - if arith.gcd(c,m)==1: + if gcd(c,m)==1: e = rings.Mod(c,m) z = zeta ** int(a*e + b*(e**(-1))) g += self(c)*z @@ -1227,7 +1227,7 @@ def kloosterman_sum_numerical(self, prec=53, a=1,b=0): zeta = CC.zeta(m) for c in range(1,m): - if arith.gcd(c,m)==1: + if gcd(c,m)==1: e = rings.Mod(c,m) z = zeta ** int(a*e + b*(e**(-1))) g += phi(self(c))*z @@ -1412,7 +1412,7 @@ def maximize_base_ring(self): g = 2 z = self.base_ring().zeta() n = z.multiplicative_order() - m = arith.LCM(g,n) + m = lcm(g,n) if n == m: return self K = rings.CyclotomicField(m) @@ -1470,7 +1470,7 @@ def minimize_base_ring(self): elif self.order() <= 2: K = rings.QQ elif (isinstance(R, number_field.NumberField_generic) - and arith.euler_phi(self.order()) < R.absolute_degree()): + and euler_phi(self.order()) < R.absolute_degree()): K = rings.CyclotomicField(self.order()) else: return self @@ -2001,7 +2001,7 @@ def __init__(self, modulus, zeta, zeta_order): if is_ComplexField(base_ring): for i in range(1, self._zeta_order): a = a * zeta - a._set_multiplicative_order(zeta_order/arith.GCD(zeta_order, i)) + a._set_multiplicative_order(zeta_order/gcd(zeta_order, i)) v[a] = i w.append(a) else: @@ -2223,7 +2223,7 @@ def decomposition(self): """ R = self.base_ring() return Sequence([DirichletGroup(p**r,R) for p, r \ - in arith.factor(self.modulus())], + in factor(self.modulus())], cr=True, universe = cat.Objects()) @@ -2271,7 +2271,7 @@ def _automorphisms(self): R = self.base_ring() p = R.characteristic() if p == 0: - Auts = [e for e in xrange(1,n) if arith.GCD(e,n) == 1] + Auts = [e for e in xrange(1,n) if gcd(e,n) == 1] else: if not rings.ZZ(p).is_prime(): raise NotImplementedError("Automorphisms for finite non-field base rings not implemented") @@ -2392,7 +2392,7 @@ def gens(self): orders = self.integers_mod().unit_group().gens_orders() for i in range(len(self.unit_gens())): z = zero.__copy__() - z[i] = ord//arith.GCD(ord, orders[i]) + z[i] = ord//gcd(ord, orders[i]) g.append(self.element_class(self, z, check=False)) return tuple(g) diff --git a/src/sage/modular/etaproducts.py b/src/sage/modular/etaproducts.py index 09ccdd36d0c..c30066cbf3f 100644 --- a/src/sage/modular/etaproducts.py +++ b/src/sage/modular/etaproducts.py @@ -31,7 +31,7 @@ from sage.structure.sage_object import SageObject from sage.rings.power_series_ring import PowerSeriesRing from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing -from sage.rings.arith import divisors, prime_divisors, is_square, euler_phi, gcd +from sage.arith.all import divisors, prime_divisors, is_square, euler_phi, gcd from sage.rings.all import Integer, IntegerRing, RationalField from sage.groups.old import AbelianGroup from sage.structure.element import MultiplicativeGroupElement diff --git a/src/sage/modular/hecke/algebra.py b/src/sage/modular/hecke/algebra.py index e082f0ca84d..40afcea5436 100644 --- a/src/sage/modular/hecke/algebra.py +++ b/src/sage/modular/hecke/algebra.py @@ -30,14 +30,14 @@ import weakref -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.infinity import sage.misc.latex as latex import module import hecke_operator import sage.rings.commutative_algebra from sage.matrix.constructor import matrix -from sage.rings.arith import lcm +from sage.arith.all import lcm from sage.matrix.matrix_space import MatrixSpace from sage.rings.all import ZZ, QQ from sage.structure.element import Element diff --git a/src/sage/modular/hecke/ambient_module.py b/src/sage/modular/hecke/ambient_module.py index fce23f22343..3ad55281cfd 100644 --- a/src/sage/modular/hecke/ambient_module.py +++ b/src/sage/modular/hecke/ambient_module.py @@ -29,7 +29,7 @@ import sage.misc.misc as misc -import sage.rings.arith as arith +import sage.arith.all as arith import sage.matrix.matrix_space as matrix_space from sage.matrix.constructor import matrix diff --git a/src/sage/modular/hecke/hecke_operator.py b/src/sage/modular/hecke/hecke_operator.py index 561373f2877..7fa4aa77bfc 100644 --- a/src/sage/modular/hecke/hecke_operator.py +++ b/src/sage/modular/hecke/hecke_operator.py @@ -21,7 +21,7 @@ import sage.algebras.algebra_element from sage.categories.homset import End -import sage.rings.arith as arith +import sage.arith.all as arith from sage.rings.integer import Integer import algebra diff --git a/src/sage/modular/hecke/module.py b/src/sage/modular/hecke/module.py index 81b61fde28e..5c86ae3b727 100644 --- a/src/sage/modular/hecke/module.py +++ b/src/sage/modular/hecke/module.py @@ -19,7 +19,7 @@ import sage.rings.all from sage.rings.commutative_ring import is_CommutativeRing -import sage.rings.arith as arith +import sage.arith.all as arith import sage.misc.misc as misc import sage.modules.module from sage.structure.all import Sequence diff --git a/src/sage/modular/hecke/submodule.py b/src/sage/modular/hecke/submodule.py index eadc3815c5a..f9d6f108325 100644 --- a/src/sage/modular/hecke/submodule.py +++ b/src/sage/modular/hecke/submodule.py @@ -19,7 +19,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** -import sage.rings.arith as arith +import sage.arith.all as arith import sage.misc.misc as misc from sage.misc.cachefunc import cached_method diff --git a/src/sage/modular/local_comp/liftings.py b/src/sage/modular/local_comp/liftings.py index 5a2d8c24902..a56a6ddd983 100644 --- a/src/sage/modular/local_comp/liftings.py +++ b/src/sage/modular/local_comp/liftings.py @@ -7,7 +7,7 @@ """ from sage.rings.all import ZZ, Zmod -from sage.rings.arith import crt, inverse_mod +from sage.arith.all import crt, inverse_mod from sage.modular.modsym.p1list import lift_to_sl2z def lift_to_gamma1(g, m, n): diff --git a/src/sage/modular/local_comp/local_comp.py b/src/sage/modular/local_comp/local_comp.py index 29ba601f4f3..2b578c4a64b 100644 --- a/src/sage/modular/local_comp/local_comp.py +++ b/src/sage/modular/local_comp/local_comp.py @@ -262,7 +262,7 @@ def central_character(self): sage: LocalComponent(Newforms(DirichletGroup(24)([1, -1,-1]), 3, names='a')[0], 2).central_character() Character of Q_2*, of level 3, mapping 7 |--> 1, 5 |--> -1, 2 |--> -2 """ - from sage.rings.arith import crt + from sage.arith.all import crt chi = self.newform().character() f = self.prime() ** self.conductor() N = self.newform().level() // f diff --git a/src/sage/modular/local_comp/smoothchar.py b/src/sage/modular/local_comp/smoothchar.py index 1f8acde598c..540fa1db8c6 100644 --- a/src/sage/modular/local_comp/smoothchar.py +++ b/src/sage/modular/local_comp/smoothchar.py @@ -142,7 +142,7 @@ def multiplicative_order(self): sage: G.character(0, [1]).multiplicative_order() 1 """ - from sage.rings.arith import lcm + from sage.arith.all import lcm from sage.rings.infinity import Infinity if self._values_on_gens[-1].multiplicative_order() == Infinity: return Infinity diff --git a/src/sage/modular/local_comp/type_space.py b/src/sage/modular/local_comp/type_space.py index 86a566cbcc4..6e21ab3fea8 100644 --- a/src/sage/modular/local_comp/type_space.py +++ b/src/sage/modular/local_comp/type_space.py @@ -22,7 +22,7 @@ from sage.modular.modsym.modsym import ModularSymbols from sage.rings.all import ZZ, Zmod, QQ from sage.rings.fast_arith import prime_range -from sage.rings.arith import crt +from sage.arith.all import crt from sage.structure.sage_object import SageObject from sage.matrix.constructor import matrix from sage.misc.cachefunc import cached_method, cached_function diff --git a/src/sage/modular/modform/ambient.py b/src/sage/modular/modform/ambient.py index a4e4f0cc416..0b359a7dcab 100644 --- a/src/sage/modular/modform/ambient.py +++ b/src/sage/modular/modform/ambient.py @@ -58,24 +58,23 @@ True """ -######################################################################### +#***************************************************************************** # Copyright (C) 2006 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### - -# system packages +#***************************************************************************** -# Sage packages -import sage.rings.all as rings import sage.modular.arithgroup.all as arithgroup import sage.modular.dirichlet as dirichlet import sage.modular.hecke.all as hecke import sage.modular.modsym.all as modsym import sage.modules.free_module as free_module import sage.rings.all as rings +from sage.arith.all import is_prime from sage.structure.sequence import Sequence @@ -692,7 +691,7 @@ def _dim_new_eisenstein(self): return self.__the_dim_new_eisenstein except AttributeError: if arithgroup.is_Gamma0(self.group()) and self.weight() == 2: - if rings.is_prime(self.level()): + if is_prime(self.level()): d = 1 else: d = 0 diff --git a/src/sage/modular/modform/eis_series.py b/src/sage/modular/modform/eis_series.py index a315bc8e248..a5283679311 100644 --- a/src/sage/modular/modform/eis_series.py +++ b/src/sage/modular/modform/eis_series.py @@ -3,25 +3,21 @@ Eisenstein Series """ -######################################################################### -# Copyright (C) 2004--2006 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) +#***************************************************************************** +# Copyright (C) 2004-2006 William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### +#***************************************************************************** import sage.misc.all as misc - import sage.modular.dirichlet as dirichlet - from sage.modular.arithgroup.congroup_gammaH import GammaH_class - -from sage.rings.all import Integer - -from sage.rings.all import (bernoulli, CyclotomicField, - ZZ, QQ, Integer, divisors, - LCM, is_squarefree) +from sage.rings.all import Integer, CyclotomicField, ZZ, QQ, Integer +from sage.arith.all import bernoulli, divisors, is_squarefree, lcm from sage.rings.finite_rings.constructor import is_FiniteField from sage.rings.power_series_ring import PowerSeriesRing from eis_series_cython import eisenstein_series_poly, Ek_ZZ @@ -187,7 +183,7 @@ def __common_minimal_basering(chi, psi): """ chi = chi.minimize_base_ring() psi = psi.minimize_base_ring() - n = LCM(chi.base_ring().zeta().multiplicative_order(),\ + n = lcm(chi.base_ring().zeta().multiplicative_order(), psi.base_ring().zeta().multiplicative_order()) if n <= 2: K = QQ diff --git a/src/sage/modular/modform/eis_series_cython.pyx b/src/sage/modular/modform/eis_series_cython.pyx index 2e95ab657f8..72d73052c24 100644 --- a/src/sage/modular/modform/eis_series_cython.pyx +++ b/src/sage/modular/modform/eis_series_cython.pyx @@ -7,7 +7,7 @@ include 'sage/ext/interrupt.pxi' from sage.rings.rational_field import QQ from sage.rings.power_series_ring import PowerSeriesRing from sage.rings.integer cimport Integer -from sage.rings.arith import primes, bernoulli +from sage.arith.all import primes, bernoulli from sage.rings.fast_arith cimport prime_range from cpython.list cimport PyList_GET_ITEM diff --git a/src/sage/modular/modform/eisenstein_submodule.py b/src/sage/modular/modform/eisenstein_submodule.py index 2aa44eb557f..3eaefe3c0c2 100644 --- a/src/sage/modular/modform/eisenstein_submodule.py +++ b/src/sage/modular/modform/eisenstein_submodule.py @@ -8,6 +8,8 @@ import sage.rings.all as rings from sage.categories.all import Objects from sage.matrix.all import Matrix +from sage.rings.all import CyclotomicField +from sage.arith.all import lcm, euler_phi import eis_series @@ -564,8 +566,6 @@ class EisensteinSubmodule_eps(EisensteinSubmodule_params): #raise NotImplementedError, "must restrict scalars down correctly." -from sage.rings.all import CyclotomicField, lcm, euler_phi - def cyclotomic_restriction(L,K): r""" Given two cyclotomic fields L and K, compute the compositum diff --git a/src/sage/modular/modform/element.py b/src/sage/modular/modform/element.py index 8f9f2ceb97e..c81c951071a 100644 --- a/src/sage/modular/modform/element.py +++ b/src/sage/modular/modform/element.py @@ -3,13 +3,15 @@ Elements of modular forms spaces """ -######################################################################### -# Copyright (C) 2004--2008 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) +#***************************************************************************** +# Copyright (C) 2004-2008 William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### +#***************************************************************************** import space import sage.modular.hecke.element as element @@ -25,7 +27,7 @@ from sage.misc.misc import verbose, sxrange from sage.modular.dirichlet import DirichletGroup from sage.misc.superseded import deprecated_function_alias -from sage.rings.arith import lcm, divisors, moebius +from sage.arith.all import lcm, divisors, moebius, sigma, factor from sage.structure.element import get_coercion_model @@ -1993,9 +1995,9 @@ def __compute_weight2_trivial_character(self, X): elif n == 0: v.append(F(t-1)/F(24)) else: - an = rings.sigma(n,1) - if n%t==0: - an -= t*rings.sigma(n/t,1) + an = sigma(n,1) + if n%t == 0: + an -= t * sigma(n/t,1) v.append(an) return v @@ -2186,8 +2188,5 @@ def new_level(self): [[60, 2], [60, 3], [60, 2], [60, 5], [60, 2], [60, 2], [60, 2], [60, 3], [60, 2], [60, 2], [60, 2]] """ if self.__chi.is_trivial() and self.__psi.is_trivial() and self.weight() == 2: - return rings.factor(self.__t)[0][0] + return factor(self.__t)[0][0] return self.L()*self.M() - - - diff --git a/src/sage/modular/modform/hecke_operator_on_qexp.py b/src/sage/modular/modform/hecke_operator_on_qexp.py index 080109dce6e..1ce4bb1f4ba 100644 --- a/src/sage/modular/modform/hecke_operator_on_qexp.py +++ b/src/sage/modular/modform/hecke_operator_on_qexp.py @@ -2,17 +2,19 @@ Hecke Operators on `q`-expansions """ -######################################################################### -# Copyright (C) 2004--2006 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) +#***************************************************************************** +# Copyright (C) 2004-2006 William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### +#***************************************************************************** from sage.modular.dirichlet import DirichletGroup, is_DirichletCharacter -from sage.rings.all import (divisors, gcd, ZZ, Integer, - Infinity, CyclotomicField) +from sage.rings.all import ZZ, Integer, Infinity, CyclotomicField +from sage.arith.all import divisors, gcd from sage.rings.power_series_ring_element import is_PowerSeries diff --git a/src/sage/modular/modform/l_series_gross_zagier_coeffs.pyx b/src/sage/modular/modform/l_series_gross_zagier_coeffs.pyx index 2eb5b37faf7..2020987ebdb 100644 --- a/src/sage/modular/modform/l_series_gross_zagier_coeffs.pyx +++ b/src/sage/modular/modform/l_series_gross_zagier_coeffs.pyx @@ -5,7 +5,7 @@ from sage.rings.fast_arith cimport arith_llong cdef arith_llong arith = arith_llong() from sage.rings.all import ZZ, PowerSeriesRing -from sage.rings.arith import kronecker_symbol +from sage.arith.all import kronecker_symbol from libc.math cimport ceil, floor, sqrt from libc.string cimport memcpy diff --git a/src/sage/modular/modform/numerical.py b/src/sage/modular/modform/numerical.py index 1116a42439d..42dec4af9d0 100644 --- a/src/sage/modular/modform/numerical.py +++ b/src/sage/modular/modform/numerical.py @@ -2,13 +2,15 @@ Numerical computation of newforms """ -######################################################################### -# Copyright (C) 2004--2006 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) +#***************************************************************************** +# Copyright (C) 2004-2006 William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### +#***************************************************************************** from sage.structure.sage_object import SageObject from sage.structure.sequence import Sequence @@ -16,7 +18,8 @@ from sage.modular.arithgroup.all import Gamma0 from sage.modules.all import vector from sage.misc.misc import verbose -from sage.rings.all import CDF, Integer, QQ, next_prime, prime_range +from sage.rings.all import CDF, Integer, QQ +from sage.arith.all import next_prime, prime_range from sage.misc.prandom import randint from sage.matrix.constructor import matrix diff --git a/src/sage/modular/modform/space.py b/src/sage/modular/modform/space.py index 75e5580c681..86f4b854b5b 100644 --- a/src/sage/modular/modform/space.py +++ b/src/sage/modular/modform/space.py @@ -70,7 +70,7 @@ import sage.modular.modform.constructor from sage.matrix.constructor import zero_matrix -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.rings.infinity import PlusInfinity WARN=False diff --git a/src/sage/modular/modform_hecketriangle/hecke_triangle_groups.py b/src/sage/modular/modform_hecketriangle/hecke_triangle_groups.py index c4a9a1db275..c8f82071c33 100644 --- a/src/sage/modular/modform_hecketriangle/hecke_triangle_groups.py +++ b/src/sage/modular/modform_hecketriangle/hecke_triangle_groups.py @@ -958,7 +958,7 @@ def _conjugacy_representatives(self, max_block_length=ZZ(0), D=None): from sage.combinat.partition import OrderedPartitions from sage.combinat.combinat import tuples - from sage.rings.arith import divisors + from sage.arith.all import divisors if not D is None: max_block_length = max(coerce_AA(0), coerce_AA((D + 4)/(self.lam()**2))).sqrt().floor() diff --git a/src/sage/modular/modform_hecketriangle/series_constructor.py b/src/sage/modular/modform_hecketriangle/series_constructor.py index 827bf07b4b2..7c555de11a6 100644 --- a/src/sage/modular/modform_hecketriangle/series_constructor.py +++ b/src/sage/modular/modform_hecketriangle/series_constructor.py @@ -20,10 +20,10 @@ # http://www.gnu.org/licenses/ #***************************************************************************** -from sage.rings.all import ZZ, QQ, infinity, rising_factorial, PolynomialRing, LaurentSeries, PowerSeriesRing, FractionField +from sage.rings.all import ZZ, QQ, infinity, PolynomialRing, LaurentSeries, PowerSeriesRing, FractionField from sage.rings.big_oh import O from sage.functions.all import exp -from sage.rings.arith import bernoulli, sigma +from sage.arith.all import bernoulli, sigma, rising_factorial from sage.structure.sage_object import SageObject from sage.structure.unique_representation import UniqueRepresentation diff --git a/src/sage/modular/modsym/ambient.py b/src/sage/modular/modsym/ambient.py index 7d70b346060..de251835e33 100644 --- a/src/sage/modular/modsym/ambient.py +++ b/src/sage/modular/modsym/ambient.py @@ -88,7 +88,7 @@ class ``ModularSymbolsAmbient``, derived from import sage.rings.rational_field as rational_field import sage.rings.integer_ring as integer_ring import sage.rings.all as rings -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.polynomial.multi_polynomial_element import sage.structure.formal_sum as formal_sum import sage.categories.all as cat @@ -2122,7 +2122,7 @@ def twisted_winding_element(self, i, eps): m = eps.modulus() s = self(0) - for a in ([ x for x in range(1,m) if rings.gcd(x,m) == 1 ]): + for a in ([ x for x in range(1,m) if arith.gcd(x,m) == 1 ]): s += eps(a) * self.modular_symbol([i, Cusp(0), Cusp(a/m)]) return s diff --git a/src/sage/modular/modsym/boundary.py b/src/sage/modular/modsym/boundary.py index 6f95f619b45..26edb8bd4a0 100644 --- a/src/sage/modular/modsym/boundary.py +++ b/src/sage/modular/modsym/boundary.py @@ -112,7 +112,7 @@ from sage.modular.modsym.manin_symbol import ManinSymbol import sage.rings.all as rings -import sage.rings.arith as arith +import sage.arith.all as arith import ambient import element diff --git a/src/sage/modular/modsym/g1list.py b/src/sage/modular/modsym/g1list.py index e77b82fd5b9..bee294986bf 100644 --- a/src/sage/modular/modsym/g1list.py +++ b/src/sage/modular/modsym/g1list.py @@ -19,7 +19,7 @@ # http://www.gnu.org/licenses/ #***************************************************************************** -import sage.rings.arith as arith +import sage.arith.all as arith class G1list: r""" diff --git a/src/sage/modular/modsym/hecke_operator.py b/src/sage/modular/modsym/hecke_operator.py index 2c62d6b2aa5..31a34411957 100644 --- a/src/sage/modular/modsym/hecke_operator.py +++ b/src/sage/modular/modsym/hecke_operator.py @@ -10,7 +10,7 @@ import sage.modular.hecke.hecke_operator -from sage.rings.arith import is_prime +from sage.arith.all import is_prime import heilbronn class HeckeOperator(sage.modular.hecke.hecke_operator.HeckeOperator): diff --git a/src/sage/modular/modsym/heilbronn.pyx b/src/sage/modular/modsym/heilbronn.pyx index 3edb94b5b8d..90f2c70e47c 100644 --- a/src/sage/modular/modsym/heilbronn.pyx +++ b/src/sage/modular/modsym/heilbronn.pyx @@ -17,7 +17,7 @@ Heilbronn matrix computation # http://www.gnu.org/licenses/ #***************************************************************************** -import sage.rings.arith +import sage.arith.all import sage.misc.misc @@ -312,7 +312,7 @@ cdef class HeilbronnCremona(Heilbronn): [3, -1, 0, 1], [-1, 0, 1, -3]] """ - if p <= 1 or not sage.rings.arith.is_prime(p): + if p <= 1 or not sage.arith.all.is_prime(p): raise ValueError, "p must be >= 2 and prime" self.p = p self._initialize_list() @@ -559,7 +559,7 @@ def hecke_images_gamma0_weight2(int u, int v, int N, indices, R): level=1, caller_name='hecke_images_gamma0_weight2') for i, n in enumerate(indices): # List the Heilbronn matrices of determinant n defined by Cremona or Merel - H = HeilbronnCremona(n) if sage.rings.arith.is_prime(n) else HeilbronnMerel(n) + H = HeilbronnCremona(n) if sage.arith.all.is_prime(n) else HeilbronnMerel(n) # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sage_malloc(sizeof(int)*H.length) @@ -694,7 +694,7 @@ def hecke_images_nonquad_character_weight2(int u, int v, int N, indices, chi, R) chi_vals = matrix(QQ, z).transpose() for i, n in enumerate(indices): - H = HeilbronnCremona(n) if sage.rings.arith.is_prime(n) else HeilbronnMerel(n) + H = HeilbronnCremona(n) if sage.arith.all.is_prime(n) else HeilbronnMerel(n) # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sage_malloc(sizeof(int)*H.length) @@ -791,7 +791,7 @@ def hecke_images_quad_character_weight2(int u, int v, int N, indices, chi, R): chi_vals[i] = _chivals[i] for i, n in enumerate(indices): - H = HeilbronnCremona(n) if sage.rings.arith.is_prime(n) else HeilbronnMerel(n) + H = HeilbronnCremona(n) if sage.arith.all.is_prime(n) else HeilbronnMerel(n) a = sage_malloc(sizeof(int)*H.length) if not a: raise MemoryError b = sage_malloc(sizeof(int)*H.length) @@ -880,7 +880,7 @@ def hecke_images_gamma0_weight_k(int u, int v, int i, int N, int k, indices, R): mpz_init(tmp) for z, m in enumerate(indices): - H = HeilbronnCremona(m) if sage.rings.arith.is_prime(m) else HeilbronnMerel(m) + H = HeilbronnCremona(m) if sage.arith.all.is_prime(m) else HeilbronnMerel(m) # Allocate memory to hold images of (u,v) under all Heilbronn matrices a = sage_malloc(sizeof(int)*H.length) diff --git a/src/sage/modular/modsym/manin_symbol_list.py b/src/sage/modular/modsym/manin_symbol_list.py index 3991119d768..e986367abf5 100644 --- a/src/sage/modular/modsym/manin_symbol_list.py +++ b/src/sage/modular/modsym/manin_symbol_list.py @@ -39,7 +39,7 @@ import sage.modular.modsym.p1list as p1list import sage.modular.modsym.g1list as g1list import sage.modular.modsym.ghlist as ghlist -from sage.rings.arith import xgcd, gcd +from sage.arith.all import xgcd, gcd from sage.rings.all import Integer from sage.structure.parent import Parent from sage.structure.sage_object import register_unpickle_override diff --git a/src/sage/modular/modsym/p1list_nf.py b/src/sage/modular/modsym/p1list_nf.py index 9f399750da3..9bf9d1952c4 100644 --- a/src/sage/modular/modsym/p1list_nf.py +++ b/src/sage/modular/modsym/p1list_nf.py @@ -992,7 +992,7 @@ def p1NFlist(N): #N.residues() = iterator through the residues mod N L = L+[MSymbol(N, k(1), r, check=False) for r in N.residues()] - from sage.rings.arith import divisors + from sage.arith.all import divisors for D in divisors(N): if not D.is_trivial() and D!=N: #we find Dp ideal coprime to N, in inverse class to D diff --git a/src/sage/modular/modsym/space.py b/src/sage/modular/modsym/space.py index 7faa104c390..082ad745037 100644 --- a/src/sage/modular/modsym/space.py +++ b/src/sage/modular/modsym/space.py @@ -28,7 +28,7 @@ from sage.modules.free_module_element import is_FreeModuleElement import sage.misc.all as misc import sage.modular.hecke.all as hecke -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.fast_arith as fast_arith from sage.rings.all import PowerSeriesRing, Integer, O, QQ, ZZ, infinity, Zmod from sage.rings.number_field.number_field_base import is_NumberField diff --git a/src/sage/modular/overconvergent/hecke_series.py b/src/sage/modular/overconvergent/hecke_series.py index c3838a1f57a..3ec5e47d435 100644 --- a/src/sage/modular/overconvergent/hecke_series.py +++ b/src/sage/modular/overconvergent/hecke_series.py @@ -64,16 +64,20 @@ forms", LMS J. of Comput. Math. 14 (2011), 214-231. """ -######################################################################### + +#***************************************************************************** # Copyright (C) 2011 Alan Lauder # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -######################################################################### +#***************************************************************************** from sage.functions.all import floor, ceil -from sage.rings.all import ZZ,Zmod,valuation,Infinity,Integer +from sage.arith.all import valuation +from sage.rings.all import ZZ, Zmod, Infinity, Integer from sage.rings.finite_rings.constructor import GF from sage.modular.modform.all import ModularForms, ModularFormsRing, delta_qexp, eisenstein_series_qexp from sage.modular.dims import dimension_modular_forms diff --git a/src/sage/modular/overconvergent/weightspace.py b/src/sage/modular/overconvergent/weightspace.py index a1f65f55d25..02251dc648b 100644 --- a/src/sage/modular/overconvergent/weightspace.py +++ b/src/sage/modular/overconvergent/weightspace.py @@ -57,14 +57,18 @@ # Copyright (C) 2008 William Stein # 2008-9 David Loeffler # -# Distributed under the terms of the GNU General Public License (GPL) +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** from sage.structure.parent_base import ParentWithBase from sage.structure.element import Element from sage.modular.dirichlet import DirichletGroup, trivial_character -from sage.rings.all import ZZ, QQ, divisors, IntegerModRing, Qp, Infinity +from sage.rings.all import ZZ, QQ, IntegerModRing, Qp, Infinity +from sage.arith.all import divisors from sage.rings.padics.padic_generic_element import pAdicGenericElement from sage.misc.misc import verbose from sage.misc.cachefunc import cached_method diff --git a/src/sage/modular/quatalg/brandt.py b/src/sage/modular/quatalg/brandt.py index 6aed0417dac..5e7f03e56fb 100644 --- a/src/sage/modular/quatalg/brandt.py +++ b/src/sage/modular/quatalg/brandt.py @@ -195,36 +195,27 @@ class if `I=aJ` for some `a \in A^*`. (Left `\mathcal{O}`-ideals are """ -################################################################################ -# Sage: Open Source Mathematical Software -# +#***************************************************************************** # Copyright (C) 2009 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -################################################################################ +#***************************************************************************** # imports -from sage.misc.all import prod, verbose -from sage.rings.all import (Integer, ZZ, QQ, prime_divisors, - kronecker, PolynomialRing, GF, next_prime, - lcm, gcd) +from sage.misc.all import prod, verbose +from sage.rings.all import Integer, ZZ, QQ, PolynomialRing, GF from sage.rings.commutative_ring import is_CommutativeRing from sage.algebras.quatalg.quaternion_algebra import QuaternionAlgebra, basis_for_quaternion_lattice from sage.algebras.quatalg.quaternion_algebra_cython import rational_matrix_from_rational_quaternions -from sage.rings.arith import gcd, factor, kronecker_symbol +from sage.arith.all import gcd, factor, prime_divisors, kronecker, next_prime, lcm from sage.modular.hecke.all import (AmbientHeckeModule, HeckeSubmodule, HeckeModuleElement) from sage.modular.dirichlet import TrivialCharacter from sage.matrix.all import MatrixSpace, matrix @@ -1458,7 +1449,7 @@ def quaternion_order_with_given_level(A, level): x = sum([int(v[i]+a)*B[i] for i in range(4)]) D = x.reduced_trace()**2 - 4 * x.reduced_norm() #x = O.random_element((-p/2).floor(), (p/2).ceil()) - if kronecker_symbol(D, p) == 1: break + if kronecker(D, p) == 1: break X = PolynomialRing(GF(p), 'x').gen() a = ZZ((X**2 - ZZ(x.reduced_trace()) * X + ZZ(x.reduced_norm())).roots()[0][0]) I = basis_for_left_ideal(O, [p**r, (x-a)**r] ) diff --git a/src/sage/modular/ssmod/ssmod.py b/src/sage/modular/ssmod/ssmod.py index a27931749dc..3a3684beba4 100644 --- a/src/sage/modular/ssmod/ssmod.py +++ b/src/sage/modular/ssmod/ssmod.py @@ -55,25 +55,23 @@ """ #***************************************************************************** -# Copyright (C) 2004,2006 William Stein +# Copyright (C) 2004, 2006 William Stein # Copyright (C) 2006 David Kohel # Copyright (C) 2006 Iftikhar Burhanuddin -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + import math import sage.modular.hecke.all as hecke import sage.rings.all as rings +from sage.arith.all import kronecker, next_prime from sage.matrix.matrix_space import MatrixSpace from sage.modular.arithgroup.all import Gamma0 from sage.libs.pari.all import pari @@ -286,7 +284,7 @@ def supersingular_D(prime): D = -1 while True: Dmod4 = rings.Mod(D,4) - if Dmod4 in (0,1) and (rings.kronecker(D,prime) != 1): + if Dmod4 in (0,1) and (kronecker(D,prime) != 1): return D D = D - 1 @@ -332,23 +330,23 @@ def supersingular_j(FF): raise ValueError("%s is not a prime"%prime) if not(rings.Integer(FF.cardinality())) == rings.Integer(prime**2): raise ValueError("%s is not a quadratic extension"%FF) - if rings.kronecker(-1, prime) != 1: + if kronecker(-1, prime) != 1: j_invss = 1728 #(2^2 * 3)^3 - elif rings.kronecker(-2, prime) != 1: + elif kronecker(-2, prime) != 1: j_invss = 8000 #(2^2 * 5)^3 - elif rings.kronecker(-3, prime) != 1: + elif kronecker(-3, prime) != 1: j_invss = 0 #0^3 - elif rings.kronecker(-7, prime) != 1: + elif kronecker(-7, prime) != 1: j_invss = 16581375 #(3 * 5 * 17)^3 - elif rings.kronecker(-11, prime) != 1: + elif kronecker(-11, prime) != 1: j_invss = -32768 #-(2^5)^3 - elif rings.kronecker(-19, prime) != 1: + elif kronecker(-19, prime) != 1: j_invss = -884736 #-(2^5 * 3)^3 - elif rings.kronecker(-43, prime) != 1: + elif kronecker(-43, prime) != 1: j_invss = -884736000 #-(2^6 * 3 * 5)^3 - elif rings.kronecker(-67, prime) != 1: + elif kronecker(-67, prime) != 1: j_invss = -147197952000 #-(2^5 * 3 * 5 * 11)^3 - elif rings.kronecker(-163, prime) != 1: + elif kronecker(-163, prime) != 1: j_invss = -262537412640768000 #-(2^6 * 3 * 5 * 23 * 29)^3 else: D = supersingular_D(prime) @@ -733,7 +731,7 @@ def upper_bound_on_elliptic_factors(self, p=None, ellmax=2): p = 997 while self.level() % p == 0: - p = rings.next_prime(p) + p = next_prime(p) ell = 2 t = self.hecke_matrix(ell).change_ring(rings.GF(p)) diff --git a/src/sage/modules/fg_pid/fgp_element.py b/src/sage/modules/fg_pid/fgp_element.py index 5b4ca856101..63864b5500f 100644 --- a/src/sage/modules/fg_pid/fgp_element.py +++ b/src/sage/modules/fg_pid/fgp_element.py @@ -5,20 +5,15 @@ - William Stein, 2009 """ -#################################################################################### +#***************************************************************************** # Copyright (C) 2009 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -#################################################################################### +#***************************************************************************** from sage.structure.element import ModuleElement @@ -456,7 +451,8 @@ def additive_order(self): I = Q.invariants() v = self.vector() - from sage.rings.all import infinity, lcm, Mod, Integer + from sage.rings.all import infinity, Mod, Integer + from sage.arith.all import lcm n = Integer(1) for i, a in enumerate(I): if a == 0: diff --git a/src/sage/modules/fg_pid/fgp_module.py b/src/sage/modules/fg_pid/fgp_module.py index 18c7938a925..33c45899ada 100644 --- a/src/sage/modules/fg_pid/fgp_module.py +++ b/src/sage/modules/fg_pid/fgp_module.py @@ -198,20 +198,15 @@ - William Stein, 2009 """ -#################################################################################### +#***************************************************************************** # Copyright (C) 2009 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ -#################################################################################### +#***************************************************************************** from sage.modules.module import Module from sage.modules.free_module import is_FreeModule @@ -219,7 +214,8 @@ from sage.structure.sequence import Sequence from fgp_element import DEBUG, FGP_Element from fgp_morphism import FGP_Morphism, FGP_Homset -from sage.rings.all import Integer, ZZ, lcm +from sage.rings.all import Integer, ZZ +from sage.arith.all import lcm from sage.misc.cachefunc import cached_method from sage.misc.superseded import deprecation diff --git a/src/sage/modules/free_module.py b/src/sage/modules/free_module.py index c27b4142269..970c3da35ba 100644 --- a/src/sage/modules/free_module.py +++ b/src/sage/modules/free_module.py @@ -5401,7 +5401,7 @@ def _denominator(self, B): if len(B) == 0: return 1 d = B[0].denominator() - from sage.rings.arith import lcm + from sage.arith.all import lcm for x in B[1:]: d = lcm(d,x.denominator()) return d diff --git a/src/sage/modules/free_module_element.pyx b/src/sage/modules/free_module_element.pyx index 3c627985f91..baff6dbc054 100644 --- a/src/sage/modules/free_module_element.pyx +++ b/src/sage/modules/free_module_element.pyx @@ -1544,7 +1544,7 @@ cdef class FreeModuleElement(Vector): # abstract base class if isinstance(ord, AnInfinity): return ord v.append(ord) - from sage.rings.arith import lcm + from sage.arith.all import lcm return lcm(v) def iteritems(self): diff --git a/src/sage/quadratic_forms/binary_qf.py b/src/sage/quadratic_forms/binary_qf.py index a2e5e6a060b..d4f8ea573a1 100644 --- a/src/sage/quadratic_forms/binary_qf.py +++ b/src/sage/quadratic_forms/binary_qf.py @@ -36,22 +36,18 @@ """ #***************************************************************************** -# Copyright (C) 2006--2009 William Stein and Jon Hanke -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +# Copyright (C) 2006-2009 William Stein and Jon Hanke # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** from sage.libs.pari.all import pari -from sage.rings.all import (is_fundamental_discriminant, ZZ, divisors) +from sage.rings.all import ZZ, is_fundamental_discriminant +from sage.arith.all import divisors, gcd from sage.structure.sage_object import SageObject from sage.misc.cachefunc import cached_method @@ -473,7 +469,6 @@ def is_primitive(self): 4*x^2 + x*y + 13*y^2, 8*x^2 + 7*x*y + 8*y^2] """ - from sage.rings.arith import gcd return gcd([self._a, self._b, self._c])==1 @cached_method @@ -823,7 +818,6 @@ def BinaryQF_reduced_representatives(D, primitive_only=False): form_list = [] from sage.misc.all import xsrange - from sage.rings.arith import gcd # Only iterate over positive a and over b of the same # parity as D such that 4a^2 + D <= b^2 <= a^2 diff --git a/src/sage/quadratic_forms/count_local_2.pyx b/src/sage/quadratic_forms/count_local_2.pyx index bcb7091d46a..7d4c303189a 100644 --- a/src/sage/quadratic_forms/count_local_2.pyx +++ b/src/sage/quadratic_forms/count_local_2.pyx @@ -2,7 +2,7 @@ r""" Optimised Cython code for counting congruence solutions """ -from sage.rings.arith import valuation, kronecker_symbol, is_prime +from sage.arith.all import valuation, kronecker_symbol, is_prime from sage.rings.finite_rings.integer_mod import IntegerMod, Mod from sage.rings.finite_rings.integer_mod_ring import IntegerModRing diff --git a/src/sage/quadratic_forms/extras.py b/src/sage/quadratic_forms/extras.py index 2ec17fc4f7d..7d703260845 100644 --- a/src/sage/quadratic_forms/extras.py +++ b/src/sage/quadratic_forms/extras.py @@ -2,7 +2,7 @@ from sage.matrix.constructor import matrix from sage.matrix.matrix import is_Matrix -from sage.rings.arith import legendre_symbol +from sage.arith.all import legendre_symbol from sage.rings.integer_ring import ZZ def is_triangular_number(n, return_value=False): diff --git a/src/sage/quadratic_forms/genera/genus.py b/src/sage/quadratic_forms/genera/genus.py index ce6b91b3604..19cb036af87 100644 --- a/src/sage/quadratic_forms/genera/genus.py +++ b/src/sage/quadratic_forms/genera/genus.py @@ -10,7 +10,7 @@ #***************************************************************************** from sage.misc.all import prod -from sage.rings.arith import LCM +from sage.arith.all import LCM from sage.matrix.matrix_space import MatrixSpace from sage.rings.integer_ring import IntegerRing from sage.rings.rational_field import RationalField diff --git a/src/sage/quadratic_forms/quadratic_form.py b/src/sage/quadratic_forms/quadratic_form.py index 415284d4ac0..94acaba2ffa 100644 --- a/src/sage/quadratic_forms/quadratic_form.py +++ b/src/sage/quadratic_forms/quadratic_form.py @@ -31,7 +31,7 @@ from sage.rings.integer_ring import IntegerRing, ZZ from sage.rings.ring import Ring from sage.misc.functional import denominator, is_even, is_field -from sage.rings.arith import GCD, LCM +from sage.arith.all import GCD, LCM from sage.rings.principal_ideal_domain import is_PrincipalIdealDomain from sage.rings.all import Ideal from sage.rings.ring import is_Ring diff --git a/src/sage/quadratic_forms/quadratic_form__automorphisms.py b/src/sage/quadratic_forms/quadratic_form__automorphisms.py index caf6a7f2029..8b851b0c397 100644 --- a/src/sage/quadratic_forms/quadratic_form__automorphisms.py +++ b/src/sage/quadratic_forms/quadratic_form__automorphisms.py @@ -17,7 +17,7 @@ from sage.modules.all import FreeModule from sage.modules.free_module_element import vector -from sage.rings.arith import GCD +from sage.arith.all import GCD @cached_method diff --git a/src/sage/quadratic_forms/quadratic_form__equivalence_testing.py b/src/sage/quadratic_forms/quadratic_form__equivalence_testing.py index a27837f6b3a..69e67d03af9 100644 --- a/src/sage/quadratic_forms/quadratic_form__equivalence_testing.py +++ b/src/sage/quadratic_forms/quadratic_form__equivalence_testing.py @@ -6,7 +6,7 @@ - Anna Haensch (2014-12-01): added test for rational isometry """ -from sage.rings.arith import hilbert_symbol, prime_divisors, is_prime, valuation, GCD, legendre_symbol +from sage.arith.all import hilbert_symbol, prime_divisors, is_prime, valuation, GCD, legendre_symbol from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ diff --git a/src/sage/quadratic_forms/quadratic_form__genus.py b/src/sage/quadratic_forms/quadratic_form__genus.py index e779dd651eb..cd63e5e4452 100644 --- a/src/sage/quadratic_forms/quadratic_form__genus.py +++ b/src/sage/quadratic_forms/quadratic_form__genus.py @@ -23,7 +23,7 @@ from sage.rings.integer_ring import IntegerRing -from sage.rings.arith import is_prime, prime_divisors +from sage.arith.all import is_prime, prime_divisors diff --git a/src/sage/quadratic_forms/quadratic_form__local_density_congruence.py b/src/sage/quadratic_forms/quadratic_form__local_density_congruence.py index 575c629e324..540715a9fa3 100644 --- a/src/sage/quadratic_forms/quadratic_form__local_density_congruence.py +++ b/src/sage/quadratic_forms/quadratic_form__local_density_congruence.py @@ -13,7 +13,7 @@ from sage.sets.set import Set from sage.rings.rational_field import QQ -from sage.rings.arith import valuation +from sage.arith.all import valuation from sage.misc.misc import verbose from sage.quadratic_forms.count_local_2 import count_modp__by_gauss_sum diff --git a/src/sage/quadratic_forms/quadratic_form__local_density_interfaces.py b/src/sage/quadratic_forms/quadratic_form__local_density_interfaces.py index 68de70fc98e..9a47626531f 100644 --- a/src/sage/quadratic_forms/quadratic_form__local_density_interfaces.py +++ b/src/sage/quadratic_forms/quadratic_form__local_density_interfaces.py @@ -5,7 +5,7 @@ ## #include "../max-min.h" -from sage.rings.arith import valuation +from sage.arith.all import valuation from sage.rings.rational_field import QQ diff --git a/src/sage/quadratic_forms/quadratic_form__local_field_invariants.py b/src/sage/quadratic_forms/quadratic_form__local_field_invariants.py index 63d9a96b5dd..f8a4572fb1f 100644 --- a/src/sage/quadratic_forms/quadratic_form__local_field_invariants.py +++ b/src/sage/quadratic_forms/quadratic_form__local_field_invariants.py @@ -27,7 +27,7 @@ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ from sage.rings.real_mpfr import RR -from sage.rings.arith import prime_divisors, hilbert_symbol +from sage.arith.all import prime_divisors, hilbert_symbol from sage.functions.all import sgn from sage.matrix.matrix_space import MatrixSpace from sage.misc.cachefunc import cached_method diff --git a/src/sage/quadratic_forms/quadratic_form__local_normal_form.py b/src/sage/quadratic_forms/quadratic_form__local_normal_form.py index 87d5d3cdf71..7d9d9947d34 100644 --- a/src/sage/quadratic_forms/quadratic_form__local_normal_form.py +++ b/src/sage/quadratic_forms/quadratic_form__local_normal_form.py @@ -21,7 +21,7 @@ from sage.rings.infinity import Infinity from sage.rings.integer_ring import IntegerRing, ZZ from sage.rings.rational_field import QQ -from sage.rings.arith import GCD, valuation, is_prime +from sage.arith.all import GCD, valuation, is_prime def find_entry_with_minimal_scale_at_prime(self, p): diff --git a/src/sage/quadratic_forms/quadratic_form__local_representation_conditions.py b/src/sage/quadratic_forms/quadratic_form__local_representation_conditions.py index a8ed40edbe8..410ab18bc6a 100644 --- a/src/sage/quadratic_forms/quadratic_form__local_representation_conditions.py +++ b/src/sage/quadratic_forms/quadratic_form__local_representation_conditions.py @@ -9,7 +9,7 @@ from copy import deepcopy from sage.rings.integer_ring import ZZ -from sage.rings.arith import prime_divisors, valuation, is_square +from sage.arith.all import prime_divisors, valuation, is_square from sage.quadratic_forms.extras import least_quadratic_nonresidue from sage.rings.infinity import infinity from sage.misc.functional import numerator, denominator diff --git a/src/sage/quadratic_forms/quadratic_form__mass__Conway_Sloane_masses.py b/src/sage/quadratic_forms/quadratic_form__mass__Conway_Sloane_masses.py index a5d75b42f63..62a451725d2 100644 --- a/src/sage/quadratic_forms/quadratic_form__mass__Conway_Sloane_masses.py +++ b/src/sage/quadratic_forms/quadratic_form__mass__Conway_Sloane_masses.py @@ -3,7 +3,7 @@ """ from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ -from sage.rings.arith import kronecker_symbol, legendre_symbol, prime_divisors, is_prime, fundamental_discriminant +from sage.arith.all import kronecker_symbol, legendre_symbol, prime_divisors, is_prime, fundamental_discriminant from sage.symbolic.constants import pi from sage.misc.all import prod from sage.quadratic_forms.special_values import gamma__exact, zeta__exact, quadratic_L_function__exact diff --git a/src/sage/quadratic_forms/quadratic_form__mass__Siegel_densities.py b/src/sage/quadratic_forms/quadratic_form__mass__Siegel_densities.py index 2d63c829f3a..225dfc2fbd3 100644 --- a/src/sage/quadratic_forms/quadratic_form__mass__Siegel_densities.py +++ b/src/sage/quadratic_forms/quadratic_form__mass__Siegel_densities.py @@ -18,7 +18,7 @@ from sage.rings.integer_ring import ZZ from sage.rings.finite_rings.integer_mod_ring import IntegerModRing from sage.rings.rational_field import QQ -from sage.rings.arith import legendre_symbol, kronecker, prime_divisors +from sage.arith.all import legendre_symbol, kronecker, prime_divisors from sage.functions.all import sgn from sage.quadratic_forms.special_values import gamma__exact, zeta__exact, quadratic_L_function__exact from sage.misc.functional import squarefree_part diff --git a/src/sage/quadratic_forms/quadratic_form__siegel_product.py b/src/sage/quadratic_forms/quadratic_form__siegel_product.py index 82f7c325db7..4134baa2aad 100644 --- a/src/sage/quadratic_forms/quadratic_form__siegel_product.py +++ b/src/sage/quadratic_forms/quadratic_form__siegel_product.py @@ -1,20 +1,19 @@ """ Siegel Products """ + #***************************************************************************** -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** -from sage.rings.arith import fundamental_discriminant from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ -from sage.rings.arith import kronecker_symbol, bernoulli, prime_divisors -#from sage.combinat.combinat import bernoulli_polynomial +from sage.arith.all import kronecker_symbol, bernoulli, prime_divisors, fundamental_discriminant from sage.functions.all import sqrt -#from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.quadratic_forms.special_values import QuadraticBernoulliNumber from sage.misc.misc import verbose diff --git a/src/sage/quadratic_forms/quadratic_form__split_local_covering.py b/src/sage/quadratic_forms/quadratic_form__split_local_covering.py index f8de77ddae1..8fb637a2ca8 100644 --- a/src/sage/quadratic_forms/quadratic_form__split_local_covering.py +++ b/src/sage/quadratic_forms/quadratic_form__split_local_covering.py @@ -18,7 +18,7 @@ from sage.matrix.constructor import matrix from sage.functions.all import floor from sage.rings.integer_ring import ZZ -from sage.rings.arith import GCD +from sage.arith.all import GCD def cholesky_decomposition(self, bit_prec = 53): diff --git a/src/sage/quadratic_forms/quadratic_form__ternary_Tornaria.py b/src/sage/quadratic_forms/quadratic_form__ternary_Tornaria.py index 87d68b12839..915ed4e1241 100644 --- a/src/sage/quadratic_forms/quadratic_form__ternary_Tornaria.py +++ b/src/sage/quadratic_forms/quadratic_form__ternary_Tornaria.py @@ -1,23 +1,25 @@ """ Tornaria Methods for Computing with Quadratic Forms - """ -######################################################################## -## Routines from Gonzalo Tornaria (7/9/07) -## for computing with ternary quadratic forms. -####################################################################### - +#***************************************************************************** +# Copyright (C) 2007 Gonzalo Tornaria +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** -#from sage.rings.rational_field import QQ from sage.rings.integer_ring import ZZ from sage.misc.functional import is_odd from sage.libs.pari.all import pari from sage.misc.all import prod -from sage.rings.arith import factor, gcd, prime_to_m_part, CRT_vectors -from sage.rings.arith import hilbert_symbol, kronecker_symbol +from sage.arith.all import (factor, gcd, prime_to_m_part, CRT_vectors, + hilbert_symbol, kronecker_symbol) from sage.quadratic_forms.quadratic_form import QuadraticForm__constructor as QuadraticForm from sage.modules.free_module import FreeModule diff --git a/src/sage/quadratic_forms/special_values.py b/src/sage/quadratic_forms/special_values.py index 2fb86657599..472ab670da0 100644 --- a/src/sage/quadratic_forms/special_values.py +++ b/src/sage/quadratic_forms/special_values.py @@ -10,7 +10,7 @@ from sage.combinat.combinat import bernoulli_polynomial from sage.misc.functional import denominator from sage.rings.all import RealField -from sage.rings.arith import kronecker_symbol, bernoulli, factorial, fundamental_discriminant +from sage.arith.all import kronecker_symbol, bernoulli, factorial, fundamental_discriminant from sage.rings.infinity import infinity from sage.rings.integer_ring import ZZ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing diff --git a/src/sage/quadratic_forms/ternary.pyx b/src/sage/quadratic_forms/ternary.pyx index 6e34d864f79..d154a78863c 100644 --- a/src/sage/quadratic_forms/ternary.pyx +++ b/src/sage/quadratic_forms/ternary.pyx @@ -16,11 +16,10 @@ Helper code for ternary quadratic forms from sage.rings.integer_ring import ZZ from sage.matrix.constructor import matrix, identity_matrix, diagonal_matrix from sage.modules.free_module_element import vector -from sage.rings.arith import inverse_mod +from sage.arith.all import inverse_mod, xgcd, gcd from sage.quadratic_forms.extras import extend_to_primitive from sage.rings.finite_rings.integer_mod import mod from sage.misc.prandom import randint -from sage.rings.arith import xgcd,gcd from sage.functions.other import ceil, floor from __builtin__ import max diff --git a/src/sage/quadratic_forms/ternary_qf.py b/src/sage/quadratic_forms/ternary_qf.py index 152a10fb713..26209e524e1 100644 --- a/src/sage/quadratic_forms/ternary_qf.py +++ b/src/sage/quadratic_forms/ternary_qf.py @@ -29,7 +29,7 @@ from sage.structure.sage_object import SageObject from sage.rings.all import ZZ -from sage.rings.arith import gcd, inverse_mod, kronecker_symbol +from sage.arith.all import gcd, inverse_mod, kronecker_symbol from sage.quadratic_forms.quadratic_form import QuadraticForm from sage.matrix.constructor import matrix, identity_matrix from sage.matrix.matrix import Matrix, is_Matrix diff --git a/src/sage/rings/algebraic_closure_finite_field.py b/src/sage/rings/algebraic_closure_finite_field.py index e974e582716..422819f1299 100644 --- a/src/sage/rings/algebraic_closure_finite_field.py +++ b/src/sage/rings/algebraic_closure_finite_field.py @@ -896,7 +896,7 @@ def _roots_univariate_polynomial(self, p, ring=None, multiplicities=None, algori ....: assert p(r).is_zero(), "r={} is not a root of p={}".format(r,p) """ - from sage.rings.arith import lcm + from sage.arith.all import lcm from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing # first build a polynomial over some finite field diff --git a/src/sage/rings/all.py b/src/sage/rings/all.py index 1c6c0bf6e5d..4444fe55220 100644 --- a/src/sage/rings/all.py +++ b/src/sage/rings/all.py @@ -131,34 +131,6 @@ # c-finite sequences from cfinite_sequence import CFiniteSequence, CFiniteSequences -# Arithmetic -from arith import algdep, bernoulli, is_prime, is_prime_power, \ - is_pseudoprime, is_pseudoprime_power, is_pseudoprime_small_power, \ - prime_powers, primes_first_n, eratosthenes, primes, \ - next_prime_power, next_probable_prime, next_prime, \ - previous_prime, previous_prime_power, random_prime, \ - divisors, sigma, gcd, GCD, lcm, LCM, xlcm, xgcd, xkcd, \ - inverse_mod, get_gcd, get_inverse_mod, power_mod, \ - rational_reconstruction, mqrr_rational_reconstruction, \ - trial_division, factor, prime_divisors, odd_part, prime_to_m_part, \ - is_square, is_squarefree, euler_phi, crt, CRT, CRT_list, CRT_basis, \ - CRT_vectors, multinomial, multinomial_coefficients, \ - kronecker_symbol, kronecker, legendre_symbol, \ - primitive_root, nth_prime, quadratic_residues, moebius, \ - continuant, number_of_divisors, hilbert_symbol, hilbert_conductor, \ - hilbert_conductor_inverse, falling_factorial, rising_factorial, \ - integer_ceil, integer_floor, \ - two_squares, three_squares, four_squares, sum_of_k_squares, \ - subfactorial, is_power_of_two, differences, \ - sort_complex_numbers_for_display, \ - fundamental_discriminant, squarefree_divisors, \ - Sigma, radical, Euler_Phi, binomial_coefficients, jacobi_symbol, \ - Moebius, dedekind_sum, \ - prime_factors, valuation - - -from fast_arith import prime_range - from bernoulli_mod_p import bernoulli_mod_p, bernoulli_mod_p_single from monomials import monomials @@ -168,9 +140,11 @@ from misc import composite_field - from sage.misc.lazy_import import lazy_import lazy_import('sage.rings.invariant_theory', 'invariant_theory') +lazy_import('sage.arith.all', '*', deprecation=19879) + +from fast_arith import prime_range # continued fractions from sage.rings.continued_fraction import (farey, convergents, diff --git a/src/sage/rings/arith.py b/src/sage/rings/arith.py index 017a2617a25..8072684027f 100644 --- a/src/sage/rings/arith.py +++ b/src/sage/rings/arith.py @@ -1,5325 +1,2 @@ -""" -Miscellaneous arithmetic functions -""" -#***************************************************************************** -# Copyright (C) 2006 William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. -# http://www.gnu.org/licenses/ -#***************************************************************************** - - -import math - -from sage.misc.misc import powerset -from sage.misc.misc_c import prod - -from sage.libs.pari.all import pari -import sage.libs.flint.arith as flint_arith - -from sage.structure.element import parent -from sage.structure.coerce import py_scalar_to_element - -from sage.rings.rational_field import QQ -from sage.rings.integer_ring import ZZ -from sage.rings.integer import Integer, GCD_list, LCM_list -from sage.rings.rational import Rational -from sage.rings.real_mpfr import RealNumber -from sage.rings.complex_number import ComplexNumber - -import fast_arith - -################################################################## -# Elementary Arithmetic -################################################################## - -def algdep(z, degree, known_bits=None, use_bits=None, known_digits=None, use_digits=None, height_bound=None, proof=False): - """ - Returns a polynomial of degree at most `degree` which is - approximately satisfied by the number `z`. Note that the returned - polynomial need not be irreducible, and indeed usually won't be if - `z` is a good approximation to an algebraic number of degree less - than `degree`. - - You can specify the number of known bits or digits of `z` with - ``known_bits=k`` or ``known_digits=k``. PARI is then told to - compute the result using `0.8k` of these bits/digits. Or, you can - specify the precision to use directly with ``use_bits=k`` or - ``use_digits=k``. If none of these are specified, then the precision - is taken from the input value. - - A height bound may be specified to indicate the maximum coefficient - size of the returned polynomial; if a sufficiently small polynomial - is not found, then ``None`` will be returned. If ``proof=True`` then - the result is returned only if it can be proved correct (i.e. the - only possible minimal polynomial satisfying the height bound, or no - such polynomial exists). Otherwise a ``ValueError`` is raised - indicating that higher precision is required. - - ALGORITHM: Uses LLL for real/complex inputs, PARI C-library - ``algdep`` command otherwise. - - Note that ``algebraic_dependency`` is a synonym for ``algdep``. - - INPUT: - - - - ``z`` - real, complex, or `p`-adic number - - - ``degree`` - an integer - - - ``height_bound`` - an integer (default: ``None``) specifying the maximum - coefficient size for the returned polynomial - - - ``proof`` - a boolean (default: ``False``), requires height_bound to be set - - - EXAMPLES:: - - sage: algdep(1.888888888888888, 1) - 9*x - 17 - sage: algdep(0.12121212121212,1) - 33*x - 4 - sage: algdep(sqrt(2),2) - x^2 - 2 - - This example involves a complex number:: - - sage: z = (1/2)*(1 + RDF(sqrt(3)) *CC.0); z - 0.500000000000000 + 0.866025403784439*I - sage: p = algdep(z, 6); p - x^3 + 1 - sage: p.factor() - (x + 1) * (x^2 - x + 1) - sage: z^2 - z + 1 # abs tol 2e-16 - 0.000000000000000 - - This example involves a `p`-adic number:: - - sage: K = Qp(3, print_mode = 'series') - sage: a = K(7/19); a - 1 + 2*3 + 3^2 + 3^3 + 2*3^4 + 2*3^5 + 3^8 + 2*3^9 + 3^11 + 3^12 + 2*3^15 + 2*3^16 + 3^17 + 2*3^19 + O(3^20) - sage: algdep(a, 1) - 19*x - 7 - - These examples show the importance of proper precision control. We - compute a 200-bit approximation to `sqrt(2)` which is wrong in the - 33'rd bit:: - - sage: z = sqrt(RealField(200)(2)) + (1/2)^33 - sage: p = algdep(z, 4); p - 227004321085*x^4 - 216947902586*x^3 - 99411220986*x^2 + 82234881648*x - 211871195088 - sage: factor(p) - 227004321085*x^4 - 216947902586*x^3 - 99411220986*x^2 + 82234881648*x - 211871195088 - sage: algdep(z, 4, known_bits=32) - x^2 - 2 - sage: algdep(z, 4, known_digits=10) - x^2 - 2 - sage: algdep(z, 4, use_bits=25) - x^2 - 2 - sage: algdep(z, 4, use_digits=8) - x^2 - 2 - - Using the ``height_bound`` and ``proof`` parameters, we can see that - `pi` is not the root of an integer polynomial of degree at most 5 - and coefficients bounded above by 10:: - - sage: algdep(pi.n(), 5, height_bound=10, proof=True) is None - True - - For stronger results, we need more precicion:: - - sage: algdep(pi.n(), 5, height_bound=100, proof=True) is None - Traceback (most recent call last): - ... - ValueError: insufficient precision for non-existence proof - sage: algdep(pi.n(200), 5, height_bound=100, proof=True) is None - True - - sage: algdep(pi.n(), 10, height_bound=10, proof=True) is None - Traceback (most recent call last): - ... - ValueError: insufficient precision for non-existence proof - sage: algdep(pi.n(200), 10, height_bound=10, proof=True) is None - True - - We can also use ``proof=True`` to get positive results:: - - sage: a = sqrt(2) + sqrt(3) + sqrt(5) - sage: algdep(a.n(), 8, height_bound=1000, proof=True) - Traceback (most recent call last): - ... - ValueError: insufficient precision for uniqueness proof - sage: f = algdep(a.n(1000), 8, height_bound=1000, proof=True); f - x^8 - 40*x^6 + 352*x^4 - 960*x^2 + 576 - sage: f(a).expand() - 0 - - TESTS:: - - sage: algdep(complex("1+2j"), 4) - x^2 - 2*x + 5 - """ - if proof and not height_bound: - raise ValueError("height_bound must be given for proof=True") - - x = ZZ['x'].gen() - - z = py_scalar_to_element(z) - - if isinstance(z, Integer): - if height_bound and abs(z) >= height_bound: - return None - return x - ZZ(z) - - degree = ZZ(degree) - - if isinstance(z, Rational): - if height_bound and max(abs(z.denominator()), abs(z.numerator())) >= height_bound: - return None - return z.denominator()*x - z.numerator() - - if isinstance(z, (RealNumber, ComplexNumber)): - - log2_10 = math.log(10,2) - - prec = z.prec() - 6 - if known_digits is not None: - known_bits = known_digits * log2_10 - if known_bits is not None: - use_bits = known_bits * 0.8 - if use_digits is not None: - use_bits = use_digits * log2_10 - if use_bits is not None: - prec = int(use_bits) - - is_complex = isinstance(z, ComplexNumber) - n = degree+1 - from sage.matrix.all import matrix - M = matrix(ZZ, n, n+1+int(is_complex)) - r = ZZ.one() << prec - M[0, 0] = 1 - M[0, -1] = r - for k in range(1, degree+1): - M[k, k] = 1 - r *= z - if is_complex: - M[k, -1] = r.real().round() - M[k, -2] = r.imag().round() - else: - M[k, -1] = r.round() - LLL = M.LLL(delta=.75) - coeffs = LLL[0][:n] - if height_bound: - def norm(v): - # norm on an integer vector invokes Integer.sqrt() which tries to factor... - from sage.rings.real_mpfi import RIF - return v.change_ring(RIF).norm() - if max(abs(a) for a in coeffs) > height_bound: - if proof: - # Given an LLL reduced basis $b_1, ..., b_n$, we only - # know that $|b_1| <= 2^((n-1)/2) |x|$ for non-zero $x \in L$. - if norm(LLL[0]) <= 2**((n-1)/2) * n.sqrt() * height_bound: - raise ValueError("insufficient precision for non-existence proof") - return None - elif proof and norm(LLL[1]) < 2**((n-1)/2) * max(norm(LLL[0]), n.sqrt()*height_bound): - raise ValueError("insufficient precision for uniqueness proof") - if coeffs[degree] < 0: - coeffs = -coeffs - f = list(coeffs) - - elif proof or height_bound: - raise NotImplementedError("proof and height bound only implemented for real and complex numbers") - - else: - y = pari(z) - f = y.algdep(degree) - - return x.parent()(f) - - -algebraic_dependency = algdep - -def bernoulli(n, algorithm='default', num_threads=1): - r""" - Return the n-th Bernoulli number, as a rational number. - - INPUT: - - - ``n`` - an integer - - ``algorithm``: - - - ``'default'`` -- use 'flint' for n <= 300000, and 'bernmm' - otherwise (this is just a heuristic, and not guaranteed to be - optimal on all hardware) - - ``'flint'`` -- use the FLINT library - - ``'pari'`` -- use the PARI C library - - ``'gap'`` -- use GAP - - ``'gp'`` -- use PARI/GP interpreter - - ``'magma'`` -- use MAGMA (optional) - - ``'bernmm'`` -- use bernmm package (a multimodular algorithm) - - - ``num_threads`` - positive integer, number of - threads to use (only used for bernmm algorithm) - - EXAMPLES:: - - sage: bernoulli(12) - -691/2730 - sage: bernoulli(50) - 495057205241079648212477525/66 - - We demonstrate each of the alternative algorithms:: - - sage: bernoulli(12, algorithm='flint') - -691/2730 - sage: bernoulli(12, algorithm='gap') - -691/2730 - sage: bernoulli(12, algorithm='gp') - -691/2730 - sage: bernoulli(12, algorithm='magma') # optional - magma - -691/2730 - sage: bernoulli(12, algorithm='pari') - -691/2730 - sage: bernoulli(12, algorithm='bernmm') - -691/2730 - sage: bernoulli(12, algorithm='bernmm', num_threads=4) - -691/2730 - - TESTS:: - - sage: algs = ['gap','gp','pari','bernmm','flint'] - sage: test_list = [ZZ.random_element(2, 2255) for _ in range(500)] - sage: vals = [[bernoulli(i,algorithm = j) for j in algs] for i in test_list] # long time (up to 21s on sage.math, 2011) - sage: union([len(union(x))==1 for x in vals]) # long time (depends on previous line) - [True] - sage: algs = ['gp','pari','bernmm'] - sage: test_list = [ZZ.random_element(2256, 5000) for _ in range(500)] - sage: vals = [[bernoulli(i,algorithm = j) for j in algs] for i in test_list] # long time (up to 30s on sage.math, 2011) - sage: union([len(union(x))==1 for x in vals]) # long time (depends on previous line) - [True] - - AUTHOR: - - - David Joyner and William Stein - """ - n = ZZ(n) - - if algorithm == 'default': - algorithm = 'flint' if n <= 300000 else 'bernmm' - - if algorithm == 'flint': - return flint_arith.bernoulli_number(n) - elif algorithm == 'pari': - x = pari(n).bernfrac() # Use the PARI C library - return Rational(x) - elif algorithm == 'gap': - import sage.interfaces.gap - x = sage.interfaces.gap.gap('Bernoulli(%s)'%n) - return Rational(x) - elif algorithm == 'magma': - import sage.interfaces.magma - x = sage.interfaces.magma.magma('Bernoulli(%s)'%n) - return Rational(x) - elif algorithm == 'gp': - import sage.interfaces.gp - x = sage.interfaces.gp.gp('bernfrac(%s)'%n) - return Rational(x) - elif algorithm == 'bernmm': - import sage.rings.bernmm - return sage.rings.bernmm.bernmm_bern_rat(n, num_threads) - else: - raise ValueError("invalid choice of algorithm") - - -def factorial(n, algorithm='gmp'): - r""" - Compute the factorial of `n`, which is the product - `1\cdot 2\cdot 3 \cdots (n-1)\cdot n`. - - INPUT: - - - ``n`` - an integer - - - ``algorithm`` - string (default: 'gmp'): - - - ``'gmp'`` - use the GMP C-library factorial function - - - ``'pari'`` - use PARI's factorial function - - OUTPUT: an integer - - EXAMPLES:: - - sage: from sage.rings.arith import factorial - sage: factorial(0) - 1 - sage: factorial(4) - 24 - sage: factorial(10) - 3628800 - sage: factorial(1) == factorial(0) - True - sage: factorial(6) == 6*5*4*3*2 - True - sage: factorial(1) == factorial(0) - True - sage: factorial(71) == 71* factorial(70) - True - sage: factorial(-32) - Traceback (most recent call last): - ... - ValueError: factorial -- must be nonnegative - - PERFORMANCE: This discussion is valid as of April 2006. All timings - below are on a Pentium Core Duo 2Ghz MacBook Pro running Linux with - a 2.6.16.1 kernel. - - - - It takes less than a minute to compute the factorial of - `10^7` using the GMP algorithm, and the factorial of - `10^6` takes less than 4 seconds. - - - The GMP algorithm is faster and more memory efficient than the - PARI algorithm. E.g., PARI computes `10^7` factorial in 100 - seconds on the core duo 2Ghz. - - - For comparison, computation in Magma `\leq` 2.12-10 of - `n!` is best done using ``*[1..n]``. It takes - 113 seconds to compute the factorial of `10^7` and 6 - seconds to compute the factorial of `10^6`. Mathematica - V5.2 compute the factorial of `10^7` in 136 seconds and the - factorial of `10^6` in 7 seconds. (Mathematica is notably - very efficient at memory usage when doing factorial - calculations.) - """ - if n < 0: - raise ValueError("factorial -- must be nonnegative") - if algorithm == 'gmp': - return ZZ(n).factorial() - elif algorithm == 'pari': - return pari.factorial(n) - else: - raise ValueError('unknown algorithm') - -def is_prime(n): - r""" - Return ``True`` if `n` is a prime number, and ``False`` otherwise. - - Use a provable primality test or a strong pseudo-primality test depending - on the global :mod:`arithmetic proof flag `. - - INPUT: - - - ``n`` - the object for which to determine primality - - .. SEEALSO:: - - - :meth:`is_pseudoprime` - - :meth:`sage.rings.integer.Integer.is_prime` - - AUTHORS: - - - Kevin Stueve kstueve@uw.edu (2010-01-17): - delegated calculation to ``n.is_prime()`` - - EXAMPLES:: - - sage: is_prime(389) - True - sage: is_prime(2000) - False - sage: is_prime(2) - True - sage: is_prime(-1) - False - sage: is_prime(1) - False - sage: is_prime(-2) - False - - sage: a = 2**2048 + 981 - sage: is_prime(a) # not tested - takes ~ 1min - sage: proof.arithmetic(False) - sage: is_prime(a) # instantaneous! - True - sage: proof.arithmetic(True) - """ - try: - return n.is_prime() - except (AttributeError, NotImplementedError): - return ZZ(n).is_prime() - -def is_pseudoprime(n, flag=None): - r""" - Test whether ``n`` is a pseudo-prime - - The result is *NOT* proven correct - *this is a pseudo-primality test!*. - - INPUT: - - - ``n`` -- an integer - - .. note:: - - We do not consider negatives of prime numbers as prime. - - EXAMPLES:: - - sage: is_pseudoprime(389) - True - sage: is_pseudoprime(2000) - False - sage: is_pseudoprime(2) - True - sage: is_pseudoprime(-1) - False - sage: factor(-6) - -1 * 2 * 3 - sage: is_pseudoprime(1) - False - sage: is_pseudoprime(-2) - False - - TESTS: - - Deprecation warning from :trac:`16878`:: - - sage: is_pseudoprime(127, flag=0) - doctest:...: DeprecationWarning: the keyword 'flag' is deprecated and no longer used - See http://trac.sagemath.org/16878 for details. - True - """ - if flag is not None: - from sage.misc.superseded import deprecation - deprecation(16878, "the keyword 'flag' is deprecated and no longer used") - return ZZ(n).is_pseudoprime() - -def is_prime_power(n, flag=None, get_data=False): - r""" - Test whether ``n`` is a positive power of a prime number - - This function simply calls the method :meth:`Integer.is_prime_power() - ` of Integers. - - INPUT: - - - ``n`` -- an integer - - - ``get_data`` -- if set to ``True``, return a pair ``(p,k)`` such that - this integer equals ``p^k`` instead of ``True`` or ``(self,0)`` instead of - ``False`` - - EXAMPLES:: - - sage: is_prime_power(389) - True - sage: is_prime_power(2000) - False - sage: is_prime_power(2) - True - sage: is_prime_power(1024) - True - sage: is_prime_power(1024, get_data=True) - (2, 10) - - The same results can be obtained with:: - - sage: 389.is_prime_power() - True - sage: 2000.is_prime_power() - False - sage: 2.is_prime_power() - True - sage: 1024.is_prime_power() - True - sage: 1024.is_prime_power(get_data=True) - (2, 10) - - TESTS:: - - sage: is_prime_power(-1) - False - sage: is_prime_power(1) - False - sage: is_prime_power(QQ(997^100)) - True - sage: is_prime_power(1/2197) - Traceback (most recent call last): - ... - TypeError: no conversion of this rational to integer - sage: is_prime_power("foo") - Traceback (most recent call last): - ... - TypeError: unable to convert 'foo' to an integer - """ - if flag is not None: - from sage.misc.superseded import deprecation - deprecation(16878, "the keyword 'flag' is deprecated and no longer used") - return ZZ(n).is_prime_power(get_data=get_data) - -def is_pseudoprime_power(n, get_data=False): - r""" - Test if ``n`` is a power of a pseudoprime. - - The result is *NOT* proven correct - *this IS a pseudo-primality test!*. - Note that a prime power is a positive power of a prime number so that 1 is - not a prime power. - - INPUT: - - - ``n`` - an integer - - - ``get_data`` - (boolean) instead of a boolean return a pair `(p,k)` so - that ``n`` equals `p^k` and `p` is a pseudoprime or `(n,0)` otherwise. - - EXAMPLES:: - - sage: is_pseudoprime_power(389) - True - sage: is_pseudoprime_power(2000) - False - sage: is_pseudoprime_power(2) - True - sage: is_pseudoprime_power(1024) - True - sage: is_pseudoprime_power(-1) - False - sage: is_pseudoprime_power(1) - False - sage: is_pseudoprime_power(997^100) - True - - Use of the get_data keyword:: - - sage: is_pseudoprime_power(3^1024, get_data=True) - (3, 1024) - sage: is_pseudoprime_power(2^256, get_data=True) - (2, 256) - sage: is_pseudoprime_power(31, get_data=True) - (31, 1) - sage: is_pseudoprime_power(15, get_data=True) - (15, 0) - """ - return ZZ(n).is_prime_power(proof=False, get_data=get_data) - -def is_pseudoprime_small_power(n, bound=None, get_data=False): - """ - Deprecated version of ``is_pseudoprime_power``. - - EXAMPLES:: - - sage: is_pseudoprime_small_power(1234) - doctest:...: DeprecationWarning: the function is_pseudoprime_small_power() is deprecated, use is_pseudoprime_power() instead. - See http://trac.sagemath.org/16878 for details. - False - sage: is_pseudoprime_small_power(3^1024, get_data=True) - [(3, 1024)] - """ - from sage.misc.superseded import deprecation - deprecation(16878, "the function is_pseudoprime_small_power() is deprecated, use is_pseudoprime_power() instead.") - if get_data: - return [ZZ(n).is_prime_power(proof=False, get_data=True)] - else: - return ZZ(n).is_prime_power(proof=False) - - -def valuation(m, *args, **kwds): - """ - Return the valuation of ``m``. - - This function simply calls the m.valuation() method. - See the documentation of m.valuation() for a more precise description. - - Note that the use of this functions is discouraged as it is better to use - m.valuation() directly. - - .. NOTE:: - - This is not always a valuation in the mathematical sense. - For more information see: - sage.rings.finite_rings.integer_mod.IntegerMod_int.valuation - - EXAMPLES:: - - sage: valuation(512,2) - 9 - sage: valuation(1,2) - 0 - sage: valuation(5/9, 3) - -2 - - Valuation of 0 is defined, but valuation with respect to 0 is not:: - - sage: valuation(0,7) - +Infinity - sage: valuation(3,0) - Traceback (most recent call last): - ... - ValueError: You can only compute the valuation with respect to a integer larger than 1. - - Here are some other examples:: - - sage: valuation(100,10) - 2 - sage: valuation(200,10) - 2 - sage: valuation(243,3) - 5 - sage: valuation(243*10007,3) - 5 - sage: valuation(243*10007,10007) - 1 - sage: y = QQ['y'].gen() - sage: valuation(y^3, y) - 3 - sage: x = QQ[['x']].gen() - sage: valuation((x^3-x^2)/(x-4)) - 2 - sage: valuation(4r,2r) - 2 - sage: valuation(1r,1r) - Traceback (most recent call last): - ... - ValueError: You can only compute the valuation with respect to a integer larger than 1. - """ - try: - return m.valuation(*args, **kwds) - except AttributeError: - return ZZ(m).valuation(*args, **kwds) - -def prime_powers(start, stop=None): - r""" - List of all positive primes powers between ``start`` and - ``stop``-1, inclusive. If the second argument is omitted, returns - the prime powers up to the first argument. - - INPUT: - - - ``start`` - an integer. If two inputs are given, a lower bound - for the returned set of prime powers. If this is the only input, - then it is an upper bound. - - - ``stop`` - an integer (default: ``None``). An upper bound for the - returned set of prime powers. - - OUTPUT: - - The set of all prime powers between ``start`` and ``stop`` or, if - only one argument is passed, the set of all prime powers between 1 - and ``start``. The number `n` is a prime power if `n=p^k`, where - `p` is a prime number and `k` is a positive integer. Thus, `1` is - not a prime power. - - EXAMPLES:: - - sage: prime_powers(20) - [2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 17, 19] - sage: len(prime_powers(1000)) - 193 - sage: len(prime_range(1000)) - 168 - - sage: a = [z for z in range(95,1234) if is_prime_power(z)] - sage: b = prime_powers(95,1234) - sage: len(b) - 194 - sage: len(a) - 194 - sage: a[:10] - [97, 101, 103, 107, 109, 113, 121, 125, 127, 128] - sage: b[:10] - [97, 101, 103, 107, 109, 113, 121, 125, 127, 128] - sage: a == b - True - - sage: prime_powers(100) == [i for i in range(100) if is_prime_power(i)] - True - - sage: prime_powers(10,7) - [] - sage: prime_powers(-5) - [] - sage: prime_powers(-1,3) - [2] - - TESTS: - - Check that output are always Sage integers (:trac:`922`):: - - sage: v = prime_powers(10) - sage: type(v[0]) - - - sage: prime_powers(0,1) - [] - sage: prime_powers(2) - [] - sage: prime_powers(3) - [2] - - sage: prime_powers("foo") - Traceback (most recent call last): - ... - TypeError: unable to convert 'foo' to an integer - - sage: prime_powers(6, "bar") - Traceback (most recent call last): - ... - TypeError: unable to convert 'bar' to an integer - - Check that long input are accepted (:trac:`17852`):: - - sage: prime_powers(6l) - [2, 3, 4, 5] - sage: prime_powers(6l,10l) - [7, 8, 9] - """ - start = ZZ(start) - - ZZ_2 = Integer(2) - if stop is None: - stop = start - start = ZZ_2 - else: - stop = ZZ(stop) - - if stop <= ZZ_2 or start >= stop: - return [] - - output = [] - for p in fast_arith.prime_range(stop): - q = p - while q < start: - q *= p - while q < stop: - output.append(q) - q *= p - - output.sort() - return output - -def primes_first_n(n, leave_pari=False): - r""" - Return the first `n` primes. - - INPUT: - - - `n` - a nonnegative integer - - OUTPUT: - - - a list of the first `n` prime numbers. - - EXAMPLES:: - - sage: primes_first_n(10) - [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] - sage: len(primes_first_n(1000)) - 1000 - sage: primes_first_n(0) - [] - """ - if n < 0: - raise ValueError("n must be nonnegative") - if n < 1: - return [] - return fast_arith.prime_range(nth_prime(n) + 1) - -# -# This is from -# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/366178 -# It's impressively fast given that it's in Pure Python. -# -def eratosthenes(n): - r""" - Return a list of the primes `\leq n`. - - This is extremely slow and is for educational purposes only. - - INPUT: - - - ``n`` - a positive integer - - OUTPUT: - - - a list of primes less than or equal to n. - - - EXAMPLES:: - - sage: eratosthenes(3) - [2, 3] - sage: eratosthenes(50) - [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] - sage: len(eratosthenes(100)) - 25 - sage: eratosthenes(213) == prime_range(213) - True - """ - n = int(n) - - if n < 2: - return [] - elif n == 2: - return [ZZ(2)] - - s = range(3, n+3, 2) - mroot = int(n ** 0.5) - half = (n+1) // 2 - i = 0 - m = 3 - while m <= mroot: - if s[i]: - j = (m*m-3) // 2 - s[j] = 0 - while j < half: - s[j] = 0 - j += m - i = i+1 - m = 2*i+3 - - return [ZZ(2)] + [ZZ(x) for x in s if x and x <= n] - -def primes(start, stop=None, proof=None): - r""" - Returns an iterator over all primes between start and stop-1, - inclusive. This is much slower than ``prime_range``, but - potentially uses less memory. As with :func:`next_prime`, the optional - argument proof controls whether the numbers returned are - guaranteed to be prime or not. - - This command is like the xrange command, except it only iterates - over primes. In some cases it is better to use primes than - ``prime_range``, because primes does not build a list of all primes in - the range in memory all at once. However, it is potentially much - slower since it simply calls the :func:`next_prime` function - repeatedly, and :func:`next_prime` is slow. - - INPUT: - - - ``start`` - an integer - lower bound for the primes - - - ``stop`` - an integer (or infinity) optional argument - - giving upper (open) bound for the primes - - - ``proof`` - bool or None (default: None) If True, the function - yields only proven primes. If False, the function uses a - pseudo-primality test, which is much faster for really big - numbers but does not provide a proof of primality. If None, - uses the global default (see :mod:`sage.structure.proof.proof`) - - OUTPUT: - - - an iterator over primes from start to stop-1, inclusive - - - EXAMPLES:: - - sage: for p in primes(5,10): - ... print p - ... - 5 - 7 - sage: list(primes(13)) - [2, 3, 5, 7, 11] - sage: list(primes(10000000000, 10000000100)) - [10000000019, 10000000033, 10000000061, 10000000069, 10000000097] - sage: max(primes(10^100, 10^100+10^4, proof=False)) - 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009631 - sage: next(p for p in primes(10^20, infinity) if is_prime(2*p+1)) - 100000000000000001243 - - - TESTS:: - - sage: for a in range(-10, 50): - ... for b in range(-10, 50): - ... assert list(primes(a,b)) == list(filter(is_prime, xrange(a,b))) - ... - sage: sum(primes(-10, 9973, proof=False)) == sum(filter(is_prime, range(-10, 9973))) - True - sage: for p in primes(10, infinity): - ... if p > 20: break - ... print p - ... - 11 - 13 - 17 - 19 - sage: next(p for p in primes(10,oo)) # checks alternate infinity notation - 11 - """ - from sage.rings.infinity import infinity - - start = ZZ(start) - if stop is None: - stop = start - start = ZZ(2) - elif stop != infinity: - stop = ZZ(stop) - n = start - 1 - while True: - n = n.next_prime(proof) - if n < stop: - yield n - else: - return - -def next_prime_power(n): - """ - Return the smallest prime power greater than ``n``. - - Note that if ``n`` is a prime power, then this function does not return - ``n``, but the next prime power after ``n``. - - This function just calls the method - :meth:`Integer.next_prime_power() ` - of Integers. - - .. SEEALSO:: - - - :func:`is_prime_power` (and - :meth:`Integer.is_prime_power() `) - - :func:`previous_prime_power` (and - :meth:`Integer.previous_prime_power() `) - - EXAMPLES:: - - sage: next_prime_power(1) - 2 - sage: next_prime_power(2) - 3 - sage: next_prime_power(10) - 11 - sage: next_prime_power(7) - 8 - sage: next_prime_power(99) - 101 - - The same results can be obtained with:: - - sage: 1.next_prime_power() - 2 - sage: 2.next_prime_power() - 3 - sage: 10.next_prime_power() - 11 - - Note that `2` is the smallest prime power:: - - sage: next_prime_power(-10) - 2 - sage: next_prime_power(0) - 2 - """ - return ZZ(n).next_prime_power() - -def next_probable_prime(n): - """ - Returns the next probable prime after self, as determined by PARI. - - INPUT: - - - - ``n`` - an integer - - - EXAMPLES:: - - sage: next_probable_prime(-100) - 2 - sage: next_probable_prime(19) - 23 - sage: next_probable_prime(int(999999999)) - 1000000007 - sage: next_probable_prime(2^768) - 1552518092300708935148979488462502555256886017116696611139052038026050952686376886330878408828646477950487730697131073206171580044114814391444287275041181139204454976020849905550265285631598444825262999193716468750892846853816058039 - """ - return ZZ(n).next_probable_prime() - -def next_prime(n, proof=None): - """ - The next prime greater than the integer n. If n is prime, then this - function does not return n, but the next prime after n. If the - optional argument proof is False, this function only returns a - pseudo-prime, as defined by the PARI nextprime function. If it is - None, uses the global default (see :mod:`sage.structure.proof.proof`) - - INPUT: - - - - ``n`` - integer - - - ``proof`` - bool or None (default: None) - - - EXAMPLES:: - - sage: next_prime(-100) - 2 - sage: next_prime(1) - 2 - sage: next_prime(2) - 3 - sage: next_prime(3) - 5 - sage: next_prime(4) - 5 - - Notice that the next_prime(5) is not 5 but 7. - - :: - - sage: next_prime(5) - 7 - sage: next_prime(2004) - 2011 - """ - return ZZ(n).next_prime(proof) - -def previous_prime(n): - """ - The largest prime < n. The result is provably correct. If n <= 1, - this function raises a ValueError. - - EXAMPLES:: - - sage: previous_prime(10) - 7 - sage: previous_prime(7) - 5 - sage: previous_prime(8) - 7 - sage: previous_prime(7) - 5 - sage: previous_prime(5) - 3 - sage: previous_prime(3) - 2 - sage: previous_prime(2) - Traceback (most recent call last): - ... - ValueError: no previous prime - sage: previous_prime(1) - Traceback (most recent call last): - ... - ValueError: no previous prime - sage: previous_prime(-20) - Traceback (most recent call last): - ... - ValueError: no previous prime - """ - n = ZZ(n)-1 - if n <= 1: - raise ValueError("no previous prime") - if n <= 3: - return ZZ(n) - if n%2 == 0: - n -= 1 - while not is_prime(n): - n -= 2 - return ZZ(n) - -def previous_prime_power(n): - r""" - Return the largest prime power smaller than ``n``. - - The result is provably correct. If ``n`` is smaller or equal than ``2`` this - function raises an error. - - This function simply call the method - :meth:`Integer.previous_prime_power() ` - of Integers. - - .. SEEALSO:: - - - :func:`is_prime_power` (and :meth:`Integer.is_prime_power() - `) - - :func:`next_prime_power` (and :meth:`Integer.next_prime_power() - `) - - EXAMPLES:: - - sage: previous_prime_power(3) - 2 - sage: previous_prime_power(10) - 9 - sage: previous_prime_power(7) - 5 - sage: previous_prime_power(127) - 125 - - The same results can be obtained with:: - - sage: 3.previous_prime_power() - 2 - sage: 10.previous_prime_power() - 9 - sage: 7.previous_prime_power() - 5 - sage: 127.previous_prime_power() - 125 - - Input less than or equal to `2` raises errors:: - - sage: previous_prime_power(2) - Traceback (most recent call last): - ... - ValueError: no prime power less than 2 - sage: previous_prime_power(-10) - Traceback (most recent call last): - ... - ValueError: no prime power less than 2 - - :: - - sage: n = previous_prime_power(2^16 - 1) - sage: while is_prime(n): - ....: n = previous_prime_power(n) - sage: factor(n) - 251^2 - """ - return ZZ(n).previous_prime_power() - -def random_prime(n, proof=None, lbound=2): - """ - Returns a random prime p between `lbound` and n (i.e. `lbound <= p <= n`). - The returned prime is chosen uniformly at random from the set of prime - numbers less than or equal to n. - - INPUT: - - - ``n`` - an integer >= 2. - - - ``proof`` - bool or None (default: None) If False, the function uses a - pseudo-primality test, which is much faster for really big numbers but - does not provide a proof of primality. If None, uses the global default - (see :mod:`sage.structure.proof.proof`) - - - ``lbound`` - an integer >= 2 - lower bound for the chosen primes - - EXAMPLES:: - - sage: random_prime(100000) - 88237 - sage: random_prime(2) - 2 - - Here we generate a random prime between 100 and 200:: - - sage: random_prime(200, lbound=100) - 149 - - If all we care about is finding a pseudo prime, then we can pass - in ``proof=False`` :: - - sage: random_prime(200, proof=False, lbound=100) - 149 - - TESTS:: - - sage: type(random_prime(2)) - - sage: type(random_prime(100)) - - sage: random_prime(1, lbound=-2) #caused Sage hang #10112 - Traceback (most recent call last): - ... - ValueError: n must be greater than or equal to 2 - sage: random_prime(126, lbound=114) - Traceback (most recent call last): - ... - ValueError: There are no primes between 114 and 126 (inclusive) - - - AUTHORS: - - - Jon Hanke (2006-08-08): with standard Stein cleanup - - - Jonathan Bober (2007-03-17) - """ - # since we don't want current_randstate to get - # pulled when you say "from sage.arith import *". - from sage.misc.randstate import current_randstate - from sage.structure.proof.proof import get_flag - proof = get_flag(proof, "arithmetic") - n = ZZ(n) - if n < 2: - raise ValueError("n must be greater than or equal to 2") - if n < lbound: - raise ValueError("n must be at least lbound: %s"%(lbound)) - elif n == 2: - return n - lbound = max(2, lbound) - if lbound > 2: - if lbound == 3 or n <= 2*lbound - 2: - # check for Betrand's postulate (proved by Chebyshev) - if lbound < 25 or n <= 6*lbound/5: - # see J. Nagura, Proc. Japan Acad. 28, (1952). 177-181. - if lbound < 2010760 or n <= 16598*lbound/16597: - # see L. Schoenfeld, Math. Comp. 30 (1976), no. 134, 337-360. - if proof: - smallest_prime = ZZ(lbound-1).next_prime() - else: - smallest_prime = ZZ(lbound-1).next_probable_prime() - if smallest_prime > n: - raise ValueError("There are no primes between %s and %s (inclusive)" % (lbound, n)) - - if proof: - prime_test = is_prime - else: - prime_test = is_pseudoprime - randint = current_randstate().python_random().randint - while True: - # In order to ensure that the returned prime is chosen - # uniformly from the set of primes it is necessary to - # choose a random number and then test for primality. - # The method of choosing a random number and then returning - # the closest prime smaller than it would typically not, - # for example, return the first of a pair of twin primes. - p = randint(lbound, n) - if prime_test(p): - return ZZ(p) - - -def divisors(n): - """ - Returns a list of all positive integer divisors of the nonzero - integer n. - - INPUT: - - - - ``n`` - the element - - - EXAMPLES:: - - sage: divisors(-3) - [1, 3] - sage: divisors(6) - [1, 2, 3, 6] - sage: divisors(28) - [1, 2, 4, 7, 14, 28] - sage: divisors(2^5) - [1, 2, 4, 8, 16, 32] - sage: divisors(100) - [1, 2, 4, 5, 10, 20, 25, 50, 100] - sage: divisors(1) - [1] - sage: divisors(0) - Traceback (most recent call last): - ... - ValueError: n must be nonzero - sage: divisors(2^3 * 3^2 * 17) - [1, 2, 3, 4, 6, 8, 9, 12, 17, 18, 24, 34, 36, 51, 68, 72, 102, 136, 153, 204, 306, 408, 612, 1224] - - This function works whenever one has unique factorization:: - - sage: K. = QuadraticField(7) - sage: divisors(K.ideal(7)) - [Fractional ideal (1), Fractional ideal (a), Fractional ideal (7)] - sage: divisors(K.ideal(3)) - [Fractional ideal (1), Fractional ideal (3), Fractional ideal (-a + 2), Fractional ideal (-a - 2)] - sage: divisors(K.ideal(35)) - [Fractional ideal (1), Fractional ideal (5), Fractional ideal (a), Fractional ideal (7), Fractional ideal (5*a), Fractional ideal (35)] - - TESTS:: - - sage: divisors(int(300)) - [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300] - """ - if not n: - raise ValueError("n must be nonzero") - - if isinstance(n, (int, long)): - n = ZZ(n) # we have specialized code for this case, make sure it gets used - - try: - return n.divisors() - except AttributeError: - pass - - f = factor(n) - one = parent(n)(1) - output = [one] - for p, e in f: - prev = output[:] - pn = one - for i in range(e): - pn *= p - output.extend(a*pn for a in prev) - output.sort() - return output - -class Sigma: - """ - Return the sum of the k-th powers of the divisors of n. - - INPUT: - - - - ``n`` - integer - - - ``k`` - integer (default: 1) - - - OUTPUT: integer - - EXAMPLES:: - - sage: sigma(5) - 6 - sage: sigma(5,2) - 26 - - The sigma function also has a special plotting method. - - :: - - sage: P = plot(sigma, 1, 100) - - This method also works with k-th powers. - - :: - - sage: P = plot(sigma, 1, 100, k=2) - - AUTHORS: - - - William Stein: original implementation - - - Craig Citro (2007-06-01): rewrote for huge speedup - - TESTS:: - - sage: sigma(100,4) - 106811523 - sage: sigma(factorial(100),3).mod(144169) - 3672 - sage: sigma(factorial(150),12).mod(691) - 176 - sage: RR(sigma(factorial(133),20)) - 2.80414775675747e4523 - sage: sigma(factorial(100),0) - 39001250856960000 - sage: sigma(factorial(41),1) - 229199532273029988767733858700732906511758707916800 - """ - def __repr__(self): - """ - A description of this class, which computes the sum of the - k-th powers of the divisors of n. - - EXAMPLES:: - - sage: Sigma().__repr__() - 'Function that adds up (k-th powers of) the divisors of n' - """ - return "Function that adds up (k-th powers of) the divisors of n" - - def __call__(self, n, k=1): - """ - Computes the sum of (the k-th powers of) the divisors of n. - - EXAMPLES:: - - sage: q = Sigma() - sage: q(10) - 18 - sage: q(10,2) - 130 - """ - n = ZZ(n) - k = ZZ(k) - one = ZZ(1) - - if (k == ZZ(0)): - return prod(expt+one for p, expt in factor(n)) - elif (k == one): - return prod((p**(expt+one) - one).divide_knowing_divisible_by(p - one) - for p, expt in factor(n)) - else: - return prod((p**((expt+one)*k)-one).divide_knowing_divisible_by(p**k-one) - for p,expt in factor(n)) - - def plot(self, xmin=1, xmax=50, k=1, pointsize=30, rgbcolor=(0,0,1), join=True, - **kwds): - """ - Plot the sigma (sum of k-th powers of divisors) function. - - INPUT: - - - - ``xmin`` - default: 1 - - - ``xmax`` - default: 50 - - - ``k`` - default: 1 - - - ``pointsize`` - default: 30 - - - ``rgbcolor`` - default: (0,0,1) - - - ``join`` - default: True; whether to join the - points. - - - ``**kwds`` - passed on - - EXAMPLES:: - - sage: p = Sigma().plot() - sage: p.ymax() - 124.0 - """ - v = [(n,sigma(n,k)) for n in range(xmin,xmax + 1)] - from sage.plot.all import list_plot - P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) - if join: - P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) - return P - -sigma = Sigma() - -def gcd(a, b=None, **kwargs): - r""" - The greatest common divisor of a and b, or if a is a list and b is - omitted the greatest common divisor of all elements of a. - - INPUT: - - - - ``a,b`` - two elements of a ring with gcd or - - - ``a`` - a list or tuple of elements of a ring with - gcd - - - Additional keyword arguments are passed to the respectively called - methods. - - OUTPUT: - - The given elements are first coerced into a common parent. Then, - their greatest common divisor *in that common parent* is returned. - - EXAMPLES:: - - sage: GCD(97,100) - 1 - sage: GCD(97*10^15, 19^20*97^2) - 97 - sage: GCD(2/3, 4/5) - 2/15 - sage: GCD([2,4,6,8]) - 2 - sage: GCD(srange(0,10000,10)) # fast !! - 10 - - Note that to take the gcd of `n` elements for `n \not= 2` you must - put the elements into a list by enclosing them in ``[..]``. Before - #4988 the following wrongly returned 3 since the third parameter - was just ignored:: - - sage: gcd(3,6,2) - Traceback (most recent call last): - ... - TypeError: gcd() takes at most 2 arguments (3 given) - sage: gcd([3,6,2]) - 1 - - Similarly, giving just one element (which is not a list) gives an error:: - - sage: gcd(3) - Traceback (most recent call last): - ... - TypeError: 'sage.rings.integer.Integer' object is not iterable - - By convention, the gcd of the empty list is (the integer) 0:: - - sage: gcd([]) - 0 - sage: type(gcd([])) - - - TESTS: - - The following shows that indeed coercion takes place before computing - the gcd. This behaviour was introduced in :trac:`10771`:: - - sage: R.=QQ[] - sage: S.=ZZ[] - sage: p = S.random_element(degree=(0,10)) - sage: q = R.random_element(degree=(0,10)) - sage: parent(gcd(1/p,q)) - Fraction Field of Univariate Polynomial Ring in x over Rational Field - sage: parent(gcd([1/p,q])) - Fraction Field of Univariate Polynomial Ring in x over Rational Field - - Make sure we try QQ and not merely ZZ (:trac:`13014`):: - - sage: bool(gcd(2/5, 3/7) == gcd(SR(2/5), SR(3/7))) - True - - Make sure that the gcd of Expressions stays symbolic:: - - sage: parent(gcd(2, 4)) - Integer Ring - sage: parent(gcd(SR(2), 4)) - Symbolic Ring - sage: parent(gcd(2, SR(4))) - Symbolic Ring - sage: parent(gcd(SR(2), SR(4))) - Symbolic Ring - - Verify that objects without gcd methods but which can't be - coerced to ZZ or QQ raise an error:: - - sage: F. = FreeMonoid(2) - sage: gcd(a,b) - Traceback (most recent call last): - ... - TypeError: unable to find gcd - - """ - # Most common use case first: - if b is not None: - try: - return a.gcd(b, **kwargs) - except (AttributeError, TypeError): - pass - try: - return ZZ(a).gcd(ZZ(b)) - except TypeError: - raise TypeError("unable to find gcd") - - from sage.structure.sequence import Sequence - seq = Sequence(a) - U = seq.universe() - if U is ZZ or U is int or U is long:# ZZ.has_coerce_map_from(U): - return GCD_list(a) - return __GCD_sequence(seq, **kwargs) - -GCD = gcd - -def __GCD_sequence(v, **kwargs): - """ - Internal function returning the gcd of the elements of a sequence - - INPUT: - - - - ``v`` - A sequence (possibly empty) - - - OUTPUT: The gcd of the elements of the sequence as an element of - the sequence's universe, or the integer 0 if the sequence is - empty. - - EXAMPLES:: - - sage: from sage.rings.arith import __GCD_sequence - sage: from sage.structure.sequence import Sequence - sage: l = () - sage: __GCD_sequence(l) - 0 - sage: __GCD_sequence(Sequence(srange(10))) - 1 - sage: X=polygen(QQ) - sage: __GCD_sequence(Sequence((2*X+4,2*X^2,2))) - 1 - sage: X=polygen(ZZ) - sage: __GCD_sequence(Sequence((2*X+4,2*X^2,2))) - 2 - """ - if len(v) == 0: - return ZZ(0) - if hasattr(v,'universe'): - g = v.universe()(0) - else: - g = ZZ(0) - one = v.universe()(1) - for vi in v: - g = vi.gcd(g, **kwargs) - if g == one: - return g - return g - -def lcm(a, b=None): - """ - The least common multiple of a and b, or if a is a list and b is - omitted the least common multiple of all elements of a. - - Note that LCM is an alias for lcm. - - INPUT: - - - - ``a,b`` - two elements of a ring with lcm or - - - ``a`` - a list or tuple of elements of a ring with - lcm - - OUTPUT: - - First, the given elements are coerced into a common parent. Then, - their least common multiple *in that parent* is returned. - - EXAMPLES:: - - sage: lcm(97,100) - 9700 - sage: LCM(97,100) - 9700 - sage: LCM(0,2) - 0 - sage: LCM(-3,-5) - 15 - sage: LCM([1,2,3,4,5]) - 60 - sage: v = LCM(range(1,10000)) # *very* fast! - sage: len(str(v)) - 4349 - - - TESTS: - - The following tests against a bug that was fixed in :trac:`10771`:: - - sage: lcm(4/1,2) - 4 - - The following shows that indeed coercion takes place before - computing the least common multiple:: - - sage: R.=QQ[] - sage: S.=ZZ[] - sage: p = S.random_element(degree=(0,5)) - sage: q = R.random_element(degree=(0,5)) - sage: parent(lcm([1/p,q])) - Fraction Field of Univariate Polynomial Ring in x over Rational Field - - Make sure we try QQ and not merely ZZ (:trac:`13014`):: - - sage: bool(lcm(2/5, 3/7) == lcm(SR(2/5), SR(3/7))) - True - - Make sure that the lcm of Expressions stays symbolic:: - - sage: parent(lcm(2, 4)) - Integer Ring - sage: parent(lcm(SR(2), 4)) - Symbolic Ring - sage: parent(lcm(2, SR(4))) - Symbolic Ring - sage: parent(lcm(SR(2), SR(4))) - Symbolic Ring - - Verify that objects without lcm methods but which can't be - coerced to ZZ or QQ raise an error:: - - sage: F. = FreeMonoid(2) - sage: lcm(a,b) - Traceback (most recent call last): - ... - TypeError: unable to find lcm - - Check rational and integers (:trac:`17852`):: - - sage: lcm(1/2, 4) - 4 - sage: lcm(4, 1/2) - 4 - """ - # Most common use case first: - if b is not None: - try: - return a.lcm(b) - except (AttributeError,TypeError): - pass - try: - return ZZ(a).lcm(ZZ(b)) - except TypeError: - raise TypeError("unable to find lcm") - - from sage.structure.sequence import Sequence - seq = Sequence(a) - U = seq.universe() - if U is ZZ or U is int or U is long: - return LCM_list(a) - return __LCM_sequence(seq) - -LCM = lcm - -def __LCM_sequence(v): - """ - Internal function returning the lcm of the elements of a sequence - - INPUT: - - - - ``v`` - A sequence (possibly empty) - - - OUTPUT: The lcm of the elements of the sequence as an element of - the sequence's universe, or the integer 1 if the sequence is - empty. - - EXAMPLES:: - - sage: from sage.structure.sequence import Sequence - sage: from sage.rings.arith import __LCM_sequence - sage: l = Sequence(()) - sage: __LCM_sequence(l) - 1 - - This is because lcm(0,x)=0 for all x (by convention) - - :: - - sage: __LCM_sequence(Sequence(srange(100))) - 0 - - So for the lcm of all integers up to 10 you must do this:: - - sage: __LCM_sequence(Sequence(srange(1,100))) - 69720375229712477164533808935312303556800 - - Note that the following example did not work in QQ[] as of 2.11, - but does in 3.1.4; the answer is different, though equivalent:: - - sage: R.=ZZ[] - sage: __LCM_sequence(Sequence((2*X+4,2*X^2,2))) - 2*X^3 + 4*X^2 - sage: R.=QQ[] - sage: __LCM_sequence(Sequence((2*X+4,2*X^2,2))) - X^3 + 2*X^2 - """ - if len(v) == 0: - return ZZ(1) - try: - g = v.universe()(1) - except AttributeError: - g = ZZ(1) - for vi in v: - g = vi.lcm(g) - if not g: - return g - return g - -def xlcm(m, n): - r""" - Extended lcm function: given two positive integers `m,n`, returns - a triple `(l,m_1,n_1)` such that `l=\mathop{\mathrm{lcm}}(m,n)=m_1 - \cdot n_1` where `m_1|m`, `n_1|n` and `\gcd(m_1,n_1)=1`, all with no - factorization. - - Used to construct an element of order `l` from elements of orders `m,n` - in any group: see sage/groups/generic.py for examples. - - EXAMPLES:: - - sage: xlcm(120,36) - (360, 40, 9) - """ - g = gcd(m, n) - l = m*n//g # = lcm(m, n) - g = gcd(m, n//g) # divisible by those primes which divide n to a - # higher power than m - - while not g==1: - m //= g - g = gcd(m, g) - - n = l//m - return (l, m, n) - -def xgcd(a, b): - r""" - Return a triple ``(g,s,t)`` such that `g = s\cdot a+t\cdot b = \gcd(a,b)`. - - .. NOTE:: - - One exception is if `a` and `b` are not in a principal ideal domain (see - :wikipedia:`Principal_ideal_domain`), e.g., they are both polynomials - over the integers. Then this function can't in general return ``(g,s,t)`` - as above, since they need not exist. Instead, over the integers, we - first multiply `g` by a divisor of the resultant of `a/g` and `b/g`, up - to sign. - - INPUT: - - - ``a, b`` - integers or more generally, element of a ring for which the - xgcd make sense (e.g. a field or univariate polynomials). - - OUTPUT: - - - ``g, s, t`` - such that `g = s\cdot a + t\cdot b` - - .. NOTE:: - - There is no guarantee that the returned cofactors (s and t) are - minimal. - - EXAMPLES:: - - sage: xgcd(56, 44) - (4, 4, -5) - sage: 4*56 + (-5)*44 - 4 - - sage: g, a, b = xgcd(5/1, 7/1); g, a, b - (1, 3, -2) - sage: a*(5/1) + b*(7/1) == g - True - - sage: x = polygen(QQ) - sage: xgcd(x^3 - 1, x^2 - 1) - (x - 1, 1, -x) - - sage: K. = NumberField(x^2-3) - sage: g.xgcd(g+2) - (1, 1/3*g, 0) - - sage: R. = K[] - sage: S. = R.fraction_field()[] - sage: xgcd(y^2, a*y+b) - (1, a^2/b^2, ((-a)/b^2)*y + 1/b) - sage: xgcd((b+g)*y^2, (a+g)*y+b) - (1, (a^2 + (2*g)*a + 3)/(b^3 + (g)*b^2), ((-a + (-g))/b^2)*y + 1/b) - - Here is an example of a xgcd for two polynomials over the integers, where the linear - combination is not the gcd but the gcd multiplied by the resultant:: - - sage: R. = ZZ[] - sage: gcd(2*x*(x-1), x^2) - x - sage: xgcd(2*x*(x-1), x^2) - (2*x, -1, 2) - sage: (2*(x-1)).resultant(x) - 2 - """ - try: - return a.xgcd(b) - except AttributeError: - pass - return ZZ(a).xgcd(ZZ(b)) - -XGCD = xgcd - -## def XGCD_python(a, b): -## """ -## Returns triple (g,p,q) such that g = p*a+b*q = GCD(a,b). -## This function should behave exactly the same as XGCD, -## but is implemented in pure python. -## """ -## if a == 0 and b == 0: -## return (0,0,1) -## if a == 0: -## return (abs(b), 0, b/abs(b)) -## if b == 0: -## return (abs(a), a/abs(a), 0) -## psign = 1 -## qsign = 1 -## if a < 0: -## a = -a -## psign = -1 -## if b < 0: -## b = -b -## qsign = -1 -## p = 1; q = 0; r = 0; s = 1 -## while b != 0: -## c = a % b -## quot = a/b -## a = b; b = c -## new_r = p - quot*r -## new_s = q - quot*s -## p = r; q = s -## r = new_r; s = new_s -## return (a, p*psign, q*qsign) - - -def xkcd(n=""): - r""" - This function is similar to the xgcd function, but behaves - in a completely different way. - - INPUT: - - - ``n`` - an integer (optional) - - OUTPUT: - - This function outputs nothing it just prints something. Note that this - function does not feel itself at ease in a html deprived environment. - - EXAMPLES:: - - sage: xkcd(353) # optional - internet -

Python

- """ - import contextlib - import json - from sage.misc.html import html - - # import compatible with py2 and py3 - from six.moves.urllib.request import urlopen - from six.moves.urllib.error import HTTPError, URLError - - data = None - url = "http://dynamic.xkcd.com/api-0/jsonp/comic/{}".format(n) - - try: - with contextlib.closing(urlopen(url)) as f: - data = f.read() - except HTTPError as error: - if error.getcode() == 400: # this error occurs when asking for a non valid comic number - raise RuntimeError("Could not obtain comic data from {}. Maybe you should enable time travel!".format(url)) - except URLError: - pass - - if n == 1024: - data = None - - if data: - data = json.loads(data) - img = data['img'] - alt = data['alt'] - title = data['safe_title'] - link = "http://xkcd.com/{}".format(data['num']) - html('

{}

'.format(title, img, alt) - + '
Source: {0}
'.format(link)) - return - - # TODO: raise this error in such a way that it's not clear that - # it is produced by sage, see http://xkcd.com/1024/ - html('') - - -def inverse_mod(a, m): - """ - The inverse of the ring element a modulo m. - - If no special inverse_mod is defined for the elements, it tries to - coerce them into integers and perform the inversion there - - :: - - sage: inverse_mod(7,1) - 0 - sage: inverse_mod(5,14) - 3 - sage: inverse_mod(3,-5) - 2 - """ - try: - return a.inverse_mod(m) - except AttributeError: - return Integer(a).inverse_mod(m) - -####################################################### -# Functions to find the fastest available commands -# for gcd and inverse_mod -####################################################### - -def get_gcd(order): - """ - Return the fastest gcd function for integers of size no larger than - order. - - EXAMPLES:: - - sage: sage.rings.arith.get_gcd(4000) - - sage: sage.rings.arith.get_gcd(400000) - - sage: sage.rings.arith.get_gcd(4000000000) - - """ - if order <= 46340: # todo: don't hard code - return fast_arith.arith_int().gcd_int - elif order <= 2147483647: # todo: don't hard code - return fast_arith.arith_llong().gcd_longlong - else: - return gcd - -def get_inverse_mod(order): - """ - Return the fastest inverse_mod function for integers of size no - larger than order. - - EXAMPLES:: - - sage: sage.rings.arith.get_inverse_mod(6000) - - sage: sage.rings.arith.get_inverse_mod(600000) - - sage: sage.rings.arith.get_inverse_mod(6000000000) - - """ - if order <= 46340: # todo: don't hard code - return fast_arith.arith_int().inverse_mod_int - elif order <= 2147483647: # todo: don't hard code - return fast_arith.arith_llong().inverse_mod_longlong - else: - return inverse_mod - -# def sqrt_mod(a, m): -# """A square root of a modulo m.""" - -# def xxx_inverse_mod(a, m): -# """The inverse of a modulo m.""" -# g,s,t = XGCD(a,m) -# if g != 1: -# raise "inverse_mod(a=%s,m=%s), error since GCD=%s"%(a,m,g) -# return s - -def power_mod(a,n,m): - """ - The n-th power of a modulo the integer m. - - EXAMPLES:: - - sage: power_mod(0,0,5) - Traceback (most recent call last): - ... - ArithmeticError: 0^0 is undefined. - sage: power_mod(2,390,391) - 285 - sage: power_mod(2,-1,7) - 4 - sage: power_mod(11,1,7) - 4 - sage: R. = ZZ[] - sage: power_mod(3*x, 10, 7) - 4*x^10 - - sage: power_mod(11,1,0) - Traceback (most recent call last): - ... - ZeroDivisionError: modulus must be nonzero. - """ - if m==0: - raise ZeroDivisionError("modulus must be nonzero.") - if m==1: - return 0 - if n < 0: - ainv = inverse_mod(a,m) - return power_mod(ainv, -n, m) - if n==0: - if a == 0: - raise ArithmeticError("0^0 is undefined.") - return 1 - - apow = a % m - while n&1 == 0: - apow = (apow*apow) % m - n = n >> 1 - power = apow - n = n >> 1 - while n != 0: - apow = (apow*apow) % m - if n&1 != 0: - power = (power*apow) % m - n = n >> 1 - - return power - - -def rational_reconstruction(a, m, algorithm='fast'): - r""" - This function tries to compute `x/y`, where `x/y` is a rational number in - lowest terms such that the reduction of `x/y` modulo `m` is equal to `a` and - the absolute values of `x` and `y` are both `\le \sqrt{m/2}`. If such `x/y` - exists, that pair is unique and this function returns it. If no - such pair exists, this function raises ZeroDivisionError. - - An efficient algorithm for computing rational reconstruction is - very similar to the extended Euclidean algorithm. For more details, - see Knuth, Vol 2, 3rd ed, pages 656-657. - - INPUT: - - - ``a`` -- an integer - - - ``m`` -- a modulus - - - ``algorithm`` -- (default: 'fast') - - - ``'fast'`` - a fast implementation using direct MPIR calls - in Cython. - - OUTPUT: - - Numerator and denominator `n`, `d` of the unique rational number - `r=n/d`, if it exists, with `n` and `|d| \le \sqrt{N/2}`. Return - `(0,0)` if no such number exists. - - The algorithm for rational reconstruction is described (with a - complete nontrivial proof) on pages 656-657 of Knuth, Vol 2, 3rd - ed. as the solution to exercise 51 on page 379. See in particular - the conclusion paragraph right in the middle of page 657, which - describes the algorithm thus: - - This discussion proves that the problem can be solved - efficiently by applying Algorithm 4.5.2X with `u=m` and `v=a`, - but with the following replacement for step X2: If - `v3 \le \sqrt{m/2}`, the algorithm terminates. The pair - `(x,y)=(|v2|,v3*\mathrm{sign}(v2))` is then the unique - solution, provided that `x` and `y` are coprime and - `x \le \sqrt{m/2}`; otherwise there is no solution. (Alg 4.5.2X is - the extended Euclidean algorithm.) - - Knuth remarks that this algorithm is due to Wang, Kornerup, and - Gregory from around 1983. - - EXAMPLES:: - - sage: m = 100000 - sage: (119*inverse_mod(53,m))%m - 11323 - sage: rational_reconstruction(11323,m) - 119/53 - - :: - - sage: rational_reconstruction(400,1000) - Traceback (most recent call last): - ... - ArithmeticError: rational reconstruction of 400 (mod 1000) does not exist - - :: - - sage: rational_reconstruction(3, 292393) - 3 - sage: a = Integers(292393)(45/97); a - 204977 - sage: rational_reconstruction(a, 292393, algorithm='fast') - 45/97 - sage: rational_reconstruction(293048, 292393) - Traceback (most recent call last): - ... - ArithmeticError: rational reconstruction of 655 (mod 292393) does not exist - sage: rational_reconstruction(0, 0) - Traceback (most recent call last): - ... - ZeroDivisionError: rational reconstruction with zero modulus - sage: rational_reconstruction(0, 1, algorithm="foobar") - Traceback (most recent call last): - ... - ValueError: unknown algorithm 'foobar' - """ - if algorithm == 'fast': - return ZZ(a).rational_reconstruction(m) - elif algorithm == 'python': - from sage.misc.superseded import deprecation - deprecation(17180, 'The %r algorithm for rational_reconstruction is deprecated' % algorithm) - return ZZ(a).rational_reconstruction(m) - else: - raise ValueError("unknown algorithm %r" % algorithm) - -def mqrr_rational_reconstruction(u, m, T): - r""" - Maximal Quotient Rational Reconstruction. - - For research purposes only - this is pure Python, so slow. - - INPUT: - - - ``u, m, T`` - integers such that `m > u \ge 0`, `T > 0`. - - OUTPUT: - - Either integers `n,d` such that `d>0`, `\mathop{\mathrm{gcd}}(n,d)=1`, `n/d=u \bmod m`, and - `T \cdot d \cdot |n| < m`, or ``None``. - - Reference: Monagan, Maximal Quotient Rational Reconstruction: An - Almost Optimal Algorithm for Rational Reconstruction (page 11) - - This algorithm is probabilistic. - - EXAMPLES:: - - sage: mqrr_rational_reconstruction(21,3100,13) - (21, 1) - """ - if u == 0: - if m > T: - return (0,1) - else: - return None - n, d = 0, 0 - t0, r0 = 0, m - t1, r1 = 1, u - while r1 != 0 and r0 > T: - q = r0/r1 # C division implicit floor - if q > T: - n, d, T = r1, t1, q - r0, r1 = r1, r0 - q*r1 - t0, t1 = t1, t0 - q*t1 - if d != 0 and GCD(n,d) == 1: - return (n,d) - return None - - -###################### - - -def trial_division(n, bound=None): - """ - Return the smallest prime divisor <= bound of the positive integer - n, or n if there is no such prime. If the optional argument bound - is omitted, then bound <= n. - - INPUT: - - - ``n`` - a positive integer - - - ``bound`` - (optional) a positive integer - - OUTPUT: - - - ``int`` - a prime p=bound that divides n, or n if - there is no such prime. - - - EXAMPLES:: - - sage: trial_division(15) - 3 - sage: trial_division(91) - 7 - sage: trial_division(11) - 11 - sage: trial_division(387833, 300) - 387833 - sage: # 300 is not big enough to split off a - sage: # factor, but 400 is. - sage: trial_division(387833, 400) - 389 - """ - if bound is None: - return ZZ(n).trial_division() - else: - return ZZ(n).trial_division(bound) - -def factor(n, proof=None, int_=False, algorithm='pari', verbose=0, **kwds): - """ - Returns the factorization of ``n``. The result depends on the - type of ``n``. - - If ``n`` is an integer, returns the factorization as an object - of type ``Factorization``. - - If n is not an integer, ``n.factor(proof=proof, **kwds)`` gets called. - See ``n.factor??`` for more documentation in this case. - - .. warning:: - - This means that applying ``factor`` to an integer result of - a symbolic computation will not factor the integer, because it is - considered as an element of a larger symbolic ring. - - EXAMPLE:: - - sage: f(n)=n^2 - sage: is_prime(f(3)) - False - sage: factor(f(3)) - 9 - - INPUT: - - - ``n`` - an nonzero integer - - - ``proof`` - bool or None (default: None) - - - ``int_`` - bool (default: False) whether to return - answers as Python ints - - - ``algorithm`` - string - - - ``'pari'`` - (default) use the PARI c library - - - ``'kash'`` - use KASH computer algebra system (requires the - optional kash package be installed) - - - ``'magma'`` - use Magma (requires magma be installed) - - - ``verbose`` - integer (default: 0); PARI's debug - variable is set to this; e.g., set to 4 or 8 to see lots of output - during factorization. - - OUTPUT: - - - factorization of n - - The qsieve and ecm commands give access to highly optimized - implementations of algorithms for doing certain integer - factorization problems. These implementations are not used by the - generic factor command, which currently just calls PARI (note that - PARI also implements sieve and ecm algorithms, but they aren't as - optimized). Thus you might consider using them instead for certain - numbers. - - The factorization returned is an element of the class - :class:`~sage.structure.factorization.Factorization`; see Factorization?? - for more details, and examples below for usage. A Factorization contains - both the unit factor (+1 or -1) and a sorted list of (prime, exponent) - pairs. - - The factorization displays in pretty-print format but it is easy to - obtain access to the (prime,exponent) pairs and the unit, to - recover the number from its factorization, and even to multiply two - factorizations. See examples below. - - EXAMPLES:: - - sage: factor(500) - 2^2 * 5^3 - sage: factor(-20) - -1 * 2^2 * 5 - sage: f=factor(-20) - sage: list(f) - [(2, 2), (5, 1)] - sage: f.unit() - -1 - sage: f.value() - -20 - sage: factor( -next_prime(10^2) * next_prime(10^7) ) - -1 * 101 * 10000019 - - :: - - sage: factor(-500, algorithm='kash') # optional - kash - -1 * 2^2 * 5^3 - - :: - - sage: factor(-500, algorithm='magma') # optional - magma - -1 * 2^2 * 5^3 - - :: - - sage: factor(0) - Traceback (most recent call last): - ... - ArithmeticError: Prime factorization of 0 not defined. - sage: factor(1) - 1 - sage: factor(-1) - -1 - sage: factor(2^(2^7)+1) - 59649589127497217 * 5704689200685129054721 - - Sage calls PARI's factor, which has proof False by default. - Sage has a global proof flag, set to True by default (see - :mod:`sage.structure.proof.proof`, or proof.[tab]). To override - the default, call this function with proof=False. - - :: - - sage: factor(3^89-1, proof=False) - 2 * 179 * 1611479891519807 * 5042939439565996049162197 - - :: - - sage: factor(2^197 + 1) # long time (2s) - 3 * 197002597249 * 1348959352853811313 * 251951573867253012259144010843 - - Any object which has a factor method can be factored like this:: - - sage: K. = QuadraticField(-1) - sage: factor(122 - 454*i) - (-3*i - 2) * (-i - 2)^3 * (i + 1)^3 * (i + 4) - - To access the data in a factorization:: - - sage: f = factor(420); f - 2^2 * 3 * 5 * 7 - sage: [x for x in f] - [(2, 2), (3, 1), (5, 1), (7, 1)] - sage: [p for p,e in f] - [2, 3, 5, 7] - sage: [e for p,e in f] - [2, 1, 1, 1] - sage: [p^e for p,e in f] - [4, 3, 5, 7] - - """ - if isinstance(n, (int, long)): - n = ZZ(n) - - if isinstance(n, Integer): - return n.factor(proof=proof, algorithm=algorithm, - int_ = int_, verbose=verbose) - else: - # e.g. n = x**2 + y**2 + 2*x*y - try: - return n.factor(proof=proof, **kwds) - except AttributeError: - raise TypeError("unable to factor n") - except TypeError: - # Just in case factor method doesn't have a proof option. - try: - return n.factor(**kwds) - except AttributeError: - raise TypeError("unable to factor n") - -def radical(n, *args, **kwds): - """ - Return the product of the prime divisors of n. - - This calls ``n.radical(*args, **kwds)``. If that doesn't work, it - does ``n.factor(*args, **kwds)`` and returns the product of the prime - factors in the resulting factorization. - - EXAMPLES:: - - sage: radical(2 * 3^2 * 5^5) - 30 - sage: radical(0) - Traceback (most recent call last): - ... - ArithmeticError: Radical of 0 not defined. - sage: K. = QuadraticField(-1) - sage: radical(K(2)) - i + 1 - - The next example shows how to compute the radical of a number, - assuming no prime > 100000 has exponent > 1 in the factorization:: - - sage: n = 2^1000-1; n / radical(n, limit=100000) - 125 - """ - try: - return n.radical(*args, **kwds) - except AttributeError: - return n.factor(*args, **kwds).radical_value() - -def prime_divisors(n): - """ - The prime divisors of ``n``. - - INPUT: - - - ``n`` -- any object which can be factored - - OUTPUT: - - A list of prime factors of ``n``. For integers, this list is sorted - in increasing order. - - EXAMPLES:: - - sage: prime_divisors(1) - [] - sage: prime_divisors(100) - [2, 5] - sage: prime_divisors(2004) - [2, 3, 167] - - If ``n`` is negative, we do *not* include -1 among the prime - divisors, since -1 is not a prime number:: - - sage: prime_divisors(-100) - [2, 5] - - For polynomials we get all irreducible factors:: - - sage: R. = PolynomialRing(QQ) - sage: prime_divisors(x^12 - 1) - [x - 1, x + 1, x^2 - x + 1, x^2 + 1, x^2 + x + 1, x^4 - x^2 + 1] - """ - try: - return n.prime_divisors() - except AttributeError: - pass - return [p for p,_ in factor(n)] - -prime_factors = prime_divisors - -def odd_part(n): - r""" - The odd part of the integer `n`. This is `n / 2^v`, - where `v = \mathrm{valuation}(n,2)`. - - EXAMPLES:: - - sage: odd_part(5) - 5 - sage: odd_part(4) - 1 - sage: odd_part(factorial(31)) - 122529844256906551386796875 - """ - if not isinstance(n, Integer): - n = ZZ(n) - return n.odd_part() - -def prime_to_m_part(n,m): - """ - Returns the prime-to-m part of n, i.e., the largest divisor of n - that is coprime to m. - - INPUT: - - - ``n`` - Integer (nonzero) - - - ``m`` - Integer - - OUTPUT: Integer - - EXAMPLES:: - - sage: 240.prime_to_m_part(2) - 15 - sage: 240.prime_to_m_part(3) - 80 - sage: 240.prime_to_m_part(5) - 48 - - sage: 43434.prime_to_m_part(20) - 21717 - """ - return ZZ(n).prime_to_m_part(m) - -def is_square(n, root=False): - """ - Returns whether or not n is square, and if n is a square also - returns the square root. If n is not square, also returns None. - - INPUT: - - - - ``n`` - an integer - - - ``root`` - whether or not to also return a square - root (default: False) - - - OUTPUT: - - - - ``bool`` - whether or not a square - - - ``object`` - (optional) an actual square if found, - and None otherwise. - - - EXAMPLES:: - - sage: is_square(2) - False - sage: is_square(4) - True - sage: is_square(2.2) - True - sage: is_square(-2.2) - False - sage: is_square(CDF(-2.2)) - True - sage: is_square((x-1)^2) - True - - :: - - sage: is_square(4, True) - (True, 2) - """ - if isinstance(n, (int,long)): - n = ZZ(n) - try: - if root: - try: - return n.is_square(root) - except TypeError: - if n.is_square(): - return True, n.sqrt() - else: - return False, None - return n.is_square() - except (AttributeError, NotImplementedError): - pass - t, x = pari(n).issquare(find_root=True) - if root: - if t: - x = parent(n)(x) - return t, x - return t - -def is_squarefree(n): - """ - Test whether ``n`` is square free. - - EXAMPLES:: - - sage: is_squarefree(100) - False - sage: is_squarefree(101) - True - - sage: R = ZZ['x'] - sage: x = R.gen() - sage: is_squarefree((x^2+x+1) * (x-2)) - True - sage: is_squarefree((x-1)**2 * (x-3)) - False - - sage: O = ZZ[sqrt(-1)] - sage: I = O.gen(1) - sage: is_squarefree(I+1) - True - sage: is_squarefree(O(2)) - False - sage: O(2).factor() - (-I) * (I + 1)^2 - - This method fails on domains which are not Unique Factorization Domains:: - - sage: O = ZZ[sqrt(-5)] - sage: a = O.gen(1) - sage: is_squarefree(a - 3) - Traceback (most recent call last): - ... - ArithmeticError: non-principal ideal in factorization - """ - if isinstance(n, (int,long)): - n = Integer(n) - - try: - return n.is_squarefree() - except AttributeError: - pass - - if n == 0: - return False - return all(r[1] == 1 for r in factor(n)) - - -################################################################# -# Euler phi function -################################################################# -class Euler_Phi: - r""" - Return the value of the Euler phi function on the integer n. We - defined this to be the number of positive integers <= n that are - relatively prime to n. Thus if n<=0 then - ``euler_phi(n)`` is defined and equals 0. - - INPUT: - - - - ``n`` - an integer - - - EXAMPLES:: - - sage: euler_phi(1) - 1 - sage: euler_phi(2) - 1 - sage: euler_phi(3) - 2 - sage: euler_phi(12) - 4 - sage: euler_phi(37) - 36 - - Notice that euler_phi is defined to be 0 on negative numbers and - 0. - - :: - - sage: euler_phi(-1) - 0 - sage: euler_phi(0) - 0 - sage: type(euler_phi(0)) - - - We verify directly that the phi function is correct for 21. - - :: - - sage: euler_phi(21) - 12 - sage: [i for i in range(21) if gcd(21,i) == 1] - [1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20] - - The length of the list of integers 'i' in range(n) such that the - gcd(i,n) == 1 equals euler_phi(n). - - :: - - sage: len([i for i in range(21) if gcd(21,i) == 1]) == euler_phi(21) - True - - The phi function also has a special plotting method. - - :: - - sage: P = plot(euler_phi, -3, 71) - - AUTHORS: - - - William Stein - - - Alex Clemesha (2006-01-10): some examples - """ - def __repr__(self): - """ - Returns a string describing this class. - - EXAMPLES:: - - sage: Euler_Phi().__repr__() - 'Number of positive integers <=n but relatively prime to n' - """ - return "Number of positive integers <=n but relatively prime to n" - - def __call__(self, n): - """ - Calls the euler_phi function. - - EXAMPLES:: - - sage: Euler_Phi()(10) - 4 - sage: Euler_Phi()(720) - 192 - """ - if n<=0: - return ZZ(0) - if n<=2: - return ZZ(1) - return ZZ(pari(n).phi()) - - def plot(self, xmin=1, xmax=50, pointsize=30, rgbcolor=(0,0,1), join=True, - **kwds): - """ - Plot the Euler phi function. - - INPUT: - - - - ``xmin`` - default: 1 - - - ``xmax`` - default: 50 - - - ``pointsize`` - default: 30 - - - ``rgbcolor`` - default: (0,0,1) - - - ``join`` - default: True; whether to join the - points. - - - ``**kwds`` - passed on - - EXAMPLES:: - - sage: p = Euler_Phi().plot() - sage: p.ymax() - 46.0 - """ - v = [(n,euler_phi(n)) for n in range(xmin,xmax + 1)] - from sage.plot.all import list_plot - P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) - if join: - P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) - return P - -euler_phi = Euler_Phi() - -def crt(a,b,m=None,n=None): - r""" - Returns a solution to a Chinese Remainder Theorem problem. - - INPUT: - - - ``a``, ``b`` - two residues (elements of some ring for which - extended gcd is available), or two lists, one of residues and - one of moduli. - - - ``m``, ``n`` - (default: ``None``) two moduli, or ``None``. - - OUTPUT: - - If ``m``, ``n`` are not ``None``, returns a solution `x` to the - simultaneous congruences `x\equiv a \bmod m` and `x\equiv b \bmod - n`, if one exists. By the Chinese Remainder Theorem, a solution to the - simultaneous congruences exists if and only if - `a\equiv b\pmod{\gcd(m,n)}`. The solution `x` is only well-defined modulo - `\text{lcm}(m,n)`. - - If ``a`` and ``b`` are lists, returns a simultaneous solution to - the congruences `x\equiv a_i\pmod{b_i}`, if one exists. - - .. SEEALSO:: - - - :func:`CRT_list` - - EXAMPLES: - - Using ``crt`` by giving it pairs of residues and moduli:: - - sage: crt(2, 1, 3, 5) - 11 - sage: crt(13, 20, 100, 301) - 28013 - sage: crt([2, 1], [3, 5]) - 11 - sage: crt([13, 20], [100, 301]) - 28013 - - You can also use upper case:: - - sage: c = CRT(2,3, 3, 5); c - 8 - sage: c % 3 == 2 - True - sage: c % 5 == 3 - True - - Note that this also works for polynomial rings:: - - sage: K. = NumberField(x^3 - 7) - sage: R. = K[] - sage: f = y^2 + 3 - sage: g = y^3 - 5 - sage: CRT(1,3,f,g) - -3/26*y^4 + 5/26*y^3 + 15/26*y + 53/26 - sage: CRT(1,a,f,g) - (-3/52*a + 3/52)*y^4 + (5/52*a - 5/52)*y^3 + (15/52*a - 15/52)*y + 27/52*a + 25/52 - - You can also do this for any number of moduli:: - - sage: K. = NumberField(x^3 - 7) - sage: R. = K[] - sage: CRT([], []) - 0 - sage: CRT([a], [x]) - a - sage: f = x^2 + 3 - sage: g = x^3 - 5 - sage: h = x^5 + x^2 - 9 - sage: k = CRT([1, a, 3], [f, g, h]); k - (127/26988*a - 5807/386828)*x^9 + (45/8996*a - 33677/1160484)*x^8 + (2/173*a - 6/173)*x^7 + (133/6747*a - 5373/96707)*x^6 + (-6/2249*a + 18584/290121)*x^5 + (-277/8996*a + 38847/386828)*x^4 + (-135/4498*a + 42673/193414)*x^3 + (-1005/8996*a + 470245/1160484)*x^2 + (-1215/8996*a + 141165/386828)*x + 621/8996*a + 836445/386828 - sage: k.mod(f) - 1 - sage: k.mod(g) - a - sage: k.mod(h) - 3 - - If the moduli are not coprime, a solution may not exist:: - - sage: crt(4,8,8,12) - 20 - sage: crt(4,6,8,12) - Traceback (most recent call last): - ... - ValueError: No solution to crt problem since gcd(8,12) does not divide 4-6 - - sage: x = polygen(QQ) - sage: crt(2,3,x-1,x+1) - -1/2*x + 5/2 - sage: crt(2,x,x^2-1,x^2+1) - -1/2*x^3 + x^2 + 1/2*x + 1 - sage: crt(2,x,x^2-1,x^3-1) - Traceback (most recent call last): - ... - ValueError: No solution to crt problem since gcd(x^2 - 1,x^3 - 1) does not divide 2-x - - sage: crt(int(2), int(3), int(7), int(11)) - 58 - """ - if isinstance(a, list): - return CRT_list(a, b) - if isinstance(a, (int, long)): - a = Integer(a) # otherwise we get an error at (b-a).quo_rem(g) - g, alpha, beta = XGCD(m, n) - q, r = (b - a).quo_rem(g) - if r != 0: - raise ValueError("No solution to crt problem since gcd(%s,%s) does not divide %s-%s" % (m, n, a, b)) - return (a + q*alpha*m) % lcm(m, n) - -CRT = crt - -def CRT_list(v, moduli): - r""" Given a list ``v`` of elements and a list of corresponding - ``moduli``, find a single element that reduces to each element of - ``v`` modulo the corresponding moduli. - - .. SEEALSO:: - - - :func:`crt` - - EXAMPLES:: - - sage: CRT_list([2,3,2], [3,5,7]) - 23 - sage: x = polygen(QQ) - sage: c = CRT_list([3], [x]); c - 3 - sage: c.parent() - Univariate Polynomial Ring in x over Rational Field - - It also works if the moduli are not coprime:: - - sage: CRT_list([32,2,2],[60,90,150]) - 452 - - But with non coprime moduli there is not always a solution:: - - sage: CRT_list([32,2,1],[60,90,150]) - Traceback (most recent call last): - ... - ValueError: No solution to crt problem since gcd(180,150) does not divide 92-1 - - The arguments must be lists:: - - sage: CRT_list([1,2,3],"not a list") - Traceback (most recent call last): - ... - ValueError: Arguments to CRT_list should be lists - sage: CRT_list("not a list",[2,3]) - Traceback (most recent call last): - ... - ValueError: Arguments to CRT_list should be lists - - The list of moduli must have the same length as the list of elements:: - - sage: CRT_list([1,2,3],[2,3,5]) - 23 - sage: CRT_list([1,2,3],[2,3]) - Traceback (most recent call last): - ... - ValueError: Arguments to CRT_list should be lists of the same length - sage: CRT_list([1,2,3],[2,3,5,7]) - Traceback (most recent call last): - ... - ValueError: Arguments to CRT_list should be lists of the same length - - TESTS:: - - sage: CRT([32r,2r,2r],[60r,90r,150r]) - 452 - - """ - if not isinstance(v,list) or not isinstance(moduli,list): - raise ValueError("Arguments to CRT_list should be lists") - if len(v) != len(moduli): - raise ValueError("Arguments to CRT_list should be lists of the same length") - if len(v) == 0: - return ZZ(0) - if len(v) == 1: - return moduli[0].parent()(v[0]) - x = v[0] - m = moduli[0] - for i in range(1,len(v)): - x = CRT(x,v[i],m,moduli[i]) - m = lcm(m,moduli[i]) - return x%m - -def CRT_basis(moduli): - r""" - Returns a CRT basis for the given moduli. - - INPUT: - - - ``moduli`` - list of pairwise coprime moduli `m` which admit an - extended Euclidean algorithm - - OUTPUT: - - - a list of elements `a_i` of the same length as `m` such that - `a_i` is congruent to 1 modulo `m_i` and to 0 modulo `m_j` for - `j\not=i`. - - .. note:: - - The pairwise coprimality of the input is not checked. - - EXAMPLES:: - - sage: a1 = ZZ(mod(42,5)) - sage: a2 = ZZ(mod(42,13)) - sage: c1,c2 = CRT_basis([5,13]) - sage: mod(a1*c1+a2*c2,5*13) - 42 - - A polynomial example:: - - sage: x=polygen(QQ) - sage: mods = [x,x^2+1,2*x-3] - sage: b = CRT_basis(mods) - sage: b - [-2/3*x^3 + x^2 - 2/3*x + 1, 6/13*x^3 - x^2 + 6/13*x, 8/39*x^3 + 8/39*x] - sage: [[bi % mj for mj in mods] for bi in b] - [[1, 0, 0], [0, 1, 0], [0, 0, 1]] - """ - n = len(moduli) - if n == 0: - return [] - M = prod(moduli) - return [((xgcd(m,M//m)[2])*(M//m))%M for m in moduli] - -def CRT_vectors(X, moduli): - r""" - Vector form of the Chinese Remainder Theorem: given a list of integer - vectors `v_i` and a list of coprime moduli `m_i`, find a vector `w` such - that `w = v_i \pmod m_i` for all `i`. This is more efficient than applying - :func:`CRT` to each entry. - - INPUT: - - - ``X`` - list or tuple, consisting of lists/tuples/vectors/etc of - integers of the same length - - ``moduli`` - list of len(X) moduli - - OUTPUT: - - - ``list`` - application of CRT componentwise. - - EXAMPLES:: - - sage: CRT_vectors([[3,5,7],[3,5,11]], [2,3]) - [3, 5, 5] - - sage: CRT_vectors([vector(ZZ, [2,3,1]), Sequence([1,7,8],ZZ)], [8,9]) - [10, 43, 17] - """ - # First find the CRT basis: - if len(X) == 0 or len(X[0]) == 0: - return [] - n = len(X) - if n != len(moduli): - raise ValueError("number of moduli must equal length of X") - a = CRT_basis(moduli) - modulus = prod(moduli) - return [sum(a[i]*X[i][j] for i in range(n)) % modulus for j in range(len(X[0]))] - -def binomial(x, m, **kwds): - r""" - Return the binomial coefficient - - .. math:: - - \binom{x}{m} = x (x-1) \cdots (x-m+1) / m! - - which is defined for `m \in \ZZ` and any - `x`. We extend this definition to include cases when - `x-m` is an integer but `m` is not by - - .. math:: - - \binom{x}{m} = \binom{x}{x-m} - - If `m < 0`, return `0`. - - INPUT: - - - ``x``, ``m`` - numbers or symbolic expressions. Either ``m`` - or ``x-m`` must be an integer. - - OUTPUT: number or symbolic expression (if input is symbolic) - - EXAMPLES:: - - sage: from sage.rings.arith import binomial - sage: binomial(5,2) - 10 - sage: binomial(2,0) - 1 - sage: binomial(1/2, 0) - 1 - sage: binomial(3,-1) - 0 - sage: binomial(20,10) - 184756 - sage: binomial(-2, 5) - -6 - sage: binomial(-5, -2) - 0 - sage: binomial(RealField()('2.5'), 2) - 1.87500000000000 - sage: n=var('n'); binomial(n,2) - 1/2*(n - 1)*n - sage: n=var('n'); binomial(n,n) - 1 - sage: n=var('n'); binomial(n,n-1) - n - sage: binomial(2^100, 2^100) - 1 - - sage: x = polygen(ZZ) - sage: binomial(x, 3) - 1/6*x^3 - 1/2*x^2 + 1/3*x - sage: binomial(x, x-3) - 1/6*x^3 - 1/2*x^2 + 1/3*x - - If `x \in \ZZ`, there is an optional 'algorithm' parameter, which - can be 'mpir' (faster for small values) or 'pari' (faster for - large values):: - - sage: a = binomial(100, 45, algorithm='mpir') - sage: b = binomial(100, 45, algorithm='pari') - sage: a == b - True - - TESTS: - - We test that certain binomials are very fast (this should be - instant) -- see :trac:`3309`:: - - sage: a = binomial(RR(1140000.78), 23310000) - - We test conversion of arguments to Integers -- see :trac:`6870`:: - - sage: binomial(1/2,1/1) - 1/2 - sage: binomial(10^20+1/1,10^20) - 100000000000000000001 - sage: binomial(SR(10**7),10**7) - 1 - sage: binomial(3/2,SR(1/1)) - 3/2 - - Some floating point cases -- see :trac:`7562`, :trac:`9633`, and - :trac:`12448`:: - - sage: binomial(1.,3) - 0.000000000000000 - sage: binomial(-2.,3) - -4.00000000000000 - sage: binomial(0.5r, 5) - 0.02734375 - sage: a = binomial(float(1001), float(1)); a - 1001.0 - sage: type(a) - - sage: binomial(float(1000), 1001) - 0.0 - - Test more output types:: - - sage: type(binomial(5r, 2)) - - sage: type(binomial(5, 2r)) - - - sage: type(binomial(5.0r, 2)) - - - sage: type(binomial(5/1, 2)) - - - sage: R = Integers(11) - sage: b = binomial(R(7), R(3)) - sage: b - 2 - sage: b.parent() - Ring of integers modulo 11 - - Test symbolic and uni/multivariate polynomials:: - - sage: x = polygen(ZZ) - sage: binomial(x, 3) - 1/6*x^3 - 1/2*x^2 + 1/3*x - sage: binomial(x, 3).parent() - Univariate Polynomial Ring in x over Rational Field - - sage: K. = Integers(7)[] - sage: binomial(y,3) - -y^3 + 3*y^2 - 2*y - sage: binomial(y,3).parent() - Multivariate Polynomial Ring in x, y over Ring of integers modulo 7 - - sage: n = var('n') - sage: binomial(n,2) - 1/2*(n - 1)*n - - Invalid inputs:: - - sage: x = polygen(ZZ) - sage: binomial(x, x^2) - Traceback (most recent call last): - ... - TypeError: either m or x-m must be an integer - - sage: k, i = var('k,i') - sage: binomial(k,i) - Traceback (most recent call last): - ... - TypeError: either m or x-m must be an integer - - sage: R6 = Zmod(6) - sage: binomial(R6(5), 2) - Traceback (most recent call last): - ... - ZeroDivisionError: factorial(2) not invertible in Ring of integers modulo 6 - - sage: R7 = Zmod(7) - sage: binomial(R7(10), 7) - Traceback (most recent call last): - ... - ZeroDivisionError: factorial(7) not invertible in Ring of integers modulo 7 - - The last two examples failed to execute since `2!` and `7!` are respectively - not invertible in `\ZZ/6\ZZ` and `\ZZ/7\ZZ`. One can check that there - is no well defined value for that binomial coefficient in the quotient:: - - sage: R6(binomial(5,2)) - 4 - sage: R6(binomial(5+6,2)) - 1 - - sage: R7(binomial(3, 7)) - 0 - sage: R7(binomial(10, 7)) - 1 - sage: R7(binomial(17, 7)) - 2 - - For symbolic manipulation, you should use the function - :func:`~sage.functions.other.binomial` from the module - :mod:`sage.functions.other`:: - - sage: from sage.functions.other import binomial - sage: binomial(k, i) - binomial(k, i) - """ - try: - m = ZZ(m) - except TypeError: - try: - m = ZZ(x-m) - except TypeError: - raise TypeError("either m or x-m must be an integer") - - P = parent(x) - x = py_scalar_to_element(x) - - # case 1: native binomial implemented on x - try: - return P(x.binomial(m, **kwds)) - except (AttributeError,TypeError): - pass - - # case 2: conversion to integers - try: - x = ZZ(x) - except TypeError: - pass - else: - # Check invertibility of factorial(m) in P - try: - c = P.characteristic() - except AttributeError: - # Assume that P has characteristic zero (can be int, float, ...) - pass - else: - if c > 0 and any(c.gcd(k) > 1 for k in range(2, m+1)): - raise ZeroDivisionError("factorial({}) not invertible in {}".format(m, P)) - return P(x.binomial(m, **kwds)) - - # case 3: rational, real numbers, complex numbers -> use pari - if isinstance(x, (Rational, RealNumber, ComplexNumber)): - return P(x._pari_().binomial(m)) - - # case 4: naive method - if m < ZZ.zero(): - return P(0) - return P(prod(x-i for i in xrange(m))) / m.factorial() - -def multinomial(*ks): - r""" - Return the multinomial coefficient - - INPUT: - - - An arbitrary number of integer arguments `k_1,\dots,k_n` - - A list of integers `[k_1,\dots,k_n]` - - OUTPUT: - - Returns the integer: - - .. math:: - - \binom{k_1 + \cdots + k_n}{k_1, \cdots, k_n} - =\frac{\left(\sum_{i=1}^n k_i\right)!}{\prod_{i=1}^n k_i!} - = \prod_{i=1}^n \binom{\sum_{j=1}^i k_j}{k_i} - - EXAMPLES:: - - sage: multinomial(0, 0, 2, 1, 0, 0) - 3 - sage: multinomial([0, 0, 2, 1, 0, 0]) - 3 - sage: multinomial(3, 2) - 10 - sage: multinomial(2^30, 2, 1) - 618970023101454657175683075 - sage: multinomial([2^30, 2, 1]) - 618970023101454657175683075 - - AUTHORS: - - - Gabriel Ebner - """ - if isinstance(ks[0],list): - if len(ks) >1: - raise ValueError("multinomial takes only one list argument") - ks=ks[0] - - s, c = 0, 1 - for k in ks: - s += k - c *= binomial(s, k) - return c - -def binomial_coefficients(n): - r""" - Return a dictionary containing pairs - `\{(k_1,k_2) : C_{k,n}\}` where `C_{k_n}` are - binomial coefficients and `n = k_1 + k_2`. - - INPUT: - - - - ``n`` - an integer - - - OUTPUT: dict - - EXAMPLES:: - - sage: sorted(binomial_coefficients(3).items()) - [((0, 3), 1), ((1, 2), 3), ((2, 1), 3), ((3, 0), 1)] - - Notice the coefficients above are the same as below:: - - sage: R. = QQ[] - sage: (x+y)^3 - x^3 + 3*x^2*y + 3*x*y^2 + y^3 - - AUTHORS: - - - Fredrik Johansson - """ - d = {(0, n):1, (n, 0):1} - a = 1 - for k in xrange(1, n//2+1): - a = (a * (n-k+1))//k - d[k, n-k] = d[n-k, k] = a - return d - -def multinomial_coefficients(m, n): - r""" - Return a dictionary containing pairs - `\{(k_1, k_2, ..., k_m) : C_{k, n}\}` where - `C_{k, n}` are multinomial coefficients such that - `n = k_1 + k_2 + ...+ k_m`. - - INPUT: - - - ``m`` - integer - - ``n`` - integer - - OUTPUT: dict - - EXAMPLES:: - - sage: sorted(multinomial_coefficients(2, 5).items()) - [((0, 5), 1), ((1, 4), 5), ((2, 3), 10), ((3, 2), 10), ((4, 1), 5), ((5, 0), 1)] - - Notice that these are the coefficients of `(x+y)^5`:: - - sage: R. = QQ[] - sage: (x+y)^5 - x^5 + 5*x^4*y + 10*x^3*y^2 + 10*x^2*y^3 + 5*x*y^4 + y^5 - - :: - - sage: sorted(multinomial_coefficients(3, 2).items()) - [((0, 0, 2), 1), ((0, 1, 1), 2), ((0, 2, 0), 1), ((1, 0, 1), 2), ((1, 1, 0), 2), ((2, 0, 0), 1)] - - ALGORITHM: The algorithm we implement for computing the multinomial - coefficients is based on the following result: - - ..math:: - - \binom{n}{k_1, \cdots, k_m} = - \frac{k_1+1}{n-k_1}\sum_{i=2}^m \binom{n}{k_1+1, \cdots, k_i-1, \cdots} - - e.g.:: - - sage: k = (2, 4, 1, 0, 2, 6, 0, 0, 3, 5, 7, 1) # random value - sage: n = sum(k) - sage: s = 0 - sage: for i in range(1, len(k)): - ... ki = list(k) - ... ki[0] += 1 - ... ki[i] -= 1 - ... s += multinomial(n, *ki) - sage: multinomial(n, *k) == (k[0] + 1) / (n - k[0]) * s - True - - TESTS:: - - sage: multinomial_coefficients(0, 0) - {(): 1} - sage: multinomial_coefficients(0, 3) - {} - - """ - if not m: - if n: - return {} - else: - return {(): 1} - if m == 2: - return binomial_coefficients(n) - t = [n] + [0] * (m - 1) - r = {tuple(t): 1} - if n: - j = 0 # j will be the leftmost nonzero position - else: - j = m - # enumerate tuples in co-lex order - while j < m - 1: - # compute next tuple - tj = t[j] - if j: - t[j] = 0 - t[0] = tj - if tj > 1: - t[j + 1] += 1 - j = 0 - start = 1 - v = 0 - else: - j += 1 - start = j + 1 - v = r[tuple(t)] - t[j] += 1 - # compute the value - # NB: the initialization of v was done above - for k in xrange(start, m): - if t[k]: - t[k] -= 1 - v += r[tuple(t)] - t[k] += 1 - t[0] -= 1 - r[tuple(t)] = (v * tj) // (n - t[0]) - return r - -# (since trac 14496) gaussian_binomial = sage.combinat.q_analogues.q_binomial - -def kronecker_symbol(x,y): - """ - The Kronecker symbol `(x|y)`. - - INPUT: - - - ``x`` - integer - - - ``y`` - integer - - EXAMPLES:: - - sage: kronecker_symbol(13,21) - -1 - sage: kronecker_symbol(101,4) - 1 - - IMPLEMENTATION: Using GMP. - """ - x = QQ(x).numerator() * QQ(x).denominator() - return ZZ(x.kronecker(y)) - -def kronecker(x,y): - r""" - Synonym for :func:`kronecker_symbol`. - - The Kronecker symbol `(x|y)`. - - INPUT: - - - ``x`` - integer - - - ``y`` - integer - - OUTPUT: - - - an integer - - EXAMPLES:: - - sage: kronecker(3,5) - -1 - sage: kronecker(3,15) - 0 - sage: kronecker(2,15) - 1 - sage: kronecker(-2,15) - -1 - sage: kronecker(2/3,5) - 1 - """ - return kronecker_symbol(x,y) - -def legendre_symbol(x,p): - r""" - The Legendre symbol `(x|p)`, for `p` prime. - - .. note:: - - The :func:`kronecker_symbol` command extends the Legendre - symbol to composite moduli and `p=2`. - - INPUT: - - - - ``x`` - integer - - - ``p`` - an odd prime number - - - EXAMPLES:: - - sage: legendre_symbol(2,3) - -1 - sage: legendre_symbol(1,3) - 1 - sage: legendre_symbol(1,2) - Traceback (most recent call last): - ... - ValueError: p must be odd - sage: legendre_symbol(2,15) - Traceback (most recent call last): - ... - ValueError: p must be a prime - sage: kronecker_symbol(2,15) - 1 - sage: legendre_symbol(2/3,7) - -1 - """ - x = QQ(x).numerator() * QQ(x).denominator() - p = ZZ(p) - if not p.is_prime(): - raise ValueError("p must be a prime") - if p == 2: - raise ValueError("p must be odd") - return x.kronecker(p) - -def jacobi_symbol(a,b): - r""" - The Jacobi symbol of integers a and b, where b is odd. - - .. note:: - - The :func:`kronecker_symbol` command extends the Jacobi - symbol to all integers b. - - If - - `b = p_1^{e_1} * ... * p_r^{e_r}` - - then - - `(a|b) = (a|p_1)^{e_1} ... (a|p_r)^{e_r}` - - where `(a|p_j)` are Legendre Symbols. - - - - INPUT: - - - ``a`` - an integer - - - ``b`` - an odd integer - - EXAMPLES:: - - sage: jacobi_symbol(10,777) - -1 - sage: jacobi_symbol(10,5) - 0 - sage: jacobi_symbol(10,2) - Traceback (most recent call last): - ... - ValueError: second input must be odd, 2 is not odd - """ - - if b%2==0: - raise ValueError("second input must be odd, %s is not odd"%b) - - return kronecker_symbol(a,b) - -def primitive_root(n, check=True): - """ - Return a positive integer that generates the multiplicative group - of integers modulo `n`, if one exists; otherwise, raise a - ``ValueError``. - - A primitive root exists if `n=4` or `n=p^k` or `n=2p^k`, where `p` - is an odd prime and `k` is a nonnegative number. - - INPUT: - - - ``n`` -- a non-zero integer - - ``check`` -- bool (default: True); if False, then `n` is assumed - to be a positive integer possessing a primitive root, and behavior - is undefined otherwise. - - OUTPUT: - - A primitive root of `n`. If `n` is prime, this is the smallest - primitive root. - - EXAMPLES:: - - sage: primitive_root(23) - 5 - sage: primitive_root(-46) - 5 - sage: primitive_root(25) - 2 - sage: print [primitive_root(p) for p in primes(100)] - [1, 2, 2, 3, 2, 2, 3, 2, 5, 2, 3, 2, 6, 3, 5, 2, 2, 2, 2, 7, 5, 3, 2, 3, 5] - sage: primitive_root(8) - Traceback (most recent call last): - ... - ValueError: no primitive root - - .. NOTE:: - - It takes extra work to check if `n` has a primitive root; to - avoid this, use ``check=False``, which may slightly speed things - up (but could also result in undefined behavior). For example, - the second call below is an order of magnitude faster than the - first: - - :: - - sage: n = 10^50 + 151 # a prime - sage: primitive_root(n) - 11 - sage: primitive_root(n, check=False) - 11 - - TESTS: - - Various special cases:: - - sage: primitive_root(-1) - 0 - sage: primitive_root(0) - Traceback (most recent call last): - ... - ValueError: no primitive root - sage: primitive_root(1) - 0 - sage: primitive_root(2) - 1 - sage: primitive_root(3) - 2 - sage: primitive_root(4) - 3 - - We test that various numbers without primitive roots give - an error - see :trac:`10836`:: - - sage: primitive_root(15) - Traceback (most recent call last): - ... - ValueError: no primitive root - sage: primitive_root(16) - Traceback (most recent call last): - ... - ValueError: no primitive root - sage: primitive_root(1729) - Traceback (most recent call last): - ... - ValueError: no primitive root - sage: primitive_root(4*7^8) - Traceback (most recent call last): - ... - ValueError: no primitive root - """ - if not check: - return ZZ(pari(n).znprimroot()) - n = ZZ(n).abs() - if n <= 4: - if n: - # n-1 is a primitive root for n in {1,2,3,4} - return n-1 - elif n%2: # n odd - if n.is_prime_power(): - return ZZ(pari(n).znprimroot()) - else: # n even - m = n // 2 - if m%2 and m.is_prime_power(): - return ZZ(pari(n).znprimroot()) - raise ValueError("no primitive root") - -def nth_prime(n): - """ - - Return the n-th prime number (1-indexed, so that 2 is the 1st prime.) - - INPUT: - - - ``n`` -- a positive integer - - OUTPUT: - - - the n-th prime number - - EXAMPLES:: - - sage: nth_prime(3) - 5 - sage: nth_prime(10) - 29 - - :: - - sage: nth_prime(0) - Traceback (most recent call last): - ... - ValueError: nth prime meaningless for non-positive n (=0) - - TESTS:: - - sage: all(prime_pi(nth_prime(j)) == j for j in range(1, 1000, 10)) - True - - """ - return ZZ(pari.nth_prime(n)) - -def quadratic_residues(n): - r""" - Return a sorted list of all squares modulo the integer `n` - in the range `0\leq x < |n|`. - - EXAMPLES:: - - sage: quadratic_residues(11) - [0, 1, 3, 4, 5, 9] - sage: quadratic_residues(1) - [0] - sage: quadratic_residues(2) - [0, 1] - sage: quadratic_residues(8) - [0, 1, 4] - sage: quadratic_residues(-10) - [0, 1, 4, 5, 6, 9] - sage: v = quadratic_residues(1000); len(v); - 159 - """ - n = abs(int(n)) - X = sorted(set(ZZ((a*a)%n) for a in range(n//2+1))) - return X - -class Moebius: - r""" - Returns the value of the Moebius function of abs(n), where n is an - integer. - - DEFINITION: `\mu(n)` is 0 if `n` is not square - free, and otherwise equals `(-1)^r`, where `n` has - `r` distinct prime factors. - - For simplicity, if `n=0` we define `\mu(n) = 0`. - - IMPLEMENTATION: Factors or - for integers - uses the PARI C - library. - - INPUT: - - - - ``n`` - anything that can be factored. - - - OUTPUT: 0, 1, or -1 - - EXAMPLES:: - - sage: moebius(-5) - -1 - sage: moebius(9) - 0 - sage: moebius(12) - 0 - sage: moebius(-35) - 1 - sage: moebius(-1) - 1 - sage: moebius(7) - -1 - - :: - - sage: moebius(0) # potentially nonstandard! - 0 - - The moebius function even makes sense for non-integer inputs. - - :: - - sage: x = GF(7)['x'].0 - sage: moebius(x+2) - -1 - """ - def __call__(self, n): - """ - EXAMPLES:: - - sage: Moebius().__call__(7) - -1 - """ - if isinstance(n, (int, long)): - n = ZZ(n) - elif not isinstance(n, Integer): - # Use a generic algorithm. - if n < 0: - n = -n - F = factor(n) - for _, e in F: - if e >= 2: - return 0 - return (-1)**len(F) - - # Use fast PARI algorithm - if n == 0: - return ZZ.zero() - return ZZ(pari(n).moebius()) - - - def __repr__(self): - """ - Returns a description of this function. - - EXAMPLES:: - - sage: q = Moebius() - sage: q.__repr__() - 'The Moebius function' - """ - return "The Moebius function" - - def plot(self, xmin=0, xmax=50, pointsize=30, rgbcolor=(0,0,1), join=True, - **kwds): - """ - Plot the Moebius function. - - INPUT: - - - - ``xmin`` - default: 0 - - - ``xmax`` - default: 50 - - - ``pointsize`` - default: 30 - - - ``rgbcolor`` - default: (0,0,1) - - - ``join`` - default: True; whether to join the points - (very helpful in seeing their order). - - - ``**kwds`` - passed on - - EXAMPLES:: - - sage: p = Moebius().plot() - sage: p.ymax() - 1.0 - """ - values = self.range(xmin, xmax + 1) - v = [(n,values[n-xmin]) for n in range(xmin,xmax + 1)] - from sage.plot.all import list_plot - P = list_plot(v, pointsize=pointsize, rgbcolor=rgbcolor, **kwds) - if join: - P += list_plot(v, plotjoined=True, rgbcolor=(0.7,0.7,0.7), **kwds) - return P - - def range(self, start, stop=None, step=None): - """ - Return the Moebius function evaluated at the given range of values, - i.e., the image of the list range(start, stop, step) under the - Mobius function. - - This is much faster than directly computing all these values with a - list comprehension. - - EXAMPLES:: - - sage: v = moebius.range(-10,10); v - [1, 0, 0, -1, 1, -1, 0, -1, -1, 1, 0, 1, -1, -1, 0, -1, 1, -1, 0, 0] - sage: v == [moebius(n) for n in range(-10,10)] - True - sage: v = moebius.range(-1000, 2000, 4) - sage: v == [moebius(n) for n in range(-1000,2000, 4)] - True - """ - if stop is None: - start, stop = 1, int(start) - else: - start = int(start) - stop = int(stop) - if step is None: - step = 1 - else: - step = int(step) - - if start <= 0 and 0 < stop and start % step == 0: - return self.range(start, 0, step) + [ZZ.zero()] +\ - self.range(step, stop, step) - - if step == 1: - v = pari('vector(%s, i, moebius(i-1+%s))'%( - stop-start, start)) - else: - n = len(range(start, stop, step)) # stupid - v = pari('vector(%s, i, moebius(%s*(i-1) + %s))'%( - n, step, start)) - return [Integer(x) for x in v] - -moebius = Moebius() - - -## Note: farey, convergent, continued_fraction_list and convergents have been moved to -## sage.rings.continued_fraction - -def continuant(v, n=None): - r""" - Function returns the continuant of the sequence `v` (list - or tuple). - - Definition: see Graham, Knuth and Patashnik, *Concrete Mathematics*, - section 6.7: Continuants. The continuant is defined by - - - `K_0() = 1` - - `K_1(x_1) = x_1` - - `K_n(x_1, \cdots, x_n) = K_{n-1}(x_n, \cdots x_{n-1})x_n + K_{n-2}(x_1, \cdots, x_{n-2})` - - If ``n = None`` or ``n > len(v)`` the default - ``n = len(v)`` is used. - - INPUT: - - - ``v`` - list or tuple of elements of a ring - - ``n`` - optional integer - - OUTPUT: element of ring (integer, polynomial, etcetera). - - EXAMPLES:: - - sage: continuant([1,2,3]) - 10 - sage: p = continuant([2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]) - sage: q = continuant([1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]) - sage: p/q - 517656/190435 - sage: continued_fraction([2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8, 1, 1, 10]).convergent(14) - 517656/190435 - sage: x = PolynomialRing(RationalField(),'x',5).gens() - sage: continuant(x) - x0*x1*x2*x3*x4 + x0*x1*x2 + x0*x1*x4 + x0*x3*x4 + x2*x3*x4 + x0 + x2 + x4 - sage: continuant(x, 3) - x0*x1*x2 + x0 + x2 - sage: continuant(x,2) - x0*x1 + 1 - - We verify the identity - - .. math:: - - K_n(z,z,\cdots,z) = \sum_{k=0}^n \binom{n-k}{k} z^{n-2k} - - for `n = 6` using polynomial arithmetic:: - - sage: z = QQ['z'].0 - sage: continuant((z,z,z,z,z,z,z,z,z,z,z,z,z,z,z),6) - z^6 + 5*z^4 + 6*z^2 + 1 - - sage: continuant(9) - Traceback (most recent call last): - ... - TypeError: object of type 'sage.rings.integer.Integer' has no len() - - AUTHORS: - - - Jaap Spies (2007-02-06) - """ - m = len(v) - if n is None or m < n: - n = m - if n == 0: - return 1 - if n == 1: - return v[0] - a, b = 1, v[0] - for k in range(1,n): - a, b = b, a + b*v[k] - return b - -def number_of_divisors(n): - """ - Return the number of divisors of the integer n. - - INPUT: - - - ``n`` - a nonzero integer - - OUTPUT: - - - an integer, the number of divisors of n - - EXAMPLES:: - - sage: number_of_divisors(100) - 9 - sage: number_of_divisors(-720) - 30 - """ - m = ZZ(n) - if m.is_zero(): - raise ValueError("input must be nonzero") - return ZZ(pari(m).numdiv()) - - - -def hilbert_symbol(a, b, p, algorithm="pari"): - """ - Returns 1 if `ax^2 + by^2` `p`-adically represents - a nonzero square, otherwise returns `-1`. If either a or b - is 0, returns 0. - - INPUT: - - - - ``a, b`` - integers - - - ``p`` - integer; either prime or -1 (which - represents the archimedean place) - - - ``algorithm`` - string - - - ``'pari'`` - (default) use the PARI C library - - - ``'direct'`` - use a Python implementation - - - ``'all'`` - use both PARI and direct and check that - the results agree, then return the common answer - - - OUTPUT: integer (0, -1, or 1) - - EXAMPLES:: - - sage: hilbert_symbol (-1, -1, -1, algorithm='all') - -1 - sage: hilbert_symbol (2,3, 5, algorithm='all') - 1 - sage: hilbert_symbol (4, 3, 5, algorithm='all') - 1 - sage: hilbert_symbol (0, 3, 5, algorithm='all') - 0 - sage: hilbert_symbol (-1, -1, 2, algorithm='all') - -1 - sage: hilbert_symbol (1, -1, 2, algorithm='all') - 1 - sage: hilbert_symbol (3, -1, 2, algorithm='all') - -1 - - sage: hilbert_symbol(QQ(-1)/QQ(4), -1, 2) == -1 - True - sage: hilbert_symbol(QQ(-1)/QQ(4), -1, 3) == 1 - True - - AUTHORS: - - - William Stein and David Kohel (2006-01-05) - """ - p = ZZ(p) - if p != -1 and not p.is_prime(): - raise ValueError("p must be prime or -1") - a = QQ(a).numerator() * QQ(a).denominator() - b = QQ(b).numerator() * QQ(b).denominator() - - if algorithm == "pari": - if p == -1: - p = 0 - return ZZ(pari(a).hilbert(b,p)) - - elif algorithm == 'direct': - if a == 0 or b == 0: - return ZZ(0) - - p = ZZ(p) - one = ZZ(1) - - if p != -1: - p_sqr = p**2 - while a%p_sqr == 0: a //= p_sqr - while b%p_sqr == 0: b //= p_sqr - - if p != 2 and True in ( kronecker(x,p) == 1 for x in (a,b,a+b) ): - return one - if a%p == 0: - if b%p == 0: - return hilbert_symbol(p,-(b//p),p)*hilbert_symbol(a//p,b,p) - elif p == 2 and (b%4) == 3: - if kronecker(a+b,p) == -1: - return -one - elif kronecker(b,p) == -1: - return -one - elif b%p == 0: - if p == 2 and (a%4) == 3: - if kronecker(a+b,p) == -1: - return -one - elif kronecker(a,p) == -1: - return -one - elif p == 2 and (a%4) == 3 and (b%4) == 3: - return -one - return one - elif algorithm == 'all': - ans_pari = hilbert_symbol(a,b,p,algorithm='pari') - ans_direct = hilbert_symbol(a,b,p,algorithm='direct') - if ans_pari != ans_direct: - raise RuntimeError("There is a bug in hilbert_symbol; two ways of computing the Hilbert symbol (%s,%s)_%s disagree"%(a,b,p)) - return ans_pari - else: - raise ValueError("Algorithm %s not defined"%algorithm) - - -def hilbert_conductor(a, b): - """ - This is the product of all (finite) primes where the Hilbert symbol is -1. - What is the same, this is the (reduced) discriminant of the quaternion - algebra `(a,b)` over `\QQ`. - - INPUT: - - - ``a``, ``b`` -- integers - - OUTPUT: - - - squarefree positive integer - - EXAMPLES:: - - sage: hilbert_conductor(-1, -1) - 2 - sage: hilbert_conductor(-1, -11) - 11 - sage: hilbert_conductor(-2, -5) - 5 - sage: hilbert_conductor(-3, -17) - 17 - - AUTHOR: - - - Gonzalo Tornaria (2009-03-02) - """ - a, b = ZZ(a), ZZ(b) - d = ZZ(1) - for p in set().union([2], prime_divisors(a), prime_divisors(b)): - if hilbert_symbol(a, b, p) == -1: - d *= p - return d - -def hilbert_conductor_inverse(d): - """ - Finds a pair of integers `(a,b)` such that ``hilbert_conductor(a,b) == d``. - The quaternion algebra `(a,b)` over `\QQ` will then have (reduced) - discriminant `d`. - - INPUT: - - - ``d`` -- square-free positive integer - - OUTPUT: pair of integers - - EXAMPLES:: - - sage: hilbert_conductor_inverse(2) - (-1, -1) - sage: hilbert_conductor_inverse(3) - (-1, -3) - sage: hilbert_conductor_inverse(6) - (-1, 3) - sage: hilbert_conductor_inverse(30) - (-3, -10) - sage: hilbert_conductor_inverse(4) - Traceback (most recent call last): - ... - ValueError: d needs to be squarefree - sage: hilbert_conductor_inverse(-1) - Traceback (most recent call last): - ... - ValueError: d needs to be positive - - AUTHOR: - - - Gonzalo Tornaria (2009-03-02) - - TESTS:: - - sage: for i in xrange(100): - ... d = ZZ.random_element(2**32).squarefree_part() - ... if hilbert_conductor(*hilbert_conductor_inverse(d)) != d: - ... print "hilbert_conductor_inverse failed for d =", d - """ - Z = ZZ - d = Z(d) - if d <= 0: - raise ValueError("d needs to be positive") - if d == 1: - return (Z(-1), Z(1)) - if d == 2: - return (Z(-1), Z(-1)) - if d.is_prime(): - if d%4 == 3: - return (Z(-1), -d) - if d%8 == 5: - return (Z(-2), -d) - q = 3 - while q%4 != 3 or kronecker_symbol(d,q) != -1: - q = next_prime(q) - return (Z(-q), -d) - else: - mo = moebius(d) - if mo == 0: - raise ValueError("d needs to be squarefree") - if d % 2 == 0 and mo*d % 16 != 2: - dd = mo * d / 2 - else: - dd = mo * d - q = 1 - while hilbert_conductor(-q, dd) != d: - q+=1; - if dd%q == 0: - dd /= q - return (Z(-q), Z(dd)) - - -############################################################################## -## falling and rising factorials -## By Jaap Spies -## -## Copyright (C) 2006 Jaap Spies -## Copyright (C) 2006 William Stein -## -## Distributed under the terms of the GNU General Public License (GPL) -## http://www.gnu.org/licenses/ -############################################################################## - - -def falling_factorial(x, a): - r""" - Returns the falling factorial `(x)_a`. - - The notation in the literature is a mess: often `(x)_a`, - but there are many other notations: GKP: Concrete Mathematics uses - `x^{\underline{a}}`. - - Definition: for integer `a \ge 0` we have - `x(x-1) \cdots (x-a+1)`. In all other cases we use the - GAMMA-function: `\frac {\Gamma(x+1)} {\Gamma(x-a+1)}`. - - INPUT: - - - ``x`` - element of a ring - - - ``a`` - a non-negative integer or - - OR - - - ``x and a`` - any numbers - - OUTPUT: the falling factorial - - EXAMPLES:: - - sage: falling_factorial(10, 3) - 720 - sage: falling_factorial(10, RR('3.0')) - 720.000000000000 - sage: falling_factorial(10, RR('3.3')) - 1310.11633396601 - sage: falling_factorial(10, 10) - 3628800 - sage: factorial(10) - 3628800 - sage: a = falling_factorial(1+I, I); a - gamma(I + 2) - sage: CC(a) - 0.652965496420167 + 0.343065839816545*I - sage: falling_factorial(1+I, 4) - 4*I + 2 - sage: falling_factorial(I, 4) - -10 - - :: - - sage: M = MatrixSpace(ZZ, 4, 4) - sage: A = M([1,0,1,0,1,0,1,0,1,0,10,10,1,0,1,1]) - sage: falling_factorial(A, 2) # A(A - I) - [ 1 0 10 10] - [ 1 0 10 10] - [ 20 0 101 100] - [ 2 0 11 10] - - :: - - sage: x = ZZ['x'].0 - sage: falling_factorial(x, 4) - x^4 - 6*x^3 + 11*x^2 - 6*x - - TESTS: - - Check that :trac:`14858` is fixed:: - - sage: falling_factorial(-4, SR(2)) - 20 - - Check that :trac:`16770` is fixed:: - - sage: d = var('d') - sage: type(falling_factorial(d, 0)) - - - AUTHORS: - - - Jaap Spies (2006-03-05) - """ - from sage.symbolic.expression import Expression - - if (isinstance(a, (Integer, int, long)) or - (isinstance(a, Expression) and - a.is_integer())) and a >= 0: - return prod(((x - i) for i in range(a)), z=x.parent().one()) - from sage.functions.all import gamma - return gamma(x+1) / gamma(x-a+1) - -def rising_factorial(x, a): - r""" - Returns the rising factorial `(x)^a`. - - The notation in the literature is a mess: often `(x)^a`, - but there are many other notations: GKP: Concrete Mathematics uses - `x^{\overline{a}}`. - - The rising factorial is also known as the Pochhammer symbol, see - Maple and Mathematica. - - Definition: for integer `a \ge 0` we have - `x(x+1) \cdots (x+a-1)`. In all other cases we use the - GAMMA-function: `\frac {\Gamma(x+a)} {\Gamma(x)}`. - - INPUT: - - - - ``x`` - element of a ring - - - ``a`` - a non-negative integer or - - - ``x and a`` - any numbers - - - OUTPUT: the rising factorial - - EXAMPLES:: - - sage: rising_factorial(10,3) - 1320 - - :: - - sage: rising_factorial(10,RR('3.0')) - 1320.00000000000 - - :: - - sage: rising_factorial(10,RR('3.3')) - 2826.38895824964 - - :: - - sage: a = rising_factorial(1+I, I); a - gamma(2*I + 1)/gamma(I + 1) - sage: CC(a) - 0.266816390637832 + 0.122783354006372*I - - :: - - sage: a = rising_factorial(I, 4); a - -10 - - See falling_factorial(I, 4). - - :: - - sage: x = polygen(ZZ) - sage: rising_factorial(x, 4) - x^4 + 6*x^3 + 11*x^2 + 6*x - - TESTS: - - Check that :trac:`14858` is fixed:: - - sage: bool(rising_factorial(-4, 2) == - ....: rising_factorial(-4, SR(2)) == - ....: rising_factorial(SR(-4), SR(2))) - True - - Check that :trac:`16770` is fixed:: - - sage: d = var('d') - sage: type(rising_factorial(d, 0)) - - - AUTHORS: - - - Jaap Spies (2006-03-05) - """ - from sage.symbolic.expression import Expression - - if (isinstance(a, (Integer, int, long)) or - (isinstance(a, Expression) and - a.is_integer())) and a >= 0: - return prod(((x + i) for i in range(a)), z=x.parent().one()) - from sage.functions.all import gamma - return gamma(x+a) / gamma(x) - - -def integer_ceil(x): - """ - Return the ceiling of x. - - EXAMPLES:: - - sage: integer_ceil(5.4) - 6 - sage: integer_ceil(x) - Traceback (most recent call last): - ... - NotImplementedError: computation of ceil of x not implemented - """ - try: - return ZZ(x.ceil()) - except AttributeError: - try: - return ZZ(math.ceil(float(x))) - except TypeError: - pass - raise NotImplementedError("computation of ceil of %s not implemented"%x) - -def integer_floor(x): - r""" - Return the largest integer `\leq x`. - - INPUT: - - - ``x`` - an object that has a floor method or is - coercible to int - - OUTPUT: an Integer - - EXAMPLES:: - - sage: integer_floor(5.4) - 5 - sage: integer_floor(float(5.4)) - 5 - sage: integer_floor(-5/2) - -3 - sage: integer_floor(RDF(-5/2)) - -3 - - sage: integer_floor(x) - Traceback (most recent call last): - ... - NotImplementedError: computation of floor of x not implemented - """ - try: - return ZZ(x.floor()) - except AttributeError: - try: - return ZZ(math.floor(float(x))) - except TypeError: - pass - raise NotImplementedError("computation of floor of %s not implemented"%x) - - -def two_squares(n): - """ - Write the integer `n` as a sum of two integer squares if possible; - otherwise raise a ``ValueError``. - - INPUT: - - - ``n`` -- an integer - - OUTPUT: a tuple `(a,b)` of non-negative integers such that - `n = a^2 + b^2` with `a <= b`. - - EXAMPLES:: - - sage: two_squares(389) - (10, 17) - sage: two_squares(21) - Traceback (most recent call last): - ... - ValueError: 21 is not a sum of 2 squares - sage: two_squares(21^2) - (0, 21) - sage: a,b = two_squares(100000000000000000129); a,b - (4418521500, 8970878873) - sage: a^2 + b^2 - 100000000000000000129 - sage: two_squares(2^222+1) - (253801659504708621991421712450521, 2583712713213354898490304645018692) - sage: two_squares(0) - (0, 0) - sage: two_squares(-1) - Traceback (most recent call last): - ... - ValueError: -1 is not a sum of 2 squares - - TESTS:: - - sage: for _ in xrange(100): - ....: a = ZZ.random_element(2**16, 2**20) - ....: b = ZZ.random_element(2**16, 2**20) - ....: n = a**2 + b**2 - ....: aa,bb = two_squares(n) - ....: assert aa**2 + bb**2 == n - - ALGORITHM: - - See http://www.schorn.ch/howto.html - """ - n = ZZ(n) - - if n <= 0: - if n == 0: - z = ZZ.zero() - return (z, z) - raise ValueError("%s is not a sum of 2 squares"%n) - - if n.nbits() <= 32: - from sage.rings import sum_of_squares - return sum_of_squares.two_squares_pyx(n) - - # Start by factoring n (which seems to be unavoidable) - F = n.factor(proof=False) - - # First check whether it is possible to write n as a sum of two - # squares: all prime powers p^e must have p = 2 or p = 1 mod 4 - # or e even. - for (p,e) in F: - if e % 2 == 1 and p % 4 == 3: - raise ValueError("%s is not a sum of 2 squares"%n) - - # We run over all factors of n, write each factor p^e as - # a sum of 2 squares and accumulate the product - # (using multiplication in Z[I]) in a^2 + b^2. - from sage.rings.finite_rings.integer_mod import Mod - a = ZZ.one() - b = ZZ.zero() - for (p,e) in F: - if e >= 2: - m = p ** (e//2) - a *= m - b *= m - if e % 2 == 1: - if p == 2: - # (a + bi) *= (1 + I) - a,b = a - b, a + b - else: # p = 1 mod 4 - # Find a square root of -1 mod p. - # If y is a non-square, then y^((p-1)/4) is a square root of -1. - y = Mod(2,p) - while True: - s = y**((p-1)/4) - if not s*s + 1: - s = s.lift() - break - y += 1 - # Apply Cornacchia's algorithm to write p as r^2 + s^2. - r = p - while s*s > p: - r,s = s, r % s - r %= s - - # Multiply (a + bI) by (r + sI) - a,b = a*r - b*s, b*r + a*s - - a = a.abs() - b = b.abs() - assert a*a + b*b == n - if a <= b: - return (a,b) - else: - return (b,a) - -def three_squares(n): - """ - Write the integer `n` as a sum of three integer squares if possible; - otherwise raise a ``ValueError``. - - INPUT: - - - ``n`` -- an integer - - OUTPUT: a tuple `(a,b,c)` of non-negative integers such that - `n = a^2 + b^2 + c^2` with `a <= b <= c`. - - EXAMPLES:: - - sage: three_squares(389) - (1, 8, 18) - sage: three_squares(946) - (9, 9, 28) - sage: three_squares(2986) - (3, 24, 49) - sage: three_squares(7^100) - (0, 0, 1798465042647412146620280340569649349251249) - sage: three_squares(11^111-1) - (616274160655975340150706442680, 901582938385735143295060746161, 6270382387635744140394001363065311967964099981788593947233) - sage: three_squares(7 * 2^41) - (1048576, 2097152, 3145728) - sage: three_squares(7 * 2^42) - Traceback (most recent call last): - ... - ValueError: 30786325577728 is not a sum of 3 squares - sage: three_squares(0) - (0, 0, 0) - sage: three_squares(-1) - Traceback (most recent call last): - ... - ValueError: -1 is not a sum of 3 squares - - TESTS:: - - sage: for _ in xrange(100): - ....: a = ZZ.random_element(2**16, 2**20) - ....: b = ZZ.random_element(2**16, 2**20) - ....: c = ZZ.random_element(2**16, 2**20) - ....: n = a**2 + b**2 + c**2 - ....: aa,bb,cc = three_squares(n) - ....: assert aa**2 + bb**2 + cc**2 == n - - ALGORITHM: - - See http://www.schorn.ch/howto.html - """ - n = ZZ(n) - - if n <= 0: - if n == 0: - z = ZZ.zero() - return (z, z, z) - raise ValueError("%s is not a sum of 3 squares"%n) - - if n.nbits() <= 32: - from sage.rings import sum_of_squares - return sum_of_squares.three_squares_pyx(n) - - # First, remove all factors 4 from n - e = n.valuation(2)//2 - m = ZZ.one() << e - N = n >> (2*e) - - # Let x be the largest integer at most sqrt(N) - x, r = N.sqrtrem() - # We need to check for this special case, - # otherwise N - x^2 will always factor. - if not r: - z = ZZ.zero() - return (z, z, x*m) - - # Consider different cases to find an x such that N - x^2 is easily - # written as the sum of 2 squares, because it is either p or 2p, - # with p a prime which is 1 mod 4. - if N % 4 == 1: - # Write N = x^2 + p with x even, p = 1 mod 4 prime - if x % 2 == 1: - x -= 1 - while x >= 0: - p = N - x*x - if p.is_pseudoprime(): - break - x -= 2 - elif N % 4 == 2: - # Write N = x^2 + p with x odd, p = 1 mod 4 prime - if x % 2 == 0: - x -= 1 - while x >= 0: - p = N - x*x - if p.is_pseudoprime(): - break - x -= 2 - elif N % 8 == 3: - # Write N = x^2 + 2p with x odd, p = 1 mod 4 prime - if x % 2 == 0: - x -= 1 - while x >= 0: - p = (N - x*x) >> 1 - if p.is_pseudoprime(): - break - x -= 2 - else: # 7 mod 8 - raise ValueError("%s is not a sum of 3 squares"%n) - - if x < 0: - # We found no good x, brute force instead. - # Normally, this should only happen for small values of N. - if N > 10000: - from warnings import warn - warn("Brute forcing sum of 3 squares for large N = %s"%N, RuntimeWarning) - x = N.isqrt() - - # In the usual case, this loop will only be executed once, since - # we already know the "right" value of x. - # This will only really loop if we hit the "x < 0" case above. - while True: - try: - a,b = two_squares(N - x*x) - break - except ValueError: - x -= 1 - assert x >= 0 - - if x >= b: - return (a*m, b*m, x*m) - elif x >= a: - return (a*m, x*m, b*m) - else: - return (x*m, a*m, b*m) - -def four_squares(n): - """ - Write the integer `n` as a sum of four integer squares. - - INPUT: - - - ``n`` -- an integer - - OUTPUT: a tuple `(a,b,c,d)` of non-negative integers such that - `n = a^2 + b^2 + c^2 + d^2` with `a <= b <= c <= d`. - - EXAMPLES:: - - sage: four_squares(3) - (0, 1, 1, 1) - sage: four_squares(13) - (0, 0, 2, 3) - sage: four_squares(130) - (0, 0, 3, 11) - sage: four_squares(1101011011004) - (90, 102, 1220, 1049290) - sage: four_squares(10^100-1) - (155024616290, 2612183768627, 14142135623730950488016887, 99999999999999999999999999999999999999999999999999) - sage: for i in range(2^129, 2^129+10000): # long time - ....: S = four_squares(i) - ....: assert sum(x^2 for x in S) == i - - TESTS:: - - sage: for _ in xrange(100): - ....: n = ZZ.random_element(2**32,2**34) - ....: aa,bb,cc,dd = four_squares(n) - ....: assert aa**2 + bb**2 + cc**2 + dd**2 == n - """ - n = ZZ(n) - - if n <= 0: - if n == 0: - z = ZZ.zero() - return (z, z, z, z) - raise ValueError("%s is not a sum of 4 squares"%n) - - if n.nbits() <= 32: - from sage.rings import sum_of_squares - return sum_of_squares.four_squares_pyx(n) - - # First, remove all factors 4 from n - e = n.valuation(2) // 2 - m = ZZ.one() << e - N = n >> (2*e) - - # Subtract a suitable x^2 such that N - x^2 is 1,2,3,5,6 mod 8, - # which can then be written as a sum of 3 squares. - x = N.isqrt() - y = N - x*x - if y >= 7 and (y % 4 == 0 or y % 8 == 7): - x -= 1 - y += 2*x + 1 - - a,b,c = three_squares(y) - - # Correct sorting is guaranteed by construction - return (a*m, b*m, c*m, x*m) - -def sum_of_k_squares(k,n): - """ - Write the integer `n` as a sum of `k` integer squares if possible; - otherwise raise a ``ValueError``. - - INPUT: - - - ``k`` -- a non-negative integer - - - ``n`` -- an integer - - OUTPUT: a tuple `(x_1, ..., x_k)` of non-negative integers such that - their squares sum to `n`. - - EXAMPLES:: - - sage: sum_of_k_squares(2, 9634) - (15, 97) - sage: sum_of_k_squares(3, 9634) - (0, 15, 97) - sage: sum_of_k_squares(4, 9634) - (1, 2, 5, 98) - sage: sum_of_k_squares(5, 9634) - (0, 1, 2, 5, 98) - sage: sum_of_k_squares(6, 11^1111-1) - (19215400822645944253860920437586326284, 37204645194585992174252915693267578306, 3473654819477394665857484221256136567800161086815834297092488779216863122, 5860191799617673633547572610351797996721850737768032876360978911074629287841061578270832330322236796556721252602860754789786937515870682024273948, 20457423294558182494001919812379023992538802203730791019728543439765347851316366537094696896669915675685581905102118246887673397020172285247862426612188418787649371716686651256443143210952163970564228423098202682066311189439731080552623884051737264415984619097656479060977602722566383385989, 311628095411678159849237738619458396497534696043580912225334269371611836910345930320700816649653412141574887113710604828156159177769285115652741014638785285820578943010943846225597311231847997461959204894255074229895666356909071243390280307709880906261008237873840245959883405303580405277298513108957483306488193844321589356441983980532251051786704380984788999660195252373574924026139168936921591652831237741973242604363696352878914129671292072201700073286987126265965322808664802662993006926302359371379531571194266134916767573373504566621665949840469229781956838744551367172353) - sage: sum_of_k_squares(7, 0) - (0, 0, 0, 0, 0, 0, 0) - sage: sum_of_k_squares(30,999999) - (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7, 44, 999) - sage: sum_of_k_squares(1, 9) - (3,) - sage: sum_of_k_squares(1, 10) - Traceback (most recent call last): - ... - ValueError: 10 is not a sum of 1 square - sage: sum_of_k_squares(1, -10) - Traceback (most recent call last): - ... - ValueError: -10 is not a sum of 1 square - sage: sum_of_k_squares(0, 9) - Traceback (most recent call last): - ... - ValueError: 9 is not a sum of 0 squares - sage: sum_of_k_squares(0, 0) - () - sage: sum_of_k_squares(7, -1) - Traceback (most recent call last): - ... - ValueError: -1 is not a sum of 7 squares - sage: sum_of_k_squares(-1, 0) - Traceback (most recent call last): - ... - ValueError: k = -1 must be non-negative - """ - n = ZZ(n) - k = int(k) - - if k <= 4: - if k == 4: - return four_squares(n) - if k == 3: - return three_squares(n) - if k == 2: - return two_squares(n) - if k == 1: - if n >= 0: - x, r = n.sqrtrem() - if not r: - return (x,) - raise ValueError("%s is not a sum of 1 square"%n) - if k == 0: - if n == 0: - return tuple() - raise ValueError("%s is not a sum of 0 squares"%n) - raise ValueError("k = %s must be non-negative"%k) - - if n < 0: - raise ValueError("%s is not a sum of %s squares"%(n,k)) - - # Recursively subtract the largest square - t = [] - while k > 4: - x = n.isqrt() - t.insert(0, x) - n -= x*x - k -= 1 - - t = list(four_squares(n)) + t - return tuple(t) - -def subfactorial(n): - r""" - Subfactorial or rencontres numbers, or derangements: number of - permutations of `n` elements with no fixed points. - - INPUT: - - - - ``n`` - non negative integer - - - OUTPUT: - - - - ``integer`` - function value - - - EXAMPLES:: - - sage: subfactorial(0) - 1 - sage: subfactorial(1) - 0 - sage: subfactorial(8) - 14833 - - AUTHORS: - - - Jaap Spies (2007-01-23) - """ - return factorial(n)*sum(((-1)**k)/factorial(k) for k in range(n+1)) - -def is_power_of_two(n): - r""" - This function returns True if and only if ``n`` is a power of - 2 - - INPUT: - - - ``n`` - integer - - OUTPUT: - - - ``True`` - if n is a power of 2 - - - ``False`` - if not - - EXAMPLES:: - - sage: is_power_of_two(1024) - True - sage: is_power_of_two(1) - True - sage: is_power_of_two(24) - False - sage: is_power_of_two(0) - False - sage: is_power_of_two(-4) - False - """ - return ZZ(n).popcount() == 1 - -def differences(lis, n=1): - """ - Returns the `n` successive differences of the elements in - `lis`. - - EXAMPLES:: - - sage: differences(prime_range(50)) - [1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4] - sage: differences([i^2 for i in range(1,11)]) - [3, 5, 7, 9, 11, 13, 15, 17, 19] - sage: differences([i^3 + 3*i for i in range(1,21)]) - [10, 22, 40, 64, 94, 130, 172, 220, 274, 334, 400, 472, 550, 634, 724, 820, 922, 1030, 1144] - sage: differences([i^3 - i^2 for i in range(1,21)], 2) - [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 100, 106, 112] - sage: differences([p - i^2 for i, p in enumerate(prime_range(50))], 3) - [-1, 2, -4, 4, -4, 4, 0, -6, 8, -6, 0, 4] - - AUTHORS: - - - Timothy Clemans (2008-03-09) - """ - n = ZZ(n) - if n < 1: - raise ValueError('n must be greater than 0') - lis = [lis[i + 1] - num for i, num in enumerate(lis[:-1])] - if n == 1: - return lis - return differences(lis, n - 1) - -def _cmp_complex_for_display(a, b): - r""" - Compare two complex numbers in a "pretty" (but mathematically - meaningless) fashion, for display only. - - Real numbers (with a zero imaginary part) come before complex numbers, - and are sorted. Complex numbers are sorted by their real part - unless their real parts are quite close, in which case they are - sorted by their imaginary part. - - EXAMPLES:: - - sage: import sage.rings.arith - sage: cmp_c = sage.rings.arith._cmp_complex_for_display - sage: teeny = 3e-11 - sage: cmp_c(CC(5), CC(3, 3)) - -1 - sage: cmp_c(CC(3), CC(5, 5)) - -1 - sage: cmp_c(CC(5), CC(3)) - 1 - sage: cmp_c(CC(teeny, -1), CC(-teeny, 1)) - -1 - sage: cmp_c(CC(teeny, 1), CC(-teeny, -1)) - 1 - sage: cmp_c(CC(0, 1), CC(1, 0.5)) - -1 - sage: cmp_c(CC(3+teeny, -1), CC(3-teeny, 1)) - -1 - sage: CIF200 = ComplexIntervalField(200) - sage: cmp_c(CIF200(teeny, -1), CIF200(-teeny, 1)) - -1 - sage: cmp_c(CIF200(teeny, 1), CIF200(-teeny, -1)) - 1 - sage: cmp_c(CIF200(0, 1), CIF200(1, 0.5)) - -1 - sage: cmp_c(CIF200(3+teeny, -1), CIF200(3-teeny, 1)) - -1 - """ - ar = a.real(); br = b.real() - ai = a.imag(); bi = b.imag() - epsilon = ar.parent()(1e-10) - if ai: - if bi: - if abs(br) < epsilon: - if abs(ar) < epsilon: - return cmp(ai, bi) - return cmp(ar, 0) - if abs((ar - br) / br) < epsilon: - return cmp(ai, bi) - return cmp(ar, br) - else: - return 1 - else: - if bi: - return -1 - else: - return cmp(ar, br) - -def sort_complex_numbers_for_display(nums): - r""" - Given a list of complex numbers (or a list of tuples, where the - first element of each tuple is a complex number), we sort the list - in a "pretty" order. First come the real numbers (with zero - imaginary part), then the complex numbers sorted according to - their real part. If two complex numbers have a real part which is - sufficiently close, then they are sorted according to their - imaginary part. - - This is not a useful function mathematically (not least because - there's no principled way to determine whether the real components - should be treated as equal or not). It is called by various - polynomial root-finders; its purpose is to make doctest printing - more reproducible. - - We deliberately choose a cumbersome name for this function to - discourage use, since it is mathematically meaningless. - - EXAMPLES:: - - sage: import sage.rings.arith - sage: sort_c = sort_complex_numbers_for_display - sage: nums = [CDF(i) for i in range(3)] - sage: for i in range(3): - ....: nums.append(CDF(i + RDF.random_element(-3e-11, 3e-11), - ....: RDF.random_element())) - ....: nums.append(CDF(i + RDF.random_element(-3e-11, 3e-11), - ....: RDF.random_element())) - sage: shuffle(nums) - sage: sort_c(nums) - [0.0, 1.0, 2.0, -2.862406201002009e-11 - 0.7088740263015161*I, 2.2108362706985576e-11 - 0.43681052967509904*I, 1.0000000000138833 - 0.7587654737635712*I, 0.9999999999760288 - 0.7238965893336062*I, 1.9999999999874383 - 0.4560801012073723*I, 1.9999999999869107 + 0.6090836283134269*I] - """ - if len(nums) == 0: - return nums - - if isinstance(nums[0], tuple): - return sorted(nums, cmp=_cmp_complex_for_display, key=lambda t: t[0]) - else: - return sorted(nums, cmp=_cmp_complex_for_display) - -def fundamental_discriminant(D): - r""" - Return the discriminant of the quadratic extension - `K=Q(\sqrt{D})`, i.e. an integer d congruent to either 0 or - 1, mod 4, and such that, at most, the only square dividing it is - 4. - - INPUT: - - - ``D`` - an integer - - OUTPUT: - - - an integer, the fundamental discriminant - - EXAMPLES:: - - sage: fundamental_discriminant(102) - 408 - sage: fundamental_discriminant(720) - 5 - sage: fundamental_discriminant(2) - 8 - - """ - D = ZZ(D) - D = D.squarefree_part() - if D%4 == 1: - return D - return 4*D - -def squarefree_divisors(x): - """ - Iterator over the squarefree divisors (up to units) of the element x. - - Depends on the output of the prime_divisors function. - - INPUT: - - - x -- an element of any ring for which the prime_divisors - function works. - - EXAMPLES:: - - sage: list(squarefree_divisors(7)) - [1, 7] - sage: list(squarefree_divisors(6)) - [1, 2, 3, 6] - sage: list(squarefree_divisors(12)) - [1, 2, 3, 6] - - TESTS: - - Check that the first divisor (i.e. `1`) is a Sage integer (see - :trac:`17852`):: - - sage: a = next(squarefree_divisors(14)) - sage: a - 1 - sage: type(a) - - """ - for a in powerset(prime_divisors(x)): - yield prod(a, ZZ.one()) - -def dedekind_sum(p, q, algorithm='default'): - r""" - Return the Dedekind sum `s(p,q)` defined for integers `p`, `q` as - - .. MATH:: - - s(p,q) = \sum_{i=0}^{q-1} \left(\!\left(\frac{i}{q}\right)\!\right) - \left(\!\left(\frac{pi}{q}\right)\!\right) - - where - - .. MATH:: - - ((x))=\begin{cases} - x-\lfloor x \rfloor - \frac{1}{2} &\mbox{if } - x \in \QQ \setminus \ZZ \\ - 0 & \mbox{if } x \in \ZZ. - \end{cases} - - .. WARNING:: - - Caution is required as the Dedekind sum sometimes depends on the - algorithm or is left undefined when `p` and `q` are not coprime. - - INPUT: - - - ``p``, ``q`` -- integers - - ``algorithm`` -- must be one of the following - - - ``'default'`` - (default) use FLINT - - ``'flint'`` - use FLINT - - ``'pari'`` - use PARI (gives different results if `p` and `q` - are not coprime) - - OUTPUT: a rational number - - EXAMPLES: - - Several small values:: - - sage: for q in range(10): print [dedekind_sum(p,q) for p in range(q+1)] - [0] - [0, 0] - [0, 0, 0] - [0, 1/18, -1/18, 0] - [0, 1/8, 0, -1/8, 0] - [0, 1/5, 0, 0, -1/5, 0] - [0, 5/18, 1/18, 0, -1/18, -5/18, 0] - [0, 5/14, 1/14, -1/14, 1/14, -1/14, -5/14, 0] - [0, 7/16, 1/8, 1/16, 0, -1/16, -1/8, -7/16, 0] - [0, 14/27, 4/27, 1/18, -4/27, 4/27, -1/18, -4/27, -14/27, 0] - - Check relations for restricted arguments:: - - sage: q = 23; dedekind_sum(1, q); (q-1)*(q-2)/(12*q) - 77/46 - 77/46 - sage: p, q = 100, 723 # must be coprime - sage: dedekind_sum(p, q) + dedekind_sum(q, p) - 31583/86760 - sage: -1/4 + (p/q + q/p + 1/(p*q))/12 - 31583/86760 - - We check that evaluation works with large input:: - - sage: dedekind_sum(3^54 - 1, 2^93 + 1) - 459340694971839990630374299870/29710560942849126597578981379 - sage: dedekind_sum(3^54 - 1, 2^93 + 1, algorithm='pari') - 459340694971839990630374299870/29710560942849126597578981379 - - We check consistency of the results:: - - sage: dedekind_sum(5, 7, algorithm='default') - -1/14 - sage: dedekind_sum(5, 7, algorithm='flint') - -1/14 - sage: dedekind_sum(5, 7, algorithm='pari') - -1/14 - sage: dedekind_sum(6, 8, algorithm='default') - -1/8 - sage: dedekind_sum(6, 8, algorithm='flint') - -1/8 - sage: dedekind_sum(6, 8, algorithm='pari') - -1/8 - - REFERENCES: - - .. [Apostol] T. Apostol, Modular functions and Dirichlet series - in number theory, Springer, 1997 (2nd ed), section 3.7--3.9. - - - :wikipedia:`Dedekind\_sum` - """ - if algorithm == 'default' or algorithm == 'flint': - return flint_arith.dedekind_sum(p, q) - - if algorithm == 'pari': - import sage.interfaces.gp - x = sage.interfaces.gp.gp('sumdedekind(%s,%s)' % (p, q)) - return Rational(x) - - raise ValueError('unknown algorithm') - +from sage.misc.lazy_import import lazy_import +lazy_import('sage.arith.all', '*', deprecation=19879) diff --git a/src/sage/rings/bernoulli_mod_p.pyx b/src/sage/rings/bernoulli_mod_p.pyx index 9b6e31711f7..554b1d48228 100644 --- a/src/sage/rings/bernoulli_mod_p.pyx +++ b/src/sage/rings/bernoulli_mod_p.pyx @@ -25,7 +25,7 @@ arith_int = sage.rings.fast_arith.arith_int() ctypedef long long llong -import sage.rings.arith +import sage.arith.all from sage.libs.ntl import all as ntl from sage.libs.ntl.ntl_ZZ_pX cimport ntl_ZZ_pX @@ -136,12 +136,12 @@ def bernoulli_mod_p(int p): if p <= 2: raise ValueError, "p (=%s) must be a prime >= 3"%p - if not sage.rings.arith.is_prime(p): + if not sage.arith.all.is_prime(p): raise ValueError, "p (=%s) must be a prime"%p cdef int g, gSqr, gInv, gInvSqr, isOdd - g = sage.rings.arith.primitive_root(p) + g = sage.arith.all.primitive_root(p) gInv = arith_int.c_inverse_mod_int(g, p) gSqr = (( g) * g) % p gInvSqr = (( gInv) * gInv) % p @@ -305,7 +305,7 @@ def bernoulli_mod_p_single(long p, long k): if p <= 2: raise ValueError, "p (=%s) must be a prime >= 3"%p - if not sage.rings.arith.is_prime(p): + if not sage.arith.all.is_prime(p): raise ValueError, "p (=%s) must be a prime"%p R = Integers(p) diff --git a/src/sage/rings/big_oh.py b/src/sage/rings/big_oh.py index 05ef1f3d47e..c51fdfdf04a 100644 --- a/src/sage/rings/big_oh.py +++ b/src/sage/rings/big_oh.py @@ -9,7 +9,7 @@ - `polynomials <../../../polynomial_rings/index.html>`_ """ -import sage.rings.arith as arith +import sage.arith.all as arith import laurent_series_ring_element import sage.rings.padics.factory as padics_factory import sage.rings.padics.padic_generic_element as padic_generic_element diff --git a/src/sage/rings/cfinite_sequence.py b/src/sage/rings/cfinite_sequence.py index fa1e2690492..de2b71fa056 100644 --- a/src/sage/rings/cfinite_sequence.py +++ b/src/sage/rings/cfinite_sequence.py @@ -98,7 +98,7 @@ from sage.rings.integer import Integer from sage.rings.integer_ring import ZZ from sage.rings.rational_field import QQ -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.polynomial.polynomial_ring import PolynomialRing_general from sage.rings.laurent_series_ring import LaurentSeriesRing diff --git a/src/sage/rings/complex_interval.pyx b/src/sage/rings/complex_interval.pyx index 618ea1ba715..ec2eb4cb6a1 100644 --- a/src/sage/rings/complex_interval.pyx +++ b/src/sage/rings/complex_interval.pyx @@ -1674,36 +1674,6 @@ cdef class ComplexIntervalFieldElement(sage.structure.element.FieldElement): """ return True -# def algdep(self, n, **kwds): -# """ -# Returns a polynomial of degree at most $n$ which is approximately -# satisfied by this complex number. Note that the returned polynomial -# need not be irreducible, and indeed usually won't be if $z$ is a good -# approximation to an algebraic number of degree less than $n$. - -# ALGORITHM: Uses the PARI C-library algdep command. - -# INPUT: Type algdep? at the top level prompt. All additional -# parameters are passed onto the top-level algdep command. - -# EXAMPLES:: -# -# sage: C = ComplexIntervalField() -# sage: z = (1/2)*(1 + sqrt(3.0) *C.0); z -# 0.500000000000000 + 0.866025403784439*I -# sage: p = z.algdep(5); p -# x^5 + x^2 -# sage: p.factor() -# (x + 1) * x^2 * (x^2 - x + 1) -# sage: z^2 - z + 1 -# 0.000000000000000111022302462516 -# """ -# import sage.rings.arith -# return sage.rings.arith.algdep(self,n, **kwds) - -# def algebraic_dependancy( self, n ): -# return self.algdep( n ) - def cos(self): r""" Compute the cosine of this complex interval. diff --git a/src/sage/rings/complex_mpc.pyx b/src/sage/rings/complex_mpc.pyx index 759d70caed3..56097c98f74 100644 --- a/src/sage/rings/complex_mpc.pyx +++ b/src/sage/rings/complex_mpc.pyx @@ -1284,8 +1284,8 @@ cdef class MPComplexNumber(sage.structure.element.FieldElement): sage: z^2 - z + 1 1.11022302462516e-16 """ - import sage.rings.arith - return sage.rings.arith.algdep(self,n, **kwds) + import sage.arith.all + return sage.arith.all.algdep(self, n, **kwds) # Former misspelling algebraic_dependancy = algebraic_dependency diff --git a/src/sage/rings/complex_number.pyx b/src/sage/rings/complex_number.pyx index 8adc720188a..27c9e6d7201 100644 --- a/src/sage/rings/complex_number.pyx +++ b/src/sage/rings/complex_number.pyx @@ -2387,8 +2387,8 @@ cdef class ComplexNumber(sage.structure.element.FieldElement): sage: z^2 - z + 1 1.11022302462516e-16 """ - import sage.rings.arith - return sage.rings.arith.algdep(self,n, **kwds) + import sage.arith.all + return sage.arith.all.algdep(self, n, **kwds) def algebraic_dependancy( self, n ): """ diff --git a/src/sage/rings/factorint.pyx b/src/sage/rings/factorint.pyx index a04638c3ce6..29815302dff 100644 --- a/src/sage/rings/factorint.pyx +++ b/src/sage/rings/factorint.pyx @@ -82,7 +82,7 @@ cpdef aurifeuillian(n, m, F=None, bint check=True): Mathematics of Computation. **61** (1993). No. 203. pp 131-149. :arXiv:`1004.5466v1`. http://www.jstor.org/stable/2152941 """ - from sage.rings.arith import euler_phi + from sage.arith.all import euler_phi from sage.rings.real_mpfi import RealIntervalField if check: if not n.is_squarefree(): diff --git a/src/sage/rings/fast_arith.pyx b/src/sage/rings/fast_arith.pyx index 4b65b5abb98..4207fe65a38 100644 --- a/src/sage/rings/fast_arith.pyx +++ b/src/sage/rings/fast_arith.pyx @@ -166,7 +166,7 @@ cpdef prime_range(start, stop=None, algorithm="pari_primes", bint py_ints=False) NEXT_PRIME_VIADIFF(p, pari_prime_ptr) elif algorithm == "pari_isprime": - from sage.rings.arith import primes + from sage.arith.all import primes res = list(primes(start, stop)) else: raise ValueError("algorithm argument must be either ``pari_primes`` or ``pari_isprime``") diff --git a/src/sage/rings/finite_rings/constructor.py b/src/sage/rings/finite_rings/constructor.py index 130adc02f87..564facec580 100644 --- a/src/sage/rings/finite_rings/constructor.py +++ b/src/sage/rings/finite_rings/constructor.py @@ -420,7 +420,7 @@ def create_key_and_extra_args(self, order, name=None, modulus=None, names=None, sage: GF.create_key_and_extra_args(9, 'a', foo='value') ((9, ('a',), x^2 + 2*x + 2, 'givaro', "{'foo': 'value'}", 3, 2, True), {'foo': 'value'}) """ - import sage.rings.arith + import sage.arith.all from sage.structure.proof.all import WithProof, arithmetic if proof is None: proof = arithmetic() diff --git a/src/sage/rings/finite_rings/element_givaro.pyx b/src/sage/rings/finite_rings/element_givaro.pyx index 403b99c8fd0..96690cbae0c 100644 --- a/src/sage/rings/finite_rings/element_givaro.pyx +++ b/src/sage/rings/finite_rings/element_givaro.pyx @@ -62,7 +62,7 @@ from element_ext_pari import FiniteField_ext_pariElement from element_pari_ffelt import FiniteFieldElement_pari_ffelt from sage.structure.sage_object cimport SageObject import operator -import sage.rings.arith +import sage.arith.all import constructor as finite_field import sage.interfaces.gap @@ -1581,7 +1581,7 @@ cdef class FiniteField_givaroElement(FinitePolyExtElement): # using how elements are represented as a power of the generator ?? # code copy'n'pasted from element_ext_pari.py - import sage.rings.arith + import sage.arith.all if self._multiplicative_order is not None: return self._multiplicative_order @@ -1590,7 +1590,7 @@ cdef class FiniteField_givaroElement(FinitePolyExtElement): raise ArithmeticError("Multiplicative order of 0 not defined.") n = (self._cache).order_c() - 1 order = Integer(1) - for p, e in sage.rings.arith.factor(n): + for p, e in sage.arith.all.factor(n): # Determine the power of p that divides the order. a = self**(n/(p**e)) while a != 1: diff --git a/src/sage/rings/finite_rings/finite_field_base.pyx b/src/sage/rings/finite_rings/finite_field_base.pyx index 92cc99f8d6e..fb2a990a9f5 100644 --- a/src/sage/rings/finite_rings/finite_field_base.pyx +++ b/src/sage/rings/finite_rings/finite_field_base.pyx @@ -639,7 +639,7 @@ cdef class FiniteField(Field): sage: K.multiplicative_generator() a + 12 """ - from sage.rings.arith import primitive_root + from sage.arith.all import primitive_root if self.__multiplicative_generator is not None: return self.__multiplicative_generator diff --git a/src/sage/rings/finite_rings/integer_mod.pyx b/src/sage/rings/finite_rings/integer_mod.pyx index 7ca1ed33e73..2b73ef60696 100644 --- a/src/sage/rings/finite_rings/integer_mod.pyx +++ b/src/sage/rings/finite_rings/integer_mod.pyx @@ -85,7 +85,6 @@ import operator cdef bint use_32bit_type(int_fast64_t modulus): return modulus <= INTEGER_MOD_INT32_LIMIT -## import arith import sage.rings.rational as rational from sage.libs.pari.all import pari, PariError import sage.rings.integer_ring as integer_ring @@ -1071,7 +1070,7 @@ cdef class IntegerMod_abstract(FiniteRingElement): vmod.append(w) moduli.append(k) # Now combine in all possible ways using the CRT - from sage.rings.arith import CRT_basis + from sage.arith.all import CRT_basis basis = CRT_basis(moduli) from sage.misc.mrange import cartesian_product_iterator v = [] diff --git a/src/sage/rings/finite_rings/integer_mod_ring.py b/src/sage/rings/finite_rings/integer_mod_ring.py index 8ab0fa50415..223ba3f43de 100644 --- a/src/sage/rings/finite_rings/integer_mod_ring.py +++ b/src/sage/rings/finite_rings/integer_mod_ring.py @@ -61,7 +61,7 @@ import sage.misc.prandom as random -from sage.rings.arith import factor, primitive_root +from sage.arith.all import factor, primitive_root, CRT_basis import sage.rings.commutative_ring as commutative_ring import sage.rings.ring as ring import integer_mod @@ -975,7 +975,6 @@ def square_roots_of_one(self): vmod.append(w) moduli.append(k) # Now combine in all possible ways using the CRT - from sage.rings.arith import CRT_basis basis = CRT_basis(moduli) from sage.misc.mrange import cartesian_product_iterator v = [] diff --git a/src/sage/rings/function_field/function_field.py b/src/sage/rings/function_field/function_field.py index 62a257d7566..10a3bfd3494 100644 --- a/src/sage/rings/function_field/function_field.py +++ b/src/sage/rings/function_field/function_field.py @@ -556,7 +556,7 @@ def _make_monic_integral(self, f): f = f / c # find lcm of denominators - from sage.rings.arith import lcm + from sage.arith.all import lcm # would be good to replace this by minimal... d = lcm([b.denominator() for b in f.list() if b]) if d != 1: @@ -1242,7 +1242,7 @@ def _to_bivariate_polynomial(self, f): (X^7*t^2 - X^4*t^5 - X^3 + t^3, t^3) """ v = f.list() - from sage.rings.arith import LCM + from sage.arith.all import LCM denom = LCM([a.denominator() for a in v]) S = denom.parent() x,t = S.base_ring()['%s,%s'%(f.parent().variable_name(),self.variable_name())].gens() diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx index bf5224ff1d5..2f212ee6965 100644 --- a/src/sage/rings/integer.pyx +++ b/src/sage/rings/integer.pyx @@ -3625,7 +3625,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement): """ if self.is_zero(): raise ArithmeticError, "Support of 0 not defined." - return sage.rings.arith.prime_factors(self) + return sage.arith.all.prime_factors(self) def coprime_integers(self, m): """ diff --git a/src/sage/rings/integer_ring.pyx b/src/sage/rings/integer_ring.pyx index 6582dd9b535..e919e92c09d 100644 --- a/src/sage/rings/integer_ring.pyx +++ b/src/sage/rings/integer_ring.pyx @@ -76,8 +76,8 @@ cdef void late_import(): # A hack to avoid circular imports. global arith if arith is None: - import sage.rings.arith - arith = sage.rings.arith + import sage.arith.all + arith = sage.arith.all cdef int number_of_integer_rings = 0 diff --git a/src/sage/rings/misc.py b/src/sage/rings/misc.py index d84298aeee4..1a380a11868 100644 --- a/src/sage/rings/misc.py +++ b/src/sage/rings/misc.py @@ -23,6 +23,8 @@ def composite_field(K, L): EXAMPLES: sage: composite_field(QQ,QQbar) + doctest:...: DeprecationWarning: The function composite_field() is deprecated. Use get_coercion_model().common_parent() instead + See http://trac.sagemath.org/19415 for details. Algebraic Field sage: composite_field(QQ,QQ[sqrt(2)]) Number Field in sqrt2 with defining polynomial x^2 - 2 @@ -33,6 +35,8 @@ def composite_field(K, L): ... ValueError: unable to find a common field """ + from sage.misc.superseded import deprecation + deprecation(19415, "The function composite_field() is deprecated. Use get_coercion_model().common_parent() instead") C = Sequence([K(0), L(0)]).universe() if C not in _Fields: raise ValueError("unable to find a common field") diff --git a/src/sage/rings/number_field/class_group.py b/src/sage/rings/number_field/class_group.py index 25cccf5b453..43333bf2fbf 100644 --- a/src/sage/rings/number_field/class_group.py +++ b/src/sage/rings/number_field/class_group.py @@ -46,7 +46,7 @@ from sage.structure.sequence import Sequence from sage.structure.element import MonoidElement from sage.groups.old import Group -from sage.rings.arith import LCM +from sage.arith.all import LCM from sage.rings.all import ZZ diff --git a/src/sage/rings/number_field/number_field.py b/src/sage/rings/number_field/number_field.py index 709575ac279..133ac360d89 100644 --- a/src/sage/rings/number_field/number_field.py +++ b/src/sage/rings/number_field/number_field.py @@ -192,7 +192,7 @@ def proof_flag(t): from sage.misc.latex import latex -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.rational_field as rational_field import sage.rings.integer_ring as integer_ring import sage.rings.infinity as infinity @@ -6202,7 +6202,7 @@ def zeta(self, n=2, all=False): # First check if the degree of K is compatible with an # inclusion QQ(\zeta_n) -> K. - if sage.rings.arith.euler_phi(n).divides(K.absolute_degree()): + if sage.arith.all.euler_phi(n).divides(K.absolute_degree()): # Factor the n-th cyclotomic polynomial over K. f = K.pari_polynomial('y') factors = pari.polcyclo(n).factornf(f).component(1) @@ -9400,7 +9400,7 @@ def _element_constructor_(self, x, check=True): ## # Find the smallest power r >= 1 of the generator g of K that is in self, ## # i.e., find the smallest r such that g^r has order dividing m. -## d = sage.rings.arith.gcd(m,n) +## d = sage.arith.all.gcd(m,n) ## r = n // d ## # Since we use the power basis for cyclomotic fields, if every diff --git a/src/sage/rings/number_field/number_field_element.pyx b/src/sage/rings/number_field/number_field_element.pyx index 07729a8fdcb..d9151a9c114 100644 --- a/src/sage/rings/number_field/number_field_element.pyx +++ b/src/sage/rings/number_field/number_field_element.pyx @@ -46,7 +46,6 @@ import sage.rings.rational_field import sage.rings.rational import sage.rings.integer_ring import sage.rings.integer -import sage.rings.arith cimport number_field_base import number_field diff --git a/src/sage/rings/number_field/number_field_ideal.py b/src/sage/rings/number_field/number_field_ideal.py index 73242201075..af4997c31c9 100644 --- a/src/sage/rings/number_field/number_field_ideal.py +++ b/src/sage/rings/number_field/number_field_ideal.py @@ -45,7 +45,7 @@ import sage.rings.rational_field as rational_field import sage.rings.integer_ring as integer_ring -import sage.rings.arith as arith +import sage.arith.all as arith import sage.misc.misc as misc from sage.rings.finite_rings.constructor import FiniteField @@ -1621,7 +1621,7 @@ def residue_symbol(self, e, m, check=True): ValueError: The residue symbol to that power is not defined for the number field """ - from sage.rings.arith import kronecker_symbol + from sage.arith.all import kronecker_symbol K = self.ring() if m == 2 and K.absolute_degree() == 1: diff --git a/src/sage/rings/number_field/number_field_rel.py b/src/sage/rings/number_field/number_field_rel.py index f6b00d94476..9de39430405 100644 --- a/src/sage/rings/number_field/number_field_rel.py +++ b/src/sage/rings/number_field/number_field_rel.py @@ -80,7 +80,6 @@ from sage.structure.parent_gens import localvars import sage.libs.ntl.all as ntl -import sage.rings.arith from sage.categories.map import is_Map from sage.structure.sequence import Sequence diff --git a/src/sage/rings/number_field/splitting_field.py b/src/sage/rings/number_field/splitting_field.py index 13123afe7a4..471e35f8d14 100644 --- a/src/sage/rings/number_field/splitting_field.py +++ b/src/sage/rings/number_field/splitting_field.py @@ -19,7 +19,7 @@ #***************************************************************************** from sage.rings.integer import Integer -from sage.rings.arith import factorial +from sage.arith.all import factorial from sage.rings.number_field.all import NumberField from sage.rings.number_field.number_field_base import is_NumberField from sage.rings.polynomial.all import PolynomialRing diff --git a/src/sage/rings/number_field/totallyreal_data.pyx b/src/sage/rings/number_field/totallyreal_data.pyx index ae9360314d1..0c25d1e63c9 100644 --- a/src/sage/rings/number_field/totallyreal_data.pyx +++ b/src/sage/rings/number_field/totallyreal_data.pyx @@ -28,7 +28,7 @@ include "sage/ext/cdefs.pxi" include "sage/ext/stdsage.pxi" include "sage/ext/interrupt.pxi" -from sage.rings.arith import binomial, gcd +from sage.arith.all import binomial, gcd from sage.rings.rational_field import RationalField from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.real_mpfi import RealIntervalField diff --git a/src/sage/rings/number_field/totallyreal_rel.py b/src/sage/rings/number_field/totallyreal_rel.py index 9a13f54447b..6bd6e1830ad 100644 --- a/src/sage/rings/number_field/totallyreal_rel.py +++ b/src/sage/rings/number_field/totallyreal_rel.py @@ -85,7 +85,7 @@ #***************************************************************************** -from sage.rings.arith import binomial, gcd, divisors +from sage.arith.all import binomial, gcd, divisors from sage.rings.integer import Integer from sage.rings.integer_ring import IntegerRing from sage.rings.number_field.totallyreal_data import ZZx, lagrange_degree_3, int_has_small_square_divisor, hermite_constant diff --git a/src/sage/rings/padics/padic_generic_element.pyx b/src/sage/rings/padics/padic_generic_element.pyx index ec3cadbea99..ce5f60b0771 100644 --- a/src/sage/rings/padics/padic_generic_element.pyx +++ b/src/sage/rings/padics/padic_generic_element.pyx @@ -667,7 +667,7 @@ cdef class pAdicGenericElement(LocalGenericElement): x^4 - x^3 + x^2 - x + 1 """ # TODO: figure out if this works for extension rings. If not, move this to padic_base_generic_element. - from sage.rings.arith import algdep + from sage.arith.all import algdep return algdep(self, n) def algebraic_dependency(self, n): @@ -1544,7 +1544,7 @@ cdef class pAdicGenericElement(LocalGenericElement): p = self.parent().prime() alpha = self.unit_part().lift() m = Integer(p**self.precision_relative()) - from sage.rings.arith import rational_reconstruction + from sage.arith.all import rational_reconstruction r = rational_reconstruction(alpha, m) return (Rational(p)**self.valuation())*r diff --git a/src/sage/rings/polynomial/complex_roots.py b/src/sage/rings/polynomial/complex_roots.py index 42432118f68..3ddfdafe174 100644 --- a/src/sage/rings/polynomial/complex_roots.py +++ b/src/sage/rings/polynomial/complex_roots.py @@ -40,7 +40,7 @@ from sage.rings.complex_field import ComplexField from sage.rings.complex_interval_field import ComplexIntervalField from sage.rings.qqbar import AA, QQbar -from sage.rings.arith import sort_complex_numbers_for_display +from sage.arith.all import sort_complex_numbers_for_display from sage.rings.polynomial.refine_root import refine_root diff --git a/src/sage/rings/polynomial/cyclotomic.pyx b/src/sage/rings/polynomial/cyclotomic.pyx index 17c576e40ef..cc7f6d0e3fa 100644 --- a/src/sage/rings/polynomial/cyclotomic.pyx +++ b/src/sage/rings/polynomial/cyclotomic.pyx @@ -32,7 +32,7 @@ from libc.string cimport memset from sage.structure.element cimport parent_c -from sage.rings.arith import factor +from sage.arith.all import factor from sage.rings.infinity import infinity from sage.rings.integer_ring import ZZ from sage.misc.all import prod, subsets diff --git a/src/sage/rings/polynomial/multi_polynomial.pyx b/src/sage/rings/polynomial/multi_polynomial.pyx index 6abf48db402..41b432aa70e 100644 --- a/src/sage/rings/polynomial/multi_polynomial.pyx +++ b/src/sage/rings/polynomial/multi_polynomial.pyx @@ -982,8 +982,7 @@ cdef class MPolynomial(CommutativeRingElement): Rational Field """ - from sage.rings.arith import gcd - from sage.rings.all import ZZ + from sage.arith.all import gcd return gcd(self.coefficients()) def is_generator(self): diff --git a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx index b76009a6a79..00b2e6d1dc6 100644 --- a/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_libsingular.pyx @@ -218,7 +218,7 @@ from sage.rings.integer cimport Integer from sage.rings.finite_rings.integer_mod_ring import is_IntegerModRing from sage.rings.number_field.number_field_base cimport NumberField -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.structure.element import coerce_binop from sage.structure.parent cimport Parent @@ -1371,14 +1371,14 @@ cdef class MPolynomialRing_libsingular(MPolynomialRing_generic): # singular converts to bits from base_10 in mpr_complex.cc by: # size_t bits = 1 + (size_t) ((float)digits * 3.5); precision = base_ring.precision() - digits = sage.rings.arith.ceil((2*precision - 2)/7.0) + digits = sage.arith.all.ceil((2*precision - 2)/7.0) self.__singular = singular.ring("(real,%d,0)"%digits, _vars, order=order) elif is_ComplexField(base_ring): # singular converts to bits from base_10 in mpr_complex.cc by: # size_t bits = 1 + (size_t) ((float)digits * 3.5); precision = base_ring.precision() - digits = sage.rings.arith.ceil((2*precision - 2)/7.0) + digits = sage.arith.all.ceil((2*precision - 2)/7.0) self.__singular = singular.ring("(complex,%d,0,I)"%digits, _vars, order=order) elif base_ring.is_prime_field(): diff --git a/src/sage/rings/polynomial/multi_polynomial_ring_generic.pyx b/src/sage/rings/polynomial/multi_polynomial_ring_generic.pyx index 0991db3ff1e..40cf4d02d69 100644 --- a/src/sage/rings/polynomial/multi_polynomial_ring_generic.pyx +++ b/src/sage/rings/polynomial/multi_polynomial_ring_generic.pyx @@ -18,7 +18,7 @@ from sage.rings.polynomial.polynomial_ring_constructor import polynomial_default from sage.misc.misc_c import prod from sage.combinat.integer_vector import IntegerVectors from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing -from sage.rings.arith import binomial +from sage.arith.all import binomial def is_MPolynomialRing(x): return isinstance(x, MPolynomialRing_generic) @@ -607,7 +607,6 @@ cdef class MPolynomialRing_generic(sage.rings.ring.CommutativeRing): 3 """ - from sage.rings.arith import binomial C = [1] #d = 0 for dbar in xrange(1, d+1): C.append(binomial(n+dbar-1, dbar)) @@ -672,7 +671,6 @@ cdef class MPolynomialRing_generic(sage.rings.ring.CommutativeRing): (0, 0, 0, 3, 0) """ # bug: doesn't handle n=1 - from sage.rings.arith import binomial #Select random degree d = ZZ.random_element(0,degree+1) total = binomial(n+d-1, d) @@ -825,7 +823,6 @@ cdef class MPolynomialRing_generic(sage.rings.ring.CommutativeRing): from sage.combinat.integer_vector import IntegerVectors - from sage.rings.arith import binomial #total is 0. Just return if total == 0: diff --git a/src/sage/rings/polynomial/pbori.pyx b/src/sage/rings/polynomial/pbori.pyx index e4f833efc76..393aadfa015 100644 --- a/src/sage/rings/polynomial/pbori.pyx +++ b/src/sage/rings/polynomial/pbori.pyx @@ -1151,7 +1151,7 @@ cdef class BooleanPolynomialRing(MPolynomialRing_generic): sage: r = B.random_element(terms=(n/2)**2) """ from sage.rings.integer import Integer - from sage.rings.arith import binomial + from sage.arith.all import binomial if not vars_set: vars_set=range(self.ngens()) diff --git a/src/sage/rings/polynomial/polynomial_element.pyx b/src/sage/rings/polynomial/polynomial_element.pyx index f2233643228..e6b71f2c9aa 100644 --- a/src/sage/rings/polynomial/polynomial_element.pyx +++ b/src/sage/rings/polynomial/polynomial_element.pyx @@ -50,7 +50,6 @@ import operator, copy, re import sage.rings.rational import sage.rings.integer import polynomial_ring -import sage.rings.arith import sage.rings.integer_ring import sage.rings.rational_field import sage.rings.finite_rings.integer_mod_ring @@ -91,7 +90,8 @@ from sage.structure.parent_gens cimport ParentWithGens from sage.misc.derivative import multi_derivative -from sage.rings.arith import sort_complex_numbers_for_display +from sage.arith.all import (sort_complex_numbers_for_display, + power_mod, lcm, is_prime) import polynomial_fateman @@ -1996,7 +1996,6 @@ cdef class Polynomial(CommutativeAlgebraElement): if right < 0: return (~self)**(-right) if modulus: - from sage.rings.arith import power_mod return power_mod(self, right, modulus) if (self).is_gen(): # special case x**n should be faster! P = self.parent() @@ -3687,7 +3686,7 @@ cdef class Polynomial(CommutativeAlgebraElement): G = None ch = R.characteristic() - if not (ch == 0 or sage.rings.arith.is_prime(ch)): + if not (ch == 0 or is_prime(ch)): raise NotImplementedError("factorization of polynomials over rings with composite characteristic is not implemented") from sage.rings.number_field.number_field_base import is_NumberField @@ -4004,7 +4003,7 @@ cdef class Polynomial(CommutativeAlgebraElement): from sage.rings.number_field.splitting_field import splitting_field return splitting_field(f, name, map, **kwds) elif is_FiniteField(F): - degree = sage.rings.arith.lcm([f.degree() for f, _ in self.factor()]) + degree = lcm([f.degree() for f, _ in self.factor()]) return F.extension(degree, name, map=map, **kwds) raise NotImplementedError("splitting_field() is only implemented over number fields and finite fields") diff --git a/src/sage/rings/polynomial/polynomial_element_generic.py b/src/sage/rings/polynomial/polynomial_element_generic.py index 1e11a5e2bbf..eddd02fbb0f 100644 --- a/src/sage/rings/polynomial/polynomial_element_generic.py +++ b/src/sage/rings/polynomial/polynomial_element_generic.py @@ -842,7 +842,7 @@ def gcd(self,other,algorithm=None): """ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing - from sage.rings.arith import lcm + from sage.arith.all import lcm if algorithm is None: if self.base_ring() == ZZ: diff --git a/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx b/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx index 5fa22d0fba1..46943a56c4e 100644 --- a/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx +++ b/src/sage/rings/polynomial/polynomial_integer_dense_flint.pyx @@ -51,7 +51,7 @@ from sage.libs.all import pari, pari_gen from sage.structure.factorization import Factorization from sage.rings.fraction_field_element import FractionFieldElement -from sage.rings.arith import lcm +from sage.arith.all import lcm from sage.libs.flint.fmpz cimport * from sage.libs.flint.fmpz_poly cimport fmpz_poly_reverse, fmpz_poly_revert_series diff --git a/src/sage/rings/polynomial/polynomial_integer_dense_ntl.pyx b/src/sage/rings/polynomial/polynomial_integer_dense_ntl.pyx index f14bece4481..dea138cc76f 100644 --- a/src/sage/rings/polynomial/polynomial_integer_dense_ntl.pyx +++ b/src/sage/rings/polynomial/polynomial_integer_dense_ntl.pyx @@ -58,7 +58,7 @@ from sage.structure.factorization import Factorization from sage.structure.element import coerce_binop from sage.rings.fraction_field_element import FractionFieldElement -from sage.rings.arith import lcm +from sage.arith.all import lcm import sage.rings.polynomial.polynomial_ring from sage.libs.ntl.ZZX cimport * diff --git a/src/sage/rings/polynomial/polynomial_quotient_ring.py b/src/sage/rings/polynomial/polynomial_quotient_ring.py index d3878e2a1b6..6567a86199e 100644 --- a/src/sage/rings/polynomial/polynomial_quotient_ring.py +++ b/src/sage/rings/polynomial/polynomial_quotient_ring.py @@ -1108,7 +1108,7 @@ def S_class_group(self, S, proof=True): for ideal_gen in clgp_gen.gens(): rel_ideal_gen = back_to_rel(phi(ideal_gen)) prod_ideal_gen = [0]*i + [rel_ideal_gen.lift()] + [0]*(n - i - 1) - poly_ideal_gen = self(sage.rings.arith.crt(prod_ideal_gen, moduli)) + poly_ideal_gen = self(sage.arith.all.crt(prod_ideal_gen, moduli)) ideal_gens.append(poly_ideal_gen) clgp_gens.append((tuple(ideal_gens), gen_order)) @@ -1277,7 +1277,7 @@ def S_units(self, S, proof=True): mul_order = unit.multiplicative_order() rel_unit = back_to_rel(phi(unit)) prod_unit = [1]*i + [rel_unit.lift()] + [1]*(n - i - 1) - poly_unit = self(sage.rings.arith.crt(prod_unit, moduli)) + poly_unit = self(sage.arith.all.crt(prod_unit, moduli)) units.append((poly_unit, mul_order)) return units @@ -1403,7 +1403,7 @@ def selmer_group(self, S, m, proof=True): for gen in component_selmer_groups[isos[i][1]]: rel_gen = back_to_rel(phi(gen)) prod_gen = [1]*i + [rel_gen.lift()] + [1]*(n - i - 1) - poly_gen = self(sage.rings.arith.crt(prod_gen, moduli)) + poly_gen = self(sage.arith.all.crt(prod_gen, moduli)) gens.append(poly_gen) return gens diff --git a/src/sage/rings/polynomial/polynomial_singular_interface.py b/src/sage/rings/polynomial/polynomial_singular_interface.py index 96440671b3c..6e7e53b3f9a 100644 --- a/src/sage/rings/polynomial/polynomial_singular_interface.py +++ b/src/sage/rings/polynomial/polynomial_singular_interface.py @@ -52,7 +52,7 @@ from sage.rings.finite_rings.finite_field_base import is_FiniteField from sage.rings.integer_ring import ZZ -import sage.rings.arith +import sage.arith.all import sage.rings.finite_rings.constructor @@ -245,14 +245,14 @@ def _singular_init_(self, singular=singular): # singular converts to bits from base_10 in mpr_complex.cc by: # size_t bits = 1 + (size_t) ((float)digits * 3.5); precision = base_ring.precision() - digits = sage.rings.arith.integer_ceil((2*precision - 2)/7.0) + digits = sage.arith.all.integer_ceil((2*precision - 2)/7.0) self.__singular = singular.ring("(real,%d,0)"%digits, _vars, order=order, check=False) elif is_ComplexField(base_ring): # singular converts to bits from base_10 in mpr_complex.cc by: # size_t bits = 1 + (size_t) ((float)digits * 3.5); precision = base_ring.precision() - digits = sage.rings.arith.integer_ceil((2*precision - 2)/7.0) + digits = sage.arith.all.integer_ceil((2*precision - 2)/7.0) self.__singular = singular.ring("(complex,%d,0,I)"%digits, _vars, order=order, check=False) elif is_RealDoubleField(base_ring): diff --git a/src/sage/rings/polynomial/real_roots.pyx b/src/sage/rings/polynomial/real_roots.pyx index fb3a23efc62..4390760e1fd 100644 --- a/src/sage/rings/polynomial/real_roots.pyx +++ b/src/sage/rings/polynomial/real_roots.pyx @@ -136,7 +136,7 @@ from random import Random import time from sage.rings.all import ZZ, QQ, RR, AA, RealField, RealIntervalField, RIF, RDF, infinity -from sage.rings.arith import binomial, factorial +from sage.arith.all import binomial, factorial from sage.modules.all import vector, FreeModule from sage.matrix.all import MatrixSpace from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing diff --git a/src/sage/rings/polynomial/toy_buchberger.py b/src/sage/rings/polynomial/toy_buchberger.py index 8ee60b44e4f..de10beb9be9 100644 --- a/src/sage/rings/polynomial/toy_buchberger.py +++ b/src/sage/rings/polynomial/toy_buchberger.py @@ -143,7 +143,7 @@ """ from sage.misc.misc import get_verbose -from sage.rings.arith import LCM +from sage.arith.all import LCM from sage.structure.sequence import Sequence #some aliases that conform to Becker and Weispfenning's notation: diff --git a/src/sage/rings/polynomial/toy_d_basis.py b/src/sage/rings/polynomial/toy_d_basis.py index 818c7e5263b..2df63841bde 100644 --- a/src/sage/rings/polynomial/toy_d_basis.py +++ b/src/sage/rings/polynomial/toy_d_basis.py @@ -117,7 +117,7 @@ """ from sage.rings.integer_ring import ZZ -from sage.rings.arith import xgcd, lcm, gcd +from sage.arith.all import xgcd, lcm, gcd from sage.rings.polynomial.toy_buchberger import inter_reduction from sage.structure.sequence import Sequence diff --git a/src/sage/rings/power_series_mpoly.pyx b/src/sage/rings/power_series_mpoly.pyx index 49eae89feee..6642a0f515c 100644 --- a/src/sage/rings/power_series_mpoly.pyx +++ b/src/sage/rings/power_series_mpoly.pyx @@ -4,7 +4,6 @@ from power_series_ring_element cimport PowerSeries from sage.structure.element cimport Element, ModuleElement, RingElement from infinity import infinity, is_Infinite -import arith from sage.libs.all import PariError from power_series_ring_element import is_PowerSeries import rational_field diff --git a/src/sage/rings/power_series_poly.pyx b/src/sage/rings/power_series_poly.pyx index 4efdf5f5506..1f28994cdf1 100644 --- a/src/sage/rings/power_series_poly.pyx +++ b/src/sage/rings/power_series_poly.pyx @@ -8,7 +8,6 @@ The class ``PowerSeries_poly`` provides additional methods for univariate power from power_series_ring_element cimport PowerSeries from sage.structure.element cimport Element, ModuleElement, RingElement from infinity import infinity, is_Infinite -import arith from sage.libs.all import PariError from power_series_ring_element import is_PowerSeries import rational_field diff --git a/src/sage/rings/power_series_ring_element.pyx b/src/sage/rings/power_series_ring_element.pyx index a18e4873d7b..e2e07924116 100644 --- a/src/sage/rings/power_series_ring_element.pyx +++ b/src/sage/rings/power_series_ring_element.pyx @@ -102,7 +102,7 @@ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing import sage.rings.polynomial.polynomial_element import power_series_ring import sage.misc.misc -import arith +import sage.arith.all as arith import sage.misc.latex import rational_field, integer_ring from integer import Integer diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 35693e39238..16fe40f13f6 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -500,7 +500,7 @@ from sage.rings.rational_field import QQ from sage.rings.number_field.number_field import NumberField, QuadraticField, CyclotomicField from sage.rings.number_field.number_field_element_quadratic import NumberFieldElement_quadratic -from sage.rings.arith import factor +from sage.arith.all import factor from sage.structure.element import generic_power, canonical_coercion import infinity from sage.misc.functional import cyclotomic_polynomial diff --git a/src/sage/rings/rational.pyx b/src/sage/rings/rational.pyx index b435b757a50..dee4f796a21 100644 --- a/src/sage/rings/rational.pyx +++ b/src/sage/rings/rational.pyx @@ -938,7 +938,7 @@ cdef class Rational(sage.structure.element.FieldElement): seq.append(self) nums = [x.numerator() for x in seq] denoms = [x.denominator() for x in seq] - from sage.rings.arith import gcd, lcm + from sage.arith.all import gcd, lcm return gcd(nums) / lcm(denoms) def valuation(self, p): @@ -1559,7 +1559,7 @@ cdef class Rational(sage.structure.element.FieldElement): if p == 2: return ((m % 8) == 1) - from sage.rings.arith import kronecker_symbol + from sage.arith.all import kronecker_symbol return (kronecker_symbol(m, p) == 1) def val_unit(self, p): @@ -2841,7 +2841,7 @@ cdef class Rational(sage.structure.element.FieldElement): """ if self.is_zero(): raise ArithmeticError, "Support of 0 not defined." - return sage.rings.arith.prime_factors(self) + return sage.arith.all.prime_factors(self) def gamma(self, prec=None): """ diff --git a/src/sage/rings/rational_field.py b/src/sage/rings/rational_field.py index 4a041a848bb..84e2fcbaee4 100644 --- a/src/sage/rings/rational_field.py +++ b/src/sage/rings/rational_field.py @@ -401,35 +401,6 @@ def __iter__(self): sage: [a.height() for a in lst] [1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4] """ - - #The previous version of this function, implemented by Nils - #Bruin, used the sequence defined by $a_0=0$ and - #$a_{n+1}=\frac{1}{2\lfloor a_n\rfloor+1-a_n}$ and generated the - #sequence $$a_0,a_1,-a_1,a_2,-a_2,\ldots$$. This is [A002487] - #in Sloane's encyclopedia, attributed to [Stern]. It is not - #monotone in height, but has other interesting properties - #described in [CalkinWilf]. - #REFERENCES: - # [A002487] Sloane's OLEIS, - # http://oeis.org/classic/A002487 - # [CalkinWilf] N. Calkin and H.S. Wilf, Recounting the - # rationals, American Mathematical Monthly 107 (2000), - # 360--363 - # [Stern] M.A. Stern, Ueber eine zahlentheoretische Funktion, - # Journal fuer die reine und angewandte Mathematik 55 - # (1858), 193--220 - # - # [beginning of Nils' code] - #from sage.rings.arith import integer_floor as floor - # - #n=self(0) - #yield n - #while True: - # n=1/(2*floor(n)+1-n) - # yield n - # yield -n - # [end of Nils' code] - yield self(0) yield self(1) yield self(-1) @@ -529,7 +500,7 @@ def primes_of_bounded_norm_iter(self, B): if B<2: raise StopIteration - from sage.rings.arith import primes + from sage.arith.all import primes for p in primes(B+1): yield p diff --git a/src/sage/rings/real_double.pyx b/src/sage/rings/real_double.pyx index ef854070d04..54eb73ed439 100644 --- a/src/sage/rings/real_double.pyx +++ b/src/sage/rings/real_double.pyx @@ -2533,7 +2533,7 @@ cdef class RealDoubleElement(FieldElement): sage: r.algebraic_dependency(5) x^2 - 2 """ - return sage.rings.arith.algdep(self,n) + return sage.arith.all.algdep(self,n) algdep = algebraic_dependency diff --git a/src/sage/rings/real_mpfi.pyx b/src/sage/rings/real_mpfi.pyx index cdd11c1c7bf..9c87df698a2 100644 --- a/src/sage/rings/real_mpfi.pyx +++ b/src/sage/rings/real_mpfi.pyx @@ -4867,7 +4867,7 @@ cdef class RealIntervalFieldElement(RingElement): known_bits = -self.relative_diameter().log2() - return sage.rings.arith.algdep(self.center(), n, known_bits=known_bits) + return sage.arith.all.algdep(self.center(), n, known_bits=known_bits) def factorial(self): """ diff --git a/src/sage/rings/real_mpfr.pyx b/src/sage/rings/real_mpfr.pyx index a075ec5e440..5443f1d2087 100644 --- a/src/sage/rings/real_mpfr.pyx +++ b/src/sage/rings/real_mpfr.pyx @@ -5058,7 +5058,7 @@ cdef class RealNumber(sage.structure.element.RingElement): sage: r.algebraic_dependency(5) x^2 - 2 """ - return sage.rings.arith.algdep(self,n) + return sage.arith.all.algdep(self,n) algdep = algebraic_dependency diff --git a/src/sage/rings/sum_of_squares.pyx b/src/sage/rings/sum_of_squares.pyx index dc5f2b9cb7f..8e8dd30bc74 100644 --- a/src/sage/rings/sum_of_squares.pyx +++ b/src/sage/rings/sum_of_squares.pyx @@ -137,7 +137,7 @@ def two_squares_pyx(uint32_t n): .. SEEALSO:: - :func:`~sage.rings.arith.two_squares` is much more suited for large inputs + :func:`~sage.arith.all.two_squares` is much more suited for large inputs EXAMPLES:: @@ -275,7 +275,7 @@ def four_squares_pyx(uint32_t n): .. SEEALSO:: - :func:`~sage.rings.arith.four_squares` is much more suited for large input + :func:`~sage.arith.all.four_squares` is much more suited for large input EXAMPLES:: diff --git a/src/sage/sandpiles/sandpile.py b/src/sage/sandpiles/sandpile.py index 5c8157c8364..9dc5665a9b9 100644 --- a/src/sage/sandpiles/sandpile.py +++ b/src/sage/sandpiles/sandpile.py @@ -342,8 +342,8 @@ from sage.misc.superseded import deprecation from sage.modules.free_module_element import vector from sage.plot.colors import rainbow -from sage.rings.arith import falling_factorial -from sage.rings.all import Integer, PolynomialRing, QQ, ZZ, lcm +from sage.arith.all import falling_factorial, lcm +from sage.rings.all import Integer, PolynomialRing, QQ, ZZ from sage.symbolic.all import I, pi # TODO: remove the following line once 4ti2 functions are removed diff --git a/src/sage/schemes/affine/affine_morphism.py b/src/sage/schemes/affine/affine_morphism.py index f142a3b979f..4107fcda2c9 100644 --- a/src/sage/schemes/affine/affine_morphism.py +++ b/src/sage/schemes/affine/affine_morphism.py @@ -16,18 +16,15 @@ for affine/projective """ -# Historical note: in trac #11599, V.B. renamed -# * _point_morphism_class -> _morphism -# * _homset_class -> _point_homset - #***************************************************************************** # Copyright (C) 2011 Volker Braun # Copyright (C) 2006 David Kohel # Copyright (C) 2006 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -37,8 +34,8 @@ from sage.matrix.constructor import matrix, identity_matrix from sage.misc.cachefunc import cached_method from sage.misc.all import prod -from sage.rings.all import Integer, moebius -from sage.rings.arith import lcm, gcd +from sage.rings.all import Integer +from sage.arith.all import lcm, gcd from sage.rings.complex_field import ComplexField from sage.rings.finite_rings.constructor import GF, is_PrimeFiniteField from sage.rings.fraction_field import FractionField @@ -1109,4 +1106,4 @@ def cyclegraph(self): pass from sage.graphs.digraph import DiGraph g=DiGraph(dict(zip(V,E)), loops=True) - return g \ No newline at end of file + return g diff --git a/src/sage/schemes/elliptic_curves/BSD.py b/src/sage/schemes/elliptic_curves/BSD.py index 2d00ce9ce22..fb3b21782a7 100644 --- a/src/sage/schemes/elliptic_curves/BSD.py +++ b/src/sage/schemes/elliptic_curves/BSD.py @@ -7,7 +7,7 @@ #from ell_number_field import EllipticCurve_number_field #import sage.groups.all -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.all as rings from sage.rings.all import ZZ, Infinity from sage.functions.all import ceil diff --git a/src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx b/src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx index b9aa96e0415..76608c01018 100644 --- a/src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx +++ b/src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx @@ -13,7 +13,7 @@ from sage.rings.all import ZZ from sage.rings.polynomial.polynomial_ring import polygen cdef object x_ZZ = polygen(ZZ) from sage.rings.polynomial.real_roots import real_roots -from sage.rings.arith import prime_divisors +from sage.arith.all import prime_divisors from sage.all import ntl include "sage/ext/stdsage.pxi" diff --git a/src/sage/schemes/elliptic_curves/ell_curve_isogeny.py b/src/sage/schemes/elliptic_curves/ell_curve_isogeny.py index ef7610d5542..5dccd9e6bb0 100644 --- a/src/sage/schemes/elliptic_curves/ell_curve_isogeny.py +++ b/src/sage/schemes/elliptic_curves/ell_curve_isogeny.py @@ -2602,7 +2602,7 @@ def __compute_omega_general(self, E, psi, psi_pr, phi, phi_pr): # thesis are wrong, the correct formulas # are coded below - from sage.rings.arith import binomial + from sage.arith.all import binomial for j in xrange(0,n-1): psi_prpr = psi_prpr + \ diff --git a/src/sage/schemes/elliptic_curves/ell_finite_field.py b/src/sage/schemes/elliptic_curves/ell_finite_field.py index 02babb16389..8f0849ac9c4 100644 --- a/src/sage/schemes/elliptic_curves/ell_finite_field.py +++ b/src/sage/schemes/elliptic_curves/ell_finite_field.py @@ -39,7 +39,7 @@ from sage.rings.finite_rings.element_base import is_FiniteFieldElement import sage.groups.generic as generic import ell_point -from sage.rings.arith import gcd, lcm +from sage.arith.all import gcd, lcm, binomial from sage.structure.sequence import Sequence import sage.plot.all as plot @@ -1958,7 +1958,6 @@ def supersingular_j_polynomial(p): J = polygen(GF(p),'j') if p<13: return J.parent().one() - from sage.rings.arith import binomial from sage.misc.all import prod m=(p-1)//2 X,T = PolynomialRing(GF(p),2,names=['X','T']).gens() diff --git a/src/sage/schemes/elliptic_curves/ell_generic.py b/src/sage/schemes/elliptic_curves/ell_generic.py index 2d8d047af99..7e752b218b4 100644 --- a/src/sage/schemes/elliptic_curves/ell_generic.py +++ b/src/sage/schemes/elliptic_curves/ell_generic.py @@ -35,20 +35,16 @@ #***************************************************************************** # Copyright (C) 2005 William Stein -# 2014 Julian Rueth -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +# Copyright (C) 2014 Julian Rueth # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + import math from sage.rings.all import PolynomialRing @@ -58,7 +54,7 @@ import sage.plot.all as plot from sage.plot.plot import generate_plot_points -import sage.rings.arith as arith +from sage.arith.all import lcm import sage.rings.all as rings from sage.rings.number_field.number_field_base import is_NumberField from sage.misc.all import prod as mul @@ -76,10 +72,8 @@ import weierstrass_morphism as wm -factor = arith.factor sqrt = math.sqrt exp = math.exp -next_prime = arith.next_prime oo = rings.infinity # infinity O = rings.O # big oh @@ -603,7 +597,7 @@ def _reduce_point(self, R, p): if R.is_zero(): return R.curve().change_ring(rings.GF(p))(0) x, y = R.xy() - d = arith.LCM(x.denominator(), y.denominator()) + d = lcm(x.denominator(), y.denominator()) return R.curve().change_ring(rings.GF(p))([x*d, y*d, d]) def is_x_coord(self, x): diff --git a/src/sage/schemes/elliptic_curves/ell_modular_symbols.py b/src/sage/schemes/elliptic_curves/ell_modular_symbols.py index 91ddfbbf3f0..a2c85e94832 100644 --- a/src/sage/schemes/elliptic_curves/ell_modular_symbols.py +++ b/src/sage/schemes/elliptic_curves/ell_modular_symbols.py @@ -56,7 +56,7 @@ from sage.libs.eclib.newforms import ECModularSymbol from sage.databases.cremona import parse_cremona_label -from sage.rings.arith import next_prime, kronecker_symbol, prime_divisors, valuation +from sage.arith.all import next_prime, kronecker_symbol, prime_divisors, valuation from sage.rings.infinity import unsigned_infinity as infinity from sage.rings.integer import Integer from sage.modular.cusps import Cusps diff --git a/src/sage/schemes/elliptic_curves/ell_number_field.py b/src/sage/schemes/elliptic_curves/ell_number_field.py index 05b466c9ea3..35d63eaa3cd 100644 --- a/src/sage/schemes/elliptic_curves/ell_number_field.py +++ b/src/sage/schemes/elliptic_curves/ell_number_field.py @@ -76,17 +76,12 @@ #***************************************************************************** # Copyright (C) 2007 Robert Bradshaw -# William Stein -# -# Distributed under the terms of the GNU General Public License (GPL) -# -# This code is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# The full text of the GPL is available at: +# William Stein # +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -94,7 +89,7 @@ import ell_point import sage.matrix.all as matrix from sage.rings.ring import Ring -from sage.rings.arith import gcd, prime_divisors +from sage.arith.all import gcd, prime_divisors, valuation from sage.misc.all import prod import ell_torsion from ell_generic import is_EllipticCurve @@ -105,7 +100,6 @@ import sage.misc.misc from sage.misc.misc import verbose, forall from sage.rings.integer import Integer -from sage.rings.arith import valuation import gal_reps_number_field diff --git a/src/sage/schemes/elliptic_curves/ell_rational_field.py b/src/sage/schemes/elliptic_curves/ell_rational_field.py index 17e6b382804..52efdfc545c 100644 --- a/src/sage/schemes/elliptic_curves/ell_rational_field.py +++ b/src/sage/schemes/elliptic_curves/ell_rational_field.py @@ -75,7 +75,7 @@ import sage.libs.eclib.all as mwrank import sage.databases.cremona -import sage.rings.arith as arith +import sage.arith.all as arith import sage.rings.all as rings from sage.rings.all import ( PowerSeriesRing, @@ -2725,7 +2725,7 @@ def point_search(self, height_limit, verbose=False, rank_bound=None): """ from sage.libs.ratpoints import ratpoints from sage.functions.all import exp - from sage.rings.arith import GCD + from sage.arith.all import GCD H = exp(float(height_limit)) # max(|p|,|q|) <= H, if x = p/q coprime coeffs = [16*self.b6(), 8*self.b4(), self.b2(), 1] points = [] @@ -7020,7 +7020,7 @@ def elliptic_curve_congruence_graph(curves): Graph on 12 vertices """ from sage.graphs.graph import Graph - from sage.rings.arith import lcm, prime_divisors + from sage.arith.all import lcm, prime_divisors from sage.rings.fast_arith import prime_range from sage.misc.all import prod G = Graph() diff --git a/src/sage/schemes/elliptic_curves/ell_tate_curve.py b/src/sage/schemes/elliptic_curves/ell_tate_curve.py index 461db06ca95..487519ae989 100644 --- a/src/sage/schemes/elliptic_curves/ell_tate_curve.py +++ b/src/sage/schemes/elliptic_curves/ell_tate_curve.py @@ -49,7 +49,7 @@ from sage.rings.integer_ring import ZZ from sage.rings.padics.factory import Qp from sage.structure.sage_object import SageObject -from sage.rings.arith import LCM +from sage.arith.all import LCM from sage.modular.modform.constructor import EisensteinForms, CuspForms from sage.schemes.elliptic_curves.constructor import EllipticCurve from sage.misc.functional import log diff --git a/src/sage/schemes/elliptic_curves/gal_reps.py b/src/sage/schemes/elliptic_curves/gal_reps.py index 6f43bb103b2..eadfa0d8d36 100644 --- a/src/sage/schemes/elliptic_curves/gal_reps.py +++ b/src/sage/schemes/elliptic_curves/gal_reps.py @@ -126,7 +126,7 @@ ###################################################################### from sage.structure.sage_object import SageObject -import sage.rings.arith as arith +import sage.arith.all as arith import sage.misc.all as misc import sage.rings.all as rings from sage.rings.all import RealField, GF diff --git a/src/sage/schemes/elliptic_curves/gal_reps_number_field.py b/src/sage/schemes/elliptic_curves/gal_reps_number_field.py index a54f987cc2d..88a1fc56847 100644 --- a/src/sage/schemes/elliptic_curves/gal_reps_number_field.py +++ b/src/sage/schemes/elliptic_curves/gal_reps_number_field.py @@ -58,7 +58,7 @@ from sage.rings.finite_rings.constructor import GF from sage.rings.integer import Integer from sage.misc.functional import cyclotomic_polynomial -from sage.rings.arith import legendre_symbol +from sage.arith.all import legendre_symbol from sage.sets.set import Set class GaloisRepresentation(SageObject): diff --git a/src/sage/schemes/elliptic_curves/heegner.py b/src/sage/schemes/elliptic_curves/heegner.py index 7d22cb7f30a..fd6ab7bd34e 100644 --- a/src/sage/schemes/elliptic_curves/heegner.py +++ b/src/sage/schemes/elliptic_curves/heegner.py @@ -100,11 +100,12 @@ import sage.rings.number_field.number_field_element import sage.rings.number_field.number_field as number_field -import sage.rings.arith as arith import sage.rings.all as rings from sage.rings.all import (ZZ, GF, QQ, CDF, Integers, RealField, ComplexField, QuadraticField, - gcd, lcm, is_fundamental_discriminant) + is_fundamental_discriminant) +from sage.arith.all import (gcd, xgcd, lcm, prime_divisors, factorial, + binomial) from sage.quadratic_forms.all import (BinaryQF, BinaryQF_reduced_representatives) from sage.matrix.all import MatrixSpace, matrix @@ -315,7 +316,7 @@ def ramified_primes(self): sage: E.heegner_point(-7).ring_class_field().ramified_primes() [7] """ - return arith.prime_divisors(self.__D * self.__c) + return prime_divisors(self.__D * self.__c) def _repr_(self): """ @@ -357,8 +358,6 @@ def degree_over_K(self): sage: E.heegner_point(-20,11).ring_class_field().degree_over_K() 24 """ - D, c = self.__D, self.__c - K = self.quadratic_field() # Multiply class number by relative degree of the Hilbert class field H over K. @@ -1002,7 +1001,7 @@ def _alpha_to_p1_element(self, alpha): w = A.coordinate_vector(alpha.vector()) w *= w.denominator() w = w.change_ring(ZZ) - n = arith.gcd(w) + n = gcd(w) w /= n c = P1.N() w = P1.normalize(ZZ(w[0])%c, ZZ(w[1])%c) @@ -2717,7 +2716,7 @@ def atkin_lehner_act(self, Q=None): Q = N if Q == 1: return self # trivial special case - g, u, v = arith.xgcd(Q*Q, -N) + g, u, v = xgcd(Q*Q, -N) if g != Q: raise ValueError("Q must divide N and be coprime to N/Q") tau = self.tau() @@ -3284,7 +3283,6 @@ def x_poly_exact(self, prec=53, algorithm='lll'): sage: P.x_poly_exact(300) x^6 + 1108754853727159228/72351048803252547*x^5 + 88875505551184048168/1953478317687818769*x^4 - 2216200271166098662132/3255797196146364615*x^3 + 14941627504168839449851/9767391588439093845*x^2 - 3456417460183342963918/3255797196146364615*x + 1306572835857500500459/5426328660243941025 """ - D, c = self.discriminant(), self.conductor() n = self.ring_class_field().degree_over_K() if algorithm == 'lll': @@ -3810,7 +3808,7 @@ def _qf_atkin_lehner_act(self, Q, f): True """ N = self.__E.conductor() - g, u, v = arith.xgcd(Q*Q, -N) + g, u, v = xgcd(Q*Q, -N) assert g == Q tau = self._qf_to_tau(f) tau2 = ((u*Q*tau + v) / (N*tau + Q)) @@ -4376,7 +4374,7 @@ def __init__(self, kolyvagin_point, n): if n is None: c = kolyvagin_point.conductor() E = kolyvagin_point.curve() - n = arith.GCD([(p+1).gcd(E.ap(p)) for p in c.prime_divisors()]) + n = gcd([(p+1).gcd(E.ap(p)) for p in c.prime_divisors()]) if not kolyvagin_point.satisfies_kolyvagin_hypothesis(n): raise ValueError("Kolyvagin point does not satisfy Kolyvagin hypothesis for %s"%n) @@ -4604,7 +4602,7 @@ def satisfies_heegner_hypothesis(self, D, c=ZZ(1)): [-8, -39, -43, -51, -79, -95] """ D = ZZ(D); c = ZZ(c) - if arith.gcd(c*D, self.__level*self.__ell) != 1 or arith.gcd(c,D) != 1: + if gcd(c*D, self.__level*self.__ell) != 1 or gcd(c,D) != 1: return False if not satisfies_weak_heegner_hypothesis(self.__level, D): return False @@ -4828,7 +4826,7 @@ def heegner_divisor(self, D, c=ZZ(1)): a = Q.theta_series(n+1)[n] if a > 0: reps = Q.representation_vector_list(n+1)[-1] - k = len([r for r in reps if arith.gcd(r) == 1]) + k = len([r for r in reps if gcd(r) == 1]) assert k%2 == 0 v[i] += k/2 return B(v) @@ -6154,7 +6152,7 @@ def make_monic(f): n = f.degree() f = f / f.leading_coefficient() # find lcm of denominators - d = arith.lcm([b.denominator() for b in f.list() if b]) + d = lcm([b.denominator() for b in f.list() if b]) x = f.variables()[0] g = (d**n) * f(x/d) return g, d @@ -6731,7 +6729,7 @@ def _bound(P): if ind.absolute_diameter() < 1: t, i = ind.is_int() if t: # unique integer in interval, so we've found exact index squared. - return arith.prime_divisors(i), D, i + return prime_divisors(i), D, i raise RuntimeError("Unable to compute bound for e=%s, D=%s (try increasing precision)"%(self, D)) # First try a quick search, in case we get lucky and find @@ -6969,7 +6967,7 @@ def heegner_sha_an(self, D, prec=53): # You can think this through or just type something like # f = function('f',x); g = function('g',x); diff(f*g,6) # into Sage to be convinced. - L = arith.binomial(rE + rF, rE) * (L_E * L_F / (arith.factorial(rE+rF)) ) + L = binomial(rE + rF, rE) * (L_E * L_F / factorial(rE+rF) ) # - ||omega||^2 -- the period. It is twice the volume of the # period lattice. See the following paper for a derivation: diff --git a/src/sage/schemes/elliptic_curves/height.py b/src/sage/schemes/elliptic_curves/height.py index 02da62312eb..32b6072630f 100644 --- a/src/sage/schemes/elliptic_curves/height.py +++ b/src/sage/schemes/elliptic_curves/height.py @@ -50,7 +50,7 @@ infinity, RealField, ComplexField) from sage.misc.all import cached_method, cartesian_product_iterator -from sage.rings.arith import lcm, factor, factorial +from sage.arith.all import lcm, factor, factorial from sage.ext.fast_callable import fast_callable from sage.functions.log import log, exp from sage.symbolic.all import SR diff --git a/src/sage/schemes/elliptic_curves/isogeny_class.py b/src/sage/schemes/elliptic_curves/isogeny_class.py index 7017dc5213b..7428d9d14e0 100644 --- a/src/sage/schemes/elliptic_curves/isogeny_class.py +++ b/src/sage/schemes/elliptic_curves/isogeny_class.py @@ -1186,7 +1186,7 @@ def possible_isogeny_degrees(E, verbose=False): from sage.libs.pari.all import pari from sage.sets.all import Set - from sage.rings.arith import kronecker_symbol + from sage.arith.all import kronecker_symbol n = E.base_field().absolute_degree() if not E.has_rational_cm(): diff --git a/src/sage/schemes/elliptic_curves/mod5family.py b/src/sage/schemes/elliptic_curves/mod5family.py index 71f660bb74e..d3afa3d09af 100644 --- a/src/sage/schemes/elliptic_curves/mod5family.py +++ b/src/sage/schemes/elliptic_curves/mod5family.py @@ -6,7 +6,17 @@ - Alice Silverberg and Karl Rubin (original PARI/GP version) - William Stein -- Sage version. """ -from sage.rings.all import PolynomialRing, QQ, FractionField, lcm + +#***************************************************************************** +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# http://www.gnu.org/licenses/ +#***************************************************************************** + +from sage.rings.all import PolynomialRing, QQ, FractionField +from sage.arith.all import lcm from constructor import EllipticCurve diff --git a/src/sage/schemes/elliptic_curves/padic_lseries.py b/src/sage/schemes/elliptic_curves/padic_lseries.py index 406681d297f..ec26a0b5f7a 100644 --- a/src/sage/schemes/elliptic_curves/padic_lseries.py +++ b/src/sage/schemes/elliptic_curves/padic_lseries.py @@ -87,12 +87,12 @@ from sage.rings.all import LaurentSeriesRing, PowerSeriesRing, PolynomialRing, Integers from sage.rings.integer import Integer -from sage.rings.arith import valuation, binomial, kronecker_symbol, gcd, prime_divisors, valuation +from sage.arith.all import valuation, binomial, kronecker_symbol, gcd, prime_divisors, valuation from sage.structure.sage_object import SageObject from sage.misc.all import verbose, denominator, get_verbose -import sage.rings.arith as arith +import sage.arith.all as arith from sage.modules.free_module_element import vector import sage.matrix.all as matrix diff --git a/src/sage/schemes/elliptic_curves/padics.py b/src/sage/schemes/elliptic_curves/padics.py index 0fe08ab0beb..30a2dd508cc 100644 --- a/src/sage/schemes/elliptic_curves/padics.py +++ b/src/sage/schemes/elliptic_curves/padics.py @@ -24,7 +24,7 @@ import sage.rings.all as rings import padic_lseries as plseries -import sage.rings.arith as arith +import sage.arith.all as arith from sage.rings.all import ( Qp, Zp, Integers, diff --git a/src/sage/schemes/elliptic_curves/sha_tate.py b/src/sage/schemes/elliptic_curves/sha_tate.py index a5c46d09eb2..e06375103c4 100644 --- a/src/sage/schemes/elliptic_curves/sha_tate.py +++ b/src/sage/schemes/elliptic_curves/sha_tate.py @@ -87,7 +87,7 @@ from sage.misc.functional import log from math import sqrt from sage.misc.all import verbose -import sage.rings.arith as arith +import sage.arith.all as arith from sage.rings.padics.factory import Qp factor = arith.factor diff --git a/src/sage/schemes/generic/homset.py b/src/sage/schemes/generic/homset.py index 8adfbe15d0b..e7628c1c640 100644 --- a/src/sage/schemes/generic/homset.py +++ b/src/sage/schemes/generic/homset.py @@ -40,7 +40,8 @@ from sage.categories.homset import HomsetWithBase from sage.structure.factory import UniqueFactory -from sage.rings.all import ( gcd, ZZ, QQ ) +from sage.rings.all import ZZ, QQ +from sage.arith.all import gcd from sage.rings.morphism import is_RingHomomorphism from sage.rings.rational_field import is_RationalField diff --git a/src/sage/schemes/generic/morphism.py b/src/sage/schemes/generic/morphism.py index 73a1fb752d0..cb55100bca8 100644 --- a/src/sage/schemes/generic/morphism.py +++ b/src/sage/schemes/generic/morphism.py @@ -92,7 +92,6 @@ from sage.rings.infinity import infinity import scheme -from sage.rings.arith import gcd, lcm from sage.categories.gcd_domains import GcdDomains from sage.rings.qqbar import QQbar from sage.rings.quotient_ring import QuotientRing_generic diff --git a/src/sage/schemes/generic/scheme.py b/src/sage/schemes/generic/scheme.py index d19033b981d..8889deaa9d9 100644 --- a/src/sage/schemes/generic/scheme.py +++ b/src/sage/schemes/generic/scheme.py @@ -1020,7 +1020,7 @@ def _an_element_(self): Point on Spectrum of Integer Ring defined by the Principal ideal (811) of Integer Ring """ if self.coordinate_ring() is ZZ: - from sage.rings.arith import random_prime + from sage.arith.all import random_prime return self(ZZ.ideal(random_prime(1000))) return self(self.coordinate_ring().zero_ideal()) diff --git a/src/sage/schemes/hyperelliptic_curves/hypellfrob.pyx b/src/sage/schemes/hyperelliptic_curves/hypellfrob.pyx index 2d69038eff3..00153880420 100644 --- a/src/sage/schemes/hyperelliptic_curves/hypellfrob.pyx +++ b/src/sage/schemes/hyperelliptic_curves/hypellfrob.pyx @@ -26,7 +26,7 @@ from sage.libs.ntl.ntl_mat_ZZ cimport ntl_mat_ZZ from sage.libs.ntl.all import ZZ, ZZX from sage.matrix.all import Matrix from sage.rings.all import Qp, O as big_oh -from sage.rings.arith import is_prime +from sage.arith.all import is_prime include "sage/libs/ntl/decl.pxi" include "sage/ext/interrupt.pxi" diff --git a/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py b/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py index 02acc245a61..703ffb8cf5e 100644 --- a/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py +++ b/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py @@ -44,7 +44,7 @@ #***************************************************************************** from sage.rings.all import ZZ, RR, QQ, GF -from sage.rings.arith import binomial +from sage.arith.all import binomial from sage.rings.power_series_ring import PowerSeriesRing import hyperelliptic_generic from sage.schemes.hyperelliptic_curves.hypellfrob import hypellfrob diff --git a/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py b/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py index a58661b9511..b18325f07f9 100644 --- a/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py +++ b/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py @@ -64,7 +64,7 @@ from sage.misc.cachefunc import cached_method from sage.rings.infinity import Infinity -from sage.rings.arith import binomial, integer_ceil as ceil +from sage.arith.all import binomial, integer_ceil as ceil from sage.misc.functional import log from sage.misc.misc import newton_method_sizes diff --git a/src/sage/schemes/plane_conics/con_rational_field.py b/src/sage/schemes/plane_conics/con_rational_field.py index 183634bbe9f..8bf895b2f86 100644 --- a/src/sage/schemes/plane_conics/con_rational_field.py +++ b/src/sage/schemes/plane_conics/con_rational_field.py @@ -39,7 +39,7 @@ from sage.structure.element import is_InfinityElement -from sage.rings.arith import (lcm, hilbert_symbol) +from sage.arith.all import lcm, hilbert_symbol class ProjectiveConic_rational_field(ProjectiveConic_number_field): r""" diff --git a/src/sage/schemes/projective/endPN_automorphism_group.py b/src/sage/schemes/projective/endPN_automorphism_group.py index 1642f0c698a..0bc5850bdca 100644 --- a/src/sage/schemes/projective/endPN_automorphism_group.py +++ b/src/sage/schemes/projective/endPN_automorphism_group.py @@ -28,13 +28,12 @@ from sage.matrix.matrix import is_Matrix from sage.misc.functional import squarefree_part from sage.misc.misc_c import prod -from sage.rings.arith import is_square, divisors from sage.rings.finite_rings.constructor import GF from sage.rings.finite_rings.integer_mod_ring import Integers from sage.rings.fraction_field import FractionField from sage.rings.integer_ring import ZZ from sage.rings.number_field.number_field import NumberField -from sage.rings.arith import gcd, lcm, CRT +from sage.arith.all import gcd, lcm, CRT, is_square, divisors from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.rational_field import QQ from sage.sets.primes import Primes diff --git a/src/sage/schemes/projective/endPN_minimal_model.py b/src/sage/schemes/projective/endPN_minimal_model.py index 8f6e6a0f475..6c007ef7ffa 100644 --- a/src/sage/schemes/projective/endPN_minimal_model.py +++ b/src/sage/schemes/projective/endPN_minimal_model.py @@ -22,21 +22,22 @@ #***************************************************************************** # Copyright (C) 2012 Alexander Molnar # -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** from sage.categories.homset import End from copy import copy from sage.matrix.constructor import matrix -from sage.rings.arith import gcd from sage.rings.finite_rings.integer_mod_ring import Zmod from sage.rings.integer_ring import ZZ from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.rings.rational_field import QQ from sage.schemes.affine.affine_space import AffineSpace +from sage.arith.all import gcd def bCheck(c, v, p, b): diff --git a/src/sage/schemes/projective/projective_morphism.py b/src/sage/schemes/projective/projective_morphism.py index 3da1092f27b..86c7b382f61 100644 --- a/src/sage/schemes/projective/projective_morphism.py +++ b/src/sage/schemes/projective/projective_morphism.py @@ -26,18 +26,15 @@ """ -# Historical note: in trac #11599, V.B. renamed -# * _point_morphism_class -> _morphism -# * _homset_class -> _point_homset - #***************************************************************************** # Copyright (C) 2011 Volker Braun # Copyright (C) 2006 David Kohel # Copyright (C) 2006 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -53,8 +50,8 @@ from sage.misc.misc import subsets from sage.misc.mrange import xmrange from sage.modules.free_module_element import vector -from sage.rings.all import Integer, moebius, CIF -from sage.rings.arith import gcd, lcm, next_prime, binomial, primes +from sage.rings.all import Integer, CIF +from sage.arith.all import gcd, lcm, next_prime, binomial, primes, moebius from sage.rings.complex_field import ComplexField_class,ComplexField from sage.rings.complex_interval_field import ComplexIntervalField_class from sage.rings.finite_rings.constructor import GF, is_PrimeFiniteField diff --git a/src/sage/schemes/projective/projective_morphism_helper.pyx b/src/sage/schemes/projective/projective_morphism_helper.pyx index f355174e479..e0add19c5ce 100644 --- a/src/sage/schemes/projective/projective_morphism_helper.pyx +++ b/src/sage/schemes/projective/projective_morphism_helper.pyx @@ -12,12 +12,14 @@ AUTHORS: #***************************************************************************** # Copyright (C) 2014 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** -from sage.rings.arith import lcm +from sage.arith.all import lcm from sage.rings.finite_rings.constructor import GF from sage.sets.all import Set from sage.misc.misc import subsets diff --git a/src/sage/schemes/projective/projective_point.py b/src/sage/schemes/projective/projective_point.py index 246bb085a06..fe47ee818b6 100644 --- a/src/sage/schemes/projective/projective_point.py +++ b/src/sage/schemes/projective/projective_point.py @@ -20,18 +20,15 @@ for affine/projective, height functionality """ -# Historical note: in trac #11599, V.B. renamed -# * _point_morphism_class -> _morphism -# * _homset_class -> _point_homset - #***************************************************************************** # Copyright (C) 2011 Volker Braun # Copyright (C) 2006 David Kohel # Copyright (C) 2006 William Stein # -# Distributed under the terms of the GNU General Public License (GPL) -# as published by the Free Software Foundation; either version 2 of -# the License, or (at your option) any later version. +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -39,7 +36,6 @@ from sage.categories.number_fields import NumberFields _NumberFields = NumberFields() from sage.rings.infinity import infinity -from sage.rings.arith import gcd, lcm, is_prime, binomial from sage.rings.integer_ring import ZZ from sage.rings.fraction_field import FractionField from sage.rings.morphism import RingHomomorphism_im_gens @@ -51,6 +47,7 @@ from sage.rings.rational_field import QQ from sage.rings.real_double import RDF from sage.rings.real_mpfr import RealField, RR, is_RealField +from sage.arith.all import gcd, lcm, is_prime, binomial from copy import copy from sage.schemes.generic.morphism import (SchemeMorphism, diff --git a/src/sage/schemes/projective/projective_rational_point.py b/src/sage/schemes/projective/projective_rational_point.py index c9e15072c9a..88c092582d5 100644 --- a/src/sage/schemes/projective/projective_rational_point.py +++ b/src/sage/schemes/projective/projective_rational_point.py @@ -51,7 +51,7 @@ #******************************************************************************* -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.rings.all import ZZ from sage.misc.all import srange, cartesian_product_iterator from sage.schemes.generic.scheme import is_Scheme diff --git a/src/sage/schemes/projective/projective_space.py b/src/sage/schemes/projective/projective_space.py index 1fc4b3d2279..61b42ed1120 100644 --- a/src/sage/schemes/projective/projective_space.py +++ b/src/sage/schemes/projective/projective_space.py @@ -96,8 +96,7 @@ from sage.misc.all import (latex, prod) from sage.structure.category_object import normalize_names -from sage.rings.arith import (gcd, - binomial) +from sage.arith.all import gcd, binomial from sage.combinat.integer_vector import IntegerVectors from sage.combinat.tuple import Tuples from sage.matrix.constructor import matrix diff --git a/src/sage/schemes/toric/library.py b/src/sage/schemes/toric/library.py index 5d7cda70562..c1555a2c1b4 100644 --- a/src/sage/schemes/toric/library.py +++ b/src/sage/schemes/toric/library.py @@ -31,8 +31,10 @@ # Copyright (C) 2010 Volker Braun # Copyright (C) 2010 Andrey Novoseltsev # -# Distributed under the terms of the GNU General Public License (GPL) -# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** @@ -40,7 +42,8 @@ from sage.matrix.all import matrix, identity_matrix from sage.geometry.all import Fan, LatticePolytope, ToricLattice -from sage.rings.all import ZZ, QQ, gcd +from sage.rings.all import ZZ, QQ +from sage.arith.all import gcd from sage.schemes.toric.variety import (DEFAULT_PREFIX, ToricVariety, normalize_names) diff --git a/src/sage/schemes/toric/morphism.py b/src/sage/schemes/toric/morphism.py index 1efb3fa4539..a8cba8ae7fe 100644 --- a/src/sage/schemes/toric/morphism.py +++ b/src/sage/schemes/toric/morphism.py @@ -355,23 +355,28 @@ http://arxiv.org/abs/1004.4924 """ - #***************************************************************************** -# Copyright (C) 2011 Volker Braun -# Copyright (C) 2010 Andrey Novoseltsev -# Copyright (C) 2006 William Stein -# Distributed under the terms of the GNU General Public License (GPL) +# Copyright (C) 2011 Volker Braun +# Copyright (C) 2010 Andrey Novoseltsev +# Copyright (C) 2006 William Stein +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** + # For now, the scheme morphism base class cannot derive from Morphism # since this would clash with elliptic curves. So we derive only on # the toric varieties level from Morphism. See # https://groups.google.com/d/msg/sage-devel/qF4yU6Vdmao/wQlNrneSmWAJ from sage.categories.morphism import Morphism -from sage.structure.sequence import Sequence -from sage.rings.all import ZZ, gcd +from sage.structure.sequence import Sequence +from sage.rings.all import ZZ +from sage.arith.all import gcd from sage.misc.all import cached_method from sage.matrix.constructor import matrix, identity_matrix from sage.modules.free_module_element import vector diff --git a/src/sage/schemes/toric/points.py b/src/sage/schemes/toric/points.py index 64a67b23619..0274ed2e84a 100644 --- a/src/sage/schemes/toric/points.py +++ b/src/sage/schemes/toric/points.py @@ -40,7 +40,7 @@ from sage.misc.all import powerset, prod from sage.misc.cachefunc import cached_method -from sage.rings.arith import gcd +from sage.arith.all import gcd from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing from sage.parallel.decorate import parallel diff --git a/src/sage/sets/primes.py b/src/sage/sets/primes.py index 19a78f22cf9..1b3cdd0fd32 100644 --- a/src/sage/sets/primes.py +++ b/src/sage/sets/primes.py @@ -23,7 +23,7 @@ from sage.rings.all import ZZ from set import Set_generic from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets -from sage.rings.arith import nth_prime +from sage.arith.all import nth_prime from sage.structure.unique_representation import UniqueRepresentation diff --git a/src/sage/structure/element.pyx b/src/sage/structure/element.pyx index e7008b2e80a..51b02cc1192 100644 --- a/src/sage/structure/element.pyx +++ b/src/sage/structure/element.pyx @@ -3023,7 +3023,7 @@ cdef class PrincipalIdealDomainElement(DedekindDomainElement): Return the least common multiple of ``self`` and ``right``. """ if not isinstance(right, Element) or not ((right)._parent is self._parent): - from sage.rings.arith import lcm + from sage.arith.all import lcm return coercion_model.bin_op(self, right, lcm) return self._lcm(right) @@ -3541,7 +3541,7 @@ coerce_binop = NamedBinopMethod ############################################################################### from sage.misc.lazy_import import lazy_import -lazy_import('sage.rings.arith', ['gcd', 'xgcd', 'lcm'], deprecation=10779) +lazy_import('sage.arith.all', ['gcd', 'xgcd', 'lcm'], deprecation=10779) ###################### diff --git a/src/sage/symbolic/pynac.pyx b/src/sage/symbolic/pynac.pyx index bf3c7132bdf..f5165bb774a 100644 --- a/src/sage/symbolic/pynac.pyx +++ b/src/sage/symbolic/pynac.pyx @@ -28,7 +28,7 @@ from ginac cimport * from sage.libs.gsl.types cimport * from sage.libs.gsl.complex cimport * from sage.libs.gsl.gamma cimport gsl_sf_lngamma_complex_e - +from sage.arith.all import gcd, lcm, is_prime, factorial, bernoulli from sage.structure.element cimport Element, parent_c from sage.rings.integer_ring import ZZ @@ -842,7 +842,6 @@ def test_binomial(n, k): ################################################################# # GCD ################################################################# -import sage.rings.arith cdef object py_gcd(object n, object k) except +: if isinstance(n, Integer) and isinstance(k, Integer): if mpz_cmp_si((n).value,1) == 0: @@ -854,7 +853,7 @@ cdef object py_gcd(object n, object k) except +: if type(n) is Rational and type(k) is Rational: return n.content(k) try: - return sage.rings.arith.gcd(n,k) + return gcd(n,k) except (TypeError, ValueError, AttributeError): # some strange meaning in case of weird things with no usual lcm. return 1 @@ -871,7 +870,7 @@ cdef object py_lcm(object n, object k) except +: return n return n.lcm(k) try: - return sage.rings.arith.lcm(n,k) + return lcm(n,k) except (TypeError, ValueError, AttributeError): # some strange meaning in case of weird things with no usual lcm, e.g., # elements of finite fields. @@ -1104,14 +1103,13 @@ cdef bint py_is_real(object a) except +: return True return py_imag(a) == 0 -import sage.rings.arith cdef bint py_is_prime(object n) except +: try: return n.is_prime() except Exception: # yes, I'm doing this on purpose. pass try: - return sage.rings.arith.is_prime(n) + return is_prime(n) except Exception: pass return False @@ -1320,7 +1318,6 @@ def py_tgamma_for_doctests(x): """ return py_tgamma(x) -from sage.rings.arith import factorial cdef object py_factorial(object x) except +: """ The factorial function exported to pynac. @@ -1413,7 +1410,6 @@ cdef object py_step(object n) except +: return ONE return ONE_HALF -from sage.rings.arith import bernoulli cdef object py_bernoulli(object x) except +: return bernoulli(x) diff --git a/src/sage/version.py b/src/sage/version.py index 715a9549caa..b9dd9bf2423 100644 --- a/src/sage/version.py +++ b/src/sage/version.py @@ -1,4 +1,4 @@ # Sage version information for Python scripts # This file is auto-generated by the sage-update-version script, do not edit! -version = '7.0.rc1' -date = '2016-01-14' +version = '7.0' +date = '2016-01-19'