Ticket #11841: trac_11841_sub_lattice.patch

File trac_11841_sub_lattice.patch, 3.4 KB (added by chapoton, 9 years ago)
  • sage/quadratic_forms/quadratic_form.py

    # HG changeset patch
    # User Anna Haensch <annahaensch@gmail.com>
    # Date 1317153891 14400
    # Node ID 53419d0f5c54a201a6ad1c2749b4246d74628e39
    # Parent  639a44854297aa4739208933bfc6830b7956576c
    trac #11841 sub lattice of a given lattice
    
    diff --git a/sage/quadratic_forms/quadratic_form.py b/sage/quadratic_forms/quadratic_form.py
    a b class QuadraticForm(): 
    877877        else:
    878878            raise(TypeError, "Oops! Presently we can only evaluate a quadratic form on a list, tuple, vector or matrix.")
    879879
     880    def new_lattice_on_space(self,L):
     881        r"""
     882        Determines the quadratic lattice spanned by the vectors in `L`
     883        with the same underlying quadratic space as the lattice given.
    880884
     885        If the given list of vectors does not span an integral
     886        lattice, an error message will occur.
    881887
     888        .. WARNING::
     889
     890            This allows for degenerate lattices.
     891
     892        INPUT:
     893
     894        `L` -- a list of vectors
     895
     896        OUTPUT:
     897
     898        a quadratic form
     899
     900        EXAMPLES:
     901
     902        First an example in dimension 2::
     903
     904            sage: L = [(2,2),(3,4)]
     905            sage: Q = QuadraticForm(ZZ,2,[1,2,1])
     906            sage: Q.new_lattice_on_space(L)
     907            Quadratic form in 2 variables over Integer Ring with coefficients:
     908            [ 16 56 ]
     909            [ * 49 ]
     910
     911        Another example in dimension 3::
     912
     913            sage: L = [(1/7,2/7,1/7),(0,1,0),(0,0,1)]
     914            sage: Q = DiagonalQuadraticForm(ZZ,[49,49,686])
     915            sage: Q.new_lattice_on_space(L)
     916            Quadratic form in 3 variables over Integer Ring with coefficients:
     917            [ 19 28 196 ]
     918            [ * 49 0 ]
     919            [ * * 686 ]
     920
     921        ::
     922
     923            sage: L = [(2,2),(1/2,1)]
     924            sage: Q = QuadraticForm(ZZ,2,[1,2,1])
     925            sage: Q.new_lattice_on_space(L)
     926            Traceback (most recent call last):
     927            ...
     928            TypeError: this basis cannot give an integral lattice
     929
     930            sage: Q.new_lattice_on_space([(2,2)])
     931            Traceback (most recent call last):
     932            ...
     933            TypeError: a basis must have n elements
     934
     935            sage: Q.new_lattice_on_space([(2,2),(1,2,1)])
     936            Traceback (most recent call last):
     937            ...
     938            TypeError: vectors must have the same dimension as the underlying space
     939        """
     940        if len(L) != self.dim():
     941            raise TypeError("a basis must have n elements")
     942        for i in range(len(L)):
     943            if len(L[i]) != self.dim():
     944                raise TypeError("vectors must have the same dimension as the underlying space")
     945        from sage.rings.rational_field import QQ
     946        V = self.base_change_to(QQ)
     947        mat_entries = []
     948        for i in range(len(L)):
     949            w_i = vector(QQ,L[i])
     950            for j in range(i,len(L)):
     951                w_j = vector(QQ,L[j])
     952                if V.bilinear_map(w_i,w_j) not in ZZ:
     953                    raise TypeError("this basis cannot give an integral lattice")
     954                elif i == j:
     955                    mat_entries += [V.bilinear_map(w_i,w_j)]
     956                elif i != j:
     957                    mat_entries += [2*V.bilinear_map(w_i,w_j)]
     958        return QuadraticForm(ZZ,self.dim(),mat_entries)
    882959
    883960## =====================================================================================================
    884961