def test_latex_PolyElement(): Ruv, u,v = ring("u,v", ZZ); Rxyz, x,y,z = ring("x,y,z", Ruv.to_domain()) assert latex(x - x) == r"0" assert latex(x - 1) == r"x - 1" assert latex(x + 1) == r"x + 1" assert latex((u**2 + 3*u*v + 1)*x**2*y + u + 1) == r"\left({u}^{2} + 3 u v + 1\right) {x}^{2} y + u + 1" assert latex((u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x) == r"\left({u}^{2} + 3 u v + 1\right) {x}^{2} y + \left(u + 1\right) x" assert latex((u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x + 1) == r"\left({u}^{2} + 3 u v + 1\right) {x}^{2} y + \left(u + 1\right) x + 1" assert latex((-u**2 + 3*u*v - 1)*x**2*y - (u + 1)*x - 1) == r"-\left({u}^{2} - 3 u v + 1\right) {x}^{2} y - \left(u + 1\right) x - 1"
def example_20(): """ Example 20 from KK """ R, x, y, z = ring('x,y,z', RR, grevlex) I = [z**2 + 3 * y - 7 * z, y * z - 4 * y, x * z - 4 * y, y**2 - 4*y, x*y - 4 * y, x**5 - 8 * x**4 + 14 * x**4 + 8 * x**2 - 15 * x + 15 * y, ] G = [z**2 + 3 * y - 7 *z, y * z - 4 * y, x * z - 4 * y, y**2 - 4*y, x *y - 4 * y, x**2 * z - 16 * y, x **2 * y - 16 * y, x**3 * z - 64 * y, x**3 * y - 64 * y, x**4 * z - 256 * y, x**4 * y - 256 * y, x **5 - 8 * x **4 + 14 * x **4 + 8 * x **2 - 15 * x + 15 * y, ] return R, I, G
def test_episode_2(s, reward): R, x, y, z, t = sp.ring('x,y,z,t', sp.FF(32003), 'grlex') F = [x**31 - x**6 - x - y, x**8 - z, x**10 - t] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, rewards='reductions') agent = BuchbergerAgent(selection=s) assert run_episode(agent, env) == reward
def generate_parabolas(N=2, sigma=0.): R, x, y = ring('x, y', RR, order=grevlex) thetas = [2 * pi / N * t for t in xrange(N)] I = [(x * sin(t) + y * cos(t) - 1)**2 - (x + cos(t))**2 - (y + sin(t))**2 + sigma * randn() for t in thetas] return R, I
def example_20(): """ Example 20 from KK """ R, x, y, z = ring('x,y,z', RR, grevlex) I = [ z**2 + 3 * y - 7 * z, y * z - 4 * y, x * z - 4 * y, y**2 - 4 * y, x * y - 4 * y, x**5 - 8 * x**4 + 14 * x**4 + 8 * x**2 - 15 * x + 15 * y, ] G = [ z**2 + 3 * y - 7 * z, y * z - 4 * y, x * z - 4 * y, y**2 - 4 * y, x * y - 4 * y, x**2 * z - 16 * y, x**2 * y - 16 * y, x**3 * z - 64 * y, x**3 * y - 64 * y, x**4 * z - 256 * y, x**4 * y - 256 * y, x**5 - 8 * x**4 + 14 * x**4 + 8 * x**2 - 15 * x + 15 * y, ] return R, I, G
def get_measurement_polynomials(self, noise = 0, seed = 0): np.random.seed(seed) k, d = self.k, self.d params = self.get_parameters() R = ring([x for x, _ in params], RR)[0] names = {str(x) : R(x) for x in R.symbols} xs = array([[names[self.x(i,j)] for j in xrange(k)] for i in xrange(d)]) params = [(names[x], v) for x, v in params] # Second order moments (TODO: 3rd order moments) P = zeros((d,d), dtype=np.object) p = zeros((d,), dtype=np.object) for i in xrange(d): p[i] = sum(xs[i,k_] for k_ in xrange(k))# / k for j in xrange(i, d): P[i,j] = sum(xs[i,k_] * xs[j,k_] for k_ in xrange(k))# / k # Project and profit m = zeros((d,)) M = zeros((d,d)) for i in xrange(d): m[i] = p[i].evaluate(params) for j in xrange(i, d): M[i,j] = P[i,j].evaluate(params) M = M + noise * np.random.randn(d,d) m = m + noise * np.random.randn(d) # TODO: Something is wrong here #m = M.sum(1) # Finally return values. return R, [f - f_ for f, f_ in zip(p.flatten(), m.flatten())] + [f - f_ for f, f_ in zip(P[triu_indices_from(P)], M[triu_indices_from(M)])]
def example_order_preservation(): """ Example 3.4 from paper. 5 variables, 7 equations. Method fails unless the lex basis is re-added to the original basis """ R, x0, x1, x2, x3, x4 = ring('x0,x1,x2,x3,x4', QQ, lex) I = [x0 - x2, x0 - x3, x1 - x3, x1 - x4, x2 - x3, x3 - x4, x2**2] return R, I
def example_simple(): """ Example 3.1 from paper. 4 variables, 4 equations, simple, method works """ R, x0, x1, x2, x3 = ring('x0,x1,x2,x3', QQ, lex) I = [x0**4 - 1, x0**2 + x2, x1**2 + x2, x2**2 + x3] return R, I
def test_extend(): """ Test extension """ R = ring('x,y', RR)[0] L = BorderBasedUniverse(R, [(2, 1), (1, 2)]) L.extend() assert L._border == [(3, 1), (2, 2), (1, 3)]
def example_subset_large(): A = range(10) S = 5 n = len(A) syms = [Symbol('s%d'%i) for i in xrange(n+1)] R = ring(syms, QQ, lex)[0] I = [syms[0]] + [(syms[i] - syms[i-1]) * (syms[i] - syms[i-1] - A[i-1]) for i in xrange(1,n+1)] + [syms[n] - S] return R, as_ring_exprs(R, I)
def _scalar_to_str(self, c): # not an elegant way to force elements of algebraic fields be printed with sqrt if isinstance(c, sympy.polys.polyclasses.ANP): dummy_ring = sympy.ring([], self.domain)[0] return f"({dummy_ring(c).as_expr()})" if isinstance(c, sympy.polys.fields.FracElement): return f"({c})" return str(c)
def example_fail(): """ Example 3.2 from paper. 2 variables, 3 equations, method fails Failure mode: one of the ideal quotients is empty, which means that the variety could be the empty set. """ R, x0, x1, x2 = ring('x0,x1,x2', QQ, lex) I = [x0 * x1 + 1, x1 + x2, x1 * x2] return R, I
def example_fail(): """ Example 3.2 from paper. 2 variables, 3 equations, method fails Failure mode: one of the ideal quotients is empty, which means that the variety could be the empty set. """ R, x0, x1, x2 = ring('x0,x1,x2', QQ, lex) I = [x0 * x1 + 1, x1 + x2, x1*x2] return R, I
def example_subset_large(): A = range(10) S = 5 n = len(A) syms = [Symbol('s%d' % i) for i in xrange(n + 1)] R = ring(syms, QQ, lex)[0] I = [syms[0]] + [(syms[i] - syms[i - 1]) * (syms[i] - syms[i - 1] - A[i - 1]) for i in xrange(1, n + 1)] + [syms[n] - S] return R, as_ring_exprs(R, I)
def example_trivial(): """ My own example. """ R, x, y = ring('x,y', RR, grevlex) I = [x**2 + y**2 - 2, x + y] return R, I
def test_episode_1(e, reward): R, a, b, c, d = sp.ring('a,b,c,d', sp.FF(32003), 'grevlex') F = [ a + b + c + d, a * b + b * c + c * d + d * a, a * b * c + b * c * d + c * d * a + d * a * b, a * b * c * d - 1 ] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, elimination=e, rewards='reductions') agent = BuchbergerAgent(selection=['normal', 'first']) assert run_episode(agent, env) == reward
def generate_univariate_problem(n_common_zeros=1, max_degree=3, n_equations=5): R, x = ring('x', RR, order=grevlex) # set of common zeros. V = 1 - 2 * rand(n_common_zeros) I = [ poly_for_zeros( R, x, np.hstack((V, 1 - 2 * rand(max_degree - n_common_zeros)))) for _ in xrange(n_equations) ] return R, I, V
def generate_dense_polynomial(nvars, deg): R, *variables = sp.ring([f"x{i}" for i in range(nvars)], sp.QQ) def gen_eq_deg(d: int): return reduce(add, [randrange(-10, 10) * reduce(mul, p) for p in itertools.product(variables, repeat=d)]) \ + randrange(-10, 10) def gen_eq(): return reduce(add, [gen_eq_deg(d) for d in range(1, deg + 1)]) return [gen_eq() for _ in range(nvars)]
def test_restrict_lt1(): _, x, y = ring('x,y', RR, grevlex) I = [x + y + 1, x**2 + y + 1] L = get_order_basis(I) B = get_support_basis(I) W = unitary_basis(B, I) L_, B_, W_ = restrict_lt(L, B, W) assert L_ == L assert np.allclose(W_, W)
def test_restrict_lt2(): _, x, y = ring('x,y', RR, grevlex) I = [x * y**2, x**2 * y + x * y**2, x**3 * y**2] L = get_order_basis([x**2 * y]) B = get_support_basis(I) W = unitary_basis(B, I) L_, B_, W_ = restrict_lt(L, B, W) assert len(difference(L_, L)) > 0 and len(difference(L, L_)) == 0 assert len(B_) == 2 assert np.allclose(W_, W[:2, 1:])
def example(): """ Example 4.12 """ R, x, y, z = ring('x,y,z', RR, grevlex) I = [ 0.130 * z**2 + 0.39 * y - 0.911 * z, 0.242 * y * z - 0.97 * y, 0.243 * x * z - 0.97 * y, 0.242 * y**2 - 0.97 * y, 0.243 * x * y - 0.97 * y, 0.035 * x**5 - 0.284 * x**4 + 0.497 * x**3 + 0.284 * x**2 - 0.532 * x + 0.533 * y ] return R, I
def example_trivial(): """ My own example. """ R, x, y = ring('x,y', RR, grevlex) I = [x**2 + y**2 - 2, x + y ] return R, I
def test_restrict_lt2(): _, x, y = ring('x,y', RR, grevlex) I = [x*y**2, x**2 * y + x * y**2, x**3 * y**2] L = get_order_basis([x**2*y]) B = get_support_basis(I) W = unitary_basis(B, I) L_, B_, W_ = restrict_lt(L, B, W) assert len(difference(L_, L)) > 0 and len(difference(L, L_)) == 0 assert len(B_) == 2 assert np.allclose(W_, W[:2,1:])
def example(): """ Example 4.12 """ R, x, y, z = ring('x,y,z', RR, grevlex) I = [0.130 * z**2 + 0.39 * y - 0.911 * z, 0.242 * y * z - 0.97 * y, 0.243 * x * z - 0.97 * y, 0.242 * y**2 - 0.97 * y, 0.243 * x * y - 0.97 * y, 0.035 * x**5 - 0.284 * x**4 + 0.497 * x**3 + 0.284 * x**2 - 0.532 * x + 0.533 * y ] return R, I
def test_episode_0(s): R, a, b, c, d, e = sp.ring('a,b,c,d,e', sp.FF(32003), 'grevlex') F = [ a + 2 * b + 2 * c + 2 * d + 2 * e - 1, a**2 + 2 * b**2 + 2 * c**2 + 2 * d**2 + 2 * e**2 - a, 2 * a * b + 2 * b * c + 2 * c * d + 2 * d * e - b, b**2 + 2 * a * c + 2 * b * d + 2 * c * e - c, 2 * b * c + 2 * a * d + 2 * b * e - d ] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, rewards='reductions') agent = BuchbergerAgent(selection=s) assert run_episode(agent, env) == -28
def generate_lifeware_conjecture(n): """ Generate the system from Conjecture 3.1 from https://hal.inria.fr/hal-02900798v2/document Also is called Long Monomial in benchmarks. """ variables = sp.ring([f"x{i}" for i in range(n)], sp.QQ)[1:] prod_all = 1 for i in range(n): prod_all *= variables[i]**2 system = [] for i in range(n): system.append(variables[(i + 1) % n]**2 + prod_all) return system
def generate_exponential(): coef_field = sp.FractionField( sp.QQ, ["Apsi", "Atheta", "bpsi", "btheta", "gamma", "B", "D"]) Apsi, Atheta, bpsi, btheta, gamma, B, D = coef_field.gens R, psi, theta, exp, thinv2, u, du = sp.ring( ["psi", "theta", "exp", "thinv2", "u", "u'"], coef_field) eqs = [ Apsi * psi + bpsi * u - D * psi * exp, Atheta * theta + btheta * u + B * D * psi * exp ] eqs.append(gamma * eqs[0] * thinv2 * exp) eqs.append(eqs[0] * (-2) * thinv2 * psi) eqs.append(du) eqs.append(du**2) return eqs
def test_LeadMonomialsEnv_1(): R, x, y, z = sp.ring('x,y,z', sp.FF(101), 'grevlex') F = [y - x**2, z - x**3] ideal_gen = FixedIdealGenerator(F) env = LeadMonomialsEnv(ideal_gen) state = env.reset() assert np.array_equal(state, np.array([[2, 0, 0, 3, 0, 0]])) state, _, done, _ = env.step(0) assert np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0]])) assert not done state, _, done, _ = env.step(0) assert np.array_equal(state, np.array([[1, 1, 0, 0, 2, 0]])) assert not done state, _, done, _ = env.step(0) assert done
def example_simple(): """ Example 19 from KK """ R, x, y = ring('x,y', RR, grevlex) I = [ x**3 - x, y**3 - y, x**2 * y - 0.5 * y - 0.5 * y**2, x * y - x - 0.5 * y + x**2 - 0.5 * y**2, x * y**2 - x - 0.5 * y + x**2 - 0.5 * y**2 ] G = [ x**2 + x * y - 0.5 * y**2 - x - 0.5 * y, y**3 - y, x * y**2 - x * y, x**2 * y - 0.5 * y**2 - 0.5 * y ] return R, I, G
def example_mog(sigma=0): R, x1, x2, y1, y2 = ring('x1,x2,y1,y2', RR, lex) m1x = x1 + x2 m1y = y1 + y2 m2xx = x1**2 + x2**2 m2xy = x1 * y1 + x2 * y2 m2yy = y1**2 + y2**2 ms = [ m1x, m1y, m2xx, m2xy,] #m2yy, ] if True: x1v, x2v = 1, -1 y1v, y2v = -1, 1 I = [ (m - m(x1v, x2v, y1v, y2v) + sigma * np.random.randn()) for m in ms] return R, I
def example_simple(): """ Example 19 from KK """ R, x, y = ring('x,y', RR, grevlex) I = [x**3 - x, y**3 - y, x**2*y - 0.5 * y - 0.5 * y**2, x*y - x - 0.5 * y + x**2 - 0.5 * y**2, x * y**2 - x - 0.5 * y + x**2 - 0.5 * y**2 ] G = [x**2 + x * y - 0.5 * y**2 - x - 0.5 * y, y**3 - y, x*y**2 - x*y, x**2 * y - 0.5 * y**2 - 0.5 * y ] return R, I, G
def test_LeadMonomialsEnv_0(): R, x, y, z = sp.ring('x,y,z', sp.FF(101), 'grevlex') F = [y - x**2, z - x**3] ideal_gen = FixedIdealGenerator(F) env = LeadMonomialsEnv(ideal_gen, elimination='none') state = env.reset() assert np.array_equal(state, np.array([[2, 0, 0, 3, 0, 0]])) state, _, done, _ = env.step(0) assert (np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0], [3, 0, 0, 1, 1, 0]])) or np.array_equal( state, np.array([[3, 0, 0, 1, 1, 0], [2, 0, 0, 1, 1, 0]]))) assert not done action = 0 if np.array_equal(state[0], np.array([3, 0, 0, 1, 1, 0])) else 1 state, _, done, _ = env.step(action) assert np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0]])) assert not done for _ in range(4): state, _, done, _ = env.step(0) assert done
def example_mog(sigma=0): R, x1, x2, y1, y2 = ring('x1,x2,y1,y2', RR, lex) m1x = x1 + x2 m1y = y1 + y2 m2xx = x1**2 + x2**2 m2xy = x1 * y1 + x2 * y2 m2yy = y1**2 + y2**2 ms = [ m1x, m1y, m2xx, m2xy, ] #m2yy, ] if True: x1v, x2v = 1, -1 y1v, y2v = -1, 1 I = [(m - m(x1v, x2v, y1v, y2v) + sigma * np.random.randn()) for m in ms] return R, I
def get_measurement_polynomials(self, noise=0, seed=0): np.random.seed(seed) k, d = self.k, self.d params = self.get_parameters() R = ring([x for x, _ in params], RR)[0] names = {str(x): R(x) for x in R.symbols} xs = array([[names[self.x(i, j)] for j in xrange(k)] for i in xrange(d)]) params = [(names[x], v) for x, v in params] # Second order moments (TODO: 3rd order moments) P = zeros((d, d), dtype=np.object) p = zeros((d, ), dtype=np.object) for i in xrange(d): p[i] = sum(xs[i, k_] for k_ in xrange(k)) # / k for j in xrange(i, d): P[i, j] = sum(xs[i, k_] * xs[j, k_] for k_ in xrange(k)) # / k # Project and profit m = zeros((d, )) M = zeros((d, d)) for i in xrange(d): m[i] = p[i].evaluate(params) for j in xrange(i, d): M[i, j] = P[i, j].evaluate(params) M = M + noise * np.random.randn(d, d) m = m + noise * np.random.randn(d) # TODO: Something is wrong here #m = M.sum(1) # Finally return values. return R, [f - f_ for f, f_ in zip(p.flatten(), m.flatten())] + [ f - f_ for f, f_ in zip(P[triu_indices_from(P)], M[ triu_indices_from(M)]) ]
def test_get_order_basis(): _, x, y = ring('x,y', RR, grevlex) f = x**2 + x * y + y g = x**2 + x * y + x b = get_order_basis([f, g]) assert b == [(2, 0), (1, 1), (1, 0), (0, 1), (0, 0)]
def generate_parabolas(N=2, sigma = 0.): R, x, y = ring('x, y', RR, order=grevlex) thetas = [2 * pi / N * t for t in xrange(N)] I = [(x * sin(t) + y * cos(t) - 1)**2 - (x+cos(t))**2 - (y+sin(t))**2 + sigma * randn() for t in thetas] return R, I
def test_get_order_basis(): _, x, y = ring('x,y', RR, grevlex) f = x**2 + x*y + y g = x**2 + x*y + x b = get_order_basis([f,g]) assert b == [(2,0), (1,1), (1,0), (0,1), (0,0)]
# Percy's playground. from __future__ import print_function import sympy as sp import numpy as np import BorderBasis as BB np.set_printoptions(precision=3) from IPython.display import display, Markdown, Math sp.init_printing() R, x, y = sp.ring('x,y', sp.RR, order=sp.grevlex) I = [ x**2 + y**2 - 1.0, x + y ] R, x, y, z = sp.ring('x,y,z', sp.RR, order=sp.grevlex) I = [ x**2 - 1, y**2 - 4, z**2 - 9] # n = 4 takes a long time n = 4 Rvs = sp.ring(' '.join('v'+str(i) for i in range(1, n + 1)), sp.RR, order=sp.grevlex) R, vs = Rvs[0], Rvs[1:] I = [] I.extend([v**2 - 1 for v in vs]) #I.extend([(v-1)**2 for v in vs]) #I.extend([v-1 for v in vs]) #I.extend([vs[i] - vs[i-1] for i in range(1, len(vs))]) # Makes it fast print('Generating') B = BB.BorderBasisFactory(1e-5).generate(R,I) print('Done') print("=== Generator Basis:")
def test_border(): R, x, y = ring('x,y', RR, grevlex) O = get_order_basis([x, y]) dO = border(R, O) assert dO == [(2, 0), (1, 1), (0, 2)]
def test_border_basis_divide(): R, x, y = ring('x,y', RR, grevlex) G = [x**2 + x + 1, x *y + y, y**2 + x + 1] f = x**3 * y**2 - x * y**2 + x**2 + 2 r = border_basis_divide(R, G, f) assert r == -3.0 * x - 1.0
def generate_univariate_problem(n_common_zeros=1, max_degree=3, n_equations=5): R, x = ring("x", RR, order=grevlex) # set of common zeros. V = 1 - 2 * rand(n_common_zeros) I = [poly_for_zeros(R, x, np.hstack((V, 1 - 2 * rand(max_degree - n_common_zeros)))) for _ in xrange(n_equations)] return R, I, V
from timeit import default_timer as clock from sympy import ring, ZZ R, x, y, z, w = ring("x y z w", ZZ) e = (x+y+z+w)**15 t1 = clock() f = e*(e+w) t2 = clock() #print f print "Total time:", t2-t1, "s" print "number of terms:", len(f)
def generate_lines(N=2, sigma = 0.): R, x, y = ring('x, y', RR, order=grevlex) thetas = [2 * pi / N * t for t in xrange(N)] I = [x * sin(t) + y * cos(t) + sigma * randn() for t in thetas] return R, I
def test_get_coefficient_term(): R, x, y, z = ring('x,y,z', QQ, lex) f = R(3 * x**2 * y * z + 2 * x**2 * y**2 + z + x * y * z) coeff = get_coefficient_term(R, f) assert coeff == R(2 * y**2 + 3 * y * z)
def test_border(): R, x, y = ring('x,y', RR, grevlex) O = get_order_basis([x, y]) dO = border(R, O) assert dO == [(2,0), (1,1), (0,2)]
def test_border_basis_divide(): R, x, y = ring('x,y', RR, grevlex) G = [x**2 + x + 1, x * y + y, y**2 + x + 1] f = x**3 * y**2 - x * y**2 + x**2 + 2 r = border_basis_divide(R, G, f) assert r == -3.0 * x - 1.0
def generate_lines(N=2, sigma=0.): R, x, y = ring('x, y', RR, order=grevlex) thetas = [2 * pi / N * t for t in xrange(N)] I = [x * sin(t) + y * cos(t) + sigma * randn() for t in thetas] return R, I