Пример #1
0
def test_m_multifcns_per_file():
    name_expr = [('foo', [2 * x, 3 * y]), ('bar', [y**2, 4 * y])]
    result = codegen(name_expr, 'Octave', header=False, empty=False)
    assert result[0][0] == 'foo.m'
    source = result[0][1]
    expected = ('function [out1, out2] = foo(x, y)\n'
                '  out1 = 2*x;\n'
                '  out2 = 3*y;\n'
                'end\n'
                'function [out1, out2] = bar(y)\n'
                '  out1 = y.^2;\n'
                '  out2 = 4*y;\n'
                'end\n')
    assert source == expected
    result = codegen(name_expr, 'Octave', header=False)
    assert result[0][0] == 'foo.m'
    source = result[0][1]
    expected = ('function [out1, out2] = foo(x, y)\n'
                '  \n'
                '  out1 = 2*x;\n'
                '  out2 = 3*y;\n'
                '  \n'
                'end\n\n'
                'function [out1, out2] = bar(y)\n'
                '  \n'
                '  out1 = y.^2;\n'
                '  out2 = 4*y;\n'
                '  \n'
                'end\n')
    assert source == expected
Пример #2
0
def test_global_vars_octave():
    x, y, z, t = symbols("x y z t")
    result = codegen(('f', x * y),
                     "Octave",
                     header=False,
                     empty=False,
                     global_vars=(y, ))
    source = result[0][1]
    expected = ("function out1 = f(x)\n"
                "  global y\n"
                "  out1 = x.*y;\n"
                "end\n")
    assert source == expected

    result = codegen(('f', x * y + z),
                     "Octave",
                     header=False,
                     empty=False,
                     argument_sequence=(x, y),
                     global_vars=(z, t))
    source = result[0][1]
    expected = ("function out1 = f(x, y)\n"
                "  global t z\n"
                "  out1 = x.*y + z;\n"
                "end\n")
    assert source == expected
Пример #3
0
def test_global_vars():
    result = codegen(('f', x*y), "F95", header=False, empty=False,
                     global_vars=(y,))
    source = result[0][1]
    expected = (
        "REAL*8 function f(x)\n"
        "implicit none\n"
        "REAL*8, intent(in) :: x\n"
        "f = x*y\n"
        "end function\n"
    )
    assert source == expected

    result = codegen(('f', x*y+z), "C", header=False, empty=False,
                     global_vars=(z, t))
    source = result[0][1]
    expected = (
        '#include "f.h"\n'
        '#include <math.h>\n'
        'double f(double x, double y) {\n'
        '   double f_result;\n'
        '   f_result = x*y + z;\n'
        '   return f_result;\n'
        '}\n'
    )
    assert source == expected
Пример #4
0
def test_global_vars_octave():
    result = codegen(('f', x * y),
                     'Octave',
                     header=False,
                     empty=False,
                     global_vars=(y, ))
    source = result[0][1]
    expected = ('function out1 = f(x)\n'
                '  global y\n'
                '  out1 = x.*y;\n'
                'end\n')
    assert source == expected

    result = codegen(('f', x * y + z),
                     'Octave',
                     header=False,
                     empty=False,
                     argument_sequence=(x, y),
                     global_vars=(z, t))
    source = result[0][1]
    expected = ('function out1 = f(x, y)\n'
                '  global t z\n'
                '  out1 = x.*y + z;\n'
                'end\n')
    assert source == expected
Пример #5
0
def test_check_case_false_positive():
    # The upper case/lower case exception should not be triggered by Diofant
    # objects that differ only because of assumptions.  (It may be useful to
    # have a check for that as well, but here we only want to test against
    # false positives with respect to case checking.)
    x2 = symbols('x', my_assumption=True)
    try:
        codegen(('test', x*x2), 'f95', 'prefix')
    except CodeGenError as e:
        if e.args[0].startswith("Fortran ignores case."):
            raise AssertionError("This exception should not be raised!")
Пример #6
0
def test_m_loops():
    # Note: an Octave programmer would probably vectorize this across one or
    # more dimensions.  Also, size(A) would be used rather than passing in m
    # and n.  Perhaps users would expect us to vectorize automatically here?
    # Or is it possible to represent such things using IndexedBase?
    n, m = symbols('n m', integer=True)
    A = IndexedBase('A')
    x = IndexedBase('x')
    y = IndexedBase('y')
    i = Idx('i', m)
    j = Idx('j', n)
    result, = codegen(('mat_vec_mult', Eq(y[i], A[i, j] * x[j])),
                      'Octave',
                      header=False,
                      empty=False)
    source = result[1]
    expected = ('function y = mat_vec_mult(A, m, n, x)\n'
                '  for i = 1:m\n'
                '    y(i) = 0;\n'
                '  end\n'
                '  for i = 1:m\n'
                '    for j = 1:n\n'
                '      y(i) = %(rhs)s + y(i);\n'
                '    end\n'
                '  end\n'
                'end\n')
    assert (source == expected % {
        'rhs': 'A(%s, %s).*x(j)' % (i, j)
    } or source == expected % {
        'rhs': 'x(j).*A(%s, %s)' % (i, j)
    })

    result, = codegen(('mat_vec_mult', Eq(y[i], A[i, j] * x[j])),
                      'Octave',
                      header=False,
                      empty=False,
                      argument_sequence=[x, A, m, n])
    source = result[1]
    expected = ('function y = mat_vec_mult(x, A, m, n)\n'
                '  for i = 1:m\n'
                '    y(i) = 0;\n'
                '  end\n'
                '  for i = 1:m\n'
                '    for j = 1:n\n'
                '      y(i) = %(rhs)s + y(i);\n'
                '    end\n'
                '  end\n'
                'end\n')
    assert (source == expected % {
        'rhs': 'A(%s, %s).*x(j)' % (i, j)
    } or source == expected % {
        'rhs': 'x(j).*A(%s, %s)' % (i, j)
    })
Пример #7
0
def test_m_tensor_loops_multiple_contractions():
    # see comments in previous test about vectorizing
    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])),
                      'Octave',
                      header=False,
                      empty=False)
    source = result[1]
    expected = ('function y = tensorthing(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) = y(i) + B(j, k, l).*A(i, j, k, l);\n'
                '        end\n'
                '      end\n'
                '    end\n'
                '  end\n'
                'end\n')
    assert source == expected
Пример #8
0
def test_m_InOutArgument():
    expr = Equality(x, x**2)
    name_expr = ('mysqr', expr)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function x = mysqr(x)\n' '  x = x.^2;\n' 'end\n')
    assert source == expected
Пример #9
0
def test_fcode_results_named_ordered():
    A = MatrixSymbol('A', 1, 3)
    expr1 = Equality(A, Matrix([[1, 2, x]]))
    expr2 = Equality(C, (x + y)*z)
    expr3 = Equality(B, 2*x)
    name_expr = ("test", [expr1, expr2, expr3])
    result = codegen(name_expr, "f95", "test", header=False, empty=False,
                     argument_sequence=(x, z, y, C, A, B))
    source = result[0][1]
    expected = (
        "subroutine test(x, z, y, C, A, B)\n"
        "implicit none\n"
        "REAL*8, intent(in) :: x\n"
        "REAL*8, intent(in) :: z\n"
        "REAL*8, intent(in) :: y\n"
        "REAL*8, intent(out) :: C\n"
        "REAL*8, intent(out) :: B\n"
        "REAL*8, intent(out), dimension(1:1, 1:3) :: A\n"
        "C = z*(x + y)\n"
        "A(1, 1) = 1\n"
        "A(1, 2) = 2\n"
        "A(1, 3) = x\n"
        "B = 2*x\n"
        "end subroutine\n"
    )
    assert source == expected
Пример #10
0
def test_m_loops():
    # Note: an Octave programmer would probably vectorize this across one or
    # more dimensions.  Also, size(A) would be used rather than passing in m
    # and n.  Perhaps users would expect us to vectorize automatically here?
    # Or is it possible to represent such things using IndexedBase?
    n, m = symbols('n m', integer=True)
    A = IndexedBase('A')
    x = IndexedBase('x')
    y = IndexedBase('y')
    i = Idx('i', m)
    j = Idx('j', n)
    result, = codegen(('mat_vec_mult', Eq(y[i], A[i, j]*x[j])), "Octave",
                      header=False, empty=False)
    source = result[1]
    expected = (
        'function y = mat_vec_mult(A, m, n, x)\n'
        '  for i = 1:m\n'
        '    y(i) = 0;\n'
        '  end\n'
        '  for i = 1:m\n'
        '    for j = 1:n\n'
        '      y(i) = %(rhs)s + y(i);\n'
        '    end\n'
        '  end\n'
        'end\n'
    )
    assert (source == expected % {'rhs': 'A(%s, %s).*x(j)' % (i, j)} or
            source == expected % {'rhs': 'x(j).*A(%s, %s)' % (i, j)})
Пример #11
0
def test_ccode_matrixsymbol_slice():
    A = MatrixSymbol('A', 5, 3)
    B = MatrixSymbol('B', 1, 3)
    C = MatrixSymbol('C', 1, 3)
    D = MatrixSymbol('D', 5, 1)
    name_expr = ("test", [Equality(B, A[0, :]),
                          Equality(C, A[1, :]),
                          Equality(D, A[:, 2])])
    result = codegen(name_expr, "c", "test", header=False, empty=False)
    source = result[0][1]
    expected = (
        '#include "test.h"\n'
        '#include <math.h>\n'
        'void test(double *A, double *B, double *C, double *D) {\n'
        '   B[0] = A[0];\n'
        '   B[1] = A[1];\n'
        '   B[2] = A[2];\n'
        '   C[0] = A[3];\n'
        '   C[1] = A[4];\n'
        '   C[2] = A[5];\n'
        '   D[0] = A[2];\n'
        '   D[1] = A[5];\n'
        '   D[2] = A[8];\n'
        '   D[3] = A[11];\n'
        '   D[4] = A[14];\n'
        '}\n'
    )
    assert source == expected
Пример #12
0
def test_m_simple_code_nameout():
    expr = Equality(z, (x + y))
    name_expr = ('test', expr)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function z = test(x, y)\n' '  z = x + y;\n' 'end\n')
    assert source == expected
Пример #13
0
def test_fcode_matrixsymbol_slice():
    A = MatrixSymbol('A', 2, 3)
    B = MatrixSymbol('B', 1, 3)
    C = MatrixSymbol('C', 1, 3)
    D = MatrixSymbol('D', 2, 1)
    name_expr = ("test", [Equality(B, A[0, :]),
                          Equality(C, A[1, :]),
                          Equality(D, A[:, 2])])
    result = codegen(name_expr, "f95", "test", header=False, empty=False)
    source = result[0][1]
    expected = (
        "subroutine test(A, B, C, D)\n"
        "implicit none\n"
        "REAL*8, intent(in), dimension(1:2, 1:3) :: A\n"
        "REAL*8, intent(out), dimension(1:1, 1:3) :: B\n"
        "REAL*8, intent(out), dimension(1:1, 1:3) :: C\n"
        "REAL*8, intent(out), dimension(1:2, 1:1) :: D\n"
        "B(1, 1) = A(1, 1)\n"
        "B(1, 2) = A(1, 2)\n"
        "B(1, 3) = A(1, 3)\n"
        "C(1, 1) = A(2, 1)\n"
        "C(1, 2) = A(2, 2)\n"
        "C(1, 3) = A(2, 3)\n"
        "D(1, 1) = A(1, 3)\n"
        "D(2, 1) = A(2, 3)\n"
        "end subroutine\n"
    )
    assert source == expected
Пример #14
0
def test_m_tensor_loops_multiple_contractions():
    # see comments in previous test about vectorizing
    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])),
                      "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        'function y = tensorthing(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) = y(i) + B(j, k, l).*A(i, j, k, l);\n'
        '        end\n'
        '      end\n'
        '    end\n'
        '  end\n'
        'end\n'
    )
    assert source == expected
Пример #15
0
def test_fcode_matrix_output():
    e1 = x + y
    e2 = Matrix([[x, y], [z, 16]])
    name_expr = ("test", (e1, e2))
    result = codegen(name_expr, "f95", "test", header=False, empty=False)
    source = result[0][1]
    expected = (
        "REAL*8 function test(x, y, z, out_%(hash)s)\n"
        "implicit none\n"
        "REAL*8, intent(in) :: x\n"
        "REAL*8, intent(in) :: y\n"
        "REAL*8, intent(in) :: z\n"
        "REAL*8, intent(out), dimension(1:2, 1:2) :: out_%(hash)s\n"
        "out_%(hash)s(1, 1) = x\n"
        "out_%(hash)s(2, 1) = z\n"
        "out_%(hash)s(1, 2) = y\n"
        "out_%(hash)s(2, 2) = 16\n"
        "test = x + y\n"
        "end function\n"
    )
    # look for the magic number
    a = source.splitlines()[5]
    b = a.split('_')
    out = b[1]
    expected = expected % {'hash': out}
    assert source == expected
Пример #16
0
def test_m_simple_code_nameout():
    expr = Equality(z, (x + y))
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function z = test(x, y)\n" "  z = x + y;\n" "end\n")
    assert source == expected
Пример #17
0
def test_m_InOutArgument():
    expr = Equality(x, x**2)
    name_expr = ("mysqr", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function x = mysqr(x)\n" "  x = x.^2;\n" "end\n")
    assert source == expected
Пример #18
0
def test_m_numbersymbol():
    name_expr = ("test", pi**Catalan)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function out1 = test()\n"
                "  out1 = pi^0.915965594177219;\n"
                "end\n")
    assert source == expected
Пример #19
0
def test_complicated_codegen_f95():
    name_expr = [
        ("test1", ((sin(x) + cos(y) + tan(z))**7).expand()),
        ("test2", cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))),
    ]
    result = codegen(name_expr, "F95", "file", header=False, empty=False)
    assert result[0][0] == "file.f90"
    expected = (
        'REAL*8 function test1(x, y, z)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'REAL*8, intent(in) :: y\n'
        'REAL*8, intent(in) :: z\n'
        'test1 = sin(x)**7 + 7*sin(x)**6*cos(y) + 7*sin(x)**6*tan(z) + 21*sin(x) &\n'
        '      **5*cos(y)**2 + 42*sin(x)**5*cos(y)*tan(z) + 21*sin(x)**5*tan(z) &\n'
        '      **2 + 35*sin(x)**4*cos(y)**3 + 105*sin(x)**4*cos(y)**2*tan(z) + &\n'
        '      105*sin(x)**4*cos(y)*tan(z)**2 + 35*sin(x)**4*tan(z)**3 + 35*sin( &\n'
        '      x)**3*cos(y)**4 + 140*sin(x)**3*cos(y)**3*tan(z) + 210*sin(x)**3* &\n'
        '      cos(y)**2*tan(z)**2 + 140*sin(x)**3*cos(y)*tan(z)**3 + 35*sin(x) &\n'
        '      **3*tan(z)**4 + 21*sin(x)**2*cos(y)**5 + 105*sin(x)**2*cos(y)**4* &\n'
        '      tan(z) + 210*sin(x)**2*cos(y)**3*tan(z)**2 + 210*sin(x)**2*cos(y) &\n'
        '      **2*tan(z)**3 + 105*sin(x)**2*cos(y)*tan(z)**4 + 21*sin(x)**2*tan &\n'
        '      (z)**5 + 7*sin(x)*cos(y)**6 + 42*sin(x)*cos(y)**5*tan(z) + 105* &\n'
        '      sin(x)*cos(y)**4*tan(z)**2 + 140*sin(x)*cos(y)**3*tan(z)**3 + 105 &\n'
        '      *sin(x)*cos(y)**2*tan(z)**4 + 42*sin(x)*cos(y)*tan(z)**5 + 7*sin( &\n'
        '      x)*tan(z)**6 + cos(y)**7 + 7*cos(y)**6*tan(z) + 21*cos(y)**5*tan( &\n'
        '      z)**2 + 35*cos(y)**4*tan(z)**3 + 35*cos(y)**3*tan(z)**4 + 21*cos( &\n'
        '      y)**2*tan(z)**5 + 7*cos(y)*tan(z)**6 + tan(z)**7\n'
        'end function\n'
        'REAL*8 function test2(x, y, z)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'REAL*8, intent(in) :: y\n'
        'REAL*8, intent(in) :: z\n'
        'test2 = cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))\n'
        'end function\n'
    )
    assert result[0][1] == expected
    assert result[1][0] == "file.h"
    expected = (
        'interface\n'
        'REAL*8 function test1(x, y, z)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'REAL*8, intent(in) :: y\n'
        'REAL*8, intent(in) :: z\n'
        'end function\n'
        'end interface\n'
        'interface\n'
        'REAL*8 function test2(x, y, z)\n'
        'implicit none\n'
        'REAL*8, intent(in) :: x\n'
        'REAL*8, intent(in) :: y\n'
        'REAL*8, intent(in) :: z\n'
        'end function\n'
        'end interface\n'
    )
    assert result[1][1] == expected
Пример #20
0
def test_m_numbersymbol():
    name_expr = ('test', pi**Catalan)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function out1 = test()\n'
                '  Catalan = 0.915965594177219;  % constant\n'
                '  out1 = pi^Catalan;\n'
                'end\n')
    assert source == expected
Пример #21
0
def test_m_matrix_output_autoname():
    expr = Matrix([[x, x + y, 3]])
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function out1 = test(x, y)\n"
                "  out1 = [x x + y 3];\n"
                "end\n")
    assert source == expected
Пример #22
0
def test_m_matrix_output_autoname():
    expr = Matrix([[x, x + y, 3]])
    name_expr = ('test', expr)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function out1 = test(x, y)\n'
                '  out1 = [x x + y 3];\n'
                'end\n')
    assert source == expected
Пример #23
0
def test_m_simple_code():
    name_expr = ('test', (x + y) * z)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    assert result[0] == 'test.m'
    source = result[1]
    expected = ('function out1 = test(x, y, z)\n'
                '  out1 = z.*(x + y);\n'
                'end\n')
    assert source == expected
    result, = codegen(name_expr, 'Octave', header=False)
    assert result[0] == 'test.m'
    source = result[1]
    expected = ('function out1 = test(x, y, z)\n'
                '  \n'
                '  out1 = z.*(x + y);\n'
                '  \n'
                'end\n')
    assert source == expected
Пример #24
0
def test_m_simple_code():
    name_expr = ("test", (x + y) * z)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0] == "test.m"
    source = result[1]
    expected = ("function out1 = test(x, y, z)\n"
                "  out1 = z.*(x + y);\n"
                "end\n")
    assert source == expected
Пример #25
0
def test_m_numbersymbol():
    name_expr = ("test", pi**Catalan)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function out1 = test()\n"
        "  out1 = pi^0.915965594177219;\n"
        "end\n"
    )
    assert source == expected
Пример #26
0
def test_m_matrix_named():
    e2 = Matrix([[x, 2 * y, pi * z]])
    name_expr = ("test", Equality(MatrixSymbol('myout1', 1, 3), e2))
    result = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0][0] == "test.m"
    source = result[0][1]
    expected = ("function myout1 = test(x, y, z)\n"
                "  myout1 = [x 2*y pi*z];\n"
                "end\n")
    assert source == expected
Пример #27
0
def test_m_matrix_named():
    e2 = Matrix([[x, 2 * y, pi * z]])
    name_expr = ('test', Equality(MatrixSymbol('myout1', 1, 3), e2))
    result = codegen(name_expr, 'Octave', header=False, empty=False)
    assert result[0][0] == 'test.m'
    source = result[0][1]
    expected = ('function myout1 = test(x, y, z)\n'
                '  myout1 = [x 2*y pi*z];\n'
                'end\n')
    assert source == expected
Пример #28
0
def test_m_matrix_named_matsym():
    myout1 = MatrixSymbol('myout1', 1, 3)
    e2 = Matrix([[x, 2 * y, pi * z]])
    name_expr = ('test', Equality(myout1, e2, evaluate=False))
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function myout1 = test(x, y, z)\n'
                '  myout1 = [x 2*y pi*z];\n'
                'end\n')
    assert source == expected
Пример #29
0
def test_m_InOutArgument_order():
    # can specify the order as (x, y)
    expr = Equality(x, x**2 + y)
    name_expr = ('test', expr)
    result, = codegen(name_expr,
                      'Octave',
                      header=False,
                      empty=False,
                      argument_sequence=(x, y))
    source = result[1]
    expected = ('function x = test(x, y)\n' '  x = x.^2 + y;\n' 'end\n')
    assert source == expected
    # make sure it gives (x, y) not (y, x)
    expr = Equality(x, x**2 + y)
    name_expr = ('test', expr)
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function x = test(x, y)\n' '  x = x.^2 + y;\n' 'end\n')
    assert source == expected
Пример #30
0
def test_m_InOutArgument():
    expr = Equality(x, x**2)
    name_expr = ("mysqr", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function x = mysqr(x)\n"
        "  x = x.^2;\n"
        "end\n"
    )
    assert source == expected
Пример #31
0
def test_m_matrix_output_autoname():
    expr = Matrix([[x, x+y, 3]])
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function out1 = test(x, y)\n"
        "  out1 = [x x + y 3];\n"
        "end\n"
    )
    assert source == expected
Пример #32
0
def test_m_simple_code():
    name_expr = ("test", (x + y)*z)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0] == "test.m"
    source = result[1]
    expected = (
        "function out1 = test(x, y, z)\n"
        "  out1 = z.*(x + y);\n"
        "end\n"
    )
    assert source == expected
Пример #33
0
def test_m_simple_code_nameout():
    expr = Equality(z, (x + y))
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function z = test(x, y)\n"
        "  z = x + y;\n"
        "end\n"
    )
    assert source == expected
Пример #34
0
def test_m_piecewise_():
    pw = Piecewise((0, x < -1), (x**2, x <= 1), (-x + 2, x > 1), (1, True))
    name_expr = ("pwtest", pw)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function out1 = pwtest(x)\n"
                "  out1 = ((x < -1).*(0) + (~(x < -1)).*( ...\n"
                "  (x <= 1).*(x.^2) + (~(x <= 1)).*( ...\n"
                "  (x > 1).*(-x + 2) + (~(x > 1)).*(1))));\n"
                "end\n")
    assert source == expected
Пример #35
0
def test_multiple_results_m():
    # Here the output order is the input order
    expr1 = (x + y) * z
    expr2 = (x - y) * z
    name_expr = ('test', [expr1, expr2])
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [out1, out2] = test(x, y, z)\n'
                '  out1 = z.*(x + y);\n'
                '  out2 = z.*(x - y);\n'
                'end\n')
    assert source == expected
Пример #36
0
def test_m_matrix_named():
    e2 = Matrix([[x, 2*y, pi*z]])
    name_expr = ("test", Equality(MatrixSymbol('myout1', 1, 3), e2))
    result = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0][0] == "test.m"
    source = result[0][1]
    expected = (
        "function myout1 = test(x, y, z)\n"
        "  myout1 = [x 2*y pi*z];\n"
        "end\n"
    )
    assert source == expected
Пример #37
0
def test_m_matrixsymbol_slice2():
    A = MatrixSymbol('A', 3, 4)
    B = MatrixSymbol('B', 2, 2)
    C = MatrixSymbol('C', 2, 2)
    name_expr = ("test", [Equality(B, A[0:2, 0:2]), Equality(C, A[0:2, 1:3])])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function [B, C] = test(A)\n"
                "  B = A(1:2, 1:2);\n"
                "  C = A(1:2, 2:3);\n"
                "end\n")
    assert source == expected
Пример #38
0
def test_m_matrix_named_matsym():
    myout1 = MatrixSymbol('myout1', 1, 3)
    e2 = Matrix([[x, 2*y, pi*z]])
    name_expr = ("test", Equality(myout1, e2, evaluate=False))
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function myout1 = test(x, y, z)\n"
        "  myout1 = [x 2*y pi*z];\n"
        "end\n"
    )
    assert source == expected
Пример #39
0
def test_m_matrixsymbol_slice3():
    A = MatrixSymbol('A', 8, 7)
    B = MatrixSymbol('B', 2, 2)
    C = MatrixSymbol('C', 4, 2)
    name_expr = ('test', [Equality(B, A[6:, 1::3]), Equality(C, A[::2, ::3])])
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [B, C] = test(A)\n'
                '  B = A(7:end, 2:3:end);\n'
                '  C = A(1:2:end, 1:3:end);\n'
                'end\n')
    assert source == expected
Пример #40
0
def test_m_not_supported():
    f = Function('f')
    name_expr = ("test", [f(x).diff(x), S.ComplexInfinity])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function [out1, out2] = test(x)\n"
                "  % unsupported: Derivative(f(x), x)\n"
                "  % unsupported: zoo\n"
                "  out1 = Derivative(f(x), x);\n"
                "  out2 = zoo;\n"
                "end\n")
    assert source == expected
Пример #41
0
def test_global_vars_octave():
    result = codegen(('f', x*y), "Octave", header=False, empty=False,
                     global_vars=(y,))
    source = result[0][1]
    expected = (
        "function out1 = f(x)\n"
        "  global y\n"
        "  out1 = x.*y;\n"
        "end\n"
    )
    assert source == expected

    result = codegen(('f', x*y+z), "Octave", header=False, empty=False,
                     argument_sequence=(x, y), global_vars=(z, t))
    source = result[0][1]
    expected = (
        "function out1 = f(x, y)\n"
        "  global t z\n"
        "  out1 = x.*y + z;\n"
        "end\n"
    )
    assert source == expected
Пример #42
0
def test_m_matrixsymbol_slice_autoname():
    A = MatrixSymbol('A', 2, 3)
    B = MatrixSymbol('B', 1, 3)
    name_expr = ('test', [Equality(B, A[0, :]), A[1, :], A[:, 0], A[:, 1]])
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [B, out2, out3, out4] = test(A)\n'
                '  B = A(1, :);\n'
                '  out2 = A(2, :);\n'
                '  out3 = A(:, 1);\n'
                '  out4 = A(:, 2);\n'
                'end\n')
    assert source == expected
Пример #43
0
def test_m_piecewise_():
    pw = Piecewise((0, x < -1), (x**2, x <= 1), (-x+2, x > 1), (1, True))
    name_expr = ("pwtest", pw)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function out1 = pwtest(x)\n"
        "  out1 = ((x < -1).*(0) + (~(x < -1)).*( ...\n"
        "  (x <= 1).*(x.^2) + (~(x <= 1)).*( ...\n"
        "  (x > 1).*(-x + 2) + (~(x > 1)).*(1))));\n"
        "end\n"
    )
    assert source == expected
Пример #44
0
def test_ansi_math1_codegen():
    # not included: log10
    name_expr = [
        ("test_fabs", Abs(x)),
        ("test_acos", acos(x)),
        ("test_asin", asin(x)),
        ("test_atan", atan(x)),
        ("test_ceil", ceiling(x)),
        ("test_cos", cos(x)),
        ("test_cosh", cosh(x)),
        ("test_floor", floor(x)),
        ("test_log", log(x)),
        ("test_ln", ln(x)),
        ("test_sin", sin(x)),
        ("test_sinh", sinh(x)),
        ("test_sqrt", sqrt(x)),
        ("test_tan", tan(x)),
        ("test_tanh", tanh(x)),
    ]
    result = codegen(name_expr, "C", "file", header=False, empty=False)
    assert result[0][0] == "file.c"
    assert result[0][1] == (
        '#include "file.h"\n#include <math.h>\n'
        'double test_fabs(double x) {\n   double test_fabs_result;\n   test_fabs_result = fabs(x);\n   return test_fabs_result;\n}\n'
        'double test_acos(double x) {\n   double test_acos_result;\n   test_acos_result = acos(x);\n   return test_acos_result;\n}\n'
        'double test_asin(double x) {\n   double test_asin_result;\n   test_asin_result = asin(x);\n   return test_asin_result;\n}\n'
        'double test_atan(double x) {\n   double test_atan_result;\n   test_atan_result = atan(x);\n   return test_atan_result;\n}\n'
        'double test_ceil(double x) {\n   double test_ceil_result;\n   test_ceil_result = ceil(x);\n   return test_ceil_result;\n}\n'
        'double test_cos(double x) {\n   double test_cos_result;\n   test_cos_result = cos(x);\n   return test_cos_result;\n}\n'
        'double test_cosh(double x) {\n   double test_cosh_result;\n   test_cosh_result = cosh(x);\n   return test_cosh_result;\n}\n'
        'double test_floor(double x) {\n   double test_floor_result;\n   test_floor_result = floor(x);\n   return test_floor_result;\n}\n'
        'double test_log(double x) {\n   double test_log_result;\n   test_log_result = log(x);\n   return test_log_result;\n}\n'
        'double test_ln(double x) {\n   double test_ln_result;\n   test_ln_result = log(x);\n   return test_ln_result;\n}\n'
        'double test_sin(double x) {\n   double test_sin_result;\n   test_sin_result = sin(x);\n   return test_sin_result;\n}\n'
        'double test_sinh(double x) {\n   double test_sinh_result;\n   test_sinh_result = sinh(x);\n   return test_sinh_result;\n}\n'
        'double test_sqrt(double x) {\n   double test_sqrt_result;\n   test_sqrt_result = sqrt(x);\n   return test_sqrt_result;\n}\n'
        'double test_tan(double x) {\n   double test_tan_result;\n   test_tan_result = tan(x);\n   return test_tan_result;\n}\n'
        'double test_tanh(double x) {\n   double test_tanh_result;\n   test_tanh_result = tanh(x);\n   return test_tanh_result;\n}\n'
    )
    assert result[1][0] == "file.h"
    assert result[1][1] == (
        '#ifndef PROJECT__FILE__H\n#define PROJECT__FILE__H\n'
        'double test_fabs(double x);\ndouble test_acos(double x);\n'
        'double test_asin(double x);\ndouble test_atan(double x);\n'
        'double test_ceil(double x);\ndouble test_cos(double x);\n'
        'double test_cosh(double x);\ndouble test_floor(double x);\n'
        'double test_log(double x);\ndouble test_ln(double x);\n'
        'double test_sin(double x);\ndouble test_sinh(double x);\n'
        'double test_sqrt(double x);\ndouble test_tan(double x);\n'
        'double test_tanh(double x);\n#endif\n'
    )
Пример #45
0
def test_multiple_results_m():
    # Here the output order is the input order
    expr1 = (x + y)*z
    expr2 = (x - y)*z
    name_expr = ("test", [expr1, expr2])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function [out1, out2] = test(x, y, z)\n"
        "  out1 = z.*(x + y);\n"
        "  out2 = z.*(x - y);\n"
        "end\n"
    )
    assert source == expected
Пример #46
0
def test_m_multifcns_per_file():
    name_expr = [("foo", [2 * x, 3 * y]), ("bar", [y**2, 4 * y])]
    result = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0][0] == "foo.m"
    source = result[0][1]
    expected = ("function [out1, out2] = foo(x, y)\n"
                "  out1 = 2*x;\n"
                "  out2 = 3*y;\n"
                "end\n"
                "function [out1, out2] = bar(y)\n"
                "  out1 = y.^2;\n"
                "  out2 = 4*y;\n"
                "end\n")
    assert source == expected
Пример #47
0
def test_m_not_supported():
    f = Function('f')
    name_expr = ("test", [f(x).diff(x), zoo])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function [out1, out2] = test(x)\n"
        "  % unsupported: Derivative(f(x), x)\n"
        "  % unsupported: zoo\n"
        "  out1 = Derivative(f(x), x);\n"
        "  out2 = zoo;\n"
        "end\n"
    )
    assert source == expected
Пример #48
0
def test_results_named_unordered():
    # Here output order is based on name_expr
    expr1 = Equality(C, (x + y) * z)
    expr2 = Equality(A, (x - y) * z)
    expr3 = Equality(B, 2 * x)
    name_expr = ('test', [expr1, expr2, expr3])
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [C, A, B] = test(x, y, z)\n'
                '  C = z.*(x + y);\n'
                '  A = z.*(x - y);\n'
                '  B = 2*x;\n'
                'end\n')
    assert source == expected
Пример #49
0
def test_m_not_supported():
    f = Function('f')
    name_expr = ('test', [f(x).diff(x), zoo, f(x)])
    result, = codegen(name_expr, 'Octave', header=False, empty=False)
    source = result[1]
    expected = ('function [out1, out2, out3] = test(x)\n'
                '  % unsupported: Derivative(f(x), x)\n'
                '  % unsupported: zoo\n'
                '  % unsupported: f\n'
                '  out1 = Derivative(f(x), x);\n'
                '  out2 = zoo;\n'
                '  out3 = f(x);\n'
                'end\n')
    assert source == expected
Пример #50
0
def test_m_matrixsymbol_slice_autoname():
    A = MatrixSymbol('A', 2, 3)
    B = MatrixSymbol('B', 1, 3)
    name_expr = ("test", [Equality(B, A[0, :]), A[1, :], A[:, 0], A[:, 1]])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function [B, out2, out3, out4] = test(A)\n"
        "  B = A(1, :);\n"
        "  out2 = A(2, :);\n"
        "  out3 = A(:, 1);\n"
        "  out4 = A(:, 2);\n"
        "end\n"
    )
    assert source == expected
Пример #51
0
def test_m_InOutArgument_order():
    # can specify the order as (x, y)
    expr = Equality(x, x**2 + y)
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False,
                      empty=False, argument_sequence=(x, y))
    source = result[1]
    expected = (
        "function x = test(x, y)\n"
        "  x = x.^2 + y;\n"
        "end\n"
    )
    assert source == expected
    # make sure it gives (x, y) not (y, x)
    expr = Equality(x, x**2 + y)
    name_expr = ("test", expr)
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function x = test(x, y)\n"
        "  x = x.^2 + y;\n"
        "end\n"
    )
    assert source == expected
Пример #52
0
def test_m_matrixsymbol_slice3():
    A = MatrixSymbol('A', 8, 7)
    B = MatrixSymbol('B', 2, 2)
    C = MatrixSymbol('C', 4, 2)
    name_expr = ("test", [Equality(B, A[6:, 1::3]),
                          Equality(C, A[::2, ::3])])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function [B, C] = test(A)\n"
        "  B = A(7:end, 2:3:end);\n"
        "  C = A(1:2:end, 1:3:end);\n"
        "end\n"
    )
    assert source == expected
Пример #53
0
def test_m_output_arg_mixed_unordered():
    # named outputs are alphabetical, unnamed output appear in the given order
    name_expr = ("foo", [cos(2*x), Equality(y, sin(x)), cos(x), Equality(a, sin(2*x))])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0] == "foo.m"
    source = result[1]
    expected = (
        'function [out1, y, out3, a] = foo(x)\n'
        '  out1 = cos(2*x);\n'
        '  y = sin(x);\n'
        '  out3 = cos(x);\n'
        '  a = sin(2*x);\n'
        'end\n'
    )
    assert source == expected
Пример #54
0
def test_complicated_m_codegen():
    name_expr = ('testlong', [((sin(x) + cos(y) + tan(z))**3).expand(),
                              cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))
                              ])
    result = codegen(name_expr, 'Octave', header=False, empty=False)
    assert result[0][0] == 'testlong.m'
    source = result[0][1]
    expected = (
        'function [out1, out2] = testlong(x, y, z)\n'
        '  out1 = sin(x).^3 + 3*sin(x).^2.*cos(y) + 3*sin(x).^2.*tan(z)'
        ' + 3*sin(x).*cos(y).^2 + 6*sin(x).*cos(y).*tan(z) + 3*sin(x).*tan(z).^2'
        ' + cos(y).^3 + 3*cos(y).^2.*tan(z) + 3*cos(y).*tan(z).^2 + tan(z).^3;\n'
        '  out2 = cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))));\n'
        'end\n')
    assert source == expected
Пример #55
0
def test_results_named_unordered():
    # Here output order is based on name_expr
    A, B, C = symbols('A,B,C')
    expr1 = Equality(C, (x + y) * z)
    expr2 = Equality(A, (x - y) * z)
    expr3 = Equality(B, 2 * x)
    name_expr = ("test", [expr1, expr2, expr3])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = ("function [C, A, B] = test(x, y, z)\n"
                "  C = z.*(x + y);\n"
                "  A = z.*(x - y);\n"
                "  B = 2*x;\n"
                "end\n")
    assert source == expected
Пример #56
0
def test_m_multifcns_per_file():
    name_expr = [ ("foo", [2*x, 3*y]), ("bar", [y**2, 4*y]) ]
    result = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0][0] == "foo.m"
    source = result[0][1]
    expected = (
        "function [out1, out2] = foo(x, y)\n"
        "  out1 = 2*x;\n"
        "  out2 = 3*y;\n"
        "end\n"
        "function [out1, out2] = bar(y)\n"
        "  out1 = y.^2;\n"
        "  out2 = 4*y;\n"
        "end\n"
    )
    assert source == expected
Пример #57
0
def test_complicated_m_codegen():
    name_expr = ("testlong",
                 [((sin(x) + cos(y) + tan(z))**3).expand(),
                  cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))))])
    result = codegen(name_expr, "Octave", header=False, empty=False)
    assert result[0][0] == "testlong.m"
    source = result[0][1]
    expected = (
        "function [out1, out2] = testlong(x, y, z)\n"
        "  out1 = sin(x).^3 + 3*sin(x).^2.*cos(y) + 3*sin(x).^2.*tan(z)"
        " + 3*sin(x).*cos(y).^2 + 6*sin(x).*cos(y).*tan(z) + 3*sin(x).*tan(z).^2"
        " + cos(y).^3 + 3*cos(y).^2.*tan(z) + 3*cos(y).*tan(z).^2 + tan(z).^3;\n"
        "  out2 = cos(cos(cos(cos(cos(cos(cos(cos(x + y + z))))))));\n"
        "end\n"
    )
    assert source == expected
Пример #58
0
def test_results_named_unordered():
    # Here output order is based on name_expr
    expr1 = Equality(C, (x + y)*z)
    expr2 = Equality(A, (x - y)*z)
    expr3 = Equality(B, 2*x)
    name_expr = ("test", [expr1, expr2, expr3])
    result, = codegen(name_expr, "Octave", header=False, empty=False)
    source = result[1]
    expected = (
        "function [C, A, B] = test(x, y, z)\n"
        "  C = z.*(x + y);\n"
        "  A = z.*(x - y);\n"
        "  B = 2*x;\n"
        "end\n"
    )
    assert source == expected
Пример #59
0
def test_m_results_matrix_named_ordered():
    A = MatrixSymbol('A', 1, 3)
    expr1 = Equality(C, (x + y)*z)
    expr2 = Equality(A, Matrix([[1, 2, x]]))
    expr3 = Equality(B, 2*x)
    name_expr = ("test", [expr1, expr2, expr3])
    result, = codegen(name_expr, "Octave", header=False, empty=False,
                      argument_sequence=(x, z, y))
    source = result[1]
    expected = (
        "function [C, A, B] = test(x, z, y)\n"
        "  C = z.*(x + y);\n"
        "  A = [1 2 x];\n"
        "  B = 2*x;\n"
        "end\n"
    )
    assert source == expected
Пример #60
0
def test_m_simple_code_with_header():
    name_expr = ("test", (x + y)*z)
    result, = codegen(name_expr, "Octave", header=True, empty=False)
    assert result[0] == "test.m"
    source = result[1]
    expected = (
        "function out1 = test(x, y, z)\n"
        "  %TEST  Autogenerated by diofant\n"
        "  %   Code generated with diofant " + diofant.__version__ + "\n"
        "  %\n"
        "  %   See https://diofant.readthedocs.io/ for more information.\n"
        "  %\n"
        "  %   This file is part of 'project'\n"
        "  out1 = z.*(x + y);\n"
        "end\n"
    )
    assert source == expected