How to use the pyccel.ast.core.Variable function in pyccel

To help you get started, we’ve selected a few pyccel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyccel / pyccel / pyccel / functional / ast.py View on Github external
def _visit_TypeList(self, stmt):
        t_var = stmt

        rank = len(stmt)
        var = self._visit(stmt.types)

        if isinstance(var, Tuple):
            ls = []
            for e,v in enumerate(var):
                d_var = _attributs_default()
                d_var = _attributs_from_type(v, d_var)
                d_var['rank'] += rank

                dtype = d_var.pop('dtype')
                name  = 'dummy_{}_{}'.format(e, stmt.tag)
                var   = Variable( dtype, name, **d_var )

                ls.append(var)

            return Tuple(*ls)

        elif isinstance(var, Variable):
            d_var = _attributs_default()
            d_var = _attributs_from_type(var, d_var)
            d_var['rank'] += rank

            dtype = d_var.pop('dtype')
            name  = 'dummy_{}'.format(stmt.tag)
            var = Variable( dtype, name, **d_var )

            return var
github pyccel / pyccel / pyccel / parser / semantic.py View on Github external
var   = body.target
            a     = self._visit(body.iterable, **settings)
            if isinstance(a, Range):
                var   = Variable('int', var.name)
                stop  = a.stop
                start = a.start
                step  = a.step
            elif isinstance(a, (Zip, Enumerate)):
                dvar  = self._infere_type(a.element, **settings)
                dtype = dvar.pop('datatype')
                if dvar['rank'] > 0:
                    dvar['rank' ] -= 1
                    dvar['shape'] = (dvar['shape'])[1:]
                if dvar['rank'] == 0:
                    dvar['allocatable'] = dvar['is_pointer'] = False
                var  = Variable(dtype, var.name, **dvar)
                stop = a.element.shape[0]
            elif isinstance(a, Variable):
                dvar  = self._infere_type(a, **settings)
                dtype = dvar.pop('datatype')
                if dvar['rank'] > 0:
                    dvar['rank'] -= 1
                    dvar['shape'] = (dvar['shape'])[1:]
                if dvar['rank'] == 0:
                    dvar['allocatable'] = dvar['is_pointer'] = False

                var  = Variable(dtype, var.name, **dvar)
                stop = a.shape[0]
            else:
                errors.report(PYCCEL_RESTRICTION_TODO,
                              bounding_box=(self._current_fst_node.lineno, self._current_fst_node.col_offset),
                              severity='fatal')
github pyccel / pyccel / pyccel / parser / semantic.py View on Github external
rhs = new_rhs
            else:
                errors.report(WRONG_NUMBER_OUTPUT_ARGS, symbol=expr,
                    bounding_box=(self._current_fst_node.lineno, self._current_fst_node.col_offset),
                    severity='error', blocker=self.blocking)
                return None
        else:
            lhs = self._visit(lhs, **settings)

        if isinstance(rhs, (Map, Zip)):
            func  = _get_name(rhs.args[0])
            func  = UndefinedFunction(func)
            alloc = Assign(lhs, Zeros(lhs.shape, lhs.dtype))
            alloc.set_fst(fst)
            index = self._get_new_variable(expr)
            index = Variable('int',index.name)
            range_ = UndefinedFunction('range')(UndefinedFunction('len')(lhs))
            name  = _get_name(lhs)
            var   = IndexedBase(name)[index]
            args  = rhs.args[1:]
            args  = [_get_name(arg) for arg in args]
            args  = [IndexedBase(arg)[index] for arg in args]
            body  = [Assign(var, func(*args))]
            body[0].set_fst(fst)
            body  = For(index, range_, body, strict=False)
            body  = self._visit_For(body, **settings)
            body  = [alloc , body]
            return CodeBlock(body)

        elif not isinstance(lhs, (list, tuple)):
            lhs = [lhs]
            if isinstance(d_var,dict):
github pyccel / pyccel / pyccel / parser / semantic.py View on Github external
elif isinstance(lhs, (PythonTuple, List)):
            is_pointer = any(l.is_pointer for l in lhs)

        # TODO: does is_pointer refer to any/all or last variable in list (currently last)
        is_pointer = is_pointer and isinstance(rhs, (Variable, Dlist, DottedVariable))
        is_pointer = is_pointer or isinstance(lhs, (Variable, DottedVariable)) and lhs.is_pointer

        # ISSUES #177: lhs must be a pointer when rhs is allocatable array
        if not ((isinstance(lhs, PythonTuple) or (isinstance(lhs, TupleVariable) and not lhs.is_homogeneous)) \
                and isinstance(rhs,(PythonTuple, TupleVariable, list))):
            lhs = [lhs]
            rhs = [rhs]

        new_expressions = []
        for l, r in zip(lhs,rhs):
            is_pointer_i = l.is_pointer if isinstance(l, (Variable, DottedVariable)) else is_pointer

            new_expr = Assign(l, r)

            if is_pointer_i:
                new_expr = AliasAssign(l, r)

            elif isinstance(expr, AugAssign):
                new_expr = AugAssign(l, expr.op, r)


            elif new_expr.is_symbolic_alias:
                new_expr = SymbolicAssign(l, r)

                # in a symbolic assign, the rhs can be a lambda expression
                # it is then treated as a def node
github pyccel / pyccel / src_old / syntax_core.py View on Github external
raise TypeError('Expecting a Lambda function, given'
                            ' {0}'.format(type(func)))

        f_name = str(func)

        arguments = []
        for a in func.variables:
            arg = Variable('double', str(a))
            arguments.append(arg)


        # since we allow Lambda expressions to return a Tuple,
        # we have to use isinstance
        if isinstance(func.expr, (Tuple, list, tuple)):
            n = len(func.expr)
            x_out   = Variable('double', 'x_out', rank=1, shape=n)
            results = [x_out]

            expressions = []
            for i in func.expr:
                expressions += [sympify(i).evalf()]
            expr = Tuple(*expressions)
        else:
            # TODO to treat other cases
            x_out   = Variable('double', 'x_out')
            results = [x_out]

            expr = sympify(func.expr).evalf()

        body = [Assign(x_out, expr)]

        # TODO local_vars must be updated inside FunctionDef
github pyccel / pyccel / pyccel / parser / semantic.py View on Github external
d_var = self._infere_type(ah, **settings)
                    dtype = d_var.pop('datatype')

                    # this is needed for the static case

                    additional_args = []
                    if isinstance(a, ValuedArgument):

                        # optional argument only if the value is None
                        if isinstance(a.value, Nil):
                            d_var['is_optional'] = True

                        a_new = ValuedVariable(dtype, str(a.name),
                                    value=a.value, **d_var)
                    else:
                        a_new = Variable(dtype, a.name, **d_var)

                    if additional_args:
                        args += additional_args

                    args.append(a_new)
                    self.insert_variable(a_new, name=str(a_new.name))

            results = expr.results
            if header_results:
                new_results = []

                for a, ah in zip(results, header_results):
                    d_var = self._infere_type(ah, **settings)
                    dtype = d_var.pop('datatype')
                    a_new = Variable(dtype, a.name, **d_var)
                    self.insert_variable(a_new, name=str(a_new.name))
github pyccel / pyccel / pyccel / ast / f2py.py View on Github external
def sanitize_arguments(args):
    _args = []
    for a in args:
        if isinstance( a, Variable ):
            _args.append(a)

        elif isinstance( a, IndexedVariable ):
            a_new = Variable( a.dtype, str(a.name),
                              shape       = a.shape,
                              rank        = a.rank,
                              order       = a.order,
                              precision   = a.precision)

            _args.append(a_new)

        else:
            raise NotImplementedError('TODO for {}'.format(type(a)))

    return _args
github pyccel / pyccel / pyccel / ast / numpyext.py View on Github external
def index(self):
        ind = Variable('int','ind1')

        return ind
github pyccel / pyccel / pyccel / ast / core.py View on Github external
names = []
    for i in ends:
        if isinstance(i, IndexedElement):
            names.append(str(i.base))
        else:
            names.append(str(i))
    names = list(set(names))

    inits = {}
    for stmt in init_method.body:
        if isinstance(stmt, Assign):
            if str(stmt.lhs) in names:
                expr = stmt.rhs
                for (a_old, a_new) in zip(args, params):
                    dtype = datatype(stmt.rhs.dtype)
                    v_old = Variable(dtype, a_old)
                    if isinstance(a_new, (IndexedVariable,
                                  IndexedElement, str, Variable)):
                        v_new = Variable(dtype, a_new)
                    else:
                        v_new = a_new
                    expr = subs(expr, v_old, v_new)
                    inits[str(stmt.lhs)] = expr

    _ends = []
    for i in ends:
        if isinstance(i, IndexedElement):
            _ends.append(i.base)
        else:
            _ends.append(i)
    ends = [inits[str(i)] for i in _ends]
github pyccel / pyccel / pyccel / codegen / printing / fcode.py View on Github external
def _print_MacroShape(self, expr):
        var = expr.argument
        if not isinstance(var, (Variable, IndexedElement)):
            raise TypeError('Expecting a variable, given {}'.format(type(var)))
        shape = None
        if isinstance(var, Variable):
            shape = var.shape

        if shape is None:
            rank = var.rank
            shape = []
            for i in range(0, rank):
                l = 'lbound({var},{i})'.format(var=self._print(var),
                                               i=self._print(i+1))
                u = 'ubound({var},{i})'.format(var=self._print(var),
                                               i=self._print(i+1))
                s = '{u}-{l}+1'.format(u=u, l=l)
                shape.append(s)