From 53d55af8df617fc4fafff79db0a88f3506d4f05a Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 17:51:16 +0800 Subject: [PATCH 01/13] add more functions to math module --- python/taichi/math/mathimpl.py | 36 +++++++++++++++++++++++++++++----- tests/python/test_api.py | 11 ++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index fced31ec73bcf..d66a50845a277 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -7,6 +7,10 @@ import taichi as ti +from taichi.lang.ops import (sin, cos, asin, acos, sqrt, round, + floor, ceil, tan, tanh, exp, log, pow, + atan2, max, min) + vec2 = ti.types.vector(2, float) # pylint: disable=E1101 """2D float vector type. """ @@ -576,7 +580,7 @@ def rot3(axis, ang): >>> from taichi.math import * >>> @ti.kernel >>> def test(): - >>> M = rot3(vec3(1, 1, 1), radians(30)) + >>> M = rot3(normalize(vec3(1, 1, 1)), radians(30)) [[0.732051, -0.366025, 0.633975], [0.633975, 0.732051, -0.366025], [-0.366025, 0.633975, 0.732051]] @@ -588,10 +592,32 @@ def rot3(axis, ang): return I + sa * K + (1.0 - ca) * K @ K +@ti.func +def length(x): + """Calculate the length of a vector. + + This function is equivalent to the `length` function is GLSL. + + Args: + x (:class:`~taichi.Matrix`): The vector of which to calculate the length. + + Returns: + The Euclidean norm of the vector. + + Example:: + + >>> x = ti.Vector([1, 1, 1]) + >>> length(x) + 1.732051 + """ + return x.norm() + + __all__ = [ - "clamp", "cross", "degrees", "distance", "dot", "e", "eye", "fract", - "ivec2", "ivec3", "ivec4", "log2", "mat2", "mat3", "mat4", "mix", "mod", - "normalize", "pi", "radians", "reflect", "refract", "rot2", "rot3", - "rotate2d", "rotate3d", "sign", "smoothstep", "step", "uvec2", "uvec3", + "acos", "asin", "atan2", "ceil", + "clamp", "cos", "cross", "degrees", "distance", "dot", "e", "exp", "eye", "floor", "fract", + "ivec2", "ivec3", "ivec4", "length", "log", "log2", "mat2", "mat3", "mat4", "max", "min", + "mix", "mod", "normalize", "pi", "pow", "radians", "reflect", "refract", "rot2", "rot3", + "rotate2d", "rotate3d", "round", "sign", "sin", "smoothstep", "sqrt", "step", "tan", "tanh", "uvec2", "uvec3", "uvec4", "vec2", "vec3", "vec4" ] diff --git a/tests/python/test_api.py b/tests/python/test_api.py index 23d5843aa70c9..ec82c1929479c 100644 --- a/tests/python/test_api.py +++ b/tests/python/test_api.py @@ -97,11 +97,12 @@ def _get_expected_matrix_apis(): 'dynamic', 'finalize', 'lazy_grad', 'place', 'pointer' ] user_api[ti.math] = [ - 'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', 'clog', 'cmul', 'cpow', 'cross', - 'csqrt', 'degrees', 'distance', 'dot', 'e', 'eye', 'fract', 'ivec2', - 'ivec3', 'ivec4', 'log2', 'mat2', 'mat3', 'mat4', 'mix', 'mod', - 'normalize', 'pi', 'radians', 'reflect', 'refract', 'rot2', 'rot3', - 'rotate2d', 'rotate3d', 'sign', 'smoothstep', 'step', 'uvec2', 'uvec3', + "acos", "asin", "atan2", "ceil", + 'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', 'clog', 'cmul', 'cos', 'cpow', 'cross', + 'csqrt', 'degrees', 'distance', 'dot', 'e', 'exp', 'eye', 'floor', 'fract', 'ivec2', + 'ivec3', 'ivec4', 'length', 'log', 'log2', 'mat2', 'mat3', 'mat4', 'max', 'min', 'mix', 'mod', + 'normalize', 'pi', 'pow', 'radians', 'reflect', 'refract', 'rot2', 'rot3', + 'rotate2d', 'rotate3d', 'round', 'sign', 'sin', 'smoothstep', 'sqrt', 'step', 'tan', 'tanh', 'uvec2', 'uvec3', 'uvec4', 'vec2', 'vec3', 'vec4' ] user_api[ti.Matrix] = _get_expected_matrix_apis() From 7871cdd8ea4be4373e72f300bd332cd316c65b98 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 May 2022 09:53:10 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/math/mathimpl.py | 19 +++++++++---------- tests/python/test_api.py | 15 ++++++++------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index d66a50845a277..842116b0715dc 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -4,13 +4,11 @@ from math import e, pi from taichi.lang import impl +from taichi.lang.ops import (acos, asin, atan2, ceil, cos, exp, floor, log, + max, min, pow, round, sin, sqrt, tan, tanh) import taichi as ti -from taichi.lang.ops import (sin, cos, asin, acos, sqrt, round, - floor, ceil, tan, tanh, exp, log, pow, - atan2, max, min) - vec2 = ti.types.vector(2, float) # pylint: disable=E1101 """2D float vector type. """ @@ -614,10 +612,11 @@ def length(x): __all__ = [ - "acos", "asin", "atan2", "ceil", - "clamp", "cos", "cross", "degrees", "distance", "dot", "e", "exp", "eye", "floor", "fract", - "ivec2", "ivec3", "ivec4", "length", "log", "log2", "mat2", "mat3", "mat4", "max", "min", - "mix", "mod", "normalize", "pi", "pow", "radians", "reflect", "refract", "rot2", "rot3", - "rotate2d", "rotate3d", "round", "sign", "sin", "smoothstep", "sqrt", "step", "tan", "tanh", "uvec2", "uvec3", - "uvec4", "vec2", "vec3", "vec4" + "acos", "asin", "atan2", "ceil", "clamp", "cos", "cross", "degrees", + "distance", "dot", "e", "exp", "eye", "floor", "fract", "ivec2", "ivec3", + "ivec4", "length", "log", "log2", "mat2", "mat3", "mat4", "max", "min", + "mix", "mod", "normalize", "pi", "pow", "radians", "reflect", "refract", + "rot2", "rot3", "rotate2d", "rotate3d", "round", "sign", "sin", + "smoothstep", "sqrt", "step", "tan", "tanh", "uvec2", "uvec3", "uvec4", + "vec2", "vec3", "vec4" ] diff --git a/tests/python/test_api.py b/tests/python/test_api.py index ec82c1929479c..65c50fefc5d94 100644 --- a/tests/python/test_api.py +++ b/tests/python/test_api.py @@ -97,13 +97,14 @@ def _get_expected_matrix_apis(): 'dynamic', 'finalize', 'lazy_grad', 'place', 'pointer' ] user_api[ti.math] = [ - "acos", "asin", "atan2", "ceil", - 'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', 'clog', 'cmul', 'cos', 'cpow', 'cross', - 'csqrt', 'degrees', 'distance', 'dot', 'e', 'exp', 'eye', 'floor', 'fract', 'ivec2', - 'ivec3', 'ivec4', 'length', 'log', 'log2', 'mat2', 'mat3', 'mat4', 'max', 'min', 'mix', 'mod', - 'normalize', 'pi', 'pow', 'radians', 'reflect', 'refract', 'rot2', 'rot3', - 'rotate2d', 'rotate3d', 'round', 'sign', 'sin', 'smoothstep', 'sqrt', 'step', 'tan', 'tanh', 'uvec2', 'uvec3', - 'uvec4', 'vec2', 'vec3', 'vec4' + "acos", "asin", "atan2", "ceil", 'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', + 'clog', 'cmul', 'cos', 'cpow', 'cross', 'csqrt', 'degrees', 'distance', + 'dot', 'e', 'exp', 'eye', 'floor', 'fract', 'ivec2', 'ivec3', 'ivec4', + 'length', 'log', 'log2', 'mat2', 'mat3', 'mat4', 'max', 'min', 'mix', + 'mod', 'normalize', 'pi', 'pow', 'radians', 'reflect', 'refract', 'rot2', + 'rot3', 'rotate2d', 'rotate3d', 'round', 'sign', 'sin', 'smoothstep', + 'sqrt', 'step', 'tan', 'tanh', 'uvec2', 'uvec3', 'uvec4', 'vec2', 'vec3', + 'vec4' ] user_api[ti.Matrix] = _get_expected_matrix_apis() user_api[ti.MatrixField] = [ From 7b0b5220088780a4f08b8e78af45d83aeeedb1f1 Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 17:57:35 +0800 Subject: [PATCH 03/13] add more functions to math module --- python/taichi/math/mathimpl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 842116b0715dc..c44927d286956 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -1,3 +1,4 @@ +# pylint: disable=W0622 """ Math functions for glsl-like functions and other stuff. """ From 87315ac71c6653957977a16ecd510bd6a84f55b0 Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 18:02:09 +0800 Subject: [PATCH 04/13] add more functions to math module --- python/taichi/math/__init__.py | 2 +- python/taichi/math/mathimpl.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/python/taichi/math/__init__.py b/python/taichi/math/__init__.py index b08ced0b26c53..b31f96b061de1 100644 --- a/python/taichi/math/__init__.py +++ b/python/taichi/math/__init__.py @@ -3,6 +3,6 @@ The math module supports glsl-style vectors, matrices and functions. """ from ._complex import * -from .mathimpl import * +from .mathimpl import * # pylint: disable=W0622 del mathimpl diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index c44927d286956..842116b0715dc 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -1,4 +1,3 @@ -# pylint: disable=W0622 """ Math functions for glsl-like functions and other stuff. """ From 4183dac80a5994388a38961a5efe4e489ee1e4b1 Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 18:13:58 +0800 Subject: [PATCH 05/13] add more functions to math module --- python/taichi/math/mathimpl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 842116b0715dc..7bdc3c046773c 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -5,7 +5,7 @@ from taichi.lang import impl from taichi.lang.ops import (acos, asin, atan2, ceil, cos, exp, floor, log, - max, min, pow, round, sin, sqrt, tan, tanh) + max, min, pow, round, sin, sqrt, tan, tanh) # pylint: disable=W0622 import taichi as ti From 71cf7d69857b8873a92e92dea1036769f7472312 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 May 2022 10:14:53 +0000 Subject: [PATCH 06/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/math/mathimpl.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 7bdc3c046773c..50bc530e5211f 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -4,8 +4,9 @@ from math import e, pi from taichi.lang import impl -from taichi.lang.ops import (acos, asin, atan2, ceil, cos, exp, floor, log, - max, min, pow, round, sin, sqrt, tan, tanh) # pylint: disable=W0622 +from taichi.lang.ops import (acos, asin, atan2, ceil, # pylint: disable=W0622 + cos, exp, floor, log, max, min, pow, round, sin, + sqrt, tan, tanh) import taichi as ti From 6fb39c0164fed0256cc399798228c6156778b2b3 Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 18:17:29 +0800 Subject: [PATCH 07/13] add more functions to math module --- python/taichi/math/mathimpl.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 7bdc3c046773c..c44927d286956 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -1,3 +1,4 @@ +# pylint: disable=W0622 """ Math functions for glsl-like functions and other stuff. """ @@ -5,7 +6,7 @@ from taichi.lang import impl from taichi.lang.ops import (acos, asin, atan2, ceil, cos, exp, floor, log, - max, min, pow, round, sin, sqrt, tan, tanh) # pylint: disable=W0622 + max, min, pow, round, sin, sqrt, tan, tanh) import taichi as ti From 2ded2ee599cfa1baf7b6a4715cbac96035e43a94 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 May 2022 10:19:42 +0000 Subject: [PATCH 08/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/math/mathimpl.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 86703add4ee70..c44927d286956 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -5,7 +5,6 @@ from math import e, pi from taichi.lang import impl - from taichi.lang.ops import (acos, asin, atan2, ceil, cos, exp, floor, log, max, min, pow, round, sin, sqrt, tan, tanh) From 4d827f743553fbe640d285049739ae724019227d Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 9 May 2022 18:41:03 +0800 Subject: [PATCH 09/13] add more functions to math module --- tests/python/test_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_api.py b/tests/python/test_api.py index 65c50fefc5d94..33a19546be3de 100644 --- a/tests/python/test_api.py +++ b/tests/python/test_api.py @@ -97,7 +97,7 @@ def _get_expected_matrix_apis(): 'dynamic', 'finalize', 'lazy_grad', 'place', 'pointer' ] user_api[ti.math] = [ - "acos", "asin", "atan2", "ceil", 'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', + 'acos', 'asin', 'atan2', 'cconj', 'cdiv', 'ceil', 'cexp', 'cinv', 'clamp', 'clog', 'cmul', 'cos', 'cpow', 'cross', 'csqrt', 'degrees', 'distance', 'dot', 'e', 'exp', 'eye', 'floor', 'fract', 'ivec2', 'ivec3', 'ivec4', 'length', 'log', 'log2', 'mat2', 'mat3', 'mat4', 'max', 'min', 'mix', From d912c4031675311ee4dfe0d8668ec7d37f572772 Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Tue, 10 May 2022 10:26:16 +0800 Subject: [PATCH 10/13] add more functions to math module --- python/taichi/_funcs.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/python/taichi/_funcs.py b/python/taichi/_funcs.py index 6753234c77070..664e646ac11ae 100644 --- a/python/taichi/_funcs.py +++ b/python/taichi/_funcs.py @@ -100,7 +100,6 @@ def _matrix_outer_product(self, other): @func def polar_decompose2d(A, dt): """Perform polar decomposition (A=UP) for 2x2 matrix. - Mathematical concept refers to https://en.wikipedia.org/wiki/Polar_decomposition. Args: @@ -108,14 +107,32 @@ def polar_decompose2d(A, dt): dt (DataType): date type of elements in matrix `A`, typically accepts ti.f32 or ti.f64. Returns: - Decomposed 2x2 matrices `U` and `P`. + Decomposed 2x2 matrices `U` and `P`. `U` is a 2x2 orthogonal matrix + and `P` is a 2x2 positive or semi-positive definite matrix. """ - x, y = A(0, 0) + A(1, 1), A(1, 0) - A(0, 1) - scale = (1.0 / ops.sqrt(x * x + y * y)) - c = x * scale - s = y * scale - r = Matrix([[c, -s], [s, c]], dt=dt) - return r, r.transpose() @ A + U = Matrix.identity(dt, 2) + P = ops.cast(A, dt) + zero = ops.cast(0.0, dt) + # if A is the zero matrix we simply return the pair (I, A) + if (A[0, 0] == zero and A[0, 1] == zero and A[1, 0] == zero + and A[0, 0] == zero): + pass + else: + detA = A[0, 0] * A[1, 1] - A[1, 0] * A[0, 1] + adetA = abs(detA) + B = Matrix([[A[0, 0] + A[1, 1], A[0, 1] - A[1, 0]], + [A[1, 0] - A[0, 1], A[1, 1] + A[0, 0]]], dt) + + if detA < zero: + B = Matrix([[A[0, 0] - A[1, 1], A[0, 1] + A[1, 0]], + [A[1, 0] + A[0, 1], A[1, 1] - A[0, 0]]], dt) + # here det(B) != 0 if A is not the zero matrix + adetB = abs(B[0, 0] * B[1, 1] - B[1, 0] * B[0, 1]) + k = ops.cast(1.0, dt) / ops.sqrt(adetB) + U = B * k + P = (A.transpose() @ A + adetA * Matrix.identity(dt, 2)) * k + + return U, P @func From 653eaae6921a006552b3d4903a50e3cb3b6fd5f1 Mon Sep 17 00:00:00 2001 From: Zhao Liang Date: Tue, 10 May 2022 13:25:25 +0800 Subject: [PATCH 11/13] Update _funcs.py --- python/taichi/_funcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/_funcs.py b/python/taichi/_funcs.py index 664e646ac11ae..3e8c50c3cf052 100644 --- a/python/taichi/_funcs.py +++ b/python/taichi/_funcs.py @@ -115,7 +115,7 @@ def polar_decompose2d(A, dt): zero = ops.cast(0.0, dt) # if A is the zero matrix we simply return the pair (I, A) if (A[0, 0] == zero and A[0, 1] == zero and A[1, 0] == zero - and A[0, 0] == zero): + and A[1, 1] == zero): pass else: detA = A[0, 0] * A[1, 1] - A[1, 0] * A[0, 1] From ebd584f69d76b0e0fa48d368c19b1edac2123d65 Mon Sep 17 00:00:00 2001 From: Zhao Liang Date: Wed, 11 May 2022 12:24:29 +0800 Subject: [PATCH 12/13] Update python/taichi/_funcs.py Co-authored-by: pengyu <6712304+FantasyVR@users.noreply.github.com> --- python/taichi/_funcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/taichi/_funcs.py b/python/taichi/_funcs.py index 3e8c50c3cf052..6fce332466981 100644 --- a/python/taichi/_funcs.py +++ b/python/taichi/_funcs.py @@ -113,7 +113,7 @@ def polar_decompose2d(A, dt): U = Matrix.identity(dt, 2) P = ops.cast(A, dt) zero = ops.cast(0.0, dt) - # if A is the zero matrix we simply return the pair (I, A) + # if A is a zero matrix we simply return the pair (I, A) if (A[0, 0] == zero and A[0, 1] == zero and A[1, 0] == zero and A[1, 1] == zero): pass From cf17513c5c069e02c9fc7f49244caaebb0c245df Mon Sep 17 00:00:00 2001 From: pengyu <6712304+FantasyVR@users.noreply.github.com> Date: Wed, 11 May 2022 12:27:29 +0800 Subject: [PATCH 13/13] Update python/taichi/math/mathimpl.py --- python/taichi/math/mathimpl.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index c44927d286956..085e7d169ff2b 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -595,8 +595,7 @@ def rot3(axis, ang): def length(x): """Calculate the length of a vector. - This function is equivalent to the `length` function is GLSL. - + This function is equivalent to the `length` function in GLSL. Args: x (:class:`~taichi.Matrix`): The vector of which to calculate the length.