Slice ===== .. cpp:class:: numpy::Slice Core array class in the numpy namespace. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Slice Slice obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``Slice(std::optionalstop\_)`` - NP_INDEX_TYPES.H:30 - * - ``Slice(std::optionalstart\_, std::optionalstop\_)`` - NP_INDEX_TYPES.H:31 - * - ``Slice(std::optionalstart\_, std::optionalstop\_, std::optionalstep\_)`` - NP_INDEX_TYPES.H:33 - Logical ------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Slice all()`` - Slice - NP_INDEX_TYPES.H:37 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Slice from(ssize_tstart\_)`` - Slice - NP_INDEX_TYPES.H:38 - :ref:`View ` * - ``size_t length(size_tdim_size)`` - size_t - NP_INDEX_TYPES.H:81 - :ref:`View ` * - ``Slice range(ssize_tstart\_, ssize_tstop\_)`` - Slice - NP_INDEX_TYPES.H:40 - :ref:`View ` * - ``Slice stepped(ssize_tstart\_, ssize_tstop\_, ssize_tstep\_)`` - Slice - NP_INDEX_TYPES.H:41 - * - ``Slice to(ssize_tstop\_)`` - Slice - NP_INDEX_TYPES.H:39 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-slice-all-0: .. dropdown:: all (np_test_4_all.cpp:23928) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 23918 :emphasize-lines: 11 } } } auto result = numpy::einsum("ijk->ik", {A}); if (result.getShape()[0] != 2 || result.getShape()[1] != 2) { throw std::runtime_error("einsum partial sum shape wrong"); } // Sum over j: 1+2+3 = 6 for all (i,k) positions if (std::abs(result.getElementAt({0, 0}) - 6.0) > 1e-10 || std::abs(result.getElementAt({1, 1}) - 6.0) > 1e-10) { throw std::runtime_error("einsum partial sum values wrong"); } // std::cout << " OK: Partial sum: 'ijk->ik'\n"; } // Test 5: Size-1 dimension handling { .. _example-slice-from-1: .. dropdown:: from (np_test_2_all.cpp:3651) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 3641 :emphasize-lines: 11 arr.setElementAt({ i, j }, static_cast(i * 4 + j)); } } auto rotated = rot90(arr, 1); if (!(rotated.getShape() == std::vector({ 4, 3 }))) { std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(rotated.getShape() == std::vector({ 4, 3 }))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // For 90-degree counter-clockwise rotation: (i,j) -> (m-1-j, i), so (0,0) gets value from (0,3) if (!(isApproxEqualMCO(rotated.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))) { std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(rotated.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Test rot90 with k=0 (no rotation) auto no_rot = rot90(arr, 0); if (!(no_rot.getShape() == arr.getShape())) { std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(no_rot.getShape() == arr.getShape())"; .. _example-slice-length-2: .. dropdown:: length (np_test_1_all.cpp:7335) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 7325 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void testStringTruncationStringTypes() { std::cout << "========= testStringTruncationStringTypes ======================="; // Test truncation behavior str8 small("This string is way too long for 8 characters"); // std::cout << "Long string in str8: '" << small << "'" << std::endl; // std::cout << "Length: " << small.length() << " (max: " << str8::max_size << ")" << std::endl; str16 medium("This is a medium length string that should be truncated"); // std::cout << "Medium string in str16: '" << medium << "'" << std::endl; // std::cout << "Length: " << medium.length() << " (max: " << str16::max_size << ")" << std::endl; std::cout << " -> tests passed" << std::endl; } void testStringComparisonsStringTypes() { std::cout << "========= testStringComparisonsStringTypes ======================="; .. _example-slice-range-3: .. dropdown:: range (np_test_1_all.cpp:9254) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9244 :emphasize-lines: 11 std::string description = std::string("testNullCharacterHandlingVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!unicode_null_str.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } std::cout << " -> tests passed" << std::endl; } void testUTF8BoundariesVariableStrings() { std::cout << "========= testUTF8BoundariesVariableStrings ======================="; // Test ASCII range (0x00-0x7F) vunicode_ ascii_min("\x01"); // Skip NULL for this test vunicode_ ascii_max("\x7F"); if (!(ascii_min.char_count() == 1)) { std::string description = std::string("testUTF8BoundariesVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ascii_min.char_count() == 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(ascii_max.char_count() == 1)) { std::string description = std::string("testUTF8BoundariesVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ascii_max.char_count() == 1)"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-slice-to-4: .. dropdown:: to (np_test_1_all.cpp:13713) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 13703 :emphasize-lines: 11 // std::cout << "[OK] Complex stride pattern maintains memory sharing"; std::cout << " -> tests passed" << std::endl; } void testStrideWithBroadcasting() { std::cout << "========= testStrideWithBroadcasting ======================="; // Test broadcasting with different stride patterns auto array1 = createInt32Array({ 3, 4 }, 5); auto array2 = createInt32Array({ 4 }, 2); // Will be broadcast to (1, 4) then (3, 4) // std::cout << "Array1 (3x4):" << std::endl; //array1.printArray(); // std::cout << "Array1 strides: (" << array1.getStrides()[0] << ", " << array1.getStrides()[1] << ")" << std::endl; // std::cout << "Array2 (4,):" << std::endl; //array2.printArray(); // std::cout << "Array2 strides: (" << array2.getStrides()[0] << ")" << std::endl; // This should broadcast array2 to match array1's shape