PrintOptionsManager#

class numpy::PrintOptionsManager#

numpy C++ class.

Example#

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

// Use PrintOptionsManager
PrintOptionsManager obj;
// ... operations ...

Indexing / Selection#

Signature

Return Type

Location

Example

PrintOptions get()

PrintOptions

NP_IO_STRING.H:67

View

PrintOptions get()

PrintOptions

NP_PRINT_CONFIG.H:39

View

std::function<std::string(const void\*,const std::vector<size_t>&,DType)> get_repr_function()

std::function<std::string(const void*,const std::vector<size_t>&,DType)>

NP_IO_STRING.H:92

View

std::function<std::string(const void\*,const std::vector<size_t>&,DType)> get_str_function()

std::function<std::string(const void*,const std::vector<size_t>&,DType)>

NP_IO_STRING.H:97

Other Methods#

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

View

void set(const PrintOptions &opts)

void

NP_IO_STRING.H:72

View

void set(const PrintOptions &opts)

void

NP_PRINT_CONFIG.H:45

View

void set_repr_function(std::function<std::string(const void\*,const std::vector<size_t>&,DType)>func)

void

NP_IO_STRING.H:82

View

void set_str_function(std::function<std::string(const void\*,const std::vector<size_t>&,DType)>func)

void

NP_IO_STRING.H:87

Code Examples#

The following examples are extracted from the test suite.

get (np_test_1_all.cpp:28526)
28516      std::cout << " -> tests passed" << std::endl;
28517    }
28518
28519    void np_test_indexing_mask_indices() {
28520      std::cout << "========= mask_indices: triangular mask indices =======================";
28521
28522      // Get upper triangular indices for 3x3 matrix
28523      auto triu_idx = numpy::mask_indices(3, "triu", 0);
28524
28525      // Should return tuple of 2 arrays
28526      bool passed = (std::get<0>(triu_idx).getSize() > 0);
28527      passed = passed && (std::get<1>(triu_idx).getSize() > 0);
28528      passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize());
28529
28530      // Get lower triangular indices
28531      auto tril_idx = numpy::mask_indices(3, "tril", 0);
28532      passed = passed && (std::get<0>(tril_idx).getSize() > 0);
28533      passed = passed && (std::get<1>(tril_idx).getSize() > 0);
28534
28535      if (!passed) {
28536        std::cout << "  [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect";
get (np_test_1_all.cpp:28526)
28516      std::cout << " -> tests passed" << std::endl;
28517    }
28518
28519    void np_test_indexing_mask_indices() {
28520      std::cout << "========= mask_indices: triangular mask indices =======================";
28521
28522      // Get upper triangular indices for 3x3 matrix
28523      auto triu_idx = numpy::mask_indices(3, "triu", 0);
28524
28525      // Should return tuple of 2 arrays
28526      bool passed = (std::get<0>(triu_idx).getSize() > 0);
28527      passed = passed && (std::get<1>(triu_idx).getSize() > 0);
28528      passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize());
28529
28530      // Get lower triangular indices
28531      auto tril_idx = numpy::mask_indices(3, "tril", 0);
28532      passed = passed && (std::get<0>(tril_idx).getSize() > 0);
28533      passed = passed && (std::get<1>(tril_idx).getSize() > 0);
28534
28535      if (!passed) {
28536        std::cout << "  [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect";
get_repr_function (np_test_3_all.cpp:10321)
10311    }
10312
10313    // ============================
10314    // TEST: Custom String Function
10315    // ============================
10316
10317    void np_test_io_string_custom_function() {
10318      std::cout << "========= np_test_io_string_custom_function =======================";
10319
10320      // Save original repr function
10321      auto original_repr = PrintOptionsManager::get_repr_function();
10322
10323      // Set custom repr function
10324      set_string_function([](const void* data, const std::vector<size_t>& shape, DType dtype) -> std::string {
10325        return "<CustomArray>";
10326        }, true);
10327
10328      NDArray<int> arr({ 3 });
10329      arr.setElementAt({ 0 }, 1);
10330      arr.setElementAt({ 1 }, 2);
10331      arr.setElementAt({ 2 }, 3);
reset (np_test_2_all.cpp:22610)
22600      // Count iterations
22601      size_t outer_count = 0;
22602      size_t total_inner_count = 0;
22603      for (; !outer.is_finished(); ++outer) {
22604        size_t inner_count = 0;
22605        for (; !inner.is_finished(); ++inner) {
22606          inner_count++;
22607          total_inner_count++;
22608        }
22609        outer_count++;
22610        inner.reset();
22611      }
22612
22613      if (outer_count != 3 || total_inner_count != 12) {
22614        std::cout << "  [FAIL] : Iteration counts incorrect";
22615        throw std::runtime_error("np_test_nested_iters_basic failed");
22616      }
22617
22618      std::cout << " -> tests passed" << std::endl;
22619    }
set (np_test_1_all.cpp:6747)
6737    std::cout << "========= testArrayPrinting =======================";
6738
6739    // Test 1D array printing
6740    std::vector<std::string> words = {"Hello", "World", "Test"};
6741    auto arr_1d = array<16>(words);
6742    // std::cout << "1D CharArray:" << std::endl;
6743    // arr_1d.print();
6744
6745    // Test 2D array printing
6746    chararray16 arr_2d({2, 2}, "test");
6747    arr_2d.set({0, 0}, "A");
6748    arr_2d.set({0, 1}, "B");
6749    arr_2d.set({1, 0}, "C");
6750    arr_2d.set({1, 1}, "D");
6751
6752    // std::cout << "2D CharArray:" << std::endl;
6753    // arr_2d.print();
6754
6755    std::cout << " -> tests passed" << std::endl;
6756}
set (np_test_1_all.cpp:6747)
6737    std::cout << "========= testArrayPrinting =======================";
6738
6739    // Test 1D array printing
6740    std::vector<std::string> words = {"Hello", "World", "Test"};
6741    auto arr_1d = array<16>(words);
6742    // std::cout << "1D CharArray:" << std::endl;
6743    // arr_1d.print();
6744
6745    // Test 2D array printing
6746    chararray16 arr_2d({2, 2}, "test");
6747    arr_2d.set({0, 0}, "A");
6748    arr_2d.set({0, 1}, "B");
6749    arr_2d.set({1, 0}, "C");
6750    arr_2d.set({1, 1}, "D");
6751
6752    // std::cout << "2D CharArray:" << std::endl;
6753    // arr_2d.print();
6754
6755    std::cout << " -> tests passed" << std::endl;
6756}
set_repr_function (np_test_3_all.cpp:10342)
10332      std::string result = array_repr(arr);
10333      // std::cout << "Custom repr result: " << result << std::endl;
10334      if (!(result == "<CustomArray>")) {
10335          std::string description = std::string("np_test_io_string_custom_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result == \"<CustomArray>\")";
10336          std::cout << std::string("[FAIL] ") + description << std::endl;
10337          throw std::runtime_error(description);
10338      }
10339
10340      // Restore original function
10341      PrintOptionsManager::set_repr_function(original_repr);
10342
10343      // Verify restoration
10344      result = array_repr(arr);
10345      // std::cout << "After restore: " << result << std::endl;
10346      if (!(result != "<CustomArray>")) {
10347          std::string description = std::string("np_test_io_string_custom_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result != \"<CustomArray>\")";
10348          std::cout << std::string("[FAIL] ") + description << std::endl;
10349          throw std::runtime_error(description);
10350      }