示例#1
0
    def fill(self, val):
        if impl.inside_kernel():

            def assign_renamed(x, y):
                return ti.assign(x, y)

            return self.element_wise_writeback_binary(assign_renamed, val)

        if isinstance(val, numbers.Number):
            val = tuple(
                [tuple([val for _ in range(self.m)]) for _ in range(self.n)])
        elif isinstance(val,
                        (list, tuple)) and isinstance(val[0], numbers.Number):
            assert self.m == 1
            val = tuple([(v, ) for v in val])
        if isinstance(val, Matrix):
            val_tuple = []
            for i in range(val.n):
                row = []
                for j in range(val.m):
                    row.append(val.get_entry(i, j))
                row = tuple(row)
                val_tuple.append(row)
            val = tuple(val_tuple)
        assert len(val) == self.n
        assert len(val[0]) == self.m
        from taichi.lang.meta import fill_matrix
        fill_matrix(self, val)
示例#2
0
    def fill(self, val):
        """Fills `self` with specific values.

        Args:
            val (Union[Number, List, Tuple, Matrix]): Values to fill, which should have dimension consistent with `self`.
        """
        if isinstance(val, numbers.Number):
            val = tuple(
                [tuple([val for _ in range(self.m)]) for _ in range(self.n)])
        elif isinstance(val,
                        (list, tuple)) and isinstance(val[0], numbers.Number):
            assert self.m == 1
            val = tuple([(v, ) for v in val])
        elif isinstance(val, ti.Matrix):
            val_tuple = []
            for i in range(val.n):
                row = []
                for j in range(val.m):
                    row.append(val(i, j))
                row = tuple(row)
                val_tuple.append(row)
            val = tuple(val_tuple)
        assert len(val) == self.n
        assert len(val[0]) == self.m
        from taichi.lang.meta import fill_matrix
        fill_matrix(self, val)
示例#3
0
    def fill(self, val):
        """Fill the element with values.

        Args:
            val (Union[Number, List, Tuple, Matrix]): the dimension of val should be consistent with the dimension of element.

        Examples:

            Fill a scalar field:

            >>> v = ti.field(float,10)
            >>> v.fill(10.0)

            Fill a vector field:

            >>> v = ti.Vector.field(2, float,4)
            >>> v.fill([10.0,11.0])

        """
        if impl.inside_kernel():

            def assign_renamed(x, y):
                return ti.assign(x, y)

            return self.element_wise_writeback_binary(assign_renamed, val)

        if isinstance(val, numbers.Number):
            val = tuple(
                [tuple([val for _ in range(self.m)]) for _ in range(self.n)])
        elif isinstance(val,
                        (list, tuple)) and isinstance(val[0], numbers.Number):
            assert self.m == 1
            val = tuple([(v, ) for v in val])
        if isinstance(val, Matrix):
            val_tuple = []
            for i in range(val.n):
                row = []
                for j in range(val.m):
                    row.append(val.get_entry(i, j))
                row = tuple(row)
                val_tuple.append(row)
            val = tuple(val_tuple)
        assert len(val) == self.n
        assert len(val[0]) == self.m
        from taichi.lang.meta import fill_matrix
        fill_matrix(self, val)