Skip to content

Commit

Permalink
[Lang] Add mod function to math module (#4809)
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhaoliang authored Apr 18, 2022
1 parent 2f1404a commit 4512ccc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 23 additions & 2 deletions python/taichi/math/mathimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,29 @@ def cross(x, y):
return x.cross(y)


@ti.func
def mod(x, y):
"""Compute value of one parameter modulo another, element-wise.
Args:
x (:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`): The first input.
y (:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`): The second input.
Returns:
the value of `x` modulo `y`. This is computed as `x - y * floor(x/y)`.
Example::
>>> x = ti.Vector([-0.5, 0.5, 1.])
>>> y = 1.0
>>> mod(x, y)
[0.5, 0.5, 0.0]
"""
return x - y * ti.floor(x / y)


__all__ = [
"clamp", "cross", "degrees", "distance", "dot", "e", "fract", "log2",
"mat2", "mat3", "mat4", "mix", "normalize", "pi", "radians", "reflect",
"refract", "sign", "smoothstep", "step"
"mat2", "mat3", "mat4", "mix", "mod", "normalize", "pi", "radians",
"reflect", "refract", "sign", "smoothstep", "step"
]
2 changes: 1 addition & 1 deletion tests/python/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
user_api[ti.math] = [
'cconj', 'cdiv', 'cexp', 'cinv', 'clamp', 'clog', 'cmul', 'cpow', 'cross',
'csqrt', 'degrees', 'distance', 'dot', 'e', 'fract', 'ivec2', 'ivec3',
'ivec4', 'log2', 'mat2', 'mat3', 'mat4', 'mix', 'normalize', 'pi',
'ivec4', 'log2', 'mat2', 'mat3', 'mat4', 'mix', 'mod', 'normalize', 'pi',
'radians', 'reflect', 'refract', 'sign', 'smoothstep', 'step', 'vec2',
'vec3', 'vec4'
]
Expand Down

0 comments on commit 4512ccc

Please sign in to comment.