NestedArray#
-
class numpy::NestedArray#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use NestedArray
NestedArray obj;
// ... operations ...
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
std::vector<NDArray<U>*> |
NP_NESTED_ARRAY.H:154 |
|
|
std::vector<std::pair<std::vector<size_t>,U*>> |
NP_NESTED_ARRAY.H:196 |
|
|
size_t |
NP_NESTED_ARRAY.H:166 |
|
|
size_t |
NP_NESTED_ARRAY.H:206 |
Shape Manipulation#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
void |
NP_NESTED_ARRAY.H:209 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
NP_NESTED_ARRAY.H:203 |
|
|
bool |
NP_NESTED_ARRAY.H:159 |
|
|
bool |
NP_NESTED_ARRAY.H:182 |
|
|
std::string |
NP_NESTED_ARRAY.H:186 |
|
|
void |
NP_NESTED_ARRAY.H:212 |
|
|
void |
NP_NESTED_ARRAY.H:157 |
|
|
void |
NP_NESTED_ARRAY.H:200 |
|
|
bool |
NP_NESTED_ARRAY.H:172 |
Code Examples#
The following examples are extracted from the test suite.
getNestedDepth (np_test_3_all.cpp:21171)
21161 // Level 2: object arrays containing int arrays
21162 numpy::NDArray<numpy::object_> mid({2});
21163 mid.setElementAt({0}, numpy::object_(inner1));
21164 mid.setElementAt({1}, numpy::object_(inner2));
21165
21166 // Level 3: object array containing object arrays
21167 numpy::NDArray<numpy::object_> outer({1});
21168 outer.setElementAt({0}, numpy::object_(mid));
21169
21170 // Test getNestedDepth
21171 size_t depth = numpy::NestedArray<int>::getNestedDepth(outer);
21172 CHECK(depth >= 2, "np_test_nested_deep", "depth < 2");
21173
21174 std::cout << " -> tests passed" << std::endl;
21175}
21176
21177// Test 4: Jagged arrays
21178void np_test_nested_jagged() {
21179 std::cout << "========= Jagged arrays ====" ;
21180
21181 // Create jagged array using convenience function
isHomogeneousArrays (np_test_1_all.cpp:1824)
1814 }
1815 }
1816
1817 // Test homogeneous array detection
1818 std::vector<NDArray<double>> double_arrays;
1819 for (int i = 0; i < 3; ++i) {
1820 double_arrays.push_back(NDArray<double>({2}, i * 1.5));
1821 }
1822
1823 NDArray<object_> homogeneous = createObjectArrayFromNDArrays(double_arrays);
1824 if (!(NestedArray<double>::isHomogeneousArrays(homogeneous))) {
1825 std::string description = std::string("testObjectArrayOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(NestedArray<double>::isHomogeneousArrays(homogeneous))";
1826 std::cout << std::string("[FAIL] ") + description << std::endl;
1827 throw std::runtime_error(description);
1828 }
1829
1830 // Test heterogeneous detection
1831 NDArray<object_> heterogeneous = createMixedObjectArray({
1832 object_(NDArray<int>({2}, 1)),
1833 object_(42)
1834 });
isRectangular (np_test_3_all.cpp:21198)
21188 CHECK(jagged.getSize() == 3, "np_test_nested_jagged", "jagged.size != 3");
21189
21190 // Test getRowLengths
21191 std::vector<size_t> lengths = numpy::NestedArray<int>::getRowLengths(jagged);
21192 CHECK(lengths.size() == 3, "np_test_nested_jagged", "lengths.size != 3");
21193 CHECK(lengths[0] == 3, "np_test_nested_jagged", "lengths[0] != 3");
21194 CHECK(lengths[1] == 2, "np_test_nested_jagged", "lengths[1] != 2");
21195 CHECK(lengths[2] == 4, "np_test_nested_jagged", "lengths[2] != 4");
21196
21197 // Test isRectangular
21198 bool is_rect = numpy::NestedArray<int>::isRectangular(jagged);
21199 CHECK(!is_rect, "np_test_nested_jagged", "jagged should not be rectangular");
21200
21201 // Create rectangular array
21202 numpy::NDArray<numpy::object_> rect = numpy::make_jagged_array({
21203 {1, 2, 3},
21204 {4, 5, 6},
21205 {7, 8, 9}
21206 });
21207 bool is_rect2 = numpy::NestedArray<int>::isRectangular(rect);
21208 CHECK(is_rect2, "np_test_nested_jagged", "rect should be rectangular");
printNestedArray (np_test_3_all.cpp:21364)
21354 // Create nested structure
21355 numpy::NDArray<numpy::object_> inner({2});
21356 inner.setElementAt({0}, numpy::object_(1));
21357 inner.setElementAt({1}, numpy::object_(2));
21358
21359 numpy::NDArray<numpy::object_> outer({2});
21360 outer.setElementAt({0}, numpy::object_(inner));
21361 outer.setElementAt({1}, numpy::object_(std::string("hello")));
21362
21363 // Get print output
21364 std::string output = numpy::NestedArray<int>::printNestedArray(outer);
21365
21366 // Check output contains expected elements
21367 CHECK(output.find("Nested Array Structure") != std::string::npos, "np_test_nested_print", "missing header");
21368 CHECK(output.find("shape=") != std::string::npos, "np_test_nested_print", "missing shape");
21369
21370 std::cout << " -> tests passed" << std::endl;
21371}
21372
21373#undef CHECK
validateNestedStructure (np_test_3_all.cpp:21285)
21275 inner1.setElementAt({1}, numpy::object_(2));
21276
21277 numpy::NDArray<numpy::object_> inner2({2});
21278 inner2.setElementAt({0}, numpy::object_(3));
21279 inner2.setElementAt({1}, numpy::object_(4));
21280
21281 numpy::NDArray<numpy::object_> outer({2});
21282 outer.setElementAt({0}, numpy::object_(inner1));
21283 outer.setElementAt({1}, numpy::object_(inner2));
21284
21285 bool valid = numpy::NestedArray<int>::validateNestedStructure(outer);
21286 CHECK(valid, "np_test_nested_validate", "homogeneous should be valid");
21287
21288 // Create heterogeneous structure (different inner shapes)
21289 numpy::NDArray<numpy::object_> inner3({3}); // Different size!
21290 inner3.setElementAt({0}, numpy::object_(1));
21291 inner3.setElementAt({1}, numpy::object_(2));
21292 inner3.setElementAt({2}, numpy::object_(3));
21293
21294 numpy::NDArray<numpy::object_> hetero({2});
21295 hetero.setElementAt({0}, numpy::object_(inner1)); // size 2