CategoricalArray#
-
class numpy::CategoricalArray#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use CategoricalArray
CategoricalArray obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
df_categorical_array.h:91 |
|
|
df_categorical_array.h:106 |
|
|
df_categorical_array.h:137 |
Construction#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
static CategoricalArray |
df_categorical_array.h:176 |
|
|
static CategoricalArray |
df_categorical_array.h:199 |
Array Creation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_categorical_array.h:286 |
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::int32 |
df_categorical_array.h:351 |
|
|
size_t |
df_categorical_array.h:1612 |
|
|
CategoricalArray |
df_categorical_array.h:1051 |
Shape Manipulation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
CategoricalArray |
df_categorical_array.h:1793 |
|
|
CategoricalArray |
df_categorical_array.h:1535 |
|
|
CategoricalArray |
df_categorical_array.h:1592 |
|
|
CategoricalArray |
df_categorical_array.h:1731 |
|
|
CategoricalArray |
df_categorical_array.h:1786 |
Statistics#
Sorting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::NDArray<numpy::int64> |
df_categorical_array.h:956 |
|
|
CategoricalArray |
df_categorical_array.h:1706 |
Math Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
CategoricalArray |
df_categorical_array.h:463 |
Comparison#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_categorical_array.h:1317 |
Set Operations#
I/O#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::vector<std::optional<std::string>> |
df_categorical_array.h:1742 |
|
|
numpy::NDArray<numpy::int32> |
df_categorical_array.h:1771 |
|
|
std::string |
df_categorical_array.h:1816 |
|
|
std::vector<std::optional<std::string>> |
df_categorical_array.h:1761 |
Joining / Splitting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
CategoricalArray |
df_categorical_array.h:1181 |
|
|
CategoricalArray |
df_categorical_array.h:1203 |
|
|
CategoricalArray |
df_categorical_array.h:1336 |
|
|
CategoricalArray |
df_categorical_array.h:1544 |
|
|
CategoricalArray |
df_categorical_array.h:1563 |
Type Handling#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::vector<std::optional<T>> |
df_categorical_array.h:1109 |
|
|
numpy::NDArray<numpy::int32> |
df_categorical_array.h:1161 |
|
|
CategoricalArray |
df_categorical_array.h:1044 |
|
|
CategoricalArray |
df_categorical_array.h:1805 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_categorical_array.h:361 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::optional<size_t> |
df_categorical_array.h:1016 |
|
|
std::optional<size_t> |
df_categorical_array.h:992 |
|
|
CategoricalArray |
df_categorical_array.h:661 |
|
|
CategoricalArray |
df_categorical_array.h:668 |
|
|
std::unordered_map<std::string, numpy::int32> |
df_categorical_array.h:48 |
|
|
const std::vector<std::string>& |
df_categorical_array.h:319 |
|
|
void |
df_categorical_array.h:1170 |
|
|
df_categorical_array.h:1613 |
||
|
df_categorical_array.h:1708 |
||
|
const numpy::NDArray<numpy::int32>& |
df_categorical_array.h:300 |
|
|
numpy::NDArray<numpy::int32> |
df_categorical_array.h:188 |
|
|
std::vector<numpy::int32> |
df_categorical_array.h:307 |
|
|
static CategoricalArray |
df_categorical_array.h:209 |
|
|
size_t |
df_categorical_array.h:434 |
|
|
std::map<std::string, std::string> |
df_categorical_array.h:1222 |
|
|
CategoricalArray |
df_categorical_array.h:420 |
|
|
CategoricalDtype |
df_categorical_array.h:247 |
|
|
numpy::NDArray<numpy::bool_> |
df_categorical_array.h:1259 |
|
|
std::pair<IntegerArray<numpy::int64>, CategoricalArray> |
df_categorical_array.h:745 |
|
|
CategoricalArray |
df_categorical_array.h:397 |
|
|
bool |
df_categorical_array.h:447 |
|
|
CategoricalArray |
df_categorical_array.h:1375 |
|
|
numpy::NDArray<numpy::bool_> |
df_categorical_array.h:375 |
|
|
numpy::NDArray<numpy::bool_> |
df_categorical_array.h:1445 |
|
|
size_t |
df_categorical_array.h:293 |
|
|
CategoricalArray |
df_categorical_array.h:1454 |
|
|
CategoricalArray |
df_categorical_array.h:1495 |
|
|
size_t |
df_categorical_array.h:1508 |
|
|
size_t |
df_categorical_array.h:261 |
|
|
constexpr int |
df_categorical_array.h:272 |
|
|
numpy::NDArray<numpy::bool_> |
df_categorical_array.h:386 |
|
|
numpy::NDArray<numpy::bool_> |
df_categorical_array.h:1527 |
|
|
bool |
df_categorical_array.h:326 |
|
|
CategoricalArray |
df_categorical_array.h:480 |
|
|
CategoricalArray |
df_categorical_array.h:619 |
|
|
CategoricalArray |
df_categorical_array.h:559 |
|
|
CategoricalArray |
df_categorical_array.h:569 |
|
|
CategoricalArray |
df_categorical_array.h:583 |
|
|
std::string |
df_categorical_array.h:1842 |
|
|
CategoricalArray |
df_categorical_array.h:522 |
|
|
CategoricalArray |
df_categorical_array.h:1660 |
|
|
std::vector<size_t> |
df_categorical_array.h:279 |
|
|
CategoricalArray |
df_categorical_array.h:1670 |
|
|
size_t |
df_categorical_array.h:254 |
|
|
CategoricalArray |
df_categorical_array.h:1086 |
|
|
void |
df_categorical_array.h:59 |
|
|
std::pair<std::vector<std::string>, std::vector<numpy::int64>> |
df_categorical_array.h:775 |
Code Examples#
The following examples are extracted from the test suite.
empty (np_test_1_all.cpp:6316)
6306}
6307
6308void test_data_generator_emptyBenchmarkSorting() {
6309 std::cout << "========= test_data_generator_empty =======================";
6310
6311 DataGenerator<int> gen(42);
6312
6313 // Test empty data generation
6314 std::vector<int> data = gen.generate(0, DataPattern::RANDOM);
6315
6316 if (!(data.empty())) {
6317 std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())";
6318 std::cout << std::string("[FAIL] ") + description << std::endl;
6319 throw std::runtime_error(description);
6320 }
6321
6322 // std::cout << "Empty data generation test passed" << std::endl;
6323
6324 std::cout << " -> tests passed" << std::endl;
6325}
searchsorted (np_test_2_all.cpp:8204)
8194 // Test missing NumPy functions
8195 void test_numpy_functions() {
8196 std::cout << "========= NumPy Functions Test ==========================";
8197
8198 // Test searchsorted
8199 {
8200 std::vector<int> sorted_arr = { 1, 3, 5, 7, 9 };
8201 std::vector<int> values = { 2, 6, 8 };
8202
8203 auto result = numpy::searchsorted(sorted_arr, values, "left");
8204 std::vector<size_t> expected = { 1, 3, 4 };
8205
8206 if (result != expected) {
8207 std::cout << " searchsorted (left): -> [FAIL]";
8208 throw std::runtime_error("searchsorted (left): FAILED - incorrect result indices");
8209 }
8210 // std::cout << "[OK] searchsorted (left): PASSED" << std::endl;
8211
8212 auto result_right = numpy::searchsorted(sorted_arr, values, "right");
8213 std::vector<size_t> expected_right = { 1, 3, 4 };
take (np_test_5_all.cpp:4313)
4303 for (size_t i = 0; i < 6; ++i) {
4304 arr.setElementAt({ i }, static_cast<int32_t>(i * 10)); // [0, 10, 20, 30, 40, 50]
4305 }
4306
4307 // Take specific indices
4308 numpy::NDArray<size_t> indices({ 3 });
4309 indices.setElementAt({ 0 }, 5); // 50
4310 indices.setElementAt({ 1 }, 2); // 20
4311 indices.setElementAt({ 2 }, 0); // 0
4312
4313 auto result = numpy::take(arr, indices);
4314
4315 if (result.getSize() != 3) {
4316 std::cout << " [FAIL] : in np_test_take_basic() : Wrong result size";
4317 throw std::runtime_error("Test failed");
4318 }
4319
4320 if (result.getElementAt({ 0 }) != 50 || result.getElementAt({ 1 }) != 20 || result.getElementAt({ 2 }) != 0) {
4321 std::cout << " [FAIL] : in np_test_take_basic() : Wrong values extracted";
4322 throw std::runtime_error("Test failed");
4323 }
T (np_test_1_all.cpp:20062)
20052 std::mt19937 gen;
20053
20054 public:
20055 MatrixGenerator(unsigned seed = 12345) : gen(seed) {}
20056
20057 // Generate real matrix with known real eigenvalues
20058 NDArray<T> real_matrix_real_eigenvalues(size_t n) {
20059 // Create diagonal matrix with known eigenvalues
20060 NDArray<T> D = NDArray<T>::createZeros({ n, n });
20061 for (size_t i = 0; i < n; ++i) {
20062 D.setElementAt({ i, i }, T(i + 1)); // Eigenvalues 1, 2, 3, ...
20063 }
20064
20065 // Create random orthogonal transformation
20066 auto Q = random_orthogonal_matrix(n);
20067 auto QT = Q.transposeArray();
20068
20069 // A = Q * D * Q^T
20070 return matmul(matmul(Q, D), QT);
20071 }
ravel (np_test_1_all.cpp:4082)
4072 std::cout << "Testing ravel function...\n";
4073
4074 // Test with 2-D array
4075 auto arr_2d = createFloat64Array({ 2, 3 });
4076 for (size_t i = 0; i < 2; ++i) {
4077 for (size_t j = 0; j < 3; ++j) {
4078 arr_2d.setElementAt({ i, j }, static_cast<double>(i * 3 + j));
4079 }
4080 }
4081
4082 auto raveled = ravel(arr_2d);
4083 if (!(raveled.getShape() == std::vector<size_t>({ 6 }))) {
4084 std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(raveled.getShape() == std::vector<size_t>({ 6 }))";
4085 std::cout << std::string("[FAIL] ") + description << std::endl;
4086 throw std::runtime_error(description);
4087 }
4088
4089 for (size_t i = 0; i < 6; ++i) {
4090 if (!(isApproxEqualExt(raveled.getElementAt({ i }), static_cast<double>(i)))) {
4091 std::string description = std::string("testRavelFunction():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualExt(raveled.getElementAt({ i }), static_cast<double>(i)))";
4092 std::cout << std::string("[FAIL] ") + description << std::endl;
reshape (np_test_1_all.cpp:5391)
5381 std::cout << "========= testInvalidReshapeExtended =======================";
5382
5383 auto array = createInt32Array({2, 3}, 42); // Size = 6
5384
5385 // std::cout << "Original array (2x3, size=6):" << std::endl;
5386 //array.printArray();
5387
5388 // Valid reshape
5389 try {
5390 auto reshaped = array.reshapeArray({3, 2}); // Size = 6, OK
5391 // std::cout << "[OK] Valid reshape (3x2) works";
5392 } catch (const std::exception& e) {
5393 std::cout << " [FAIL] : in testInvalidReshapeExtended(): Unexpected error in valid reshape: " << e.what();
5394 throw std::runtime_error("testInvalidReshapeExtended(): Unexpected error in valid reshape: " + std::string(e.what()));
5395 }
5396
5397 // Invalid reshape - wrong total size
5398 try {
5399 auto reshaped = array.reshapeArray({2, 2}); // Size = 4, should fail
5400 std::cout << "[FAIL] Invalid reshape should have thrown";
5401 } catch (const std::invalid_argument& e) {
swapaxes (np_test_2_all.cpp:3707)
3697 std::cout << std::string("[FAIL] ") + description << std::endl;
3698 throw std::runtime_error(description);
3699 }
3700 if (!(isApproxEqualMCO(rolled.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))) {
3701 std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(rolled.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))";
3702 std::cout << std::string("[FAIL] ") + description << std::endl;
3703 throw std::runtime_error(description);
3704 }
3705
3706 // Test swapaxes
3707 auto swapped = swapaxes(arr, 0, 1);
3708 if (!(swapped.getShape() == std::vector<size_t>({ 4, 3 }))) {
3709 std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(swapped.getShape() == std::vector<size_t>({ 4, 3 }))";
3710 std::cout << std::string("[FAIL] ") + description << std::endl;
3711 throw std::runtime_error(description);
3712 }
3713 if (!(isApproxEqualMCO(swapped.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 0 })))) {
3714 std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(swapped.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 0 })))";
3715 std::cout << std::string("[FAIL] ") + description << std::endl;
3716 throw std::runtime_error(description);
3717 }
transpose (np_test_2_all.cpp:4973)
4963 }
4964
4965 /**
4966 * Test matrix properties and methods
4967 */
4968 void testMatrixProperties() {
4969 std::cout << "========= testMatrixProperties =======================";
4970
4971 // Test transpose
4972 numpy::Matrix<double> m("1 2 3; 4 5 6");
4973 auto mt = m.transpose();
4974 assert_test(mt.rows() == 3, "Transpose rows");
4975 assert_test(mt.cols() == 2, "Transpose cols");
4976 assert_test(std::abs(mt(0, 1) - 4.0) < 1e-10, "Transpose element");
4977 assert_test(std::abs(mt(2, 0) - 3.0) < 1e-10, "Transpose element");
4978
4979 // Test trace for square matrix
4980 numpy::Matrix<double> square("1 2; 3 4");
4981 double tr = square.trace();
4982 assert_test(std::abs(tr - 5.0) < 1e-10, "Matrix trace");
max (np_test_1_all.cpp:7274)
7264 if (sizeof(uintp) == sizeof(void*)) {
7265 // std::cout << " -> uintp size matches pointer size";
7266 } else {
7267 // std::cout << " ✗ uintp size doesn't match pointer size" << std::endl;
7268 }
7269
7270 // Test range limits
7271 // std::cout << "Range Information:" << std::endl;
7272 // std::cout << " intp min: " << std::numeric_limits<intp>::min() << std::endl;
7273 // std::cout << " intp max: " << std::numeric_limits<intp>::max() << std::endl;
7274 // std::cout << " uintp max: " << std::numeric_limits<uintp>::max() << std::endl;
7275 // std::cout << " longdouble digits: " << std::numeric_limits<longdouble>::digits << std::endl;
7276
7277 std::cout << " -> tests passed" << std::endl;
7278}
7279
7280void testComplexArithmeticExtendedTypes() {
7281 std::cout << "========= testComplexArithmeticExtendedTypes =======================";
7282
7283 clongdouble c1(3.0L, 4.0L); // 3 + 4i
min (np_test_1_all.cpp:2350)
2340 if (i % 3 == 0) {
2341 large_array.setElementAt({i}, object_(static_cast<int>(i)));
2342 } else if (i % 3 == 1) {
2343 large_array.setElementAt({i}, object_(static_cast<double>(i) * 0.5));
2344 } else {
2345 large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i)));
2346 }
2347 }
2348
2349 // Verify pattern
2350 for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) { // Check first 100
2351 object_ obj = large_array.getElementAt({i});
2352 if (i % 3 == 0) {
2353 if (!(obj.is_type<int>())) {
2354 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<int>())";
2355 std::cout << std::string("[FAIL] ") + description << std::endl;
2356 throw std::runtime_error(description);
2357 }
2358 } else if (i % 3 == 1) {
2359 if (!(obj.is_type<double>())) {
2360 std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<double>())";
argsort (np_test_1_all.cpp:16841)
16831 std::cout << "========= test_argsort =======================";
16832
16833 // Test 1D array argsort
16834 NDArray<int> arr1d({ 5 });
16835 arr1d.setElementAt({ 0 }, 30); // index 0
16836 arr1d.setElementAt({ 1 }, 10); // index 1
16837 arr1d.setElementAt({ 2 }, 40); // index 2
16838 arr1d.setElementAt({ 3 }, 20); // index 3
16839 arr1d.setElementAt({ 4 }, 50); // index 4
16840
16841 auto indices = argsort(arr1d);
16842
16843 // Expected order: 10(idx1), 20(idx3), 30(idx0), 40(idx2), 50(idx4)
16844 if (!(indices.getElementAt({ 0 }) == 1)) {
16845 std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 0 }) == 1)";
16846 std::cout << std::string("[FAIL] ") + description << std::endl;
16847 throw std::runtime_error(description);
16848 }
16849 if (!(indices.getElementAt({ 1 }) == 3)) {
16850 std::string description = std::string("test_argsort():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(indices.getElementAt({ 1 }) == 3)";
16851 std::cout << std::string("[FAIL] ") + description << std::endl;
isin (np_test_1_all.cpp:19434)
19424 std::cout << std::string("[FAIL] ") + description << std::endl;
19425 throw std::runtime_error(description);
19426 }
19427 if (!(membership.getElementAt({ 5 }) == false)) {
19428 std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(membership.getElementAt({ 5 }) == false)";
19429 std::cout << std::string("[FAIL] ") + description << std::endl;
19430 throw std::runtime_error(description);
19431 }
19432 // std::cout << "[OK] Membership testing (in1d) works correctly\n";
19433
19434 // Test isin (alias for in1d)
19435 auto isin_result = isin(test_array, test_values);
19436
19437 // Should be identical to in1d result
19438 for (size_t i = 0; i < membership.getSize(); ++i) {
19439 if (!(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))) {
19440 std::string description = std::string("testMembershipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isin_result.getElementAt({ i }) == membership.getElementAt({ i }))";
19441 std::cout << std::string("[FAIL] ") + description << std::endl;
19442 throw std::runtime_error(description);
19443 }
19444 }
unique (np_test_1_all.cpp:6259)
6249 if (!(data.size() == 100)) {
6250 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.size() == 100)";
6251 std::cout << std::string("[FAIL] ") + description << std::endl;
6252 throw std::runtime_error(description);
6253 }
6254
6255 // Count unique values
6256 std::vector<int> sorted_copy = data;
6257 std::sort(sorted_copy.begin(), sorted_copy.end());
6258 auto unique_end = std::unique(sorted_copy.begin(), sorted_copy.end());
6259 size_t unique_count = std::distance(sorted_copy.begin(), unique_end);
6260
6261 // Should have significantly fewer unique values than total elements
6262 if (!(unique_count < data.size() / 2)) {
6263 std::string description = std::string("test_data_generator_few_uniqueBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(unique_count < data.size() / 2)";
6264 std::cout << std::string("[FAIL] ") + description << std::endl;
6265 throw std::runtime_error(description);
6266 }
6267
6268 // std::cout << "Few unique data generation test passed. Unique values: " << unique_count << std::endl;
to_numpy (np_test_5_all.cpp:21373)
21363 if (errors == 0) {
21364 std::cout << "np_test_timedelta_components -> tests passed" << std::endl;
21365 }
21366 return errors;
21367}
21368
21369// =============================================================================
21370// Test 4: Total Conversion Properties
21371// =============================================================================
21372int np_test_timedelta_total_conversions() {
21373 int errors = 0;
21374
21375 numpy::Timedelta td(1, 12, 0); // 1 day 12 hours = 1.5 days = 36 hours
21376
21377 // total_seconds
21378 {
21379 double secs = td.total_seconds();
21380 double expected = (24 + 12) * 3600.0;
21381 if (std::abs(secs - expected) > 0.0001) {
21382 std::cout << "[FAIL] np_test_timedelta_total_conversions: total_seconds expected "
21383 << expected << ", got " << secs << std::endl;
to_string (np_test_1_all.cpp:454)
444 // Modify through different views
445 view1.setElementAt({0, 0}, 100);
446 view2.setElementAt({2, 1}, 200); // This is (1, 2) in original
447 view3.setElementAt({0, 0}, 300); // This is (1, 1) in original
448
449 // std::cout << "After modifications through multiple views:" << std::endl;
450 //original.printArray();
451
452 // Verify all changes are reflected
453 if (!(original.getElementAt({0, 0}) == 100)) {
454 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({0, 0}) == 100)";
455 std::cout << std::string("[FAIL] ") + description << std::endl;
456 throw std::runtime_error(description);
457 }
458 if (!(original.getElementAt({1, 2}) == 200)) {
459 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 2}) == 200)";
460 std::cout << std::string("[FAIL] ") + description << std::endl;
461 throw std::runtime_error(description);
462 }
463 if (!(original.getElementAt({1, 1}) == 300)) {
464 std::string description = std::string("testAdvancedViewLifecycle():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 300)";
tolist (np_test_4_all.cpp:20046)
20036} // namespace numpy_tests
20037
20038/**
20039 * @file np_test_phase6.cpp
20040 * @brief Test suite for Phase 6A - HIGH PRIORITY NDArray methods and masked array utilities
20041 *
20042 * Tests:
20043 * - ndarray.nonzero() - Return indices of non-zero elements
20044 * - ndarray.tobytes() - Convert to bytes representation
20045 * - ndarray.tolist() - Convert to nested list string
20046 * - ndarray.fill() - Fill with scalar value (in-place)
20047 * - ndarray.item() - Get single element as scalar
20048 * - ndarray.itemset() - Set single element value
20049 * - ma.masked_all_like() - Create masked array with all elements masked
20050 *
20051 * Created: 2025-10-28
20052 * Status: Phase 6A Implementation - HIGH PRIORITY functions
20053 */
20054
20055#include "../numpy/np_ndarray.h"
delete_ (np_test_4_all.cpp:18411)
18401 }
18402
18403 // ============================================================================
18404 // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS
18405 // ============================================================================
18406
18407 void np_test_phase1_delete() {
18408 std::cout << "========= delete_: element deletion ====";
18409
18410 auto arr = numpy::array({10, 20, 30, 40, 50});
18411 auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30)
18412
18413 // Verify size reduced
18414 if (result.getSize() != 4) {
18415 std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n";
18416 throw std::runtime_error("delete_ failed: wrong size");
18417 }
18418
18419 // Verify correct element was removed
18420 if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 ||
18421 result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) {
delete_ (np_test_4_all.cpp:18411)
18401 }
18402
18403 // ============================================================================
18404 // SPRINT 1.2: ESSENTIAL OPERATIONS TESTS
18405 // ============================================================================
18406
18407 void np_test_phase1_delete() {
18408 std::cout << "========= delete_: element deletion ====";
18409
18410 auto arr = numpy::array({10, 20, 30, 40, 50});
18411 auto result = numpy::delete_(arr, 2); // Delete index 2 (value 30)
18412
18413 // Verify size reduced
18414 if (result.getSize() != 4) {
18415 std::cout << " [FAIL] : delete_ result size is " << result.getSize() << ", expected 4\n";
18416 throw std::runtime_error("delete_ failed: wrong size");
18417 }
18418
18419 // Verify correct element was removed
18420 if (result.getElementAt({0}) != 10 || result.getElementAt({1}) != 20 ||
18421 result.getElementAt({2}) != 40 || result.getElementAt({3}) != 50) {
insert (np_test_1_all.cpp:6990)
6980}
6981
6982void testBusinessDateRangeDateTime() {
6983 std::cout << "========= testBusinessDateRangeDateTime =======================";
6984
6985 datetime64 start("2024-03-11"); // Monday
6986 datetime64 end("2024-03-22"); // Friday (2 weeks later)
6987
6988 // Create holidays set
6989 std::set<datetime64> holidays;
6990 holidays.insert(datetime64("2024-03-15")); // Friday holiday
6991
6992 auto business_dates = createBusinessDateRange(start, end, holidays);
6993 // std::cout << "Business dates from " << start.toString()
6994 // << " to " << end.toString() << " (excluding 2024-03-15):" << std::endl;
6995 //business_dates.printArray();
6996
6997 std::cout << " -> tests passed" << std::endl;
6998}
6999
7000void testMonthRangeDateTime() {
repeat (np_test_1_all.cpp:18268)
18258 // Create test array
18259 auto array = createInt32Array({ 2, 3 }, 0);
18260 for (size_t i = 0; i < 2; ++i) {
18261 for (size_t j = 0; j < 3; ++j) {
18262 array.setElementAt({ i, j }, static_cast<int32_t>(i * 3 + j + 1));
18263 }
18264 }
18265
18266 // Test repeat without axis (flattened)
18267 auto repeated_flat = repeat(array, 2);
18268 if (!(repeated_flat.getShape()[0] == 12)) {
18269 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getShape()[0] == 12)";
18270 std::cout << std::string("[FAIL] ") + description << std::endl;
18271 throw std::runtime_error(description);
18272 }
18273 if (!(repeated_flat.getElementAt({ 0 }) == 1)) {
18274 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getElementAt({ 0 }) == 1)";
18275 std::cout << std::string("[FAIL] ") + description << std::endl;
18276 throw std::runtime_error(description);
18277 }
repeat (np_test_1_all.cpp:18268)
18258 // Create test array
18259 auto array = createInt32Array({ 2, 3 }, 0);
18260 for (size_t i = 0; i < 2; ++i) {
18261 for (size_t j = 0; j < 3; ++j) {
18262 array.setElementAt({ i, j }, static_cast<int32_t>(i * 3 + j + 1));
18263 }
18264 }
18265
18266 // Test repeat without axis (flattened)
18267 auto repeated_flat = repeat(array, 2);
18268 if (!(repeated_flat.getShape()[0] == 12)) {
18269 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getShape()[0] == 12)";
18270 std::cout << std::string("[FAIL] ") + description << std::endl;
18271 throw std::runtime_error(description);
18272 }
18273 if (!(repeated_flat.getElementAt({ 0 }) == 1)) {
18274 std::string description = std::string("testRepeat():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(repeated_flat.getElementAt({ 0 }) == 1)";
18275 std::cout << std::string("[FAIL] ") + description << std::endl;
18276 throw std::runtime_error(description);
18277 }
astype (np_test_3_all.cpp:796)
786 auto ufunc_square = numpy::frompyfunc(square, 1, 1);
787
788 numpy::NDArray<double> arr({ 3 });
789 arr.setElementAt({ 0 }, 2.0);
790 arr.setElementAt({ 1 }, 3.0);
791 arr.setElementAt({ 2 }, 4.0);
792
793 auto result = ufunc_square(arr);
794
795 // Convert back to double to check values
796 auto typed_result = numpy::astype<double>(result);
797 if (std::abs(typed_result.getElementAt({ 0 }) - 4.0) > 1e-10 ||
798 std::abs(typed_result.getElementAt({ 1 }) - 9.0) > 1e-10 ||
799 std::abs(typed_result.getElementAt({ 2 }) - 16.0) > 1e-10) {
800 std::cout << "[FAIL] Basic frompyfunc creation failed";
801 return 1;
802 }
803
804 // std::cout << "[OK] Basic frompyfunc creation works correctly" << std::endl;
805 std::cout << " -> tests passed" << std::endl;
806 return 0;
copy (np_test_1_all.cpp:9812)
9802 //original.printArray();
9803
9804 // The modification should be at position (1,1) in original
9805 if (!(original.getElementAt({1, 1}) == 9999)) {
9806 std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)";
9807 std::cout << std::string("[FAIL] ") + description << std::endl;
9808 throw std::runtime_error(description);
9809 }
9810 // std::cout << "[OK] Slice view shares memory with original";
9811
9812 // Test slice copy (should be independent)
9813 auto slice_copy = original.sliceArray({{2, 4}, {2, 4}});
9814 // std::cout << "Slice copy [2:4, 2:4]:";
9815 //slice_copy.printArray();
9816
9817 slice_copy.setElementAt({0, 0}, 7777);
9818
9819 // std::cout << "After modifying slice copy at (0,0):" << std::endl;
9820 // std::cout << "Original array:" << std::endl;
9821 //original.printArray();
9822 // std::cout << "Slice copy:" << std::endl;
view (np_test_1_all.cpp:440)
430 for (size_t i = 0; i < 3; ++i) {
431 for (size_t j = 0; j < 3; ++j) {
432 original.setElementAt({i, j}, static_cast<int32_t>(i * 3 + j));
433 }
434 }
435
436 // std::cout << "Original array:" << std::endl;
437 //original.printArray();
438
439 // Create multiple views
440 auto view1 = original.view();
441 auto view2 = original.transposeView();
442 auto view3 = original.sliceView({{1, 3}, {1, 3}});
443
444 // Modify through different views
445 view1.setElementAt({0, 0}, 100);
446 view2.setElementAt({2, 1}, 200); // This is (1, 2) in original
447 view3.setElementAt({0, 0}, 300); // This is (1, 1) in original
448
449 // std::cout << "After modifications through multiple views:" << std::endl;
450 //original.printArray();
argmax (np_test_3_all.cpp:20521)
20511 auto result = numpy::argmin(arr);
20512 // Should return 1 (index of (1,5))
20513 if (result.getElementAt({0}) != 1) {
20514 throw std::runtime_error("argmin() failed: expected index 1");
20515 }
20516 std::cout << "PASSED\n";
20517}
20518
20519void test_argmax_complex128() {
20520 std::cout << " Testing argmax() with complex128... ";
20521 numpy::NDArray<complex128> arr({3});
20522 arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0 - largest
20523 arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1
20524 arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2
20525
20526 auto result = numpy::argmax(arr);
20527 // Should return 0 (index of (3,1))
20528 if (result.getElementAt({0}) != 0) {
20529 throw std::runtime_error("argmax() failed: expected index 0");
20530 }
argmin (np_test_3_all.cpp:20506)
20496 if (min_result.getElementAt({0}).real() != 2.0 || min_result.getElementAt({0}).imag() != 1.0) {
20497 throw std::runtime_error("min() tie-breaking failed: expected (2,1)");
20498 }
20499 if (max_result.getElementAt({0}).real() != 2.0 || max_result.getElementAt({0}).imag() != 5.0) {
20500 throw std::runtime_error("max() tie-breaking failed: expected (2,5)");
20501 }
20502 std::cout << "PASSED\n";
20503}
20504
20505void test_argmin_complex128() {
20506 std::cout << " Testing argmin() with complex128... ";
20507 numpy::NDArray<complex128> arr({3});
20508 arr.setElementAt({0}, complex128(3.0, 1.0)); // index 0
20509 arr.setElementAt({1}, complex128(1.0, 5.0)); // index 1 - smallest
20510 arr.setElementAt({2}, complex128(2.0, 2.0)); // index 2
20511
20512 auto result = numpy::argmin(arr);
20513 // Should return 1 (index of (1,5))
20514 if (result.getElementAt({0}) != 1) {
20515 throw std::runtime_error("argmin() failed: expected index 1");
20516 }
count (np_test_1_all.cpp:3616)
3606 // Create larger arrays for performance testing
3607 auto large_arr = NDArray<double>::createOnes({100, 100});
3608 auto broadcast_arr = NDArray<double>::createOnes({1, 100});
3609
3610 // Time the operation (basic timing)
3611 auto start = std::chrono::high_resolution_clock::now();
3612 auto result = large_arr.addArrays(broadcast_arr);
3613 auto end = std::chrono::high_resolution_clock::now();
3614
3615 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
3616 // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl;
3617
3618 // Verify result shape
3619 if (!((result.getShape() == std::vector<size_t>{100, 100}))) {
3620 std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector<size_t>{100, 100}))";
3621 std::cout << std::string("[FAIL] ") + description << std::endl;
3622 throw std::runtime_error(description);
3623 }
3624 if (!(result.getElementAt({50, 50}) == 2.0)) {
3625 std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({50, 50}) == 2.0)";
3626 std::cout << std::string("[FAIL] ") + description << std::endl;
describe (np_test_1_all.cpp:11448)
11438 auto moment2_result = moment(normal_data, 2);
11439 // std::cout << "2nd moment: " << moment2_result.getElementAt({ 0 }) << std::endl;
11440 if (!(moment2_result.getElementAt({ 0 }) > 0)) {
11441 std::string description = std::string("testDistributionShapeMeasures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(moment2_result.getElementAt({ 0 }) > 0)";
11442 std::cout << std::string("[FAIL] ") + description << std::endl;
11443 throw std::runtime_error(description);
11444 }
11445 // std::cout << "[OK] Moment calculation\n";
11446
11447 // Test describe
11448 auto desc_result = describe(normal_data);
11449 // std::cout << "Describe result:" << std::endl;
11450 // std::cout << " nobs: " << desc_result.nobs << std::endl;
11451 // std::cout << " min: " << desc_result.minmax.first << ", max: " << desc_result.minmax.second << std::endl;
11452 // std::cout << " mean: " << desc_result.mean << std::endl;
11453 // std::cout << " variance: " << desc_result.variance << std::endl;
11454 if (!(desc_result.nobs == 5)) {
11455 std::string description = std::string("testDistributionShapeMeasures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(desc_result.nobs == 5)";
11456 std::cout << std::string("[FAIL] ") + description << std::endl;
11457 throw std::runtime_error(description);
11458 }
dtype (np_test_3_all.cpp:7942)
7932 if (info.max != expected_max) {
7933 std::cerr << "[FAIL] IInfo<uint64_t>::max mismatch";
7934 throw std::runtime_error("IInfo uint64 max test failed");
7935 }
7936 // std::cout << "[OK] IInfo<uint64_t>::max = " << info.max << std::endl;
7937
7938 std::cout << " -> tests passed" << std::endl;
7939}
7940
7941// ============================================================================
7942// dtype() Tests - DType descriptor creation
7943// ============================================================================
7944
7945void np_test_dtype_from_string_integers() {
7946 std::cout << "========= np_test_dtype_from_string_integers =======================";
7947
7948 // Test integer type strings
7949 if (dtype("int8") != DType::INT8) {
7950 std::cerr << "[FAIL] dtype(\"int8\") should return INT8";
7951 throw std::runtime_error("dtype string test failed");
7952 }
nbytes (np_test_2_all.cpp:3547)
3537 }
3538
3539 // Test itemsize
3540 if (!(itemsize(arr) == sizeof(double))) {
3541 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))";
3542 std::cout << std::string("[FAIL] ") + description << std::endl;
3543 throw std::runtime_error(description);
3544 }
3545
3546 // Test nbytes
3547 if (!(nbytes(arr) == 24 * sizeof(double))) {
3548 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))";
3549 std::cout << std::string("[FAIL] ") + description << std::endl;
3550 throw std::runtime_error(description);
3551 }
3552
3553 // Test flags
3554 auto flags_info = flags(arr);
3555 if (!(flags_info.owndata == true)) {
3556 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(flags_info.owndata == true)";
3557 std::cout << std::string("[FAIL] ") + description << std::endl;
ndim (np_test_2_all.cpp:3526)
3516 void testArrayInfo() {
3517 std::cout << "Testing array information functions...\n";
3518
3519 // Test basic info functions
3520 auto arr = createFloat64Array({ 2, 3, 4 });
3521 for (size_t i = 0; i < arr.getSize(); ++i) {
3522 arr.setElementAt({ i / 12, (i / 4) % 3, i % 4 }, static_cast<double>(i));
3523 }
3524
3525 // Test ndim
3526 if (!(ndim(arr) == 3)) {
3527 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ndim(arr) == 3)";
3528 std::cout << std::string("[FAIL] ") + description << std::endl;
3529 throw std::runtime_error(description);
3530 }
3531
3532 // Test size
3533 if (!(size(arr) == 24)) {
3534 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)";
3535 std::cout << std::string("[FAIL] ") + description << std::endl;
3536 throw std::runtime_error(description);
ordered (np_test_2_all.cpp:8640)
8630 if (indices.size() != n) {
8631 std::cout << " IPP large lexsort: -> [FAIL]";
8632 throw std::runtime_error("IPP large lexsort: FAILED - incorrect result size");
8633 }
8634
8635 // Verify sorting correctness
8636 for (size_t i = 1; i < indices.size(); ++i) {
8637 size_t prev_idx = indices[i - 1];
8638 size_t curr_idx = indices[i];
8639
8640 // Check if correctly ordered (second key primary)
8641 if (keys[1][curr_idx] < keys[1][prev_idx] ||
8642 (keys[1][curr_idx] == keys[1][prev_idx] && keys[0][curr_idx] < keys[0][prev_idx])) {
8643 std::cout << " IPP large lexsort order: -> [FAIL]";
8644 throw std::runtime_error("IPP large lexsort: FAILED - incorrect ordering");
8645 }
8646 }
8647
8648 auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
8649 // std::cout << "[OK] IPP large lexsort (" << n << " elements): PASSED in " << duration_us << " μs" << std::endl;
8650 }
shape (np_test_1_all.cpp:3751)
3741 }
3742
3743 for (size_t j = 0; j < 3; ++j) {
3744 arr3.setElementAt({0, j}, static_cast<double>((j + 1) * 100)); // [100, 200, 300]
3745 }
3746
3747 // Broadcast all arrays together
3748 std::vector<NDArray<double>> arrays = {arr1, arr2, arr3};
3749 auto broadcasted = broadcast_arrays(arrays);
3750
3751 // All should have shape (2, 3)
3752 for (const auto& arr : broadcasted) {
3753 if (!((arr.getShape() == std::vector<size_t>{2, 3}))) {
3754 std::string description = std::string("test_broadcast_arrays():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((arr.getShape() == std::vector<size_t>{2, 3}))";
3755 std::cout << std::string("[FAIL] ") + description << std::endl;
3756 throw std::runtime_error(description);
3757 }
3758 }
3759
3760 // Check values
3761 if (!(broadcasted[0].getElementAt({0, 1}) == 2.0)) {
shift (np_test_1_all.cpp:2761)
2751 throw std::runtime_error(description);
2752 }
2753 if (!(left_array.getElementAt({3}) == 16384)) {
2754 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(left_array.getElementAt({3}) == 16384)";
2755 std::cout << std::string("[FAIL] ") + description << std::endl;
2756 throw std::runtime_error(description);
2757 }
2758
2759 // std::cout << "[OK] Left shift by array\n";
2760
2761 // Test excessive shift (should result in 0)
2762 auto excessive_left = left_shift(arr, 20); // More than 16 bits
2763 if (!(excessive_left.getElementAt({0}) == 0)) {
2764 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({0}) == 0)";
2765 std::cout << std::string("[FAIL] ") + description << std::endl;
2766 throw std::runtime_error(description);
2767 }
2768 if (!(excessive_left.getElementAt({1}) == 0)) {
2769 std::string description = std::string("testBitShifts():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(excessive_left.getElementAt({1}) == 0)";
2770 std::cout << std::string("[FAIL] ") + description << std::endl;
2771 throw std::runtime_error(description);
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43 const double TOLERANCE = 1e-10;
44
45 bool allTrue(const NDArray<bool>& arr) {
46 std::vector<size_t> indices(arr.getShape().size(), 0);
47 do {
48 if (!arr.getElementAt(indices)) {
49 return false;
50 }
51 } while (incrementIndices(indices, arr.getShape()));
52 return true;
53 }
54
55 bool allFalse(const NDArray<bool>& arr) {
56 std::vector<size_t> indices(arr.getShape().size(), 0);
slice (np_test_1_all.cpp:13009)
12999 // Test substr valid start but excessive length
13000 auto result3 = str1.substr(2, 10); // start=2, len=10 on string of length 5
13001 if (!(result3.to_string() == "llo")) {
13002 std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result3.to_string() == \"llo\")";
13003 std::cout << std::string("[FAIL] ") + description << std::endl;
13004 throw std::runtime_error(description);
13005 }
13006 // std::cout << "[OK] vstring_ substr(2, 10) on 'hello' returns 'llo'\n";
13007
13008 // Test slice method with out-of-bounds
13009 auto result4 = str1.slice(10, 15); // slice[10:15] on string of length 5
13010 if (!(result4.to_string() == "")) {
13011 std::string description = std::string("test_variable_string_out_of_bounds():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result4.to_string() == \"\")";
13012 std::cout << std::string("[FAIL] ") + description << std::endl;
13013 throw std::runtime_error(description);
13014 }
13015 // std::cout << "[OK] vstring_ slice(10, 15) on 'hello' returns empty string\n";
13016
13017 // Test slice with negative indices
13018 auto result5 = str1.slice(-2, -1); // slice[-2:-1] should be "l"
13019 if (!(result5.to_string() == "l")) {