-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDArrayTest.php
More file actions
430 lines (339 loc) · 12.3 KB
/
Copy pathNDArrayTest.php
File metadata and controls
430 lines (339 loc) · 12.3 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
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray\Tests\Unit;
use PhpMlKit\NDArray\DType;
use PhpMlKit\NDArray\Exceptions\IndexException;
use PhpMlKit\NDArray\Exceptions\ShapeException;
use PhpMlKit\NDArray\NDArray;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Tests for NDArray::array() factory method.
*
* @internal
*
* @coversNothing
*/
final class NDArrayTest extends TestCase
{
// =========================================================================
// Basic Creation Tests
// =========================================================================
public function testCreateFrom1DArray(): void
{
$arr = NDArray::array([1.0, 2.0, 3.0, 4.0]);
$this->assertSame([4], $arr->shape());
$this->assertSame(1, $arr->ndim());
$this->assertSame(4, $arr->size());
$this->assertSame(DType::Float64, $arr->dtype());
}
public function testCreateFrom2DArray(): void
{
$arr = NDArray::array([
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
]);
$this->assertSame([2, 3], $arr->shape());
$this->assertSame(2, $arr->ndim());
$this->assertSame(6, $arr->size());
}
public function testCreateFrom3DArray(): void
{
$arr = NDArray::array([
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
]);
$this->assertSame([2, 2, 2], $arr->shape());
$this->assertSame(3, $arr->ndim());
$this->assertSame(8, $arr->size());
}
// =========================================================================
// DType Inference Tests
// =========================================================================
public function testInferFloat64FromFloatArray(): void
{
$arr = NDArray::array([1.5, 2.5, 3.5]);
$this->assertSame(DType::Float64, $arr->dtype());
}
public function testInferInt64FromIntArray(): void
{
$arr = NDArray::array([1, 2, 3]);
$this->assertSame(DType::Int64, $arr->dtype());
}
public function testInferBoolFromBoolArray(): void
{
$arr = NDArray::array([true, false, true]);
$this->assertSame(DType::Bool, $arr->dtype());
}
public function testInferFloat64FromMixedIntFloat(): void
{
$arr = NDArray::array([1, 2.5, 3]);
$this->assertSame(DType::Float64, $arr->dtype());
}
// =========================================================================
// Explicit DType Tests
// =========================================================================
/**
* @param array<bool|float|int> $data
*/
#[DataProvider('dtypeProvider')]
public function testCreateWithExplicitDType(DType $dtype, array $data): void
{
$arr = NDArray::array($data, $dtype);
$this->assertSame($dtype, $arr->dtype());
$this->assertSame($dtype->itemSize(), $arr->itemsize());
}
/**
* @return array<string, array{DType, array<bool|float|int>}>
*/
public static function dtypeProvider(): array
{
return [
'Float64' => [DType::Float64, [1.0, 2.0, 3.0]],
'Float32' => [DType::Float32, [1.0, 2.0, 3.0]],
'Int64' => [DType::Int64, [1, 2, 3]],
'Int32' => [DType::Int32, [1, 2, 3]],
'Int16' => [DType::Int16, [1, 2, 3]],
'Int8' => [DType::Int8, [1, 2, 3]],
'Uint64' => [DType::UInt64, [1, 2, 3]],
'Uint32' => [DType::UInt32, [1, 2, 3]],
'Uint16' => [DType::UInt16, [1, 2, 3]],
'Uint8' => [DType::UInt8, [1, 2, 3]],
'Bool' => [DType::Bool, [true, false, true]],
];
}
// =========================================================================
// Properties Tests
// =========================================================================
public function testItemsize(): void
{
$f64 = NDArray::array([1.0], DType::Float64);
$f32 = NDArray::array([1.0], DType::Float32);
$i64 = NDArray::array([1], DType::Int64);
$i32 = NDArray::array([1], DType::Int32);
$i8 = NDArray::array([1], DType::Int8);
$bool = NDArray::array([true], DType::Bool);
$this->assertSame(8, $f64->itemsize());
$this->assertSame(4, $f32->itemsize());
$this->assertSame(8, $i64->itemsize());
$this->assertSame(4, $i32->itemsize());
$this->assertSame(1, $i8->itemsize());
$this->assertSame(1, $bool->itemsize());
}
public function testNbytes(): void
{
$arr = NDArray::array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], DType::Float64);
// 6 elements * 8 bytes = 48 bytes
$this->assertSame(48, $arr->nbytes());
}
// =========================================================================
// toArray Tests
// =========================================================================
public function testToArray1D(): void
{
$data = [1.0, 2.0, 3.0, 4.0];
$arr = NDArray::array($data);
$result = $arr->toArray();
$this->assertEqualsWithDelta($data, $result, 0.0001);
}
public function testToArray2D(): void
{
$data = [
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
];
$arr = NDArray::array($data);
$result = $arr->toArray();
$this->assertEqualsWithDelta($data, $result, 0.0001);
}
public function testToArrayInt(): void
{
$data = [1, 2, 3, 4];
$arr = NDArray::array($data, DType::Int32);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testToArrayBool(): void
{
$data = [true, false, true, false];
$arr = NDArray::array($data, DType::Bool);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
// =========================================================================
// Edge Cases
// =========================================================================
public function testCreateSingleElement(): void
{
$arr = NDArray::array([42.0]);
$this->assertSame([1], $arr->shape());
$this->assertSame(1, $arr->size());
$this->assertEqualsWithDelta([42.0], $arr->toArray(), 0.0001);
}
public function testCreateLargeArray(): void
{
$data = range(1, 1000);
$arr = NDArray::array($data, DType::Int64);
$this->assertSame([1000], $arr->shape());
$this->assertSame(1000, $arr->size());
}
public function testEmptyArrayThrows(): void
{
$this->expectException(ShapeException::class);
$this->expectExceptionMessage('Cannot create array from empty data');
NDArray::array([]);
}
public function testJaggedArrayThrows(): void
{
$this->expectException(ShapeException::class);
// Rust error message should be propagated
$this->expectExceptionMessage('Data length 3 does not match shape [2, 2] (expected 4)');
// [[1, 2], [3]] -> inferred shape [2, 2], flattened length 3
NDArray::array([[1, 2], [3]]);
}
// =========================================================================
// All DType Roundtrip Tests
// =========================================================================
public function testFloat64Roundtrip(): void
{
$data = [1.5, 2.5, 3.5, 4.5];
$arr = NDArray::array($data, DType::Float64);
$result = $arr->toArray();
$this->assertEqualsWithDelta($data, $result, 0.0001);
}
public function testFloat32Roundtrip(): void
{
$data = [1.5, 2.5, 3.5, 4.5];
$arr = NDArray::array($data, DType::Float32);
$result = $arr->toArray();
$this->assertEqualsWithDelta($data, $result, 0.001);
}
public function testInt64Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::Int64);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testInt32Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::Int32);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testInt16Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::Int16);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testInt8Roundtrip(): void
{
$data = [10, 20, 30, 40];
$arr = NDArray::array($data, DType::Int8);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testUint64Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::UInt64);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testUint32Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::UInt32);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testUint16Roundtrip(): void
{
$data = [100, 200, 300, 400];
$arr = NDArray::array($data, DType::UInt16);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testUint8Roundtrip(): void
{
$data = [10, 20, 30, 40];
$arr = NDArray::array($data, DType::UInt8);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function testBoolRoundtrip(): void
{
$data = [true, false, true, false];
$arr = NDArray::array($data, DType::Bool);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
// =========================================================================
// 2D Roundtrip for Each DType
// =========================================================================
public function test2DFloat64Roundtrip(): void
{
$data = [[1.0, 2.0], [3.0, 4.0]];
$arr = NDArray::array($data, DType::Float64);
$result = $arr->toArray();
$this->assertEqualsWithDelta($data, $result, 0.0001);
}
public function test2DInt32Roundtrip(): void
{
$data = [[1, 2], [3, 4]];
$arr = NDArray::array($data, DType::Int32);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
public function test2DBoolRoundtrip(): void
{
$data = [[true, false], [false, true]];
$arr = NDArray::array($data, DType::Bool);
$result = $arr->toArray();
$this->assertSame($data, $result);
}
// =========================================================================
// shape() optional axis
// =========================================================================
public function testShapeNullEqualsOmitted(): void
{
$arr = NDArray::zeros([3, 4, 5]);
$this->assertSame([3, 4, 5], $arr->shape());
$this->assertSame([3, 4, 5], $arr->shape(null));
}
public function testShapeAxisPositiveAndNegative(): void
{
$arr = NDArray::zeros([3, 4, 5]);
$this->assertSame(3, $arr->shape(0));
$this->assertSame(4, $arr->shape(1));
$this->assertSame(5, $arr->shape(2));
$this->assertSame(5, $arr->shape(-1));
$this->assertSame(4, $arr->shape(-2));
$this->assertSame(3, $arr->shape(-3));
}
public function testShapeAxisOutOfBoundsThrows(): void
{
$arr = NDArray::zeros([2, 3]);
$this->expectException(IndexException::class);
$this->expectExceptionMessage('Shape axis 2 is out of bounds');
$arr->shape(2);
}
public function testShapeAxisNegativeOutOfBoundsThrows(): void
{
$arr = NDArray::zeros([2, 3]);
$this->expectException(IndexException::class);
$this->expectExceptionMessage('Shape axis -3 is out of bounds');
$arr->shape(-3);
}
public function testShapeAxisOnZeroDimensionalThrows(): void
{
$arr = NDArray::array([3.14], DType::Float64)->reshape([]);
$this->assertSame([], $arr->shape());
$this->expectException(IndexException::class);
$this->expectExceptionMessage('zero-dimensional');
$arr->shape(0);
}
}