gstaichi 2.1.1rc3__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. gstaichi/CHANGELOG.md +4 -0
  2. gstaichi/__init__.py +40 -0
  3. gstaichi/_funcs.py +706 -0
  4. gstaichi/_kernels.py +420 -0
  5. gstaichi/_lib/__init__.py +3 -0
  6. gstaichi/_lib/core/__init__.py +0 -0
  7. gstaichi/_lib/core/gstaichi_python.cpython-311-darwin.so +0 -0
  8. gstaichi/_lib/core/gstaichi_python.pyi +2909 -0
  9. gstaichi/_lib/core/py.typed +0 -0
  10. gstaichi/_lib/runtime/libMoltenVK.dylib +0 -0
  11. gstaichi/_lib/runtime/runtime_arm64.bc +0 -0
  12. gstaichi/_lib/utils.py +243 -0
  13. gstaichi/_logging.py +131 -0
  14. gstaichi/_snode/__init__.py +5 -0
  15. gstaichi/_snode/fields_builder.py +187 -0
  16. gstaichi/_snode/snode_tree.py +34 -0
  17. gstaichi/_test_tools/__init__.py +18 -0
  18. gstaichi/_test_tools/dataclass_test_tools.py +36 -0
  19. gstaichi/_test_tools/load_kernel_string.py +30 -0
  20. gstaichi/_test_tools/textwrap2.py +6 -0
  21. gstaichi/_version.py +1 -0
  22. gstaichi/_version_check.py +100 -0
  23. gstaichi/ad/__init__.py +3 -0
  24. gstaichi/ad/_ad.py +530 -0
  25. gstaichi/algorithms/__init__.py +3 -0
  26. gstaichi/algorithms/_algorithms.py +117 -0
  27. gstaichi/assets/.git +1 -0
  28. gstaichi/assets/Go-Regular.ttf +0 -0
  29. gstaichi/assets/static/imgs/ti_gallery.png +0 -0
  30. gstaichi/examples/lcg_python.py +26 -0
  31. gstaichi/examples/lcg_taichi.py +34 -0
  32. gstaichi/examples/minimal.py +28 -0
  33. gstaichi/experimental.py +16 -0
  34. gstaichi/lang/__init__.py +50 -0
  35. gstaichi/lang/_dataclass_util.py +31 -0
  36. gstaichi/lang/_fast_caching/__init__.py +3 -0
  37. gstaichi/lang/_fast_caching/args_hasher.py +110 -0
  38. gstaichi/lang/_fast_caching/config_hasher.py +30 -0
  39. gstaichi/lang/_fast_caching/fast_caching_types.py +21 -0
  40. gstaichi/lang/_fast_caching/function_hasher.py +57 -0
  41. gstaichi/lang/_fast_caching/hash_utils.py +11 -0
  42. gstaichi/lang/_fast_caching/python_side_cache.py +52 -0
  43. gstaichi/lang/_fast_caching/src_hasher.py +75 -0
  44. gstaichi/lang/_kernel_impl_dataclass.py +212 -0
  45. gstaichi/lang/_ndarray.py +352 -0
  46. gstaichi/lang/_ndrange.py +152 -0
  47. gstaichi/lang/_template_mapper.py +195 -0
  48. gstaichi/lang/_texture.py +172 -0
  49. gstaichi/lang/_wrap_inspect.py +215 -0
  50. gstaichi/lang/any_array.py +99 -0
  51. gstaichi/lang/ast/__init__.py +5 -0
  52. gstaichi/lang/ast/ast_transformer.py +1323 -0
  53. gstaichi/lang/ast/ast_transformer_utils.py +346 -0
  54. gstaichi/lang/ast/ast_transformers/__init__.py +0 -0
  55. gstaichi/lang/ast/ast_transformers/call_transformer.py +324 -0
  56. gstaichi/lang/ast/ast_transformers/function_def_transformer.py +304 -0
  57. gstaichi/lang/ast/checkers.py +106 -0
  58. gstaichi/lang/ast/symbol_resolver.py +57 -0
  59. gstaichi/lang/ast/transform.py +9 -0
  60. gstaichi/lang/common_ops.py +310 -0
  61. gstaichi/lang/exception.py +80 -0
  62. gstaichi/lang/expr.py +180 -0
  63. gstaichi/lang/field.py +428 -0
  64. gstaichi/lang/impl.py +1243 -0
  65. gstaichi/lang/kernel_arguments.py +155 -0
  66. gstaichi/lang/kernel_impl.py +1341 -0
  67. gstaichi/lang/matrix.py +1835 -0
  68. gstaichi/lang/matrix_ops.py +341 -0
  69. gstaichi/lang/matrix_ops_utils.py +190 -0
  70. gstaichi/lang/mesh.py +687 -0
  71. gstaichi/lang/misc.py +782 -0
  72. gstaichi/lang/ops.py +1494 -0
  73. gstaichi/lang/runtime_ops.py +13 -0
  74. gstaichi/lang/shell.py +35 -0
  75. gstaichi/lang/simt/__init__.py +5 -0
  76. gstaichi/lang/simt/block.py +94 -0
  77. gstaichi/lang/simt/grid.py +7 -0
  78. gstaichi/lang/simt/subgroup.py +191 -0
  79. gstaichi/lang/simt/warp.py +96 -0
  80. gstaichi/lang/snode.py +489 -0
  81. gstaichi/lang/source_builder.py +150 -0
  82. gstaichi/lang/struct.py +810 -0
  83. gstaichi/lang/util.py +312 -0
  84. gstaichi/linalg/__init__.py +8 -0
  85. gstaichi/linalg/matrixfree_cg.py +310 -0
  86. gstaichi/linalg/sparse_cg.py +59 -0
  87. gstaichi/linalg/sparse_matrix.py +303 -0
  88. gstaichi/linalg/sparse_solver.py +123 -0
  89. gstaichi/math/__init__.py +11 -0
  90. gstaichi/math/_complex.py +205 -0
  91. gstaichi/math/mathimpl.py +886 -0
  92. gstaichi/profiler/__init__.py +6 -0
  93. gstaichi/profiler/kernel_metrics.py +260 -0
  94. gstaichi/profiler/kernel_profiler.py +586 -0
  95. gstaichi/profiler/memory_profiler.py +15 -0
  96. gstaichi/profiler/scoped_profiler.py +36 -0
  97. gstaichi/sparse/__init__.py +3 -0
  98. gstaichi/sparse/_sparse_grid.py +77 -0
  99. gstaichi/tools/__init__.py +12 -0
  100. gstaichi/tools/diagnose.py +117 -0
  101. gstaichi/tools/np2ply.py +364 -0
  102. gstaichi/tools/vtk.py +38 -0
  103. gstaichi/types/__init__.py +19 -0
  104. gstaichi/types/annotations.py +52 -0
  105. gstaichi/types/compound_types.py +71 -0
  106. gstaichi/types/enums.py +49 -0
  107. gstaichi/types/ndarray_type.py +169 -0
  108. gstaichi/types/primitive_types.py +206 -0
  109. gstaichi/types/quant.py +88 -0
  110. gstaichi/types/texture_type.py +85 -0
  111. gstaichi/types/utils.py +11 -0
  112. gstaichi-2.1.1rc3.data/data/include/GLFW/glfw3.h +6389 -0
  113. gstaichi-2.1.1rc3.data/data/include/GLFW/glfw3native.h +594 -0
  114. gstaichi-2.1.1rc3.data/data/include/spirv-tools/instrument.hpp +268 -0
  115. gstaichi-2.1.1rc3.data/data/include/spirv-tools/libspirv.h +907 -0
  116. gstaichi-2.1.1rc3.data/data/include/spirv-tools/libspirv.hpp +375 -0
  117. gstaichi-2.1.1rc3.data/data/include/spirv-tools/linker.hpp +97 -0
  118. gstaichi-2.1.1rc3.data/data/include/spirv-tools/optimizer.hpp +970 -0
  119. gstaichi-2.1.1rc3.data/data/include/spirv_cross/GLSL.std.450.h +114 -0
  120. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv.h +2568 -0
  121. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv.hpp +2579 -0
  122. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cfg.hpp +168 -0
  123. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_common.hpp +1920 -0
  124. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cpp.hpp +93 -0
  125. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross.hpp +1171 -0
  126. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross_c.h +1074 -0
  127. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross_containers.hpp +754 -0
  128. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross_error_handling.hpp +94 -0
  129. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross_parsed_ir.hpp +256 -0
  130. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_cross_util.hpp +37 -0
  131. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_glsl.hpp +1001 -0
  132. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_hlsl.hpp +406 -0
  133. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_msl.hpp +1273 -0
  134. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_parser.hpp +103 -0
  135. gstaichi-2.1.1rc3.data/data/include/spirv_cross/spirv_reflect.hpp +91 -0
  136. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsConfig.cmake +5 -0
  137. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget-release.cmake +29 -0
  138. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools/SPIRV-ToolsTarget.cmake +114 -0
  139. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffConfig.cmake +5 -0
  140. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets-release.cmake +19 -0
  141. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-diff/SPIRV-Tools-diffTargets.cmake +123 -0
  142. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkConfig.cmake +5 -0
  143. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets-release.cmake +19 -0
  144. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-link/SPIRV-Tools-linkTargets.cmake +123 -0
  145. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintConfig.cmake +5 -0
  146. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets-release.cmake +19 -0
  147. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-lint/SPIRV-Tools-lintTargets.cmake +123 -0
  148. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optConfig.cmake +5 -0
  149. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets-release.cmake +19 -0
  150. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-opt/SPIRV-Tools-optTargets.cmake +123 -0
  151. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceConfig.cmake +5 -0
  152. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget-release.cmake +19 -0
  153. gstaichi-2.1.1rc3.data/data/lib/cmake/SPIRV-Tools-reduce/SPIRV-Tools-reduceTarget.cmake +123 -0
  154. gstaichi-2.1.1rc3.data/data/lib/cmake/glfw3/glfw3Config.cmake +3 -0
  155. gstaichi-2.1.1rc3.data/data/lib/cmake/glfw3/glfw3ConfigVersion.cmake +65 -0
  156. gstaichi-2.1.1rc3.data/data/lib/cmake/glfw3/glfw3Targets-release.cmake +19 -0
  157. gstaichi-2.1.1rc3.data/data/lib/cmake/glfw3/glfw3Targets.cmake +107 -0
  158. gstaichi-2.1.1rc3.data/data/lib/libSPIRV-Tools-shared.dylib +0 -0
  159. gstaichi-2.1.1rc3.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig-release.cmake +19 -0
  160. gstaichi-2.1.1rc3.data/data/share/spirv_cross_c/cmake/spirv_cross_cConfig.cmake +123 -0
  161. gstaichi-2.1.1rc3.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig-release.cmake +19 -0
  162. gstaichi-2.1.1rc3.data/data/share/spirv_cross_core/cmake/spirv_cross_coreConfig.cmake +106 -0
  163. gstaichi-2.1.1rc3.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig-release.cmake +19 -0
  164. gstaichi-2.1.1rc3.data/data/share/spirv_cross_cpp/cmake/spirv_cross_cppConfig.cmake +123 -0
  165. gstaichi-2.1.1rc3.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig-release.cmake +19 -0
  166. gstaichi-2.1.1rc3.data/data/share/spirv_cross_glsl/cmake/spirv_cross_glslConfig.cmake +123 -0
  167. gstaichi-2.1.1rc3.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig-release.cmake +19 -0
  168. gstaichi-2.1.1rc3.data/data/share/spirv_cross_hlsl/cmake/spirv_cross_hlslConfig.cmake +123 -0
  169. gstaichi-2.1.1rc3.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig-release.cmake +19 -0
  170. gstaichi-2.1.1rc3.data/data/share/spirv_cross_msl/cmake/spirv_cross_mslConfig.cmake +123 -0
  171. gstaichi-2.1.1rc3.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig-release.cmake +19 -0
  172. gstaichi-2.1.1rc3.data/data/share/spirv_cross_reflect/cmake/spirv_cross_reflectConfig.cmake +106 -0
  173. gstaichi-2.1.1rc3.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig-release.cmake +19 -0
  174. gstaichi-2.1.1rc3.data/data/share/spirv_cross_util/cmake/spirv_cross_utilConfig.cmake +123 -0
  175. gstaichi-2.1.1rc3.dist-info/METADATA +106 -0
  176. gstaichi-2.1.1rc3.dist-info/RECORD +179 -0
  177. gstaichi-2.1.1rc3.dist-info/WHEEL +5 -0
  178. gstaichi-2.1.1rc3.dist-info/licenses/LICENSE +201 -0
  179. gstaichi-2.1.1rc3.dist-info/top_level.txt +1 -0
@@ -0,0 +1,886 @@
1
+ # type: ignore
2
+
3
+ # pylint: disable=W0622
4
+ """
5
+ Math functions for glsl-like functions and other stuff.
6
+ """
7
+ import math
8
+
9
+ from gstaichi.lang import impl, ops
10
+ from gstaichi.lang.impl import static, zero
11
+ from gstaichi.lang.kernel_impl import func
12
+ from gstaichi.lang.matrix import Matrix
13
+ from gstaichi.lang.ops import (
14
+ acos,
15
+ asin,
16
+ atan2,
17
+ ceil,
18
+ cos,
19
+ exp,
20
+ floor,
21
+ log,
22
+ max,
23
+ min,
24
+ pow,
25
+ round,
26
+ sin,
27
+ sqrt,
28
+ tan,
29
+ tanh,
30
+ )
31
+ from gstaichi.types import matrix, template, vector
32
+ from gstaichi.types.primitive_types import f64, u32, u64
33
+
34
+ cfg = impl.default_cfg
35
+
36
+ e = math.e
37
+ """The mathematical constant e = 2.718281….
38
+ Directly imported from the Python standard library `math`.
39
+ """
40
+
41
+ pi = math.pi
42
+ """The mathematical constant π = 3.141592….
43
+ Directly imported from the Python standard library `math`.
44
+ """
45
+
46
+ inf = math.inf
47
+ """A floating-point positive infinity. (For negative infinity, use `-inf`).
48
+ Directly imported from the Python standard library `math`.
49
+ """
50
+
51
+ nan = math.nan
52
+ """A floating-point "not a number" (NaN) value.
53
+ Directly imported from the Python standard library `math`
54
+ """
55
+
56
+ vec2 = vector(2, cfg().default_fp)
57
+ """2D floating vector type.
58
+ """
59
+
60
+ vec3 = vector(3, cfg().default_fp)
61
+ """3D floating vector type.
62
+ """
63
+
64
+ vec4 = vector(4, cfg().default_fp)
65
+ """4D floating vector type.
66
+ """
67
+
68
+ ivec2 = vector(2, cfg().default_ip)
69
+ """2D signed int vector type.
70
+ """
71
+
72
+ ivec3 = vector(3, cfg().default_ip)
73
+ """3D signed int vector type.
74
+ """
75
+
76
+ ivec4 = vector(4, cfg().default_ip)
77
+ """3D signed int vector type.
78
+ """
79
+
80
+ uvec2 = vector(2, cfg().default_up)
81
+ """2D unsigned int vector type.
82
+ """
83
+
84
+ uvec3 = vector(3, cfg().default_up)
85
+ """3D unsigned int vector type.
86
+ """
87
+
88
+ uvec4 = vector(4, cfg().default_up)
89
+ """4D unsigned int vector type.
90
+ """
91
+
92
+ mat2 = matrix(2, 2, cfg().default_fp)
93
+ """2x2 floating matrix type.
94
+ """
95
+
96
+ mat3 = matrix(3, 3, cfg().default_fp)
97
+ """3x3 floating matrix type.
98
+ """
99
+
100
+ mat4 = matrix(4, 4, cfg().default_fp)
101
+ """4x4 floating matrix type.
102
+ """
103
+
104
+
105
+ @func
106
+ def mix(x, y, a):
107
+ """Performs a linear interpolation between `x` and `y` using
108
+ `a` to weight between them. The return value is computed as
109
+ `x * (1 - a) + a * y`.
110
+
111
+ The arguments can be scalars or :class:`~gstaichi.Matrix`,
112
+ as long as the operation can be performed.
113
+
114
+ This function is similar to the `mix` function in GLSL.
115
+
116
+ Args:
117
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
118
+ the start of the range in which to interpolate.
119
+ y (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
120
+ the end of the range in which to interpolate.
121
+ a (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
122
+ the weight to use to interpolate between x and y.
123
+
124
+ Returns:
125
+ (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The linear
126
+ interpolation of `x` and `y` by weight `a`.
127
+
128
+ Example::
129
+
130
+ >>> x = ti.Vector([1, 1, 1])
131
+ >>> y = ti.Vector([2, 2, 2])
132
+ >>> a = ti.Vector([1, 0, 0])
133
+ >>> ti.math.mix(x, y, a)
134
+ [2.000000, 1.000000, 1.000000]
135
+ >>> x = ti.Matrix([[1, 2], [2, 3]], ti.f32)
136
+ >>> y = ti.Matrix([[3, 5], [4, 5]], ti.f32)
137
+ >>> a = 0.5
138
+ >>> ti.math.mix(x, y, a)
139
+ [[2.000000, 3.500000], [3.000000, 4.000000]]
140
+ """
141
+ return x * (1.0 - a) + y * a
142
+
143
+
144
+ @func
145
+ def clamp(x, xmin, xmax):
146
+ """Constrain a value to lie between two further values, element-wise.
147
+ The returned value is computed as `min(max(x, xmin), xmax)`.
148
+
149
+ The arguments can be scalars or :class:`~gstaichi.Matrix`,
150
+ as long as they can be broadcasted to a common shape.
151
+
152
+ Args:
153
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
154
+ the value to constrain.
155
+ y (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
156
+ the lower end of the range into which to constrain `x`.
157
+ a (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
158
+ the upper end of the range into which to constrain `x`.
159
+
160
+ Returns:
161
+ The value of `x` constrained to the range `xmin` to `xmax`.
162
+
163
+ Example::
164
+
165
+ >>> v = ti.Vector([0, 0.5, 1.0, 1.5])
166
+ >>> ti.math.clamp(v, 0.5, 1.0)
167
+ [0.500000, 0.500000, 1.000000, 1.000000]
168
+ >>> x = ti.Matrix([[0, 1], [-2, 2]], ti.f32)
169
+ >>> y = ti.Matrix([[1, 2], [1, 2]], ti.f32)
170
+ >>> ti.math.clamp(x, 0.5, y)
171
+ [[0.500000, 1.000000], [0.500000, 2.000000]]
172
+ """
173
+ return ops.min(xmax, ops.max(xmin, x))
174
+
175
+
176
+ @func
177
+ def step(edge, x):
178
+ """Generate a step function by comparing two values, element-wise.
179
+
180
+ `step` generates a step function by comparing `x` to edge.
181
+ For element i of the return value, 0.0 is returned if x[i] < edge[i],
182
+ and 1.0 is returned otherwise.
183
+
184
+ The two arguments can be scalars or :class:`~gstaichi.Matrix`,
185
+ as long as they can be broadcasted to a common shape.
186
+
187
+ Args:
188
+ edge (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
189
+ the location of the edge of the step function.
190
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specify
191
+ the value to be used to generate the step function.
192
+
193
+ Returns:
194
+ The return value is computed as `x >= edge`, with type promoted.
195
+
196
+ Example::
197
+
198
+ >>> x = ti.Matrix([[0, 1], [2, 3]], ti.f32)
199
+ >>> y = 1
200
+ >>> ti.math.step(x, y)
201
+ [[1.000000, 1.000000], [0.000000, 0.000000]]
202
+ """
203
+ return ops.cast(x >= edge, float)
204
+
205
+
206
+ @func
207
+ def fract(x):
208
+ """Compute the fractional part of the argument, element-wise.
209
+ It's equivalent to `x - ti.floor(x)`.
210
+
211
+ Args:
212
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The
213
+ input value.
214
+
215
+ Returns:
216
+ The fractional part of `x`.
217
+
218
+ Example::
219
+
220
+ >>> x = ti.Vector([-1.2, -0.7, 0.3, 1.2])
221
+ >>> ti.math.fract(x)
222
+ [0.800000, 0.300000, 0.300000, 0.200000]
223
+ """
224
+ return x - ops.floor(x)
225
+
226
+
227
+ @func
228
+ def smoothstep(edge0, edge1, x):
229
+ """Performs smooth Hermite interpolation between 0 and 1 when
230
+ `edge0 < x < edge1`, element-wise.
231
+
232
+ The arguments can be scalars or :class:`~gstaichi.Matrix`,
233
+ as long as they can be broadcasted to a common shape.
234
+
235
+ This function is equivalent to the `smoothstep` in GLSL.
236
+
237
+ Args:
238
+ edge0 (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specifies
239
+ the value of the lower edge of the Hermite function.
240
+ edge1 (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specifies
241
+ the value of the upper edge of the Hermite function.
242
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): Specifies
243
+ the source value for interpolation.
244
+
245
+ Returns:
246
+ The smoothly interpolated value.
247
+
248
+ Example::
249
+
250
+ >>> edge0 = ti.Vector([0, 1, 2])
251
+ >>> edge1 = 1
252
+ >>> x = ti.Vector([0.5, 1.5, 2.5])
253
+ >>> ti.math.smoothstep(edge0, edge1, x)
254
+ [0.500000, 1.000000, 0.000000]
255
+ """
256
+ t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0)
257
+ return t * t * (3.0 - 2.0 * t)
258
+
259
+
260
+ @func
261
+ def sign(x):
262
+ """Extract the sign of the parameter, element-wise.
263
+
264
+ Args:
265
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The
266
+ input value.
267
+
268
+ Returns:
269
+ -1.0 if `x` is less than 0.0, 0.0 if `x` is equal to 0.0,
270
+ and +1.0 if `x` is greater than 0.0.
271
+
272
+ Example::
273
+
274
+ >>> x = ti.Vector([-1.0, 0.0, 1.0])
275
+ >>> ti.math.sign(x)
276
+ [-1.000000, 0.000000, 1.000000]
277
+ """
278
+ return ops.cast((x >= 0.0), float) - ops.cast((x <= 0.0), float)
279
+
280
+
281
+ @func
282
+ def normalize(v):
283
+ """Calculates the unit vector in the same direction as the
284
+ original vector `v`.
285
+
286
+ It's equivalent to the `normalize` function is GLSL.
287
+
288
+ Args:
289
+ x (:class:`~gstaichi.Matrix`): The vector to normalize.
290
+
291
+ Returns:
292
+ The normalized vector :math:`v/|v|`.
293
+
294
+ Example::
295
+
296
+ >>> v = ti.Vector([1, 2, 3])
297
+ >>> ti.math.normalize(v)
298
+ [0.267261, 0.534522, 0.801784]
299
+ """
300
+ return v / v.norm()
301
+
302
+
303
+ @func
304
+ def log2(x):
305
+ """Return the base 2 logarithm of `x`, so that if :math:`2^y=x`,
306
+ then :math:`y=\\log2(x)`.
307
+
308
+ This is equivalent to the `log2` function is GLSL.
309
+
310
+ Args:
311
+ x (:class:`~gstaichi.Matrix`): The input value.
312
+
313
+ Returns:
314
+ The base 2 logarithm of `x`.
315
+
316
+ Example::
317
+
318
+ >>> x = ti.Vector([1., 2., 3.])
319
+ >>> ti.math.log2(x)
320
+ [0.000000, 1.000000, 1.584962]
321
+ """
322
+ return ops.log(x) / static(ops.log(2.0))
323
+
324
+
325
+ @func
326
+ def reflect(x, n):
327
+ """Calculate the reflection direction for an incident vector.
328
+
329
+ For a given incident vector `x` and surface normal `n` this
330
+ function returns the reflection direction calculated as
331
+ :math:`x - 2.0 * dot(x, n) * n`.
332
+
333
+ This is equivalent to the `reflect` function is GLSL.
334
+
335
+ `n` should be normalized in order to achieve the desired result.
336
+
337
+ Args:
338
+ x (:class:`~gstaichi.Matrix`): The incident vector.
339
+ n (:class:`~gstaichi.Matrix`): The normal vector.
340
+
341
+ Returns:
342
+ The reflected vector.
343
+
344
+ Example::
345
+
346
+ >>> x = ti.Vector([1., 2., 3.])
347
+ >>> n = ti.Vector([0., 1., 0.])
348
+ >>> ti.math.reflect(x, n)
349
+ [1.000000, -2.000000, 3.000000]
350
+ """
351
+ k = x.dot(n)
352
+ return x - 2.0 * k * n
353
+
354
+
355
+ @func
356
+ def degrees(x):
357
+ """Convert `x` in radians to degrees, element-wise.
358
+
359
+ Args:
360
+ x (:class:`~gstaichi.Matrix`): The input angle in radians.
361
+
362
+ Returns:
363
+ angle in degrees.
364
+
365
+ Example::
366
+
367
+ >>> x = ti.Vector([-pi/2, pi/2])
368
+ >>> ti.math.degrees(x)
369
+ [-90.000000, 90.000000]
370
+ """
371
+ return x * static(180.0 / pi)
372
+
373
+
374
+ @func
375
+ def radians(x):
376
+ """Convert `x` in degrees to radians, element-wise.
377
+
378
+ Args:
379
+ x (:class:`~gstaichi.Matrix`): The input angle in degrees.
380
+
381
+ Returns:
382
+ angle in radians.
383
+
384
+ Example::
385
+
386
+ >>> x = ti.Vector([-90., 45., 90.])
387
+ >>> ti.math.radians(x) / pi
388
+ [-0.500000, 0.250000, 0.500000]
389
+ """
390
+ return x * static(pi / 180.0)
391
+
392
+
393
+ @func
394
+ def distance(x, y):
395
+ """Calculate the distance between two points.
396
+
397
+ This function is equivalent to the `distance` function is GLSL.
398
+
399
+ Args:
400
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The first input point.
401
+ y (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The second input point.
402
+
403
+ Returns:
404
+ The distance between the two points.
405
+
406
+ Example::
407
+
408
+ >>> x = ti.Vector([0, 0, 0])
409
+ >>> y = ti.Vector([1, 1, 1])
410
+ >>> ti.math.distance(x, y)
411
+ 1.732051
412
+ """
413
+ return (x - y).norm()
414
+
415
+
416
+ @func
417
+ def refract(x, n, eta):
418
+ """Calculate the refraction direction for an incident vector.
419
+
420
+ This function is equivalent to the `refract` function in GLSL.
421
+
422
+ Args:
423
+ x (:class:`~gstaichi.Matrix`): The incident vector.
424
+ n (:class:`~gstaichi.Matrix`): The normal vector.
425
+ eta (float): The ratio of indices of refraction.
426
+
427
+ Returns:
428
+ :class:`~gstaichi.Matrix`: The refraction direction vector.
429
+
430
+ Example::
431
+
432
+ >>> x = ti.Vector([1., 1., 1.])
433
+ >>> y = ti.Vector([0, 1., 0])
434
+ >>> ti.math.refract(x, y, 2.0)
435
+ [2.000000, -1.000000, 2.000000]
436
+ """
437
+ dxn = x.dot(n)
438
+ result = zero(x)
439
+ k = 1.0 - eta * eta * (1.0 - dxn * dxn)
440
+ if k >= 0.0:
441
+ result = eta * x - (eta * dxn + ops.sqrt(k)) * n
442
+ return result
443
+
444
+
445
+ @func
446
+ def dot(x, y):
447
+ """Calculate the dot product of two vectors.
448
+
449
+ Args:
450
+ x (:class:`~gstaichi.Matrix`): The first input vector.
451
+ y (:class:`~gstaichi.Matrix`): The second input vector.
452
+
453
+ Returns:
454
+ The dot product of two vectors.
455
+
456
+ Example::
457
+
458
+ >>> x = ti.Vector([1., 1., 0.])
459
+ >>> y = ti.Vector([0., 1., 1.])
460
+ >>> ti.math.dot(x, y)
461
+ 1.000000
462
+ """
463
+ return x.dot(y)
464
+
465
+
466
+ @func
467
+ def cross(x, y):
468
+ """Calculate the cross product of two vectors.
469
+
470
+ The two input vectors must have the same dimension :math:`d <= 3`.
471
+
472
+ This function calls the `cross` method of :class:`~gstaichi.Vector`.
473
+
474
+ Args:
475
+ x (:class:`~gstaichi.Matrix`): The first input vector.
476
+ y (:class:`~gstaichi.Matrix`): The second input vector.
477
+
478
+ Returns:
479
+ The cross product of two vectors.
480
+
481
+ Example::
482
+
483
+ >>> x = ti.Vector([1., 0., 0.])
484
+ >>> y = ti.Vector([0., 1., 0.])
485
+ >>> ti.math.cross(x, y)
486
+ [0.000000, 0.000000, 1.000000]
487
+ """
488
+ return x.cross(y)
489
+
490
+
491
+ @func
492
+ def mod(x, y):
493
+ """Compute value of one parameter modulo another, element-wise.
494
+
495
+ Args:
496
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The first input.
497
+ y (:mod:`~gstaichi.types.primitive_types`, :class:`~gstaichi.Matrix`): The second input.
498
+
499
+ Returns:
500
+ the value of `x` modulo `y`. This is computed as `x - y * floor(x/y)`.
501
+
502
+ Example::
503
+
504
+ >>> x = ti.Vector([-0.5, 0.5, 1.])
505
+ >>> y = 1.0
506
+ >>> ti.math.mod(x, y)
507
+ [0.500000, 0.500000, 0.000000]
508
+ """
509
+ return x - y * ops.floor(x / y)
510
+
511
+
512
+ @func
513
+ def translate(dx, dy, dz):
514
+ """Constructs a translation Matrix with shape (4, 4).
515
+
516
+ Args:
517
+ dx (float): delta x.
518
+ dy (float): delta y.
519
+ dz (float): delta z.
520
+
521
+ Returns:
522
+ :class:`~gstaichi.math.mat4`: translation matrix.
523
+
524
+ Example::
525
+
526
+ >>> ti.math.translate(1, 2, 3)
527
+ [[ 1. 0. 0. 1.]
528
+ [ 0. 1. 0. 2.]
529
+ [ 0. 0. 1. 3.]
530
+ [ 0. 0. 0. 1.]]
531
+ """
532
+ return mat4(
533
+ [
534
+ [1.0, 0.0, 0.0, dx],
535
+ [0.0, 1.0, 0.0, dy],
536
+ [0.0, 0.0, 1.0, dz],
537
+ [0.0, 0.0, 0.0, 1.0],
538
+ ]
539
+ )
540
+
541
+
542
+ @func
543
+ def scale(sx, sy, sz):
544
+ """Constructs a scale Matrix with shape (4, 4).
545
+
546
+ Args:
547
+ sx (float): scale x.
548
+ sy (float): scale y.
549
+ sz (float): scale z.
550
+
551
+ Returns:
552
+ :class:`~gstaichi.math.mat4`: scale matrix.
553
+
554
+ Example::
555
+
556
+ >>> ti.math.scale(1, 2, 3)
557
+ [[ 1. 0. 0. 0.]
558
+ [ 0. 2. 0. 0.]
559
+ [ 0. 0. 3. 0.]
560
+ [ 0. 0. 0. 1.]]
561
+ """
562
+ return mat4(
563
+ [
564
+ [sx, 0.0, 0.0, 0.0],
565
+ [0.0, sy, 0.0, 0.0],
566
+ [0.0, 0.0, sz, 0.0],
567
+ [0.0, 0.0, 0.0, 1.0],
568
+ ]
569
+ )
570
+
571
+
572
+ @func
573
+ def rot_by_axis(axis, ang):
574
+ """Returns the 4x4 matrix representation of a 3d rotation with given axis `axis` and angle `ang`.
575
+
576
+ Args:
577
+ axis (vec3): rotation axis
578
+ ang (float): angle in radians unit
579
+
580
+ Returns:
581
+ :class:`~gstaichi.math.mat4`: rotation matrix
582
+ """
583
+ c = ops.cos(ang)
584
+ s = ops.sin(ang)
585
+
586
+ axis = normalize(axis)
587
+ temp = (1 - c) * axis
588
+ return mat4(
589
+ [
590
+ [
591
+ c + temp[0] * axis[0],
592
+ temp[0] * axis[1] + s * axis[2],
593
+ temp[0] * axis[2] - s * axis[1],
594
+ 0.0,
595
+ ],
596
+ [
597
+ temp[1] * axis[0] - s * axis[2],
598
+ c + temp[1] * axis[1],
599
+ temp[1] * axis[2] + s * axis[0],
600
+ 0.0,
601
+ ],
602
+ [
603
+ temp[2] * axis[0] + s * axis[1],
604
+ temp[2] * axis[1] - s * axis[0],
605
+ c + temp[2] * axis[2],
606
+ 0.0,
607
+ ],
608
+ [0.0, 0.0, 0.0, 1.0],
609
+ ]
610
+ )
611
+
612
+
613
+ @func
614
+ def rot_yaw_pitch_roll(yaw, pitch, roll):
615
+ """Returns a 4x4 homogeneous rotation matrix representing the 3d rotation with Euler angles (rotate with Y axis first, X axis second, Z axis third).
616
+
617
+ Args:
618
+ yaw (float): yaw angle in radians unit
619
+ pitch (float): pitch angle in radians unit
620
+ roll (float): roll angle in radians unit
621
+
622
+ Returns:
623
+ :class:`~gstaichi.math.mat4`: rotation matrix
624
+ """
625
+ ch = ops.cos(yaw)
626
+ sh = ops.sin(yaw)
627
+ cp = ops.cos(pitch)
628
+ sp = ops.sin(pitch)
629
+ cb = ops.cos(roll)
630
+ sb = ops.sin(roll)
631
+
632
+ return mat4(
633
+ [
634
+ [ch * cb + sh * sp * sb, sb * cp, -sh * cb + ch * sp * sb, 0.0],
635
+ [-ch * sb + sh * sp * cb, cb * cp, sb * sh + ch * sp * cb, 0.0],
636
+ [sh * cp, -sp, ch * cp, 0.0],
637
+ [0.0, 0.0, 0.0, 1.0],
638
+ ]
639
+ )
640
+
641
+
642
+ @func
643
+ def rotation2d(ang):
644
+ """Returns the matrix representation of a 2d counter-clockwise rotation,
645
+ given the angle of rotation.
646
+
647
+ Args:
648
+ ang (float): Angle of rotation in radians.
649
+
650
+ Returns:
651
+ :class:`~gstaichi.math.mat2`: 2x2 rotation matrix.
652
+
653
+ Example::
654
+
655
+ >>>ti.math.rotation2d(ti.math.radians(30))
656
+ [[0.866025, -0.500000], [0.500000, 0.866025]]
657
+ """
658
+ ca, sa = ops.cos(ang), ops.sin(ang)
659
+ return mat2([[ca, -sa], [sa, ca]])
660
+
661
+
662
+ @func
663
+ def rotation3d(ang_x, ang_y, ang_z):
664
+ """Returns a 4x4 homogeneous rotation matrix representing the 3d rotation with Euler angles (rotate with Y axis first, X axis second, Z axis third).
665
+
666
+ Args:
667
+ ang_x (float): angle in radians unit around X axis
668
+ ang_y (float): angle in radians unit around Y axis
669
+ ang_z (float): angle in radians unit around Z axis
670
+ Returns:
671
+ :class:`~gstaichi.math.mat4`: rotation matrix
672
+ Example:
673
+
674
+ >>> ti.math.rotation3d(0.52, -0.785, 1.046)
675
+ [[ 0.05048351 -0.61339645 -0.78816002 0. ]
676
+ [ 0.65833154 0.61388511 -0.4355969 0. ]
677
+ [ 0.75103329 -0.49688014 0.4348093 0. ]
678
+ [ 0. 0. 0. 1. ]]
679
+ """
680
+ return rot_yaw_pitch_roll(ang_z, ang_x, ang_y)
681
+
682
+
683
+ @func
684
+ def eye(n: template()):
685
+ """Returns the nxn identity matrix.
686
+
687
+ Alias for :func:`~gstaichi.Matrix.identity`.
688
+ """
689
+ return Matrix.identity(float, n)
690
+
691
+
692
+ @func
693
+ def length(x):
694
+ """Calculate the length of a vector.
695
+
696
+ This function is equivalent to the `length` function in GLSL.
697
+ Args:
698
+ x (:class:`~gstaichi.Matrix`): The vector of which to calculate the length.
699
+
700
+ Returns:
701
+ The Euclidean norm of the vector.
702
+
703
+ Example::
704
+
705
+ >>> x = ti.Vector([1, 1, 1])
706
+ >>> ti.math.length(x)
707
+ 1.732051
708
+ """
709
+ return x.norm()
710
+
711
+
712
+ @func
713
+ def determinant(m):
714
+ """Alias for :func:`gstaichi.Matrix.determinant`."""
715
+ return m.determinant()
716
+
717
+
718
+ @func
719
+ def inverse(mat): # pylint: disable=R1710
720
+ """Calculate the inverse of a matrix.
721
+
722
+ This function is equivalent to the `inverse` function in GLSL.
723
+
724
+ Args:
725
+ mat (:class:`gstaichi.Matrix`): The matrix of which to take the inverse. \
726
+ Supports only 2x2, 3x3 and 4x4 matrices.
727
+
728
+ Returns:
729
+ Inverse of the input matrix.
730
+
731
+ Example::
732
+
733
+ >>> m = ti.math.mat3([(1, 1, 0), (0, 1, 1), (0, 0, 1)])
734
+ >>> ti.math.inverse(m)
735
+ [[1.000000, -1.000000, 1.000000],
736
+ [0.000000, 1.000000, -1.000000],
737
+ [0.000000, 0.000000, 1.000000]]
738
+ """
739
+ return mat.inverse()
740
+
741
+
742
+ @func
743
+ def isinf(x):
744
+ """Determines whether the parameter is positive or negative infinity, element-wise.
745
+
746
+ Args:
747
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`gstaichi.Matrix`): The input.
748
+
749
+ Example:
750
+
751
+ >>> x = ti.math.vec4(inf, -inf, nan, 1)
752
+ >>> ti.math.isinf(x)
753
+ [1, 1, 0, 0]
754
+
755
+ Returns:
756
+ For each element i of the result, returns 1 if x[i] is posititve or negative floating point infinity and 0 otherwise.
757
+ """
758
+ ftype = impl.get_runtime().default_fp
759
+ fx = ops.cast(x, ftype)
760
+ if static(ftype == f64):
761
+ y = ops.bit_cast(fx, u64)
762
+ return (ops.cast(y >> 32, u32) & 0x7FFFFFFF) == 0x7FF00000 and (ops.cast(y, u32) == 0)
763
+
764
+ y = ops.bit_cast(fx, u32)
765
+ return (y & 0x7FFFFFFF) == 0x7F800000
766
+
767
+
768
+ @func
769
+ def isnan(x):
770
+ """Determines whether the parameter is a number, element-wise.
771
+
772
+ Args:
773
+ x (:mod:`~gstaichi.types.primitive_types`, :class:`gstaichi.Matrix`): The input.
774
+
775
+ Example:
776
+
777
+ >>> x = ti.math.vec4(nan, -nan, inf, 1)
778
+ >>> ti.math.isnan(x)
779
+ [1, 1, 0, 0]
780
+
781
+ Returns:
782
+ For each element i of the result, returns 1 if x[i] is posititve or negative floating point NaN (Not a Number) and 0 otherwise.
783
+ """
784
+ ftype = impl.get_runtime().default_fp
785
+ fx = ops.cast(x, ftype)
786
+ if static(ftype == f64):
787
+ y = ops.bit_cast(fx, u64)
788
+ return (ops.cast(y >> 32, u32) & 0x7FFFFFFF) + (ops.cast(y, u32) != 0) > 0x7FF00000
789
+
790
+ y = ops.bit_cast(fx, u32)
791
+ return (y & 0x7FFFFFFF) > 0x7F800000
792
+
793
+
794
+ @func
795
+ def vdir(ang):
796
+ """Returns the 2d unit vector with argument equals `ang`.
797
+
798
+ x (:mod:`~gstaichi.types.primitive_types`): The input angle in radians.
799
+
800
+ Example:
801
+
802
+ >>> x = pi / 2
803
+ >>> ti.math.vdir(x)
804
+ [0, 1]
805
+
806
+ Returns:
807
+ a 2d vector with argument equals `ang`.
808
+ """
809
+ return vec2(cos(ang), sin(ang))
810
+
811
+
812
+ @func
813
+ def popcnt(x):
814
+ return ops.popcnt(x)
815
+
816
+
817
+ @func
818
+ def clz(x):
819
+ return ops.clz(x)
820
+
821
+
822
+ __all__ = [
823
+ "acos",
824
+ "asin",
825
+ "atan2",
826
+ "ceil",
827
+ "clamp",
828
+ "clz",
829
+ "cos",
830
+ "cross",
831
+ "degrees",
832
+ "determinant",
833
+ "distance",
834
+ "dot",
835
+ "e",
836
+ "exp",
837
+ "eye",
838
+ "floor",
839
+ "fract",
840
+ "inf",
841
+ "inverse",
842
+ "isinf",
843
+ "isnan",
844
+ "ivec2",
845
+ "ivec3",
846
+ "ivec4",
847
+ "length",
848
+ "log",
849
+ "log2",
850
+ "mat2",
851
+ "mat3",
852
+ "mat4",
853
+ "max",
854
+ "min",
855
+ "mix",
856
+ "mod",
857
+ "translate",
858
+ "scale",
859
+ "nan",
860
+ "normalize",
861
+ "pi",
862
+ "pow",
863
+ "popcnt",
864
+ "radians",
865
+ "reflect",
866
+ "refract",
867
+ "rot_by_axis",
868
+ "rot_yaw_pitch_roll",
869
+ "rotation2d",
870
+ "rotation3d",
871
+ "round",
872
+ "sign",
873
+ "sin",
874
+ "smoothstep",
875
+ "sqrt",
876
+ "step",
877
+ "tan",
878
+ "tanh",
879
+ "uvec2",
880
+ "uvec3",
881
+ "uvec4",
882
+ "vdir",
883
+ "vec2",
884
+ "vec3",
885
+ "vec4",
886
+ ]