Skip to content

Commit d5c3da1

Browse files
committed
Clone xlogy from scipy
1 parent 6ee53ec commit d5c3da1

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This directory is a fork from scipy 0.13.3
2+
See: https://github.com/scipy/scipy

ylearn/sklearn_ex/cloned/scipy/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#include <numpy/npy_math.h>
6+
7+
#ifdef __cplusplus
8+
}
9+
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*-cython-*-
2+
#
3+
# Common functions required when doing complex arithmetic with Cython.
4+
#
5+
6+
cimport numpy as np
7+
cimport libc.math
8+
9+
cdef extern from "_complexstuff.h":
10+
double npy_cabs(np.npy_cdouble z) nogil
11+
np.npy_cdouble npy_clog(np.npy_cdouble z) nogil
12+
np.npy_cdouble npy_cexp(np.npy_cdouble z) nogil
13+
double npy_log1p(double x) nogil
14+
int npy_isnan(double x) nogil
15+
int npy_isinf(double x) nogil
16+
int npy_isfinite(double x) nogil
17+
double inf "NPY_INFINITY"
18+
double pi "NPY_PI"
19+
double nan "NPY_NAN"
20+
21+
ctypedef double complex double_complex
22+
23+
ctypedef fused number_t:
24+
double
25+
double_complex
26+
27+
cdef inline bint zisnan(number_t x) nogil:
28+
if number_t is double_complex:
29+
return npy_isnan(x.real) or npy_isnan(x.imag)
30+
else:
31+
return npy_isnan(x)
32+
33+
cdef inline bint zisfinite(number_t x) nogil:
34+
if number_t is double_complex:
35+
return npy_isfinite(x.real) and npy_isfinite(x.imag)
36+
else:
37+
return npy_isfinite(x)
38+
39+
cdef inline bint zisinf(number_t x) nogil:
40+
if number_t is double_complex:
41+
return not zisnan(x) and not zisfinite(x)
42+
else:
43+
return npy_isinf(x)
44+
45+
cdef inline double zabs(number_t x) nogil:
46+
if number_t is double_complex:
47+
return npy_cabs((<np.npy_cdouble*>&x)[0])
48+
else:
49+
return libc.math.fabs(x)
50+
51+
cdef inline number_t zlog(number_t x) nogil:
52+
cdef np.npy_cdouble r
53+
if number_t is double_complex:
54+
r = npy_clog((<np.npy_cdouble*>&x)[0])
55+
return (<double_complex*>&r)[0]
56+
else:
57+
return libc.math.log(x)
58+
59+
cdef inline number_t zexp(number_t x) nogil:
60+
cdef np.npy_cdouble r
61+
if number_t is double_complex:
62+
r = npy_cexp((<np.npy_cdouble*>&x)[0])
63+
return (<double_complex*>&r)[0]
64+
else:
65+
return libc.math.exp(x)
66+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*-cython-*-
2+
3+
from ._complexstuff cimport zlog, zisnan, number_t
4+
5+
cdef inline number_t xlogy(number_t x, number_t y) nogil:
6+
if x == 0 and not zisnan(y):
7+
return 0
8+
else:
9+
return x * zlog(y)
10+

ylearn/sklearn_ex/cloned/tree/_criterion.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ cimport numpy as np
2525
np.import_array()
2626

2727
from numpy.math cimport INFINITY
28-
from scipy.special.cython_special cimport xlogy
28+
## from scipy.special.cython_special cimport xlogy
29+
from ..scipy._xlogy cimport xlogy
2930

3031
from ._utils cimport log
3132
from ._utils cimport WeightedMedianCalculator

0 commit comments

Comments
 (0)