From b571bc10752b80b5338eed2d37045858784dae1a Mon Sep 17 00:00:00 2001 From: Kevin Sheppard Date: Wed, 22 Dec 2021 09:49:10 +0000 Subject: [PATCH] PERF: Speed up check_constraint checks Remove python api calls from check_constraint xref #20636 --- numpy/random/_common.pyx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/numpy/random/_common.pyx b/numpy/random/_common.pyx index 4c34c503cd2f..86828e200241 100644 --- a/numpy/random/_common.pyx +++ b/numpy/random/_common.pyx @@ -5,6 +5,7 @@ from cpython cimport PyFloat_AsDouble import sys import numpy as np cimport numpy as np +cimport numpy.math as npmath from libc.stdint cimport uintptr_t @@ -398,10 +399,10 @@ cdef int check_array_constraint(np.ndarray val, object name, constraint_type con cdef int check_constraint(double val, object name, constraint_type cons) except -1: cdef bint is_nan if cons == CONS_NON_NEGATIVE: - if not np.isnan(val) and np.signbit(val): + if not npmath.isnan(val) and npmath.signbit(val): raise ValueError(name + " < 0") elif cons == CONS_POSITIVE or cons == CONS_POSITIVE_NOT_NAN: - if cons == CONS_POSITIVE_NOT_NAN and np.isnan(val): + if cons == CONS_POSITIVE_NOT_NAN and npmath.isnan(val): raise ValueError(name + " must not be NaN") elif val <= 0: raise ValueError(name + " <= 0")