Ticket #6040: patch-3__QF_misc_doctests__3.4.1.patch

File patch-3__QF_misc_doctests__3.4.1.patch, 37.9 KB (added by jonhanke, 12 years ago)
  • sage/quadratic_forms/quadratic_form.py

    # HG changeset patch
    # User Jonathan Hanke <jonhanke@gmail.com>
    # Date 1242360366 25200
    # Node ID 12d1fb4068effe1cd9d790a740a6a1841e0a323a
    # Parent  b3d0bbb703abcd636299538541c6706d6fd19b94
    Added Doctests and made minor fixes to some Quafratic Forms routines.
    
    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form.py
    a b  
    112112            rational_diagonal_form, \
    113113            signature_vector, \
    114114            signature, \
    115             local_diagonal, \
    116115            hasse_invariant, \
    117116            hasse_invariant__OMeara, \
    118117            is_hyperbolic, \
     
    120119            is_isotropic, \
    121120            anisotropic_primes, \
    122121            compute_definiteness, \
     122            compute_definiteness_string_by_determinants, \
    123123            is_positive_definite, \
    124124            is_negative_definite, \
    125125            is_indefinite, \
     
    192192    ## Routines to compute p-neighbors
    193193    from sage.quadratic_forms.quadratic_form__neighbors import \
    194194            find_primitive_p_divisible_vector__random, \
    195             find_primitive_p_divisible_vector__all, \
    196195            find_primitive_p_divisible_vector__next, \
    197196            find_p_neighbor_from_vec
    198197
  • sage/quadratic_forms/quadratic_form__evaluate.pyx

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__evaluate.pyx
    a b  
    22
    33def QFEvaluateVector(Q, v):
    44    """
    5     Python wrapper for our fast evaluation routine.
    6     """
    7     return QFEvaluateVector_cdef(Q, v)
    8 
    9 
    10 
    11 cdef QFEvaluateVector_cdef(Q, v):
    12     """
    135    Evaluate this quadratic form Q on a vector or matrix of elements
    146    coercible to the base ring of the quadratic form.  If a vector
    157    is given then the output will be the ring element Q(v), but if a
     
    1810
    1911            Q' = v^t * Q * v.
    2012
     13    Note: This is a Python wrapper for the fast evaluation routine
     14    QFEvaluateVector_cdef().  This routine is for internal use and is
     15    called more conveniently as Q(M).
     16
     17    INPUT:
     18        Q -- QuadraticForm over a base ring R
     19        v -- a tuple or list (or column matrix) of Q.dim() elements of R
     20
     21    OUTPUT:
     22        an element of R
     23
     24    EXAMPLES:
     25        sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateVector
     26        sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
     27        Quadratic form in 4 variables over Integer Ring with coefficients:
     28        [ 0 1 2 3 ]
     29        [ * 4 5 6 ]
     30        [ * * 7 8 ]
     31        [ * * * 9 ]
     32        sage: QFEvaluateVector(Q, (1,0,0,0))
     33        0
     34        sage: QFEvaluateVector(Q, (1,0,1,0))
     35        9
     36
     37    """
     38    return QFEvaluateVector_cdef(Q, v)
     39
     40
     41
     42cdef QFEvaluateVector_cdef(Q, v):
     43    """
     44    Routine to quickly evaluate a quadratic form Q on a vector v.  See
     45    the Python wrapper function QFEvaluate() above for details.
     46
    2147    """
    2248    ## If we are passed a matrix A, return the quadratic form Q(A(x))
    2349    ## (In matrix notation: A^t * Q * A)
     
    3359   
    3460
    3561
    36 def QFEvaluateMatrix(Q, v, Q2):
     62def QFEvaluateMatrix(Q, M, Q2):
    3763    """
    38     Python wrapper for our fast evaluation routine.
     64    Evaluate this quadratic form Q on a matrix M of elements coercible
     65    to the base ring of the quadratic form, which in matrix notation
     66    is given by:
     67
     68            Q2 = M^t * Q * M.
     69
     70    Note: This is a Python wrapper for the fast evaluation routine
     71    QFEvaluateMatrix_cdef().  This routine is for internal use and is
     72    called more conveniently as Q(M).  The inclusion of Q2 as an
     73    argument is to avoid having to create a QuadraticForm here, which
     74    for now creates circular imports.
     75
     76    INPUT:
     77        Q -- QuadraticForm over a base ring R
     78        M -- a Q.dim() x Q2.dim() matrix of elements of R
     79
     80    OUTPUT:
     81        Q2 -- a QuadraticForm over R
     82
     83    EXAMPLES:
     84        sage: from sage.quadratic_forms.quadratic_form__evaluate import QFEvaluateMatrix
     85        sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
     86        Quadratic form in 4 variables over Integer Ring with coefficients:
     87        [ 0 1 2 3 ]
     88        [ * 4 5 6 ]
     89        [ * * 7 8 ]
     90        [ * * * 9 ]
     91        sage: Q2 = QuadraticForm(ZZ, 2)
     92        sage: M = Matrix(ZZ, 4, 2, [1,0,0,0, 0,1,0,0]); M
     93        [1 0]
     94        [0 0]
     95        [0 1]
     96        [0 0]
     97        sage: QFEvaluateMatrix(Q, M, Q2)
     98        Quadratic form in 2 variables over Integer Ring with coefficients:
     99        [ 0 2 ]
     100        [ * 7 ]
     101
    39102    """
    40     return QFEvaluateMatrix_cdef(Q, v, Q2)
     103    return QFEvaluateMatrix_cdef(Q, M, Q2)
    41104
    42105
    43106
    44 cdef QFEvaluateMatrix_cdef(Q, v, Q2):
     107cdef QFEvaluateMatrix_cdef(Q, M, Q2):
    45108    """
    46     Fast evaluation on a matrix.
     109    Routine to quickly evaluate a quadratic form Q on a matrix M.  See
     110    the Python wrapper function QFEvaluateMatrix() above for details.
     111
    47112    """
    48113    ## Create the new quadratic form
    49114    n = Q.dim()
    50115    m = Q2.dim()
     116
     117    ## TO DO: Check the dimensions of M are compatible with those of Q and Q2
     118
     119    ## Evaluate Q(M) into Q2
    51120    for k from 0 <= k < m:
    52121        for l from k <= l < m:
    53122            tmp_sum = Q2.base_ring()(0)
    54123            for i from 0 <= i < n:
    55124                for j from i <= j < n:
    56125                    if (k == l):
    57                         tmp_sum += Q[i,j] * (v[i,k] * v[j,l])
     126                        tmp_sum += Q[i,j] * (M[i,k] * M[j,l])
    58127                    else:
    59                         tmp_sum += Q[i,j] * (v[i,k] * v[j,l] + v[i,l] * v[j,k])
     128                        tmp_sum += Q[i,j] * (M[i,k] * M[j,l] + M[i,l] * M[j,k])
    60129            Q2[k,l] = tmp_sum
    61130    return Q2
  • sage/quadratic_forms/quadratic_form__local_field_invariants.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__local_field_invariants.py
    a b  
    4545    the transformation matrix performing the diagonalization as the
    4646    second argument.
    4747
     48    INPUT:
     49        none
     50
    4851    OUTPUT:
    4952        Q -- the diagonalized form of this quadratic form
    5053        (optional) T -- matrix which diagonalizes Q (over it's fraction field)
     
    108111        ## Create a matrix which deals with off-diagonal entries (all at once for each row)
    109112        temp = MS(1)
    110113        for j in range(i+1, n):
    111             temp[i,j] = -Q[i,j] / (Q[i,i] * 2)
     114            if Q[i,j] != 0:
     115                temp[i,j] = -Q[i,j] / (Q[i,i] * 2)    ## This should only occur when Q[i,i] != 0, which the above step guarantees.
    112116
    113117        Q = Q(temp)
    114118        T = T * temp
     
    136140        None
    137141
    138142    OUTPUT:
    139         a triple of integers
     143        a triple of integers >= 0
    140144
    141145    EXAMPLES:
    142    
     146        sage: Q = DiagonalQuadraticForm(ZZ, [1,0,0,-4])
     147        sage: Q.signature_vector()
     148        (1, 1, 2)
     149
     150        sage: Q = DiagonalQuadraticForm(ZZ, [1,2,-3,-4])
     151        sage: Q.signature_vector()
     152        (2, 2, 0)
     153
     154        sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
     155        Quadratic form in 4 variables over Integer Ring with coefficients:
     156        [ 0 1 2 3 ]
     157        [ * 4 5 6 ]
     158        [ * * 7 8 ]
     159        [ * * * 9 ]
     160        sage: Q.signature_vector()
     161        (3, 1, 0)
     162
    143163    """
    144164    diag = self.rational_diagonal_form()
    145165    p = 0
     
    167187
    168188    of the matrix of the quadratic form.
    169189
     190    INPUT:
     191        None
     192
    170193    OUTPUT:
    171194        an integer
    172195
    173196    EXAMPLES:
    174    
     197        sage: Q = DiagonalQuadraticForm(ZZ, [1,0,0,-4,3,11,3])
     198        sage: Q.signature()
     199        3
     200
     201        sage: Q = DiagonalQuadraticForm(ZZ, [1,2,-3,-4])
     202        sage: Q.signature()
     203        0
     204
     205        sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
     206        Quadratic form in 4 variables over Integer Ring with coefficients:
     207        [ 0 1 2 3 ]
     208        [ * 4 5 6 ]
     209        [ * * 7 8 ]
     210        [ * * * 9 ]
     211        sage: Q.signature()
     212        2
     213
    175214    """
    176215    (p, n, z) = self.signature_vector()
    177216    return p - n
     
    180219
    181220
    182221
    183 def local_diagonal(self, p):
    184     """
    185     Finds a diagonal form equivalent to Q over the p-adic numbers Q_p.
    186 
    187     INPUT:
    188     p -- prime number in ZZ
    189 
    190     OUTPUT:
    191     A quadratic form whose only non-zero entries are on the diagonal.
    192 
    193     """
    194     ## TODO: Check that  is a prime number
    195 
    196     if (p != 2):
    197         return self.local_normal_form(p)  ## This is a diagonal matrix for Q over Z_p. =)
    198     else:
    199         Q2 = self.local_normal_form(p)
    200         for i in range(Q2.dim() - 1):
    201             if (Q2[i, i+1] != 0):
    202                 ## This corresponds to [0 1]  =  [0  1/2]   ===>>>  [1  0]
    203                 ##                     [0 0]     [1/2  0]           [0 -1]
    204                 if (Q2[i,i] == 0):
    205                     Q2[i, i] = Q2[i, i+1]
    206                     Q2[i+1, i+1] = -Q2[i, i+1]
    207                     Q2[i, i+1] = 0
    208 
    209                 ## This corresponds to [1 1]  =  [1  1/2]   ===>>>  [1 0]
    210                 ##                     [0 1]     [1/2  1]           [0 3]
    211                 else:
    212                     Q2[i, i+1] = 0
    213                     Q2[i+1, i+1] = Q2[i+1, i+1] * 3   
    214 
    215     return Q2
    216 
    217 
    218 
    219222def hasse_invariant(self, p):
    220223    """
    221224    Computes the Hasse invariant at a prime p, as given on p55 of
     
    224227
    225228        c_p = \prod_{i < j} (a_i, a_j)_p
    226229
    227     where (a,b)_p is the Hilbert symbol at p.
     230    where (a,b)_p is the Hilbert symbol at p.  The underlying
     231    quadratic form must be non-degenerate over Q_p for this to make
     232    sense.
    228233
    229234    WARNING: This is different from the O'Meara Hasse invariant, which
    230235    allows i <= j in the product.  That is given by the method
     
    232237
    233238    NOTE: We should really rename this hasse_invariant__Cassels(), and
    234239    set hasse_invariant() as a front-end to it.
     240
     241   
     242    INPUT:
     243        p -- a prime number > 0
     244
     245    OUTPUT:
     246        1 or -1
     247       
     248    EXAMPLES:
     249        sage: Q = QuadraticForm(ZZ, 2, [1,2,3])
     250        sage: Q.rational_diagonal_form()
     251        Quadratic form in 2 variables over Rational Field with coefficients:
     252        [ 1 0 ]
     253        [ * 2 ]
     254        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     255        [1, 1, 1, 1, 1, 1, 1, 1]
     256        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     257        [1, 1, 1, 1, 1, 1, 1, 1]
     258
     259        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1])
     260        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     261        [1, 1, 1, 1, 1, 1, 1, 1]
     262        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     263        [1, 1, 1, 1, 1, 1, 1, 1]
     264
     265        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1, -1])
     266        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     267        [-1, 1, 1, 1, 1, 1, 1, 1]
     268        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     269        [1, 1, 1, 1, 1, 1, 1, 1]
    235270    """
    236271    ## TO DO: Need to deal with the case n=1 separately somewhere!
    237272
    238     Diag = self.local_diagonal(p);
     273    Diag = self.rational_diagonal_form()
    239274
    240275    ## DIAGNOSTIC
    241276    #print "\n Q = " + str(self)
     
    263298
    264299    WARNING: This is different from the (Cassels) Hasse invariant, which
    265300    only allows i < j in the product.  That is given by the method
    266     hasse_invariant_OMeara(p)
     301    hasse_invariant(p).
     302
     303
     304    INPUT:
     305        p -- a prime number > 0
     306
     307    OUTPUT:
     308        1 or -1
     309
     310    EXAMPLES:
     311        sage: Q = QuadraticForm(ZZ, 2, [1,2,3])
     312        sage: Q.rational_diagonal_form()
     313        Quadratic form in 2 variables over Rational Field with coefficients:
     314        [ 1 0 ]
     315        [ * 2 ]
     316        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     317        [1, 1, 1, 1, 1, 1, 1, 1]
     318        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     319        [1, 1, 1, 1, 1, 1, 1, 1]
     320
     321        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1])
     322        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     323        [1, 1, 1, 1, 1, 1, 1, 1]
     324        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     325        [1, 1, 1, 1, 1, 1, 1, 1]
     326
     327        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1, -1])
     328        sage: [Q.hasse_invariant(p) for p in prime_range(20)]
     329        [-1, 1, 1, 1, 1, 1, 1, 1]
     330        sage: [Q.hasse_invariant__OMeara(p) for p in prime_range(20)]
     331        [1, 1, 1, 1, 1, 1, 1, 1]
     332
    267333    """
    268334    ## TO DO: Need to deal with the case n=1 separately somewhere!
    269335
    270     Diag = self.local_diagonal(p);
     336    Diag = self.rational_diagonal_form()
    271337
    272338    ## DIAGNOSTIC
    273339    #print "\n Q = " + str(self)
     
    295361            - local invariants for hyperbolic plane (Lemma 2.4, p58)
    296362            - direct sum formulas (Lemma 2.3 on p58)
    297363
     364    INPUT:
     365        p -- a prime number > 0
     366
     367    OUTPUT:
     368        boolean
     369
    298370    EXAMPLES:
    299371        sage: Q = DiagonalQuadraticForm(ZZ, [1,1])
    300372
     
    343415def is_anisotropic(self, p):
    344416    """
    345417    Checks if the quadratic form is anisotropic over the p-adic numbers Q_p.
     418
     419    INPUT:
     420        p -- a prime number > 0
     421
     422    OUTPUT:
     423        boolean
     424
     425    EXAMPLES:
     426        sage: Q = DiagonalQuadraticForm(ZZ, [1,1])
     427        sage: Q.is_anisotropic(2)
     428        True
     429        sage: Q.is_anisotropic(3)
     430        True
     431        sage: Q.is_anisotropic(5)
     432        False
     433
     434        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1])
     435        sage: Q.is_anisotropic(2)
     436        False
     437        sage: Q.is_anisotropic(3)
     438        False
     439        sage: Q.is_anisotropic(5)
     440        False
     441
     442        sage: [DiagonalQuadraticForm(ZZ, [1, -quadratic_nonresidue(p)]).is_anisotropic(p)  for p in prime_range(3, 30)]
     443        [True, True, True, True, True, True, True, True, True]
     444
     445        sage: [DiagonalQuadraticForm(ZZ, [1, -quadratic_nonresidue(p), p, -p*quadratic_nonresidue(p)]).is_anisotropic(p)  for p in prime_range(3, 30)]
     446        [True, True, True, True, True, True, True, True, True]
     447
     448
    346449    """
    347450    n = self.dim()
    348451    D = self.det()
     
    370473def is_isotropic(self, p):
    371474    """
    372475    Checks if Q is isotropic over the p-adic numbers Q_p.
     476
     477    INPUT:
     478        p -- a prime number > 0
     479
     480    OUTPUT:
     481        boolean
     482
     483    EXAMPLES:
     484        sage: Q = DiagonalQuadraticForm(ZZ, [1,1])
     485        sage: Q.is_isotropic(2)
     486        False
     487        sage: Q.is_isotropic(3)
     488        False
     489        sage: Q.is_isotropic(5)
     490        True
     491
     492        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1])
     493        sage: Q.is_isotropic(2)
     494        True
     495        sage: Q.is_isotropic(3)
     496        True
     497        sage: Q.is_isotropic(5)
     498        True
     499
     500        sage: [DiagonalQuadraticForm(ZZ, [1, -quadratic_nonresidue(p)]).is_isotropic(p)  for p in prime_range(3, 30)]
     501        [False, False, False, False, False, False, False, False, False]
     502
     503        sage: [DiagonalQuadraticForm(ZZ, [1, -quadratic_nonresidue(p), p, -p*quadratic_nonresidue(p)]).is_isotropic(p)  for p in prime_range(3, 30)]
     504        [False, False, False, False, False, False, False, False, False]
     505
    373506    """
    374507    return not self.is_anisotropic(p)
    375508
     
    416549    return AnisoPrimes
    417550
    418551
     552
     553
    419554def compute_definiteness(self):
    420555    """
    421556    Computes whether the given quadratic form is positive-definite,
     
    438573
    439574    EXAMPLES:
    440575        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1,1])
     576        sage: Q.compute_definiteness()
    441577        sage: Q.is_positive_definite()
    442578        True
    443579        sage: Q.is_negative_definite()
     
    448584        True
    449585
    450586        sage: Q = DiagonalQuadraticForm(ZZ, [])
     587        sage: Q.compute_definiteness()
    451588        sage: Q.is_positive_definite()
    452589        True
    453590        sage: Q.is_negative_definite()
     
    458595        True
    459596
    460597        sage: Q = DiagonalQuadraticForm(ZZ, [1,0,-1])
     598        sage: Q.compute_definiteness()
    461599        sage: Q.is_positive_definite()
    462600        False
    463601        sage: Q.is_negative_definite()
     
    482620        self.__definiteness_string = "zero"
    483621        return
    484622
     623
     624    sig_pos, sig_neg, sig_zer = self.signature_vector()
     625
     626    ## Determine and cache the definiteness string
     627    if sig_zer > 0:
     628        self.__definiteness_string = "degenerate"
     629        return
     630    elif sig_neg == n:
     631        self.__definiteness_string = "neg_def"
     632        return
     633    elif sig_pos == n:
     634        self.__definiteness_string = "pos_def"
     635        return
     636    else:
     637        self.__definiteness_string = "indefinite"
     638        return
     639
     640
     641
     642def compute_definiteness_string_by_determinants(self):
     643    """
     644    Compute the (positive) definiteness of a quadratic form by looking
     645    at the signs of all of its upper-left subdeterminants.  See also
     646    self.compute_definiteness() for more documentation.
     647
     648    INPUT:
     649        None
     650
     651    OUTPUT:
     652        string describing the definiteness
     653
     654    EXAMPLES:
     655        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1,1])
     656        sage: Q.compute_definiteness_string_by_determinants()
     657        'pos_def'
     658
     659        sage: Q = DiagonalQuadraticForm(ZZ, [])
     660        sage: Q.compute_definiteness_string_by_determinants()
     661        'zero'
     662
     663        sage: Q = DiagonalQuadraticForm(ZZ, [1,0,-1])
     664        sage: Q.compute_definiteness_string_by_determinants()
     665        'degenerate'
     666
     667        sage: Q = DiagonalQuadraticForm(ZZ, [1,-1])
     668        sage: Q.compute_definiteness_string_by_determinants()
     669        'indefinite'
     670
     671        sage: Q = DiagonalQuadraticForm(ZZ, [-1,-1])
     672        sage: Q.compute_definiteness_string_by_determinants()
     673        'neg_def'
     674
     675    """
     676    ## Sanity Check
     677    if not ((self.base_ring() == ZZ) or (self.base_ring() == QQ) or (self.base_ring() == RR)):
     678        raise NotImplementedError, "Oops!  We can only check definiteness over ZZ, QQ, and RR for now."
     679   
     680    ## Some useful variables
     681    n = self.dim()
     682    M = self.matrix()
     683
     684
     685    ## Deal with the zero-diml form
     686    if n == 0:
     687        return "zero"
     688
     689
     690
    485691    ## Deal with degenerate forms
    486692    if self.det() == 0:
    487         self.__definiteness_string = "degenerate"
    488         return
     693        return "degenerate"
    489694   
    490695
    491696    ## Check the sign of the ratios of consecutive determinants of the upper triangular r x r submatrices
     
    496701
    497702        ## Check for a (non-degenerate) zero -- so it's indefinite
    498703        if new_det == 0:       
    499             self.__definiteness_string = "indefinite"
    500             return
     704            return "indefinite"
     705           
    501706
    502707        ## Check for a change of signs in the upper r x r submatrix -- so it's indefinite
    503708        if sgn(first_coeff)**r != sgn(new_det):
    504             self.__definiteness_string = "indefinite"
    505             return
     709            return "indefinite"
     710           
    506711
    507712    ## Here all ratios of determinants have the correct sign, so the matrix is (pos or neg) definite.
    508713    if first_coeff > 0:
    509         self.__definiteness_string = "pos_def"
     714        return "pos_def"
    510715    else:
    511         self.__definiteness_string = "neg_def"
     716        return "neg_def"
    512717       
    513718
    514719
    515720
     721
    516722def is_positive_definite(self):
    517723    """
    518724    Determines if the given quadratic form is positive-definite.
     
    633839
    634840    EXAMPLES:
    635841        sage: Q = DiagonalQuadraticForm(ZZ, [-1,-3,-5])
    636         sage: Q.is_indefinite()
    637         False
     842        sage: Q.is_definite()
     843        True
    638844
    639845        sage: Q = DiagonalQuadraticForm(ZZ, [1,-3,5])
    640         sage: Q.is_indefinite()
    641         True
     846        sage: Q.is_definite()
     847        False
    642848   
    643849    """
    644850    ## Try to use the cached value
  • sage/quadratic_forms/quadratic_form__local_normal_form.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__local_normal_form.py
    a b  
    3333
    3434    Note: This answer is independent of the kind of matrix (Gram or
    3535    Hessian) associated to the form.
     36
     37    INPUT:
     38        p -- a prime number > 0
     39
     40    OUTPUT:
     41        a pair of integers >= 0
     42
     43    EXAMPLES:
     44        sage: Q = QuadraticForm(ZZ, 2, [6, 2, 20]); Q
     45        Quadratic form in 2 variables over Integer Ring with coefficients:
     46        [ 6 2 ]
     47        [ * 20 ]
     48        sage: Q.find_entry_with_minimal_scale_at_prime(2)
     49        (0, 1)
     50        sage: Q.find_entry_with_minimal_scale_at_prime(3)
     51        (1, 1)
     52        sage: Q.find_entry_with_minimal_scale_at_prime(5)
     53        (0, 0)
     54
    3655    """
    3756    n = self.dim()
    3857    min_val = Infinity
     
    6988
    7089    INPUT:
    7190        p -- a positive prime number.
     91
     92    OUTPUT:
     93        a quadratic form over ZZ
    7294   
    7395    WARNING:  Currently this only works for quadratic forms defined over ZZ.
    7496   
     
    262284    False, then the routine is much faster but the return values are
    263285    vulnerable to being corrupted by the user.
    264286
    265 
    266287    INPUT:
    267288        p -- a prime number > 0.
    268289
  • sage/quadratic_forms/quadratic_form__local_representation_conditions.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__local_representation_conditions.py
    a b  
    11
    2 #########################################################################
    3 ## Class for keping track of the local conditions for representability ##
    4 ## of numbers by a quadratic form over ZZ (and eventually QQ also).    ##
    5 #########################################################################
     2##########################################################################
     3## Class for keeping track of the local conditions for representability ##
     4## of numbers by a quadratic form over ZZ (and eventually QQ also).     ##
     5##########################################################################
    66
    77from copy import deepcopy
    88
     
    1313from sage.misc.functional import numerator, denominator
    1414from sage.rings.rational_field import QQ
    1515
     16
     17
    1618class QuadraticFormLocalRepresentationConditions():
    1719    """
    1820    Creates a class for dealing with the local conditions of a
     
    8890        previously computed conditions if they exist, and stores the
    8991        new conditions.
    9092
     93        INPUT:
     94            Q -- Quadratic form over ZZ
     95       
     96        OUTPUT:
     97            a  QuadraticFormLocalRepresentationConditions object
     98
    9199        EXAMPLES:
    92100            sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1])
    93101            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
     
    189197        """
    190198        Print the local conditions.
    191199
     200        INPUT:
     201            none
     202
     203        OUTPUT:
     204            string
     205
    192206        TO DO:  Improve the output for the real numbers, and special output for locally unversality.
    193207        Also give names to the squareclasses, so it's clear what the output means! =)
    194208
     
    227241    def __eq__(self, right):
    228242        """
    229243        Determines if two sets of local conditions are equal.
     244
     245        INPUT:
     246            right -- a QuadraticFormLocalRepresentationConditions object
     247
     248        OUTPUT:
     249            boolean
    230250   
    231251        EXAMPLES:
    232252             sage: Q1 = DiagonalQuadraticForm(ZZ, [1,1])
     
    295315        """
    296316        Returns a local representation vector for the (possibly infinite) prime p.
    297317
     318        INPUT:
     319            p -- a positive prime number.  (Is 'infinity' allowed here?)
     320
     321        OUTPUT:
     322            a list of integers
     323
    298324        EXAMPLES:
    299325            sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
    300326            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
     
    340366        raise RuntimeError, "Error... The dimension stored should be a non-negative integer!"
    341367
    342368
     369
    343370    def is_universal_at_prime(self, p):
    344371        """
    345372        Determines if the (integer-valued/rational) quadratic form represents all of Z_p.
    346373
     374        INPUT:
     375            p -- a positive prime number or "infinity".
     376
     377        OUTPUT:
     378            boolean
     379
    347380        EXAMPLES:
    348381            sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
    349382            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
     
    383416        """
    384417        Determines if the quadratic form represents Z_p for all finite/non-archimedian primes.
    385418
     419        INPUT:
     420            none
     421
     422        OUTPUT:
     423            boolean
     424
    386425        EXAMPLES:
    387426            sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
    388427            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
     
    411450        """
    412451        Determines if the quadratic form represents Z_p for all
    413452        finite/non-archimedian primes, and represents all real numbers.
     453
     454        INPUT:
     455            none
     456
     457        OUTPUT:
     458            boolean
     459
    414460        EXAMPLES:
    415461            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
    416462
     
    446492        Determines if the rational number m is locally represented by the
    447493        quadratic form at the (possibly infinite) prime p.
    448494
     495        INPUT:
     496            m -- an integer
     497            p -- a positive prime number or "infinity".
     498
     499        OUTPUT:
     500            boolean
     501
    449502        EXAMPLES:
    450503            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
    451504
     
    506559
    507560    def is_locally_represented(self, m):
    508561        """
    509         Determines if the rational number m is locally represented by the quadratic form (allowing vectors with coeffs in Z_p at all places).
     562        Determines if the rational number m is locally represented by
     563        the quadratic form (allowing vectors with coeffs in Z_p at all
     564        places).
     565
     566        INPUT:
     567            m -- an integer
     568
     569        OUTPUT:
     570            boolean
    510571
    511572        EXAMPLES:
    512573            sage: from sage.quadratic_forms.quadratic_form__local_representation_conditions import QuadraticFormLocalRepresentationConditions
     
    604665    positive reals are represented).  The real vector always appears,
    605666    and is listed before the other ones.
    606667
     668    INPUT:
     669        none
     670
    607671    OUTPUT:
    608672        A list of 9-element vectors describing the representation
    609673        obstructions at primes dividing the level.
     
    673737def is_locally_universal_at_prime(self, p):
    674738    """
    675739    Determines if the (integer-valued/rational) quadratic form represents all of Z_p.
     740
     741    INPUT:
     742        p -- a positive prime number or "infinity".
     743
     744    OUTPUT:
     745        boolean
     746
     747    EXAMPLES:
     748        sage: Q = DiagonalQuadraticForm(ZZ, [1,3,5,7])
     749        sage: Q.is_locally_universal_at_prime(2)
     750        True
     751        sage: Q.is_locally_universal_at_prime(3)
     752        True
     753        sage: Q.is_locally_universal_at_prime(5)
     754        True
     755        sage: Q.is_locally_universal_at_prime(infinity)
     756        False
     757
     758        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
     759        sage: Q.is_locally_universal_at_prime(2)
     760        False
     761        sage: Q.is_locally_universal_at_prime(3)
     762        True
     763        sage: Q.is_locally_universal_at_prime(5)
     764        True
     765        sage: Q.is_locally_universal_at_prime(infinity)
     766        False
     767
     768        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,-1])
     769        sage: Q.is_locally_universal_at_prime(infinity)
     770        True
     771
    676772    """
    677773    self.local_representation_conditions(silent_flag=True)
    678774    return self.__local_representability_conditions.is_universal_at_prime(p)
     
    682778def is_locally_universal_at_all_primes(self):
    683779    """
    684780    Determines if the quadratic form represents Z_p for all finite/non-archimedian primes.
     781
     782    INPUT:
     783        none
     784
     785    OUTPUT:
     786        boolean
     787
     788    EXAMPLES:
     789        sage: Q = DiagonalQuadraticForm(ZZ, [1,3,5,7])
     790        sage: Q.is_locally_universal_at_all_primes()
     791        True
     792
     793        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1])
     794        sage: Q.is_locally_universal_at_all_primes()
     795        True
     796
     797        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
     798        sage: Q.is_locally_universal_at_all_primes()
     799        False
     800
    685801    """   
    686802    self.local_representation_conditions(silent_flag=True)
    687803    return self.__local_representability_conditions.is_universal_at_all_finite_primes()
     
    692808    """
    693809    Determines if the quadratic form represents Z_p for all
    694810    finite/non-archimedian primes, and represents all real numbers.
     811
     812    INPUT:
     813        none
     814
     815    OUTPUT:
     816        boolean
     817
     818    EXAMPLES:
     819        sage: Q = DiagonalQuadraticForm(ZZ, [1,3,5,7])
     820        sage: Q.is_locally_universal_at_all_places()
     821        False
     822
     823        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1])
     824        sage: Q.is_locally_universal_at_all_places()
     825        False
     826
     827        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1,-1])
     828        sage: Q.is_locally_universal_at_all_places()
     829        True
     830
    695831    """
    696832    self.local_representation_conditions(silent_flag=True)
    697833    return self.__local_representability_conditions.is_universal_at_all_places()
     
    702838    """
    703839    Determines if the rational number m is locally represented by the
    704840    quadratic form at the (possibly infinite) prime p.
     841
     842    INPUT:
     843        m -- an integer
     844        p -- a prime number > 0 or 'infinity'
     845
     846    OUTPUT:
     847        boolean
     848
     849    EXAMPLES:
     850        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
     851        sage: Q.is_locally_represented_number_at_place(7, infinity)
     852        True
     853        sage: Q.is_locally_represented_number_at_place(7, 2)
     854        False
     855        sage: Q.is_locally_represented_number_at_place(7, 3)
     856        True
     857        sage: Q.is_locally_represented_number_at_place(7, 5)
     858        True
     859
     860        sage: Q.is_locally_represented_number_at_place(7, infinity)
     861        True
     862        sage: Q.is_locally_represented_number_at_place(7, 2)
     863        True
     864        sage: Q.is_locally_represented_number_at_place(7, 3)
     865        True
     866        sage: Q.is_locally_represented_number_at_place(7, 5)
     867        True
     868
     869
     870        sage: Q.is_locally_represented_number_at_place(-1, infinity)
     871        False
     872        sage: Q.is_locally_represented_number_at_place(-1, 2)
     873        False
     874        sage: Q.is_locally_represented_number_at_place(7, 3)
     875        True
     876        sage: Q.is_locally_represented_number_at_place(7, 5)
     877        True
     878
    705879    """
    706880    self.local_representation_conditions(silent_flag=True)
    707881    return self.__local_representability_conditions.is_locally_represented_at_place(m, p)
     
    711885def is_locally_represented_number(self, m):
    712886    """
    713887    Determines if the rational number m is locally represented by the quadratic form.
     888
     889    INPUT:
     890        m -- an integer
     891
     892    OUTPUT:
     893        boolean
     894
     895    EXAMPLES:
     896        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1,1])
     897        sage: Q.is_locally_represented_number(2)
     898        True
     899        sage: Q.is_locally_represented_number(7)
     900        False
     901        sage: Q.is_locally_represented_number(-1)
     902        False
     903        sage: Q.is_locally_represented_number(28)
     904        False
     905        sage: Q.is_locally_represented_number(0)
     906        True
     907       
    714908    """
    715909    self.local_representation_conditions(silent_flag=True)
    716910    return self.__local_representability_conditions.is_locally_represented(m)
  • sage/quadratic_forms/quadratic_form__neighbors.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__neighbors.py
    a b  
    5555
    5656
    5757
    58 def find_primitive_p_divisible_vector__all(self, p):
    59     """
    60     Finds all random p-primitive vectors (up to scaling) in L/pL whose
    61     value is p-divisible.
    62 
    63     Note: Since there are about p^(n-2) of these lines, we should avoid this for large n.
    64     """
    65     pass
     58#def find_primitive_p_divisible_vector__all(self, p):
     59#    """
     60#    Finds all random p-primitive vectors (up to scaling) in L/pL whose
     61#    value is p-divisible.
     62#
     63#    Note: Since there are about p^(n-2) of these lines, we should avoid this for large n.
     64#    """
     65#    pass
    6666
    6767
    6868def find_primitive_p_divisible_vector__next(self, p, v=None):
  • sage/quadratic_forms/quadratic_form__split_local_covering.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__split_local_covering.py
    a b  
    5353
    5454
    5555    EXAMPLES:
     56        sage: Q = DiagonalQuadraticForm(ZZ, [1,1,1])
     57        sage: Q.cholesky_decomposition()
     58        [ 1.00000000000000 0.000000000000000 0.000000000000000]
     59        [0.000000000000000  1.00000000000000 0.000000000000000]
     60        [0.000000000000000 0.000000000000000  1.00000000000000]
    5661
     62        sage: Q = QuadraticForm(QQ, 3, range(1,7)); Q
     63        Quadratic form in 3 variables over Rational Field with coefficients:
     64        [ 1 2 3 ]
     65        [ * 4 5 ]
     66        [ * * 6 ]
     67        sage: Q.cholesky_decomposition()
     68        [ 1.00000000000000  1.00000000000000  1.50000000000000]
     69        [0.000000000000000  3.00000000000000 0.333333333333333]
     70        [0.000000000000000 0.000000000000000  3.41666666666667]
    5771
    5872    """
    5973
     
    8397
    8498    ## 4. Zero out the strictly lower-triangular entries
    8599    for i in range(n):
    86         for j in range(i-1):
     100        for j in range(i):
    87101            Q[i,j] = 0
    88102
    89103    return Q
     
    97111    This is a maive algorithm which uses the Cholesky decomposition,
    98112    but does not use the LLL-reduction algorithm.
    99113
     114    INPUT:
     115       bound -- an integer >= 0
     116
    100117    OUTPUT:
    101118        A list L of length (bound + 1) whose entry L[i] is a list of
    102119        all vectors of length i.
     
    250267    lattice (as opposed to a sublattice in the C++ code) since we
    251268    now extend v to a unimodular matrix.
    252269
     270    INPUT:
     271        v -- a list of self.dim() integers
     272
     273    OUTPUT:
     274        a QuadraticForm over ZZ
     275
     276
    253277    EXAMPLES:
    254278    sage: Q1 = DiagonalQuadraticForm(ZZ, [1,3,5,7])
    255279    sage: Q1.complementary_subform_to_vector([1,0,0,0])
     
    341365    comparing the local conditions of its sum with it's complementary
    342366    lattice and the original quadratic form Q.
    343367
     368    INPUT:
     369        none
     370
     371    OUTPUT:
     372        a QuadraticForm over ZZ
     373
    344374    EXAMPLES:
    345375        sage: Q1 = DiagonalQuadraticForm(ZZ, [7,5,3])
    346376        sage: Q1.split_local_cover()
  • sage/quadratic_forms/quadratic_form__variable_substitutions.py

    diff -r b3d0bbb703ab -r 12d1fb4068ef sage/quadratic_forms/quadratic_form__variable_substitutions.py
    a b  
    2121    Switch the variables x_r and x_s in the quadratic form
    2222    (replacing the original form if the in_place flag is True).
    2323
     24    INPUT:
     25        r, s -- integers >= 0
     26
     27    OUTPUT:
     28        a QuadraticForm (by default, otherwise none)
     29
    2430    EXAMPLES:
    2531        sage: Q = QuadraticForm(ZZ, 4, range(1,11))
    2632        sage: Q
     
    7379    Here c must be an element of the base_ring defining the
    7480    quadratic form.
    7581
     82    INPUT:
     83        c -- an element of Q.base_ring()
     84        i -- an integer >= 0
     85
     86    OUTPUT:
     87        a QuadraticForm (by default, otherwise none)
     88
    7689    EXAMPLES:
    7790        sage: Q = DiagonalQuadraticForm(ZZ, [1,9,5,7])
    7891        sage: Q.multiply_variable(5,0)
     
    110123    quadratic form, and the division must be definied in the base
    111124    ring.
    112125
     126    INPUT:
     127        c -- an element of Q.base_ring()
     128        i -- an integer >= 0
     129
     130    OUTPUT:
     131        a QuadraticForm (by default, otherwise none)
     132
    113133    EXAMPLES:
    114134        sage: Q = DiagonalQuadraticForm(ZZ, [1,9,5,7])
    115135        sage: Q.divide_variable(3,1)
     
    196216    """
    197217    Extract the variables (in order) whose indices are listed in
    198218    var_indices, to give a new quadratic form.
     219
     220    INPUT:
     221        var_indices -- a list of integers >= 0
     222
     223    OUTPUT:
     224        a QuadraticForm
     225
     226    EXAMPLES:
     227        sage: Q = QuadraticForm(ZZ, 4, range(10)); Q
     228        Quadratic form in 4 variables over Integer Ring with coefficients:
     229        [ 0 1 2 3 ]
     230        [ * 4 5 6 ]
     231        [ * * 7 8 ]
     232        [ * * * 9 ]
     233        sage: Q.extract_variables([1,3])
     234        Quadratic form in 2 variables over Integer Ring with coefficients:
     235        [ 4 6 ]
     236        [ * 9 ]
     237
    199238    """
    200239    m = len(var_indices)
    201240    Q = copy.deepcopy(self)
     
    213252    Perform the substitution x_i --> x_i + c*x_j (replacing the
    214253    original form if the in_place flag is True).
    215254
     255    INPUT:
     256        c -- an element of Q.base_ring()
     257        i,j -- integers >= 0
     258
     259    OUTPUT:
     260        a QuadraticForm (by default, otherwise none)
     261
    216262    EXAMPLES:
    217263        sage: Q = QuadraticForm(ZZ, 4, range(1,11))
    218264        sage: Q
     
    284330    NOTE: This is meant for compatibility with previous code,
    285331    which implemented a matrix model for this class.  It is used
    286332    in the local_normal_form() method.
     333
     334
     335    INPUT:
     336        c -- an element of Q.base_ring()
     337        i,j -- integers >= 0
     338
     339    OUTPUT:
     340        a QuadraticForm (by default, otherwise none)
     341
     342    EXAMPLES:
     343        sage: Q = QuadraticForm(ZZ, 3, range(1,7)); Q
     344        Quadratic form in 3 variables over Integer Ring with coefficients:
     345        [ 1 2 3 ]
     346        [ * 4 5 ]
     347        [ * * 6 ]
     348        sage: Q.add_symmetric(-1, 1, 0)
     349        Quadratic form in 3 variables over Integer Ring with coefficients:
     350        [ 1 0 3 ]
     351        [ * 3 2 ]
     352        [ * * 6 ]
     353        sage: Q.add_symmetric(-3/2, 2, 0)     ## ERROR: -3/2 isn't in the base ring ZZ
     354        Traceback (most recent call last):
     355        ...
     356        RuntimeError: Oops!  This coefficient can't be coerced to an element of the base ring for the quadratic form.
     357
     358        sage: Q = QuadraticForm(QQ, 3, range(1,7)); Q
     359        Quadratic form in 3 variables over Rational Field with coefficients:
     360        [ 1 2 3 ]
     361        [ * 4 5 ]
     362        [ * * 6 ]
     363        sage: Q.add_symmetric(-3/2, 2, 0)
     364        Quadratic form in 3 variables over Rational Field with coefficients:
     365        [ 1 2 0 ]
     366        [ * 4 2 ]
     367        [ * * 15/4 ]
     368       
    287369    """
    288370    return self.elementary_substitution(c, j, i, in_place)
    289371