Repository Public MIT
1#!/usr/bin/env python32# ORIX Calc3# File: Packages/ORIXSymbolic/Reference/generate_reference.py4# Description: Generates the sympy / mpmath reference values used by the ORIXSymbolic test suite (semantic comparison at sample points).5#6# Author: Simon-Pierre Boucher7# Contact: contact@spboucher.ai8#9# Usage: python3 Reference/generate_reference.py10# Writes Reference/reference.json and copies it to Tests/ORIXSymbolicTests/Resources/reference.json.11# Expressions are written once, in ORIX syntax, and translated to sympy here, so both engines read the same input.12import json, random, shutil, os, sys13import sympy as sp14import mpmath as mp15from sympy.parsing.sympy_parser import parse_expr, standard_transformations, convert_xor1617mp.mp.dps = 3018random.seed(20261120)19HERE = os.path.dirname(os.path.abspath(__file__))2021LOCALS = {22 'e': sp.E, 'pi': sp.pi, 'i': sp.I, 'inf': sp.oo, 'ln': sp.log, 'abs': sp.Abs, 'cbrt': sp.cbrt, 'sign': sp.sign,23 'log': lambda *a: sp.log(a[0], a[1] if len(a) > 1 else 10),24 'log2': lambda a: sp.log(a, 2), 'log10': lambda a: sp.log(a, 10),25 'root': lambda a, n: sp.root(a, n),26}27for name in ['x', 'y', 'z', 'a', 'b', 'c', 'n', 'k', 't', 'r']:28 LOCALS[name] = sp.Symbol(name, real=True)2930def P(text):31 return parse_expr(text, local_dict=dict(LOCALS), transformations=standard_transformations + (convert_xor,))3233def num(value):34 """High-precision evaluation to a real float; None when not real or not finite."""35 try:36 v = sp.N(value, 25)37 except Exception:38 return None39 if v.is_real is False:40 im = sp.im(v)41 if abs(im) > 1e-20:42 return None43 v = sp.re(v)44 try:45 f = float(v)46 except Exception:47 return None48 if f != f or abs(f) == float('inf'):49 return None50 return f5152def points(variables, domain, count=6):53 lo, hi = domain54 out = []55 for _ in range(count):56 out.append({v: round(random.uniform(lo, hi), 6) for v in variables})57 return out5859def evaluate(expr, pts):60 rows = []61 for p in pts:62 v = num(expr.subs({LOCALS[k]: sp.Float(val, 30) for k, val in p.items()}))63 if v is not None:64 rows.append({'point': p, 'value': v})65 return rows6667D01 = (0.15, 0.9) # inside (0, 1): asin, acos, atanh68DPOS = (0.3, 1.7) # positive reals69DGT1 = (1.15, 2.6) # above 1: acosh, asec, acsc7071# ---------------------------------------------------------------- derivatives72DERIVATIVES = [73 ("x^2", "x", DPOS), ("x^3 - 2*x + 1", "x", DPOS), ("5", "x", DPOS), ("x", "x", DPOS), ("1/x", "x", DPOS), ("sqrt(x)", "x", DPOS),74 ("x^(3/2)", "x", DPOS), ("cbrt(x)", "x", DPOS), ("x^(-3)", "x", DPOS), ("(x^2+1)^5", "x", DPOS), ("sin(x)", "x", DPOS), ("cos(x)", "x", DPOS),75 ("tan(x)", "x", D01), ("sec(x)", "x", D01), ("csc(x)", "x", DPOS), ("cot(x)", "x", DPOS), ("asin(x)", "x", D01), ("acos(x)", "x", D01),76 ("atan(x)", "x", DPOS), ("acot(x)", "x", DPOS), ("asec(x)", "x", DGT1), ("acsc(x)", "x", DGT1), ("sinh(x)", "x", DPOS), ("cosh(x)", "x", DPOS),77 ("tanh(x)", "x", DPOS), ("asinh(x)", "x", DPOS), ("acosh(x)", "x", DGT1), ("atanh(x)", "x", D01), ("exp(x)", "x", DPOS), ("e^(2*x)", "x", DPOS),78 ("2^x", "x", DPOS), ("ln(x)", "x", DPOS), ("log(x)", "x", DPOS), ("log(x, 2)", "x", DPOS), ("log2(x)", "x", DPOS), ("abs(x)", "x", DPOS),79 ("x*sin(x)", "x", DPOS), ("x^2*exp(x)", "x", DPOS), ("sin(x)*cos(x)", "x", DPOS), ("x*ln(x)", "x", DPOS), ("exp(x)*sin(x)*x", "x", DPOS),80 ("sin(x)/x", "x", DPOS), ("x/(x^2+1)", "x", DPOS), ("(x^2-1)/(x^2+1)", "x", DPOS), ("exp(x)/(1+exp(x))", "x", DPOS), ("ln(x)/x^2", "x", DPOS),81 ("sin(x^2)", "x", DPOS), ("exp(-x^2)", "x", DPOS), ("ln(sin(x))", "x", DPOS), ("sqrt(1-x^2)", "x", D01), ("cos(3*x+1)", "x", DPOS),82 ("sin(cos(x))", "x", DPOS), ("exp(sin(x))", "x", DPOS), ("tan(x)^2", "x", D01), ("ln(x^2+1)", "x", DPOS), ("atan(2*x)", "x", DPOS),83 ("x^x", "x", DPOS), ("x^sin(x)", "x", DPOS), ("sin(x)^x", "x", DPOS), ("(1+1/x)^x", "x", DPOS), ("sqrt(x)*ln(x)", "x", DPOS),84 ("1/sqrt(x^2+1)", "x", DPOS), ("asin(x/2)", "x", D01), ("sinh(x)*cosh(x)", "x", DPOS), ("ln(ln(x))", "x", DGT1), ("exp(exp(x))", "x", D01),85 ("x^2*y^3", "x", DPOS), ("x^2*y^3", "y", DPOS), ("sin(x*y)", "x", DPOS), ("exp(x/y)", "y", DPOS), ("a*x^n", "x", DPOS), ("ln(a*x+b)", "x", DPOS),86 ("cbrt(x^2+1)", "x", DPOS), ("root(x, 5)", "x", DPOS), ("x*abs(x)", "x", DPOS), ("atanh(x^2)", "x", D01), ("sec(x)*tan(x)", "x", D01),87]88HIGHER = [("sin(x)", "x", 4, DPOS), ("x^5", "x", 3, DPOS), ("exp(x^2)", "x", 2, DPOS), ("ln(x)", "x", 3, DPOS), ("x*exp(x)", "x", 5, DPOS), ("tan(x)", "x", 2, D01)]8990def derivatives():91 out = []92 for text, var, dom in DERIVATIVES:93 f = P(text)94 d = sp.diff(f, LOCALS[var])95 rows = evaluate(d, points(sorted(str(s) for s in f.free_symbols), dom))96 assert len(rows) >= 4, text97 out.append({'expr': text, 'variable': var, 'order': 1, 'sympy': str(d), 'samples': rows})98 for text, var, order, dom in HIGHER:99 f = P(text)100 d = sp.diff(f, LOCALS[var], order)101 rows = evaluate(d, points([var], dom))102 out.append({'expr': text, 'variable': var, 'order': order, 'sympy': str(d), 'samples': rows})103 return out104105# ---------------------------------------------------------------- integrals106# (integrand, interval on which it is continuous). The reference is mpmath quadrature from the first sample point to each of the107# others, so it does not depend on sympy finding the antiderivative; sympy's own antiderivative is stored for the record.108INTEGRALS = [109 ("x^2", DPOS), ("3*x^4 - 2*x + 7", DPOS), ("1/x", DPOS), ("sqrt(x)", DPOS), ("x^(-3)", DPOS), ("1/sqrt(x)", DPOS), ("(2*x+1)^5", DPOS), ("1/(3*x+2)", DPOS),110 ("exp(x)", DPOS), ("exp(-2*x)", DPOS), ("2^x", DPOS), ("sin(x)", DPOS), ("cos(x)", DPOS), ("sin(2*x+1)", DPOS), ("tan(x)", D01), ("cot(x)", DPOS),111 ("sec(x)", D01), ("csc(x)", DPOS), ("sec(x)^2", D01), ("csc(x)^2", DPOS), ("sec(x)*tan(x)", D01), ("sinh(x)", DPOS), ("cosh(x)", DPOS), ("tanh(x)", DPOS),112 ("ln(x)", DPOS), ("x*ln(x)", DPOS), ("x^2*ln(x)", DPOS), ("ln(x)^2", DPOS), ("ln(x)/x", DPOS), ("log(x)", DPOS), ("x*exp(x)", DPOS), ("x^2*exp(x)", DPOS),113 ("x^3*exp(-x)", DPOS), ("x*sin(x)", DPOS), ("x^2*cos(x)", DPOS), ("x*cos(3*x)", DPOS), ("exp(x)*sin(x)", DPOS), ("exp(2*x)*cos(3*x)", DPOS),114 ("sin(x)^2", DPOS), ("cos(x)^2", DPOS), ("sin(x)^3", DPOS), ("cos(x)^4", DPOS), ("tan(x)^2", D01), ("sin(x)*cos(x)", DPOS), ("sin(3*x)*cos(2*x)", DPOS),115 ("sin(x)^3*cos(x)", DPOS), ("2*x*cos(x^2)", DPOS), ("x*exp(-x^2)", DPOS), ("x/(x^2+1)", DPOS), ("x*sqrt(x^2+1)", DPOS), ("cos(x)*exp(sin(x))", DPOS),116 ("exp(x)/(1+exp(x))", DPOS), ("1/(x*ln(x))", DGT1), ("x*sqrt(x+1)", DPOS), ("x/sqrt(x+1)", DPOS), ("1/(x^2+1)", DPOS), ("1/(x^2+4)", DPOS),117 ("1/(x^2-1)", DGT1), ("1/(x^2+2*x+5)", DPOS), ("(2*x+3)/(x^2+2*x+5)", DPOS), ("1/(x*(x+1))", DPOS), ("1/(x*(x+1)^2)", DPOS), ("x^3/(x^2+1)", DPOS),118 ("(x^2+1)/(x^3+3*x)", DPOS), ("1/(x^3+1)", DPOS), ("1/(x^2+1)^2", DPOS), ("(3*x+5)/(x^2-4*x+3)", (3.2, 5.0)), ("1/sqrt(1-x^2)", D01), ("1/sqrt(4-x^2)", D01),119 ("sqrt(4-x^2)", D01), ("1/sqrt(x^2+1)", DPOS), ("sqrt(x^2+1)", DPOS), ("1/sqrt(x^2-1)", DGT1), ("atan(x)", DPOS), ("asin(x)", D01), ("x*atan(x)", DPOS),120 ("acos(x)", D01), ("asinh(x)", DPOS), ("sinh(x)^2", DPOS), ("1/(1+exp(x))", DPOS), ("abs(x)", DPOS), ("(x+1)^2*(x-2)", DPOS), ("1/cos(x)^2", D01),121 ("ln(x^2+1)", DPOS), ("x^2/(x+1)", DPOS), ("exp(x)*x^2*3", DPOS), ("cos(x)^2*sin(x)", DPOS), ("1/(x^2+a^2)", DPOS),122]123124def integrals():125 out = []126 x = LOCALS['x']127 for text, dom in INTEGRALS:128 f = P(text)129 others = sorted(str(s) for s in f.free_symbols if s != x)130 params = {name: round(random.uniform(0.6, 1.6), 6) for name in others}131 fn = sp.lambdify(x, f.subs({LOCALS[k]: v for k, v in params.items()}), 'mpmath')132 xs = sorted(round(random.uniform(*dom), 6) for _ in range(6))133 base = xs[0]134 rows = [{'x': p, 'value': float(mp.quad(fn, [base, p]))} for p in xs[1:]]135 try:136 anti = str(sp.integrate(f, x))137 except Exception as error: # sympy failing is not a problem: the reference is the quadrature138 anti = 'sympy failed: %s' % type(error).__name__139 out.append({'expr': text, 'variable': 'x', 'parameters': params, 'base': base, 'sympy': anti, 'samples': rows})140 return out141142DEFINITE = [143 ("x^2", "0", "1"), ("sin(x)", "0", "pi"), ("1/x", "1", "e"), ("exp(-x)", "0", "inf"), ("1/(1+x^2)", "-inf", "inf"), ("ln(x)", "0", "1"),144 ("1/sqrt(x)", "0", "4"), ("x*exp(x)", "0", "1"), ("1/x^2", "1", "inf"), ("cos(x)^2", "0", "2*pi"), ("exp(-x^2)", "0", "1"), ("sin(x)/x", "0", "1"),145 ("x^x", "1", "2"), ("exp(-x^2)", "-inf", "inf"), ("sqrt(1-x^2)", "-1", "1"), ("1/(x^2+4)", "0", "2"), ("x*sin(x)", "0", "pi"), ("exp(-2*x)*x", "0", "inf"),146 ("tan(x)", "0", "1"), ("1/(x^4+1)", "0", "1"),147]148149def definite():150 out = []151 x = LOCALS['x']152 for text, a, b in DEFINITE:153 f = P(text)154 lo, hi = P(a), P(b)155 fn = sp.lambdify(x, f, 'mpmath')156 value = float(mp.quad(fn, [mp.mpf(sp.N(lo, 30)) if lo.is_finite else (-mp.inf), mp.mpf(sp.N(hi, 30)) if hi.is_finite else mp.inf]))157 try:158 exact = str(sp.integrate(f, (x, lo, hi)))159 except Exception as error:160 exact = 'sympy failed: %s' % type(error).__name__161 out.append({'expr': text, 'from': a, 'to': b, 'value': value, 'sympy': exact})162 return out163164# ---------------------------------------------------------------- simplification corpus165SIMPLIFY = [166 "x + x", "x*x", "x^2*x^3", "2*(x+1)", "x - x", "x/x", "x^0", "x^1", "0*x + y", "1*x", "-(-x)", "x + 0", "3*x - 2*x + x", "x*y - y*x", "2*x*y + 3*y*x",167 "(x^2-1)/(x-1)", "(x^2+2*x+1)/(x+1)", "(2*x+2)/(x+1)", "x*(x+1)/x", "(x^3-x)/(x^2-1)", "1/x + 1/x", "x/2 + x/3", "-x/2 + 3*x/4", "(x+1)/(x+1)^2",168 "sin(x)^2 + cos(x)^2", "2*sin(x)^2 + 2*cos(x)^2", "y*sin(x)^2 + y*cos(x)^2", "cosh(x)^2 - sinh(x)^2", "sin(x)^2 + cos(x)^2 + x", "exp(ln(x))", "ln(exp(x))",169 "ln(1)", "ln(e)", "exp(0)", "sqrt(x)^2", "sqrt(x^2)", "cbrt(x)^3", "(x^2)^(3/2)", "sqrt(8)", "sqrt(12)/2", "sqrt(2)*sqrt(2)", "sqrt(1/2)", "2^(1/2)*2^(3/2)",170 "exp(x)*exp(2*x)", "exp(x)/exp(x)", "exp(x)^2", "1/exp(x)", "exp(2*ln(x))", "x^(1/2)*x^(1/2)", "x^a*x^b", "x^(3/2)/x", "1/sqrt(x)", "x^(-2)*x^5",171 "sin(-x)", "cos(-x)", "tan(-x)", "abs(-x)", "abs(abs(x))", "abs(x)^2", "sign(x)/abs(x)", "sinh(-x)", "atan(-x)", "sin(0)", "cos(0)", "sin(pi)", "cos(pi)",172 "sin(pi/6)", "cos(pi/3)", "tan(pi/4)", "sin(pi/2)", "cos(pi/4)", "asin(1)", "acos(0)", "atan(1)", "log(100)", "log(8, 2)", "log2(32)", "ln(8)/ln(2)",173 "(x+1)^2 - (x+1)^2", "(x+y)^2 - x^2", "x*(y+z) - x*y", "3*(x+2) - 2*(x-1)", "(x+1)*(x-1) - x^2", "a*x^2 + b*x + c - a*x^2", "x^2 + 2*x + 1 + x^2",174 "0.1 + 0.2", "1/3 + 1/6", "2/4", "3 - 5", "2^10", "6/4*x", "(1/2)*(4*x)", "x/(2*x)", "(4*x^2)/(2*x)", "sqrt(4*x^2)", "sqrt(9*x)", "(2*x)^3", "(x*y)^2",175 "(x/y)^2", "(-x)^2", "(-x)^3", "1/(1/x)", "x/(1/y)", "(x^2*y)/(x*y^2)", "x^2/x^2", "e^x*e^(-x)", "ln(x) + ln(x)", "2*ln(x) - ln(x)", "exp(x+1)/e",176 "sin(x)*cos(x) + cos(x)*sin(x)", "tan(x)*cos(x)/cos(x)", "x*sin(x)/x", "(sin(x)+1)^2 - sin(x)^2", "x + y + z - x - y", "x*y*z/(x*y)", "x^2*y^2/(x*y)^2",177 "(1-x)/(x-1)", "(2-2*x)/(x-1)", "(x-1)*(1-x)", "5*x^3 - 3*x^3 + x^2 - x^2", "(x+1)*(x+1)", "(x+1)^3/(x+1)", "x*sqrt(x)", "sqrt(x)/x", "x^2*sqrt(x)/x^(1/2)",178 "pi + pi", "e*e", "2*pi/pi", "i^2", "i^4", "abs(-3)", "abs(x^2)", "abs(exp(x))", "ln(1/x)", "sin(x)/cos(x)*cos(x)", "sec(x)*cos(x)/sec(x)",179]180181def simplify_corpus():182 out = []183 for text in SIMPLIFY:184 f = P(text)185 names = sorted(str(s) for s in f.free_symbols)186 rows = evaluate(f, points(names, (0.35, 1.45), 5 if names else 1))187 if not rows and names:188 raise SystemExit('no real sample for ' + text)189 out.append({'expr': text, 'variables': names, 'samples': rows})190 return out191192# ---------------------------------------------------------------- limits193LIMITS = [194 ("sin(x)/x", "0", "both"), ("(1+1/x)^x", "inf", "both"), ("(x^2-1)/(x-1)", "1", "both"), ("1/x", "0", "right"), ("1/x", "0", "left"), ("1/x^2", "0", "both"),195 ("abs(x)/x", "0", "left"), ("abs(x)/x", "0", "right"), ("(3*x^2+1)/(2*x^2-x)", "inf", "both"), ("x*ln(x)", "0", "right"), ("(1-cos(x))/x^2", "0", "both"),196 ("exp(-x)", "inf", "both"), ("(exp(x)-1)/x", "0", "both"), ("x^x", "0", "right"), ("atan(x)", "-inf", "both"), ("x^3/exp(x)", "inf", "both"),197 ("ln(x)/x", "inf", "both"), ("(x^3+1)/(x+2)", "-inf", "both"), ("x^2", "3", "both"), ("(sin(x)-x)/x^3", "0", "both"), ("(1+2/x)^x", "inf", "both"),198 ("tan(x)/x", "0", "both"), ("(ln(1+x))/x", "0", "both"), ("(x^2+x)/(3*x^2+1)", "-inf", "both"), ("(sqrt(1+x)-1)/x", "0", "both"), ("x*exp(-x)", "inf", "both"),199 ("(cos(x)-1)/x", "0", "both"), ("(x-2)/(x^2-4)", "2", "both"), ("ln(x)", "0", "right"), ("exp(1/x)", "0", "left"), ("(2^x-1)/x", "0", "both"),200 ("sin(3*x)/sin(2*x)", "0", "both"), ("(1+x)^(1/x)", "0", "both"), ("x/(x+1)", "inf", "both"), ("tanh(x)", "inf", "both"), ("(x^2-9)/(x-3)", "3", "both"),201 ("sqrt(x^2+x)-x", "inf", "both"),202]203204def limits():205 out = []206 x = LOCALS['x']207 for text, point, direction in LIMITS:208 f = P(text)209 p = P(point)210 if direction == "both" and p.is_finite:211 right, left = sp.limit(f, x, p, '+'), sp.limit(f, x, p, '-')212 value = right if right == left else None213 else:214 value = sp.limit(f, x, p, '-' if direction == 'left' else '+')215 if value is None: kind, v = 'dne', None216 elif value == sp.oo: kind, v = 'inf', None217 elif value == -sp.oo: kind, v = '-inf', None218 else: kind, v = 'finite', num(value)219 out.append({'expr': text, 'point': point, 'direction': direction, 'kind': kind, 'value': v, 'sympy': str(value)})220 return out221222# ---------------------------------------------------------------- series223SERIES = [("sin(x)", "0", 7), ("cos(x)", "0", 6), ("exp(x)", "0", 6), ("ln(1+x)", "0", 5), ("1/(1-x)", "0", 5), ("sqrt(1+x)", "0", 4), ("tan(x)", "0", 7),224 ("atan(x)", "0", 7), ("ln(x)", "1", 4), ("exp(x)", "1", 3), ("sin(x)", "pi/2", 4), ("1/x", "2", 4), ("exp(x)*sin(x)", "0", 6), ("sinh(x)", "0", 5),225 ("(1+x)^(1/3)", "0", 4), ("exp(-x^2)", "0", 6), ("cos(x)^2", "0", 6), ("asin(x)", "0", 5), ("x/(1+x^2)", "0", 7), ("sqrt(x)", "4", 3)]226227def series():228 out = []229 x = LOCALS['x']230 for text, point, order in SERIES:231 f = P(text)232 a = P(point)233 coefficients, exact = [], []234 d = f235 for k in range(order + 1):236 c = sp.simplify(d.subs(x, a) / sp.factorial(k))237 coefficients.append(num(c)); exact.append(str(c))238 d = sp.diff(d, x)239 out.append({'expr': text, 'point': point, 'order': order, 'coefficients': coefficients, 'sympy': exact})240 return out241242# ---------------------------------------------------------------- equations (real roots)243EQUATIONS = ["2*x+3=7", "x^2-5*x+6=0", "x^2-2*x+1=0", "x^3-6*x^2+11*x-6=0", "x^2=2", "x^4-5*x^2+4=0", "x^2+x-1=0", "6*x^2-5*x+1=0", "x^3-2*x^2-5*x+6=0",244 "x^4-10*x^2+1=0", "x^3-x+1=0", "x^5-x-1=0", "2*x^3+3*x^2-11*x-6=0", "x^4-1=0", "x^3=8", "x^6-7*x^3-8=0", "x^3-3*x+1=0", "x^4+x^3-7*x^2-x+6=0"]245246def equations():247 out = []248 x = LOCALS['x']249 for text in EQUATIONS:250 lhs, rhs = text.split('=')251 poly = sp.Poly(P(lhs) - P(rhs), x)252 roots = sp.roots(poly, x) if poly.degree() <= 4 else {}253 all_roots = [complex(r) for r in poly.nroots(n=20, maxsteps=200)]254 real = sorted(set(round(r.real, 12) for r in all_roots if abs(r.imag) < 1e-12))255 complex_count = len([r for r in all_roots if abs(r.imag) >= 1e-12])256 out.append({'equation': text, 'real_roots': real, 'complex_count': complex_count, 'degree': poly.degree(), 'sympy': str(roots)})257 return out258259# ---------------------------------------------------------------- polynomial algebra (exact)260def coeffs(p, x):261 return [str(c) for c in reversed(sp.Poly(p, x).all_coeffs())] if p != 0 else []262263POLY_PAIRS = [("x^5 - 3*x^3 + x - 7", "x^2 + 2*x - 1"), ("x^4 - 1", "x^2 - 1"), ("2*x^6 + x^3/2 - 1/3", "3*x^2 - x/2 + 2"), ("x^3 - 6*x^2 + 11*x - 6", "x^2 - 3*x + 2"),264 ("x^7 + 1", "x^3 + x + 1"), ("(x-1)^3*(x+2)^2", "(x-1)*(x+2)^3"), ("x^4 + 4", "x^2 + 2*x + 2"), ("6*x^4 - 5*x^3 + x^2", "2*x^3 - x^2")]265FACTOR = ["x^2 - 1", "x^4 - 5*x^2 + 4", "2*x^3 + 4*x^2 + 2*x", "x^4 + 4", "x^2 + 1", "x^5 - 1", "6*x^2 - 5*x + 1", "x^6 - 1", "x^4 + x^2 + 1", "(x-1)^3*(x+2)^2",266 "x^3 - 2", "x^8 - 1", "3*x^3 - 3*x/4", "x^5 + x + 1", "x^4 - 10*x^2 + 1", "x^6 + x^5 + x^4 + x^3 + x^2 + x + 1", "12*x^4 - 4*x^3 - 5*x^2 + x", "x^4 - 4*x^3 + 6*x^2 - 4*x + 1"]267268def polynomials():269 x = LOCALS['x']270 division, factor = [], []271 for a, b in POLY_PAIRS:272 pa, pb = sp.expand(P(a)), sp.expand(P(b))273 q, r = sp.div(pa, pb, x)274 g = sp.Poly(sp.gcd(pa, pb), x)275 g = sp.Poly(g.primitive()[1].as_expr() * (1 if g.LC() > 0 else -1), x)276 division.append({'a': a, 'b': b, 'quotient': coeffs(q, x), 'remainder': coeffs(r, x), 'gcd': coeffs(g.as_expr(), x)})277 for text in FACTOR:278 p = sp.expand(P(text))279 content, parts = sp.factor_list(p, x)280 factor.append({'expr': text, 'content': str(content), 'factors': sorted([[coeffs(f, x), m] for f, m in parts], key=str),281 'count': sum(m for _, m in parts), 'sympy': str(sp.factor(p))})282 return {'division': division, 'factor': factor}283284SUMS = [("k", "1", "n"), ("k^2", "1", "n"), ("k^3", "1", "n"), ("2^k", "0", "n"), ("3*k+1", "0", "n"), ("k^4", "1", "n"), ("k*(k+1)", "1", "n"), ("(1/3)^k", "2", "n")]285286def sums():287 out = []288 k, n = LOCALS['k'], LOCALS['n']289 for term, lo, hi in SUMS:290 s = sp.summation(P(term), (k, P(lo), n))291 rows = [{'n': m, 'value': num(s.subs(n, m))} for m in (3, 7, 12, 20)]292 out.append({'term': term, 'from': lo, 'to': hi, 'sympy': str(sp.factor(s)), 'samples': rows})293 return out294295def main():296 data = {297 'generator': 'sympy %s, mpmath %s' % (sp.__version__, mp.__version__),298 'derivatives': derivatives(), 'integrals': integrals(), 'definite': definite(), 'simplify': simplify_corpus(),299 'limits': limits(), 'series': series(), 'equations': equations(), 'polynomials': polynomials(), 'sums': sums(),300 }301 path = os.path.join(HERE, 'reference.json')302 with open(path, 'w') as f:303 json.dump(data, f, indent=1, sort_keys=True)304 target = os.path.join(HERE, '..', 'Tests', 'ORIXSymbolicTests', 'Resources', 'reference.json')305 shutil.copyfile(path, target)306 for key, value in data.items():307 if isinstance(value, list): print('%-12s %d cases' % (key, len(value)))308 print('polynomials %d divisions, %d factorisations' % (len(data['polynomials']['division']), len(data['polynomials']['factor'])))309310if __name__ == '__main__':311 main()312