PrintOptionsManager =================== .. cpp:class:: numpy::PrintOptionsManager numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use PrintOptionsManager PrintOptionsManager obj; // ... operations ... Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``PrintOptions get()`` - PrintOptions - NP_IO_STRING.H:67 - :ref:`View ` * - ``PrintOptions get()`` - PrintOptions - NP_PRINT_CONFIG.H:39 - :ref:`View ` * - ``std::function&,DType)> get_repr_function()`` - std::function&,DType)> - NP_IO_STRING.H:92 - :ref:`View ` * - ``std::function&,DType)> get_str_function()`` - std::function&,DType)> - NP_IO_STRING.H:97 - Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void pop()`` - void - NP_PRINT_CONFIG.H:58 - * - ``void push(const PrintOptions &opts)`` - void - NP_PRINT_CONFIG.H:51 - * - ``void reset()`` - void - NP_IO_STRING.H:77 - :ref:`View ` * - ``void set(const PrintOptions &opts)`` - void - NP_IO_STRING.H:72 - :ref:`View ` * - ``void set(const PrintOptions &opts)`` - void - NP_PRINT_CONFIG.H:45 - :ref:`View ` * - ``void set_repr_function(std::function&,DType)>func)`` - void - NP_IO_STRING.H:82 - :ref:`View ` * - ``void set_str_function(std::function&,DType)>func)`` - void - NP_IO_STRING.H:87 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-printoptionsmanager-get-0: .. dropdown:: get (np_test_1_all.cpp:28526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28516 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_indexing_mask_indices() { std::cout << "========= mask_indices: triangular mask indices ======================="; // Get upper triangular indices for 3x3 matrix auto triu_idx = numpy::mask_indices(3, "triu", 0); // Should return tuple of 2 arrays bool passed = (std::get<0>(triu_idx).getSize() > 0); passed = passed && (std::get<1>(triu_idx).getSize() > 0); passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize()); // Get lower triangular indices auto tril_idx = numpy::mask_indices(3, "tril", 0); passed = passed && (std::get<0>(tril_idx).getSize() > 0); passed = passed && (std::get<1>(tril_idx).getSize() > 0); if (!passed) { std::cout << " [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect"; .. _example-printoptionsmanager-get-1: .. dropdown:: get (np_test_1_all.cpp:28526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28516 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_indexing_mask_indices() { std::cout << "========= mask_indices: triangular mask indices ======================="; // Get upper triangular indices for 3x3 matrix auto triu_idx = numpy::mask_indices(3, "triu", 0); // Should return tuple of 2 arrays bool passed = (std::get<0>(triu_idx).getSize() > 0); passed = passed && (std::get<1>(triu_idx).getSize() > 0); passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize()); // Get lower triangular indices auto tril_idx = numpy::mask_indices(3, "tril", 0); passed = passed && (std::get<0>(tril_idx).getSize() > 0); passed = passed && (std::get<1>(tril_idx).getSize() > 0); if (!passed) { std::cout << " [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect"; .. _example-printoptionsmanager-get_repr_function-2: .. dropdown:: get_repr_function (np_test_3_all.cpp:10321) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10311 :emphasize-lines: 11 } // ============================ // TEST: Custom String Function // ============================ void np_test_io_string_custom_function() { std::cout << "========= np_test_io_string_custom_function ======================="; // Save original repr function auto original_repr = PrintOptionsManager::get_repr_function(); // Set custom repr function set_string_function([](const void* data, const std::vector& shape, DType dtype) -> std::string { return ""; }, true); NDArray arr({ 3 }); arr.setElementAt({ 0 }, 1); arr.setElementAt({ 1 }, 2); arr.setElementAt({ 2 }, 3); .. _example-printoptionsmanager-reset-3: .. dropdown:: reset (np_test_2_all.cpp:22610) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 22600 :emphasize-lines: 11 // Count iterations size_t outer_count = 0; size_t total_inner_count = 0; for (; !outer.is_finished(); ++outer) { size_t inner_count = 0; for (; !inner.is_finished(); ++inner) { inner_count++; total_inner_count++; } outer_count++; inner.reset(); } if (outer_count != 3 || total_inner_count != 12) { std::cout << " [FAIL] : Iteration counts incorrect"; throw std::runtime_error("np_test_nested_iters_basic failed"); } std::cout << " -> tests passed" << std::endl; } .. _example-printoptionsmanager-set-4: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } .. _example-printoptionsmanager-set-5: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } .. _example-printoptionsmanager-set_repr_function-6: .. dropdown:: set_repr_function (np_test_3_all.cpp:10342) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10332 :emphasize-lines: 11 std::string result = array_repr(arr); // std::cout << "Custom repr result: " << result << std::endl; if (!(result == "")) { std::string description = std::string("np_test_io_string_custom_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result == \"\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // Restore original function PrintOptionsManager::set_repr_function(original_repr); // Verify restoration result = array_repr(arr); // std::cout << "After restore: " << result << std::endl; if (!(result != "")) { std::string description = std::string("np_test_io_string_custom_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result != \"\")"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); }