Import fix for Py3

This commit is contained in:
Mike Croucher 2015-02-27 17:35:26 +00:00
parent 6554c32d23
commit 786feded41
2 changed files with 45 additions and 44 deletions

View file

@ -2,7 +2,7 @@
# Licensed under the BSD 3-clause license (see LICENSE.txt)
import numpy as np
from scipy import weave
#from scipy import weave
def std_norm_pdf(x):
"""Standard Gaussian density function"""
@ -37,41 +37,42 @@ def std_norm_cdf(x):
cdf_x = cdf_x.reshape(x_shape)
return cdf_x
def std_norm_cdf_weave(x):
"""
Cumulative standard Gaussian distribution
Based on Abramowitz, M. and Stegun, I. (1970)
A weave implementation of std_norm_cdf, which is faster. this is unused,
because of the difficulties of a weave dependency. (see github issue #94)
"""
#Generalize for many x
x = np.asarray(x).copy()
cdf_x = np.zeros_like(x)
N = x.size
support_code = "#include <math.h>"
code = """
double sign, t, erf;
for (int i=0; i<N; i++){
sign = 1.0;
if (x[i] < 0.0){
sign = -1.0;
x[i] = -x[i];
}
x[i] = x[i]/sqrt(2.0);
t = 1.0/(1.0 + 0.3275911*x[i]);
erf = 1. - exp(-x[i]*x[i])*t*(0.254829592 + t*(-0.284496736 + t*(1.421413741 + t*(-1.453152027 + t*(1.061405429)))));
//return_val = 0.5*(1.0 + sign*erf);
cdf_x[i] = 0.5*(1.0 + sign*erf);
}
"""
weave.inline(code, arg_names=['x', 'cdf_x', 'N'], support_code=support_code)
return cdf_x
#Commented out since this isn't used...and since it breaks Py3 compatibility
#def std_norm_cdf_weave(x):
# """
# Cumulative standard Gaussian distribution
# Based on Abramowitz, M. and Stegun, I. (1970)
#
# A weave implementation of std_norm_cdf, which is faster. this is unused,
# because of the difficulties of a weave dependency. (see github issue #94)
#
# """
# #Generalize for many x
# x = np.asarray(x).copy()
# cdf_x = np.zeros_like(x)
# N = x.size
# support_code = "#include <math.h>"
# code = """
#
# double sign, t, erf;
# for (int i=0; i<N; i++){
# sign = 1.0;
# if (x[i] < 0.0){
# sign = -1.0;
# x[i] = -x[i];
# }
# x[i] = x[i]/sqrt(2.0);
#
# t = 1.0/(1.0 + 0.3275911*x[i]);
#
# erf = 1. - exp(-x[i]*x[i])*t*(0.254829592 + t*(-0.284496736 + t*(1.421413741 + t*(-1.453152027 + t*(1.061405429)))));
#
# //return_val = 0.5*(1.0 + sign*erf);
# cdf_x[i] = 0.5*(1.0 + sign*erf);
# }
# """
# weave.inline(code, arg_names=['x', 'cdf_x', 'N'], support_code=support_code)
# return cdf_x
def inv_std_norm_cdf(x):
"""