Skip to content

Commit

Permalink
[bug] MatrixType bug fix: Fix indexing support for custom vector types (
Browse files Browse the repository at this point in the history
taichi-dev#6568)

Issue: taichi-dev#5819

### Brief Summary

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and quadpixels committed May 13, 2023
1 parent 4712e0c commit 505f6f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 18 additions & 0 deletions python/taichi/lang/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,11 @@ def __call__(self, *args):
entries += x
elif isinstance(x, np.ndarray):
entries += list(x.ravel())
elif isinstance(x, impl.Expr) and x.ptr.is_tensor():
entries += [
impl.Expr(e) for e in impl.get_runtime().prog.
current_ast_builder().expand_expr([x.ptr])
]
elif isinstance(x, Matrix):
entries += x.entries
else:
Expand All @@ -1797,6 +1802,10 @@ def cast(self, mat):
if isinstance(mat, impl.Expr) and mat.ptr.is_tensor():
return ops_mod.cast(mat, self.dtype)

if isinstance(mat, Matrix) and impl.current_cfg().real_matrix:
arr = mat.entries
return ops_mod.cast(make_matrix(arr), self.dtype)

return mat.cast(self.dtype)

def filled_with_scalar(self, value):
Expand Down Expand Up @@ -1863,6 +1872,11 @@ def __call__(self, *args):
entries += list(x.ravel())
elif isinstance(x, Matrix):
entries += x.entries
elif isinstance(x, impl.Expr) and x.ptr.is_tensor():
entries += [
impl.Expr(e) for e in impl.get_runtime().prog.
current_ast_builder().expand_expr([x.ptr])
]
else:
entries.append(x)

Expand All @@ -1884,6 +1898,10 @@ def cast(self, vec):
if isinstance(vec, impl.Expr) and vec.ptr.is_tensor():
return ops_mod.cast(vec, self.dtype)

if isinstance(vec, Matrix) and impl.current_cfg().real_matrix:
arr = vec.entries
return ops_mod.cast(make_matrix(arr), self.dtype)

return vec.cast(self.dtype)

def filled_with_scalar(self, value):
Expand Down
13 changes: 11 additions & 2 deletions tests/python/test_vector_swizzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def test_vector_swizzle_python():
assert all(z == w.xxxx)


@test_utils.test(debug=True)
def test_vector_swizzle_taichi():
def _test_vector_swizzle_taichi():
@ti.kernel
def foo():
v = ti.math.vec3(0)
Expand All @@ -50,6 +49,16 @@ def foo():
foo()


@test_utils.test(debug=True)
def test_vector_swizzle_taichi():
_test_vector_swizzle_taichi()


@test_utils.test(real_matrix=True, real_matrix_scalarize=True, debug=True)
def test_vector_swizzle_taichi_matrix_scalarize():
_test_vector_swizzle_taichi()


@test_utils.test(debug=True)
def test_vector_swizzle2_taichi():
@ti.kernel
Expand Down

0 comments on commit 505f6f5

Please sign in to comment.