Skip to content

Commit fc0c590

Browse files
math: add promoted elementary real ufuncs (#870)
* math: add promoted atan2 for mixed tensor dtypes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * math: add promoted sqrt ufunc Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted cube root ufunc Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted exp and log ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted trigonometric ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted inverse and hyperbolic ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted inverse hyperbolic ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): expand promoted exponential ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted rounding ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): add promoted binary numeric ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): promote broadcast binary ufuncs Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): promote mixed-dtype copysign Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): promote mixed-dtype nextafter Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): promote degree-radian conversions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(math): promote mixed-input heaviside Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 749a591 commit fc0c590

10 files changed

Lines changed: 1043 additions & 1 deletion

‎docs/NUMPY_PARITY.md‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,25 @@ NumPy-style axis-removed output shape.
206206
gradients along selected axes; elementwise logical AND/OR/XOR/NOT; integer
207207
bitwise AND/OR/XOR/invert and scalar left/right shifts. Gradients support
208208
uniform spacing or monotonic non-uniform coordinates and first/second-order
209-
boundary differences. Same-dtype `floor_divide` and `floor_divide_scalar`
209+
boundary differences. `atan2_promoted`, `sqrt_promoted`, `cbrt_promoted`,
210+
`exp_promoted`, `exp2_promoted`, `expm1_promoted`, `log_promoted`,
211+
`log2_promoted`, `log10_promoted`, `log1p_promoted`, `sin_promoted`,
212+
`cos_promoted`, and `tan_promoted`, the inverse-trigonometric functions, and
213+
the direct and inverse hyperbolic functions provide NumPy-style
214+
floating-point outputs for real numeric or boolean inputs. They return f64
215+
for f64 or 32-bit-and-wider integer inputs, otherwise f32.
216+
The same dtype rules apply to the broadcasted binary `copysign_promoted`,
217+
`hypot_promoted`, `logaddexp_promoted`, `logaddexp2_promoted`, and
218+
`nextafter_promoted`.
219+
`rint_promoted` follows the same floating output rules and uses ties-to-even;
220+
`floor_promoted`, `ceil_promoted`, and `trunc_promoted` preserve the input
221+
dtype, including exact large integer values.
222+
NumPy float16 results for booleans and 8-bit integers are widened because VTL
223+
has no float16. The legacy same-dtype `Tensor.atan2()`, `Tensor.sqrt()`, and
224+
`Tensor.cbrt()` methods preserve their generic input dtype; use the promoted
225+
functions for integer or mixed-dtype results. Complex and string inputs are
226+
unsupported.
227+
Same-dtype `floor_divide` and `floor_divide_scalar`
210228
round signed quotients toward negative infinity, preserve input dtype, return
211229
zero for integer zero divisors, and follow IEEE results for floating-point
212230
zero divisors. Mixed-dtype promotion remains tracked in
@@ -233,6 +251,17 @@ See
233251
[gradient tests](../autograd_tests/gates_remainder_test.v), and
234252
[dtype conversion tutorial](./TUTORIAL_FIRST_STEPS.md#numeric-dtype-conversion).
235253

254+
**Remaining ufunc gaps:** same-dtype methods exist for `abs`, `sign`, extrema,
255+
`fmod`, `pow`, and `frexp`/`modf`/`ldexp`, but NumPy's
256+
mixed-dtype, broadcast, and edge-case contracts are not fully covered. Unary
257+
`abs`/`sign` and the multi-output decomposition functions still need broader
258+
dtype and special-value differential tests; promoted mixed-input variants
259+
remain for `maximum`/`minimum`/`fmax`/`fmin`, `fmod`, `pow`, and `ldexp`.
260+
`erf`/`erfc`, `gamma`, and `log_gamma` are useful VTL math APIs beyond NumPy
261+
core ufunc parity. The general NumPy ufunc `out` and `where` execution protocol
262+
is tracked separately in
263+
[#783](https://github.com/vlang/vtl/issues/783).
264+
236265
Promoted tensor-to-tensor result dtypes are:
237266

238267
| Tensor operands | Add/subtract/multiply | True divide | Remainder |

‎docs/TUTORIAL_MATH_UFUNCS.md‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,144 @@ VTL provides elementwise math operations on tensors, including decomposition of
44
floating-point values into integer/fractional parts or binary mantissas and
55
exponents.
66

7+
## Promoted square and cube roots
8+
9+
`vtl.sqrt_promoted[R, T](values)` and `vtl.cbrt_promoted[R, T](values)` return
10+
floating-point square and cube roots. They return f64 for f64 or
11+
32-bit-and-wider integer inputs, and f32 otherwise. NumPy's float16 results for
12+
booleans and 8-bit integers are widened because VTL does not support float16.
13+
The output type is explicit and validated:
14+
15+
```v
16+
import vtl
17+
18+
values := vtl.from_1d[i32]([1, 2, 9])!
19+
roots := vtl.sqrt_promoted[f64, i32](values)!
20+
println(roots.to_array()) // [1.0, 1.414..., 3.0]
21+
22+
signed_values := vtl.from_1d[i16]([-8, 0, 27])!
23+
cube_roots := vtl.cbrt_promoted[f32, i16](signed_values)!
24+
println(cube_roots.to_array()) // [-2.0, 0.0, 3.0]
25+
```
26+
27+
Complex and string inputs are unsupported. The same-dtype `Tensor.sqrt()` and
28+
`Tensor.cbrt()` methods remain available for compatibility; use the promoted
29+
functions when NumPy-style integer output is needed.
30+
31+
## Promoted exponential and logarithm
32+
33+
`vtl.exp_promoted[R, T](values)` and `vtl.log_promoted[R, T](values)`, along
34+
with `exp2_promoted`, `expm1_promoted`, `log2_promoted`, `log10_promoted`, and
35+
`log1p_promoted`, share the promoted real-output dtype rules: f64 for f64 or
36+
32-bit-and-wider integer inputs, and f32 otherwise. They preserve IEEE domain
37+
results, such as `-inf` for `log(0)` and `NaN` for logarithms of negative
38+
values. `expm1` and `log1p` retain precision near zero.
39+
40+
```v
41+
import vtl
42+
43+
values := vtl.from_1d[i16]([0, 1])!
44+
exponents := vtl.exp_promoted[f32, i16](values)!
45+
logs := vtl.log_promoted[f32, i16](values)!
46+
println(exponents.to_array())
47+
println(logs.to_array()) // [-inf, 0.0]
48+
```
49+
50+
## Promoted rounding
51+
52+
`rint_promoted` rounds ties to even and uses the same floating output dtype
53+
rules as `sqrt_promoted`. `floor_promoted`, `ceil_promoted`, and
54+
`trunc_promoted` preserve their input dtype, including booleans and integers;
55+
integer inputs are copied without conversion through floating point.
56+
57+
```v
58+
import vtl
59+
60+
values := vtl.from_1d[f64]([-2.5, -1.5, 1.5, 2.5])!
61+
rounded := vtl.rint_promoted[f64, f64](values)!
62+
floored := vtl.floor_promoted[f64, f64](values)!
63+
println(rounded.to_array()) // [-2.0, -2.0, 2.0, 2.0]
64+
println(floored.to_array()) // [-3.0, -2.0, 1.0, 2.0]
65+
```
66+
67+
## Promoted trigonometric functions
68+
69+
`vtl.sin_promoted[R, T]`, `vtl.cos_promoted[R, T]`, and
70+
`vtl.tan_promoted[R, T]` apply the trigonometric functions elementwise and use
71+
the same promoted output dtype rules. Angles are in radians.
72+
`degrees_promoted` and `radians_promoted` convert angle units with those same
73+
output dtype rules.
74+
75+
```v
76+
import math
77+
import vtl
78+
79+
angles := vtl.from_1d[f32]([0.0, f32(math.pi / 4.0)])!
80+
sines := vtl.sin_promoted[f32, f32](angles)!
81+
cosines := vtl.cos_promoted[f32, f32](angles)!
82+
tangents := vtl.tan_promoted[f32, f32](angles)!
83+
println(sines.to_array())
84+
println(cosines.to_array())
85+
println(tangents.to_array())
86+
```
87+
88+
The promoted inverse trigonometric APIs are `asin_promoted`, `acos_promoted`,
89+
and `atan_promoted`; the hyperbolic counterparts are `sinh_promoted`,
90+
`cosh_promoted`, and `tanh_promoted`. Their inverse hyperbolic counterparts
91+
are `asinh_promoted`, `acosh_promoted`, and `atanh_promoted`. All preserve the
92+
floating-point domain behavior of the underlying operations; the trigonometric
93+
functions take angles in radians.
94+
95+
## Promoted arctangent with two arguments
96+
97+
`vtl.atan2_promoted[R, T, U](y, x)` broadcasts real numeric or boolean tensors
98+
and returns the floating-point result. It returns f64 if either input is f64 or a
99+
32-bit-or-wider integer; otherwise it returns f32. NumPy's float16 results for
100+
boolean and 8-bit integer inputs are widened because VTL does not support
101+
float16. Since V requires statically selected generic types, callers specify
102+
the output and both input types; mismatches return an error:
103+
104+
```v
105+
import vtl
106+
107+
y := vtl.from_1d[i32]([1, 0])!
108+
x := vtl.from_1d[f64]([1.0, -1.0])!
109+
angles := vtl.atan2_promoted[f64, i32, f64](y, x)!
110+
println(angles.to_array()) // [0.785398..., 3.141592...]
111+
```
112+
113+
Complex and string inputs are unsupported. The same-dtype
114+
`Tensor.atan2()` method remains available for compatibility; use the promoted
115+
function when NumPy-style integer or mixed-dtype output is needed.
116+
117+
## Promoted binary numeric functions
118+
119+
`copysign_promoted`, `heaviside_promoted`, `hypot_promoted`,
120+
`logaddexp_promoted`, `logaddexp2_promoted`, and `nextafter_promoted` broadcast
121+
real numeric or boolean operands and use the same floating output dtype rules
122+
as `atan2_promoted`. The f32 `nextafter_promoted` path steps at f32 precision.
123+
The log-add-exp variants use stable calculations for large-magnitude inputs:
124+
125+
```v
126+
import math
127+
import vtl
128+
129+
a := vtl.from_1d[i16]([1000, -1000])!
130+
b := vtl.from_1d[f32]([1000.0, -1000.0])!
131+
values := vtl.logaddexp_promoted[f32, i16, f32](a, b)!
132+
println(values.to_array())
133+
134+
x := vtl.from_1d[i16]([0, 1])!
135+
toward := vtl.from_1d[f32]([1.0, -math.inf(1)])!
136+
steps := vtl.nextafter_promoted[f32, i16, f32](x, toward)!
137+
println(steps.to_array())
138+
139+
x1 := vtl.from_1d[f32]([-1.0, 0.0, 1.0])!
140+
x2 := vtl.from_1d[i8]([0, 2, 0])!
141+
heaviside_result := vtl.heaviside_promoted[f32, f32, i8](x1, x2)!
142+
println(heaviside_result.to_array())
143+
```
144+
7145
## Floor division
8146

9147
`Tensor.floor_divide()` broadcasts same-dtype real integer or floating-point
@@ -77,6 +215,9 @@ values, including negative zero and negative infinity. Signed integers are true
77215
when negative; unsigned integers and booleans return false for every element.
78216
`vtl.copysign(magnitude, sign)` combines the magnitude of its first f32/f64
79217
tensor with the sign bit of its second, with standard broadcasting.
218+
`vtl.copysign_promoted` additionally accepts mixed real numeric and boolean
219+
inputs and applies the same NumPy-style float output promotion as
220+
`atan2_promoted`, including negative-zero and NaN sign bits.
80221

81222
```v
82223
import vtl

0 commit comments

Comments
 (0)