def test_ccode_loops_add(): from sympy.tensor import IndexedBase, Idx from sympy import symbols n, m = symbols('n m', integer=True) A = IndexedBase('A') x = IndexedBase('x') y = IndexedBase('y') z = IndexedBase('z') i = Idx('i', m) j = Idx('j', n) s = ( 'for (int i=0; i<m; i++){\n' ' y[i] = x[i] + z[i];\n' '}\n' 'for (int i=0; i<m; i++){\n' ' for (int j=0; j<n; j++){\n' ' y[i] = A[%s]*x[j] + y[i];\n' % (i*n + j) +\ ' }\n' '}' ) assert ccode(A[i, j] * x[j] + x[i] + z[i], assign_to=y[i]) == s
def test_glsl_code_loops_add(): from sympy.tensor import IndexedBase, Idx from sympy import symbols n, m = symbols("n m", integer=True) A = IndexedBase("A") x = IndexedBase("x") y = IndexedBase("y") z = IndexedBase("z") i = Idx("i", m) j = Idx("j", n) s = ("for (int i=0; i<m; i++){\n" " y[i] = x[i] + z[i];\n" "}\n" "for (int i=0; i<m; i++){\n" " for (int j=0; j<n; j++){\n" " y[i] = A[n*i + j]*x[j] + y[i];\n" " }\n" "}") c = glsl_code(A[i, j] * x[j] + x[i] + z[i], assign_to=y[i]) assert c == s
def test_glsl_code_inline_function(): x = symbols("x") g = implemented_function("g", Lambda(x, 2 * x)) assert glsl_code(g(x)) == "2*x" g = implemented_function("g", Lambda(x, 2 * x / Catalan)) assert glsl_code(g(x)) == "float Catalan = 0.915965594;\n2*x/Catalan" A = IndexedBase("A") i = Idx("i", symbols("n", integer=True)) g = implemented_function("g", Lambda(x, x * (1 + x) * (2 + x))) assert glsl_code(g( A[i]), assign_to=A[i]) == ("for (int i=0; i<n; i++){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}")
def test_dummy_loops(): i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ( 'for (int i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n' ' y[i_%(icount)i] = x[i_%(icount)i];\n' '}' ) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index} code = ccode(x[i], assign_to=y[i]) assert code == expected
def test_jscode_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2 * x)) assert jscode(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2 * x / Catalan)) assert jscode(g(x)) == "var Catalan = %s;\n2*x/Catalan" % Catalan.n() A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x * (1 + x) * (2 + x))) assert jscode(g(A[i]), assign_to=A[i]) == ("for (var i=0; i<n; i++){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}")
def test_inline_function(): from sympy.tensor import IndexedBase, Idx from sympy import symbols n, m = symbols('n m', integer=True) A, x, y = map(IndexedBase, 'Axy') i = Idx('i', m) j = Idx('j', n) p = FCodeGen() func = implemented_function('func', Lambda(n, n * (n + 1))) routine = Routine('test_inline', Eq(y[i], func(x[i]))) code = get_string(p.dump_f95, [routine]) expected = ('subroutine test_inline(m, x, y)\n' 'implicit none\n' 'INTEGER*4, intent(in) :: m\n' 'REAL*8, intent(in), dimension(1:m) :: x\n' 'REAL*8, intent(out), dimension(1:m) :: y\n' 'INTEGER*4 :: i\n' 'do i = 1, m\n' ' y(i) = (1 + x(i))*x(i)\n' 'end do\n' 'end subroutine\n') assert code == expected
def test_Idx_func_args(): i, a, b = symbols('i a b', integer=True) ii = Idx(i) assert ii.func(*ii.args) == ii ii = Idx(i, a) assert ii.func(*ii.args) == ii ii = Idx(i, (a, b)) assert ii.func(*ii.args) == ii
def test_dummy_loops(): i, m = symbols("i m", integer=True, cls=Dummy) x = IndexedBase("x") y = IndexedBase("y") i = Idx(i, m) expected = ("do i_%(icount)i = 1, m_%(mcount)i\n" " y(i_%(icount)i) = x(i_%(icount)i)\n" "end do") % { "icount": i.label.dummy_index, "mcount": m.dummy_index } code = fcode(x[i], assign_to=y[i], source_format="free") assert code == expected
def test_ccode_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2 * x)) assert ccode(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2 * x / Catalan)) assert ccode( g(x)) == "double const Catalan = %s;\n2*x/Catalan" % Catalan.n() A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x * (1 + x) * (2 + x))) assert ccode(g(A[i]), assign_to=A[i]) == ("for (int i=0; i<n; i++){\n" " A[i] = A[i]*(1 + A[i])*(2 + A[i]);\n" "}")
def test_rcode_loops_add(): from sympy.tensor import IndexedBase, Idx from sympy import symbols n, m = symbols('n m', integer=True) A = IndexedBase('A') x = IndexedBase('x') y = IndexedBase('y') z = IndexedBase('z') i = Idx('i', m) j = Idx('j', n) s = ( 'for (i in 1:m){\n' ' y[i] = x[i] + z[i];\n' '}\n' 'for (i in 1:m){\n' ' for (j in 1:n){\n' ' y[i] = A[i, j]*x[j] + y[i];\n' ' }\n' '}' ) c = rcode(A[i, j]*x[j] + x[i] + z[i], assign_to=y[i]) assert c == s
def test_dummy_loops(): i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ('do i_%(icount)i = 1, m_%(mcount)i\n' ' y(i_%(icount)i) = x(i_%(icount)i)\n' 'end do') % { 'icount': i.label.dummy_index, 'mcount': m.dummy_index } code = fcode(x[i], assign_to=y[i], source_format='free') assert code == expected
def test_loops_multiple_contractions(): n, m, o, p = symbols('n m o p', integer=True) a = IndexedBase('a') b = IndexedBase('b') y = IndexedBase('y') i = Idx('i', m) j = Idx('j', n) k = Idx('k', o) l = Idx('l', p) assert rust_code(b[j, k, l]*a[i, j, k, l], assign_to=y[i]) == ( "for i in 0..m {\n" " y[i] = 0;\n" "}\n" "for i in 0..m {\n" " for j in 0..n {\n" " for k in 0..o {\n" " for l in 0..p {\n" " y[i] = a[%s]*b[%s] + y[i];\n" % (i*n*o*p + j*o*p + k*p + l, j*o*p + k*p + l) +\ " }\n" " }\n" " }\n" "}")
def test_loops(): n, m = symbols('n,m', integer=True) A = IndexedBase('A') x = IndexedBase('x') y = IndexedBase('y') i = Idx('i', m) j = Idx('j', n) expected = ('do i = 1, m\n' ' y(i) = 0\n' 'end do\n' 'do i = 1, m\n' ' do j = 1, n\n' ' y(i) = %(rhs)s\n' ' end do\n' 'end do') code = fcode(A[i, j] * x[j], assign_to=y[i], source_format='free') assert (code == expected % { 'rhs': 'y(i) + A(i, j)*x(j)' } or code == expected % { 'rhs': 'y(i) + x(j)*A(i, j)' })
def test_rcode_loops_multiple_contractions(): n, m, o, p = symbols('n m o p', integer=True) a = IndexedBase('a') b = IndexedBase('b') y = IndexedBase('y') i = Idx('i', m) j = Idx('j', n) k = Idx('k', o) l = Idx('l', p) s = ('for (i in 1:m){\n' ' y[i] = 0;\n' '}\n' 'for (i in 1:m){\n' ' for (j in 1:n){\n' ' for (k in 1:o){\n' ' for (l in 1:p){\n' ' y[i] = a[i, j, k, l]*b[j, k, l] + y[i];\n' ' }\n' ' }\n' ' }\n' '}') c = rcode(b[j, k, l] * a[i, j, k, l], assign_to=y[i]) assert c == s
def test_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2 * x)) assert fcode(g(x)) == " 2*x" g = implemented_function('g', Lambda(x, 2 * pi / x)) assert fcode(g(x)) == (" parameter (pi = 3.14159265358979d0)\n" " 2*pi/x") A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x * (1 + x) * (2 + x))) assert fcode( g(A[i]), assign_to=A[i]) == (" do i = 1, n\n" " A(i) = (1 + A(i))*(2 + A(i))*A(i)\n" " end do")
def test_inline_function(): x = symbols("x") g = implemented_function("g", Lambda(x, 2 * x)) assert fcode(g(x)) == " 2*x" g = implemented_function("g", Lambda(x, 2 * pi / x)) assert fcode(g(x)) == (" parameter (pi = %sd0)\n" " 2*pi/x") % pi.evalf(17) A = IndexedBase("A") i = Idx("i", symbols("n", integer=True)) g = implemented_function("g", Lambda(x, x * (1 + x) * (2 + x))) assert fcode( g(A[i]), assign_to=A[i]) == (" do i = 1, n\n" " A(i) = (A(i) + 1)*(A(i) + 2)*A(i)\n" " end do")
def test_dummy_loops(): i, m = symbols("i m", integer=True, cls=Dummy) x = IndexedBase("x") y = IndexedBase("y") i = Idx(i, m) expected = ( "for (var i_%(icount)i=0; i_%(icount)i<m_%(mcount)i; i_%(icount)i++){\n" " y[i_%(icount)i] = x[i_%(icount)i];\n" "}") % { "icount": i.label.dummy_index, "mcount": m.dummy_index } code = jscode(x[i], assign_to=y[i]) assert code == expected
def test_jl_tensor_loops_multiple_contractions(): # see comments in previous test about vectorizing from sympy.tensor import IndexedBase, Idx from sympy import symbols n, m, o, p = symbols("n m o p", integer=True) A = IndexedBase("A") B = IndexedBase("B") y = IndexedBase("y") i = Idx("i", m) j = Idx("j", n) k = Idx("k", o) l = Idx("l", p) (result, ) = codegen( ("tensorthing", Eq(y[i], B[j, k, l] * A[i, j, k, l])), "Julia", header=False, empty=False, ) source = result[1] expected = ("function tensorthing(y, A, B, m, n, o, p)\n" " for i = 1:m\n" " y[i] = 0\n" " end\n" " for i = 1:m\n" " for j = 1:n\n" " for k = 1:o\n" " for l = 1:p\n" " y[i] = A[i,j,k,l].*B[j,k,l] + y[i]\n" " end\n" " end\n" " end\n" " end\n" " return y\n" "end\n") assert source == expected
def test_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2*x)) assert rust_code(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2*x/Catalan)) assert rust_code(g(x)) == ( "const Catalan: f64 = %s;\n2*x/Catalan" % Catalan.evalf(17)) A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x))) assert rust_code(g(A[i]), assign_to=A[i]) == ( "for i in 0..n {\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}")
def test_dummy_loops(): # the following line could also be # [Dummy(s, integer=True) for s in 'im'] # or [Dummy(integer=True) for s in 'im'] i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ( 'for (i_%(icount)i in 1:m_%(mcount)i){\n' ' y[i_%(icount)i] = x[i_%(icount)i];\n' '}' ) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index} code = rcode(x[i], assign_to=y[i]) assert code == expected
def test_dummy_loops(): # the following line could also be # [Dummy(s, integer=True) for s in 'im'] # or [Dummy(integer=True) for s in 'im'] i, m = symbols("i m", integer=True, cls=Dummy) x = IndexedBase("x") y = IndexedBase("y") i = Idx(i, m) expected = ("for (i_%(icount)i in 1:m_%(mcount)i){\n" " y[i_%(icount)i] = x[i_%(icount)i];\n" "}") % { "icount": i.label.dummy_index, "mcount": m.dummy_index } code = rcode(x[i], assign_to=y[i]) assert code == expected
def test_rcode_inline_function(): x = symbols('x') g = implemented_function('g', Lambda(x, 2*x)) assert rcode(g(x)) == "2*x" g = implemented_function('g', Lambda(x, 2*x/Catalan)) assert rcode( g(x)) == "Catalan = %s;\n2*x/Catalan" % Catalan.n() A = IndexedBase('A') i = Idx('i', symbols('n', integer=True)) g = implemented_function('g', Lambda(x, x*(1 + x)*(2 + x))) res=rcode(g(A[i]), assign_to=A[i]) ref=( "for (i in 1:n){\n" " A[i] = (A[i] + 1)*(A[i] + 2)*A[i];\n" "}" ) assert res == ref
def test_Routine_argument_order(): a, x, y, z = symbols('a x y z') expr = (x+y)*z raises(CodeGenArgumentListError, 'Routine("test", expr, argument_sequence=[z, x])') raises(CodeGenArgumentListError, 'Routine("test", Eq(a, expr), argument_sequence=[z, x, y])') r = Routine('test', Eq(a, expr), argument_sequence=[z, x, a, y]) assert [ arg.name for arg in r.arguments ] == [z, x, a, y] assert [ type(arg) for arg in r.arguments ] == [ InputArgument, InputArgument, OutputArgument, InputArgument ] r = Routine('test', Eq(z, expr), argument_sequence=[z, x, y]) assert [ type(arg) for arg in r.arguments ] == [ InOutArgument, InputArgument, InputArgument ] from sympy.tensor import IndexedBase, Idx A, B = map(IndexedBase, ['A', 'B']) m = symbols('m', integer=True) i = Idx('i', m) r = Routine('test', Eq(A[i], B[i]), argument_sequence=[B, A, m]) assert [ arg.name for arg in r.arguments ] == [B.label, A.label, m]
def test_ccode_Indexed(): from sympy.tensor import IndexedBase, Idx from sympy import symbols i, j, k, n, m, o = symbols('i j k n m o', integer=True) p = CCodePrinter() p._not_c = set() x = IndexedBase('x')[Idx(j, n)] assert p._print_Indexed(x) == 'x[j]' A = IndexedBase('A')[Idx(i, m), Idx(j, n)] assert p._print_Indexed(A) == 'A[%s]' % str(j + n * i) B = IndexedBase('B')[Idx(i, m), Idx(j, n), Idx(k, o)] assert p._print_Indexed(B) == 'B[%s]' % str(k + i * n * o + j * o) assert p._not_c == set()
def test_dummy_loops_f95(): from sympy.tensor import IndexedBase, Idx i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ( 'subroutine test_dummies(m_%(mcount)i, x, y)\n' 'implicit none\n' 'INTEGER*4, intent(in) :: m_%(mcount)i\n' 'REAL*8, intent(in), dimension(1:m_%(mcount)i) :: x\n' 'REAL*8, intent(out), dimension(1:m_%(mcount)i) :: y\n' 'INTEGER*4 :: i_%(icount)i\n' 'do i_%(icount)i = 1, m_%(mcount)i\n' ' y(i_%(icount)i) = x(i_%(icount)i)\n' 'end do\n' 'end subroutine\n' ) % {'icount': i.label.dummy_index, 'mcount': m.dummy_index} r = make_routine('test_dummies', Eq(y[i], x[i])) c = FCodeGen() code = get_string(c.dump_f95, [r]) assert code == expected
def test_dummy_loops_c(): from sympy.tensor import IndexedBase, Idx i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ( '#include "file.h"\n' '#include <math.h>\n' 'void test_dummies(int m_%(mno)i, double *x, double *y) {\n' ' for (int i_%(ino)i=0; i_%(ino)i<m_%(mno)i; i_%(ino)i++){\n' ' y[i_%(ino)i] = x[i_%(ino)i];\n' ' }\n' '}\n' ) % {'ino': i.label.dummy_index, 'mno': m.dummy_index} r = make_routine('test_dummies', Eq(y[i], x[i])) c89 = C89CodeGen() c99 = C99CodeGen() code = get_string(c99.dump_c, [r]) assert code == expected with raises(NotImplementedError): get_string(c89.dump_c, [r])
def test_dummy_loops_c(): from sympy.tensor import IndexedBase, Idx # the following line could also be # [Dummy(s, integer=True) for s in 'im'] # or [Dummy(integer=True) for s in 'im'] i, m = symbols('i m', integer=True, cls=Dummy) x = IndexedBase('x') y = IndexedBase('y') i = Idx(i, m) expected = ( '#include "file.h"\n' '#include <math.h>\n' 'void test_dummies(int m_%(mno)i, double *x, double *y) {\n' ' for (int i_%(ino)i=0; i_%(ino)i<m_%(mno)i; i_%(ino)i++){\n' ' y[i_%(ino)i] = x[i_%(ino)i];\n' ' }\n' '}\n' ) % {'ino': i.label.dummy_index, 'mno': m.dummy_index} r = Routine('test_dummies', Eq(y[i], x[i])) c = CCodeGen() code = get_string(c.dump_c, [r]) assert code == expected
Cython = import_module('Cython', min_module_version='0.15.1') f2py = import_module('numpy.f2py', __import__kwargs={'fromlist': ['f2py']}) f2pyworks = False if f2py: try: autowrap(symbols('x'), 'f95', 'f2py') except (CodeWrapError, ImportError, OSError): f2pyworks = False else: f2pyworks = True a, b, c = symbols('a b c') n, m, d = symbols('n m d', integer=True) A, B, C = symbols('A B C', cls=IndexedBase) i = Idx('i', m) j = Idx('j', n) k = Idx('k', d) def has_module(module): """ Return True if module exists, otherwise run skip(). module should be a string. """ # To give a string of the module name to skip(), this function takes a # string. So we don't waste time running import_module() more than once, # just map the three modules tested here in this dict. modnames = {'numpy': numpy, 'Cython': Cython, 'f2py': f2py}
Cython = import_module("Cython", min_module_version="0.15.1") f2py = import_module("numpy.f2py", import_kwargs={"fromlist": ["f2py"]}) f2pyworks = False if f2py: try: autowrap(symbols("x"), "f95", "f2py") except (CodeWrapError, ImportError, OSError): f2pyworks = False else: f2pyworks = True a, b, c = symbols("a b c") n, m, d = symbols("n m d", integer=True) A, B, C = symbols("A B C", cls=IndexedBase) i = Idx("i", m) j = Idx("j", n) k = Idx("k", d) def has_module(module): """ Return True if module exists, otherwise run skip(). module should be a string. """ # To give a string of the module name to skip(), this function takes a # string. So we don't waste time running import_module() more than once, # just map the three modules tested here in this dict. modnames = {"numpy": numpy, "Cython": Cython, "f2py": f2py}
def tt(): A = VIB("A") B = VIB("B") i = Idx('i', 2) res = A[i] * B[i] print(res)
return exprs A = IndexedBase("A") B = IndexedBase("B") C = IndexedBase("\mathbb{C}") N = IndexedBase("N") dNdX = IndexedBase("\\frac{dN}{dX}") IoI = IndexedBase("I \otimes I") II = IndexedBase("\mathbb{I}") P = IndexedBase("\mathbb{P}") dRdu = IndexedBase("dRdu") strain = IndexedBase("varepsilon") i = Idx("i", 2) j = Idx("j", 2) k = Idx("k", 2) l = Idx("l", 2) r = Idx("r", 2) m = Idx("m", 2) n = Idx("n", 2) I = Idx("I", 4) J = Idx("J", 4) M = Idx("M", 4) # Strain tensor expr = Equals(strain[i, j], strain[i, j]) exprs = transform(expr, (i, j), (i, j), (i, j)) mat = generate_matrix(exprs)