Пример #1
0
def test_Declaration():
    u = symbols('u', real=True)
    vu = Variable(u, type=Type.from_expr(u))
    assert Declaration(vu).variable.type == real
    vn = Variable(n, type=Type.from_expr(n))
    assert Declaration(vn).variable.type == integer

    lt = StrictLessThan(vu, vn)
    assert isinstance(lt, StrictLessThan)

    vuc = Variable(u, Type.from_expr(u), value=3.0, attrs={value_const})
    assert value_const in vuc.attrs
    assert pointer_const not in vuc.attrs
    decl = Declaration(vuc)
    assert decl.variable == vuc
    assert isinstance(decl.variable.value, Float)
    assert decl.variable.value == 3.0
    assert decl.func(*decl.args) == decl
    assert vuc.as_Declaration() == decl
    assert vuc.as_Declaration(value=None, attrs=None) == Declaration(vu)

    vy = Variable(y, type=integer, value=3)
    decl2 = Declaration(vy)
    assert decl2.variable == vy
    assert decl2.variable.value == Integer(3)

    vi = Variable(i, type=Type.from_expr(i), value=3.0)
    decl3 = Declaration(vi)
    assert decl3.variable.type == integer
    assert decl3.variable.value == 3.0

    raises(ValueError, lambda: Declaration(vi, 42))
Пример #2
0
        def visit_Assignment(self, node):
            """Visitor Function for Assignment

            Visits each Assignment is the LFortran ASR and creates corresponding
            assignment for SymPy.

            Notes
            =====

            The function currently only supports variable assignment and binary
            operation assignments of varying multitudes. Any type of numberS or
            array is not supported.

            Raises
            ======

            NotImplementedError() when called for Numeric assignments or Arrays

            """
            # TODO: Arithmatic Assignment
            if isinstance(node.target, asr.Variable):
                target = node.target
                value = node.value
                if isinstance(value, asr.Variable):
                    new_node = Assignment(Variable(target.name), Variable(value.name))
                elif type(value) == asr.BinOp:
                    exp_ast = call_visitor(value)
                    for expr in exp_ast:
                        new_node = Assignment(Variable(target.name), expr)
                else:
                    raise NotImplementedError("Numeric assignments not supported")
            else:
                raise NotImplementedError("Arrays not supported")
            self._py_ast.append(new_node)
Пример #3
0
    def test_c_parse():
        src1 = """\
        int a, b = 4;
        float c, d = 2.4;
        """
        expr1.convert_to_expr(src1, 'c')
        ls = expr1.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol('a'), type=IntBaseType(String('intc'))))
        assert ls[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('intc')),
                     value=Integer(4)))
        assert ls[2] == Declaration(
            Variable(Symbol('c'),
                     type=FloatType(String('float32'),
                                    nbits=Integer(32),
                                    nmant=Integer(23),
                                    nexp=Integer(8))))
        assert ls[3] == Declaration(
            Variable(Symbol('d'),
                     type=FloatType(String('float32'),
                                    nbits=Integer(32),
                                    nmant=Integer(23),
                                    nexp=Integer(8)),
                     value=Float('2.3999999999999999', precision=53)))
Пример #4
0
 def test_mul_binop():
     src1 = (
         src
         + """\
         d = a + b - c
         c = a * b + d
         s = p * q / r
         r = p * s + q / p
         """
     )
     expr1.convert_to_expr(src1, "f")
     ls1 = expr1.return_expr()
     for iter in range(8, 12):
         assert isinstance(ls1[iter], Assignment)
     assert ls1[8] == Assignment(
         Variable(Symbol("d")), Symbol("a") + Symbol("b") - Symbol("c")
     )
     assert ls1[9] == Assignment(
         Variable(Symbol("c")), Symbol("a") * Symbol("b") + Symbol("d")
     )
     assert ls1[10] == Assignment(
         Variable(Symbol("s")), Symbol("p") * Symbol("q") / Symbol("r")
     )
     assert ls1[11] == Assignment(
         Variable(Symbol("r")), Symbol("p") * Symbol("s") + Symbol("q") / Symbol("p")
     )
Пример #5
0
def test_ccode_Declaration():
    i = symbols("i", integer=True)
    var1 = Variable(i, type=Type.from_expr(i))
    dcl1 = Declaration(var1)
    assert ccode(dcl1) == "int i"

    var2 = Variable(x, type=float32, attrs={value_const})
    dcl2a = Declaration(var2)
    assert ccode(dcl2a) == "const float x"
    dcl2b = var2.as_Declaration(value=pi)
    assert ccode(dcl2b) == "const float x = M_PI"

    var3 = Variable(y, type=Type("bool"))
    dcl3 = Declaration(var3)
    printer = C89CodePrinter()
    assert "stdbool.h" not in printer.headers
    assert printer.doprint(dcl3) == "bool y"
    assert "stdbool.h" in printer.headers

    u = symbols("u", real=True)
    ptr4 = Pointer.deduced(u, attrs={pointer_const, restrict})
    dcl4 = Declaration(ptr4)
    assert ccode(dcl4) == "double * const restrict u"

    var5 = Variable(x, Type("__float128"), attrs={value_const})
    dcl5a = Declaration(var5)
    assert ccode(dcl5a) == "const __float128 x"
    var5b = Variable(var5.symbol, var5.type, pi, attrs=var5.attrs)
    dcl5b = Declaration(var5b)
    assert ccode(dcl5b) == "const __float128 x = M_PI"
Пример #6
0
def test_ccode_Declaration():
    i = symbols('i', integer=True)
    var1 = Variable(i, type=Type.from_expr(i))
    dcl1 = Declaration(var1)
    assert ccode(dcl1) == 'int i'

    var2 = Variable(x, type=float32, attrs={value_const})
    dcl2a = Declaration(var2)
    assert ccode(dcl2a) == 'const float x'
    dcl2b = var2.as_Declaration(value=pi)
    assert ccode(dcl2b) == 'const float x = M_PI'

    var3 = Variable(y, type=Type('bool'))
    dcl3 = Declaration(var3)
    printer = C89CodePrinter()
    assert 'stdbool.h' not in printer.headers
    assert printer.doprint(dcl3) == 'bool y'
    assert 'stdbool.h' in printer.headers

    u = symbols('u', real=True)
    ptr4 = Pointer.deduced(u, attrs={pointer_const, restrict})
    dcl4 = Declaration(ptr4)
    assert ccode(dcl4) == 'double * const restrict u'

    var5 = Variable(x, Type('__float128'), attrs={value_const})
    dcl5a = Declaration(var5)
    assert ccode(dcl5a) == 'const __float128 x'
    var5b = Variable(var5.symbol, var5.type, pi, attrs=var5.attrs)
    dcl5b = Declaration(var5b)
    assert ccode(dcl5b) == 'const __float128 x = M_PI'
Пример #7
0
        def perform_operation(self, lhs, rhs, op):
            """Performs operation supported by the SymPy core

            Returns
            =======

            combined_variable: list
                contains variable content and type of variable

            """
            lhs_value = self.get_expr_for_operand(lhs)
            rhs_value = self.get_expr_for_operand(rhs)
            if op == '+':
                return [Add(lhs_value, rhs_value), 'expr']
            if op == '-':
                return [Add(lhs_value, -rhs_value), 'expr']
            if op == '*':
                return [Mul(lhs_value, rhs_value), 'expr']
            if op == '/':
                return [Mul(lhs_value, Pow(rhs_value, Integer(-1))), 'expr']
            if op == '%':
                return [Mod(lhs_value, rhs_value), 'expr']
            if op in ['<', '<=', '>', '>=', '==', '!=']:
                return [Rel(lhs_value, rhs_value, op), 'expr']
            if op == '&&':
                return [And(as_Boolean(lhs_value), as_Boolean(rhs_value)), 'expr']
            if op == '||':
                return [Or(as_Boolean(lhs_value), as_Boolean(rhs_value)), 'expr']
            if op == '=':
                return [Assignment(Variable(lhs_value), rhs_value), 'expr']
            if op in ['+=', '-=', '*=', '/=', '%=']:
                return [aug_assign(Variable(lhs_value), op[0], rhs_value), 'expr']
Пример #8
0
def test_FunctionPrototype_and_FunctionDefinition():
    vx = Variable(x, type=real)
    vn = Variable(n, type=integer)
    fp1 = FunctionPrototype(real, 'power', [vx, vn])
    assert fp1.return_type == real
    assert fp1.name == String('power')
    assert fp1.parameters == Tuple(vx, vn)
    assert fp1 == FunctionPrototype(real, 'power', [vx, vn])
    assert fp1 != FunctionPrototype(real, 'power', [vn, vx])
    assert fp1.func(*fp1.args) == fp1

    body = [Assignment(x, x**n), Return(x)]
    fd1 = FunctionDefinition(real, 'power', [vx, vn], body)
    assert fd1.return_type == real
    assert str(fd1.name) == 'power'
    assert fd1.parameters == Tuple(vx, vn)
    assert fd1.body == CodeBlock(*body)
    assert fd1 == FunctionDefinition(real, 'power', [vx, vn], body)
    assert fd1 != FunctionDefinition(real, 'power', [vx, vn], body[::-1])
    assert fd1.func(*fd1.args) == fd1

    fp2 = FunctionPrototype.from_FunctionDefinition(fd1)
    assert fp2 == fp1

    fd2 = FunctionDefinition.from_FunctionPrototype(fp1, body)
    assert fd2 == fd1
Пример #9
0
    def test_c_parse():
        src1 = """\
        int a, b = 4;
        float c, d = 2.4;
        """
        expr1.convert_to_expr(src1, "c")
        ls = expr1.return_expr()

        assert ls[0] == Declaration(
            Variable(Symbol("a"),
                     type=IntBaseType(String("integer")),
                     value=Integer(0)))
        assert ls[1] == Declaration(
            Variable(Symbol("b"),
                     type=IntBaseType(String("integer")),
                     value=Integer(4)))
        assert ls[2] == Declaration(
            Variable(
                Symbol("c"),
                type=FloatBaseType(String("real")),
                value=Float("0.0", precision=53),
            ))
        assert ls[3] == Declaration(
            Variable(
                Symbol("d"),
                type=FloatBaseType(String("real")),
                value=Float("2.3999999999999999", precision=53),
            ))
Пример #10
0
    def test_function():
        c_src1 = "void fun1()" + "\n" + "{" + "\n" + "int a;" + "\n" + "}"
        c_src2 = ("int fun2()" + "\n" + "{" + "\n" + "int a;" + "\n" +
                  "return a;" + "\n" + "}")
        c_src3 = ("float fun3()" + "\n" + "{" + "\n" + "float b;" + "\n" +
                  "return b;" + "\n" + "}")
        c_src4 = "float fun4()" + "\n" + "{}"

        res1 = SymPyExpression(c_src1, "c").return_expr()
        res2 = SymPyExpression(c_src2, "c").return_expr()
        res3 = SymPyExpression(c_src3, "c").return_expr()
        res4 = SymPyExpression(c_src4, "c").return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String("fun1"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("a"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    ))),
        )

        assert res2[0] == FunctionDefinition(
            IntBaseType(String("integer")),
            name=String("fun2"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("a"),
                        type=IntBaseType(String("integer")),
                        value=Integer(0),
                    )),
                Return("a"),
            ),
        )

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String("real")),
            name=String("fun3"),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol("b"),
                        type=FloatBaseType(String("real")),
                        value=Float("0.0", precision=53),
                    )),
                Return("b"),
            ),
        )

        assert res4[0] == FunctionPrototype(FloatBaseType(String("real")),
                                            name=String("fun4"),
                                            parameters=())
def _mk_func1():
    declars = n, inp, out = Variable('n', integer), Pointer('inp',
                                                            real), Pointer(
                                                                'out', real)
    i = Variable('i', integer)
    whl = While(i < n, [Assignment(out[i], inp[i]), PreIncrement(i)])
    body = CodeBlock(i.as_Declaration(value=0), whl)
    return FunctionDefinition(void, 'our_test_function', declars, body)
Пример #12
0
        def transform_parm_decl(self, node):
            """Transformation function for Parameter Declaration

            Used to create parameter nodes for the required functions for the
            respective nodes in the clang AST

            Returns
            =======

            param : Codegen AST Node
                Variable node with the value nad type of the variable

            Raises
            ======

            ValueError if multiple children encountered in the parameter node

            """
            if (node.type.kind == cin.TypeKind.INT):
                type = IntBaseType(String('integer'))
                value = Integer(0)
            elif (node.type.kind == cin.TypeKind.FLOAT):
                type = FloatBaseType(String('real'))
                value = Float(0.0)
            try:
                children = node.get_children()
                child = next(children)

                # Any namespace nodes can be ignored
                while child.kind in [cin.CursorKind.NAMESPACE_REF,
                                     cin.CursorKind.TYPE_REF,
                                     cin.CursorKind.TEMPLATE_REF]:
                    child = next(children)

                # If there is a child, it is the default value of the parameter.
                args = self.transform(child)
                param = Variable(
                    node.spelling
                ).as_Declaration(
                    type = args[0],
                    value = args[1]
                )
            except StopIteration:
                param = Variable(
                    node.spelling
                ).as_Declaration(
                        type = type,
                        value = value
                )

            try:
                value = self.transform(next(children))
                raise ValueError("Can't handle multiple children on parameter")
            except StopIteration:
                pass

            return param
Пример #13
0
def test_FunctionPrototype_print():
    x = symbols('x')
    n = symbols('n', integer=True)
    vx = Variable(x, type=real)
    vn = Variable(n, type=integer)
    fp1 = FunctionPrototype(real, 'power', [vx, vn])
    # Should be changed to proper test once multi-line generation is working
    # see https://github.com/sympy/sympy/issues/15824
    raises(NotImplementedError, lambda: fcode(fp1))
Пример #14
0
def _mk_func1():
    declars = n, inp, out = (
        Variable("n", integer),
        Pointer("inp", real),
        Pointer("out", real),
    )
    i = Variable("i", integer)
    whl = While(i < n, [Assignment(out[i], inp[i]), PreIncrement(i)])
    body = CodeBlock(i.as_Declaration(value=0), whl)
    return FunctionDefinition(void, "our_test_function", declars, body)
Пример #15
0
def test_FunctionDefinition_print():
    x = symbols('x')
    n = symbols('n', integer=True)
    vx = Variable(x, type=real)
    vn = Variable(n, type=integer)
    body = [Assignment(x, x**n), Return(x)]
    fd1 = FunctionDefinition(real, 'power', [vx, vn], body)
    # Should be changed to proper test once multi-line generation is working
    # see https://github.com/sympy/sympy/issues/15824
    raises(NotImplementedError, lambda: fcode(fd1))
Пример #16
0
    def test_parse():
        c_src1 = (
            'int a;' + '\n' +
            'int b;' + '\n'
        )
        c_src2 = (
            'void fun1()' + '\n' +
            '{' + '\n' +
            'int a;' + '\n' +
            '}'
        )

        f1 = open('..a.h', 'w')
        f2 = open('..b.h', 'w')

        f1.write(c_src1)
        f2. write(c_src2)

        f1.close()
        f2.close()

        res1 = SymPyExpression('..a.h', 'c').return_expr()
        res2 = SymPyExpression('..b.h', 'c').return_expr()

        os.remove('..a.h')
        os.remove('..b.h')

        assert res1[0] == Declaration(
            Variable(
                Symbol('a'),
                type=IntBaseType(String('integer')),
                value=Integer(0)
            )
        )
        assert res1[1] == Declaration(
            Variable(
                Symbol('b'),
                type=IntBaseType(String('integer')),
                value=Integer(0)
            )
        )
        assert res2[0] == FunctionDefinition(
            NoneToken(),
            name=String('fun1'),
            parameters=(),
            body=CodeBlock(
                Declaration(
                    Variable(
                        Symbol('a'),
                        type=IntBaseType(String('integer')),
                        value=Integer(0)
                    )
                )
            )
        )
Пример #17
0
def test_union():
    vx, vy = Variable(x, type=float64), Variable(y, type=int64)
    u = union("dualuse", [vx, vy])
    assert u.func(*u.args) == u
    assert u == union("dualuse", (vx, vy))
    assert str(u.name) == "dualuse"
    assert len(u.declarations) == 2
    assert all(isinstance(arg, Declaration) for arg in u.declarations)
    assert ccode(u) == ("union dualuse {\n"
                        "   double x;\n"
                        "   int64_t y;\n"
                        "}")
Пример #18
0
 def kernel_arg_local_assign(self, args_var):
     member_types = {
         "int32": lambda x: Variable(x, type=int32),
         "uint32": lambda x: Variable(x, type=uint32),
         "pint8": lambda x: Pointer(x, type=int8, attrs=(restrict, )),
     }
     return [
         AssignmentEx(
             Declaration(member_types[kernel_arg[1]](kernel_arg[0])),
             StructVariable(args_var, Symbol(kernel_arg[0])))
         for kernel_arg in self.kernel_args
     ]
Пример #19
0
    def test_int():
        c_src1 = 'int a = 1;'
        c_src2 = ('int a = 1;' + '\n' + 'int b = 2;' + '\n')
        c_src3 = 'int a = 2.345, b = 5.67;'
        c_src4 = 'int p = 6, q = 23.45;'
        c_src5 = "int x = '0', y = 'a';"

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()
        res4 = SymPyExpression(c_src4, 'c').return_expr()
        res5 = SymPyExpression(c_src5, 'c').return_expr()

        assert res1[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(1)))

        assert res2[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(1)))

        assert res2[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(2)))

        assert res3[0] == Declaration(
            Variable(Symbol('a'),
                     type=IntBaseType(String('integer')),
                     value=Integer(2)))

        assert res3[1] == Declaration(
            Variable(Symbol('b'),
                     type=IntBaseType(String('integer')),
                     value=Integer(5)))

        assert res4[0] == Declaration(
            Variable(Symbol('p'),
                     type=IntBaseType(String('integer')),
                     value=Integer(6)))

        assert res4[1] == Declaration(
            Variable(Symbol('q'),
                     type=IntBaseType(String('integer')),
                     value=Integer(23)))

        assert res5[0] == Declaration(
            Variable(Symbol('x'),
                     type=IntBaseType(String('integer')),
                     value=Integer(48)))

        assert res5[1] == Declaration(
            Variable(Symbol('y'),
                     type=IntBaseType(String('integer')),
                     value=Integer(97)))
Пример #20
0
def test_struct():
    vx, vy = Variable(x, type=float64), Variable(y, type=float64)
    s = struct("vec2", [vx, vy])
    assert s.func(*s.args) == s
    assert s == struct("vec2", (vx, vy))
    assert s != struct("vec2", (vy, vx))
    assert str(s.name) == "vec2"
    assert len(s.declarations) == 2
    assert all(isinstance(arg, Declaration) for arg in s.declarations)
    assert ccode(s) == ("struct vec2 {\n"
                        "   double x;\n"
                        "   double y;\n"
                        "}")
Пример #21
0
    def test_parameters():
        c_src1 = ('void fun1( int a)' + '\n' + '{' + '\n' + 'int i;' + '\n' +
                  '}')
        c_src2 = ('int fun2(float x, float y)' + '\n' + '{' + '\n' + 'int a;' +
                  '\n' + 'return a;' + '\n' + '}')
        c_src3 = ('float fun3(int p, float q, int r)' + '\n' + '{' + '\n' +
                  'float b;' + '\n' + 'return b;' + '\n' + '}')

        res1 = SymPyExpression(c_src1, 'c').return_expr()
        res2 = SymPyExpression(c_src2, 'c').return_expr()
        res3 = SymPyExpression(c_src3, 'c').return_expr()

        assert res1[0] == FunctionDefinition(
            NoneToken(),
            name=String('fun1'),
            parameters=(Variable(Symbol('a'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0)), ),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('i'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0)))))

        assert res2[0] == FunctionDefinition(
            IntBaseType(String('integer')),
            name=String('fun2'),
            parameters=(Variable(Symbol('x'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53)),
                        Variable(Symbol('y'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53))),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('a'),
                             type=IntBaseType(String('integer')),
                             value=Integer(0))), Return('a')))

        assert res3[0] == FunctionDefinition(
            FloatBaseType(String('real')),
            name=String('fun3'),
            parameters=(Variable(Symbol('p'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0)),
                        Variable(Symbol('q'),
                                 type=FloatBaseType(String('real')),
                                 value=Float('0.0', precision=53)),
                        Variable(Symbol('r'),
                                 type=IntBaseType(String('integer')),
                                 value=Integer(0))),
            body=CodeBlock(
                Declaration(
                    Variable(Symbol('b'),
                             type=FloatBaseType(String('real')),
                             value=Float('0.0', precision=53))), Return('b')))
Пример #22
0
 def test_var():
     expr1.convert_to_expr(src, "f")
     ls = expr1.return_expr()
     for iter in expr1.return_expr():
         assert isinstance(iter, Declaration)
     assert ls[0] == Declaration(
         Variable(Symbol("a"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[1] == Declaration(
         Variable(Symbol("b"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[2] == Declaration(
         Variable(Symbol("c"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[3] == Declaration(
         Variable(Symbol("d"), type=IntBaseType(String("integer")), value=Integer(0))
     )
     assert ls[4] == Declaration(
         Variable(Symbol("p"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[5] == Declaration(
         Variable(Symbol("q"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[6] == Declaration(
         Variable(Symbol("r"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
     assert ls[7] == Declaration(
         Variable(Symbol("s"), type=FloatBaseType(String("real")), value=Float(0.0))
     )
Пример #23
0
 def test_function():
     src1 = """\
     integer function f(a,b)
     integer :: x, y
     f = x + y
     end function
     """
     expr1.convert_to_expr(src1, "f")
     for iter in expr1.return_expr():
         assert isinstance(iter, FunctionDefinition)
         assert iter == FunctionDefinition(
             IntBaseType(String("integer")),
             name=String("f"),
             parameters=(Variable(Symbol("a")), Variable(Symbol("b"))),
             body=CodeBlock(
                 Declaration(
                     Variable(
                         Symbol("a"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0),
                     )
                 ),
                 Declaration(
                     Variable(
                         Symbol("b"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0),
                     )
                 ),
                 Declaration(
                     Variable(
                         Symbol("f"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0),
                     )
                 ),
                 Declaration(
                     Variable(
                         Symbol("x"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0),
                     )
                 ),
                 Declaration(
                     Variable(
                         Symbol("y"),
                         type=IntBaseType(String("integer")),
                         value=Integer(0),
                     )
                 ),
                 Assignment(Variable(Symbol("f")), Add(Symbol("x"), Symbol("y"))),
                 Return(Variable(Symbol("f"))),
             ),
         )
Пример #24
0
def test_C99CodePrinter_custom_type():
    # We will look at __float128 (new in glibc 2.26)
    f128 = FloatType('_Float128', float128.nbits, float128.nmant,
                     float128.nexp)
    p128 = C99CodePrinter(
        dict(type_aliases={real: f128},
             type_literal_suffixes={f128: 'Q'},
             type_func_suffixes={f128: 'f128'},
             type_math_macro_suffixes={
                 real: 'f128',
                 f128: 'f128'
             },
             type_macros={f128: ('__STDC_WANT_IEC_60559_TYPES_EXT__', )}))
    assert p128.doprint(x) == 'x'
    assert not p128.headers
    assert not p128.libraries
    assert not p128.macros
    assert p128.doprint(2.0) == '2.0Q'
    assert not p128.headers
    assert not p128.libraries
    assert p128.macros == {'__STDC_WANT_IEC_60559_TYPES_EXT__'}

    assert p128.doprint(Rational(1, 2)) == '1.0Q/2.0Q'
    assert p128.doprint(sin(x)) == 'sinf128(x)'
    assert p128.doprint(cos(2., evaluate=False)) == 'cosf128(2.0Q)'

    var5 = Variable(x, {value_const}, f128)

    dcl5a = Declaration(var5)
    assert ccode(dcl5a) == 'const _Float128 x'
    dcl5b = Declaration(var5, pi)
    assert p128.doprint(dcl5b) == 'const _Float128 x = M_PIf128'
    dcl5c = Declaration(var5, Catalan.evalf(38))
    assert p128.doprint(dcl5c) == 'const _Float128 x = %sQ' % Catalan.evalf(
        f128.decimal_dig)
Пример #25
0
def newtons_method_function(expr, wrt, params=None, func_name="newton", attrs=Tuple(), **kwargs):
    """ Generates an AST for a function implementing the Newton-Raphson method.

    Parameters
    ==========

    expr : expression
    wrt : Symbol
        With respect to, i.e. what is the variable
    params : iterable of symbols
        Symbols appearing in expr that are taken as constants during the iterations
        (these will be accepted as parameters to the generated function).
    func_name : str
        Name of the generated function.
    attrs : Tuple
        Attribute instances passed as ``attrs`` to ``FunctionDefinition``.
    \\*\\*kwargs :
        Keyword arguments passed to :func:`sympy.codegen.algorithms.newtons_method`.

    Examples
    ========

    >>> from sympy import symbols, cos
    >>> from sympy.codegen.algorithms import newtons_method_function
    >>> from sympy.codegen.pyutils import render_as_module
    >>> from sympy.core.compatibility import exec_
    >>> x = symbols('x')
    >>> expr = cos(x) - x**3
    >>> func = newtons_method_function(expr, x)
    >>> py_mod = render_as_module(func)  # source code as string
    >>> namespace = {}
    >>> exec_(py_mod, namespace, namespace)
    >>> res = eval('newton(0.5)', namespace)
    >>> abs(res - 0.865474033102) < 1e-12
    True

    See Also
    ========

    sympy.codegen.algorithms.newtons_method

    """
    if params is None:
        params = (wrt,)
    pointer_subs = {p.symbol: Symbol('(*%s)' % p.symbol.name)
                    for p in params if isinstance(p, Pointer)}
    delta = kwargs.pop('delta', None)
    if delta is None:
        delta = Symbol('d_' + wrt.name)
        if expr.has(delta):
            delta = None  # will use Dummy
    algo = newtons_method(expr, wrt, delta=delta, **kwargs).xreplace(pointer_subs)
    if isinstance(algo, Scope):
        algo = algo.body
    not_in_params = expr.free_symbols.difference(set(_symbol_of(p) for p in params))
    if not_in_params:
        raise ValueError("Missing symbols in params: %s" % ', '.join(map(str, not_in_params)))
    declars = tuple(Variable(p, real) for p in params)
    body = CodeBlock(algo, Return(wrt))
    return FunctionDefinition(real, func_name, declars, body, attrs=attrs)
Пример #26
0
def test_binop_add():
    src1 = (src + """\
        c = a + b
        d = a + c
        s = p + q + r
        """)
    expr1.convert_to_expr(src1, 'f')
    ls1 = expr1.return_expr()
    for iter in range(8, 11):
        assert isinstance(ls1[iter], Assignment)
    assert ls1[8] == Assignment(Variable(Symbol('c')),
                                Symbol('a') + Symbol('b'))
    assert ls1[9] == Assignment(Variable(Symbol('d')),
                                Symbol('a') + Symbol('c'))
    assert ls1[10] == Assignment(Variable(Symbol('s')),
                                 Symbol('p') + Symbol('q') + Symbol('r'))
def test_Variable():
    v = Variable(x, type_=Type('real'))
    assert v.symbol == x
    assert v.type == real
    assert v.value_const == False
    w = Variable(y, {value_const}, f32)
    assert w.symbol == y
    assert w.type == f32
    assert w.value_const
    v_n = Variable(n, type_=Type.from_expr(n))
    assert v_n.type == integer
    v_i = Variable(i, type_=Type.from_expr(n))
    assert v_i.type == integer

    a_i = Variable.deduced(i)
    assert a_i.type == integer
Пример #28
0
def test_ccode_codegen_ast():
    assert ccode(Comment("this is a comment")) == "// this is a comment"
    assert ccode(While(abs(x) > 1,
                       [aug_assign(x, '-', 1)])) == ('while (fabs(x) > 1) {\n'
                                                     '   x -= 1;\n'
                                                     '}')
    assert ccode(Scope([AddAugmentedAssignment(x, 1)])) == ('{\n'
                                                            '   x += 1;\n'
                                                            '}')
    inp_x = Declaration(Variable(x, type=real))
    assert ccode(FunctionPrototype(real, 'pwer',
                                   [inp_x])) == 'double pwer(double x)'
    assert ccode(
        FunctionDefinition(
            real, 'pwer', [inp_x],
            [Assignment(x, x**2)])) == ('double pwer(double x){\n'
                                        '   x = pow(x, 2);\n'
                                        '}')

    # Elements of CodeBlock are formatted as statements:
    block = CodeBlock(
        x,
        Print([x, y], "%d %d"),
        FunctionCall('pwer', [x]),
        Return(x),
    )
    assert ccode(block) == '\n'.join([
        'x;',
        'printf("%d %d", x, y);',
        'pwer(x);',
        'return x;',
    ])
Пример #29
0
        def visit_Variable(self, node):
            """Visitor Function for Variable Declaration

            Visits each variable declaration present in the ASR and creates a
            Symbol declaration for each variable

            Notes
            =====

            The functions currently only support declaration of integer and
            real variables. Other data types are still under development.

            Raises
            ======

            NotImplementedError() when called for unsupported data types

            """
            if isinstance(node.type, asr.Integer):
                var_type = IntBaseType(String('integer'))
                value = Integer(0)
            elif isinstance(node.type, asr.Real):
                var_type = FloatBaseType(String('real'))
                value = Float(0.0)
            else:
                raise NotImplementedError("Data type not supported")

            if not (node.intent == 'in'):
                new_node = Variable(node.name).as_Declaration(type=var_type,
                                                              value=value)
                self._py_ast.append(new_node)
Пример #30
0
 def _declare_number_const(self, name, value):
     type_ = self.type_aliases[real]
     var = Variable(name,
                    type=type_,
                    value=value.evalf(type_.decimal_dig),
                    attrs={value_const})
     decl = Declaration(var)
     return self._get_statement(self._print(decl))