Slice#

class numpy::Slice#

Core array class in the numpy namespace.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use Slice
Slice obj;
// ... operations ...

Constructors#

Signature

Location

Example

Slice(std::optional<ssize_t>stop\_)

NP_INDEX_TYPES.H:30

Slice(std::optional<ssize_t>start\_, std::optional<ssize_t>stop\_)

NP_INDEX_TYPES.H:31

Slice(std::optional<ssize_t>start\_, std::optional<ssize_t>stop\_, std::optional<ssize_t>step\_)

NP_INDEX_TYPES.H:33

Logical#

Signature

Return Type

Location

Example

Slice all()

Slice

NP_INDEX_TYPES.H:37

View

Other Methods#

Signature

Return Type

Location

Example

Slice from(ssize_tstart\_)

Slice

NP_INDEX_TYPES.H:38

View

size_t length(size_tdim_size)

size_t

NP_INDEX_TYPES.H:81

View

Slice range(ssize_tstart\_, ssize_tstop\_)

Slice

NP_INDEX_TYPES.H:40

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

View

Code Examples#

The following examples are extracted from the test suite.

all (np_test_4_all.cpp:23928)
23918                        }
23919                    }
23920                }
23921
23922                auto result = numpy::einsum<numpy::float64>("ijk->ik", {A});
23923
23924                if (result.getShape()[0] != 2 || result.getShape()[1] != 2) {
23925                    throw std::runtime_error("einsum partial sum shape wrong");
23926                }
23927
23928                // Sum over j: 1+2+3 = 6 for all (i,k) positions
23929                if (std::abs(result.getElementAt({0, 0}) - 6.0) > 1e-10 ||
23930                    std::abs(result.getElementAt({1, 1}) - 6.0) > 1e-10) {
23931                    throw std::runtime_error("einsum partial sum values wrong");
23932                }
23933
23934                // std::cout << "  OK: Partial sum: 'ijk->ik'\n";
23935            }
23936
23937            // Test 5: Size-1 dimension handling
23938            {
from (np_test_2_all.cpp:3651)
3641          arr.setElementAt({ i, j }, static_cast<double>(i * 4 + j));
3642        }
3643      }
3644
3645      auto rotated = rot90(arr, 1);
3646      if (!(rotated.getShape() == std::vector<size_t>({ 4, 3 }))) {
3647          std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(rotated.getShape() == std::vector<size_t>({ 4, 3 }))";
3648          std::cout << std::string("[FAIL] ") + description << std::endl;
3649          throw std::runtime_error(description);
3650      }
3651      // For 90-degree counter-clockwise rotation: (i,j) -> (m-1-j, i), so (0,0) gets value from (0,3)
3652      if (!(isApproxEqualMCO(rotated.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))) {
3653          std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(isApproxEqualMCO(rotated.getElementAt({ 0, 0 }), arr.getElementAt({ 0, 3 })))";
3654          std::cout << std::string("[FAIL] ") + description << std::endl;
3655          throw std::runtime_error(description);
3656      }
3657
3658      // Test rot90 with k=0 (no rotation)
3659      auto no_rot = rot90(arr, 0);
3660      if (!(no_rot.getShape() == arr.getShape())) {
3661          std::string description = std::string("testRotationFlipOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(no_rot.getShape() == arr.getShape())";
length (np_test_1_all.cpp:7335)
7325    std::cout << " -> tests passed" << std::endl;
7326}
7327
7328void testStringTruncationStringTypes() {
7329    std::cout << "========= testStringTruncationStringTypes =======================";
7330
7331    // Test truncation behavior
7332    str8 small("This string is way too long for 8 characters");
7333    // std::cout << "Long string in str8: '" << small << "'" << std::endl;
7334    // std::cout << "Length: " << small.length() << " (max: " << str8::max_size << ")" << std::endl;
7335
7336    str16 medium("This is a medium length string that should be truncated");
7337    // std::cout << "Medium string in str16: '" << medium << "'" << std::endl;
7338    // std::cout << "Length: " << medium.length() << " (max: " << str16::max_size << ")" << std::endl;
7339
7340    std::cout << " -> tests passed" << std::endl;
7341}
7342
7343void testStringComparisonsStringTypes() {
7344    std::cout << "========= testStringComparisonsStringTypes =======================";
range (np_test_1_all.cpp:9254)
9244        std::string description = std::string("testNullCharacterHandlingVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(!unicode_null_str.empty())";
9245        std::cout << std::string("[FAIL] ") + description << std::endl;
9246        throw std::runtime_error(description);
9247    }
9248    std::cout << " -> tests passed" << std::endl;
9249}
9250
9251void testUTF8BoundariesVariableStrings() {
9252    std::cout << "========= testUTF8BoundariesVariableStrings =======================";
9253
9254    // Test ASCII range (0x00-0x7F)
9255    vunicode_ ascii_min("\x01");  // Skip NULL for this test
9256    vunicode_ ascii_max("\x7F");
9257    if (!(ascii_min.char_count() == 1)) {
9258        std::string description = std::string("testUTF8BoundariesVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ascii_min.char_count() == 1)";
9259        std::cout << std::string("[FAIL] ") + description << std::endl;
9260        throw std::runtime_error(description);
9261    }
9262    if (!(ascii_max.char_count() == 1)) {
9263        std::string description = std::string("testUTF8BoundariesVariableStrings():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(ascii_max.char_count() == 1)";
9264        std::cout << std::string("[FAIL] ") + description << std::endl;
to (np_test_1_all.cpp:13713)
13703    // std::cout << "[OK] Complex stride pattern maintains memory sharing";
13704
13705    std::cout << " -> tests passed" << std::endl;
13706  }
13707
13708  void testStrideWithBroadcasting() {
13709    std::cout << "========= testStrideWithBroadcasting =======================";
13710
13711    // Test broadcasting with different stride patterns
13712    auto array1 = createInt32Array({ 3, 4 }, 5);
13713    auto array2 = createInt32Array({ 4 }, 2);  // Will be broadcast to (1, 4) then (3, 4)
13714
13715    // std::cout << "Array1 (3x4):" << std::endl;
13716    //array1.printArray();
13717    // std::cout << "Array1 strides: (" << array1.getStrides()[0] << ", " << array1.getStrides()[1] << ")" << std::endl;
13718
13719    // std::cout << "Array2 (4,):" << std::endl;
13720    //array2.printArray();
13721    // std::cout << "Array2 strides: (" << array2.getStrides()[0] << ")" << std::endl;
13722
13723    // This should broadcast array2 to match array1's shape