From be74254466ceace85ae51b0c98ccc43a6d62476a Mon Sep 17 00:00:00 2001 From: neozhaoliang Date: Mon, 18 Apr 2022 08:40:08 +0800 Subject: [PATCH] add mod function to math module --- python/taichi/math/mathimpl.py | 25 +++++++++++++++++++++++-- tests/python/test_api.py | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/python/taichi/math/mathimpl.py b/python/taichi/math/mathimpl.py index 742a0bb06c05a..335dfe3c33615 100644 --- a/python/taichi/math/mathimpl.py +++ b/python/taichi/math/mathimpl.py @@ -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" ] diff --git a/tests/python/test_api.py b/tests/python/test_api.py index 4681004553d35..e9e626c43ebbf 100644 --- a/tests/python/test_api.py +++ b/tests/python/test_api.py @@ -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' ]