Ticket #4539: plural_2.patch

File plural_2.patch, 11.0 KB (added by burcin, 8 years ago)

better user interface for plural rings

  • sage/algebras/free_algebra.py

    # HG changeset patch
    # User Michael Brickenstein <brickenstein@mfo.de>
    # Date 1226934659 -3600
    # Node ID 0acacd97c7f38f5afdb0c762b421e12b1ee8c3ea
    # Parent  1611ab2e6f45c6b11ace32518e8d36e8231179ed
    MPolynomialRing_plural now accepts matrix parameters to specify commutativity
    relations. Added ExteriorAlgebra.
    
    diff --git a/sage/algebras/free_algebra.py b/sage/algebras/free_algebra.py
    a b  
    5555
    5656import sage.structure.parent_gens
    5757
    58        
    5958def FreeAlgebra(R, n, names):
    6059    """
    6160    Return the free algebra over the ring $R$ on $n$ generators with
     
    394393        """
    395394        return self.__monoid
    396395   
    397                    
     396    def g_algebra(self, relations, order='degrevlex'):
     397        """
     398           
     399            The G-Algebra derrived from this algebra by relations.
     400            By default is assumed, that two variables commute.
     401           
     402            TODO:
     403            * Coercion doesn't work yet, there is some cheating about assumptions
     404            * Check degeneracy conditions
     405            Furthermore, the default values interfere with non degeneracy conditions...
     406           
     407            EXAMPLES:
     408            sage: A.<x,y,z>=FreeAlgebra(QQ,3)
     409            sage: G=A.g_algebra({y*x:-x*y})
     410            sage: (x,y,z)=G.gens()
     411            sage: x*y
     412            x*y
     413            sage: y*x
     414            -x*y
     415            sage: z*x
     416            x*z
     417            sage: (x,y,z)=A.gens()
     418            sage: G=A.g_algebra({y*x:-x*y+1})
     419            sage: (x,y,z)=G.gens()
     420            sage: y*x
     421            -x*y + 1
     422            sage: (x,y,z)=A.gens()
     423            sage: G=A.g_algebra({y*x:-x*y+z})
     424            sage: (x,y,z)=G.gens()
     425            sage: y*x
     426            -x*y + z
     427        """
     428        from sage.matrix.constructor  import Matrix
     429        from sage.rings.polynomial.plural import MPolynomialRing_plural
     430       
     431        base_ring=self.base_ring()
     432        n=self.ngens()
     433        cmat=Matrix(base_ring,n)
     434        dmat=Matrix(self,n)
     435        for i in xrange(n):
     436            for j in xrange(i+1,n):
     437                cmat[i,j]=1
     438        for (to_commute,commuted) in relations.iteritems():
     439            #This is dirty, coercion is broken
     440            assert isinstance(to_commute,FreeAlgebraElement), to_commute.__class__
     441            assert isinstance(commuted,FreeAlgebraElement), commuted
     442            ((v1,e1),(v2,e2))=list(list(to_commute)[0][1])
     443            assert e1==1
     444            assert e2==1
     445            assert v1>v2
     446            c_coef=None
     447            d_poly=None
     448            for (c,m) in commuted:
     449                if list(m)==[(v2,1),(v1,1)]:
     450                    c_coef=c
     451                    #buggy coercion workaround
     452                    d_poly=commuted-self(c)*self(m)
     453                    break
     454            assert not c_coef is None,list(m)
     455            cmat[v2,v1]=c_coef
     456            if d_poly:
     457                dmat[v2,v1]=d_poly
     458       
     459        return MPolynomialRing_plural(base_ring, n, ",".join([str(g) for g in self.gens()]), c=cmat, d=dmat,order=order)
     460           
     461           
    398462from sage.misc.cache import Cache
    399463cache = Cache(FreeAlgebra_generic)
  • sage/algebras/free_algebra_element.py

    diff --git a/sage/algebras/free_algebra_element.py b/sage/algebras/free_algebra_element.py
    a b  
    5757            self.__monomial_coefficients = dict([ (A.monoid()(e1),R(e2)) for e1,e2 in x.items()])
    5858        else:
    5959            raise TypeError, "Argument x (= %s) is of the wrong type."%x
     60
     61    def __iter__(self):
     62        """
     63        Returns an iterator which yields tuples of coeficient and monoid.
     64       
     65        EXAMPLES:
     66        sage: a = FreeAlgebra(QQ, 5, 'a').gens()
     67        sage: list(3*a[0]*a[1]*a[4]**3*a[0]+1)
     68        [(1, 1), (3, a0*a1*a4^3*a0)]
     69        """
     70        for (key,val) in self.__monomial_coefficients.iteritems():
     71            yield (val,key)
     72   
    6073
    6174    def _repr_(self):
    6275        """
  • sage/libs/singular/singular-cdefs.pxi

    diff --git a/sage/libs/singular/singular-cdefs.pxi b/sage/libs/singular/singular-cdefs.pxi
    a b  
    9191        napoly *n
    9292        int s
    9393
     94    # polynomials
     95
     96    ctypedef struct poly "polyrec":
     97        poly *next
     98
     99    # ideals
     100
     101    ctypedef struct ideal "ip_sideal":
     102        poly **m # gens array
     103        long rank # rank of module, 1 for ideals
     104        int nrows # always 1
     105        int ncols # number of gens
     106       
     107    # polynomial procs
     108    ctypedef struct p_Procs_s "p_Procs_s":
     109        pass
    94110    # rings
    95111
    96112    ctypedef struct ring "ip_sring":
     
    103119        int  CanShortOut # control printing capabilities
    104120        number *minpoly # minpoly for base extension field
    105121        char **names # variable names
     122        p_Procs_s *p_Procs #polxnomial procs
     123        ideal *qideal #quotient ideal
     124       
    106125        char **parameter # parameter names
    107126        ring *algring # base extension field
    108127        short N # number of variables
     
    134153        ringorder_Ws
    135154        ringorder_L
    136155
    137     # polynomials
    138156
    139     ctypedef struct poly "polyrec":
    140         poly *next
    141157
    142158       
    143159    # groebner basis options
     
    146162        isNotHomog
    147163        isHomog
    148164        testHomog
    149 
    150     # ideals
    151 
    152     ctypedef struct ideal "ip_sideal":
    153         poly **m # gens array
    154         long rank # rank of module, 1 for ideals
    155         int nrows # always 1
    156         int ncols # number of gens
    157165
    158166    # dense matrices
    159167   
     
    694702   
    695703    int nc_CallPlural(matrix* CC, matrix* DD, poly* CN, poly* DN, ring* r)
    696704
     705# Plural functions
     706   
     707    int nc_CallPlural(matrix* CC, matrix* DD, poly* CN, poly* DN, ring* r)
     708   
     709    bint nc_SetupQuotient(ring *, ring *)
     710 
     711    ctypedef enum nc_type:
     712   
     713      nc_error # Something's gone wrong!
     714      nc_general # yx=q xy+...
     715      nc_skew # yx=q xy
     716      nc_comm # yx= xy
     717      nc_lie,  # yx=xy+...
     718      nc_undef, # for internal reasons */
     719      nc_exterior #
     720     
     721cdef extern from "sca.h":
     722    void sca_p_ProcsSet(ring *, p_Procs_s *)
     723   
     724    void scaFirstAltVar(ring *, int)
     725   
     726    void scaLastAltVar(ring *, int)
     727   
     728    void ncRingType(ring *, int)
     729   
    697730cdef extern from "stairc.h":
    698731    # Computes the monomial basis for R[x]/I
    699732    ideal *scKBase(int deg, ideal *s, ideal *Q)
  • sage/monoids/free_monoid_element.py

    diff --git a/sage/monoids/free_monoid_element.py b/sage/monoids/free_monoid_element.py
    a b  
    7878        else:
    7979            # TODO: should have some other checks here...
    8080            raise TypeError, "Argument x (= %s) is of the wrong type."%x
    81 
     81    def __iter__(self):
     82        """
     83        Returns an iterator which yields tuples of variable and exponent.
     84       
     85        EXAMPLES:
     86        sage: a = FreeMonoid(5, 'a').gens()
     87        sage: list(a[0]*a[1]*a[4]**3*a[0])
     88        [(0, 1), (1, 1), (4, 3), (0, 1)]
     89        """
     90        return iter(self._element_list)
    8291##     def __cmp__(left, right):
    8392##         """
    8493##         Compare two free monoid elements with the same parents.
  • sage/rings/polynomial/plural.pxd

    diff --git a/sage/rings/polynomial/plural.pxd b/sage/rings/polynomial/plural.pxd
    a b  
    55cdef class MPolynomialRing_plural(MPolynomialRing_libsingular):
    66    pass
    77
     8cdef class ExteriorAlgebra_plural(MPolynomialRing_plural):
     9    pass
  • sage/rings/polynomial/plural.pyx

    diff --git a/sage/rings/polynomial/plural.pyx b/sage/rings/polynomial/plural.pyx
    a b  
    11include "sage/ext/stdsage.pxi"
    22include "sage/ext/interrupt.pxi"
    33
     4from sage.matrix.constructor  import Matrix
     5
    46cdef class MPolynomialRing_plural(MPolynomialRing_libsingular):
    5     def __init__(self, base_ring, n, names, order='degrevlex'):
     7    def __init__(self, base_ring, n, names, c, d, order='degrevlex'):
    68        MPolynomialRing_libsingular.__init__(self, base_ring, n, names, order)
    7 
    89        cdef matrix* matc=mpNew(n,n)
    910        cdef matrix* matd=mpNew(n,n)
    10 
     11        cdef MPolynomial_libsingular f
     12       
    1113        for i from 0 <= i < n:
    1214            for j from i+1 <= j < n:
    13                 matc.m[n*i+j] = p_ISet(-1, self._ring)
    14 
    15         for i from 0 <= i < n:
    16             for j from i+1 <= j < n:
    17                 matd.m[n*i+j] = p_ISet(-1, self._ring)
    18 
    19         nc_CallPlural(matc, matd, NULL, NULL, self._ring)
    20 
     15                if int(c[i,j])!=0:
     16                    matc.m[n*i+j] = p_ISet(int(c[i,j]), self._ring)
     17        if not d is None:
     18            #have matrix
     19            for i from 0 <= i < n:
     20                for j from i+1 <= j < n:
     21                    commuted=d[i,j]
     22                    if commuted and commuted!=0:
     23                        f =self(commuted)
     24                        matd.m[n*i+j] = p_Copy(f._poly, self._ring)
     25   
     26        nc_CallPlural(matc,matd, NULL, NULL, self._ring)
    2127        id_Delete(<ideal**>&matc, self._ring)
    2228        id_Delete(<ideal**>&matd, self._ring)
    23  
     29
    2430    def _repr_(self):
    2531        """
    2632        EXAMPLE:
    27             sage: from sage.rings.polynomial.plural import MPolynomial_plural
    28             sage: P = MPolynomialRing_plural(QQ, 2, 'x,y')
     33            sage: from sage.rings.polynomial.plural import MPolynomialRing_plural
     34            sage: from sage.matrix.constructor  import Matrix
     35            sage: c=Matrix(2)
     36            sage: c[0,1]=-1
     37            sage: P = MPolynomialRing_plural(QQ, 2, 'x,y', c=c, d=Matrix(2))
    2938            sage: P # indirect doctest
    30             Multivariate Polynomial Ring in x, y over Rational Field
     39            Noncommutative Multivariate Polynomial Ring in x, y over Rational Field
     40            sage: P("x")*P("y")
     41            x*y
     42            sage: P("y")*P("x")
     43            -x*y
    3144        """
    3245#TODO: print the relations
    3346        varstr = ", ".join([ rRingVar(i,self._ring)  for i in range(self.__ngens) ])
    3447        return "Noncommutative Multivariate Polynomial Ring in %s over %s"%(varstr,self.base_ring())
     48
     49cdef class ExteriorAlgebra_plural(MPolynomialRing_plural):
     50    """docstring for ExteriorAlgebra_plural"""
     51    def __init__(self, base_ring, n, names):
     52        """
     53        EXAMPLE:
     54            sage: from sage.rings.polynomial.plural import ExteriorAlgebra_plural
     55
     56            sage: P = ExteriorAlgebra_plural(QQ, 3, 'x,y,z')
     57            sage: P("x")*P("y")
     58            x*y
     59            sage: P("y")*P("x")
     60            -x*y
     61            sage: P("x")*P("x")
     62            0
     63        """
     64        c=Matrix(n)
     65        d=Matrix(n)
     66        for i from 0 <= i < n:
     67            for j from i+1 <= j < n:
     68                c[i,j]=-1
     69       
     70        super(ExteriorAlgebra_plural, self).__init__(base_ring, n,names,c=c,d=d)
     71        self.setupQuotient()
     72       
     73    def setupQuotient(self):
     74        cdef int n=int(self.ngens())
     75        ncRingType( self._ring, nc_exterior );
     76        scaFirstAltVar( self._ring, 1);
     77        scaLastAltVar( self._ring, n );
     78        sca_p_ProcsSet(self._ring, self._ring.p_Procs);