def get_proj(imp_lst, pmz_lst, mat):
    '''
    Attributes
    ----------        
    imp_lst: list<sage_POLY>
        A list of polynomials in QQ[x0,...,xn] for n<9.
        
    pmz_lst: list<sage_POLY>
        A list n+1 of polynomials in QQ[c0,s0,c1,s1]/<c0^2+s0^2-1,c1^2+s1^2-1>.

    mat: sage_matrix<QQ>    
        A 4*n matrix defined over QQ and of full rank.
    
    Returns
    -------
    ( sage_POLY, list<sage_POLY> )
        Returns a 2-tuple ( f_xyz, prj_pmz_lst ) where 
              
              * "f_xyz" is a polynomial in QQ[x,y,z,w] whose zeroset 
                is the linear projection of the zeroset of "imp_lst".
                The linear projection is defined by "mat". 
              
              * "prj_pmz_lst" is the linear projection of the vector
                 defined by "pmz_lst". The linear projection is 
                 defined by "mat".
    '''
    # init lists of polynomial ring generators
    #
    v = OrbRing.coerce('[v0,v1,v2,v3,v4,v5,v6,v7,v8]')
    x = OrbRing.coerce('[x0,x1,x2,x3,x4,x5,x6,x7,x8]')

    # obtain the linear equations of the projection map
    #
    leq_lst = list(mat * sage_vector(x[:mat.ncols()]))

    # compute the image of this projection map
    #
    proj_lst = [v[i] - leq_lst[i] for i in range(len(leq_lst))]
    p_lst = sage_ideal(imp_lst + proj_lst).elimination_ideal(x).gens()

    # subs (v0:v1:v2:v3) => (1:x:y:z)
    #
    v_xyz_dct = {
        v[0]: 1,
        v[1]: sage_var('x'),
        v[2]: sage_var('y'),
        v[3]: sage_var('z')
    }
    p_lst = [p.subs(v_xyz_dct) for p in p_lst]
    if len(p_lst) != 1:
        raise ValueError('Expect projection to be a surface: p_lst =', p_lst)
    f_xyz = p_lst[0]

    # project parametrization
    #
    prj_pmz_lst = list(mat * sage_vector(pmz_lst))

    return f_xyz, prj_pmz_lst
Example #2
0
def hilbert_poly(X, base=sage_QQ):
    '''
    Computes the Hilbert polynomial of an ideal.
    
    Parameters
    ----------
    X : string(sage_POLY)
        A polynomial in the variables x=(x0,...,xn)
        or y=(y0,...,yn).
    
    base : sage_RING 
        Ground field of polynomials.      
    
    Returns
    -------
    sage_POLY
        Hilbert polynomial of ideal.
    
    '''
    # make sure that the input are strings
    X = str(X)

    # if the input are polynomials in x then the output
    # is a list of poynomials in y
    (vx, vy) = ('x', 'y') if 'x' in X else ('y', 'x')

    # detect the number of x-variables occurring
    n_lst = []
    n = 0
    while n < 50:
        if vx + str(n) in X:
            n_lst += [n]
        n = n + 1
    n = max(n_lst)

    x_lst = [vx + str(i) for i in range(n + 1)]
    ring = sage_PolynomialRing(base, x_lst)
    x_lst = ring.gens()
    dct = ring_dict(ring)
    X = sage__eval(X, dct)

    return sage_ideal(X).hilbert_polynomial()
def verify_get_surf(dct):
    '''
    Attributes
    ----------
    dct: dict 
        The dictionary which is the output of "get_surf()".
    
    Returns
    -------
    dict
        Returns a dictionary "test_dct" with the keys "dct.keys()" and the key 'all'.
        The values are True if it passed the test and False otherwise.
        If test_dct['all'] is True then all test were passed successfully. 
    '''
    x_var = ','.join(['x' + str(i) for i in range(dct['M'].nrows())])
    x_lst = OrbRing.coerce(x_var)
    x_vec = sage_vector(OrbRing.R, x_lst)

    Q = dct['Q']
    M = dct['M']
    U, J = dct['UJ']
    imp_lst = dct['imp_lst']
    pmz_lst = dct['pmz_lst']

    pmz_dct = {x_lst[i]: pmz_lst[i] for i in range(len(pmz_lst))}
    sub_lst = [imp.subs(pmz_dct) for imp in imp_lst]

    test_dct = {}
    test_dct['Q'] = Q.rank() == Q.nrows()
    test_dct['M'] = x_vec * M * x_vec in sage_ideal(imp_lst)
    test_dct['UJ'] = M == approx_QQ(U.T * J * U)
    test_dct['imp_lst'] = len(imp_lst) > 0
    test_dct['pmz_lst'] = set(sub_lst) == set([0])

    test_all = True
    for key in test_dct:
        test_all = test_all and test_dct[key]
    test_dct['all'] = test_all

    return test_dct
Example #4
0
def invert_map(f, X, base=sage_QQ):
    '''
    Computes the inverse of a map defined by polynomials.
    
    If the parameters f and X are not strings, then
    they are automatically converted to strings.
    
    Parameters
    ----------
    f : string(list<sage_POLY>)
        A string of a list of m+1 polynomials in 
        x=(x0,...,xn) or y=(y0,...,yn) with n<=50 that
        define a projective map:
        F: X ---> P^m
        where P^m denotes projective n-space.

    X : string(list<sage_POLY>)
        A string of a list of polynomials in 
        the same variables as parameter f that 
        define the ideal of a variety X in 
        projective space.
    
    base : sage_RING 
        Ground field of polynomials.           
              
    Returns
    -------
    list<sage_POLY>
            Suppose that the input were polynomials in x.
        The output is of the form
            [ p0(x)-r0(y) ,..., pn(x)-rn(y) ]
        were ri(y) are rational functions and pi(x) are
        polynomials. If f is a birational map, then pi(x)=xi.
        The polynomials live in the polynomial ring R[x]
        where R=B(y) and base ring B is defined by the base
        parameter.  
    '''
    # make sure that the input are strings
    f = str(f)
    X = str(X)

    # if the input are polynomials in x then the output
    # is a list of poynomials in y
    (vx, vy) = ('x', 'y') if 'x' in f else ('y', 'x')

    # detect the number of x-variables occurring
    n_lst = []
    n = 0
    while n < 50:
        if vx + str(n) in f or vx + str(n) in X:
            n_lst += [n]
        n = n + 1
    n = max(n_lst)

    # construct ring B(y0,...ym)[x0,...,xn]
    # over fraction field B(y0,...ym)
    # where m is the number of elements in map f and B equals base
    #
    # For example B is rationals sage_QQ so that:
    # xring  = sage_PolynomialRing( sage_QQ, 'x0,x1,x2,x3,x4')
    # yfield = sage_FractionField( sage_PolynomialRing( sage_QQ, 'y0,y1,y2,y3' ) )
    # ring   = sage_PolynomialRing( yfield, 'x0,x1,x2,x3,x4')
    #
    x_lst = [vx + str(i) for i in range(n + 1)]
    y_lst = [vy + str(i) for i in range(len(f.split(',')))]
    yfield = sage_FractionField(sage_PolynomialRing(base, y_lst))
    ring = sage_PolynomialRing(yfield, x_lst)
    y_lst = yfield.gens()
    x_lst = ring.gens()

    dct = ring_dict(ring)
    X = sage__eval(X, dct)
    f = sage__eval(f, dct)
    map = [y_lst[i] - f[i] for i in range(len(f))]
    gb_lst = list(sage_ideal(X + map).groebner_basis())

    OrbTools.p(gb_lst)

    return gb_lst
Example #5
0
def preimage_map(f, X, Y, base=sage_QQ):
    '''
    Computes the preimage f^{-1}(Y) of a variety Y under a map 
    f: X ---> P^m, defined by polynomials, where the domain 
    X is a variety and P^m denotes projective space.
    
    If the parameters f, X and Y are not strings, then
    they are automatically converted to strings.
    
    Parameters
    ----------
    f : string(list<sage_POLY>)
        A string of a list of m+1 polynomials in 
        x=(x0,...,xn) or y=(y0,...,yn) with n<=50 that
        define a projective map:
        f: X ---> P^m
        where P^m denotes projective n-space.

    X : string(list<sage_POLY>)
        A string of a list of polynomials in 
        the same variables as parameter f that 
        define the ideal of a variety X in 
        projective space.

    Y : string(list<sage_POLY>)
        A string of a list of polynomials in y if
        f consists of polynomials in x (and vice versa). 
        Polynomials define the generators of an ideal of 
        a variety Y in projective space.
    
    base : sage_RING 
        Ground field of polynomials.     
        
    Returns
    -------
    list(list<sage_POLY>)
        A list of lists of polynomials. Each list of polynomials
        defines a component in the primary decomposition of the ideal 
        of the preimage f^{-1}(Y). 
        The polynomials are in x or y if parameter f represents 
        polynomials in x respectively y.
        Note that some of the components in the primary decomposition
        are not part of the ideal, but correspond to the locus 
        where the map f is not defined.             
    '''
    # make sure that the input are strings
    f = str(f)
    X = str(X)
    Y = str(Y)

    # if the input are polynomials in x then the output
    # is a list of poynomials in y
    (vx, vy) = ('x', 'y') if 'x' in f else ('y', 'x')

    # detect the number of x-variables occurring
    n_lst = []
    n = 0
    while n < 50:
        if vx + str(n) in f or vx + str(n) in X:
            n_lst += [n]
        n = n + 1
    n = max(n_lst)

    # construct polynomial ring B[x0,...,xn,y0,...,ym]
    # where B is given by parameter base.
    x_lst = [vx + str(i) for i in range(n + 1)]
    y_lst = [vy + str(i) for i in range(len(f.split(',')))]
    mord = 'degrevlex' if base == sage_QQ else 'lex'  # needed for elimination
    xyring = sage_PolynomialRing(base, y_lst + x_lst, order=mord)
    y_lst = xyring.gens()[:len(y_lst)]
    x_lst = xyring.gens()[len(y_lst):]

    # coerce into common ring with xi and yi variables
    dct = ring_dict(xyring)
    f_lst = sage__eval(f, dct)
    X_lst = sage__eval(X, dct)
    Y_lst = sage__eval(Y, dct)
    mf_lst = [y_lst[i] - f_lst[i] for i in range(len(f_lst))]

    # compute image by using groebner basis
    OrbTools.p(X_lst + mf_lst + Y_lst)
    try:

        img_id = sage_ideal(X_lst + mf_lst + Y_lst).elimination_ideal(y_lst)
        img_lst = img_id.primary_decomposition()
        img_lst = [img.gens() for img in img_lst]
        OrbTools.p(img_lst)
        return img_lst

    except Exception, e:

        OrbTools.p('Exception occurred:', repr(e))
        gb_lst = sage_ideal(X_lst + mf_lst + Y_lst).groebner_basis()
        OrbTools.p(gb_lst)
        e_lst = []
        for gb in gb_lst:
            if vy not in str(gb):
                e_lst += [gb]
        return e_lst
Example #6
0
def image_map(f, X, base=sage_QQ):
    '''
    Computes the image f(X) of a variety X
    under a map f, defined by polynomials.
    
    If the parameters f and X are not strings, then
    they are automatically converted to strings.
    
    Parameters
    ----------
    f : string(list<sage_POLY>)
        A string of a list of m+1 polynomials in 
        x=(x0,...,xn) or y=(y0,...,yn) with n<=50 that
        define a projective map:
        f: X ---> P^m
        where P^m denotes projective n-space.

    X : string(list<sage_POLY>)
        A string of a list of polynomials in 
        the same variables as parameter f that 
        define the ideal of a variety X in 
        projective space.
    
    base : sage_RING 
        Ground field of polynomials. 
        
    Returns
    -------
    list<sage_POLY>
        A list of polynomials that define the
        ideal of f(X). The polynomials are in y
        if the input are polynomials in x, and
        vice versa.            
    '''
    # make sure that the input are strings
    f = str(f)
    X = str(X)

    # if the input are polynomials in x then the output
    # is a list of poynomials in y
    (vx, vy) = ('x', 'y') if 'x' in f else ('y', 'x')

    # detect the number of x-variables occurring
    n_lst = []
    n = 0
    while n < 50:
        if vx + str(n) in f or vx + str(n) in X:
            n_lst += [n]
        n = n + 1
    n = max(n_lst)

    # construct polynomial ring B[x0,...,xn,y0,...,ym]
    # where B is given by parameter base.
    x_lst = [vx + str(i) for i in range(n + 1)]
    y_lst = [vy + str(i) for i in range(len(f.split(',')))]
    mord = 'degrevlex' if base == sage_QQ else 'lexdeg'  # needed for elimination
    xyring = sage_PolynomialRing(base, x_lst + y_lst, mord)
    x_lst = xyring.gens()[:len(x_lst)]
    y_lst = xyring.gens()[len(x_lst):]

    # coerce into common ring with xi and yi variables
    dct = ring_dict(xyring)
    f_lst = sage__eval(f, dct)
    X_lst = sage__eval(X, dct)
    mf_lst = [y_lst[i] - f_lst[i] for i in range(len(f_lst))]

    # compute image by using groebner basis
    OrbTools.p(X_lst + mf_lst)
    try:

        # compute image by using groebner basis
        img_lst = sage_ideal(X_lst + mf_lst).elimination_ideal(x_lst).gens()
        OrbTools.p(img_lst)
        return img_lst

    except Exception, e:

        OrbTools.p('Exception occurred:', repr(e))
        gb_lst = sage_ideal(X_lst + mf_lst).groebner_basis()
        OrbTools.p(gb_lst)
        e_lst = []
        for gb in gb_lst:
            if vx not in str(gb):
                e_lst += [gb]
        return e_lst