Пример #1
0
def ll_factorize(A):
    """
    Solves LL of A using polymom
    Assumes that diagonal elements of L are 1
    M = L L^T; M_{ij} = \sum{k=1}^{min{i,j}} L_{ik} L_{jk}
    """
    d, d_ = A.shape
    assert d == d_ # Only triangular for now

    l_vars = [(i,j) for i in xrange(d) for j in xrange(i+1)]
    syms = ["l_%d%d"% (i+1,j+1) for i, j in l_vars]
    R, syms = xring(",".join(syms), RR)
    l_vars_ = dict(zip(l_vars, syms[:len(l_vars)]))

    # Construct equations
    def eqn(i,j):
        return sum(l_vars_.get((i,k), 1 if i == k else 0) * l_vars_.get((j,k), 0) for k in xrange(min(i,j)+1))
    I = [eqn(i,j) - A[i,j] for i in xrange(d) for j in xrange(i+1)]
    I += [l_vars_.get((0,0)) - np.sqrt(A[0,0])]

    return R, I

    B = BB.BorderBasisFactory(1e-5).generate(R,I)
    print B
    V = B.zeros()
    assert len(V) == 1
    V = V[0]
    L = zeros(d,d)
    for idx, (i, j) in enumerate(l_vars):
        L[i, j] = V[idx]

    return L
Пример #2
0
def eigen_factorize(A):
    """
    Solves LU of A using polymom
    Assumes that diagonal elements of L are 1
    """
    d, d_ = A.shape
    assert d == d_ # Only triangular for now

    syms = ["l"] + ["x_%d" % i for i in xrange(d)]
    R, syms = xring(",".join(syms), RR)
    l, xs = syms[0], syms[1:]

    # Construct equations
    def eqn(i):
        return sum(A[i,j] * xs[j] for j in xrange(d))
    I = [eqn(i) - l * xs[i] for i in xrange(d)]
    I += [ xs[0] - 1.0 ]
    #I += [ sum(xs[i]**2 for i in xrange(d)) - 1.0 ]

    return R, I

    B = BB.BorderBasisFactory(1e-5).generate(R,I)
    V = B.zeros()

    return R, I
Пример #3
0
def lu_factorize(A):
    """
    Solves LU of A using polymom
    Assumes that diagonal elements of L are 1
    """
    d, d_ = A.shape
    assert d == d_ # Only triangular for now

    l_vars = [(i,j) for i in xrange(d) for j in xrange(i)]
    u_vars = [(i,j) for i in xrange(d) for j in xrange(i, d)]
    syms = ["l_%d%d"% (i+1,j+1) for i, j in l_vars] + ["u_%d%d"% (i+1,j+1) for i, j in u_vars]
    R, syms = xring(",".join(syms), RR)
    l_vars_ = dict(zip(l_vars, syms[:len(l_vars)]))
    u_vars_ = dict(zip(u_vars, syms[len(l_vars):]))

    # Construct equations
    def eqn(i,j):
        return sum(l_vars_.get((i,k), 1 if i == k else 0) * u_vars_.get((k,j), 0) for k in xrange(min(i,j)+1))
    I = [eqn(i,j) - A[i,j] for i in xrange(d) for j in xrange(d)]

    B = BB.BorderBasisFactory(1e-5).generate(R,I)
    print B
    V = B.zeros()
    assert len(V) == 1
    V = V[0]
    L = np.eye(d)
    U = np.zeros((d,d))
    for idx, (i, j) in enumerate(l_vars):
        L[i, j] = V[idx]

    for idx, (i, j) in enumerate(u_vars):
        U[i, j] = V[len(l_vars)+idx]

    return L, U
Пример #4
0
def eigen_factorize(A):
    """
    Solves LU of A using polymom
    Assumes that diagonal elements of L are 1
    """
    d, d_ = A.shape
    assert d == d_  # Only triangular for now

    syms = ["l"] + ["x_%d" % i for i in xrange(d)]
    R, syms = xring(",".join(syms), RR)
    l, xs = syms[0], syms[1:]

    # Construct equations
    def eqn(i):
        return sum(A[i, j] * xs[j] for j in xrange(d))

    I = [eqn(i) - l * xs[i] for i in xrange(d)]
    I += [xs[0] - 1.0]
    #I += [ sum(xs[i]**2 for i in xrange(d)) - 1.0 ]

    return R, I

    B = BB.BorderBasisFactory(1e-5).generate(R, I)
    V = B.zeros()

    return R, I
Пример #5
0
def cyclic(n, coefficient_ring=sp.FF(32003), order='grevlex'):
    """Return the cyclic-n ideal."""
    R, gens = sp.xring('x:' + str(n), coefficient_ring, order)
    F = [
        sum(np.prod([gens[(i + k) % n] for k in range(d)]) for i in range(n))
        for d in range(1, n)
    ]
    return F + [np.product(gens) - 1]
Пример #6
0
    def __init__(self, d, opt):
        self.d = d
        self.opt = opt

        l_vars = [(i, j) for i in xrange(d) for j in xrange(i + 1)]
        syms = ["l_%d%d" % (i + 1, j + 1) for i, j in l_vars]
        self.R, self.syms = xring(",".join(syms), RR, order=grevlex)
        self.l_vars = dict(zip(l_vars, self.syms))
Пример #7
0
    def __init__(self, d, opt):
        self.d = d
        self.opt = opt

        l_vars = [(i,j) for i in xrange(d) for j in xrange(i+1)]
        syms = ["l_%d%d"% (i+1,j+1) for i, j in l_vars]
        self.R, self.syms = xring(",".join(syms), RR, order=grevlex)
        self.l_vars = dict(zip(l_vars, self.syms))
Пример #8
0
def generate_multivariate_problem(n_variables=10, n_equations=10):
    syms = ['x%d' % i for i in xrange(1, n_variables + 1)]
    R, syms = xring(','.join(syms), RR, order=grevlex)

    I = [
        sum(coeff * sym for coeff, sym in zip(coeffs, syms))
        for coeffs in np.random.randn(n_variables, n_equations)
    ]
    return R, I, [np.zeros(n_variables)]
Пример #9
0
def make_env(args):
    """Return the training environment for this run."""
    if args.environment in ['CartPole-v0', 'CartPole-v1', 'LunarLander-v2']:
        env = gym.make(args.environment)
    elif args.binned:
        dirname = "".join([
            'data/bins/{0}-{1}-{2}'.format(args.variables, args.degree,
                                           args.generators),
            '-consts' if args.constants else "",
            '-' + args.degree_distribution,
            '-homog' if args.homogeneous else "",
            '-pure' if args.pure else "",
        ])
        ring = sp.xring('x:' + str(args.variables), sp.FF(32003), 'grevlex')[0]
        ideal_gen = FromDirectoryIdealGenerator(dirname, ring)
        env = BuchbergerEnv(ideal_gen,
                            elimination=args.elimination,
                            rewards=args.rewards)
        env = LeadMonomialsWrapper(env, k=args.k)
    elif args.environment == "MixedRandomBinomialIdeal":
        ideal_gen = MixedRandomBinomialIdealGenerator(
            args.variables,
            list(range(5, args.degree + 1)),
            list(range(4, args.generators + 1)),
            constants=args.constants,
            degrees=args.degree_distribution,
            homogeneous=args.homogeneous,
            pure=args.pure)
        env = BuchbergerEnv(ideal_gen,
                            elimination=args.elimination,
                            rewards=args.rewards)
        env = LeadMonomialsWrapper(env, k=args.k)
    elif args.environment == "RandomPolynomialIdeal":
        ideal_gen = RandomIdealGenerator(args.variables,
                                         args.degree,
                                         args.generators,
                                         args.l,
                                         constants=args.constants,
                                         degrees=args.degree_distribution)
        env = BuchbergerEnv(ideal_gen,
                            elimination=args.elimination,
                            rewards=args.rewards)
        env = LeadMonomialsWrapper(env, k=args.k)
    else:
        ideal_gen = RandomBinomialIdealGenerator(
            args.variables,
            args.degree,
            args.generators,
            constants=args.constants,
            degrees=args.degree_distribution,
            homogeneous=args.homogeneous,
            pure=args.pure)
        env = BuchbergerEnv(ideal_gen,
                            elimination=args.elimination,
                            rewards=args.rewards)
        env = LeadMonomialsWrapper(env, k=args.k)
    return env
Пример #10
0
 def __init__(self,
              n,
              d,
              s,
              lam,
              coefficient_ring=sp.FF(32003),
              order='grevlex',
              constants=False,
              degrees='uniform'):
     self.ring = sp.xring('x:' + str(n), coefficient_ring, order)[0]
     self.dist = self._make_dist(n, d, constants, degrees)
     self.lam = lam
     self.generators = s
     self.bases = [basis(self.ring, i) for i in range(d + 1)]
Пример #11
0
 def __init__(self,
              n=3,
              d=20,
              s=10,
              lam=0.5,
              dist='uniform',
              constants=False,
              homogeneous=False,
              coefficient_ring=sp.FF(32003),
              order='grevlex'):
     ring = sp.xring('x:' + str(n), coefficient_ring, order)[0]
     self.s = s
     self.lam = lam
     self.homogeneous = homogeneous
     self.bases = [basis(ring, i) for i in range(d + 1)]
     self.rng = np.random.default_rng()
     self.degree_dist = degree_distribution(ring,
                                            d,
                                            dist=dist,
                                            constants=constants)
     self.ring = ring
     self.P = ring.domain.characteristic()
Пример #12
0
def generate_multivariate_problem(n_variables=10, n_equations=10):
    syms = ["x%d" % i for i in xrange(1, n_variables + 1)]
    R, syms = xring(",".join(syms), RR, order=grevlex)

    I = [sum(coeff * sym for coeff, sym in zip(coeffs, syms)) for coeffs in np.random.randn(n_variables, n_equations)]
    return R, I, [np.zeros(n_variables)]
Пример #13
0
    def __init__(self, n_variables, n_equations):
        self.n_variables, self.n_equations = n_variables, n_equations

        syms = ['x%d' % i for i in xrange(1, n_variables + 1)]
        self.R, self.syms = xring(','.join(syms), RR, order=grevlex)
Пример #14
0
    def __init__(self, d, opt):
        self.d = d
        self.opt = opt

        syms = ["l"] + ["x_%d" % (i + 1) for i in xrange(d)]
        self.R, self.syms = xring(",".join(syms), RR, order=grevlex)
Пример #15
0
    def __init__(self, n_variables, n_equations):
        self.n_variables, self.n_equations = n_variables, n_equations

        syms = ['x%d' % i for i in xrange(1, n_variables+1)]
        self.R, self.syms = xring(','.join(syms), RR, order=grevlex)
Пример #16
0
    def __init__(self, d, opt):
        self.d = d
        self.opt = opt

        syms = ["l"] + ["x_%d" % (i+1) for i in xrange(d)]
        self.R, self.syms = xring(",".join(syms), RR, order=grevlex)