def build_IfExp(ctx, node): node.test = build_stmt(ctx, node.test) node.body = build_stmt(ctx, node.body) node.orelse = build_stmt(ctx, node.orelse) if ti.is_taichi_class(node.test.ptr) or ti.is_taichi_class( node.body.ptr) or ti.is_taichi_class(node.orelse.ptr): node.ptr = ti.select(node.test.ptr, node.body.ptr, node.orelse.ptr) return node is_static_if = (IRBuilder.get_decorator(ctx, node.test) == "static") if is_static_if: if node.test.ptr: node.body = build_stmt(ctx, node.body) node.ptr = node.body.ptr else: node.orelse = build_stmt(ctx, node.orelse) node.ptr = node.orelse.ptr return node val = ti.expr_init(None) ti.begin_frontend_if(node.test.ptr) ti.core.begin_frontend_if_true() val.assign(node.body.ptr) ti.core.pop_scope() ti.core.begin_frontend_if_false() val.assign(node.orelse.ptr) ti.core.pop_scope() node.ptr = val return node
def build_IfExp(ctx, node): build_stmt(ctx, node.test) build_stmt(ctx, node.body) build_stmt(ctx, node.orelse) if ti.is_taichi_class(node.test.ptr) or ti.is_taichi_class( node.body.ptr) or ti.is_taichi_class(node.orelse.ptr): node.ptr = ti.select(node.test.ptr, node.body.ptr, node.orelse.ptr) return node.ptr is_static_if = (ASTTransformer.get_decorator(ctx, node.test) == "static") if is_static_if: if node.test.ptr: node.ptr = build_stmt(ctx, node.body) else: node.ptr = build_stmt(ctx, node.orelse) return node.ptr val = ti.expr_init(None) ti.begin_frontend_if(node.test.ptr) _ti_core.begin_frontend_if_true() val.assign(node.body.ptr) _ti_core.pop_scope() _ti_core.begin_frontend_if_false() val.assign(node.orelse.ptr) _ti_core.pop_scope() node.ptr = val return node.ptr
def truth(cond): ''' return 1 if condition is not false, return 0 vice versa :param x: :return: The return value is computed as `return ti.select(cond, 1, 0)` note: Currently Taichi use -1 to represent True. This function serves as a helper function to those who want 1 to represent True ''' return ti.select(cond, 1, 0)
def test_constant_matrices(): assert ti.cos(ti.math.pi / 3) == approx(0.5) assert np.allclose((-ti.Vector([2, 3])).to_numpy(), np.array([-2, -3])) assert ti.cos(ti.Vector([2, 3])).to_numpy() == approx(np.cos(np.array([2, 3]))) assert ti.max(2, 3) == 3 res = ti.max(4, ti.Vector([3, 4, 5])) assert np.allclose(res.to_numpy(), np.array([4, 4, 5])) res = ti.Vector([2, 3]) + ti.Vector([3, 4]) assert np.allclose(res.to_numpy(), np.array([5, 7])) res = ti.atan2(ti.Vector([2, 3]), ti.Vector([3, 4])) assert res.to_numpy() == approx( np.arctan2(np.array([2, 3]), np.array([3, 4]))) res = ti.Matrix([[2, 3], [4, 5]]) @ ti.Vector([2, 3]) assert np.allclose(res.to_numpy(), np.array([13, 23])) v = ti.Vector([3, 4]) w = ti.Vector([5, -12]) r = ti.Vector([1, 2, 3, 4]) s = ti.Matrix([[1, 2], [3, 4]]) assert v.normalized().to_numpy() == approx(np.array([0.6, 0.8])) assert v.cross(w) == approx(-12 * 3 - 4 * 5) w.y = v.x * w[0] r.x = r.y r.y = r.z r.z = r.w r.w = r.x assert np.allclose(w.to_numpy(), np.array([5, 15])) assert ti.select(ti.Vector([1, 0]), ti.Vector([2, 3]), ti.Vector([4, 5])) == ti.Vector([2, 5]) s[0, 1] = 2 assert s[0, 1] == 2 @ti.kernel def func(t: ti.i32): m = ti.Matrix([[2, 3], [4, t]]) print(m @ ti.Vector([2, 3])) m += ti.Matrix([[3, 4], [5, t]]) print(m @ v) print(r.x, r.y, r.z, r.w) s = w.transpose() @ m print(s) print(m) func(5)
def func(): x[0] = ti.select(y[None], z[None], w[None])