Skip to content

Commit ee206fa

Browse files
authored
Merge pull request #20 from EconForge/albop/nopandas
Removed dependencies on pandas and cython.
2 parents 165b69a + a908ce4 commit ee206fa

File tree

4 files changed

+78
-153
lines changed

4 files changed

+78
-153
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ install:
2424
# Useful for debugging any issues with conda
2525
- conda info -a
2626

27-
- deps='pip nose numba numpy pandas cython scipy'
27+
- deps='pip nose numba numpy scipy'
2828
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION $deps
2929
- source activate test-environment
3030
- python setup.py install
3131
- export NUMBA_ENABLE_CUDASIM=1
3232

33-
34-
3533
script:
3634

3735
- nosetests

interpolation/smolyak/grid.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,17 @@
2929
from itertools import chain
3030
import numpy as np
3131
from scipy.linalg import lu
32-
import pandas as pd
3332
from functools import reduce
3433
from .util import *
3534

3635
## --------------- ##
3736
#- Building Blocks -#
3837
## --------------- ##
3938

40-
__all__ = ['num_grid_points', 'm_i', 'cheby2n', 's_n', 'a_chain', 'phi_chain',
41-
'smol_inds', 'build_grid', 'build_B', 'SmolyakGrid']
42-
43-
39+
__all__ = [
40+
'num_grid_points', 'm_i', 'cheby2n', 's_n', 'a_chain', 'phi_chain',
41+
'smol_inds', 'build_grid', 'build_B', 'SmolyakGrid'
42+
]
4443

4544

4645
def num_grid_points(d, mu):
@@ -63,13 +62,14 @@ def num_grid_points(d, mu):
6362
6463
"""
6564
if mu == 1:
66-
return 2*d + 1
65+
return 2 * d + 1
6766

6867
if mu == 2:
69-
return 1 + 4*d + 4*d*(d-1)/2.
68+
return 1 + 4 * d + 4 * d * (d - 1) / 2.
7069

7170
if mu == 3:
72-
return 1 + 8*d + 12*d*(d-1)/2. + 8*d*(d-1)*(d-2)/6.
71+
return 1 + 8 * d + 12 * d * (d - 1) / 2. + 8 * d * (d - 1) * (
72+
d - 2) / 6.
7373

7474

7575
def m_i(i):
@@ -105,6 +105,7 @@ def m_i(i):
105105
else:
106106
return 2**(i - 1) + 1
107107

108+
108109
def chebyvalto(x, n, kind=1.):
109110
"""
110111
Computes first :math:`n` Chebychev polynomials of the first kind
@@ -138,11 +139,11 @@ def chebyvalto(x, n, kind=1.):
138139
x = np.asarray(x)
139140
row, col = x.shape
140141

141-
ret_matrix = np.zeros((row, col * (n-1)))
142+
ret_matrix = np.zeros((row, col * (n - 1)))
142143

143144
init = np.ones((row, col))
144145
ret_matrix[:, :col] = x * kind
145-
ret_matrix[:, col:2*col] = 2 * x * ret_matrix[:, :col] - init
146+
ret_matrix[:, col:2 * col] = 2 * x * ret_matrix[:, :col] - init
146147

147148
for i in range(3, n):
148149
ret_matrix[:, col*(i-1): col*(i)] = 2 * x * ret_matrix[:, col*(i-2):col*(i-1)] \
@@ -185,8 +186,8 @@ def cheby2n(x, n, kind=1.):
185186
results = np.zeros((n + 1, ) + dim)
186187
results[0, ...] = np.ones(dim)
187188
results[1, ...] = x * kind
188-
for i in range(2, n+1):
189-
results[i, ...] = 2 * x * results[i-1, ...] - results[i-2, ...]
189+
for i in range(2, n + 1):
190+
results[i, ...] = 2 * x * results[i - 1, ...] - results[i - 2, ...]
190191
return results
191192

192193

@@ -212,14 +213,14 @@ def s_n(n):
212213
return np.array([0.])
213214

214215
# Apply the necessary transformation to get the nested sequence
215-
m_i = 2**(n-1) + 1
216+
m_i = 2**(n - 1) + 1
216217

217218
# Create an array of values that will be passed in to calculate
218219
# the set of values
219220
comp_vals = np.arange(1., m_i + 1.)
220221

221222
# Values are - cos(pi(j-1)/(n-1)) for j in [1, 2, ..., n]
222-
vals = -1. * np.cos(np.pi*(comp_vals - 1.)/(m_i-1.))
223+
vals = -1. * np.cos(np.pi * (comp_vals - 1.) / (m_i - 1.))
223224
vals[np.where(np.abs(vals) < 1e-14)] = 0.0
224225

225226
return vals
@@ -294,14 +295,15 @@ def phi_chain(n):
294295
aphi_chain[2] = [2, 3]
295296

296297
curr_val = 4
297-
for i in range(3, n+1):
298-
end_val = 2**(i-1) + 1
299-
temp = range(curr_val, end_val+1)
298+
for i in range(3, n + 1):
299+
end_val = 2**(i - 1) + 1
300+
temp = range(curr_val, end_val + 1)
300301
aphi_chain[i] = temp
301-
curr_val = end_val+1
302+
curr_val = end_val + 1
302303

303304
return aphi_chain
304305

306+
305307
## ---------------------- ##
306308
#- Construction Utilities -#
307309
## ---------------------- ##
@@ -344,18 +346,20 @@ def smol_inds(d, mu):
344346

345347
# find all (i1, i2, ... id) such that their sum is in range
346348
# we want; this will cut down on later iterations
347-
poss_inds = [el for el in combinations_with_replacement(possible_values, d)
348-
if d < sum(el) <= d+max_mu]
349+
poss_inds = [
350+
el for el in combinations_with_replacement(possible_values, d)
351+
if d < sum(el) <= d + max_mu
352+
]
349353

350354
if isinstance(mu, int):
351355
true_inds = [[el for el in permute(list(val))] for val in poss_inds]
352356
else:
353-
true_inds = [[el for el in permute(list(val)) if all(el <= mu+1)]
357+
true_inds = [[el for el in permute(list(val)) if all(el <= mu + 1)]
354358
for val in poss_inds]
355359

356360
# Add the d dimension 1 array so that we don't repeat it a bunch
357361
# of times
358-
true_inds.extend([[[1]*d]])
362+
true_inds.extend([[[1] * d]])
359363

360364
tinds = list(chain.from_iterable(true_inds))
361365

@@ -461,8 +465,7 @@ def build_grid(d, mu, inds=None):
461465
# inds.append(el)
462466
points.extend(list(product(*temp)))
463467

464-
# TODO do we need a pandas grid here?
465-
grid = pd.lib.to_object_array_tuples(points).astype(float)
468+
grid = np.array(points)
466469

467470
return grid
468471

@@ -519,8 +522,7 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
519522
npts = pts.shape[0]
520523
B = np.empty((npts, npolys), order='F')
521524
for ind, comb in enumerate(b_inds):
522-
B[:, ind] = reduce(mul, [Ts[comb[i] - 1, i, :]
523-
for i in range(d)])
525+
B[:, ind] = reduce(mul, [Ts[comb[i] - 1, i, :] for i in range(d)])
524526

525527
if deriv:
526528
# TODO: test this. I am going to bed.
@@ -533,14 +535,13 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
533535

534536
for i in range(d):
535537
for ind, comb in enumerate(b_inds):
536-
der_B[ind, i, :] = reduce(mul, [(Ts[comb[k] - 1, k, :] if i != k
537-
else Us[comb[k] - 1, k, :])
538-
for k in range(d)])
538+
der_B[ind, i, :] = reduce(
539+
mul, [(Ts[comb[k] - 1, k, :]
540+
if i != k else Us[comb[k] - 1, k, :])
541+
for k in range(d)])
539542

540543
return B, der_B
541544

542-
543-
544545
return B
545546

546547

@@ -571,7 +572,6 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
571572
# if mu==2:
572573
# for i in range(d-1):
573574

574-
575575
# mult_inds = np.hstack([np.arange(i+1, d), np.arange(d + (i+1), 2*d)])
576576

577577
# temp1 = easy_B[:, i].reshape(npts, 1) * easy_B[:, mult_inds]
@@ -581,7 +581,6 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
581581
# B[:, B_col_mrk: B_col_mrk + new_cols] = np.hstack([temp1, temp2])
582582
# B_col_mrk = B_col_mrk + new_cols
583583

584-
585584
# #-----------------------------------------------------------------#
586585
# #-----------------------------------------------------------------#
587586
# # This part will be the general section. Above I am trying to
@@ -633,12 +632,11 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
633632

634633
# return B
635634

636-
637-
638635
## ------------------ ##
639636
#- Class: SmolyakGrid -#
640637
## ------------------ ##
641638

639+
642640
class SmolyakGrid(object):
643641
"""
644642
This class currently takes a dimension and a degree of polynomial
@@ -706,6 +704,7 @@ class SmolyakGrid(object):
706704
B: 0.68% non-zero
707705
708706
"""
707+
709708
def __init__(self, d, mu, lb=None, ub=None):
710709
self.d = d
711710

@@ -771,7 +770,7 @@ def __init__(self, d, mu, lb=None, ub=None):
771770
def __repr__(self):
772771
npoints = self.cube_grid.shape[0]
773772
nz_pts = np.count_nonzero(self.B)
774-
pct_nz = nz_pts / (npoints ** 2.)
773+
pct_nz = nz_pts / (npoints**2.)
775774

776775
if isinstance(self.mu, int):
777776
msg = "Smolyak Grid:\n\td: {0} \n\tmu: {1} \n\tnpoints: {2}"
@@ -797,10 +796,10 @@ def dom2cube(self, pts):
797796
lb = self.lb
798797
ub = self.ub
799798

800-
centers = lb + (ub - lb)/2
801-
radii = (ub - lb)/2
799+
centers = lb + (ub - lb) / 2
800+
radii = (ub - lb) / 2
802801

803-
trans_pts = (pts-centers)/radii
802+
trans_pts = (pts - centers) / radii
804803

805804
return trans_pts
806805

@@ -815,10 +814,10 @@ def cube2dom(self, pts):
815814
lb = self.lb
816815
ub = self.ub
817816

818-
centers = lb + (ub - lb)/2
819-
radii = (ub - lb)/2
817+
centers = lb + (ub - lb) / 2
818+
radii = (ub - lb) / 2
820819

821-
inv_trans_pts = pts*radii + centers
820+
inv_trans_pts = pts * radii + centers
822821

823822
return inv_trans_pts
824823

0 commit comments

Comments
 (0)