numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
#!/usr/bin/env python3
"""
NumPy Reference Tests for NumRS2 Array Operations

This script generates reference values from NumPy array operations
to compare against NumRS2 implementations. The output is used by Rust
reference tests to validate array operation implementations.

The test strategy:
1. Create various array configurations using fixed seeds
2. Apply NumPy operations and record results
3. Save these reference values to a JSON file for Rust tests to use
"""

import numpy as np
import json
import os
from typing import Dict, Any, List, Union

# Set a fixed seed for reproducibility
SEED = 42
np.random.seed(SEED)

# Directory to save reference data
REFERENCE_DIR = os.path.dirname(os.path.abspath(__file__))
REFERENCE_FILE = os.path.join(REFERENCE_DIR, "array_operations_reference_data.json")

# Dictionary to store reference data
reference_data: Dict[str, Any] = {}

def serialize_array(arr: np.ndarray) -> Dict[str, Any]:
    """Convert NumPy array to serializable format"""
    return {
        "data": arr.flatten().tolist(),
        "shape": list(arr.shape),
        "dtype": str(arr.dtype)
    }

def create_test_arrays() -> Dict[str, np.ndarray]:
    """Create standard test arrays used across multiple tests"""
    return {
        "small_1d": np.array([1, 2, 3, 4, 5], dtype=np.float64),
        "medium_1d": np.arange(10, dtype=np.float64),
        "small_2d": np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64),
        "square_2d": np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.float64),
        "rect_2d": np.arange(12, dtype=np.float64).reshape(3, 4),
        "small_3d": np.arange(24, dtype=np.float64).reshape(2, 3, 4),
        "zeros_2d": np.zeros((3, 3), dtype=np.float64),
        "ones_2d": np.ones((2, 4), dtype=np.float64),
        "random_2d": np.random.rand(4, 4),
        "negative_vals": np.array([-2, -1, 0, 1, 2], dtype=np.float64),
        "mixed_vals": np.array([1.5, -2.3, 0, 4.7, -1.1], dtype=np.float64)
    }

print("Generating reference data for array creation operations...")

# Array creation operations
creation_tests = {}
shapes_to_test = [(5,), (3, 4), (2, 3, 4), (1, 10), (0,), (0, 5)]

for i, shape in enumerate(shapes_to_test):
    # zeros
    zeros_arr = np.zeros(shape, dtype=np.float64)
    creation_tests[f"zeros_shape_{i}"] = {
        "operation": "zeros",
        "shape": list(shape),
        "result": serialize_array(zeros_arr)
    }
    
    # ones
    ones_arr = np.ones(shape, dtype=np.float64)
    creation_tests[f"ones_shape_{i}"] = {
        "operation": "ones", 
        "shape": list(shape),
        "result": serialize_array(ones_arr)
    }
    
    # full
    if np.prod(shape) > 0:  # Skip empty arrays for full
        full_arr = np.full(shape, 3.14, dtype=np.float64)
        creation_tests[f"full_shape_{i}"] = {
            "operation": "full",
            "shape": list(shape),
            "fill_value": 3.14,
            "result": serialize_array(full_arr)
        }

# arange tests
arange_tests = [
    {"start": 0, "stop": 10, "step": 1},
    {"start": 5, "stop": 0, "step": -1},
    {"start": 0, "stop": 5, "step": 0.5},
    {"start": -5, "stop": 5, "step": 2},
]

for i, params in enumerate(arange_tests):
    arr = np.arange(params["start"], params["stop"], params["step"], dtype=np.float64)
    creation_tests[f"arange_{i}"] = {
        "operation": "arange",
        "params": params,
        "result": serialize_array(arr)
    }

# linspace tests
linspace_tests = [
    {"start": 0, "stop": 10, "num": 11},
    {"start": -1, "stop": 1, "num": 5},
    {"start": 0, "stop": 1, "num": 101},
]

for i, params in enumerate(linspace_tests):
    arr = np.linspace(params["start"], params["stop"], params["num"], dtype=np.float64)
    creation_tests[f"linspace_{i}"] = {
        "operation": "linspace",
        "params": params,
        "result": serialize_array(arr)
    }

reference_data["array_creation"] = creation_tests

print("Generating reference data for array manipulation operations...")

# Array manipulation operations
test_arrays = create_test_arrays()
manipulation_tests = {}

# Reshape operations
reshape_tests = [
    {"array": "small_1d", "new_shape": [5, 1]},
    {"array": "small_1d", "new_shape": [1, 5]},
    {"array": "medium_1d", "new_shape": [2, 5]},
    {"array": "rect_2d", "new_shape": [4, 3]},
    {"array": "rect_2d", "new_shape": [2, 6]},
    {"array": "small_3d", "new_shape": [6, 4]},
    {"array": "small_3d", "new_shape": [24]},
]

for i, test in enumerate(reshape_tests):
    arr = test_arrays[test["array"]]
    reshaped = arr.reshape(test["new_shape"])
    manipulation_tests[f"reshape_{i}"] = {
        "operation": "reshape",
        "input": serialize_array(arr),
        "new_shape": test["new_shape"],
        "result": serialize_array(reshaped)
    }

# Transpose operations
transpose_arrays = ["small_2d", "square_2d", "rect_2d", "small_3d"]
for i, arr_name in enumerate(transpose_arrays):
    arr = test_arrays[arr_name]
    transposed = arr.T
    manipulation_tests[f"transpose_{i}"] = {
        "operation": "transpose",
        "input": serialize_array(arr),
        "result": serialize_array(transposed)
    }

# Flatten operations
for i, arr_name in enumerate(["small_2d", "square_2d", "small_3d"]):
    arr = test_arrays[arr_name]
    flattened = arr.flatten()
    manipulation_tests[f"flatten_{i}"] = {
        "operation": "flatten",
        "input": serialize_array(arr),
        "result": serialize_array(flattened)
    }

# Squeeze operations
squeeze_test_arrays = [
    np.array([[[1, 2, 3]]]),  # shape (1, 1, 3)
    np.array([[1], [2], [3]]),  # shape (3, 1)
    np.array([[[1]], [[2]]]),  # shape (2, 1, 1)
]

for i, arr in enumerate(squeeze_test_arrays):
    squeezed = arr.squeeze()
    manipulation_tests[f"squeeze_{i}"] = {
        "operation": "squeeze",
        "input": serialize_array(arr),
        "result": serialize_array(squeezed)
    }

reference_data["array_manipulation"] = manipulation_tests

print("Generating reference data for arithmetic operations...")

# Arithmetic operations
arithmetic_tests = {}

# Element-wise operations
test_pairs = [
    ("small_1d", "small_1d"),
    ("small_2d", "small_2d"), 
    ("square_2d", "square_2d"),
]

operations = ["add", "subtract", "multiply", "divide"]

for op in operations:
    for i, (arr1_name, arr2_name) in enumerate(test_pairs):
        arr1 = test_arrays[arr1_name]
        arr2 = test_arrays[arr2_name]
        
        if op == "add":
            result = arr1 + arr2
        elif op == "subtract":
            result = arr1 - arr2
        elif op == "multiply":
            result = arr1 * arr2
        elif op == "divide":
            # Avoid division by zero
            arr2_safe = arr2 + 1e-10
            result = arr1 / arr2_safe
        
        arithmetic_tests[f"{op}_{i}"] = {
            "operation": op,
            "input1": serialize_array(arr1),
            "input2": serialize_array(arr2 if op != "divide" else arr2_safe),
            "result": serialize_array(result)
        }

# Scalar operations
scalar_ops = [
    {"op": "add", "scalar": 5.0},
    {"op": "subtract", "scalar": 2.5},
    {"op": "multiply", "scalar": 3.0},
    {"op": "divide", "scalar": 2.0},
]

for i, test in enumerate(scalar_ops):
    arr = test_arrays["small_2d"]
    scalar = test["scalar"]
    
    if test["op"] == "add":
        result = arr + scalar
    elif test["op"] == "subtract":
        result = arr - scalar
    elif test["op"] == "multiply":
        result = arr * scalar
    elif test["op"] == "divide":
        result = arr / scalar
    
    arithmetic_tests[f"scalar_{test['op']}_{i}"] = {
        "operation": f"scalar_{test['op']}",
        "input": serialize_array(arr),
        "scalar": scalar,
        "result": serialize_array(result)
    }

reference_data["arithmetic_operations"] = arithmetic_tests

print("Generating reference data for mathematical functions...")

# Mathematical functions
math_tests = {}

test_arrays_math = {
    "positive": np.array([1, 4, 9, 16, 25], dtype=np.float64),
    "mixed": np.array([-2, -1, 0, 1, 2], dtype=np.float64),
    "angles": np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2], dtype=np.float64),
    "small_positive": np.array([0.1, 0.5, 1.0, 2.0, 5.0], dtype=np.float64),
}

# Basic math functions
math_functions = {
    "sqrt": lambda x: np.sqrt(np.abs(x)),  # Use abs to avoid complex numbers
    "exp": lambda x: np.exp(x),
    "log": lambda x: np.log(np.abs(x) + 1e-10),  # Avoid log of negative/zero
    "abs": lambda x: np.abs(x),
    "sin": lambda x: np.sin(x),
    "cos": lambda x: np.cos(x),
    "tan": lambda x: np.tan(x),
}

for func_name, func in math_functions.items():
    for arr_name, arr in test_arrays_math.items():
        try:
            result = func(arr)
            math_tests[f"{func_name}_{arr_name}"] = {
                "operation": func_name,
                "input": serialize_array(arr),
                "result": serialize_array(result)
            }
        except (ValueError, RuntimeWarning):
            # Skip problematic combinations
            continue

# Power operations
power_tests = [
    {"base": "positive", "exponent": 2.0},
    {"base": "small_positive", "exponent": 0.5},
    {"base": "positive", "exponent": -1.0},
]

for i, test in enumerate(power_tests):
    base_arr = test_arrays_math[test["base"]]
    result = np.power(base_arr, test["exponent"])
    math_tests[f"power_{i}"] = {
        "operation": "power",
        "base": serialize_array(base_arr),
        "exponent": test["exponent"],
        "result": serialize_array(result)
    }

# Rounding operations
rounding_arr = np.array([1.2, 2.7, -1.5, -2.3, 3.9], dtype=np.float64)
rounding_functions = {
    "floor": np.floor,
    "ceil": np.ceil,
    "round": np.round,
}

for func_name, func in rounding_functions.items():
    result = func(rounding_arr)
    math_tests[f"{func_name}"] = {
        "operation": func_name,
        "input": serialize_array(rounding_arr),
        "result": serialize_array(result)
    }

reference_data["mathematical_functions"] = math_tests

print("Generating reference data for statistical operations...")

# Statistical operations
stats_tests = {}

stat_arrays = {
    "simple": np.array([1, 2, 3, 4, 5], dtype=np.float64),
    "with_negative": np.array([-2, -1, 0, 1, 2], dtype=np.float64),
    "2d": np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64),
    "3x3": np.arange(9, dtype=np.float64).reshape(3, 3),
}

# Basic statistics
basic_stats = ["mean", "sum", "min", "max", "std", "var"]

for stat_name in basic_stats:
    for arr_name, arr in stat_arrays.items():
        stat_func = getattr(np, stat_name)
        
        # Overall statistic
        result_overall = stat_func(arr)
        stats_tests[f"{stat_name}_{arr_name}_overall"] = {
            "operation": stat_name,
            "input": serialize_array(arr),
            "axis": None,
            "result": float(result_overall)
        }
        
        # Axis-wise statistics for multidimensional arrays
        if arr.ndim > 1:
            for axis in range(arr.ndim):
                try:
                    result_axis = stat_func(arr, axis=axis)
                    stats_tests[f"{stat_name}_{arr_name}_axis{axis}"] = {
                        "operation": stat_name,
                        "input": serialize_array(arr),
                        "axis": axis,
                        "result": serialize_array(result_axis) if hasattr(result_axis, "shape") else result_axis.tolist()
                    }
                except:
                    continue

# Percentiles
percentile_arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=np.float64)
percentiles = [0, 25, 50, 75, 100]

for p in percentiles:
    result = np.percentile(percentile_arr, p)
    stats_tests[f"percentile_{p}"] = {
        "operation": "percentile",
        "input": serialize_array(percentile_arr),
        "percentile": p,
        "result": float(result)
    }

reference_data["statistical_operations"] = stats_tests

print("Generating reference data for comparison operations...")

# Comparison operations
comparison_tests = {}

comp_arrays = {
    "arr1": np.array([1, 3, 5, 7], dtype=np.float64),
    "arr2": np.array([2, 3, 4, 8], dtype=np.float64),
    "equal_arr": np.array([1, 3, 5, 7], dtype=np.float64),
}

comparison_ops = {
    "greater": np.greater,
    "greater_equal": np.greater_equal,
    "less": np.less,
    "less_equal": np.less_equal,
    "equal": np.equal,
    "not_equal": np.not_equal,
}

for op_name, op_func in comparison_ops.items():
    # Array vs Array
    result = op_func(comp_arrays["arr1"], comp_arrays["arr2"])
    comparison_tests[f"{op_name}_arrays"] = {
        "operation": op_name,
        "input1": serialize_array(comp_arrays["arr1"]),
        "input2": serialize_array(comp_arrays["arr2"]),
        "result": result.tolist()
    }
    
    # Array vs Scalar
    scalar = 3.0
    result_scalar = op_func(comp_arrays["arr1"], scalar)
    comparison_tests[f"{op_name}_scalar"] = {
        "operation": f"{op_name}_scalar",
        "input": serialize_array(comp_arrays["arr1"]),
        "scalar": scalar,
        "result": result_scalar.tolist()
    }

# Array equality functions
arr_equal_result = np.array_equal(comp_arrays["arr1"], comp_arrays["equal_arr"])
comparison_tests["array_equal_true"] = {
    "operation": "array_equal",
    "input1": serialize_array(comp_arrays["arr1"]),
    "input2": serialize_array(comp_arrays["equal_arr"]),
    "result": bool(arr_equal_result)
}

arr_not_equal_result = np.array_equal(comp_arrays["arr1"], comp_arrays["arr2"])
comparison_tests["array_equal_false"] = {
    "operation": "array_equal",
    "input1": serialize_array(comp_arrays["arr1"]),
    "input2": serialize_array(comp_arrays["arr2"]),
    "result": bool(arr_not_equal_result)
}

# allclose
close_arr1 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
close_arr2 = np.array([1.0000001, 2.0000002, 3.0000003], dtype=np.float64)
not_close_arr = np.array([1.01, 2.02, 3.03], dtype=np.float64)

allclose_true = np.allclose(close_arr1, close_arr2)
comparison_tests["allclose_true"] = {
    "operation": "allclose",
    "input1": serialize_array(close_arr1),
    "input2": serialize_array(close_arr2),
    "result": bool(allclose_true)
}

allclose_false = np.allclose(close_arr1, not_close_arr)
comparison_tests["allclose_false"] = {
    "operation": "allclose",
    "input1": serialize_array(close_arr1),
    "input2": serialize_array(not_close_arr),
    "result": bool(allclose_false)
}

reference_data["comparison_operations"] = comparison_tests

print("Generating reference data for indexing and slicing...")

# Indexing and slicing operations
indexing_tests = {}

# Basic indexing
test_2d = np.arange(12, dtype=np.float64).reshape(3, 4)
test_3d = np.arange(24, dtype=np.float64).reshape(2, 3, 4)

# Single element access
indexing_tests["get_2d_00"] = {
    "operation": "get",
    "input": serialize_array(test_2d),
    "indices": [0, 0],
    "result": float(test_2d[0, 0])
}

indexing_tests["get_2d_12"] = {
    "operation": "get",
    "input": serialize_array(test_2d),
    "indices": [1, 2],
    "result": float(test_2d[1, 2])
}

indexing_tests["get_3d_123"] = {
    "operation": "get",
    "input": serialize_array(test_3d),
    "indices": [1, 2, 3],
    "result": float(test_3d[1, 2, 3])
}

# Row/column slicing
row_slice = test_2d[0, :]
indexing_tests["slice_row_0"] = {
    "operation": "slice_row",
    "input": serialize_array(test_2d),
    "row": 0,
    "result": serialize_array(row_slice)
}

col_slice = test_2d[:, 1]
indexing_tests["slice_col_1"] = {
    "operation": "slice_col",
    "input": serialize_array(test_2d),
    "col": 1,
    "result": serialize_array(col_slice)
}

reference_data["indexing_operations"] = indexing_tests

print("Saving reference data...")

# Save reference data to JSON file
with open(REFERENCE_FILE, 'w') as f:
    json.dump(reference_data, f, indent=2)

print(f"Reference data saved to {REFERENCE_FILE}")
print("Total test categories:", len(reference_data))
for category, tests in reference_data.items():
    print(f"  {category}: {len(tests)} tests")
print("This data can be used by Rust tests to validate array operation implementations.")