SparseDtype#
-
class numpy::SparseDtype#
numpy C++ class.
Example#
#include <numpy/np_ndarray.h>
using namespace numpy;
// Use SparseDtype
SparseDtype obj;
// ... operations ...
Indexing / Selection#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
size_t |
df_sparse_dtype.h:120 |
Type Checking#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
bool |
df_sparse_dtype.h:182 |
Other Methods#
Signature |
Return Type |
Location |
Example |
|---|---|---|---|
|
static T |
df_sparse_dtype.h:78 |
|
|
T |
df_sparse_dtype.h:136 |
|
|
SparseDtype() : |
df_sparse_dtype.h:64 |
|
|
explicit SparseDtype(T fill_value) : |
df_sparse_dtype.h:70 |
|
|
std::string |
df_sparse_dtype.h:128 |
|
|
std::string |
df_sparse_dtype.h:92 |
|
|
numpy::DType |
df_sparse_dtype.h:113 |
|
|
std::string |
df_sparse_dtype.h:175 |
|
|
std::string |
df_sparse_dtype.h:144 |
|
|
const std::type_info& |
df_sparse_dtype.h:151 |
Code Examples#
The following examples are extracted from the test suite.
itemsize (np_test_2_all.cpp:3540)
3530 }
3531
3532 // Test size
3533 if (!(size(arr) == 24)) {
3534 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(size(arr) == 24)";
3535 std::cout << std::string("[FAIL] ") + description << std::endl;
3536 throw std::runtime_error(description);
3537 }
3538
3539 // Test itemsize
3540 if (!(itemsize(arr) == sizeof(double))) {
3541 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(itemsize(arr) == sizeof(double))";
3542 std::cout << std::string("[FAIL] ") + description << std::endl;
3543 throw std::runtime_error(description);
3544 }
3545
3546 // Test nbytes
3547 if (!(nbytes(arr) == 24 * sizeof(double))) {
3548 std::string description = std::string("testArrayInfo():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(nbytes(arr) == 24 * sizeof(double))";
3549 std::cout << std::string("[FAIL] ") + description << std::endl;
3550 throw std::runtime_error(description);
default_fill_value (np_test_1_all.cpp:27637)
27627 // std::cout << " ma::isMA(regular_array): " << (numpy::ma::isMA(regular_array) ? "true" : "false") << " (expected: false)" << std::endl;
27628 std::cout << " -> tests passed" << std::endl;
27629 }
27630
27631 void test_fill_value_functions() {
27632 std::cout << "========= Fill value query functions ===";
27633
27634 int int_val = 42;
27635 double double_val = 3.14;
27636
27637 int default_int = numpy::ma::default_fill_value(int_val);
27638 double default_double = numpy::ma::default_fill_value(double_val);
27639
27640 // std::cout << " ma::default_fill_value(int): " << default_int << " (expected: 999999)" << std::endl;
27641 // std::cout << " ma::default_fill_value(double): " << default_double << " (expected: 1e+20)" << std::endl;
27642
27643 int max_int = numpy::ma::maximum_fill_value(int_val);
27644 double max_double = numpy::ma::maximum_fill_value(double_val);
27645
27646 // std::cout << " ma::maximum_fill_value(int): " << max_int << " (expected: INT_MAX)" << std::endl;
27647 // std::cout << " ma::maximum_fill_value(double): " << (std::isinf(max_double) ? "inf" : "finite") << " (expected: inf)" << std::endl;
fill_value (np_test_4_all.cpp:20577)
20567 }
20568
20569 // std::cout << "OK: masked_all_like(NDArray) test passed" << std::endl;
20570 }
20571
20572 // Test with custom fill value
20573 {
20574 NDArray<int> data({2, 2});
20575 auto masked = ma::masked_all_like(data, 999);
20576
20577 if (!(masked.fill_value() == 999)) {
20578 std::string description = std::string("np_test_phase6_ma_masked_all_like():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(masked.fill_value() == 999)";
20579 std::cout << std::string("[FAIL] ") + description << std::endl;
20580 throw std::runtime_error(description);
20581 }
20582
20583 // All elements should have fill_value
20584 for (size_t i = 0; i < 2; ++i) {
20585 for (size_t j = 0; j < 2; ++j) {
20586 if (!(masked.data().getElementAt({i, j}) == 999)) {
20587 std::string description = std::string("np_test_phase6_ma_masked_all_like():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(masked.data().getElementAt({i, j}) == 999)";
name (np_test_1_all.cpp:24865)
24855 uint64_t before_restore = rng.next_uint64();
24856 rng.set_state(state);
24857 uint64_t after_restore = rng.next_uint64();
24858
24859 if (before_restore != after_restore) {
24860 std::cout << " [FAIL] : in np_test_bitgen_mt19937() : state restore failed";
24861 throw std::runtime_error("np_test_bitgen_mt19937 failed: state");
24862 }
24863
24864 // Test name
24865 if (rng.name() != "MT19937") {
24866 std::cout << " [FAIL] : in np_test_bitgen_mt19937() : incorrect name";
24867 throw std::runtime_error("np_test_bitgen_mt19937 failed: name");
24868 }
24869
24870 std::cout << " -> tests passed" << std::endl;
24871 }
24872
24873 // ============================================================================
24874 // PCG64 TESTS
24875 // ============================================================================
type (np_test_1_all.cpp:2240)
2230 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 1}).is_type<std::string>())";
2231 std::cout << std::string("[FAIL] ") + description << std::endl;
2232 throw std::runtime_error(description);
2233 }
2234 if (!(multi_array.getElementAt({0, 2}).is_type<double>())) {
2235 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(multi_array.getElementAt({0, 2}).is_type<double>())";
2236 std::cout << std::string("[FAIL] ") + description << std::endl;
2237 throw std::runtime_error(description);
2238 }
2239
2240 // Test 4: Arrays with all same type (homogeneous)
2241 NDArray<object_> homo_array = createObjectZeros({5});
2242 for (size_t i = 0; i < 5; ++i) {
2243 homo_array.setElementAt({i}, object_(static_cast<int>(i * 10)));
2244 }
2245
2246 // Verify all are same type
2247 for (size_t i = 0; i < 5; ++i) {
2248 object_ obj = homo_array.getElementAt({i});
2249 if (!(obj.is_type<int>())) {
2250 std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<int>())";