Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lang] Add more functions to math module #4939

Merged
merged 15 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions python/taichi/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,39 @@ 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:
A (ti.Matrix(2, 2)): input 2x2 matrix `A`.
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 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
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
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 32 additions & 6 deletions python/taichi/math/mathimpl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# pylint: disable=W0622
"""
Math functions for glsl-like functions and other stuff.
"""
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

Expand Down Expand Up @@ -576,7 +579,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]]
Expand All @@ -588,10 +591,33 @@ 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.

FantasyVR marked this conversation as resolved.
Show resolved Hide resolved
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",
"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"
]
14 changes: 8 additions & 6 deletions tests/python/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ 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',
'uvec4', 'vec2', 'vec3', 'vec4'
'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',
'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] = [
Expand Down