29
29
from itertools import chain
30
30
import numpy as np
31
31
from scipy .linalg import lu
32
- import pandas as pd
33
32
from functools import reduce
34
33
from .util import *
35
34
36
35
## --------------- ##
37
36
#- Building Blocks -#
38
37
## --------------- ##
39
38
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
+ ]
44
43
45
44
46
45
def num_grid_points (d , mu ):
@@ -63,13 +62,14 @@ def num_grid_points(d, mu):
63
62
64
63
"""
65
64
if mu == 1 :
66
- return 2 * d + 1
65
+ return 2 * d + 1
67
66
68
67
if mu == 2 :
69
- return 1 + 4 * d + 4 * d * ( d - 1 ) / 2.
68
+ return 1 + 4 * d + 4 * d * ( d - 1 ) / 2.
70
69
71
70
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.
73
73
74
74
75
75
def m_i (i ):
@@ -105,6 +105,7 @@ def m_i(i):
105
105
else :
106
106
return 2 ** (i - 1 ) + 1
107
107
108
+
108
109
def chebyvalto (x , n , kind = 1. ):
109
110
"""
110
111
Computes first :math:`n` Chebychev polynomials of the first kind
@@ -138,11 +139,11 @@ def chebyvalto(x, n, kind=1.):
138
139
x = np .asarray (x )
139
140
row , col = x .shape
140
141
141
- ret_matrix = np .zeros ((row , col * (n - 1 )))
142
+ ret_matrix = np .zeros ((row , col * (n - 1 )))
142
143
143
144
init = np .ones ((row , col ))
144
145
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
146
147
147
148
for i in range (3 , n ):
148
149
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.):
185
186
results = np .zeros ((n + 1 , ) + dim )
186
187
results [0 , ...] = np .ones (dim )
187
188
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 , ...]
190
191
return results
191
192
192
193
@@ -212,14 +213,14 @@ def s_n(n):
212
213
return np .array ([0. ])
213
214
214
215
# Apply the necessary transformation to get the nested sequence
215
- m_i = 2 ** (n - 1 ) + 1
216
+ m_i = 2 ** (n - 1 ) + 1
216
217
217
218
# Create an array of values that will be passed in to calculate
218
219
# the set of values
219
220
comp_vals = np .arange (1. , m_i + 1. )
220
221
221
222
# 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. ))
223
224
vals [np .where (np .abs (vals ) < 1e-14 )] = 0.0
224
225
225
226
return vals
@@ -294,14 +295,15 @@ def phi_chain(n):
294
295
aphi_chain [2 ] = [2 , 3 ]
295
296
296
297
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 )
300
301
aphi_chain [i ] = temp
301
- curr_val = end_val + 1
302
+ curr_val = end_val + 1
302
303
303
304
return aphi_chain
304
305
306
+
305
307
## ---------------------- ##
306
308
#- Construction Utilities -#
307
309
## ---------------------- ##
@@ -344,18 +346,20 @@ def smol_inds(d, mu):
344
346
345
347
# find all (i1, i2, ... id) such that their sum is in range
346
348
# 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
+ ]
349
353
350
354
if isinstance (mu , int ):
351
355
true_inds = [[el for el in permute (list (val ))] for val in poss_inds ]
352
356
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 )]
354
358
for val in poss_inds ]
355
359
356
360
# Add the d dimension 1 array so that we don't repeat it a bunch
357
361
# of times
358
- true_inds .extend ([[[1 ]* d ]])
362
+ true_inds .extend ([[[1 ] * d ]])
359
363
360
364
tinds = list (chain .from_iterable (true_inds ))
361
365
@@ -461,8 +465,7 @@ def build_grid(d, mu, inds=None):
461
465
# inds.append(el)
462
466
points .extend (list (product (* temp )))
463
467
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 )
466
469
467
470
return grid
468
471
@@ -519,8 +522,7 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
519
522
npts = pts .shape [0 ]
520
523
B = np .empty ((npts , npolys ), order = 'F' )
521
524
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 )])
524
526
525
527
if deriv :
526
528
# TODO: test this. I am going to bed.
@@ -533,14 +535,13 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
533
535
534
536
for i in range (d ):
535
537
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 )])
539
542
540
543
return B , der_B
541
544
542
-
543
-
544
545
return B
545
546
546
547
@@ -571,7 +572,6 @@ def build_B(d, mu, pts, b_inds=None, deriv=False):
571
572
# if mu==2:
572
573
# for i in range(d-1):
573
574
574
-
575
575
# mult_inds = np.hstack([np.arange(i+1, d), np.arange(d + (i+1), 2*d)])
576
576
577
577
# 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):
581
581
# B[:, B_col_mrk: B_col_mrk + new_cols] = np.hstack([temp1, temp2])
582
582
# B_col_mrk = B_col_mrk + new_cols
583
583
584
-
585
584
# #-----------------------------------------------------------------#
586
585
# #-----------------------------------------------------------------#
587
586
# # 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):
633
632
634
633
# return B
635
634
636
-
637
-
638
635
## ------------------ ##
639
636
#- Class: SmolyakGrid -#
640
637
## ------------------ ##
641
638
639
+
642
640
class SmolyakGrid (object ):
643
641
"""
644
642
This class currently takes a dimension and a degree of polynomial
@@ -706,6 +704,7 @@ class SmolyakGrid(object):
706
704
B: 0.68% non-zero
707
705
708
706
"""
707
+
709
708
def __init__ (self , d , mu , lb = None , ub = None ):
710
709
self .d = d
711
710
@@ -771,7 +770,7 @@ def __init__(self, d, mu, lb=None, ub=None):
771
770
def __repr__ (self ):
772
771
npoints = self .cube_grid .shape [0 ]
773
772
nz_pts = np .count_nonzero (self .B )
774
- pct_nz = nz_pts / (npoints ** 2. )
773
+ pct_nz = nz_pts / (npoints ** 2. )
775
774
776
775
if isinstance (self .mu , int ):
777
776
msg = "Smolyak Grid:\n \t d: {0} \n \t mu: {1} \n \t npoints: {2}"
@@ -797,10 +796,10 @@ def dom2cube(self, pts):
797
796
lb = self .lb
798
797
ub = self .ub
799
798
800
- centers = lb + (ub - lb )/ 2
801
- radii = (ub - lb )/ 2
799
+ centers = lb + (ub - lb ) / 2
800
+ radii = (ub - lb ) / 2
802
801
803
- trans_pts = (pts - centers )/ radii
802
+ trans_pts = (pts - centers ) / radii
804
803
805
804
return trans_pts
806
805
@@ -815,10 +814,10 @@ def cube2dom(self, pts):
815
814
lb = self .lb
816
815
ub = self .ub
817
816
818
- centers = lb + (ub - lb )/ 2
819
- radii = (ub - lb )/ 2
817
+ centers = lb + (ub - lb ) / 2
818
+ radii = (ub - lb ) / 2
820
819
821
- inv_trans_pts = pts * radii + centers
820
+ inv_trans_pts = pts * radii + centers
822
821
823
822
return inv_trans_pts
824
823
0 commit comments