-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathtest_simple_matrix_slice.py
68 lines (55 loc) · 2.1 KB
/
test_simple_matrix_slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
import taichi as ti
from tests import test_utils
@test_utils.test()
def test_slice():
b = 3
@ti.kernel
def foo1() -> ti.types.vector(3, dtype=ti.i32):
c = ti.Vector([0, 1, 2, 3, 4, 5, 6])
return c[:5:2]
@ti.kernel
def foo2() -> ti.types.matrix(2, 2, dtype=ti.i32):
a = ti.Matrix([[1, 2, 3], [4, 5, 6]])
return a[:, :b:2]
v1 = foo1()
assert (v1 == ti.Vector([0, 2, 4])).all() == 1
m1 = foo2()
assert (m1 == ti.Matrix([[1, 3], [4, 6]])).all() == 1
@test_utils.test(dynamic_index=True)
def test_dyn():
@ti.kernel
def test_one_row_slice() -> ti.types.matrix(2, 1, dtype=ti.i32):
m = ti.Matrix([[1, 2, 3], [4, 5, 6]])
index = 1
return m[:, index]
@ti.kernel
def test_one_col_slice() -> ti.types.matrix(1, 3, dtype=ti.i32):
m = ti.Matrix([[1, 2, 3], [4, 5, 6]])
index = 1
return m[index, :]
r1 = test_one_row_slice()
assert (r1 == ti.Matrix([[2], [5]])).all() == 1
c1 = test_one_col_slice()
assert (c1 == ti.Matrix([[4, 5, 6]])).all() == 1
@test_utils.test(dynamic_index=False)
def test_no_dyn():
@ti.kernel
def test_one_col_slice() -> ti.types.matrix(1, 3, dtype=ti.i32):
m = ti.Matrix([[1, 2, 3], [4, 5, 6]])
index = 1
return m[index, :]
with pytest.raises(
ti.TaichiCompilationError,
match=
r'The 0-th index of a Matrix/Vector must be a compile-time constant '
r"integer, got <class 'taichi.lang.expr.Expr'>.\n"
r'This is because matrix operations will be \*\*unrolled\*\* at compile-time '
r'for performance reason.\n'
r'If you want to \*iterate through matrix elements\*, use a static range:\n'
r' for i in ti.static\(range\(3\)\):\n'
r' print\(i, "-th component is", vec\[i\]\)\n'
r'See https://docs.taichi-lang.org/docs/meta#when-to-use-tistatic-with-for-loops for more details.'
r'Or turn on ti.init\(..., dynamic_index=True\) to support indexing with variables!'
):
test_one_col_slice()