Exemple #1
0
def generate_test_file():
    '''
    This function is assuming the name of file containing the fullform is test_1.m.
    It can be changes as per use.

    For more details, see
    `https://github.com/sympy/sympy/wiki/Rubi-parsing-guide#parsing-tests`
    '''
    res = []
    file_name = 'test_1.m'
    with open(file_name, 'r') as myfile:
        fullform = myfile.read().replace('\n', '')
    fullform = fullform.replace('$VersionNumber', 'version_number')
    fullform = fullform.replace('Defer[Int][', 'Integrate[')
    path_header = os.path.dirname(
        os.path.abspath(inspect.getfile(inspect.currentframe())))
    h = open(os.path.join(path_header, "header.py.txt"), "r").read()
    header = "import sys\nfrom sympy.external import import_module\nmatchpy = import_module({})".format(
        '\"matchpy\"')
    header += "\nif not matchpy:\n    disabled = True\n"
    header += "if sys.version_info[:2] < (3, 6):\n    disabled = True\n"
    header += "\n".join(h.split("\n")[8:-9])
    header += "from sympy.integrals.rubi.rubi import rubi_integrate\n"
    header += "from sympy import Integral as Integrate, exp, log\n"
    header += "\na, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = symbols('a b c d e f g h i j k l m n o p q r s t u v w x y z')"
    header += "\nA, B, C, F, G, H, J, K, L, M, N, O, P, Q, R, T, U, V, W, X, Y, Z = symbols('A B C F G H J K L M N O P Q R T U V W X Y Z')"
    header += "\n\ndef {}():\n".format(file_name[0:-2])
    s = parse_full_form(fullform)
    tests = []
    for i in s:
        res[:] = []
        if i[0] == 'HoldComplete':
            ss = sympify(generate_sympy_from_parsed(i[1]),
                         locals={
                             'version_number': 11,
                             'If': If
                         })
            ss = List(*ss.args)
            tests.append(ss)

    t = ''
    for a in tests:
        if len(a) == 5:
            r = 'rubi_integrate({}, x)'.format(rubi_sstr(a[0]))
            t += '\n    assert rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True) or rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True)'.format(
                r, rubi_sstr(a[1]), rubi_sstr(a[3]), r, rubi_sstr(a[1]),
                rubi_sstr(a[4]))
        else:
            r = 'rubi_integrate({}, x)'.format(rubi_sstr(a[0]))
            t += '\n    assert rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True)'.format(
                r, rubi_sstr(a[1]), rubi_sstr(a[3]))
    t = header + t + '\n'
    test = open('parsed_tests.py', 'w')
    test.write((t))
    test.close()
def generate_rules_from_downvalues():
    '''
    This function generate rules and saves in file. For more details,
    see `sympy/integrals/rubi/parsetools/rubi_parsing_guide.md` in `parsetools`.
    '''
    cons_dict = {}
    cons_index =0
    index = 0
    cons = ''
    input = ["Integrand_simplification.txt", "Linear_products.txt", "Quadratic_products.txt", "Binomial_products.txt",
        "Trinomial_products.txt", "Miscellaneous_algebra.txt", "Piecewise_linear.txt", "Exponentials.txt", "Logarithms.txt",
        "Sine.txt", "Tangent.txt", "Secant.txt", "Miscellaneous_trig.txt", "Inverse_trig.txt", "Hyperbolic.txt",
        "Inverse_hyperbolic.txt", "Special_functions.txt", "Miscellaneous_integration.txt"]

    output =['integrand_simplification.py', 'linear_products.py', 'quadratic_products.py', 'binomial_products.py', 'trinomial_products.py',
        'miscellaneous_algebraic.py' ,'piecewise_linear.py', 'exponential.py', 'logarithms.py', 'sine.py', 'tangent.py', 'secant.py', 'miscellaneous_trig.py',
        'inverse_trig.py', 'hyperbolic.py', 'inverse_hyperbolic.py', 'special_functions.py', 'miscellaneous_integration.py']

    for k in range(0, 18):
        module_name = output[k][0:-3]
        path_header = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
        header = open(os.path.join(path_header, "header.py.txt"), "r").read()
        header = header.format(module_name)
        with open(input[k], 'r') as myfile:
            fullform =myfile.read().replace('\n', '')
        for i in temporary_variable_replacement:
            fullform = fullform.replace(i, temporary_variable_replacement[i])
        # Permanently rename these variables
        for i in permanent_variable_replacement:
            fullform = fullform.replace(i, permanent_variable_replacement[i])

        rules = []
        for i in parse_full_form(fullform): # separate all rules
            if i[0] == 'RuleDelayed':
                rules.append(i)
        parsed = downvalues_rules(rules, header, cons_dict, cons_index, index)
        result = parsed[0].strip() + '\n'
        cons_index = parsed[1]
        cons += parsed[2]
        index = parsed[3]
        # Replace temporary variables by actual values
        for i in temporary_variable_replacement:
            cons = cons.replace(temporary_variable_replacement[i], i)
            result = result.replace(temporary_variable_replacement[i], i)

        file = open(output[k],'w')
        file.write(str(result))
        file.close()

    cons = "\n".join(header.split("\n")[:-2])+ '\n' + cons
    constraints = open('constraints.py', 'w')
    constraints.write(str(cons))
    constraints.close()
Exemple #3
0
def generate_test_file():
    '''
    This function is assuming the name of file containing the fullform is test_1.m.
    It can be changes as per use.
    See `rubi_parsing_guide.md` in `parsetools` for more details.
    '''
    res =[]
    file_name = 'test_1.m'
    with open(file_name, 'r') as myfile:
        fullform =myfile.read().replace('\n', '')
    fullform = fullform.replace('$VersionNumber', 'version_number')
    fullform = fullform.replace('Defer[Int][', 'Integrate[')
    path_header = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    h = open(os.path.join(path_header, "header.py.txt"), "r").read()
    header = "import sys\nfrom sympy.external import import_module\nmatchpy = import_module({})".format('\"matchpy\"')
    header += "\nif not matchpy:\n    disabled = True\n"
    header += "if sys.version_info[:2] < (3, 6):\n    disabled = True\n"
    header += "\n".join(h.split("\n")[8:-9])
    header += "from sympy.integrals.rubi.rubi import rubi_integrate\n"
    header += "from sympy import Integral as Integrate, exp, log\n"
    header += "\na, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z = symbols('a b c d e f g h i j k l m n o p q r s t u v w x y z')"
    header += "\nA, B, C, F, G, H, J, K, L, M, N, O, P, Q, R, T, U, V, W, X, Y, Z = symbols('A B C F G H J K L M N O P Q R T U V W X Y Z')"
    header += "\n\ndef {}():\n".format(file_name[0:-2])
    s = parse_full_form(fullform)
    tests = []
    for i in s:
        res[:] = []
        if i[0] == 'HoldComplete':
            ss = sympify(generate_sympy_from_parsed(i[1]), locals = { 'version_number' : 11, 'If' : If})
            ss = List(*ss.args)
            tests.append(ss)

    t = ''
    for a in tests:
        if len(a) == 5:
            r = 'rubi_integrate({}, x)'.format(rubi_sstr(a[0]))
            t += '\n    assert rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True) or rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True)'.format(r, rubi_sstr(a[1]), rubi_sstr(a[3]), r, rubi_sstr(a[1]),rubi_sstr(a[4]))
        else:
            r = 'rubi_integrate({}, x)'.format(rubi_sstr(a[0]))
            t += '\n    assert rubi_test({}, {}, {}, expand=True, _diff=True, _numerical=True)'.format(r, rubi_sstr(a[1]), rubi_sstr(a[3]))
    t = header+t+'\n'
    test = open('parsed_tests.py', 'w')
    test.write((t))
    test.close()
Exemple #4
0
def generate_rules_from_downvalues():
    """
    This function generate rules and saves in file. For more details,
    see `https://github.com/sympy/sympy/wiki/Rubi-parsing-guide`
    """
    cons_dict = {}
    cons_index = 0
    index = 0
    cons = ""
    input = [
        "Integrand_simplification.txt",
        "Linear_products.txt",
        "Quadratic_products.txt",
        "Binomial_products.txt",
        "Trinomial_products.txt",
        "Miscellaneous_algebra.txt",
        "Piecewise_linear.txt",
        "Exponentials.txt",
        "Logarithms.txt",
        "Sine.txt",
        "Tangent.txt",
        "Secant.txt",
        "Miscellaneous_trig.txt",
        "Inverse_trig.txt",
        "Hyperbolic.txt",
        "Inverse_hyperbolic.txt",
        "Special_functions.txt",
        "Miscellaneous_integration.txt",
    ]

    output = [
        "integrand_simplification.py",
        "linear_products.py",
        "quadratic_products.py",
        "binomial_products.py",
        "trinomial_products.py",
        "miscellaneous_algebraic.py",
        "piecewise_linear.py",
        "exponential.py",
        "logarithms.py",
        "sine.py",
        "tangent.py",
        "secant.py",
        "miscellaneous_trig.py",
        "inverse_trig.py",
        "hyperbolic.py",
        "inverse_hyperbolic.py",
        "special_functions.py",
        "miscellaneous_integration.py",
    ]

    for k in range(0, 18):
        module_name = output[k][0:-3]
        path_header = os.path.dirname(
            os.path.abspath(inspect.getfile(inspect.currentframe()))
        )
        header = open(os.path.join(path_header, "header.py.txt"), "r").read()
        header = header.format(module_name)
        with open(input[k], "r") as myfile:
            fullform = myfile.read().replace("\n", "")
        for i in temporary_variable_replacement:
            fullform = fullform.replace(i, temporary_variable_replacement[i])
        # Permanently rename these variables
        for i in permanent_variable_replacement:
            fullform = fullform.replace(i, permanent_variable_replacement[i])

        rules = []
        for i in parse_full_form(fullform):  # separate all rules
            if i[0] == "RuleDelayed":
                rules.append(i)
        parsed = downvalues_rules(rules, header, cons_dict, cons_index, index)
        result = parsed[0].strip() + "\n"
        cons_index = parsed[1]
        cons += parsed[2]
        index = parsed[3]
        # Replace temporary variables by actual values
        for i in temporary_variable_replacement:
            cons = cons.replace(temporary_variable_replacement[i], i)
            result = result.replace(temporary_variable_replacement[i], i)

        file = open(output[k], "w")
        file.write(str(result))
        file.close()

    cons = "\n".join(header.split("\n")[:-2]) + "\n" + cons
    constraints = open("constraints.py", "w")
    constraints.write(str(cons))
    constraints.close()