Extra functions for quadratic forms#

sage.quadratic_forms.extras.extend_to_primitive(A_input)[source]#

Given a matrix (resp. list of vectors), extend it to a square matrix (resp. list of vectors), such that its determinant is the gcd of its minors (i.e. extend the basis of a lattice to a “maximal” one in \(\ZZ^n\)).

Author(s): Gonzalo Tornaria and Jonathan Hanke.

INPUT:

a matrix, or a list of length n vectors (in the same space)

OUTPUT:

a square matrix, or a list of n vectors (resp.)

EXAMPLES:

sage: A = Matrix(ZZ, 3, 2, range(6))
sage: extend_to_primitive(A)
[ 0  1 -1]
[ 2  3  0]
[ 4  5  0]

sage: extend_to_primitive([vector([1,2,3])])
[(1, 2, 3), (0, 1, 1), (-1, 0, 0)]
>>> from sage.all import *
>>> A = Matrix(ZZ, Integer(3), Integer(2), range(Integer(6)))
>>> extend_to_primitive(A)
[ 0  1 -1]
[ 2  3  0]
[ 4  5  0]

>>> extend_to_primitive([vector([Integer(1),Integer(2),Integer(3)])])
[(1, 2, 3), (0, 1, 1), (-1, 0, 0)]
sage.quadratic_forms.extras.is_triangular_number(n, return_value=False)[source]#

Return whether n is a triangular number.

A triangular number is a number of the form \(k(k+1)/2\) for some non-negative integer \(n\). See Wikipedia article Triangular_number. The sequence of triangular number is references as A000217 in the Online encyclopedia of integer sequences (OEIS).

If you want to get the value of \(k\) for which \(n=k(k+1)/2\) set the argument return_value to True (see the examples below).

INPUT:

  • n – an integer

  • return_value – a boolean set to False by default. If set to True the function returns a pair made of a boolean and the value \(v\) such that \(v(v+1)/2 = n\).

EXAMPLES:

sage: is_triangular_number(3)
True
sage: is_triangular_number(3, return_value=True)
(True, 2)

sage: is_triangular_number(2)
False
sage: is_triangular_number(2, return_value=True)
(False, None)

sage: is_triangular_number(25*(25+1)/2)
True

sage: is_triangular_number(10^6 * (10^6 +1)/2, return_value=True)
(True, 1000000)
>>> from sage.all import *
>>> is_triangular_number(Integer(3))
True
>>> is_triangular_number(Integer(3), return_value=True)
(True, 2)

>>> is_triangular_number(Integer(2))
False
>>> is_triangular_number(Integer(2), return_value=True)
(False, None)

>>> is_triangular_number(Integer(25)*(Integer(25)+Integer(1))/Integer(2))
True

>>> is_triangular_number(Integer(10)**Integer(6) * (Integer(10)**Integer(6) +Integer(1))/Integer(2), return_value=True)
(True, 1000000)
sage.quadratic_forms.extras.least_quadratic_nonresidue(p)[source]#

Return the smallest positive integer quadratic non-residue in \(\ZZ/p\ZZ\) for primes \(p>2\).

EXAMPLES:

sage: least_quadratic_nonresidue(5)
2
sage: [least_quadratic_nonresidue(p) for p in prime_range(3, 100)]              # needs sage.libs.pari
[2, 2, 3, 2, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 2, 2, 2, 7, 5, 3, 2, 3, 5]
>>> from sage.all import *
>>> least_quadratic_nonresidue(Integer(5))
2
>>> [least_quadratic_nonresidue(p) for p in prime_range(Integer(3), Integer(100))]              # needs sage.libs.pari
[2, 2, 3, 2, 2, 3, 2, 5, 2, 3, 2, 3, 2, 5, 2, 2, 2, 2, 7, 5, 3, 2, 3, 5]