Best Python code snippet using lisa_python
test_autowrap.py
Source:test_autowrap.py  
...24A, B, C = symbols('A B C', cls=IndexedBase)25i = Idx('i', m)26j = Idx('j', n)27k = Idx('k', d)28def has_module(module):29    """30    Return True if module exists, otherwise run skip().31    module should be a string.32    """33    # To give a string of the module name to skip(), this function takes a34    # string.  So we don't waste time running import_module() more than once,35    # just map the three modules tested here in this dict.36    modnames = {'numpy': numpy, 'Cython': Cython, 'f2py': f2py}37    if modnames[module]:38        if module == 'f2py' and not f2pyworks:39            skip("Couldn't run f2py.")40        return True41    skip("Couldn't import %s." % module)42#43# test runners used by several language-backend combinations44#45def runtest_autowrap_twice(language, backend):46    f = autowrap((((a + b)/c)**5).expand(), language, backend)47    g = autowrap((((a + b)/c)**4).expand(), language, backend)48    # check that autowrap updates the module name.  Else, g gives the same as f49    assert f(1, -2, 1) == -1.050    assert g(1, -2, 1) == 1.051def runtest_autowrap_trace(language, backend):52    has_module('numpy')53    trace = autowrap(A[i, i], language, backend)54    assert trace(numpy.eye(100)) == 10055def runtest_autowrap_matrix_vector(language, backend):56    has_module('numpy')57    x, y = symbols('x y', cls=IndexedBase)58    expr = Eq(y[i], A[i, j]*x[j])59    mv = autowrap(expr, language, backend)60    # compare with numpy's dot product61    M = numpy.random.rand(10, 20)62    x = numpy.random.rand(20)63    y = numpy.dot(M, x)64    assert numpy.sum(numpy.abs(y - mv(M, x))) < 1e-1365def runtest_autowrap_matrix_matrix(language, backend):66    has_module('numpy')67    expr = Eq(C[i, j], A[i, k]*B[k, j])68    matmat = autowrap(expr, language, backend)69    # compare with numpy's dot product70    M1 = numpy.random.rand(10, 20)71    M2 = numpy.random.rand(20, 15)72    M3 = numpy.dot(M1, M2)73    assert numpy.sum(numpy.abs(M3 - matmat(M1, M2))) < 1e-1374def runtest_ufuncify(language, backend):75    has_module('numpy')76    a, b, c = symbols('a b c')77    fabc = ufuncify([a, b, c], a*b + c, backend=backend)78    facb = ufuncify([a, c, b], a*b + c, backend=backend)79    grid = numpy.linspace(-2, 2, 50)80    b = numpy.linspace(-5, 4, 50)81    c = numpy.linspace(-1, 1, 50)82    expected = grid*b + c83    numpy.testing.assert_allclose(fabc(grid, b, c), expected)84    numpy.testing.assert_allclose(facb(grid, c, b), expected)85def runtest_issue_10274(language, backend):86    expr = (a - b + c)**(13)87    tmp = tempfile.mkdtemp()88    f = autowrap(expr, language, backend, tempdir=tmp, helpers=('helper', a - b + c, (a, b, c)))89    assert f(1, 1, 1) == 190    for file in os.listdir(tmp):91        if file.startswith("wrapped_code_") and file.endswith(".c"):92            fil = open(tmp + '/' + file)93            lines = fil.readlines()94            assert lines[0] == "/******************************************************************************\n"95            assert "Code generated with sympy " + sympy.__version__ in lines[1]96            assert lines[2:] == [97                " *                                                                            *\n",98                " *              See http://www.sympy.org/ for more information.               *\n",99                " *                                                                            *\n",100                " *                      This file is part of 'autowrap'                       *\n",101                " ******************************************************************************/\n",102                "#include " + '"' + file[:-1]+ 'h"' + "\n",103                "#include <math.h>\n",104                "\n",105                "double helper(double a, double b, double c) {\n",106                "\n",107                "   double helper_result;\n",108                "   helper_result = a - b + c;\n",109                "   return helper_result;\n",110                "\n",111                "}\n",112                "\n",113                "double autofunc(double a, double b, double c) {\n",114                "\n",115                "   double autofunc_result;\n",116                "   autofunc_result = pow(helper(a, b, c), 13);\n",117                "   return autofunc_result;\n",118                "\n",119                "}\n",120                ]121#122# tests of language-backend combinations123#124# f2py125def test_wrap_twice_f95_f2py():126    has_module('f2py')127    runtest_autowrap_twice('f95', 'f2py')128def test_autowrap_trace_f95_f2py():129    has_module('f2py')130    runtest_autowrap_trace('f95', 'f2py')131def test_autowrap_matrix_vector_f95_f2py():132    has_module('f2py')133    runtest_autowrap_matrix_vector('f95', 'f2py')134def test_autowrap_matrix_matrix_f95_f2py():135    has_module('f2py')136    runtest_autowrap_matrix_matrix('f95', 'f2py')137def test_ufuncify_f95_f2py():138    has_module('f2py')139    runtest_ufuncify('f95', 'f2py')140# Cython141def test_wrap_twice_c_cython():142    has_module('Cython')143    with warnings.catch_warnings():144        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)145        runtest_autowrap_twice('C', 'cython')146def test_autowrap_trace_C_Cython():147    has_module('Cython')148    runtest_autowrap_trace('C99', 'cython')149def test_autowrap_matrix_vector_C_cython():150    has_module('Cython')151    runtest_autowrap_matrix_vector('C99', 'cython')152def test_autowrap_matrix_matrix_C_cython():153    has_module('Cython')154    runtest_autowrap_matrix_matrix('C99', 'cython')155def test_ufuncify_C_Cython():156    has_module('Cython')157    with warnings.catch_warnings():158        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)159        runtest_ufuncify('C99', 'cython')160def test_issue_10274_C_cython():161    has_module('Cython')162    runtest_issue_10274('C89', 'cython')163# Numpy164def test_ufuncify_numpy():165    # This test doesn't use Cython, but if Cython works, then there is a valid166    # C compiler, which is needed.167    has_module('Cython')168    with warnings.catch_warnings():169        warnings.filterwarnings("ignore", category=SymPyDeprecationWarning)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
