-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.php
More file actions
2264 lines (2064 loc) · 68 KB
/
Copy pathFunctions.php
File metadata and controls
2264 lines (2064 loc) · 68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray {
use FFI\CData;
use PhpMlKit\NDArray\Exceptions\ShapeException;
/**
* Functional proxies for {@see NDArray} instance and static APIs, grouped by trait.
*/
// =============================================================================
// CreatesArrays — factories, buffers, random, sequences, copy / astype
// =============================================================================
/**
* Create array from PHP array.
*
* @param array<mixed> $data Nested PHP array
* @param null|array<int> $shape Optional shape. If null, inferred from data.
* @param null|DType $dtype Data type (auto-inferred if null)
*/
function nd_array(array $data, ?array $shape = null, ?DType $dtype = null): NDArray
{
return NDArray::fromArray($data, $shape, $dtype);
}
/**
* Create an array filled with zeros.
*
* @param array<int> $shape Array shape
* @param DType $dtype Data type (default: Float64)
*/
function zeros(array $shape, DType $dtype = DType::Float64): NDArray
{
return NDArray::zeros($shape, $dtype);
}
/**
* Create an array filled with ones.
*
* @param array<int> $shape Array shape
* @param DType $dtype Data type (default: Float64)
*/
function ones(array $shape, DType $dtype = DType::Float64): NDArray
{
return NDArray::ones($shape, $dtype);
}
/**
* Create an array filled with a specific value.
*
* @param bool|Complex|float|int $value Value to fill array with
* @param array<int> $shape Array shape
* @param null|DType $dtype Data type (default: inferred from value)
*/
function full(bool|Complex|float|int $value, array $shape, ?DType $dtype = null): NDArray
{
return NDArray::full($value, $shape, $dtype);
}
/**
* Create a 0-dimensional (scalar) array from a single PHP value.
*
* @see NDArray::fromScalar()
*/
function from_scalar(bool|Complex|float|int $value, ?DType $dtype = null): NDArray
{
return NDArray::fromScalar($value, $dtype);
}
/**
* Create an array from an external C buffer pointer.
*
* This method copies data from an external FFI buffer into a new NDArray. The source buffer
* remains owned by the caller - this method creates an independent copy.
*
* @param CData $buffer Pointer to raw C data (e.g., float*, int16_t*)
* @param array<int> $shape Array shape
* @param DType $dtype Data type of the buffer
*
* @throws ShapeException If buffer size doesn't match shape
*/
function from_buffer(CData $buffer, array $shape, DType $dtype): NDArray
{
return NDArray::fromBuffer($buffer, $shape, $dtype);
}
/**
* Create an array from a binary string.
*
* This method interprets a PHP binary string as raw array data in little-endian format.
* The bytes are copied into a new NDArray with the specified shape and dtype.
*
* @param string $bytes Binary string containing raw array data (little-endian)
* @param array<int> $shape Array shape
* @param DType $dtype Data type of the buffer
*
* @throws ShapeException If buffer size doesn't match shape
*/
function from_bytes(string $bytes, array $shape, DType $dtype): NDArray
{
return NDArray::fromBytes($bytes, $shape, $dtype);
}
/**
* Create an array of zeros with the same shape as the input array.
*
* @param NDArray $array Input array defining the output shape
* @param null|DType $dtype Data type (default: same as input array)
*/
function zeros_like(NDArray $array, ?DType $dtype = null): NDArray
{
return NDArray::zerosLike($array, $dtype);
}
/**
* Create an array of ones with the same shape as the input array.
*
* @param NDArray $array Input array defining the output shape
* @param null|DType $dtype Data type (default: same as input array)
*/
function ones_like(NDArray $array, ?DType $dtype = null): NDArray
{
return NDArray::onesLike($array, $dtype);
}
/**
* Create an array filled with a specific value, with the same shape as the input array.
*
* @param NDArray $array Input array defining the output shape
* @param bool|Complex|float|int $value Value to fill array with
* @param null|DType $dtype Data type (default: inferred from value or same as input)
*/
function full_like(NDArray $array, bool|Complex|float|int $value, ?DType $dtype = null): NDArray
{
return NDArray::fullLike($array, $value, $dtype);
}
/**
* Create coordinate matrices from one-dimensional coordinate vectors.
*
* @param array<array<mixed>|NDArray> $arrays One-dimensional coordinate vectors
* @param string $indexing Cartesian ('xy') or matrix ('ij') indexing
* @param bool $sparse Whether to return sparse coordinate grids
*
* @return array<NDArray> Coordinate grids, one for each input vector
*/
function meshgrid(array $arrays, string $indexing = 'xy', bool $sparse = false): array
{
return NDArray::meshgrid($arrays, $indexing, $sparse);
}
/**
* Create a 2D identity matrix.
*
* @param int $N Number of rows
* @param null|int $M Number of columns (default: N)
* @param int $k Diagonal index (0: main, >0: upper, <0: lower)
* @param DType $dtype Data type (default: Float64)
*/
function eye(int $N, ?int $M = null, int $k = 0, DType $dtype = DType::Float64): NDArray
{
return NDArray::eye($N, $M, $k, $dtype);
}
/**
* Create evenly spaced values within a given interval.
*
* @param float|int $start Start of interval (inclusive)
* @param null|float|int $stop End of interval (exclusive)
* @param float|int $step Spacing between values
* @param null|DType $dtype Data type
*/
function arange(float|int $start, float|int|null $stop = null, float|int $step = 1, ?DType $dtype = null): NDArray
{
return NDArray::arange($start, $stop, $step, $dtype);
}
/**
* Create evenly spaced numbers over a specified interval.
*
* @param float $start The starting value of the sequence
* @param float $stop The end value of the sequence
* @param int $num Number of samples to generate
* @param bool $endpoint If true, stop is the last sample
* @param DType $dtype Data type (default: Float64)
*/
function linspace(
float $start,
float $stop,
int $num = 50,
bool $endpoint = true,
DType $dtype = DType::Float64,
): NDArray {
return NDArray::linspace($start, $stop, $num, $endpoint, $dtype);
}
/**
* Create numbers spaced evenly on a log scale.
*
* @param float $start The starting exponent (base**start is the first value)
* @param float $stop The end exponent (base**stop is the final value)
* @param int $num Number of samples to generate
* @param float $base The base of the log space
* @param DType $dtype Data type (default: Float64)
*/
function logspace(
float $start,
float $stop,
int $num = 50,
float $base = 10.0,
DType $dtype = DType::Float64,
): NDArray {
return NDArray::logspace($start, $stop, $num, $base, $dtype);
}
/**
* Create numbers spaced geometrically from start to stop.
*
* @param float $start The starting value of the sequence
* @param float $stop The end value of the sequence
* @param int $num Number of samples to generate
* @param DType $dtype Data type (default: Float64)
*/
function geomspace(
float $start,
float $stop,
int $num = 50,
DType $dtype = DType::Float64,
): NDArray {
return NDArray::geomspace($start, $stop, $num, $dtype);
}
/**
* Create random samples from a uniform distribution over [0, 1).
*
* @param array<int> $shape Output shape
* @param null|DType $dtype Float dtype (default: Float64)
* @param null|int $seed Optional seed for deterministic output
*/
function random(array $shape, ?DType $dtype = null, ?int $seed = null): NDArray
{
return NDArray::random($shape, $dtype, $seed);
}
/**
* Create random integer samples from [low, high).
*
* @param int $low Inclusive lower bound
* @param int $high Exclusive upper bound
* @param array<int> $shape Output shape
* @param null|DType $dtype Integer dtype (default: Int64)
* @param null|int $seed Optional seed for deterministic output
*/
function random_int(int $low, int $high, array $shape, ?DType $dtype = null, ?int $seed = null): NDArray
{
return NDArray::randomInt($low, $high, $shape, $dtype, $seed);
}
/**
* Create random samples from a standard normal distribution N(0, 1).
*
* @param array<int> $shape Output shape
* @param null|DType $dtype Float dtype (default: Float64)
* @param null|int $seed Optional seed for deterministic output
*/
function randn(array $shape, ?DType $dtype = null, ?int $seed = null): NDArray
{
return NDArray::randn($shape, $dtype, $seed);
}
/**
* Create random samples from a normal distribution N(mean, std).
*
* @param float $mean Mean of the distribution
* @param float $std Standard deviation (must be > 0)
* @param array<int> $shape Output shape
* @param null|DType $dtype Float dtype (default: Float64)
* @param null|int $seed Optional seed for deterministic output
*/
function normal(float $mean, float $std, array $shape, ?DType $dtype = null, ?int $seed = null): NDArray
{
return NDArray::normal($mean, $std, $shape, $dtype, $seed);
}
/**
* Create random samples from a uniform distribution over [low, high).
*
* @param float $low Inclusive lower bound
* @param float $high Exclusive upper bound
* @param array<int> $shape Output shape
* @param null|DType $dtype Float dtype (default: Float64)
* @param null|int $seed Optional seed for deterministic output
*/
function uniform(float $low, float $high, array $shape, ?DType $dtype = null, ?int $seed = null): NDArray
{
return NDArray::uniform($low, $high, $shape, $dtype, $seed);
}
/**
* Tile an array by repeating it along each axis.
*
* Static convenience method that accepts either a PHP array or NDArray.
*
* @param array<int>|NDArray $a Input array or NDArray
* @param array<int>|int|NDArray $reps The number of repetitions of A along each axis
*
* @return NDArray The tiled output array
*/
function tile_array(array|NDArray $a, array|int|NDArray $reps): NDArray
{
return NDArray::tileArray($a, $reps);
}
/**
* Repeat elements of an array.
*
* Static convenience method that accepts either a PHP array or NDArray.
*
* @param array<mixed>|NDArray $a Input array or NDArray
* @param array<int>|int|NDArray $repeats The number of repetitions for each element
* @param null|int $axis The axis along which to repeat values. By default, use the flattened input array
*
* @return NDArray Output array which has the same shape as a, except along the given axis
*/
function repeat_array(array|NDArray $a, array|int|NDArray $repeats, ?int $axis = null): NDArray
{
return NDArray::repeatArray($a, $repeats, $axis);
}
/**
* Create a deep copy of the array (or view).
*
* The returned array is always C-contiguous and owns its data.
*/
function copy(NDArray $a): NDArray
{
return $a->copy();
}
/**
* Cast array to a different data type.
*
* Returns a new array with the specified dtype. If the target dtype
* is the same as the current dtype, this is equivalent to copy().
*
* @param DType $dtype Target data type
*
* @return NDArray New array with converted data
*/
function astype(NDArray $a, DType $dtype): NDArray
{
return $a->astype($dtype);
}
/**
* Cast an array to a different dtype, returning the same instance when possible.
*
* Unlike {@see astype()} which always copies, this function returns the
* input array unchanged (zero-cost) when the dtype already matches.
*
* @see NDArray::cast()
*/
function cast(NDArray $a, DType $dtype): NDArray
{
return $a->cast($dtype);
}
// =============================================================================
// HasMath — element-wise arithmetic, ufuncs, bitwise, clamp, min/max, softmax
// =============================================================================
/**
* Add another array or scalar to this array.
*
* @param Complex|float|int|NDArray $other Array or scalar to add
*
* @return NDArray New array with result
*/
function add(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->add($other);
}
/**
* Subtract another array or scalar from this array.
*
* @param Complex|float|int|NDArray $other Array or scalar to subtract
*
* @return NDArray New array with result
*/
function subtract(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->subtract($other);
}
/**
* Multiply this array by another array or scalar.
*
* @param Complex|float|int|NDArray $other Array or scalar to multiply by
*
* @return NDArray New array with result
*/
function multiply(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->multiply($other);
}
/**
* Divide this array by another array or scalar.
*
* @param Complex|float|int|NDArray $other Array or scalar to divide by
*
* @return NDArray New array with result
*/
function divide(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->divide($other);
}
/**
* Compute remainder (modulo) with another array or scalar.
*
* @param Complex|float|int|NDArray $other Array or scalar
*
* @return NDArray New array with result
*/
function rem(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->rem($other);
}
/**
* Compute modulo with another array or scalar.
*
* Alias for rem().
*
* @param Complex|float|int|NDArray $other Array or scalar
*
* @return NDArray New array with result
*/
function mod(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->mod($other);
}
/**
* Compute absolute value element-wise.
*/
function abs(NDArray $a): NDArray
{
return $a->abs();
}
/**
* Compute negation element-wise (-$a).
* Not supported for unsigned integers or bool.
*/
function negative(NDArray $a): NDArray
{
return $a->negative();
}
/**
* Extract real part element-wise.
*
* For complex arrays, returns the real component as float.
* For real arrays, returns a copy with the same dtype.
*/
function real(NDArray $a): NDArray
{
return $a->real();
}
/**
* Extract imaginary part element-wise.
*
* For complex arrays, returns the imaginary component as float.
* For real arrays, returns zeros with the same dtype.
*/
function imag(NDArray $a): NDArray
{
return $a->imag();
}
/**
* Compute complex conjugate element-wise.
*
* For complex arrays, negates the imaginary part.
* For real arrays, returns a copy with the same dtype.
*/
function conjugate(NDArray $a): NDArray
{
return $a->conjugate();
}
/**
* Alias for conjugate().
*/
function conj(NDArray $a): NDArray
{
return $a->conj();
}
/**
* Returns bool array: true where element has non-zero imaginary part.
*
* For complex arrays, checks if imag ≠ 0.
* For real arrays, always returns false.
*/
function iscomplex(NDArray $a): NDArray
{
return $a->iscomplex();
}
/**
* Returns bool array: true where element has zero imaginary part.
*
* For complex arrays, checks if imag = 0.
* For real arrays, always returns true.
*/
function isreal(NDArray $a): NDArray
{
return $a->isreal();
}
/**
* Compute phase angle element-wise.
*
* Returns the counterclockwise angle from the positive real axis.
* Always returns Float64 regardless of input dtype.
*
* @param bool $deg if true, returns angle in degrees; otherwise radians
*/
function angle(NDArray $a, bool $deg = false): NDArray
{
return $a->angle($deg);
}
/**
* Compute square root element-wise.
*/
function sqrt(NDArray $a): NDArray
{
return $a->sqrt();
}
/**
* Compute exponential element-wise.
*/
function exp(NDArray $a): NDArray
{
return $a->exp();
}
/**
* Compute natural logarithm element-wise.
*/
function log(NDArray $a): NDArray
{
return $a->log();
}
/**
* Compute natural logarithm element-wise.
*
* Alias for log().
*/
function ln(NDArray $a): NDArray
{
return $a->ln();
}
/**
* Compute sine element-wise.
*/
function sin(NDArray $a): NDArray
{
return $a->sin();
}
/**
* Compute cosine element-wise.
*/
function cos(NDArray $a): NDArray
{
return $a->cos();
}
/**
* Compute tangent element-wise.
*/
function tan(NDArray $a): NDArray
{
return $a->tan();
}
/**
* Compute hyperbolic sine element-wise.
*/
function sinh(NDArray $a): NDArray
{
return $a->sinh();
}
/**
* Compute hyperbolic cosine element-wise.
*/
function cosh(NDArray $a): NDArray
{
return $a->cosh();
}
/**
* Compute hyperbolic tangent element-wise.
*/
function tanh(NDArray $a): NDArray
{
return $a->tanh();
}
/**
* Compute arc sine element-wise.
*/
function asin(NDArray $a): NDArray
{
return $a->asin();
}
/**
* Compute arc cosine element-wise.
*/
function acos(NDArray $a): NDArray
{
return $a->acos();
}
/**
* Compute arc tangent element-wise.
*/
function atan(NDArray $a): NDArray
{
return $a->atan();
}
/**
* Compute cube root element-wise.
*/
function cbrt(NDArray $a): NDArray
{
return $a->cbrt();
}
/**
* Compute ceiling element-wise.
*/
function ceil(NDArray $a): NDArray
{
return $a->ceil();
}
/**
* Compute base-2 exponential (2^x) element-wise.
*/
function exp2(NDArray $a): NDArray
{
return $a->exp2();
}
/**
* Compute floor element-wise.
*/
function floor(NDArray $a): NDArray
{
return $a->floor();
}
/**
* Compute base-2 logarithm element-wise.
*/
function log2(NDArray $a): NDArray
{
return $a->log2();
}
/**
* Compute base-10 logarithm element-wise.
*/
function log10(NDArray $a): NDArray
{
return $a->log10();
}
/**
* Compute x^2 (square) element-wise.
*/
function pow2(NDArray $a): NDArray
{
return $a->pow2();
}
/**
* Compute round element-wise.
*/
function round(NDArray $a): NDArray
{
return $a->round();
}
/**
* Compute signum element-wise.
*/
function signum(NDArray $a): NDArray
{
return $a->signum();
}
/**
* Compute reciprocal (1/x) element-wise.
*/
function recip(NDArray $a): NDArray
{
return $a->recip();
}
/**
* Compute ln(1+x) element-wise.
*
* More accurate than log(1+x) for small x.
*/
function ln1p(NDArray $a): NDArray
{
return $a->ln1p();
}
/**
* Convert radians to degrees element-wise.
*/
function to_degrees(NDArray $a): NDArray
{
return $a->toDegrees();
}
/**
* Convert degrees to radians element-wise.
*/
function to_radians(NDArray $a): NDArray
{
return $a->toRadians();
}
/**
* Compute x^n where n is an integer, element-wise.
*
* Generally faster than pow() for integer exponents.
*
* @param int $exp Integer exponent
*/
function powi(NDArray $a, int $exp): NDArray
{
return $a->powi($exp);
}
/**
* Compute x^y where y is a float, element-wise.
*
* @param float $exp Float exponent
*/
function powf(NDArray $a, float $exp): NDArray
{
return $a->powf($exp);
}
/**
* Compute hypotenuse element-wise.
*
* @param float $other Scalar value
*/
function hypot(NDArray $a, float $other): NDArray
{
return $a->hypot($other);
}
/**
* Bitwise AND with another array or scalar.
*
* @param int|NDArray $other Array or scalar
*
* @return NDArray New array with result
*/
function bitand(NDArray $a, int|NDArray $other): NDArray
{
return $a->bitand($other);
}
/**
* Bitwise OR with another array or scalar.
*
* @param int|NDArray $other Array or scalar
*
* @return NDArray New array with result
*/
function bitor(NDArray $a, int|NDArray $other): NDArray
{
return $a->bitor($other);
}
/**
* Bitwise XOR with another array or scalar.
*
* @param int|NDArray $other Array or scalar
*
* @return NDArray New array with result
*/
function bitxor(NDArray $a, int|NDArray $other): NDArray
{
return $a->bitxor($other);
}
/**
* Left shift by another array or scalar.
*
* @param int|NDArray $other Array or scalar (number of bits)
*
* @return NDArray New array with result
*/
function left_shift(NDArray $a, int|NDArray $other): NDArray
{
return $a->leftShift($other);
}
/**
* Right shift by another array or scalar.
*
* @param int|NDArray $other Array or scalar (number of bits)
*
* @return NDArray New array with result
*/
function right_shift(NDArray $a, int|NDArray $other): NDArray
{
return $a->rightShift($other);
}
/**
* Clamp (clip) array values to a specified range.
*
* Similar to NumPy's clip function. Values outside [min, max] are set
* to the nearest boundary.
*
* @param float $min Minimum value
* @param float $max Maximum value
*
* @throws \InvalidArgumentException If min > max
*/
function clamp(NDArray $a, float|int $min, float|int $max): NDArray
{
return $a->clamp($min, $max);
}
/**
* Clip array values to a specified range.
*
* Alias for clamp().
*
* @param float $min Minimum value
* @param float $max Maximum value
*
* @throws \InvalidArgumentException If min > max
*/
function clip(NDArray $a, float|int $min, float|int $max): NDArray
{
return $a->clip($min, $max);
}
/**
* Element-wise minimum of two arrays.
*
* Compares two arrays element-wise and returns a new array containing
* the smaller value at each position. Supports broadcasting.
*
* @param NDArray $other The array to compare with
*
* @return NDArray New array with element-wise minimum values
*/
function minimum(NDArray $a, NDArray $other): NDArray
{
return $a->minimum($other);
}
/**
* Element-wise maximum of two arrays.
*
* Compares two arrays element-wise and returns a new array containing
* the larger value at each position. Supports broadcasting.
*
* @param NDArray $other The array to compare with
*
* @return NDArray New array with element-wise maximum values
*/
function maximum(NDArray $a, NDArray $other): NDArray
{
return $a->maximum($other);
}
/**
* Compute sigmoid element-wise: 1 / (1 + exp(-x)).
*/
function sigmoid(NDArray $a): NDArray
{
return $a->sigmoid();
}
/**
* Compute softmax along axis: exp(x - max) / sum(exp(x - max)).
*
* Numerically stable. Default axis -1 (last axis) for typical logits.
*
* @param int $axis Axis along which to compute softmax
*/
function softmax(NDArray $a, int $axis = -1): NDArray
{
return $a->softmax($axis);
}
// =============================================================================
// HasComparison — element-wise comparisons (Bool result)
// =============================================================================
/**
* Element-wise equal comparison. Returns Bool array.
*/
function eq(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->eq($other);
}
/**
* Element-wise not-equal comparison. Returns Bool array.
*/
function ne(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->ne($other);
}
/**
* Element-wise greater-than comparison. Returns Bool array.
*/
function gt(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->gt($other);
}
/**
* Element-wise greater-or-equal comparison. Returns Bool array.
*/
function gte(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->gte($other);
}
/**
* Element-wise less-than comparison. Returns Bool array.
*/
function lt(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->lt($other);
}
/**
* Element-wise less-or-equal comparison. Returns Bool array.
*/
function lte(NDArray $a, Complex|float|int|NDArray $other): NDArray
{
return $a->lte($other);
}
// =============================================================================
// HasLogical — element-wise logical (Bool result).
// =============================================================================
/**
* Element-wise logical AND. Returns Bool array.
*
* Works with any input type (converts to bool first).
* Zero values are treated as false, non-zero as true.
*
* @see NDArray::and()
*/
function logical_and(NDArray $a, NDArray $other): NDArray
{
return $a->and($other);
}
/**
* Element-wise logical OR. Returns Bool array.
*
* Works with any input type (converts to bool first).
* Zero values are treated as false, non-zero as true.