From 69b29e62aafdbbbc612cfe88878c6508891339f3 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Wed, 27 May 2020 23:16:06 +0800 Subject: [PATCH 01/17] [skip ci][doc] improve a bit in scalar_tensor.rst --- docs/scalar_tensor.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/scalar_tensor.rst b/docs/scalar_tensor.rst index 94ae57da44e70..962696ba385f4 100644 --- a/docs/scalar_tensor.rst +++ b/docs/scalar_tensor.rst @@ -92,7 +92,7 @@ You can access an element of the Taichi tensor by an index or indices. This sets the element value at index ``2`` of 1D tensor ``b`` to ``5``: :: - a[2] = 2 + b[2] = 5 .. note :: @@ -158,6 +158,7 @@ Meta data :return: (SNode) the parent of ``a``'s containing SNode :: + x = ti.var(ti.i32) y = ti.var(ti.i32) blk1 = ti.root.dense(ti.ij, (6, 5)) From 3ad3ddf7d9602ae14f280a1e1a2daa9d8022008d Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Wed, 27 May 2020 23:39:37 -0700 Subject: [PATCH 02/17] [lang] make cross function work for vec2d --- python/taichi/lang/matrix.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 6db47f9aac901..e757ea5dc3579 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -415,13 +415,19 @@ def E(x, y): @staticmethod def cross(a, b): - assert a.n == 3 and a.m == 1 - assert b.n == 3 and b.m == 1 - return Matrix([ - a(1) * b(2) - a(2) * b(1), - a(2) * b(0) - a(0) * b(2), - a(0) * b(1) - a(1) * b(0), - ]) + if a.n == 3 and a.m == 1 and b.n == 3 and b.m == 1: + return Matrix([ + a(1) * b(2) - a(2) * b(1), + a(2) * b(0) - a(0) * b(2), + a(0) * b(1) - a(1) * b(0), + ]) + + elif a.n == 2 and a.m == 1 and b.n == 2 and b.m == 1: + return Matrix(a(0) * b(1) - a(1) * b(0)) + + else: + raise Exception( + "CrossProduct only supports 3D vector and 2D vector") @staticmethod def diag(dim, val): @@ -571,10 +577,12 @@ def from_torch(self, torch_tensor): def __ti_repr__(self): yield '[' for i in range(self.n): - if i: yield ', ' + if i: + yield ', ' yield '[' for j in range(self.m): - if j: yield ', ' + if j: + yield ', ' yield self(i, j) yield ']' yield ']' From f8968ac73702968023ce509ba173b19007e33503 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Thu, 28 May 2020 15:22:37 +0800 Subject: [PATCH 03/17] Update python/taichi/lang/matrix.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit return true scalar instead of a 1x1 mat Co-authored-by: 彭于斌 <1931127624@qq.com> --- python/taichi/lang/matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index e757ea5dc3579..0b063fa444701 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -423,7 +423,7 @@ def cross(a, b): ]) elif a.n == 2 and a.m == 1 and b.n == 2 and b.m == 1: - return Matrix(a(0) * b(1) - a(1) * b(0)) + return a(0) * b(1) - a(1) * b(0) else: raise Exception( From c6772cba7b1b22fd21b75ab342916d54f29052a4 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Thu, 28 May 2020 03:44:48 -0400 Subject: [PATCH 04/17] [skip ci] enforce code format --- docs/scalar_tensor.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scalar_tensor.rst b/docs/scalar_tensor.rst index 962696ba385f4..46b3c34e35d39 100644 --- a/docs/scalar_tensor.rst +++ b/docs/scalar_tensor.rst @@ -158,7 +158,7 @@ Meta data :return: (SNode) the parent of ``a``'s containing SNode :: - + x = ti.var(ti.i32) y = ti.var(ti.i32) blk1 = ti.root.dense(ti.ij, (6, 5)) From a198ab0f13dd9463b152fd7b3ec5130dd3faf6c6 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Thu, 28 May 2020 19:24:55 -0700 Subject: [PATCH 05/17] [skip ci] clearer description of cross's exception --- python/taichi/lang/matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 0b063fa444701..8df9452bc51ed 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -427,7 +427,7 @@ def cross(a, b): else: raise Exception( - "CrossProduct only supports 3D vector and 2D vector") + "cross product is only supported between 3D or 2D vectors") @staticmethod def diag(dim, val): From 3ee305b60daa5462340ffe561059f52014effa2a Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Thu, 28 May 2020 20:04:50 -0700 Subject: [PATCH 06/17] [Test] improve basic ults test of matrix/linalg --- tests/python/test_linalg.py | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/tests/python/test_linalg.py b/tests/python/test_linalg.py index 5ca046208b3e8..c842e008827e3 100644 --- a/tests/python/test_linalg.py +++ b/tests/python/test_linalg.py @@ -3,6 +3,106 @@ from taichi import approx +@ti.all_archs +def test_BasicUlts(): + a = ti.Vector(3, dt=ti.f32) + b = ti.Vector(3, dt=ti.f32) + abT = ti.Matrix(3, 3, dt=ti.f32) + aNormalized = ti.Vector(3, dt=ti.f32) + + normA = ti.var(ti.f32) + normSqrA = ti.var(ti.f32) + + @ti.layout + def place(): + ti.root.place(a, b, abT, aNormalized, normA, normSqrA) + + @ti.kernel + def init(): + a[None] = ti.Vector([1.0, 2.0, 3.0]) + b[None] = ti.Vector([4.0, 5.0, 6.0]) + abT[None] = ti.Matrix.outer_product(a, b) + + normA[None] = a.norm() + normSqrA[None] = a.norm_sqr() + + aNormalized[None] = ti.Matrix.normalized(a) + + init() + + for i in range(3): + for j in range(3): + assert abT[None][i, j] == a[None][i]*b[None][j] + + sqrt14 = np.sqrt(14.0) + invSqrt14 = 1.0/sqrt14 + assert normSqrA[None] == 14.0 + assert normA[None] == approx(sqrt14) + assert aNormalized[None][0] == approx(1.0*invSqrt14) + assert aNormalized[None][1] == approx(2.0*invSqrt14) + assert aNormalized[None][2] == approx(3.0*invSqrt14) + + +@ti.all_archs +def test_cross(): + a = ti.Vector(3, dt=ti.f32) + b = ti.Vector(3, dt=ti.f32) + c = ti.Vector(3, dt=ti.f32) + + a2 = ti.Vector(2, dt=ti.f32) + b2 = ti.Vector(2, dt=ti.f32) + c2 = ti.var(dt=ti.f32) + + @ti.layout + def place(): + ti.root.place(a, b, c, a2, b2, c2) + + @ti.kernel + def init(): + a[None] = ti.Vector([1.0, 2.0, 3.0]) + b[None] = ti.Vector([4.0, 5.0, 6.0]) + c[None] = ti.Matrix.cross(a, b) + + a2[None] = ti.Vector([1.0, 2.0]) + b2[None] = ti.Vector([4.0, 5.0]) + c2[None] = ti.Matrix.cross(a2, b2) + + init() + assert c[None][0] == -3.0 + assert c[None][1] == 6.0 + assert c[None][2] == -3.0 + assert c2[None] == -3.0 + + +@ti.all_archs +def test_dot(): + a = ti.Vector(3, dt=ti.f32) + b = ti.Vector(3, dt=ti.f32) + c = ti.var(dt=ti.f32) + + a2 = ti.Vector(2, dt=ti.f32) + b2 = ti.Vector(2, dt=ti.f32) + c2 = ti.var(dt=ti.f32) + + @ti.layout + def place(): + ti.root.place(a, b, c, a2, b2, c2) + + @ti.kernel + def init(): + a[None] = ti.Vector([1.0, 2.0, 3.0]) + b[None] = ti.Vector([4.0, 5.0, 6.0]) + c[None] = ti.Matrix.dot(a, b) + + a2[None] = ti.Vector([1.0, 2.0]) + b2[None] = ti.Vector([4.0, 5.0]) + c2[None] = ti.Matrix.dot(a2, b2) + + init() + assert c[None] == 32.0 + assert c2[None] == 14.0 + + @ti.all_archs def test_transpose(): dim = 3 From 1393b99fd66b52430ec63e79c32c4d5f69f0d0c1 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Thu, 28 May 2020 23:22:12 -0400 Subject: [PATCH 07/17] [skip ci] enforce code format --- python/taichi/lang/matrix.py | 3 ++- tests/python/test_linalg.py | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 16c029bdce6f2..db8f40ccc4b72 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -610,7 +610,8 @@ def cross(a, b): else: raise Exception( - "cross product is only supported between pairs of 3D or those of 2D vectors") + "cross product is only supported between pairs of 3D or those of 2D vectors" + ) @staticmethod def outer_product(a, b): diff --git a/tests/python/test_linalg.py b/tests/python/test_linalg.py index 51b8b34fd5a6b..c22aa29f790e4 100644 --- a/tests/python/test_linalg.py +++ b/tests/python/test_linalg.py @@ -32,15 +32,15 @@ def init(): for i in range(3): for j in range(3): - assert abT[None][i, j] == a[None][i]*b[None][j] + assert abT[None][i, j] == a[None][i] * b[None][j] sqrt14 = np.sqrt(14.0) - invSqrt14 = 1.0/sqrt14 + invSqrt14 = 1.0 / sqrt14 assert normSqrA[None] == 14.0 assert normA[None] == approx(sqrt14) - assert aNormalized[None][0] == approx(1.0*invSqrt14) - assert aNormalized[None][1] == approx(2.0*invSqrt14) - assert aNormalized[None][2] == approx(3.0*invSqrt14) + assert aNormalized[None][0] == approx(1.0 * invSqrt14) + assert aNormalized[None][1] == approx(2.0 * invSqrt14) + assert aNormalized[None][2] == approx(3.0 * invSqrt14) @ti.all_archs From ec86c165f85e9ae288434af8f886e5344e10d477 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Thu, 28 May 2020 20:38:04 -0700 Subject: [PATCH 08/17] [skip ci] mention cross2D in vector.rst --- docs/vector.rst | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/vector.rst b/docs/vector.rst index 534007dfcfbf1..80c2782713aa0 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -138,16 +138,24 @@ Methods .. function:: ti.cross(a, b) - :parameter a: (Vector, 3 component) - :parameter b: (Vector, 3 component) - :return: (Vector, 3D) the cross product of ``a`` and ``b`` + :parameter a: (Vector, 2, or 3 component) + :parameter b: (Vector with the same component number as a) + :return: (Vector, 3D) the cross product of ``a`` and ``b``, if the component number is 3 + :return: (Scalar) the last component of cross product of ``Vector([a,0])`` and ``Vector([b,0])``, if the component number is 2 We use a right-handed coordinate system. E.g., :: a = ti.Vector([1, 2, 3]) b = ti.Vector([4, 5, 6]) - c = ti.cross(a, b) # [2*6 - 5*3, 4*3 - 1*6, 1*5 - 4*2] + c = ti.cross(a, b) + # [2*6 - 5*3, 4*3 - 1*6, 1*5 - 4*2] = [-3, 6, -3] + + a2 = ti.Vector([1, 2]) + b2 = ti.Vector([4, 5]) + c2 = ti.cross(a, b) + # [2*0 - 5*0, 4*0 - 1*0, 1*5 - 4*2] = [0, 0, -3] + # here it only calculates and returns the last component -3 .. function:: ti.outer_product(a, b) From 9964be795c375afc992b3a73dbae0616d425aa06 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 11:41:19 +0800 Subject: [PATCH 09/17] Update tests/python/test_linalg.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update to underline code style Co-authored-by: 彭于斌 <1931127624@qq.com> --- tests/python/test_linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_linalg.py b/tests/python/test_linalg.py index c22aa29f790e4..1c52168524e88 100644 --- a/tests/python/test_linalg.py +++ b/tests/python/test_linalg.py @@ -4,7 +4,7 @@ @ti.all_archs -def test_BasicUlts(): +def test_basic_utils(): a = ti.Vector(3, dt=ti.f32) b = ti.Vector(3, dt=ti.f32) abT = ti.Matrix(3, 3, dt=ti.f32) From 6d828dd8571dd9931cf9c7b6b2a3e23f0d682255 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 12:05:12 +0800 Subject: [PATCH 10/17] Update docs/vector.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit include var name Co-authored-by: 彭于斌 <1931127624@qq.com> --- docs/vector.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/vector.rst b/docs/vector.rst index 80c2782713aa0..696a0de855528 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -149,7 +149,7 @@ Methods a = ti.Vector([1, 2, 3]) b = ti.Vector([4, 5, 6]) c = ti.cross(a, b) - # [2*6 - 5*3, 4*3 - 1*6, 1*5 - 4*2] = [-3, 6, -3] + # c = [2*6 - 5*3, 4*3 - 1*6, 1*5 - 4*2] = [-3, 6, -3] a2 = ti.Vector([1, 2]) b2 = ti.Vector([4, 5]) From 4d736127be041d0ff9a141e425746e48e3da8d23 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 12:05:53 +0800 Subject: [PATCH 11/17] Update docs/vector.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit avoid making people confused about return type of cross(vec2d,vec2d) Co-authored-by: 彭于斌 <1931127624@qq.com> --- docs/vector.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/vector.rst b/docs/vector.rst index 696a0de855528..6d9a25baa5b6f 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -154,8 +154,7 @@ Methods a2 = ti.Vector([1, 2]) b2 = ti.Vector([4, 5]) c2 = ti.cross(a, b) - # [2*0 - 5*0, 4*0 - 1*0, 1*5 - 4*2] = [0, 0, -3] - # here it only calculates and returns the last component -3 + # c2 = 1*5 - 4*2 = -3 .. function:: ti.outer_product(a, b) From 487fc9ac6818e0158f6b3eced651e6e4d5103ab9 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 22:16:40 +0800 Subject: [PATCH 12/17] Update docs/vector.rst writing nits Co-authored-by: Yuanming Hu --- docs/vector.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/vector.rst b/docs/vector.rst index 6d9a25baa5b6f..4c2d9f8730479 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -138,7 +138,7 @@ Methods .. function:: ti.cross(a, b) - :parameter a: (Vector, 2, or 3 component) + :parameter a: (Vector, 2 or 3 components) :parameter b: (Vector with the same component number as a) :return: (Vector, 3D) the cross product of ``a`` and ``b``, if the component number is 3 :return: (Scalar) the last component of cross product of ``Vector([a,0])`` and ``Vector([b,0])``, if the component number is 2 From 44617ebfbe680b23d5d2434a48c51d44af64a5cf Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 22:17:37 +0800 Subject: [PATCH 13/17] Update docs/vector.rst another writing nits Co-authored-by: Yuanming Hu --- docs/vector.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/vector.rst b/docs/vector.rst index 4c2d9f8730479..b3df4cb2c063e 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -139,7 +139,7 @@ Methods .. function:: ti.cross(a, b) :parameter a: (Vector, 2 or 3 components) - :parameter b: (Vector with the same component number as a) + :parameter b: (Vector of the same size as a) :return: (Vector, 3D) the cross product of ``a`` and ``b``, if the component number is 3 :return: (Scalar) the last component of cross product of ``Vector([a,0])`` and ``Vector([b,0])``, if the component number is 2 From 70587f10df35f6bc2f2eaa566732f9bf2419d5a2 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 22:21:33 +0800 Subject: [PATCH 14/17] Update docs/vector.rst last writing nit! Co-authored-by: Yuanming Hu --- docs/vector.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/vector.rst b/docs/vector.rst index b3df4cb2c063e..98e5d9d4669e2 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -151,10 +151,10 @@ Methods c = ti.cross(a, b) # c = [2*6 - 5*3, 4*3 - 1*6, 1*5 - 4*2] = [-3, 6, -3] - a2 = ti.Vector([1, 2]) - b2 = ti.Vector([4, 5]) - c2 = ti.cross(a, b) - # c2 = 1*5 - 4*2 = -3 + p = ti.Vector([1, 2]) + q = ti.Vector([4, 5]) + r = ti.cross(a, b) + # r = 1*5 - 4*2 = -3 .. function:: ti.outer_product(a, b) From b54dfcc1ec623cfc20d1f62470099e903e7990bb Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 22:23:42 +0800 Subject: [PATCH 15/17] Update docs/vector.rst Last writing nits in vector.rst Co-authored-by: Yuanming Hu --- docs/vector.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/vector.rst b/docs/vector.rst index 98e5d9d4669e2..3a03d6dda4968 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -140,8 +140,7 @@ Methods :parameter a: (Vector, 2 or 3 components) :parameter b: (Vector of the same size as a) - :return: (Vector, 3D) the cross product of ``a`` and ``b``, if the component number is 3 - :return: (Scalar) the last component of cross product of ``Vector([a,0])`` and ``Vector([b,0])``, if the component number is 2 + :return: (scalar (for 2D inputs), or 3D Vector (for 3D inputs)) the cross product of ``a`` and ``b`` We use a right-handed coordinate system. E.g., :: From c2aea7b7b6e319c69a00e49cc74b2bf353c1244a Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Fri, 29 May 2020 10:24:03 -0400 Subject: [PATCH 16/17] [skip ci] enforce code format --- docs/vector.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/vector.rst b/docs/vector.rst index 3a03d6dda4968..4e4303c5d5a06 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -140,7 +140,7 @@ Methods :parameter a: (Vector, 2 or 3 components) :parameter b: (Vector of the same size as a) - :return: (scalar (for 2D inputs), or 3D Vector (for 3D inputs)) the cross product of ``a`` and ``b`` + :return: (scalar (for 2D inputs), or 3D Vector (for 3D inputs)) the cross product of ``a`` and ``b`` We use a right-handed coordinate system. E.g., :: From 99cbb45780d7ad4dc263d56eabf829fdf30e6021 Mon Sep 17 00:00:00 2001 From: Yadi Cao Date: Fri, 29 May 2020 22:27:14 +0800 Subject: [PATCH 17/17] Update python/taichi/lang/matrix.py Capital letter at the beginning of a sentence. Co-authored-by: Yuanming Hu --- python/taichi/lang/matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index db8f40ccc4b72..3a5bf03c6a319 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -610,7 +610,7 @@ def cross(a, b): else: raise Exception( - "cross product is only supported between pairs of 3D or those of 2D vectors" + "Cross product is only supported between pairs of 2D/3D vectors" ) @staticmethod