Exemplo n.º 1
0
 def __init__(self, *args, tb=None):
     _taichi_skip_traceback = 1
     self.getter = None
     self.setter = None
     self.tb = tb
     if len(args) == 1:
         if isinstance(args[0], taichi_lang_core.Expr):
             self.ptr = args[0]
         elif isinstance(args[0], Expr):
             self.ptr = args[0].ptr
             self.tb = args[0].tb
         elif is_taichi_class(args[0]):
             raise ValueError('cannot initialize scalar expression from '
                              f'taichi class: {type(args[0])}')
         else:
             # assume to be constant
             arg = args[0]
             try:
                 import numpy as np
                 if isinstance(arg, np.ndarray):
                     arg = arg.dtype(arg)
             except:
                 pass
             self.ptr = impl.make_constant_expr(arg).ptr
     else:
         assert False
     if self.tb:
         self.ptr.set_tb(self.tb)
     self.grad = None
     self.val = self
Exemplo n.º 2
0
 def __init__(self, *args, tb=None):
     self.tb = tb
     if len(args) == 1:
         if isinstance(args[0], _ti_core.Expr):
             self.ptr = args[0]
         elif isinstance(args[0], Expr):
             self.ptr = args[0].ptr
             self.tb = args[0].tb
         elif is_taichi_class(args[0]):
             raise TaichiTypeError(
                 'Cannot initialize scalar expression from '
                 f'taichi class: {type(args[0])}')
         else:
             # assume to be constant
             arg = args[0]
             if isinstance(arg, np.ndarray):
                 if arg.shape:
                     raise TaichiTypeError(
                         "Only 0-dimensional numpy array can be used to initialize a scalar expression"
                     )
                 try:
                     arg = arg.dtype.type(arg)
                 except:
                     pass
             self.ptr = impl.make_constant_expr(arg).ptr
     else:
         assert False
     if self.tb:
         self.ptr.set_tb(self.tb)
     self.ptr.type_check()
Exemplo n.º 3
0
 def __init__(self, *args, tb=None):
     self.tb = tb
     if len(args) == 1:
         if isinstance(args[0], _ti_core.Expr):
             self.ptr = args[0]
         elif isinstance(args[0], Expr):
             self.ptr = args[0].ptr
             self.tb = args[0].tb
         elif is_taichi_class(args[0]):
             raise ValueError('cannot initialize scalar expression from '
                              f'taichi class: {type(args[0])}')
         else:
             # assume to be constant
             arg = args[0]
             try:
                 if isinstance(arg, np.ndarray):
                     arg = arg.dtype(arg)
             except:
                 pass
             self.ptr = impl.make_constant_expr(arg).ptr
     else:
         assert False
     if self.tb:
         self.ptr.set_tb(self.tb)
     self.ptr.type_check()
Exemplo n.º 4
0
    def build_assign_annotated(ctx, target, value, is_static_assign,
                               annotation):
        """Build an annotated assginment like this: target: annotation = value.

         Args:
            ctx (ast_builder_utils.BuilderContext): The builder context.
            target (ast.Name): A variable name. `target.id` holds the name as
            a string.
            annotation: A type we hope to assign to the target
            value: A node representing the value.
            is_static_assign: A boolean value indicating whether this is a static assignment
        """
        is_local = isinstance(target, ast.Name)
        anno = impl.expr_init(annotation)
        if is_static_assign:
            raise TaichiSyntaxError(
                "Static assign cannot be used on annotated assignment")
        if is_local and not ctx.is_var_declared(target.id):
            if isinstance(value, expr.Expr):
                var = ti_ops.cast(value, anno)
            else:
                var = impl.make_constant_expr(value, anno)
            var = impl.expr_init(var)
            ctx.create_variable(target.id, var)
        else:
            var = build_stmt(ctx, target)
            if var.ptr.get_ret_type() != anno:
                raise TaichiSyntaxError(
                    "Static assign cannot have type overloading")
            var._assign(value)
        return var