From 824bf1f869530aca063297f62ab64f1e43bf3a93 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 09:08:06 -0500 Subject: [PATCH 01/14] Replaced use of channel intel in workflows --- .github/workflows/conda-package.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/conda-package.yml b/.github/workflows/conda-package.yml index 4869ab5..84a0253 100644 --- a/.github/workflows/conda-package.yml +++ b/.github/workflows/conda-package.yml @@ -43,7 +43,7 @@ jobs: run: conda install conda-build - name: Build conda package run: | - CHANNELS="-c conda-forge -c intel --override-channels" + CHANNELS="-c conda-forge -c https://software.repos.intel.com/python/conda --override-channels" VERSIONS="--python ${{ matrix.python }}" TEST="--no-test" @@ -89,7 +89,7 @@ jobs: - name: Install conda-build run: conda install conda-build - name: Build conda package - run: conda build --no-test --python ${{ matrix.python }} -c intel -c conda-forge --override-channels conda-recipe + run: conda build --no-test --python ${{ matrix.python }} -c https://software.repos.intel.com/python/conda -c conda-forge --override-channels conda-recipe - name: Upload artifact uses: actions/upload-artifact@v4 with: @@ -107,7 +107,7 @@ jobs: runner: [ubuntu-latest] continue-on-error: ${{ matrix.experimental }} env: - CHANNELS: -c conda-forge -c intel --override-channels + CHANNELS: -c conda-forge -c https://software.repos.intel.com/python/conda --override-channels steps: - name: Download artifact @@ -174,7 +174,7 @@ jobs: runner: [windows-2019] continue-on-error: ${{ matrix.experimental }} env: - CHANNELS: -c conda-forge -c intel --override-channels + CHANNELS: -c conda-forge -c https://software.repos.intel.com/python/conda --override-channels steps: - name: Download artifact From d929a955e1be09690290cd298c667be2cce4395b Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 09:11:39 -0500 Subject: [PATCH 02/14] Updated installation steps to replace intel channel --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 64a88f5..dd82edf 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,16 @@ Per NumPy's community suggestions, voiced in https://github.com/numpy/numpy/pull/8209, it is being released as a stand-alone package. -Prebuilt `mkl_random` can be installed into conda environment from Intel's channel on Anaconda cloud: +Prebuilt `mkl_random` can be installed into conda environment from Intel's channel: ``` - conda install -c intel mkl_random + conda install -c https://software.repos.intel.com/python/conda mkl_random +``` + +or from conda forge channel: + +``` + conda install -c conda-forge mkl_random ``` --- From a53c5bff70e8dc5268e75e19699c23f691ca82e4 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 09:59:00 -0500 Subject: [PATCH 03/14] Require numpy 1.26* in test environment for now --- .github/workflows/conda-package.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/conda-package.yml b/.github/workflows/conda-package.yml index 84a0253..11f119b 100644 --- a/.github/workflows/conda-package.yml +++ b/.github/workflows/conda-package.yml @@ -103,6 +103,7 @@ jobs: strategy: matrix: python: ['3.9', '3.10'] + numpy: ['1.26*'] experimental: [false] runner: [ubuntu-latest] continue-on-error: ${{ matrix.experimental }} @@ -132,7 +133,7 @@ jobs: . $CONDA/etc/profile.d/conda.sh CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}" export PACKAGE_VERSION=$(python -c "${VER_SCRIPT1} ${VER_SCRIPT2}") - conda create -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME=${PACKAGE_VERSION} python=${{ matrix.python }} $CHANNELS --only-deps --dry-run > lockfile + conda create -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME=${PACKAGE_VERSION} python=${{ matrix.python }} numpy=${{ matrix.numpy }} $CHANNELS --only-deps --dry-run > lockfile cat lockfile - name: Set pkgs_dirs run: | @@ -154,7 +155,7 @@ jobs: . $CONDA/etc/profile.d/conda.sh CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}" export PACKAGE_VERSION=$(python -c "${VER_SCRIPT1} ${VER_SCRIPT2}") - conda create -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME=${PACKAGE_VERSION} pytest python=${{ matrix.python }} $CHANNELS + conda create -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME=${PACKAGE_VERSION} pytest python=${{ matrix.python }} numpy=${{ matrix.numpy }} $CHANNELS # Test installed packages conda list - name: Run tests @@ -170,6 +171,7 @@ jobs: strategy: matrix: python: ['3.9', '3.10'] + numpy: ['1.26*'] experimental: [false] runner: [windows-2019] continue-on-error: ${{ matrix.experimental }} @@ -205,7 +207,7 @@ jobs: FOR /F "tokens=* USEBACKQ" %%F IN (`python -c "%SCRIPT%"`) DO ( SET PACKAGE_VERSION=%%F ) - conda create -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% python=${{ matrix.python }} -c ${{ env.GITHUB_WORKSPACE }}/channel ${{ env.CHANNELS }} --only-deps --dry-run > lockfile + conda create -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% python=${{ matrix.python }} numpy=${{ matrix.numpy }} -c ${{ env.GITHUB_WORKSPACE }}/channel ${{ env.CHANNELS }} --only-deps --dry-run > lockfile more lockfile - name: Cache conda packages uses: actions/cache@v4 @@ -227,7 +229,7 @@ jobs: FOR /F "tokens=* USEBACKQ" %%F IN (`python -c "%SCRIPT%"`) DO ( SET PACKAGE_VERSION=%%F ) - conda create -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% pytest python=${{ matrix.python }} -c ${{ env.GITHUB_WORKSPACE }}/channel ${{ env.CHANNELS }} + conda create -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% pytest python=${{ matrix.python }} numpy=${{ matrix.numpy }} -c ${{ env.GITHUB_WORKSPACE }}/channel ${{ env.CHANNELS }} # Test installed packages conda list - name: Run tests From 3bd2e339929663d31896646dc044865e664d57d6 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 10:10:48 -0500 Subject: [PATCH 04/14] Use NumPy <2 and Numpy >=2 in clang workflow --- .github/workflows/build-with-clang.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-with-clang.yml b/.github/workflows/build-with-clang.yml index ead3291..0ddc05c 100644 --- a/.github/workflows/build-with-clang.yml +++ b/.github/workflows/build-with-clang.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: python: ["3.9", "3.10", "3.11", "3.12"] - use_pre: ["", "--pre"] + numpy_build_version: ["numpy'<2'", "numpy'>=2'"] env: ONEAPI_ROOT: /opt/intel/oneapi @@ -53,7 +53,7 @@ jobs: shell: bash -l {0} run: | pip install cython setuptools pytest pytest-cov - pip install numpy ${{ matrix.use_pre }} + pip install ${{ matrix.numpy_build_version }} - name: List oneAPI folder content shell: bash -l {0} From 64d9fddefec4d9ddd12aef8a20fbdb9883e16e0b Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 11:07:37 -0500 Subject: [PATCH 05/14] Clang workflow to always test with NumPy<2 --- .github/workflows/build-with-clang.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-with-clang.yml b/.github/workflows/build-with-clang.yml index 0ddc05c..86ad416 100644 --- a/.github/workflows/build-with-clang.yml +++ b/.github/workflows/build-with-clang.yml @@ -73,4 +73,6 @@ jobs: shell: bash -l {0} run: | source /opt/intel/oneapi/setvars.sh + # Test with NumPy<2 for now + pip install numpy"<2" pytest -s -v --pyargs mkl_random From 51f8ddc1769e6d34d743ed95078ae8c68816fd87 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 13:28:03 -0500 Subject: [PATCH 06/14] Update code for newer NumPy 1. Removed use of vendored numpy.pxd, replaced it with cimport numpy to resolve runtime warning during import of change in size of broadcast struct 2. Use explicit namespace for NumPy C-API symbols imported by Cython. This caught several issues where C-API ndarray object was used where Python object of type np.ndarray was intended. 3. Resolve compilation warnings reported by Clang about ignored type qualifier during casting. Local build now passes, import does not report the runtime warning, and test suite passes. --- mkl_random/mklrand.pyx | 1168 +++++++++++++------------- mkl_random/src/mkl_distributions.cpp | 20 +- mkl_random/src/numpy.pxd | 152 ---- setup.py | 3 +- 4 files changed, 612 insertions(+), 731 deletions(-) delete mode 100644 mkl_random/src/numpy.pxd diff --git a/mkl_random/mklrand.pyx b/mkl_random/mklrand.pyx index 5176579..9bb9759 100644 --- a/mkl_random/mklrand.pyx +++ b/mkl_random/mklrand.pyx @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Copyright (c) 2017-2020, Intel Corporation +# Copyright (c) 2017-2024, Intel Corporation # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -37,7 +37,7 @@ cdef extern from "Python.h": void PyErr_Clear() -include "numpy.pxd" +cimport numpy as cnp from libc.string cimport memset, memcpy cdef extern from "math.h": @@ -47,7 +47,7 @@ cdef extern from "numpy/npy_math.h": int npy_isfinite(double x) cdef extern from "mklrand_py_helper.h": - object empty_py_bytes(npy_intp length, void **bytesVec) + object empty_py_bytes(cnp.npy_intp length, void **bytesVec) char* py_bytes_DataPtr(object b) int is_bytes_object(object b) @@ -90,100 +90,100 @@ cdef extern from "randomkit.h": cdef extern from "mkl_distributions.h": - void irk_double_vec(irk_state *state, npy_intp len, double *res) noexcept nogil - void irk_uniform_vec(irk_state *state, npy_intp len, double *res, double dlow, double dhigh) noexcept nogil - - void irk_normal_vec_BM1(irk_state *state, npy_intp len, double *res, double mean, double sigma) noexcept nogil - void irk_normal_vec_BM2(irk_state *state, npy_intp len, double *res, double mean, double sigma) noexcept nogil - void irk_normal_vec_ICDF(irk_state *state, npy_intp len, double *res, double mean, double sigma) noexcept nogil - - void irk_standard_normal_vec_BM1(irk_state *state, npy_intp len, double *res) noexcept nogil - void irk_standard_normal_vec_BM2(irk_state *state, npy_intp len, double *res) noexcept nogil - void irk_standard_normal_vec_ICDF(irk_state *state, npy_intp len, double *res) noexcept nogil - - void irk_standard_exponential_vec(irk_state *state, npy_intp len, double *res) noexcept nogil - void irk_exponential_vec(irk_state *state, npy_intp len, double *res, double scale) noexcept nogil - - void irk_standard_cauchy_vec(irk_state *state, npy_intp len, double *res) noexcept nogil - void irk_standard_gamma_vec(irk_state *state, npy_intp len, double *res, double shape) noexcept nogil - void irk_gamma_vec(irk_state *state, npy_intp len, double *res, double shape, double scale) noexcept nogil - - void irk_beta_vec(irk_state *state, npy_intp len, double *res, double p, double q) noexcept nogil - - void irk_chisquare_vec(irk_state *state, npy_intp len, double *res, double df) noexcept nogil - void irk_standard_t_vec(irk_state *state, npy_intp len, double *res, double df) noexcept nogil - - void irk_rayleigh_vec(irk_state *state, npy_intp len, double *res, double sigma) noexcept nogil - void irk_pareto_vec(irk_state *state, npy_intp len, double *res, double alp) noexcept nogil - void irk_power_vec(irk_state *state, npy_intp len, double *res, double alp) noexcept nogil - void irk_weibull_vec(irk_state *state, npy_intp len, double *res, double alp) noexcept nogil - void irk_f_vec(irk_state *state, npy_intp len, double *res, double df_num, double df_den) noexcept nogil - void irk_noncentral_chisquare_vec(irk_state *state, npy_intp len, double *res, double df, double nonc) noexcept nogil - void irk_laplace_vec(irk_state *state, npy_intp len, double *res, double loc, double scale) noexcept nogil - void irk_gumbel_vec(irk_state *state, npy_intp len, double *res, double loc, double scale) noexcept nogil - void irk_logistic_vec(irk_state *state, npy_intp len, double *res, double loc, double scale) noexcept nogil - void irk_wald_vec(irk_state *state, npy_intp len, double *res, double mean, double scale) noexcept nogil - void irk_lognormal_vec_ICDF(irk_state *state, npy_intp len, double *res, double mean, double scale) noexcept nogil - void irk_lognormal_vec_BM(irk_state *state, npy_intp len, double *res, double mean, double scale) noexcept nogil - void irk_vonmises_vec(irk_state *state, npy_intp len, double *res, double mu, double kappa) noexcept nogil - - void irk_noncentral_f_vec(irk_state *state, npy_intp len, double *res, double df_num, double df_den, double nonc) noexcept nogil - void irk_triangular_vec(irk_state *state, npy_intp len, double *res, double left, double mode, double right) noexcept nogil - - void irk_geometric_vec(irk_state *state, npy_intp len, int *res, double p) noexcept nogil - void irk_negbinomial_vec(irk_state *state, npy_intp len, int *res, double a, double p) noexcept nogil - void irk_binomial_vec(irk_state *state, npy_intp len, int *res, int n, double p) noexcept nogil - void irk_multinomial_vec(irk_state *state, npy_intp len, int *res, int n, int d, double *pvec) noexcept nogil - void irk_hypergeometric_vec(irk_state *state, npy_intp len, int *res, int ls, int ss, int ms) noexcept nogil - - void irk_poisson_vec_PTPE(irk_state *state, npy_intp len, int *res, double lam) noexcept nogil - void irk_poisson_vec_POISNORM(irk_state *state, npy_intp len, int *res, double lam) noexcept nogil - void irk_poisson_vec_V(irk_state *state, npy_intp len, int *res, double *lam_vec) noexcept nogil - - void irk_zipf_long_vec(irk_state *state, npy_intp len, long *res, double alpha) noexcept nogil - void irk_logseries_vec(irk_state *state, npy_intp len, int *res, double theta) noexcept nogil + void irk_double_vec(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + void irk_uniform_vec(irk_state *state, cnp.npy_intp len, double *res, double dlow, double dhigh) noexcept nogil + + void irk_normal_vec_BM1(irk_state *state, cnp.npy_intp len, double *res, double mean, double sigma) noexcept nogil + void irk_normal_vec_BM2(irk_state *state, cnp.npy_intp len, double *res, double mean, double sigma) noexcept nogil + void irk_normal_vec_ICDF(irk_state *state, cnp.npy_intp len, double *res, double mean, double sigma) noexcept nogil + + void irk_standard_normal_vec_BM1(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + void irk_standard_normal_vec_BM2(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + void irk_standard_normal_vec_ICDF(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + + void irk_standard_exponential_vec(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + void irk_exponential_vec(irk_state *state, cnp.npy_intp len, double *res, double scale) noexcept nogil + + void irk_standard_cauchy_vec(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil + void irk_standard_gamma_vec(irk_state *state, cnp.npy_intp len, double *res, double shape) noexcept nogil + void irk_gamma_vec(irk_state *state, cnp.npy_intp len, double *res, double shape, double scale) noexcept nogil + + void irk_beta_vec(irk_state *state, cnp.npy_intp len, double *res, double p, double q) noexcept nogil + + void irk_chisquare_vec(irk_state *state, cnp.npy_intp len, double *res, double df) noexcept nogil + void irk_standard_t_vec(irk_state *state, cnp.npy_intp len, double *res, double df) noexcept nogil + + void irk_rayleigh_vec(irk_state *state, cnp.npy_intp len, double *res, double sigma) noexcept nogil + void irk_pareto_vec(irk_state *state, cnp.npy_intp len, double *res, double alp) noexcept nogil + void irk_power_vec(irk_state *state, cnp.npy_intp len, double *res, double alp) noexcept nogil + void irk_weibull_vec(irk_state *state, cnp.npy_intp len, double *res, double alp) noexcept nogil + void irk_f_vec(irk_state *state, cnp.npy_intp len, double *res, double df_num, double df_den) noexcept nogil + void irk_noncentral_chisquare_vec(irk_state *state, cnp.npy_intp len, double *res, double df, double nonc) noexcept nogil + void irk_laplace_vec(irk_state *state, cnp.npy_intp len, double *res, double loc, double scale) noexcept nogil + void irk_gumbel_vec(irk_state *state, cnp.npy_intp len, double *res, double loc, double scale) noexcept nogil + void irk_logistic_vec(irk_state *state, cnp.npy_intp len, double *res, double loc, double scale) noexcept nogil + void irk_wald_vec(irk_state *state, cnp.npy_intp len, double *res, double mean, double scale) noexcept nogil + void irk_lognormal_vec_ICDF(irk_state *state, cnp.npy_intp len, double *res, double mean, double scale) noexcept nogil + void irk_lognormal_vec_BM(irk_state *state, cnp.npy_intp len, double *res, double mean, double scale) noexcept nogil + void irk_vonmises_vec(irk_state *state, cnp.npy_intp len, double *res, double mu, double kappa) noexcept nogil + + void irk_noncentral_f_vec(irk_state *state, cnp.npy_intp len, double *res, double df_num, double df_den, double nonc) noexcept nogil + void irk_triangular_vec(irk_state *state, cnp.npy_intp len, double *res, double left, double mode, double right) noexcept nogil + + void irk_geometric_vec(irk_state *state, cnp.npy_intp len, int *res, double p) noexcept nogil + void irk_negbinomial_vec(irk_state *state, cnp.npy_intp len, int *res, double a, double p) noexcept nogil + void irk_binomial_vec(irk_state *state, cnp.npy_intp len, int *res, int n, double p) noexcept nogil + void irk_multinomial_vec(irk_state *state, cnp.npy_intp len, int *res, int n, int d, double *pvec) noexcept nogil + void irk_hypergeometric_vec(irk_state *state, cnp.npy_intp len, int *res, int ls, int ss, int ms) noexcept nogil + + void irk_poisson_vec_PTPE(irk_state *state, cnp.npy_intp len, int *res, double lam) noexcept nogil + void irk_poisson_vec_POISNORM(irk_state *state, cnp.npy_intp len, int *res, double lam) noexcept nogil + void irk_poisson_vec_V(irk_state *state, cnp.npy_intp len, int *res, double *lam_vec) noexcept nogil + + void irk_zipf_long_vec(irk_state *state, cnp.npy_intp len, long *res, double alpha) noexcept nogil + void irk_logseries_vec(irk_state *state, cnp.npy_intp len, int *res, double theta) noexcept nogil # random integers madness - void irk_discrete_uniform_vec(irk_state *state, npy_intp len, int *res, int low, int high) noexcept nogil - void irk_discrete_uniform_long_vec(irk_state *state, npy_intp len, long *res, long low, long high) noexcept nogil - void irk_rand_bool_vec(irk_state *state, npy_intp len, npy_bool *res, npy_bool low, npy_bool high) noexcept nogil - void irk_rand_uint8_vec(irk_state *state, npy_intp len, npy_uint8 *res, npy_uint8 low, npy_uint8 high) noexcept nogil - void irk_rand_int8_vec(irk_state *state, npy_intp len, npy_int8 *res, npy_int8 low, npy_int8 high) noexcept nogil - void irk_rand_uint16_vec(irk_state *state, npy_intp len, npy_uint16 *res, npy_uint16 low, npy_uint16 high) noexcept nogil - void irk_rand_int16_vec(irk_state *state, npy_intp len, npy_int16 *res, npy_int16 low, npy_int16 high) noexcept nogil - void irk_rand_uint32_vec(irk_state *state, npy_intp len, npy_uint32 *res, npy_uint32 low, npy_uint32 high) noexcept nogil - void irk_rand_int32_vec(irk_state *state, npy_intp len, npy_int32 *res, npy_int32 low, npy_int32 high) noexcept nogil - void irk_rand_uint64_vec(irk_state *state, npy_intp len, npy_uint64 *res, npy_uint64 low, npy_uint64 high) noexcept nogil - void irk_rand_int64_vec(irk_state *state, npy_intp len, npy_int64 *res, npy_int64 low, npy_int64 high) noexcept nogil - - void irk_long_vec(irk_state *state, npy_intp len, long *res) noexcept nogil + void irk_discrete_uniform_vec(irk_state *state, cnp.npy_intp len, int *res, int low, int high) noexcept nogil + void irk_discrete_uniform_long_vec(irk_state *state, cnp.npy_intp len, long *res, long low, long high) noexcept nogil + void irk_rand_bool_vec(irk_state *state, cnp.npy_intp len, cnp.npy_bool *res, cnp.npy_bool low, cnp.npy_bool high) noexcept nogil + void irk_rand_uint8_vec(irk_state *state, cnp.npy_intp len, cnp.npy_uint8 *res, cnp.npy_uint8 low, cnp.npy_uint8 high) noexcept nogil + void irk_rand_int8_vec(irk_state *state, cnp.npy_intp len, cnp.npy_int8 *res, cnp.npy_int8 low, cnp.npy_int8 high) noexcept nogil + void irk_rand_uint16_vec(irk_state *state, cnp.npy_intp len, cnp.npy_uint16 *res, cnp.npy_uint16 low, cnp.npy_uint16 high) noexcept nogil + void irk_rand_int16_vec(irk_state *state, cnp.npy_intp len, cnp.npy_int16 *res, cnp.npy_int16 low, cnp.npy_int16 high) noexcept nogil + void irk_rand_uint32_vec(irk_state *state, cnp.npy_intp len, cnp.npy_uint32 *res, cnp.npy_uint32 low, cnp.npy_uint32 high) noexcept nogil + void irk_rand_int32_vec(irk_state *state, cnp.npy_intp len, cnp.npy_int32 *res, cnp.npy_int32 low, cnp.npy_int32 high) noexcept nogil + void irk_rand_uint64_vec(irk_state *state, cnp.npy_intp len, cnp.npy_uint64 *res, cnp.npy_uint64 low, cnp.npy_uint64 high) noexcept nogil + void irk_rand_int64_vec(irk_state *state, cnp.npy_intp len, cnp.npy_int64 *res, cnp.npy_int64 low, cnp.npy_int64 high) noexcept nogil + + void irk_long_vec(irk_state *state, cnp.npy_intp len, long *res) noexcept nogil ctypedef enum ch_st_enum: MATRIX = 0 PACKED = 1 DIAGONAL = 2 - void irk_multinormal_vec_ICDF(irk_state *state, npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil - void irk_multinormal_vec_BM1(irk_state *state, npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil - void irk_multinormal_vec_BM2(irk_state *state, npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil + void irk_multinormal_vec_ICDF(irk_state *state, cnp.npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil + void irk_multinormal_vec_BM1(irk_state *state, cnp.npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil + void irk_multinormal_vec_BM2(irk_state *state, cnp.npy_intp len, double *res, int dim, double *mean_vec, double *ch, ch_st_enum storage_mode) noexcept nogil -ctypedef void (* irk_cont0_vec)(irk_state *state, npy_intp len, double *res) noexcept nogil -ctypedef void (* irk_cont1_vec)(irk_state *state, npy_intp len, double *res, double a) noexcept nogil -ctypedef void (* irk_cont2_vec)(irk_state *state, npy_intp len, double *res, double a, double b) noexcept nogil -ctypedef void (* irk_cont3_vec)(irk_state *state, npy_intp len, double *res, double a, double b, double c) noexcept nogil +ctypedef void (* irk_cont0_vec)(irk_state *state, cnp.npy_intp len, double *res) noexcept nogil +ctypedef void (* irk_cont1_vec)(irk_state *state, cnp.npy_intp len, double *res, double a) noexcept nogil +ctypedef void (* irk_cont2_vec)(irk_state *state, cnp.npy_intp len, double *res, double a, double b) noexcept nogil +ctypedef void (* irk_cont3_vec)(irk_state *state, cnp.npy_intp len, double *res, double a, double b, double c) noexcept nogil -ctypedef void (* irk_disc0_vec)(irk_state *state, npy_intp len, int *res) noexcept nogil -ctypedef void (* irk_disc0_vec_long)(irk_state *state, npy_intp len, long *res) noexcept nogil -ctypedef void (* irk_discnp_vec)(irk_state *state, npy_intp len, int *res, int n, double a) noexcept nogil -ctypedef void (* irk_discdd_vec)(irk_state *state, npy_intp len, int *res, double n, double p) noexcept nogil -ctypedef void (* irk_discnmN_vec)(irk_state *state, npy_intp len, int *res, int n, int m, int N) noexcept nogil -ctypedef void (* irk_discd_vec)(irk_state *state, npy_intp len, int *res, double a) noexcept nogil -ctypedef void (* irk_discd_long_vec)(irk_state *state, npy_intp len, long *res, double a) noexcept nogil -ctypedef void (* irk_discdptr_vec)(irk_state *state, npy_intp len, int *res, double *a) noexcept nogil +ctypedef void (* irk_disc0_vec)(irk_state *state, cnp.npy_intp len, int *res) noexcept nogil +ctypedef void (* irk_disc0_vec_long)(irk_state *state, cnp.npy_intp len, long *res) noexcept nogil +ctypedef void (* irk_discnp_vec)(irk_state *state, cnp.npy_intp len, int *res, int n, double a) noexcept nogil +ctypedef void (* irk_discdd_vec)(irk_state *state, cnp.npy_intp len, int *res, double n, double p) noexcept nogil +ctypedef void (* irk_discnmN_vec)(irk_state *state, cnp.npy_intp len, int *res, int n, int m, int N) noexcept nogil +ctypedef void (* irk_discd_vec)(irk_state *state, cnp.npy_intp len, int *res, double a) noexcept nogil +ctypedef void (* irk_discd_long_vec)(irk_state *state, cnp.npy_intp len, long *res, double a) noexcept nogil +ctypedef void (* irk_discdptr_vec)(irk_state *state, cnp.npy_intp len, int *res, double *a) noexcept nogil -cdef int r = _import_array() +cdef int r = cnp._import_array() if (r<0): raise ImportError("Failed to import NumPy") @@ -200,16 +200,16 @@ cdef object vec_cont0_array(irk_state *state, irk_cont0_vec func, object size, object lock): cdef double *array_data cdef double res - cdef ndarray array "arrayObject" - cdef npy_intp length + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length if size is None: func(state, 1, &res) return res else: - array = np.empty(size, np.float64) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.float64) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data) @@ -219,16 +219,16 @@ cdef object vec_cont1_array_sc(irk_state *state, irk_cont1_vec func, object size object lock): cdef double *array_data cdef double res - cdef ndarray array "arrayObject" - cdef npy_intp length + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length if size is None: func(state, 1, &res, a) return res else: - array = np.empty(size, np.float64) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.float64) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, a) @@ -236,190 +236,199 @@ cdef object vec_cont1_array_sc(irk_state *state, irk_cont1_vec func, object size cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, - ndarray oa, object lock): + cnp.ndarray oa, object lock): cdef double *array_data cdef double *oa_data - cdef ndarray array "arrayObject" - cdef npy_intp i, n, imax, res_size - cdef flatiter itera - cdef broadcast multi + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp i, n, imax, res_size + cdef cnp.flatiter itera + cdef cnp.broadcast multi + cdef object arr_obj if size is None: - array = PyArray_SimpleNew(PyArray_NDIM(oa), - PyArray_DIMS(oa) , NPY_DOUBLE) - imax = PyArray_SIZE(array) - array_data = PyArray_DATA(array) - itera = PyArray_IterNew(oa) + array = cnp.PyArray_SimpleNew(cnp.PyArray_NDIM(oa), + cnp.PyArray_DIMS(oa) , cnp.NPY_DOUBLE) + imax = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) + itera = cnp.PyArray_IterNew(oa) with lock, nogil: for i from 0 <= i < imax: - func(state, 1, array_data + i, ((itera.dataptr))[0]) - PyArray_ITER_NEXT(itera) + func(state, 1, array_data + i, ((cnp.PyArray_ITER_DATA(itera)))[0]) + cnp.PyArray_ITER_NEXT(itera) + arr_obj = array else: - array = np.empty(size, np.float64) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(2, array, oa) - res_size = PyArray_SIZE(array); + array = np.empty(size, np.float64) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, array, oa) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(1, oa) + multi = cnp.PyArray_MultiIterNew(1, oa) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 0) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) func(state, n, array_data + n*i, oa_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_cont2_array_sc(irk_state *state, irk_cont2_vec func, object size, double a, double b, object lock): cdef double *array_data cdef double res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, a, b) return res else: - array = np.empty(size, np.float64) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.float64) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, a, b) return array cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, - ndarray oa, ndarray ob, object lock): + cnp.ndarray oa, cnp.ndarray ob, object lock): cdef double *array_data cdef double *oa_data cdef double *ob_data - cdef ndarray array "arrayObject" - cdef npy_intp i, n, imax, res_size - cdef broadcast multi + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp i, n, imax, res_size + cdef cnp.broadcast multi + cdef object arr_obj if size is None: - multi = PyArray_MultiIterNew(2, oa, ob) - array = PyArray_SimpleNew(multi.nd, multi.dimensions, NPY_DOUBLE) - array_data = PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, oa, ob) + array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_DOUBLE) + array_data = cnp.PyArray_DATA(array) with lock, nogil: for i from 0 <= i < multi.size: - oa_data = PyArray_MultiIter_DATA(multi, 0) - ob_data = PyArray_MultiIter_DATA(multi, 1) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) + ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], oa_data[0], ob_data[0]) - PyArray_MultiIter_NEXT(multi) + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array else: - array = np.empty(size, np.float64) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(3, array, oa, ob) - res_size = PyArray_SIZE(array); + array = np.empty(size, np.float64) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(3, array, oa, ob) + res_size = cnp.PyArray_SIZE(array); if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(2, oa, ob) + multi = cnp.PyArray_MultiIterNew(2, oa, ob) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 0) - ob_data = PyArray_MultiIter_DATA(multi, 1) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) + ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, n, array_data + n*i, oa_data[0], ob_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_cont3_array_sc(irk_state *state, irk_cont3_vec func, object size, double a, double b, double c, object lock): cdef double *array_data cdef double res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, a, b, c) return res else: - array = np.empty(size, np.float64) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.float64) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, a, b, c) return array cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, - ndarray oa, ndarray ob, ndarray oc, object lock): + cnp.ndarray oa, cnp.ndarray ob, cnp.ndarray oc, object lock): cdef double *array_data cdef double *oa_data cdef double *ob_data cdef double *oc_data - cdef ndarray array "arrayObject" - cdef npy_intp i, res_size, n, imax - cdef broadcast multi + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp i, res_size, n, imax + cdef cnp.broadcast multi + cdef object arr_obj if size is None: - multi = PyArray_MultiIterNew(3, oa, ob, oc) - array = PyArray_SimpleNew(multi.nd, multi.dimensions, NPY_DOUBLE) - array_data = PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) + array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_DOUBLE) + array_data = cnp.PyArray_DATA(array) with lock, nogil: for i from 0 <= i < multi.size: - oa_data = PyArray_MultiIter_DATA(multi, 0) - ob_data = PyArray_MultiIter_DATA(multi, 1) - oc_data = PyArray_MultiIter_DATA(multi, 2) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) + ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) + oc_data = cnp.PyArray_MultiIter_DATA(multi, 2) func(state, 1, &array_data[i], oa_data[0], ob_data[0], oc_data[0]) - PyArray_MultiIter_NEXT(multi) + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array else: - array = np.empty(size, np.float64) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(4, array, oa, + array = np.empty(size, np.float64) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(4, array, oa, ob, oc) - res_size = PyArray_SIZE(array) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(3, oa, ob, oc) + multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 0) - ob_data = PyArray_MultiIter_DATA(multi, 1) - oc_data = PyArray_MultiIter_DATA(multi, 2) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) + ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) + oc_data = cnp.PyArray_MultiIter_DATA(multi, 2) func(state, n, array_data + n*i, oa_data[0], ob_data[0], oc_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_disc0_array(irk_state *state, irk_disc0_vec func, object size, object lock): cdef int *array_data cdef int res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res) return res else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data) @@ -431,17 +440,17 @@ cdef object vec_long_disc0_array( ): cdef long *array_data cdef long res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res) return res else: - array = np.empty(size, np.uint) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.uint) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data) @@ -454,81 +463,84 @@ cdef object vec_discnp_array_sc( ): cdef int *array_data cdef int res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, n, p) return res else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, n, p) return array cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, - ndarray on, ndarray op, object lock): + cnp.ndarray on, cnp.ndarray op, object lock): cdef int *array_data - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i, n, imax, res_size + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i, n, imax, res_size cdef double *op_data cdef int *on_data - cdef broadcast multi + cdef cnp.broadcast multi + cdef object arr_obj if size is None: - multi = PyArray_MultiIterNew(2, on, op) - array = PyArray_SimpleNew(multi.nd, multi.dimensions, NPY_INT) - array_data = PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, on, op) + array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array_data = cnp.PyArray_DATA(array) with lock, nogil: for i from 0 <= i < multi.size: - on_data = PyArray_MultiIter_DATA(multi, 0) - op_data = PyArray_MultiIter_DATA(multi, 1) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) - PyArray_MultiIter_NEXT(multi) + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array else: - array = np.empty(size, np.int32) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(3, array, on, op) - res_size = PyArray_SIZE(array) + array = np.empty(size, np.int32) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(3, array, on, op) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(2, on, op) + multi = cnp.PyArray_MultiIterNew(2, on, op) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - on_data = PyArray_MultiIter_DATA(multi, 0) - op_data = PyArray_MultiIter_DATA(multi, 1) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, n, array_data + n * i, on_data[0], op_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_discdd_array_sc(irk_state *state, irk_discdd_vec func, object size, double n, double p, object lock): cdef int *array_data cdef int res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, n, p) return res else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, n, p) @@ -536,130 +548,136 @@ cdef object vec_discdd_array_sc(irk_state *state, irk_discdd_vec func, object si cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, - ndarray on, ndarray op, object lock): + cnp.ndarray on, cnp.ndarray op, object lock): cdef int *array_data - cdef ndarray array "arrayObject" - cdef npy_intp i, imax, n, res_size + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp i, imax, n, res_size cdef double *op_data cdef double *on_data - cdef broadcast multi + cdef cnp.broadcast multi + cdef object arr_obj if size is None: - multi = PyArray_MultiIterNew(2, on, op) - array = PyArray_SimpleNew(multi.nd, multi.dimensions, NPY_INT) - array_data = PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, on, op) + array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array_data = cnp.PyArray_DATA(array) with lock, nogil: for i from 0 <= i < multi.size: - on_data = PyArray_MultiIter_DATA(multi, 0) - op_data = PyArray_MultiIter_DATA(multi, 1) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) - PyArray_MultiIter_NEXT(multi) + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array else: - array = np.empty(size, np.int32) - array_data = PyArray_DATA(array) - res_size = PyArray_SIZE(array) - multi = PyArray_MultiIterNew(3, array, on, op) + array = np.empty(size, np.int32) + array_data = cnp.PyArray_DATA(array) + res_size = cnp.PyArray_SIZE(array) + multi = cnp.PyArray_MultiIterNew(3, array, on, op) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(2, on, op) + multi = cnp.PyArray_MultiIterNew(2, on, op) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - on_data = PyArray_MultiIter_DATA(multi, 0) - op_data = PyArray_MultiIter_DATA(multi, 1) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, n, array_data + n * i, on_data[0], op_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_discnmN_array_sc(irk_state *state, irk_discnmN_vec func, object size, int n, int m, int N, object lock): cdef int *array_data cdef int res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, n, m, N) return res else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, n, m, N) return array cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object size, - ndarray on, ndarray om, ndarray oN, object lock): + cnp.ndarray on, cnp.ndarray om, cnp.ndarray oN, object lock): cdef int *array_data cdef int *on_data cdef int *om_data cdef int *oN_data - cdef ndarray array "arrayObject" - cdef npy_intp i - cdef broadcast multi, multi2 - cdef npy_intp imax, n, res_size + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp i + cdef cnp.broadcast multi, multi2 + cdef cnp.npy_intp imax, n, res_size + cdef object arr_obj if size is None: - multi = PyArray_MultiIterNew(3, on, om, oN) - array = PyArray_SimpleNew(multi.nd, multi.dimensions, NPY_INT) - array_data = PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(3, on, om, oN) + array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array_data = cnp.PyArray_DATA(array) with lock, nogil: for i from 0 <= i < multi.size: - on_data = PyArray_MultiIter_DATA(multi, 0) - om_data = PyArray_MultiIter_DATA(multi, 1) - oN_data = PyArray_MultiIter_DATA(multi, 2) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + om_data = cnp.PyArray_MultiIter_DATA(multi, 1) + oN_data = cnp.PyArray_MultiIter_DATA(multi, 2) func(state, 1, array_data + i, on_data[0], om_data[0], oN_data[0]) - PyArray_MultiIter_NEXT(multi) + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array else: - array = np.empty(size, np.int32) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(4, array, on, om, + array = np.empty(size, np.int32) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(4, array, on, om, oN) - res_size = PyArray_SIZE(array) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") - multi = PyArray_MultiIterNew(3, on, om, oN) + multi = cnp.PyArray_MultiIterNew(3, on, om, oN) imax = multi.size n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - on_data = PyArray_MultiIter_DATA(multi, 0) - om_data = PyArray_MultiIter_DATA(multi, 1) - oN_data = PyArray_MultiIter_DATA(multi, 2) + on_data = cnp.PyArray_MultiIter_DATA(multi, 0) + om_data = cnp.PyArray_MultiIter_DATA(multi, 1) + oN_data = cnp.PyArray_MultiIter_DATA(multi, 2) func(state, n, array_data + n*i, on_data[0], om_data[0], oN_data[0]) - PyArray_MultiIter_NEXT(multi) - array.shape = (multi.shape + array.shape)[:array.ndim] + cnp.PyArray_MultiIter_NEXT(multi) + arr_obj = array + arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] multi_ndim = len(multi.shape) - array = array.transpose(tuple(range(multi_ndim, array.ndim)) + tuple(range(0, multi_ndim))) + arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) - return array + return arr_obj cdef object vec_discd_array_sc(irk_state *state, irk_discd_vec func, object size, double a, object lock): cdef int *array_data cdef int res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, a) return res else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, a) @@ -669,47 +687,49 @@ cdef object vec_long_discd_array_sc(irk_state *state, irk_discd_long_vec func, o double a, object lock): cdef long *array_data cdef long res - cdef ndarray array "arrayObject" - cdef npy_intp length - cdef npy_intp i + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length + cdef cnp.npy_intp i if size is None: func(state, 1, &res, a) return res else: - array = np.empty(size, int) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) + array = np.empty(size, int) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) with lock, nogil: func(state, length, array_data, a) return array -cdef object vec_discd_array(irk_state *state, irk_discd_vec func, object size, ndarray oa, +cdef object vec_discd_array(irk_state *state, irk_discd_vec func, object size, cnp.ndarray oa, object lock): cdef int *array_data cdef double *oa_data - cdef ndarray array "arrayObject" - cdef npy_intp length, res_size - cdef npy_intp i, imax, n - cdef broadcast multi - cdef flatiter itera + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length, res_size + cdef cnp.npy_intp i, imax, n + cdef cnp.broadcast multi + cdef cnp.flatiter itera + cdef object arr_obj if size is None: - array = PyArray_SimpleNew(PyArray_NDIM(oa), - PyArray_DIMS(oa), NPY_INT) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) - itera = PyArray_IterNew(oa) + array = cnp.PyArray_SimpleNew(cnp.PyArray_NDIM(oa), + cnp.PyArray_DIMS(oa), cnp.NPY_INT) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) + itera = cnp.PyArray_IterNew(oa) with lock, nogil: for i from 0 <= i < length: - func(state, 1, &array_data[i], ((itera.dataptr))[0]) - PyArray_ITER_NEXT(itera) + func(state, 1, &array_data[i], ((cnp.PyArray_ITER_DATA(itera)))[0]) + cnp.PyArray_ITER_NEXT(itera) + arr_obj = array else: - array = np.empty(size, np.int32) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(2, array, oa) - res_size = PyArray_SIZE(array) + array = np.empty(size, np.int32) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, array, oa) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") @@ -717,38 +737,42 @@ cdef object vec_discd_array(irk_state *state, irk_discd_vec func, object size, n n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 1) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, n, array_data + n*i, oa_data[0]) - PyArray_MultiIter_NEXTi(multi, 1) - array.shape = (oa.shape + array.shape)[:array.ndim] - array = array.transpose(tuple(range(oa.ndim, array.ndim)) + tuple(range(0, oa.ndim))) - return array + cnp.PyArray_MultiIter_NEXTi(multi, 1) + arr_obj = array + arr_obj.shape = ((oa).shape + arr_obj.shape)[:arr_obj.ndim] + arr_obj = arr_obj.transpose(tuple(range(oa.ndim, arr_obj.ndim)) + tuple(range(0, oa.ndim))) -cdef object vec_long_discd_array(irk_state *state, irk_discd_long_vec func, object size, ndarray oa, + return arr_obj + +cdef object vec_long_discd_array(irk_state *state, irk_discd_long_vec func, object size, cnp.ndarray oa, object lock): cdef long *array_data cdef double *oa_data - cdef ndarray array "arrayObject" - cdef npy_intp length, res_size - cdef npy_intp i, imax, n - cdef broadcast multi - cdef flatiter itera + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length, res_size + cdef cnp.npy_intp i, imax, n + cdef cnp.broadcast multi + cdef cnp.flatiter itera + cdef object arr_obj if size is None: - array = PyArray_SimpleNew(PyArray_NDIM(oa), - PyArray_DIMS(oa), NPY_LONG) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) - itera = PyArray_IterNew(oa) + array = cnp.PyArray_SimpleNew(cnp.PyArray_NDIM(oa), + cnp.PyArray_DIMS(oa), cnp.NPY_LONG) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) + itera = cnp.PyArray_IterNew(oa) with lock, nogil: for i from 0 <= i < length: - func(state, 1, array_data + i, ((itera.dataptr))[0]) - PyArray_ITER_NEXT(itera) + func(state, 1, array_data + i, ((cnp.PyArray_ITER_DATA(itera)))[0]) + cnp.PyArray_ITER_NEXT(itera) + arr_obj = array else: - array = np.empty(size, int) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(2, array, oa) - res_size = PyArray_SIZE(array) + array = np.empty(size, int) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, array, oa) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") @@ -756,36 +780,40 @@ cdef object vec_long_discd_array(irk_state *state, irk_discd_long_vec func, obje n = res_size // imax with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 1) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, n, array_data + n*i, oa_data[0]) - PyArray_MultiIter_NEXTi(multi, 1) - array.shape = (oa.shape + array.shape)[:array.ndim] - array = array.transpose(tuple(range(oa.ndim, array.ndim)) + tuple(range(0, oa.ndim))) - return array + cnp.PyArray_MultiIter_NEXTi(multi, 1) + arr_obj = array + arr_obj.shape = (( oa).shape + arr_obj.shape)[:arr_obj.ndim] + arr_obj = arr_obj.transpose(tuple(range(oa.ndim, arr_obj.ndim)) + tuple(range(0, oa.ndim))) + + return arr_obj -cdef object vec_Poisson_array(irk_state *state, irk_discdptr_vec func1, irk_discd_vec func2, object size, ndarray olambda, +cdef object vec_Poisson_array(irk_state *state, irk_discdptr_vec func1, irk_discd_vec func2, object size, cnp.ndarray olambda, object lock): cdef int *array_data cdef double *oa_data - cdef ndarray array "arrayObject" - cdef npy_intp length, res_size - cdef npy_intp i, imax, n - cdef broadcast multi - cdef flatiter itera + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length, res_size + cdef cnp.npy_intp i, imax, n + cdef cnp.broadcast multi + cdef cnp.flatiter itera + cdef object arr_obj if size is None: - array = PyArray_SimpleNew(PyArray_NDIM(olambda), - PyArray_DIMS(olambda), NPY_INT) - length = PyArray_SIZE(array) - array_data = PyArray_DATA(array) - oa_data = PyArray_DATA(olambda) + array = cnp.PyArray_SimpleNew(cnp.PyArray_NDIM(olambda), + cnp.PyArray_DIMS(olambda), cnp.NPY_INT) + length = cnp.PyArray_SIZE(array) + array_data = cnp.PyArray_DATA(array) + oa_data = cnp.PyArray_DATA(olambda) with lock, nogil: func1(state, length, array_data, oa_data) + arr_obj = array else: - array = np.empty(size, np.int32) - array_data = PyArray_DATA(array) - multi = PyArray_MultiIterNew(2, array, olambda) - res_size = PyArray_SIZE(array) + array = np.empty(size, np.int32) + array_data = cnp.PyArray_DATA(array) + multi = cnp.PyArray_MultiIterNew(2, array, olambda) + res_size = cnp.PyArray_SIZE(array) if (multi.size != res_size): raise ValueError("size is not compatible with inputs") @@ -794,23 +822,25 @@ cdef object vec_Poisson_array(irk_state *state, irk_discdptr_vec func1, irk_disc if imax < n: with lock, nogil: for i from 0 <= i < imax: - oa_data = PyArray_MultiIter_DATA(multi, 1) + oa_data = cnp.PyArray_MultiIter_DATA(multi, 1) func2(state, n, array_data + n*i, oa_data[0]) - PyArray_MultiIter_NEXTi(multi, 1) - array.shape = (olambda.shape + array.shape)[:array.ndim] - array = array.transpose(tuple(range(olambda.ndim, array.ndim)) + tuple(range(0, olambda.ndim))) + cnp.PyArray_MultiIter_NEXTi(multi, 1) + arr_obj = array + arr_obj.shape = ((olambda).shape + arr_obj.shape)[:arr_obj.ndim] + arr_obj = arr_obj.transpose(tuple(range(olambda.ndim, arr_obj.ndim)) + tuple(range(0, olambda.ndim))) else: - oa_data = PyArray_DATA(olambda); + oa_data = cnp.PyArray_DATA(olambda) with lock, nogil: for i from 0 <= i < n: func1(state, imax, array_data + imax*i, oa_data) + arr_obj = array - return array + return arr_obj -cdef double kahan_sum(double *darr, npy_intp n) nogil: +cdef double kahan_sum(double *darr, cnp.npy_intp n) nogil: cdef double c, y, t, sum - cdef npy_intp i + cdef cnp.npy_intp i sum = darr[0] c = 0.0 for i from 1 <= i < n: @@ -1051,7 +1081,7 @@ cdef class RandomState: cdef irk_error errcode cdef irk_brng_t brng_token = MT19937 cdef unsigned int stream_id - cdef ndarray obj "arrayObject_obj" + cdef cnp.ndarray obj "arrayObject_obj" if (brng): brng_token, stream_id = _parse_brng_argument(brng); @@ -1069,14 +1099,14 @@ cdef class RandomState: irk_seed_mkl(self.internal_state, idx, brng_token, stream_id) except TypeError: obj = np.asarray(seed) - if not obj.dtype is dtype('uint64'): + if not obj.dtype is np.dtype('uint64'): obj = obj.astype(np.int64, casting='safe') if ((obj > int(2**32 - 1)) | (obj < 0)).any(): raise ValueError("Seed must be between 0 and 4294967295") obj = obj.astype('uint32', casting='unsafe') with self.lock: - irk_seed_mkl_array(self.internal_state, PyArray_DATA(obj), - PyArray_DIM(obj, 0), brng_token, stream_id) + irk_seed_mkl_array(self.internal_state, cnp.PyArray_DATA(obj), + cnp.PyArray_DIM(obj, 0), brng_token, stream_id) def get_state(self): """ @@ -1170,7 +1200,7 @@ cdef class RandomState: """ cdef char *bytes_ptr cdef int brng_id - cdef ndarray obj "arrayObject_obj" + cdef cnp.ndarray obj "arrayObject_obj" state_len = len(state) if(state_len != 2): @@ -1179,10 +1209,10 @@ cdef class RandomState: if algo_name != 'MT19937': raise ValueError("The legacy state input algorithm must be 'MT19937'") try: - obj = PyArray_ContiguousFromObject(key, NPY_ULONG, 1, 1) + obj = cnp.PyArray_ContiguousFromObject(key, cnp.NPY_ULONG, 1, 1) except TypeError: # compatibility -- could be an older pickle - obj = PyArray_ContiguousFromObject(key, NPY_LONG, 1, 1) + obj = cnp.PyArray_ContiguousFromObject(key, cnp.NPY_LONG, 1, 1) self.seed(obj, brng = algo_name) return raise ValueError("The argument to set_state must be a list of 2 elements") @@ -1376,79 +1406,79 @@ cdef class RandomState: return _randint_type[key] # generates typed random integer in [low, high] - def _rand_bool(self, npy_bool low, npy_bool high, size): + def _rand_bool(self, cnp.npy_bool low, cnp.npy_bool high, size): """ _rand_bool(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_bool buf - cdef npy_bool *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_bool buf + cdef cnp.npy_bool *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_bool_vec(self.internal_state, 1, &buf, low, high) return np.bool_(buf) else: - array = np.empty(size, np.bool_) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.bool_) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_bool_vec(self.internal_state, cnt, out, low, high) return array - def _rand_int8(self, npy_int8 low, npy_int8 high, size): + def _rand_int8(self, cnp.npy_int8 low, cnp.npy_int8 high, size): """ _rand_int8(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_int8 buf - cdef npy_int8 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_int8 buf + cdef cnp.npy_int8 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_int8_vec(self.internal_state, 1, &buf, low, high) - return np.int8(buf) + return np.int8(buf) else: - array = np.empty(size, np.int8) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.int8) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_int8_vec(self.internal_state, cnt, out, low, high) return array - def _rand_int16(self, npy_int16 low, npy_int16 high, size): + def _rand_int16(self, cnp.npy_int16 low, cnp.npy_int16 high, size): """ _rand_int16(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_int16 buf - cdef npy_int16 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_int16 buf + cdef cnp.npy_int16 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_int16_vec(self.internal_state, 1, &buf, low, high) - return np.int16(buf) + return np.int16(buf) else: - array = np.empty(size, np.int16) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.int16) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_int16_vec(self.internal_state, cnt, out, low, high) return array - def _rand_int32(self, npy_int32 low, npy_int32 high, size): + def _rand_int32(self, cnp.npy_int32 low, cnp.npy_int32 high, size): """ _rand_int32(self, low, high, size) @@ -1476,137 +1506,137 @@ cdef class RandomState: distribution, or a single such random int if `size` not provided. """ - cdef npy_int32 buf - cdef npy_int32 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_int32 buf + cdef cnp.npy_int32 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_int32_vec(self.internal_state, 1, &buf, low, high) return np.int32(buf) else: - array = np.empty(size, np.int32) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.int32) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_int32_vec(self.internal_state, cnt, out, low, high) return array - def _rand_int64(self, npy_int64 low, npy_int64 high, size): + def _rand_int64(self, cnp.npy_int64 low, cnp.npy_int64 high, size): """ _rand_int64(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_int64 buf - cdef npy_int64 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_int64 buf + cdef cnp.npy_int64 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_int64_vec(self.internal_state, 1, &buf, low, high) return np.int64(buf) else: - array = np.empty(size, np.int64) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.int64) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_int64_vec(self.internal_state, cnt, out, low, high) return array - def _rand_uint8(self, npy_uint8 low, npy_uint8 high, size): + def _rand_uint8(self, cnp.npy_uint8 low, cnp.npy_uint8 high, size): """ _rand_uint8(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_uint8 buf - cdef npy_uint8 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_uint8 buf + cdef cnp.npy_uint8 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_uint8_vec(self.internal_state, 1, &buf, low, high) return np.uint8(buf) else: - array = np.empty(size, np.uint8) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.uint8) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_uint8_vec(self.internal_state, cnt, out, low, high) return array - def _rand_uint16(self, npy_uint16 low, npy_uint16 high, size): + def _rand_uint16(self, cnp.npy_uint16 low, cnp.npy_uint16 high, size): """ _rand_uint16(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_uint16 off, rng, buf - cdef npy_uint16 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_uint16 off, rng, buf + cdef cnp.npy_uint16 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_uint16_vec(self.internal_state, 1, &buf, low, high) return np.uint16(buf) else: - array = np.empty(size, np.uint16) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.uint16) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_uint16_vec(self.internal_state, cnt, out, low, high) return array - def _rand_uint32(self, npy_uint32 low, npy_uint32 high, size): + def _rand_uint32(self, cnp.npy_uint32 low, cnp.npy_uint32 high, size): """ _rand_uint32(self, low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_uint32 buf - cdef npy_uint32 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_uint32 buf + cdef cnp.npy_uint32 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_uint32_vec(self.internal_state, 1, &buf, low, high) return np.uint32(buf) else: - array = np.empty(size, np.uint32) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.uint32) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_uint32_vec(self.internal_state, cnt, out, low, high) return array - def _rand_uint64(self, npy_uint64 low, npy_uint64 high, size): + def _rand_uint64(self, cnp.npy_uint64 low, cnp.npy_uint64 high, size): """ _rand_uint64(low, high, size) See `_rand_int32` for documentation, only the return type changes. """ - cdef npy_uint64 buf - cdef npy_uint64 *out - cdef ndarray array "arrayObject" - cdef npy_intp cnt + cdef cnp.npy_uint64 buf + cdef cnp.npy_uint64 *out + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp cnt if size is None: irk_rand_uint64_vec(self.internal_state, 1, &buf, low, high) return np.uint64(buf) else: - array = np.empty(size, np.uint64) - cnt = PyArray_SIZE(array) - out = PyArray_DATA(array) + array = np.empty(size, np.uint64) + cnt = cnp.PyArray_SIZE(array) + out = cnp.PyArray_DATA(array) with nogil: irk_rand_uint64_vec(self.internal_state, cnt, out, low, high) return array @@ -1747,8 +1777,8 @@ cdef class RandomState: cdef long lo, hi cdef long *array_long_data cdef int * array_int_data - cdef ndarray array "arrayObject" - cdef npy_intp length + cdef cnp.ndarray array "arrayObject" + cdef cnp.npy_intp length cdef int rv_int cdef long rv_long @@ -1767,9 +1797,9 @@ cdef class RandomState: irk_discrete_uniform_vec(self.internal_state, 1, &rv_int, lo, hi) return rv_int else: - array = np.empty(size, np.int32) - length = PyArray_SIZE(array) - array_int_data = PyArray_DATA(array) + array = np.empty(size, np.int32) + length = cnp.PyArray_SIZE(array) + array_int_data = cnp.PyArray_DATA(array) with self.lock, nogil: irk_discrete_uniform_vec(self.internal_state, length, array_int_data, lo, hi) return array @@ -1778,14 +1808,14 @@ cdef class RandomState: irk_discrete_uniform_long_vec(self.internal_state, 1, &rv_long, lo, hi) return rv_long else: - array = np.empty(size, int) - length = PyArray_SIZE(array) - array_long_data = PyArray_DATA(array) + array = np.empty(size, int) + length = cnp.PyArray_SIZE(array) + array_long_data = cnp.PyArray_DATA(array) with self.lock, nogil: irk_discrete_uniform_long_vec(self.internal_state, length, array_long_data, lo, hi) return array - def bytes(self, npy_intp length): + def bytes(self, cnp.npy_intp length): """ bytes(length) @@ -1891,6 +1921,7 @@ cdef class RandomState: dtype='|S11') """ + cdef double *pix # Format and Verify input a = np.asarray(a) @@ -1917,8 +1948,8 @@ cdef class RandomState: if np.issubdtype(p.dtype, np.floating): atol = max(atol, np.sqrt(np.finfo(p.dtype).eps)) - p = PyArray_ContiguousFromObject(p, NPY_DOUBLE, 1, 1) - pix = PyArray_DATA(p) + p = cnp.PyArray_ContiguousFromObject(p, cnp.NPY_DOUBLE, 1, 1) + pix = cnp.PyArray_DATA(p) if p.ndim != 1: raise ValueError("p must be 1-dimensional") @@ -2066,7 +2097,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray olow, ohigh + cdef cnp.ndarray olow, ohigh cdef double flow, fhigh cdef object temp @@ -2082,8 +2113,8 @@ cdef class RandomState: fhigh, self.lock) PyErr_Clear() - olow = PyArray_FROM_OTF(low, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - ohigh = PyArray_FROM_OTF(high, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + olow = cnp.PyArray_FROM_OTF(low, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + ohigh = cnp.PyArray_FROM_OTF(high, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if not np.all(np.isfinite(olow)) or not np.all(np.isfinite(ohigh)): raise OverflowError('Range exceeds valid bounds') @@ -2419,7 +2450,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oloc, oscale + cdef cnp.ndarray oloc, oscale cdef double floc, fscale floc = PyFloat_AsDouble(loc) @@ -2437,8 +2468,8 @@ cdef class RandomState: PyErr_Clear() - oloc = PyArray_FROM_OTF(loc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oloc = cnp.PyArray_FROM_OTF(loc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0)): raise ValueError("scale <= 0") method = choose_method(method, [ICDF, BOXMULLER, BOXMULLER2], _method_alias_dict_gaussian) @@ -2487,7 +2518,7 @@ cdef class RandomState: Beta distribution. """ - cdef ndarray oa, ob + cdef cnp.ndarray oa, ob cdef double fa, fb fa = PyFloat_AsDouble(a) @@ -2502,8 +2533,8 @@ cdef class RandomState: PyErr_Clear() - oa = PyArray_FROM_OTF(a, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - ob = PyArray_FROM_OTF(b, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oa = cnp.PyArray_FROM_OTF(a, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + ob = cnp.PyArray_FROM_OTF(b, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oa, 0)): raise ValueError("a <= 0") if np.any(np.less_equal(ob, 0)): @@ -2550,7 +2581,7 @@ cdef class RandomState: http://en.wikipedia.org/wiki/Exponential_distribution """ - cdef ndarray oscale + cdef cnp.ndarray oscale cdef double fscale fscale = PyFloat_AsDouble(scale) @@ -2562,8 +2593,8 @@ cdef class RandomState: PyErr_Clear() - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, - NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, + cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0.0)): raise ValueError("scale <= 0") return vec_cont1_array(self.internal_state, irk_exponential_vec, size, oscale, @@ -2668,7 +2699,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oshape + cdef cnp.ndarray oshape cdef double fshape fshape = PyFloat_AsDouble(shape) @@ -2679,8 +2710,8 @@ cdef class RandomState: size, fshape, self.lock) PyErr_Clear() - oshape = PyArray_FROM_OTF(shape, NPY_DOUBLE, - NPY_ARRAY_ALIGNED) + oshape = cnp.PyArray_FROM_OTF(shape, cnp.NPY_DOUBLE, + cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oshape, 0.0)): raise ValueError("shape <= 0") return vec_cont1_array(self.internal_state, irk_standard_gamma_vec, size, @@ -2757,7 +2788,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oshape, oscale + cdef cnp.ndarray oshape, oscale cdef double fshape, fscale fshape = PyFloat_AsDouble(shape) @@ -2771,8 +2802,8 @@ cdef class RandomState: fscale, self.lock) PyErr_Clear() - oshape = PyArray_FROM_OTF(shape, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oshape = cnp.PyArray_FROM_OTF(shape, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oshape, 0.0)): raise ValueError("shape <= 0") if np.any(np.less_equal(oscale, 0.0)): @@ -2862,7 +2893,7 @@ cdef class RandomState: level. """ - cdef ndarray odfnum, odfden + cdef cnp.ndarray odfnum, odfden cdef double fdfnum, fdfden fdfnum = PyFloat_AsDouble(dfnum) @@ -2877,8 +2908,8 @@ cdef class RandomState: PyErr_Clear() - odfnum = PyArray_FROM_OTF(dfnum, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - odfden = PyArray_FROM_OTF(dfden, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + odfnum = cnp.PyArray_FROM_OTF(dfnum, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + odfden = cnp.PyArray_FROM_OTF(dfden, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(odfnum, 0.0)): raise ValueError("dfnum <= 0") if np.any(np.less_equal(odfden, 0.0)): @@ -2951,7 +2982,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray odfnum, odfden, ononc + cdef cnp.ndarray odfnum, odfden, ononc cdef double fdfnum, fdfden, fnonc fdfnum = PyFloat_AsDouble(dfnum) @@ -2969,9 +3000,9 @@ cdef class RandomState: PyErr_Clear() - odfnum = PyArray_FROM_OTF(dfnum, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - odfden = PyArray_FROM_OTF(dfden, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - ononc = PyArray_FROM_OTF(nonc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + odfnum = cnp.PyArray_FROM_OTF(dfnum, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + odfden = cnp.PyArray_FROM_OTF(dfden, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + ononc = cnp.PyArray_FROM_OTF(nonc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(odfnum, 1.0)): raise ValueError("dfnum <= 1") @@ -3045,7 +3076,7 @@ cdef class RandomState: array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272]) """ - cdef ndarray odf + cdef cnp.ndarray odf cdef double fdf fdf = PyFloat_AsDouble(df) @@ -3057,7 +3088,7 @@ cdef class RandomState: PyErr_Clear() - odf = PyArray_FROM_OTF(df, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + odf = cnp.PyArray_FROM_OTF(df, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(odf, 0.0)): raise ValueError("df <= 0") return vec_cont1_array(self.internal_state, irk_chisquare_vec, size, odf, @@ -3137,8 +3168,9 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray odf, ononc + cdef cnp.ndarray odf, ononc cdef double fdf, fnonc + fdf = PyFloat_AsDouble(df) fnonc = PyFloat_AsDouble(nonc) if not PyErr_Occurred(): @@ -3151,8 +3183,8 @@ cdef class RandomState: PyErr_Clear() - odf = PyArray_FROM_OTF(df, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - ononc = PyArray_FROM_OTF(nonc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + odf = cnp.PyArray_FROM_OTF(df, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + ononc = cnp.PyArray_FROM_OTF(nonc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(odf, 0.0)): raise ValueError("df <= 0") if np.any(np.less(ononc, 0.0)): @@ -3311,7 +3343,7 @@ cdef class RandomState: probability of about 99% of being true. """ - cdef ndarray odf + cdef cnp.ndarray odf cdef double fdf fdf = PyFloat_AsDouble(df) @@ -3323,7 +3355,7 @@ cdef class RandomState: PyErr_Clear() - odf = PyArray_FROM_OTF(df, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + odf = cnp.PyArray_FROM_OTF(df, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(odf, 0.0)): raise ValueError("df <= 0") return vec_cont1_array(self.internal_state, irk_standard_t_vec, size, odf, @@ -3406,7 +3438,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray omu, okappa + cdef cnp.ndarray omu, okappa cdef double fmu, fkappa fmu = PyFloat_AsDouble(mu) @@ -3419,9 +3451,9 @@ cdef class RandomState: PyErr_Clear() - omu = PyArray_FROM_OTF(mu, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - okappa = PyArray_FROM_OTF(kappa, NPY_DOUBLE, - NPY_ARRAY_ALIGNED) + omu = cnp.PyArray_FROM_OTF(mu, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + okappa = cnp.PyArray_FROM_OTF(kappa, cnp.NPY_DOUBLE, + cnp.NPY_ARRAY_ALIGNED) if np.any(np.less(okappa, 0.0)): raise ValueError("kappa < 0") return vec_cont2_array(self.internal_state, irk_vonmises_vec, size, omu, okappa, @@ -3514,7 +3546,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oa + cdef cnp.ndarray oa cdef double fa fa = PyFloat_AsDouble(a) @@ -3526,7 +3558,7 @@ cdef class RandomState: PyErr_Clear() - oa = PyArray_FROM_OTF(a, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oa = cnp.PyArray_FROM_OTF(a, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oa, 0.0)): raise ValueError("a <= 0") return vec_cont1_array(self.internal_state, irk_pareto_vec, size, oa, self.lock) @@ -3622,7 +3654,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oa + cdef cnp.ndarray oa cdef double fa fa = PyFloat_AsDouble(a) @@ -3634,7 +3666,7 @@ cdef class RandomState: PyErr_Clear() - oa = PyArray_FROM_OTF(a, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oa = cnp.PyArray_FROM_OTF(a, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oa, 0.0)): raise ValueError("a <= 0") return vec_cont1_array(self.internal_state, irk_weibull_vec, size, oa, @@ -3734,7 +3766,7 @@ cdef class RandomState: >>> plt.title('inverse of stats.pareto(5)') """ - cdef ndarray oa + cdef cnp.ndarray oa cdef double fa fa = PyFloat_AsDouble(a) @@ -3746,7 +3778,7 @@ cdef class RandomState: PyErr_Clear() - oa = PyArray_FROM_OTF(a, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oa = cnp.PyArray_FROM_OTF(a, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oa, 0.0)): raise ValueError("a <= 0") return vec_cont1_array(self.internal_state, irk_power_vec, size, oa, self.lock) @@ -3828,7 +3860,7 @@ cdef class RandomState: >>> plt.plot(x,g) """ - cdef ndarray oloc, oscale + cdef cnp.ndarray oloc, oscale cdef double floc, fscale floc = PyFloat_AsDouble(loc) @@ -3840,8 +3872,8 @@ cdef class RandomState: fscale, self.lock) PyErr_Clear() - oloc = PyArray_FROM_OTF(loc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oloc = cnp.PyArray_FROM_OTF(loc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0.0)): raise ValueError("scale <= 0") return vec_cont2_array(self.internal_state, irk_laplace_vec, size, oloc, oscale, @@ -3957,7 +3989,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oloc, oscale + cdef cnp.ndarray oloc, oscale cdef double floc, fscale floc = PyFloat_AsDouble(loc) @@ -3969,8 +4001,8 @@ cdef class RandomState: fscale, self.lock) PyErr_Clear() - oloc = PyArray_FROM_OTF(loc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oloc = cnp.PyArray_FROM_OTF(loc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0.0)): raise ValueError("scale <= 0") return vec_cont2_array(self.internal_state, irk_gumbel_vec, size, oloc, oscale, @@ -4048,7 +4080,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oloc, oscale + cdef cnp.ndarray oloc, oscale cdef double floc, fscale floc = PyFloat_AsDouble(loc) @@ -4060,8 +4092,8 @@ cdef class RandomState: fscale, self.lock) PyErr_Clear() - oloc = PyArray_FROM_OTF(loc, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oloc = cnp.PyArray_FROM_OTF(loc, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0.0)): raise ValueError("scale <= 0") return vec_cont2_array(self.internal_state, irk_logistic_vec, size, oloc, @@ -4174,7 +4206,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray omean, osigma + cdef cnp.ndarray omean, osigma cdef double fmean, fsigma fmean = PyFloat_AsDouble(mean) @@ -4193,8 +4225,8 @@ cdef class RandomState: PyErr_Clear() - omean = PyArray_FROM_OTF(mean, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - osigma = PyArray_FROM_OTF(sigma, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + omean = cnp.PyArray_FROM_OTF(mean, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + osigma = cnp.PyArray_FROM_OTF(sigma, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(osigma, 0.0)): raise ValueError("sigma <= 0.0") @@ -4263,7 +4295,7 @@ cdef class RandomState: 0.087300000000000003 """ - cdef ndarray oscale + cdef cnp.ndarray oscale cdef double fscale fscale = PyFloat_AsDouble(scale) @@ -4276,7 +4308,7 @@ cdef class RandomState: PyErr_Clear() - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(oscale, 0.0)): raise ValueError("scale <= 0.0") return vec_cont1_array(self.internal_state, irk_rayleigh_vec, size, oscale, @@ -4344,7 +4376,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray omean, oscale + cdef cnp.ndarray omean, oscale cdef double fmean, fscale fmean = PyFloat_AsDouble(mean) @@ -4358,8 +4390,8 @@ cdef class RandomState: fscale, self.lock) PyErr_Clear() - omean = PyArray_FROM_OTF(mean, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oscale = PyArray_FROM_OTF(scale, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + omean = cnp.PyArray_FROM_OTF(mean, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oscale = cnp.PyArray_FROM_OTF(scale, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(omean,0.0)): raise ValueError("mean <= 0.0") elif np.any(np.less_equal(oscale,0.0)): @@ -4427,7 +4459,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oleft, omode, oright + cdef cnp.ndarray oleft, omode, oright cdef double fleft, fmode, fright fleft = PyFloat_AsDouble(left) @@ -4444,9 +4476,9 @@ cdef class RandomState: fleft, fmode, fright, self.lock) PyErr_Clear() - oleft = PyArray_FROM_OTF(left, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - omode = PyArray_FROM_OTF(mode, NPY_DOUBLE, NPY_ARRAY_ALIGNED) - oright = PyArray_FROM_OTF(right, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + oleft = cnp.PyArray_FROM_OTF(left, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + omode = cnp.PyArray_FROM_OTF(mode, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) + oright = cnp.PyArray_FROM_OTF(right, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.greater(oleft, omode)): raise ValueError("left > mode") @@ -4540,7 +4572,7 @@ cdef class RandomState: # answer = 0.38885, or 38%. """ - cdef ndarray on, op + cdef cnp.ndarray on, op cdef long ln cdef double fp @@ -4564,8 +4596,8 @@ cdef class RandomState: PyErr_Clear() - on = PyArray_FROM_OTF(n, NPY_LONG, NPY_ARRAY_IN_ARRAY) - op = PyArray_FROM_OTF(p, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + on = cnp.PyArray_FROM_OTF(n, cnp.NPY_LONG, cnp.NPY_ARRAY_IN_ARRAY) + op = cnp.PyArray_FROM_OTF(p, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less(n, 0)): raise ValueError("n < 0") if np.any(np.less(op, 0)): @@ -4645,8 +4677,8 @@ cdef class RandomState: ... print i, "wells drilled, probability of one success =", probability """ - cdef ndarray on - cdef ndarray op + cdef cnp.ndarray on + cdef cnp.ndarray op cdef double fn cdef double fp @@ -4664,8 +4696,8 @@ cdef class RandomState: PyErr_Clear() - on = PyArray_FROM_OTF(n, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) - op = PyArray_FROM_OTF(p, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + on = cnp.PyArray_FROM_OTF(n, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) + op = cnp.PyArray_FROM_OTF(p, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less_equal(n, 0)): raise ValueError("n <= 0") if np.any(np.less(p, 0)): @@ -4743,7 +4775,7 @@ cdef class RandomState: >>> s = mkl_random.poisson(lam=(100., 500.), size=(100, 2)) """ - cdef ndarray olam + cdef cnp.ndarray olam cdef double flam flam = PyFloat_AsDouble(lam) if not PyErr_Occurred(): @@ -4759,7 +4791,7 @@ cdef class RandomState: PyErr_Clear() - olam = PyArray_FROM_OTF(lam, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + olam = cnp.PyArray_FROM_OTF(lam, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less(olam, 0)): raise ValueError("lam < 0") if np.any(np.greater(olam, self.poisson_lam_max)): @@ -4842,7 +4874,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray oa + cdef cnp.ndarray oa cdef double fa fa = PyFloat_AsDouble(a) @@ -4854,7 +4886,7 @@ cdef class RandomState: PyErr_Clear() - oa = PyArray_FROM_OTF(a, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + oa = cnp.PyArray_FROM_OTF(a, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less_equal(oa, 1.0)): raise ValueError("a <= 1.0") return vec_long_discd_array(self.internal_state, irk_zipf_long_vec, size, oa, self.lock) @@ -4905,7 +4937,7 @@ cdef class RandomState: 0.34889999999999999 #random """ - cdef ndarray op + cdef cnp.ndarray op cdef double fp fp = PyFloat_AsDouble(p) @@ -4920,7 +4952,7 @@ cdef class RandomState: PyErr_Clear() - op = PyArray_FROM_OTF(p, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + op = cnp.PyArray_FROM_OTF(p, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less_equal(op, 0.0)): raise ValueError("p < 0.0") if np.any(np.greater(op, 1.0)): @@ -5014,7 +5046,7 @@ cdef class RandomState: # answer = 0.003 ... pretty unlikely! """ - cdef ndarray ongood, onbad, onsample, otot + cdef cnp.ndarray ongood, onbad, onsample, otot cdef long lngood, lnbad, lnsample, lntot lngood = PyInt_AsLong(ngood) @@ -5037,9 +5069,9 @@ cdef class RandomState: PyErr_Clear() - ongood = PyArray_FROM_OTF(ngood, NPY_LONG, NPY_ARRAY_IN_ARRAY) - onbad = PyArray_FROM_OTF(nbad, NPY_LONG, NPY_ARRAY_IN_ARRAY) - onsample = PyArray_FROM_OTF(nsample, NPY_LONG, NPY_ARRAY_IN_ARRAY) + ongood = cnp.PyArray_FROM_OTF(ngood, cnp.NPY_LONG, cnp.NPY_ARRAY_IN_ARRAY) + onbad = cnp.PyArray_FROM_OTF(nbad, cnp.NPY_LONG, cnp.NPY_ARRAY_IN_ARRAY) + onsample = cnp.PyArray_FROM_OTF(nsample, cnp.NPY_LONG, cnp.NPY_ARRAY_IN_ARRAY) if np.any(np.less(ongood, 0)): raise ValueError("ngood < 0") if np.any(np.less(onbad, 0)): @@ -5133,7 +5165,7 @@ cdef class RandomState: >>> plt.show() """ - cdef ndarray op + cdef cnp.ndarray op cdef double fp fp = PyFloat_AsDouble(p) @@ -5147,7 +5179,7 @@ cdef class RandomState: PyErr_Clear() - op = PyArray_FROM_OTF(p, NPY_DOUBLE, NPY_ARRAY_ALIGNED) + op = cnp.PyArray_FROM_OTF(p, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if np.any(np.less_equal(op, 0.0)): raise ValueError("p <= 0.0") if np.any(np.greater_equal(op, 1.0)): @@ -5404,18 +5436,18 @@ cdef class RandomState: [True, True] """ - cdef ndarray resarr "arrayObject_resarr" - cdef ndarray marr "arrayObject_marr" - cdef ndarray tarr "arrayObject_tarr" + cdef cnp.ndarray resarr "arrayObject_resarr" + cdef cnp.ndarray marr "arrayObject_marr" + cdef cnp.ndarray tarr "arrayObject_tarr" cdef double *res_data cdef double *mean_data cdef double *t_data - cdef npy_intp dim, n + cdef cnp.npy_intp dim, n cdef ch_st_enum storage_mode # Check preconditions on arguments - marr = PyArray_FROM_OTF(mean, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) - tarr = PyArray_FROM_OTF(ch, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY) + marr = cnp.PyArray_FROM_OTF(mean, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) + tarr = cnp.PyArray_FROM_OTF(ch, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_IN_ARRAY) if size is None: shape = [] @@ -5450,12 +5482,12 @@ cdef class RandomState: final_shape = list(shape[:]) final_shape.append(int(dim)) - resarr = np.empty(final_shape, np.float64) - res_data = PyArray_DATA(resarr) - mean_data = PyArray_DATA(marr) - t_data = PyArray_DATA(tarr) + resarr = np.empty(final_shape, np.float64) + res_data = cnp.PyArray_DATA(resarr) + mean_data = cnp.PyArray_DATA(marr) + t_data = cnp.PyArray_DATA(tarr) - n = PyArray_SIZE(resarr) // dim + n = cnp.PyArray_SIZE(resarr) // dim method = choose_method(method, [ICDF, BOXMULLER2, BOXMULLER], _method_alias_dict_gaussian) if (method is ICDF): @@ -5545,17 +5577,17 @@ cdef class RandomState: array([100, 0]) """ - cdef npy_intp d - cdef ndarray parr "arrayObject_parr", mnarr "arrayObject_mnarr" + cdef cnp.npy_intp d + cdef cnp.ndarray parr "arrayObject_parr", mnarr "arrayObject_mnarr" cdef double *pix cdef int *mnix - cdef npy_intp i, j, sz + cdef cnp.npy_intp i, j, sz cdef double Sum cdef int dn d = len(pvals) - parr = PyArray_ContiguousFromObject(pvals, NPY_DOUBLE, 1, 1) - pix = PyArray_DATA(parr) + parr = cnp.PyArray_ContiguousFromObject(pvals, cnp.NPY_DOUBLE, 1, 1) + pix = cnp.PyArray_DATA(parr) if kahan_sum(pix, d-1) > (1.0 + 1e-12): raise ValueError("sum(pvals[:-1]) > 1.0") @@ -5563,9 +5595,9 @@ cdef class RandomState: shape = _shape_from_size(size, d) multin = np.zeros(shape, np.int32) - mnarr = multin - mnix = PyArray_DATA(mnarr) - sz = PyArray_SIZE(mnarr) + mnarr = multin + mnix = cnp.PyArray_DATA(mnarr) + sz = cnp.PyArray_SIZE(mnarr) irk_multinomial_vec(self.internal_state, sz // d, mnix, n, d, pix) @@ -5651,16 +5683,16 @@ cdef class RandomState: # val = val.T #return val - cdef npy_intp k - cdef npy_intp totsize - cdef ndarray alpha_arr, val_arr + cdef cnp.npy_intp k + cdef cnp.npy_intp totsize + cdef cnp.ndarray alpha_arr, val_arr cdef double *alpha_data cdef double *val_data - cdef npy_intp i, j + cdef cnp.npy_intp i, j cdef double invacc, acc - cdef broadcast multi1, multi2 + cdef cnp.broadcast multi1, multi2 - alpha_arr = PyArray_FROM_OTF(alpha, NPY_DOUBLE, NPY_ARRAY_ALIGNED); + alpha_arr = cnp.PyArray_FROM_OTF(alpha, cnp.NPY_DOUBLE, cnp.NPY_ARRAY_ALIGNED) if (alpha_arr.ndim != 1): raise ValueError("Parameter alpha is not a vector") @@ -5669,26 +5701,26 @@ cdef class RandomState: diric = self.standard_gamma(alpha_arr, shape) - val_arr = diric - totsize = PyArray_SIZE(val_arr) + val_arr = diric + totsize = cnp.PyArray_SIZE(val_arr) # Use of iterators is faster than calling PyArray_ContiguousFromObject and iterating in C - multi1 = PyArray_MultiIterNew(2, val_arr, alpha_arr); - multi2 = PyArray_MultiIterNew(2, val_arr, alpha_arr); + multi1 = cnp.PyArray_MultiIterNew(2, val_arr, alpha_arr) + multi2 = cnp.PyArray_MultiIterNew(2, val_arr, alpha_arr) i = 0 with self.lock, nogil: while i < totsize: acc = 0.0 for j from 0 <= j < k: - val_data = PyArray_MultiIter_DATA(multi1, 0) + val_data = cnp.PyArray_MultiIter_DATA(multi1, 0) acc += val_data[0] - PyArray_MultiIter_NEXTi(multi1, 0) - invacc = 1.0/acc; + cnp.PyArray_MultiIter_NEXTi(multi1, 0) + invacc = 1.0/acc for j from 0 <= j < k: - val_data = PyArray_MultiIter_DATA(multi2, 0); + val_data = cnp.PyArray_MultiIter_DATA(multi2, 0) val_data[0] *= invacc - PyArray_MultiIter_NEXTi(multi2, 0) + cnp.PyArray_MultiIter_NEXTi(multi2, 0) i += k return diric @@ -5728,17 +5760,17 @@ cdef class RandomState: """ cdef: - npy_intp i, j, n = len(x), stride, itemsize + cnp.npy_intp i, j, n = len(x), stride, itemsize char* x_ptr char* buf_ptr - cdef ndarray u "arrayObject_u" + cdef cnp.ndarray u "arrayObject_u" cdef double *u_data if (n == 0): return - u = self.random_sample(n-1) - u_data = PyArray_DATA(u) + u = self.random_sample(n-1) + u_data = cnp.PyArray_DATA(u) if type(x) is np.ndarray and x.ndim == 1 and x.size: # Fast, statically typed path: shuffle the underlying buffer. @@ -5757,8 +5789,8 @@ cdef class RandomState: # We trick gcc into providing a specialized implementation for # the most common case, yielding a ~33% performance improvement. # Note that apparently, only one branch can ever be specialized. - if itemsize == sizeof(npy_intp): - self._shuffle_raw(n, sizeof(npy_intp), stride, x_ptr, buf_ptr, u_data) + if itemsize == sizeof(cnp.npy_intp): + self._shuffle_raw(n, sizeof(cnp.npy_intp), stride, x_ptr, buf_ptr, u_data) else: self._shuffle_raw(n, itemsize, stride, x_ptr, buf_ptr, u_data) elif isinstance(x, np.ndarray) and x.ndim > 1 and x.size: @@ -5766,7 +5798,7 @@ cdef class RandomState: buf = np.empty_like(x[0]) with self.lock: for i in reversed(range(1, n)): - j = floor( (i + 1) * u_data[i - 1]) + j = floor( (i + 1) * u_data[i - 1]) if (j < i): buf[...] = x[j] x[j] = x[i] @@ -5775,14 +5807,14 @@ cdef class RandomState: # Untyped path. with self.lock: for i in reversed(range(1, n)): - j = floor( (i + 1) * u_data[i - 1]) + j = floor( (i + 1) * u_data[i - 1]) x[i], x[j] = x[j], x[i] - cdef inline _shuffle_raw(self, npy_intp n, npy_intp itemsize, - npy_intp stride, char* data, char* buf, double* udata): - cdef npy_intp i, j + cdef inline _shuffle_raw(self, cnp.npy_intp n, cnp.npy_intp itemsize, + cnp.npy_intp stride, char* data, char* buf, double* udata): + cdef cnp.npy_intp i, j for i in reversed(range(1, n)): - j = floor( (i + 1) * udata[i - 1]) + j = floor( (i + 1) * udata[i - 1]) memcpy(buf, data + j * stride, itemsize) memcpy(data + j * stride, data + i * stride, itemsize) memcpy(data + i * stride, buf, itemsize) diff --git a/mkl_random/src/mkl_distributions.cpp b/mkl_random/src/mkl_distributions.cpp index 38be263..ff24a27 100644 --- a/mkl_random/src/mkl_distributions.cpp +++ b/mkl_random/src/mkl_distributions.cpp @@ -1510,7 +1510,7 @@ void irk_discrete_uniform_long_vec(irk_state *state, npy_intp len, long *res, co int *buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, -1, (const int)max); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, -1, (int)max); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1626,7 +1626,7 @@ void irk_rand_bool_vec(irk_state *state, npy_intp len, npy_bool *res, const npy_ buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1666,7 +1666,7 @@ void irk_rand_uint8_vec(irk_state *state, npy_intp len, npy_uint8 *res, const np buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1706,7 +1706,7 @@ void irk_rand_int8_vec(irk_state *state, npy_intp len, npy_int8 *res, const npy_ buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1746,7 +1746,7 @@ void irk_rand_uint16_vec(irk_state *state, npy_intp len, npy_uint16 *res, const buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1786,7 +1786,7 @@ void irk_rand_int16_vec(irk_state *state, npy_intp len, npy_int16 *res, const np buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1831,7 +1831,7 @@ void irk_rand_uint32_vec(irk_state *state, npy_intp len, npy_uint32 *res, const if (lo) shft++; - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)(lo - shft), (const int)(hi - shft + 1U)); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)(lo - shft), (int)(hi - shft + 1U)); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR @@ -1840,7 +1840,7 @@ void irk_rand_uint32_vec(irk_state *state, npy_intp len, npy_uint32 *res, const } else { - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); } } @@ -1873,7 +1873,7 @@ void irk_rand_int32_vec(irk_state *state, npy_intp len, npy_int32 *res, const np } else { - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (const int)lo, (const int)hi + 1); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, (int *)res, (int)lo, (int)hi + 1); assert(err == VSL_STATUS_OK); } } @@ -1921,7 +1921,7 @@ void irk_rand_uint64_vec(irk_state *state, npy_intp len, npy_uint64 *res, const int *buf = (int *)mkl_malloc(len * sizeof(int), 64); assert(buf != nullptr); - err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, 0, (const int)rng); + err = viRngUniform(VSL_RNG_METHOD_UNIFORM_STD, state->stream, len, buf, 0, (int)rng); assert(err == VSL_STATUS_OK); DIST_PRAGMA_VECTOR diff --git a/mkl_random/src/numpy.pxd b/mkl_random/src/numpy.pxd deleted file mode 100644 index fa8af74..0000000 --- a/mkl_random/src/numpy.pxd +++ /dev/null @@ -1,152 +0,0 @@ -# :Author: Travis Oliphant - -cdef extern from "numpy/npy_no_deprecated_api.h": pass - -cdef extern from "numpy/arrayobject.h": - - cdef enum NPY_TYPES: - NPY_BOOL - NPY_BYTE - NPY_UBYTE - NPY_SHORT - NPY_USHORT - NPY_INT - NPY_UINT - NPY_LONG - NPY_ULONG - NPY_LONGLONG - NPY_ULONGLONG - NPY_FLOAT - NPY_DOUBLE - NPY_LONGDOUBLE - NPY_CFLOAT - NPY_CDOUBLE - NPY_CLONGDOUBLE - NPY_OBJECT - NPY_STRING - NPY_UNICODE - NPY_VOID - NPY_NTYPES - NPY_NOTYPE - - cdef enum requirements: - NPY_ARRAY_C_CONTIGUOUS - NPY_ARRAY_F_CONTIGUOUS - NPY_ARRAY_OWNDATA - NPY_ARRAY_FORCECAST - NPY_ARRAY_ENSURECOPY - NPY_ARRAY_ENSUREARRAY - NPY_ARRAY_ELEMENTSTRIDES - NPY_ARRAY_ALIGNED - NPY_ARRAY_NOTSWAPPED - NPY_ARRAY_WRITEABLE - NPY_ARRAY_UPDATEIFCOPY - NPY_ARR_HAS_DESCR - - NPY_ARRAY_BEHAVED - NPY_ARRAY_BEHAVED_NS - NPY_ARRAY_CARRAY - NPY_ARRAY_CARRAY_RO - NPY_ARRAY_FARRAY - NPY_ARRAY_FARRAY_RO - NPY_ARRAY_DEFAULT - - NPY_ARRAY_IN_ARRAY - NPY_ARRAY_OUT_ARRAY - NPY_ARRAY_INOUT_ARRAY - NPY_ARRAY_IN_FARRAY - NPY_ARRAY_OUT_FARRAY - NPY_ARRAY_INOUT_FARRAY - - NPY_ARRAY_UPDATE_ALL - - cdef enum defines: - NPY_MAXDIMS - - ctypedef struct npy_cdouble: - double real - double imag - - ctypedef struct npy_cfloat: - double real - double imag - - ctypedef int npy_int - ctypedef int npy_intp - ctypedef int npy_int64 - ctypedef int npy_uint64 - ctypedef int npy_int32 - ctypedef int npy_uint32 - ctypedef int npy_int16 - ctypedef int npy_uint16 - ctypedef int npy_int8 - ctypedef int npy_uint8 - ctypedef int npy_bool - - ctypedef extern class numpy.dtype [object PyArray_Descr]: pass - - ctypedef extern class numpy.ndarray [object PyArrayObject]: pass - - ctypedef extern class numpy.flatiter [object PyArrayIterObject]: - cdef int nd_m1 - cdef npy_intp index, size - cdef ndarray ao - cdef char *dataptr - - ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]: - cdef int numiter - cdef npy_intp size, index - cdef int nd - cdef npy_intp *dimensions - cdef void **iters - - object PyArray_ZEROS(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran) - object PyArray_EMPTY(int ndims, npy_intp* dims, NPY_TYPES type_num, int fortran) - dtype PyArray_DescrFromTypeNum(NPY_TYPES type_num) - object PyArray_SimpleNew(int ndims, npy_intp* dims, NPY_TYPES type_num) - int PyArray_Check(object obj) - object PyArray_ContiguousFromAny(object obj, NPY_TYPES type, - int mindim, int maxdim) - object PyArray_ContiguousFromObject(object obj, NPY_TYPES type, - int mindim, int maxdim) - npy_intp PyArray_SIZE(ndarray arr) - npy_intp PyArray_NBYTES(ndarray arr) - object PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim, - int requirements, object context) - object PyArray_FROMANY(object obj, NPY_TYPES type_num, int min, - int max, int requirements) - object PyArray_NewFromDescr(object subtype, dtype newtype, int nd, - npy_intp* dims, npy_intp* strides, void* data, - int flags, object parent) - - object PyArray_FROM_OTF(object obj, NPY_TYPES type, int flags) - object PyArray_EnsureArray(object) - - object PyArray_MultiIterNew(int n, ...) - - char *PyArray_MultiIter_DATA(broadcast multi, int i) nogil - void PyArray_MultiIter_NEXTi(broadcast multi, int i) nogil - void PyArray_MultiIter_NEXT(broadcast multi) nogil - - object PyArray_IterNew(object arr) - void PyArray_ITER_NEXT(flatiter it) nogil - - dtype PyArray_DescrFromType(int) - -# include functions that were once macros in the new api - - int PyArray_NDIM(ndarray arr) - char * PyArray_DATA(ndarray arr) - npy_intp * PyArray_DIMS(ndarray arr) - npy_intp * PyArray_STRIDES(ndarray arr) - npy_intp PyArray_DIM(ndarray arr, int idim) - npy_intp PyArray_STRIDE(ndarray arr, int istride) - object PyArray_BASE(ndarray arr) - dtype PyArray_DESCR(ndarray arr) - int PyArray_FLAGS(ndarray arr) - npy_intp PyArray_ITEMSIZE(ndarray arr) - int PyArray_TYPE(ndarray arr) - int PyArray_CHKFLAGS(ndarray arr, int flags) - object PyArray_GETITEM(ndarray arr, char *itemptr) - - int _import_array() diff --git a/setup.py b/setup.py index b0ce3b1..6291420 100644 --- a/setup.py +++ b/setup.py @@ -96,7 +96,8 @@ def extensions(): defs = [('_FILE_OFFSET_BITS', '64'), ('_LARGEFILE_SOURCE', '1'), ('_LARGEFILE64_SOURCE', '1'), - ("PY_ARRAY_UNIQUE_SYMBOL", "mkl_random_ext")] + ("PY_ARRAY_UNIQUE_SYMBOL", "mkl_random_ext"), + ("NPY_NO_DEPRICATED_API", None)] exts = [ Extension( From 4f59e824362d1955b9c6f1520a4b241957fdd850 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 14:34:18 -0500 Subject: [PATCH 07/14] Use public API to work with cnp.broadcast --- mkl_random/mklrand.pyx | 129 +++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 38 deletions(-) diff --git a/mkl_random/mklrand.pyx b/mkl_random/mklrand.pyx index 9bb9759..8159a9c 100644 --- a/mkl_random/mklrand.pyx +++ b/mkl_random/mklrand.pyx @@ -39,6 +39,7 @@ cdef extern from "Python.h": cimport numpy as cnp from libc.string cimport memset, memcpy +cimport cpython.tuple cdef extern from "math.h": double floor(double x) @@ -244,6 +245,9 @@ cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, cdef cnp.flatiter itera cdef cnp.broadcast multi cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: array = cnp.PyArray_SimpleNew(cnp.PyArray_NDIM(oa), @@ -261,11 +265,11 @@ cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(1, oa) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -273,8 +277,13 @@ cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, func(state, n, array_data + n*i, oa_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -308,13 +317,20 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, cdef cnp.npy_intp i, n, imax, res_size cdef cnp.broadcast multi cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: multi = cnp.PyArray_MultiIterNew(2, oa, ob) - array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_DOUBLE) + array = cnp.PyArray_SimpleNew( + cnp.PyArray_MultiIter_NDIM(multi), + cnp.PyArray_MultiIter_DIMS(multi), + cnp.NPY_DOUBLE + ) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < multi.size: + for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], oa_data[0], ob_data[0]) @@ -324,12 +340,12 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, array = np.empty(size, np.float64) array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(3, array, oa, ob) - res_size = cnp.PyArray_SIZE(array); - if (multi.size != res_size): + res_size = cnp.PyArray_SIZE(array) + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, oa, ob) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -338,8 +354,13 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, func(state, n, array_data + n*i, oa_data[0], ob_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -375,13 +396,16 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, cdef cnp.npy_intp i, res_size, n, imax cdef cnp.broadcast multi cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) - array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_DOUBLE) + array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_DOUBLE) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < multi.size: + for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) oc_data = cnp.PyArray_MultiIter_DATA(multi, 2) @@ -394,11 +418,11 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, multi = cnp.PyArray_MultiIterNew(4, array, oa, ob, oc) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -408,8 +432,13 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, func(state, n, array_data + n*i, oa_data[0], ob_data[0], oc_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -489,13 +518,16 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, cdef int *on_data cdef cnp.broadcast multi cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: multi = cnp.PyArray_MultiIterNew(2, on, op) - array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < multi.size: + for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) @@ -506,11 +538,11 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(3, array, on, op) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, on, op) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -519,8 +551,13 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, func(state, n, array_data + n * i, on_data[0], op_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -556,13 +593,16 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, cdef double *on_data cdef cnp.broadcast multi cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: multi = cnp.PyArray_MultiIterNew(2, on, op) - array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < multi.size: + for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) @@ -573,11 +613,11 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, array_data = cnp.PyArray_DATA(array) res_size = cnp.PyArray_SIZE(array) multi = cnp.PyArray_MultiIterNew(3, array, on, op) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, on, op) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -586,8 +626,13 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, func(state, n, array_data + n * i, on_data[0], op_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -624,13 +669,16 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz cdef cnp.broadcast multi, multi2 cdef cnp.npy_intp imax, n, res_size cdef object arr_obj + cdef Py_ssize_t multi_nd + cdef tuple multi_shape + cdef cnp.npy_intp *multi_dims if size is None: multi = cnp.PyArray_MultiIterNew(3, on, om, oN) - array = cnp.PyArray_SimpleNew(multi.nd, multi.dimensions, cnp.NPY_INT) + array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < multi.size: + for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) om_data = cnp.PyArray_MultiIter_DATA(multi, 1) oN_data = cnp.PyArray_MultiIter_DATA(multi, 2) @@ -643,11 +691,11 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz multi = cnp.PyArray_MultiIterNew(4, array, on, om, oN) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(3, on, om, oN) - imax = multi.size + imax = cnp.PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -657,8 +705,13 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz func(state, n, array_data + n*i, on_data[0], om_data[0], oN_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - arr_obj.shape = (multi.shape + arr_obj.shape)[:arr_obj.ndim] - multi_ndim = len(multi.shape) + multi_nd = cnp.PyArray_MultiIter_NDIM(multi) + multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_shape = cpython.tuple.PyTuple_New(multi_nd) + for i from 0 <= i < multi_nd: + cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) + arr_obj.shape = (multi_shape + arr_obj.shape)[:arr_obj.ndim] + multi_ndim = len(multi_shape) arr_obj = arr_obj.transpose(tuple(range(multi_ndim, arr_obj.ndim)) + tuple(range(0, multi_ndim))) return arr_obj @@ -730,7 +783,7 @@ cdef object vec_discd_array(irk_state *state, irk_discd_vec func, object size, c array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = oa.size @@ -773,7 +826,7 @@ cdef object vec_long_discd_array(irk_state *state, irk_discd_long_vec func, obje array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = oa.size @@ -814,7 +867,7 @@ cdef object vec_Poisson_array(irk_state *state, irk_discdptr_vec func1, irk_disc array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, olambda) res_size = cnp.PyArray_SIZE(array) - if (multi.size != res_size): + if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = olambda.size From d5032adc96a57fda0d220683513fe090d63178b2 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 19 Jul 2024 21:20:28 -0500 Subject: [PATCH 08/14] Only build mkl_random with NumPy 2.0 in build env This is due to https://github.com/numpy/numpy/issues/26990 --- .github/workflows/build-with-clang.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-with-clang.yml b/.github/workflows/build-with-clang.yml index 86ad416..fa5ba6d 100644 --- a/.github/workflows/build-with-clang.yml +++ b/.github/workflows/build-with-clang.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: python: ["3.9", "3.10", "3.11", "3.12"] - numpy_build_version: ["numpy'<2'", "numpy'>=2'"] + numpy_build_version: ["numpy'>=2'"] env: ONEAPI_ROOT: /opt/intel/oneapi From 792b3f7181ef2970a71da26a1b1a0164926fd7cc Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Sun, 21 Jul 2024 09:54:55 -0500 Subject: [PATCH 09/14] Introduce helper file to work around issue numpy#26990 The helper file defines workaround_PyArray_MultiIter_SIZE, etc which used NumPy 2.0 API when building with NumPy 2.0, and a work-around for earlier versions. --- mkl_random/mklrand.pyx | 88 ++++++++++++--------- mkl_random/src/numpy_multiiter_workaround.h | 78 ++++++++++++++++++ 2 files changed, 127 insertions(+), 39 deletions(-) create mode 100644 mkl_random/src/numpy_multiiter_workaround.h diff --git a/mkl_random/mklrand.pyx b/mkl_random/mklrand.pyx index 8159a9c..b307899 100644 --- a/mkl_random/mklrand.pyx +++ b/mkl_random/mklrand.pyx @@ -36,10 +36,13 @@ cdef extern from "Python.h": int PyErr_Occurred() void PyErr_Clear() +cdef extern from "numpy/npy_no_deprecated_api.h": + pass cimport numpy as cnp from libc.string cimport memset, memcpy cimport cpython.tuple +cimport cython cdef extern from "math.h": double floor(double x) @@ -52,6 +55,11 @@ cdef extern from "mklrand_py_helper.h": char* py_bytes_DataPtr(object b) int is_bytes_object(object b) +cdef extern from "numpy_multiiter_workaround.h": + cnp.npy_intp cnp_PyArray_MultiIter_SIZE "workaround_PyArray_MultiIter_SIZE"(cnp.broadcast multi) nogil + int cnp_PyArray_MultiIter_NDIM "workaround_PyArray_MultiIter_NDIM"(cnp.broadcast multi) nogil + cnp.npy_intp* cnp_PyArray_MultiIter_DIMS "workaround_PyArray_MultiIter_DIMS"(cnp.broadcast multi) nogil + cdef extern from "randomkit.h": ctypedef struct irk_state: @@ -188,7 +196,6 @@ cdef int r = cnp._import_array() if (r<0): raise ImportError("Failed to import NumPy") -cimport cython import numpy as np import operator import warnings @@ -265,11 +272,11 @@ cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(1, oa) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -277,8 +284,8 @@ cdef object vec_cont1_array(irk_state *state, irk_cont1_vec func, object size, func(state, n, array_data + n*i, oa_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -324,13 +331,13 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, if size is None: multi = cnp.PyArray_MultiIterNew(2, oa, ob) array = cnp.PyArray_SimpleNew( - cnp.PyArray_MultiIter_NDIM(multi), - cnp.PyArray_MultiIter_DIMS(multi), + cnp_PyArray_MultiIter_NDIM(multi), + cnp_PyArray_MultiIter_DIMS(multi), cnp.NPY_DOUBLE ) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): + for i from 0 <= i < cnp_PyArray_MultiIter_SIZE(multi): oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], oa_data[0], ob_data[0]) @@ -341,11 +348,11 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(3, array, oa, ob) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, oa, ob) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -354,8 +361,8 @@ cdef object vec_cont2_array(irk_state *state, irk_cont2_vec func, object size, func(state, n, array_data + n*i, oa_data[0], ob_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -402,10 +409,10 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, if size is None: multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) - array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_DOUBLE) + array = cnp.PyArray_SimpleNew(cnp_PyArray_MultiIter_NDIM(multi), cnp_PyArray_MultiIter_DIMS(multi), cnp.NPY_DOUBLE) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): + for i from 0 <= i < cnp_PyArray_MultiIter_SIZE(multi): oa_data = cnp.PyArray_MultiIter_DATA(multi, 0) ob_data = cnp.PyArray_MultiIter_DATA(multi, 1) oc_data = cnp.PyArray_MultiIter_DATA(multi, 2) @@ -418,11 +425,11 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, multi = cnp.PyArray_MultiIterNew(4, array, oa, ob, oc) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(3, oa, ob, oc) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -432,8 +439,8 @@ cdef object vec_cont3_array(irk_state *state, irk_cont3_vec func, object size, func(state, n, array_data + n*i, oa_data[0], ob_data[0], oc_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -521,13 +528,16 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, cdef Py_ssize_t multi_nd cdef tuple multi_shape cdef cnp.npy_intp *multi_dims + cdef int multi_nd_i if size is None: multi = cnp.PyArray_MultiIterNew(2, on, op) - array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) + multi_nd_i = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) + array = cnp.PyArray_SimpleNew(multi_nd_i, multi_dims, cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): + for i from 0 <= i < cnp_PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) @@ -538,11 +548,11 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(3, array, on, op) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, on, op) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -551,8 +561,8 @@ cdef object vec_discnp_array(irk_state *state, irk_discnp_vec func, object size, func(state, n, array_data + n * i, on_data[0], op_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -599,10 +609,10 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, if size is None: multi = cnp.PyArray_MultiIterNew(2, on, op) - array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) + array = cnp.PyArray_SimpleNew(cnp_PyArray_MultiIter_NDIM(multi), cnp_PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): + for i from 0 <= i < cnp_PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) op_data = cnp.PyArray_MultiIter_DATA(multi, 1) func(state, 1, &array_data[i], on_data[0], op_data[0]) @@ -613,11 +623,11 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, array_data = cnp.PyArray_DATA(array) res_size = cnp.PyArray_SIZE(array) multi = cnp.PyArray_MultiIterNew(3, array, on, op) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(2, on, op) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -626,8 +636,8 @@ cdef object vec_discdd_array(irk_state *state, irk_discdd_vec func, object size, func(state, n, array_data + n * i, on_data[0], op_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -675,10 +685,10 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz if size is None: multi = cnp.PyArray_MultiIterNew(3, on, om, oN) - array = cnp.PyArray_SimpleNew(cnp.PyArray_MultiIter_NDIM(multi), cnp.PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) + array = cnp.PyArray_SimpleNew(cnp_PyArray_MultiIter_NDIM(multi), cnp_PyArray_MultiIter_DIMS(multi), cnp.NPY_INT) array_data = cnp.PyArray_DATA(array) with lock, nogil: - for i from 0 <= i < cnp.PyArray_MultiIter_SIZE(multi): + for i from 0 <= i < cnp_PyArray_MultiIter_SIZE(multi): on_data = cnp.PyArray_MultiIter_DATA(multi, 0) om_data = cnp.PyArray_MultiIter_DATA(multi, 1) oN_data = cnp.PyArray_MultiIter_DATA(multi, 2) @@ -691,11 +701,11 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz multi = cnp.PyArray_MultiIterNew(4, array, on, om, oN) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") multi = cnp.PyArray_MultiIterNew(3, on, om, oN) - imax = cnp.PyArray_MultiIter_SIZE(multi) + imax = cnp_PyArray_MultiIter_SIZE(multi) n = res_size // imax with lock, nogil: for i from 0 <= i < imax: @@ -705,8 +715,8 @@ cdef object vec_discnmN_array(irk_state *state, irk_discnmN_vec func, object siz func(state, n, array_data + n*i, on_data[0], om_data[0], oN_data[0]) cnp.PyArray_MultiIter_NEXT(multi) arr_obj = array - multi_nd = cnp.PyArray_MultiIter_NDIM(multi) - multi_dims = cnp.PyArray_MultiIter_DIMS(multi) + multi_nd = cnp_PyArray_MultiIter_NDIM(multi) + multi_dims = cnp_PyArray_MultiIter_DIMS(multi) multi_shape = cpython.tuple.PyTuple_New(multi_nd) for i from 0 <= i < multi_nd: cpython.tuple.PyTuple_SetItem(multi_shape, i, multi_dims[i]) @@ -783,7 +793,7 @@ cdef object vec_discd_array(irk_state *state, irk_discd_vec func, object size, c array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = oa.size @@ -826,7 +836,7 @@ cdef object vec_long_discd_array(irk_state *state, irk_discd_long_vec func, obje array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, oa) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = oa.size @@ -867,7 +877,7 @@ cdef object vec_Poisson_array(irk_state *state, irk_discdptr_vec func1, irk_disc array_data = cnp.PyArray_DATA(array) multi = cnp.PyArray_MultiIterNew(2, array, olambda) res_size = cnp.PyArray_SIZE(array) - if (cnp.PyArray_MultiIter_SIZE(multi) != res_size): + if (cnp_PyArray_MultiIter_SIZE(multi) != res_size): raise ValueError("size is not compatible with inputs") imax = olambda.size diff --git a/mkl_random/src/numpy_multiiter_workaround.h b/mkl_random/src/numpy_multiiter_workaround.h new file mode 100644 index 0000000..38a0005 --- /dev/null +++ b/mkl_random/src/numpy_multiiter_workaround.h @@ -0,0 +1,78 @@ +/* + Copyright (c) 2024-2024, Intel Corporation + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Intel Corporation nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Python.h" +#include "numpy/arrayobject.h" + +/* This header file is a work-around for issue + * https://github.com/numpy/numpy/issues/26990 + * + * It is included once in mklrandom.pyx + * + * The work-around is needed to support building with + * NumPy < 2.0.0 + * + * Once building transitions to using NumPy 2.0 only + * this file can be removed and corresponding changes + * in mklrand.pyx can be applied to always use + * `PyArray_MultiIter_SIZE`, PyArray_MultiIter_NDIM`, + * and `PyArray_MultiIter_DIMS`. + */ + +typedef struct { + PyObject_HEAD + int numiter; + npy_intp size; + npy_intp index; + int nd; + npy_intp dimensions[32]; + void **iters; +} multi_iter_proxy_st; + +npy_intp workaround_PyArray_MultiIter_SIZE(PyArrayMultiIterObject *multi) { +#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) + return PyArray_MultiIter_SIZE(multi); +#else + return ((multi_iter_proxy_st *)(multi))->size; +#endif +} + +int workaround_PyArray_MultiIter_NDIM(PyArrayMultiIterObject *multi) { +#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) + return PyArray_MultiIter_NDIM(multi); +#else + return ((multi_iter_proxy_st *)(multi))->nd; +#endif +} + +npy_intp* workaround_PyArray_MultiIter_DIMS(PyArrayMultiIterObject *multi) { +#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) + return PyArray_MultiIter_DIMS(multi); +#else + return (((multi_iter_proxy_st *)(multi))->dimensions); +#endif +} \ No newline at end of file From 2dad3ae9bc376b552e7fcf97d8e8f21271da1d5e Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Sun, 21 Jul 2024 09:56:31 -0500 Subject: [PATCH 10/14] Update copyright years --- mkl_random/src/mkl_distributions.cpp | 2 +- mkl_random/src/randomkit.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mkl_random/src/mkl_distributions.cpp b/mkl_random/src/mkl_distributions.cpp index ff24a27..a1233c5 100644 --- a/mkl_random/src/mkl_distributions.cpp +++ b/mkl_random/src/mkl_distributions.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2017-2021, Intel Corporation + Copyright (c) 2017-2024, Intel Corporation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/mkl_random/src/randomkit.cpp b/mkl_random/src/randomkit.cpp index af19167..07fea0e 100644 --- a/mkl_random/src/randomkit.cpp +++ b/mkl_random/src/randomkit.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2017-2019, Intel Corporation + Copyright (c) 2017-2024, Intel Corporation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: From 9640607a32ad7a6a7baa767e31c5a5eee4652c05 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Sun, 21 Jul 2024 09:57:21 -0500 Subject: [PATCH 11/14] Reenabled building with NumPy pre 2.0 --- .github/workflows/build-with-clang.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-with-clang.yml b/.github/workflows/build-with-clang.yml index fa5ba6d..86ad416 100644 --- a/.github/workflows/build-with-clang.yml +++ b/.github/workflows/build-with-clang.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: python: ["3.9", "3.10", "3.11", "3.12"] - numpy_build_version: ["numpy'>=2'"] + numpy_build_version: ["numpy'<2'", "numpy'>=2'"] env: ONEAPI_ROOT: /opt/intel/oneapi From 47697e33b952ae6baef8eca8c2b4eedbdd40f99f Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Mon, 22 Jul 2024 06:50:15 -0500 Subject: [PATCH 12/14] Implement work-around for the test failure due to a bug in MKL --- mkl_random/tests/test_random.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mkl_random/tests/test_random.py b/mkl_random/tests/test_random.py index fc39083..1236fff 100644 --- a/mkl_random/tests/test_random.py +++ b/mkl_random/tests/test_random.py @@ -804,7 +804,8 @@ def test_randomdist_normal(randomdist): np.testing.assert_allclose(actual, desired, atol=1e-8, rtol=1e-8) rnd.seed(randomdist.seed, brng=randomdist.brng) - actual = rnd.normal(loc=.123456789, scale=2.0, size=(3, 2), method="BoxMuller2") + workaround = rnd.normal(loc=.123456789, scale=2.0, size=(4, 2), method="BoxMuller2") + actual = workaround[:3,:] desired = np.array([[0.16673479781277187, 0.48153966449249175], [-3.4809986872165952, -0.8101190082826486], [-0.051937610825354905, 2.4088402362484342]]) @@ -902,7 +903,8 @@ def test_randomdist_standard_normal(randomdist): np.testing.assert_allclose(actual, desired, atol=1e-7, rtol=1e-10) rnd.seed(randomdist.seed, brng=randomdist.brng) - actual = rnd.standard_normal(size=(3, 2), method='BoxMuller2') + workaround = rnd.standard_normal(size=(4, 2), method='BoxMuller2') + actual = workaround[:3, :] desired = np.array([[0.021639004406385935, 0.17904143774624587], [-1.8022277381082976, -0.4667878986413243], [-0.08769719991267745, 1.1426917236242171]]) From 655e8ba81b783f657fed551fcab95e18ea37d158 Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 26 Jul 2024 13:31:21 -0500 Subject: [PATCH 13/14] Remove setting of misspelled preprocessor variable --- setup.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 6291420..d23364c 100644 --- a/setup.py +++ b/setup.py @@ -96,8 +96,7 @@ def extensions(): defs = [('_FILE_OFFSET_BITS', '64'), ('_LARGEFILE_SOURCE', '1'), ('_LARGEFILE64_SOURCE', '1'), - ("PY_ARRAY_UNIQUE_SYMBOL", "mkl_random_ext"), - ("NPY_NO_DEPRICATED_API", None)] + ("PY_ARRAY_UNIQUE_SYMBOL", "mkl_random_ext")] exts = [ Extension( @@ -110,7 +109,7 @@ def extensions(): depends = [ os.path.join("mkl_random", "src", "mkl_distributions.hpp"), os.path.join("mkl_random", "src", "randomkit.h"), - os.path.join("mkl_random", "src", "numpy.pxd") + os.path.join("mkl_random", "src", "numpy_multiiter_workaround.h") ], include_dirs = [os.path.join("mkl_random", "src"), np.get_include()] + mkl_include_dirs, libraries = libs, From f0685e08576fe2239bf27ffc96490fdf3aba337e Mon Sep 17 00:00:00 2001 From: Oleksandr Pavlyk Date: Fri, 26 Jul 2024 13:31:48 -0500 Subject: [PATCH 14/14] Added EOL, wrapped struct into preprocessor if/else --- mkl_random/src/numpy_multiiter_workaround.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mkl_random/src/numpy_multiiter_workaround.h b/mkl_random/src/numpy_multiiter_workaround.h index 38a0005..db6a325 100644 --- a/mkl_random/src/numpy_multiiter_workaround.h +++ b/mkl_random/src/numpy_multiiter_workaround.h @@ -43,6 +43,9 @@ * and `PyArray_MultiIter_DIMS`. */ +#define WORKAROUND_NEEDED (defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION)) + +#if !WORKAROUND_NEEDED typedef struct { PyObject_HEAD int numiter; @@ -52,9 +55,10 @@ typedef struct { npy_intp dimensions[32]; void **iters; } multi_iter_proxy_st; +#endif npy_intp workaround_PyArray_MultiIter_SIZE(PyArrayMultiIterObject *multi) { -#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) +#if WORKAROUND_NEEDED return PyArray_MultiIter_SIZE(multi); #else return ((multi_iter_proxy_st *)(multi))->size; @@ -62,7 +66,7 @@ npy_intp workaround_PyArray_MultiIter_SIZE(PyArrayMultiIterObject *multi) { } int workaround_PyArray_MultiIter_NDIM(PyArrayMultiIterObject *multi) { -#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) +#if WORKAROUND_NEEDED return PyArray_MultiIter_NDIM(multi); #else return ((multi_iter_proxy_st *)(multi))->nd; @@ -70,9 +74,9 @@ int workaround_PyArray_MultiIter_NDIM(PyArrayMultiIterObject *multi) { } npy_intp* workaround_PyArray_MultiIter_DIMS(PyArrayMultiIterObject *multi) { -#if defined(NPY_2_0_API_VERSION) && (NPY_API_VERSION >= NPY_2_0_API_VERSION) +#if WORKAROUND_NEEDED return PyArray_MultiIter_DIMS(multi); #else return (((multi_iter_proxy_st *)(multi))->dimensions); #endif -} \ No newline at end of file +}