Expanding#

class numpy::Expanding#

numpy C++ class.

Example#

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

// Use Expanding
Expanding obj;
// ... operations ...

Constructors#

Signature

Location

Example

Expanding(const Series<T>& series, size_t min_periods = 1)

df_expanding.h:45

Indexing / Selection#

Signature

Return Type

Location

Example

double get_value(size_t i) const

double

df_expanding.h:282

Statistics#

Signature

Return Type

Location

Example

Series<double> max() const

Series<double>

df_expanding.h:143

View

Series<double> mean() const

Series<double>

df_expanding.h:76

View

Series<double> median() const

Series<double>

df_expanding.h:223

View

Series<double> min() const

Series<double>

df_expanding.h:120

View

Series<double> std\_(int ddof = 1) const

Series<double>

df_expanding.h:166

Series<double> sum() const

Series<double>

df_expanding.h:53

View

Series<double> var(int ddof = 1) const

Series<double>

df_expanding.h:195

View

Other Methods#

Signature

Return Type

Location

Example

Series<double> apply(Func&& func) const

Series<double>

df_expanding.h:257

Series<double> count() const

Series<double>

df_expanding.h:99

View

Code Examples#

The following examples are extracted from the test suite.

max (np_test_1_all.cpp:7274)
7264    if (sizeof(uintp) == sizeof(void*)) {
7265        // std::cout << "                -> uintp size matches pointer size";
7266    } else {
7267        // std::cout << "  ✗ uintp size doesn't match pointer size" << std::endl;
7268    }
7269
7270    // Test range limits
7271    // std::cout << "Range Information:" << std::endl;
7272    // std::cout << "  intp min: " << std::numeric_limits<intp>::min() << std::endl;
7273    // std::cout << "  intp max: " << std::numeric_limits<intp>::max() << std::endl;
7274    // std::cout << "  uintp max: " << std::numeric_limits<uintp>::max() << std::endl;
7275    // std::cout << "  longdouble digits: " << std::numeric_limits<longdouble>::digits << std::endl;
7276
7277    std::cout << " -> tests passed" << std::endl;
7278}
7279
7280void testComplexArithmeticExtendedTypes() {
7281    std::cout << "========= testComplexArithmeticExtendedTypes =======================";
7282
7283    clongdouble c1(3.0L, 4.0L);  // 3 + 4i
mean (np_test_1_all.cpp:11714)
11704    // Create test array
11705    auto array = createInt32Array({ 2, 3 }, 0);
11706    array.setElementAt({ 0, 0 }, 1);
11707    array.setElementAt({ 0, 1 }, 2);
11708    array.setElementAt({ 0, 2 }, 3);
11709    array.setElementAt({ 1, 0 }, 4);
11710    array.setElementAt({ 1, 1 }, 5);
11711    array.setElementAt({ 1, 2 }, 6);
11712
11713    // Test mean without axis
11714    auto mean_all = mean(array);
11715    if (!(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))) {
11716        std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_all.getElementAt({ 0 }), 3.5, 1e-10))";
11717        std::cout << std::string("[FAIL] ") + description << std::endl;
11718        throw std::runtime_error(description);
11719    }
11720    // std::cout << "[OK] mean (all elements) works correctly\n";
11721
11722    // Test mean along axis 0
11723    auto mean_axis0 = mean(array, 0);
11724    if (!(mean_axis0.getShape()[0] == 3)) {
median (np_test_1_all.cpp:11882)
11872    std::cout << "========= testMedianAndPercentiles =======================";
11873
11874    // Test median with odd number of elements
11875    auto odd_array = createInt32Array({ 5 }, 0);
11876    odd_array.setElementAt({ 0 }, 1);
11877    odd_array.setElementAt({ 1 }, 3);
11878    odd_array.setElementAt({ 2 }, 5);
11879    odd_array.setElementAt({ 3 }, 7);
11880    odd_array.setElementAt({ 4 }, 9);
11881
11882    auto median_odd = median(odd_array);
11883    if (!(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))) {
11884        std::string description = std::string("testMedianAndPercentiles():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(median_odd.getElementAt({ 0 }), 5.0, 1e-10))";
11885        std::cout << std::string("[FAIL] ") + description << std::endl;
11886        throw std::runtime_error(description);
11887    }
11888    // std::cout << "[OK] Median with odd count works correctly\n";
11889
11890    // Test median with even number of elements
11891    auto even_array = createInt32Array({ 4 }, 0);
11892    even_array.setElementAt({ 0 }, 1);
min (np_test_1_all.cpp:2350)
2340        if (i % 3 == 0) {
2341            large_array.setElementAt({i}, object_(static_cast<int>(i)));
2342        } else if (i % 3 == 1) {
2343            large_array.setElementAt({i}, object_(static_cast<double>(i) * 0.5));
2344        } else {
2345            large_array.setElementAt({i}, object_(std::string("str") + std::to_string(i)));
2346        }
2347    }
2348
2349    // Verify pattern
2350    for (size_t i = 0; i < std::min(large_size, size_t(100)); ++i) {  // Check first 100
2351        object_ obj = large_array.getElementAt({i});
2352        if (i % 3 == 0) {
2353            if (!(obj.is_type<int>())) {
2354                std::string description = std::string("testArrayEdgeCases():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<int>())";
2355                std::cout << std::string("[FAIL] ") + description << std::endl;
2356                throw std::runtime_error(description);
2357            }
2358        } else if (i % 3 == 1) {
2359            if (!(obj.is_type<double>())) {
2360                std::string description = std::string("unknown_function():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(obj.is_type<double>())";
sum (np_test_1_all.cpp:11766)
11756        throw std::runtime_error(description);
11757    }
11758    if (!(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))) {
11759        std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(mean_axis1.getElementAt({ 1 }), 5.0, 1e-10))";
11760        std::cout << std::string("[FAIL] ") + description << std::endl;
11761        throw std::runtime_error(description);
11762    }
11763    // std::cout << "[OK] mean along axis 1 works correctly\n";
11764
11765    // Test sum
11766    auto sum_all = sum(array);
11767    if (!(sum_all.getElementAt({ 0 }) == 21)) {
11768        std::string description = std::string("testBasicStatistics():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(sum_all.getElementAt({ 0 }) == 21)";
11769        std::cout << std::string("[FAIL] ") + description << std::endl;
11770        throw std::runtime_error(description);
11771    }
11772    // std::cout << "[OK] sum works correctly\n";
11773
11774    // Test min and max
11775    auto min_all = min(array);
11776    auto max_all = max(array);
var (np_test_1_all.cpp:11816)
11806    std::cout << "========= testVarianceAndStandardDeviation =======================";
11807
11808    // Create test array with known variance
11809    auto array = createFloat64Array({ 4 }, 0);
11810    array.setElementAt({ 0 }, 1.0);
11811    array.setElementAt({ 1 }, 2.0);
11812    array.setElementAt({ 2 }, 3.0);
11813    array.setElementAt({ 3 }, 4.0);
11814
11815    // Test variance (population)
11816    auto var_result = var(array, std::nullopt, 0);  // ddof=0 for population variance
11817    double expected_var = 1.25;  // Known variance for [1,2,3,4]
11818    if (!(approx_equal(var_result.getElementAt({ 0 }), expected_var, 1e-10))) {
11819        std::string description = std::string("testVarianceAndStandardDeviation():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(approx_equal(var_result.getElementAt({ 0 }), expected_var, 1e-10))";
11820        std::cout << std::string("[FAIL] ") + description << std::endl;
11821        throw std::runtime_error(description);
11822    }
11823    // std::cout << "[OK] Population variance works correctly\n";
11824
11825    // Test variance (sample)
11826    auto var_sample = var(array, std::nullopt, 1);  // ddof=1 for sample variance
count (np_test_1_all.cpp:3616)
3606    // Create larger arrays for performance testing
3607    auto large_arr = NDArray<double>::createOnes({100, 100});
3608    auto broadcast_arr = NDArray<double>::createOnes({1, 100});
3609
3610    // Time the operation (basic timing)
3611    auto start = std::chrono::high_resolution_clock::now();
3612    auto result = large_arr.addArrays(broadcast_arr);
3613    auto end = std::chrono::high_resolution_clock::now();
3614
3615    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
3616    // std::cout << "Large array broadcasting took " << duration.count() << " microseconds" << std::endl;
3617
3618    // Verify result shape
3619    if (!((result.getShape() == std::vector<size_t>{100, 100}))) {
3620        std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !((result.getShape() == std::vector<size_t>{100, 100}))";
3621        std::cout << std::string("[FAIL] ") + description << std::endl;
3622        throw std::runtime_error(description);
3623    }
3624    if (!(result.getElementAt({50, 50}) == 2.0)) {
3625        std::string description = std::string("test_broadcasting_performance():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(result.getElementAt({50, 50}) == 2.0)";
3626        std::cout << std::string("[FAIL] ") + description << std::endl;