Skip to content

Commit b19e2a3

Browse files
committed
Add rsqrt
1 parent 541b407 commit b19e2a3

21 files changed

Lines changed: 370 additions & 104 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- 4.1.0
44
- Added `softplus()` and `erf()` special functions
55
- Added `sinh()` and `cosh()` trigonometric functions
6+
- Added reciprocal square root `rsqrt()` function
67

78
- 4.0.0
89
- Data are now backed by a contiguous C buffer instead of PHP array

‎docs/Matrix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ See [Unary](interfaces/unary.md). Each method returns a new `Matrix`.
419419
- `abs() : self` — absolute value of each element
420420
- `square() : self` — square of the matrix element-wise
421421
- `sqrt() : self` — square root of the matrix
422+
- `rsqrt() : self` — reciprocal square root of the matrix
422423
- `reciprocal() : self` — element-wise reciprocal of the matrix
423424
- `exp() : self` — exponential of the matrix
424425
- `expm1() : self` — exponential of each element minus 1

‎docs/Vector.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ See [Unary](interfaces/unary.md). Each method returns a new `Vector`.
302302
- `abs() : self` — absolute value of the vector
303303
- `square() : self` — square the vector
304304
- `sqrt() : self` — square root of the vector
305+
- `rsqrt() : self` — reciprocal square root of the vector
305306
- `reciprocal() : self` — element-wise reciprocal of the vector
306307
- `exp() : self` — exponentiate each element
307308
- `expm1() : self` — exponential of each element minus 1

‎docs/interfaces/tensor.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface Tensor extends ArrayLike, Arithmetic, Comparable,
2020
| [ArrayLike](arraylike.md) | `shape`, `shapeString`, `size`, `map`, `reduce`, `asArray` |
2121
| [Arithmetic](arithmetic.md) | `multiply`, `divide`, `add`, `subtract`, `pow`, `mod` |
2222
| [Comparable](comparable.md) | `equal`, `notEqual`, `greater`, `greaterEqual`, `less`, `lessEqual` |
23-
| [Unary](unary.md) | `abs`, `square`, `sqrt`, `exp`, `expm1`, `log`, `log1p`, `sigmoid`, `softplus`, `round`, `floor`, `ceil`, `sign`, `negate`, `clip`, `clipLower`, `clipUpper` |
23+
| [Unary](unary.md) | `abs`, `square`, `sqrt`, `rsqrt`, `exp`, `expm1`, `log`, `log1p`, `sigmoid`, `softplus`, `round`, `floor`, `ceil`, `sign`, `negate`, `clip`, `clipLower`, `clipUpper` |
2424
| [Trigonometric](trigonometric.md) | `sin`, `asin`, `cos`, `acos`, `tan`, `atan`, `tanh`, `sinh`, `cosh`, `rad2deg`, `deg2rad` |
2525
| [Reductions](reductions.md) | `sum`, `product`, `min`, `max`, `argmin`, `argmax`, `mean`, `variance`, `median`, `quantile` |
2626
| [Special](special.md) | `softmax`, `erf` |

‎docs/interfaces/unary.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Square the tensor.
2626

2727
Return the square root of the tensor.
2828

29+
### `rsqrt() : mixed`
30+
31+
Return the reciprocal square root of the tensor.
32+
2933
### `reciprocal() : mixed`
3034

3135
Return the element-wise reciprocal of the tensor.

‎ext/include/unary.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686

8787
TENSOR_UNARY_DISPATCH(abs, fabs(va[i]))
8888
TENSOR_UNARY_DISPATCH(sqrt, sqrt(va[i]))
89+
TENSOR_UNARY_DISPATCH(rsqrt, 1.0 / sqrt(va[i]))
8990
TENSOR_UNARY_DISPATCH(floor, floor(va[i]))
9091
TENSOR_UNARY_DISPATCH(ceil, ceil(va[i]))
9192
TENSOR_UNARY_DISPATCH(negate, -va[i])
@@ -466,6 +467,7 @@ void tensor_unary_dispatch_sse_init(void)
466467
{
467468
tensor_abs_route = tensor_abs_sse;
468469
tensor_sqrt_route = tensor_sqrt_sse;
470+
tensor_rsqrt_route = tensor_rsqrt_sse;
469471
tensor_floor_route = tensor_floor_sse;
470472
tensor_ceil_route = tensor_ceil_sse;
471473
tensor_negate_route = tensor_negate_sse;
@@ -484,6 +486,7 @@ void tensor_unary_dispatch_avx_init(void)
484486
{
485487
tensor_abs_route = tensor_abs_avx;
486488
tensor_sqrt_route = tensor_sqrt_avx;
489+
tensor_rsqrt_route = tensor_rsqrt_avx;
487490
tensor_floor_route = tensor_floor_avx;
488491
tensor_ceil_route = tensor_ceil_avx;
489492
tensor_negate_route = tensor_negate_avx;
@@ -502,6 +505,7 @@ void tensor_unary_dispatch_avx512_init(void)
502505
{
503506
tensor_abs_route = tensor_abs_avx512;
504507
tensor_sqrt_route = tensor_sqrt_avx512;
508+
tensor_rsqrt_route = tensor_rsqrt_avx512;
505509
tensor_floor_route = tensor_floor_avx512;
506510
tensor_ceil_route = tensor_ceil_avx512;
507511
tensor_negate_route = tensor_negate_avx512;

‎ext/include/unary.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
void tensor_abs(zval * return_value, zval * a);
77
void tensor_sqrt(zval * return_value, zval * a);
8+
void tensor_rsqrt(zval * return_value, zval * a);
89
void tensor_exp(zval * return_value, zval * a);
910
void tensor_expm1(zval * return_value, zval * a);
1011
void tensor_log(zval * return_value, zval * a);

0 commit comments

Comments
 (0)