def test_module(): from pyexactreal import ZZModule, RealNumber M = ZZModule(RealNumber.rational(1), RealNumber.random()) x = M.gen(1) # there used to be a segfault in calling module() on a temporary assert str((x*x).module()) == "ℤ-Module(1, ℝ(0.303644…), ℝ(0.303644…)^2)"
def test_serialization(): from pyexactreal import RealNumber, ZZModule, QQModule, NumberFieldModule, NumberField M = ZZModule(RealNumber.rational(1)) assert M.gen(0).to_json() == '{"parent": {"shared": 2147483649, "basis": [{"shared": 2147483650, "kind": "rational", "value": "1"}], "ring": {}}, "coefficients": ["1"]}' M = QQModule(RealNumber.rational(1)) assert M.gen(0).to_json() == '{"parent": {"shared": 2147483649, "basis": [{"shared": 2147483650, "kind": "rational", "value": "1"}], "ring": {}}, "coefficients": ["1"]}' K = NumberField("x^2 - 3", "x", "1.73 +/- 0.1") M = NumberFieldModule(K, RealNumber.rational(1)) assert M.gen(0).to_json() == '{"parent": {"shared": 2147483649, "basis": [{"shared": 2147483650, "kind": "rational", "value": "1"}], "ring": {"field": {"shared": 2147483651, "name": "x", "embedding": "[1.7320508075688772935274463415058723669 +/- 5.08e-38]", "minpoly": "x^2 - 3", "precision": 64}}}, "coefficients": [{"cereal_class_version": 0, "parent": {"shared": 2147483652, "name": "a", "embedding": "1e+0", "minpoly": "a-1", "precision": 64}, "value": "1"}]}'
def random_hexagon(R2): x = K.gen() M = NumberFieldModule(K, RealNumber.rational(1), RealNumber.random(), RealNumber.random()) # The side lengths are going to be 2, 2·μ, 2·ν where μ,ν are the random parameters of M. one = M.gen(0) μ = M.gen(1) ν = M.gen(2) # We build our vectors from (2, 0), μ·(1, √3), ν.(-1, √3). u = R2(2 * one, 0 * one) v = R2(μ, x * μ) w = R2(-ν, x * ν) vectors = [u, v, u + v, -w, u + v - w, u + v] vertices = [[1, 3, -4, -5, -3, -2], [2, -1, -6, 4, 5, 6]] return Surface(vertices, vectors)
def test_binder(): from pyexactreal import RealNumber, ZZModule M = ZZModule(RealNumber.rational(1), RealNumber.random()) assert str(M) == "ℤ-Module(1, ℝ(0.303644…))" x = M.gen(1) assert str(x) == "ℝ(0.303644…)" y = x + 2 * x assert str(y) == "3*ℝ(0.303644…)" z = x * x + y assert str(z) == "ℝ(0.303644…)^2 + 3*ℝ(0.303644…)" assert str(z.module()) == "ℤ-Module(1, ℝ(0.303644…), ℝ(0.303644…)^2)"
def test_multiplication(): from pyexactreal import RealNumber, NumberFieldModule, NumberField # The following came up when doing arithmetic between renf_elem_class and # Element<NumberField> when trying to construct a hexagon with random side # lengths K = NumberField("x^2 - 3", "x", "1.73 +/- 0.1") x = K.gen() M = NumberFieldModule(K, RealNumber.rational(1), RealNumber.random(), RealNumber.random()) one = M.gen(0) μ = M.gen(1) ν = M.gen(2) assert 2*one == one + one assert 0*one == one - one assert x*μ == μ*x assert 2*one + μ - ν - 2*one - μ + ν == 0 assert 0*one + x*μ + x*ν - 0*one - x*ν - x*μ == 0