Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mzmzm committed Mar 15, 2022
1 parent 2304371 commit af4c3b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 11 additions & 6 deletions python/taichi/lang/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def _linearize_entry_id(self, *args):
' for i in ti.static(range(3)):\n'
' print(i, "-th component is", vec[i])\n'
'See https://docs.taichi.graphics/lang/articles/advanced/meta#when-to-use-for-loops-with-tistatic for more details.'
'Or turn on ti.init(..., dynamic_index=True) to support indexing with variables!'
)
assert 0 <= args[0] < self.n, \
f"The 0-th matrix index is out of range: 0 <= {args[0]} < {self.n}"
Expand Down Expand Up @@ -263,6 +264,16 @@ def _get_slice(self, a, b):
def _cal_slice(self, index, dim):
start, stop, step = index.start or 0, index.stop or (
self.n if dim == 0 else self.m), index.step or 1

def helper(x):
# TODO(mzmzm): support variable in slice
if isinstance(x, expr.Expr):
raise TaichiCompilationError(
"Taichi does not support variables in slice now, please use constant instead of it."
)
return x

start, stop, step = helper(start), helper(stop), helper(step)
return [_ for _ in range(start, stop, step)]

@taichi_scope
Expand All @@ -283,12 +294,6 @@ def _subscript(self, *indices):
i = [i]
if not isinstance(j, list):
j = [j]

if self.local_tensor_proxy is None:
if isinstance(i[0], expr.Expr) or isinstance(j[0], expr.Expr):
raise TaichiCompilationError(
"Please turn on ti.init(..., dynamic_index=True) to support indexing with variables!"
)
if len(indices) == 1:
return Vector([self._subscript(a) for a in i])
return Matrix([[self._subscript(a, b) for b in j] for a in i])
Expand Down
10 changes: 9 additions & 1 deletion tests/python/test_simple_matrix_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def test_one_col_slice() -> ti.types.matrix(1, 3, dtype=ti.i32):
with pytest.raises(
ti.TaichiCompilationError,
match=
"Please turn on ti.init\(..., dynamic_index=True\) to support indexing with variables!"
'The 0-th index of a Matrix/Vector must be a compile-time constant '
"integer, got <class 'taichi.lang.expr.Expr'>.\n"
'This is because matrix operations will be \*\*unrolled\*\* at compile-time '
'for performance reason.\n'
'If you want to \*iterate through matrix elements\*, use a static range:\n'
' for i in ti.static\(range\(3\)\):\n'
' print\(i, "-th component is", vec\[i\]\)\n'
'See https://docs.taichi.graphics/lang/articles/advanced/meta#when-to-use-for-loops-with-tistatic for more details.'
'Or turn on ti.init\(..., dynamic_index=True\) to support indexing with variables!'
):
test_one_col_slice()

0 comments on commit af4c3b1

Please sign in to comment.