TimedeltaArray#
-
class numpy::TimedeltaArray#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use TimedeltaArray
TimedeltaArray obj;
// ... operations ...
Constructors#
Signature |
Location |
Example |
|---|---|---|
|
df_timedelta_array.h:151 |
|
|
df_timedelta_array.h:166 |
|
|
df_timedelta_array.h:207 |
|
|
df_timedelta_array.h:227 |
Construction#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
static TimedeltaArray |
df_timedelta_array.h:437 |
Array Creation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_timedelta_array.h:292 |
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::timedelta64 |
df_timedelta_array.h:344 |
|
|
TimedeltaArray |
df_timedelta_array.h:391 |
Statistics#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::optional<numpy::timedelta64> |
df_timedelta_array.h:817 |
|
|
std::optional<numpy::timedelta64> |
df_timedelta_array.h:808 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_array.h:896 |
Sorting#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::NDArray<size_t> |
df_timedelta_array.h:737 |
Math Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaArray |
df_timedelta_array.h:1255 |
|
|
TimedeltaArray |
df_timedelta_array.h:1303 |
|
|
TimedeltaArray |
df_timedelta_array.h:1278 |
|
|
TimedeltaArray |
df_timedelta_array.h:1328 |
Linear Algebra#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
df_timedelta_array.h:1405 |
Set Operations#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaArray |
df_timedelta_array.h:675 |
I/O#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
numpy::NDArray<numpy::timedelta64> |
df_timedelta_array.h:500 |
|
|
std::vector<std::optional<PyTimedelta>> |
df_timedelta_array.h:961 |
|
|
std::string |
df_timedelta_array.h:1357 |
Type Handling#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
TimedeltaArray |
df_timedelta_array.h:384 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_timedelta_array.h:355 |
|
|
BooleanArray |
df_timedelta_array.h:1075 |
|
|
BooleanArray |
df_timedelta_array.h:1056 |
|
|
BooleanArray |
df_timedelta_array.h:1094 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::optional<size_t> |
df_timedelta_array.h:788 |
|
|
std::optional<size_t> |
df_timedelta_array.h:768 |
|
|
static TimedeltaArray |
df_timedelta_array.h:444 |
|
|
size_t |
df_timedelta_array.h:513 |
|
|
const numpy::NDArray<numpy::timedelta64>& |
df_timedelta_array.h:317 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_array.h:830 |
|
|
TimedeltaArray |
df_timedelta_array.h:487 |
|
|
dtype_type |
df_timedelta_array.h:257 |
|
|
std::pair<numpy::NDArray<numpy::int64>, TimedeltaArray> |
df_timedelta_array.h:701 |
|
|
TimedeltaArray |
df_timedelta_array.h:473 |
|
|
bool |
df_timedelta_array.h:526 |
|
|
numpy::NDArray<numpy::bool_> |
df_timedelta_array.h:366 |
|
|
size_t |
df_timedelta_array.h:299 |
|
|
const numpy::NDArray<numpy::bool_>& |
df_timedelta_array.h:324 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_array.h:873 |
|
|
size_t |
df_timedelta_array.h:271 |
|
|
constexpr int |
df_timedelta_array.h:278 |
|
|
numpy::NDArray<numpy::bool_> |
df_timedelta_array.h:373 |
|
|
static int64_t |
df_timedelta_array.h:1415 |
|
|
std::string |
df_timedelta_array.h:1374 |
|
|
IntegerArray<numpy::int64> |
df_timedelta_array.h:849 |
|
|
std::vector<size_t> |
df_timedelta_array.h:285 |
|
|
size_t |
df_timedelta_array.h:264 |
|
|
numpy::timedelta64 |
df_timedelta_array.h:238 |
|
|
FloatingArray<numpy::float64> |
df_timedelta_array.h:934 |
|
|
numpy::DateTimeUnit |
df_timedelta_array.h:306 |
|
|
void |
df_timedelta_array.h:1387 |
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}
at (np_test_1_all.cpp:144)
134 array.setElementAt({0, 0}, 1);
135 array.setElementAt({0, 1}, 2);
136 array.setElementAt({0, 2}, 3);
137 array.setElementAt({1, 1}, 5);
138 array.setElementAt({2, 2}, 9);
139
140 // std::cout << "Array after setting elements:" << std::endl;
141 //array.printArray();
142
143 // std::cout << "Element at (1,1): " << array.getElementAt({1, 1}) << std::endl;
144 // std::cout << "Element at (2,2): " << array.getElementAt({2, 2});
145
146 std::cout << " -> tests passed" << std::endl;
147}
148
149void testArithmetic() {
150 std::cout << "========= testArithmeticOperations =======================";
151
152 auto array1 = createFloat32Array({2, 2}, 5.0f);
153 auto array2 = createFloat32Array({2, 2}, 3.0f);
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 }
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>())";
nanoseconds (np_test_5_all.cpp:21255)
21245 errors++;
21246 }
21247 }
21248
21249 if (errors == 0) {
21250 std::cout << "np_test_timedelta_constructors -> tests passed" << std::endl;
21251 }
21252 return errors;
21253}
21254
21255// =============================================================================
21256// Test 2: Static Factory Methods
21257// =============================================================================
21258int np_test_timedelta_static_factories() {
21259 int errors = 0;
21260
21261 // NaT()
21262 {
21263 numpy::Timedelta nat = numpy::Timedelta::NaT();
21264 if (!nat.isNaT()) {
21265 std::cout << "[FAIL] np_test_timedelta_static_factories: NaT().isNaT() should be true"
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;
abs (np_test_1_all.cpp:101)
91 if (arr.getShape().size() == 1 && arr.getShape()[0] <= 10) {
92 for (size_t i = 0; i < arr.getShape()[0]; ++i) {
93 // std::cout << static_cast<int64_t>(arr.getElementAt({i})) << " ";
94 }
95 }
96 // std::cout << std::endl;
97 }
98
99 // Helper function for core array extensions tests
100 bool isApproxEqualExt(double a, double b, double tolerance = 1e-10) {
101 return std::abs(a - b) < tolerance;
102 }
103}
104
105void f_nothing() {
106 // This function does nothing and tests nothing
107 // It's a placeholder test function
108}
109
110// ------ merging np_test_advanced_indexing.cpp -- number of functions merged=16 --------------------------------
ceil (np_test_2_all.cpp:5735)
5725 throw std::runtime_error(description);
5726 }
5727 if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) {
5728 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))";
5729 std::cout << std::string("[FAIL] ") + description << std::endl;
5730 throw std::runtime_error(description);
5731 }
5732 // std::cout << "[OK] floor function works correctly\n";
5733
5734 // Test ceil
5735 auto ceil_result = ceil(values);
5736 if (!(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))) {
5737 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 0 }), std::ceil(-2.7)))";
5738 std::cout << std::string("[FAIL] ") + description << std::endl;
5739 throw std::runtime_error(description);
5740 }
5741 if (!(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))) {
5742 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(ceil_result.getElementAt({ 5 }), std::ceil(2.7)))";
5743 std::cout << std::string("[FAIL] ") + description << std::endl;
5744 throw std::runtime_error(description);
5745 }
floor (np_test_2_all.cpp:5721)
5711 throw std::runtime_error(description);
5712 }
5713 if (!(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))) {
5714 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(round_result.getElementAt({ 5 }), std::round(2.7)))";
5715 std::cout << std::string("[FAIL] ") + description << std::endl;
5716 throw std::runtime_error(description);
5717 }
5718 // std::cout << "[OK] round function works correctly\n";
5719
5720 // Test floor
5721 auto floor_result = floor(values);
5722 if (!(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))) {
5723 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 0 }), std::floor(-2.7)))";
5724 std::cout << std::string("[FAIL] ") + description << std::endl;
5725 throw std::runtime_error(description);
5726 }
5727 if (!(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))) {
5728 std::string description = std::string("testRoundingFunctions():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(floor_result.getElementAt({ 5 }), std::floor(2.7)))";
5729 std::cout << std::string("[FAIL] ") + description << std::endl;
5730 throw std::runtime_error(description);
5731 }
round (np_test_1_all.cpp:23769)
23759 throw std::runtime_error(description);
23760 }
23761 if (!(power_scalar_result.getShape() == array.getShape())) {
23762 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(power_scalar_result.getShape() == array.getShape())";
23763 std::cout << std::string("[FAIL] ") + description << std::endl;
23764 throw std::runtime_error(description);
23765 }
23766 // std::cout << "[OK] Power functions have correct signatures\n";
23767
23768 // Test rounding functions with new decimals parameter
23769 auto round_result = round(array); // Default decimals=0
23770 auto round_decimals_result = round(array, 2); // With decimals
23771 if (!(round_result.getShape() == array.getShape())) {
23772 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_result.getShape() == array.getShape())";
23773 std::cout << std::string("[FAIL] ") + description << std::endl;
23774 throw std::runtime_error(description);
23775 }
23776 if (!(round_decimals_result.getShape() == array.getShape())) {
23777 std::string description = std::string("testMathFunctionSignatures():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(round_decimals_result.getShape() == array.getShape())";
23778 std::cout << std::string("[FAIL] ") + description << std::endl;
23779 throw std::runtime_error(description);
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)";
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;
is_zero (np_test_2_all.cpp:7329)
7319 // Test machine epsilon
7320 auto eps_double = machine_epsilon<double>();
7321 if (!(eps_double > 0)) {
7322 std::string description = std::string("test_basic_utilities():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(eps_double > 0)";
7323 std::cout << std::string("[FAIL] ") + description << std::endl;
7324 throw std::runtime_error(description);
7325 }
7326 // std::cout << "Machine epsilon (double): " << eps_double << std::endl;
7327
7328 // Test zero check
7329 bool is_zero_result = is_zero(1e-16);
7330 if (!(is_zero_result == true)) {
7331 std::string description = std::string("test_basic_utilities():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(is_zero_result == true)";
7332 std::cout << std::string("[FAIL] ") + description << std::endl;
7333 throw std::runtime_error(description);
7334 }
7335 // std::cout << "Is 1e-16 zero? " << (is_zero_result ? "Yes" : "No") << std::endl;
7336
7337 // Test safe divide
7338 double safe_result = safe_divide(1.0, 2.0);
7339 if (!(safe_result == 0.5)) {
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;
data (np_test_1_all.cpp:2084)
2074 }
2075 if (!(derived_ptr->extra == 2)) {
2076 std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(derived_ptr->extra == 2)";
2077 std::cout << std::string("[FAIL] ") + description << std::endl;
2078 throw std::runtime_error(description);
2079 }
2080
2081 // Test 6: Large object handling
2082 struct LargeObject {
2083 std::vector<double> data;
2084 LargeObject() : data(10000, 3.14159) {} // Large object
2085 };
2086
2087 LargeObject large;
2088 object_ large_obj(large); // Should handle large objects
2089 if (!(!large_obj.is_null())) {
2090 std::string description = std::string("testObjectHolderEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!large_obj.is_null())";
2091 std::cout << std::string("[FAIL] ") + description << std::endl;
2092 throw std::runtime_error(description);
2093 }
2094 if (!(large_obj.is_type<LargeObject>())) {
days (np_test_1_all.cpp:28638)
28628 }
28629
28630 void np_test_datetime_business_date_range() {
28631 std::cout << "========= businessDateRange: business days only =======================";
28632
28633 // Get business days in first week of January 2024
28634 auto start_dt = numpy::datetime64("2024-01-01");
28635 auto end_dt = numpy::datetime64("2024-01-07");
28636 auto biz_dates = numpy::datetime_utils::businessDateRange(start_dt, end_dt);
28637
28638 // Should have between 3-5 business days (accounting for holidays)
28639 bool passed = (biz_dates.size() >= 3 && biz_dates.size() <= 5);
28640
28641 if (!passed) {
28642 std::cout << " [FAIL] : in np_test_datetime_business_date_range() : Got " << biz_dates.size() << " business days";
28643 throw std::runtime_error("np_test_datetime_business_date_range failed");
28644 }
28645
28646 std::cout << " -> tests passed" << std::endl;
28647 }
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 }
mask (np_test_1_all.cpp:27691)
27681 void test_mask_rows_cols() {
27682 std::cout << "========= ma::mask_rows() and ma::mask_cols(): mask operations ===";
27683
27684 auto data = numpy::array({ {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} });
27685 auto mask = numpy::array({ {false, true, false}, {false, false, false} });
27686 auto ma = numpy::ma::masked_array(data, mask);
27687
27688 auto result_rows = numpy::ma::mask_rows(ma);
27689 auto result_cols = numpy::ma::mask_cols(ma);
27690
27691 // std::cout << " ma::mask_rows(): row 0 fully masked: " << (result_rows.mask().getElementAt({ 0, 0 }) ? "true" : "false") << std::endl;
27692 // std::cout << " ma::mask_cols(): column 1 fully masked: " << (result_cols.mask().getElementAt({ 1, 1 }) ? "true" : "false");
27693 std::cout << " -> tests passed";
27694 }
27695
27696 void test_compress_rowcols() {
27697 std::cout << "========= ma::compress_rowcols(): remove masked rows/columns ===";
27698
27699 auto data = numpy::array({ {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0} });
27700 auto mask = numpy::array({ {false, false, false}, {false, true, false}, {false, false, false} });
27701 auto ma = numpy::ma::masked_array(data, mask);
microseconds (np_test_2_all.cpp:4731)
4721 }
4722
4723 // Write thread-specific pattern
4724 for (size_t j = 0; j < size; ++j) {
4725 ptr[j] = thread_id * 1000 + static_cast<int>(j);
4726 }
4727
4728 allocations.push_back({ ptr, size });
4729
4730 // Small delay to increase chance of concurrent access
4731 std::this_thread::sleep_for(std::chrono::microseconds(1));
4732 }
4733
4734 // Verify data integrity
4735 for (auto& alloc : allocations) {
4736 for (size_t j = 0; j < alloc.second; ++j) {
4737 if (!(alloc.first[j] == thread_id * 1000 + static_cast<int>(j))) {
4738 std::string description = std::string("test_concurrent_allocations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(alloc.first[j] == thread_id * 1000 + static_cast<int>(j))";
4739 std::cout << std::string("[FAIL] ") + description << std::endl;
4740 throw std::runtime_error(description);
4741 }
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);
seconds (np_test_5_all.cpp:21240)
21230 // From value and unit - static factory for fractional values
21231 {
21232 numpy::Timedelta td_frac = numpy::Timedelta::from_value(1.5, "h"); // 1.5 hours = 90 minutes
21233 std::cout << " Timedelta::from_value(1.5, 'h') = " << td_frac.total_minutes() << " minutes" << std::endl;
21234 if (std::abs(td_frac.total_minutes() - 90.0) > 0.0001) {
21235 std::cout << "[FAIL] np_test_timedelta_constructors: from_value(1.5, 'h') expected 90 minutes, got "
21236 << td_frac.total_minutes() << std::endl;
21237 errors++;
21238 }
21239
21240 numpy::Timedelta td_frac2 = numpy::Timedelta::from_value(0.5, "D"); // 0.5 days = 12 hours
21241 std::cout << " Timedelta::from_value(0.5, 'D') = " << td_frac2.total_hours() << " hours" << std::endl;
21242 if (std::abs(td_frac2.total_hours() - 12.0) > 0.0001) {
21243 std::cout << "[FAIL] np_test_timedelta_constructors: from_value(0.5, 'D') expected 12 hours, got "
21244 << td_frac2.total_hours() << std::endl;
21245 errors++;
21246 }
21247 }
21248
21249 if (errors == 0) {
21250 std::cout << "np_test_timedelta_constructors -> tests passed" << std::endl;
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)) {
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);
td (np_test_2_all.cpp:21974)
21964 {numpy::DateTimeUnit::Hour, "h"},
21965 {numpy::DateTimeUnit::Minute, "m"},
21966 {numpy::DateTimeUnit::Second, "s"},
21967 {numpy::DateTimeUnit::Millisecond, "ms"},
21968 {numpy::DateTimeUnit::Microsecond, "us"},
21969 {numpy::DateTimeUnit::Nanosecond, "ns"}
21970 };
21971
21972 // Test all timedelta64 units
21973 for (const auto& test : tests) {
21974 numpy::timedelta64 td(1, test.unit);
21975 auto [unit_str, count] = numpy::datetime_utils::datetime_data(td);
21976
21977 if (unit_str != test.expected_str || count != 1) {
21978 std::cout << " [FAIL] : in np_test_datetime_data_timedelta() : timedelta64 unit test failed for "
21979 << test.expected_str << ": got ('" << unit_str << "', " << count << ")" << std::endl;
21980 throw std::runtime_error("np_test_datetime_data_timedelta failed: timedelta64 unit mismatch");
21981 }
21982 }
21983
21984 std::cout << " -> tests passed" << std::endl;
total_seconds (np_test_5_all.cpp:21288)
21278 }
21279 }
21280
21281 // max()
21282 {
21283 numpy::Timedelta max_td = numpy::Timedelta::max();
21284 if (max_td.value() != INT64_MAX) {
21285 std::cout << "[FAIL] np_test_timedelta_static_factories: max() expected INT64_MAX"
21286 << std::endl;
21287 errors++;
21288 }
21289 }
21290
21291 // resolution()
21292 {
21293 numpy::Timedelta res = numpy::Timedelta::resolution();
21294 if (res.value() != 1) {
21295 std::cout << "[FAIL] np_test_timedelta_static_factories: resolution() expected 1 ns"
21296 << std::endl;
21297 errors++;
21298 }