Skip to content

Commit 77a6480

Browse files
committed
Remove functional.index_iterator
1 parent d8383ae commit 77a6480

3 files changed

Lines changed: 18 additions & 36 deletions

File tree

FIAT/barycentric_interpolation.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import numpy
1010
from FIAT import reference_element, expansions, polynomial_set
11-
from FIAT.functional import index_iterator
1211

1312

1413
def get_lagrange_points(nodes):
@@ -94,33 +93,28 @@ def _tabulate_on_cell(self, n, pts, order=0, cell=0, direction=None):
9493

9594
class LagrangePolynomialSet(polynomial_set.PolynomialSet):
9695

97-
def __init__(self, ref_el, pts, shape=tuple()):
96+
def __init__(self, ref_el, pts, shape=()):
9897
if ref_el.get_shape() != reference_element.LINE:
9998
raise ValueError("Invalid reference element type.")
10099

101100
expansion_set = LagrangeLineExpansionSet(ref_el, pts)
102101
degree = expansion_set.degree
103-
if shape == tuple():
104-
num_components = 1
105-
else:
106-
flat_shape = numpy.ravel(shape)
107-
num_components = numpy.prod(flat_shape)
102+
num_components = numpy.prod(shape, dtype=int)
108103
num_exp_functions = expansion_set.get_num_members(degree)
109104
num_members = num_components * num_exp_functions
110105
embedded_degree = degree
111106

112107
# set up coefficients
113-
if shape == tuple():
108+
if shape == ():
114109
coeffs = numpy.eye(num_members, dtype="d")
115110
else:
116111
coeffs_shape = (num_members, *shape, num_exp_functions)
117112
coeffs = numpy.zeros(coeffs_shape, "d")
118-
# use functional's index_iterator function
119-
cur_bf = 0
120-
for idx in index_iterator(shape):
121-
for exp_bf in range(num_exp_functions):
122-
cur_idx = (cur_bf, *idx, exp_bf)
123-
coeffs[cur_idx] = 1.0
124-
cur_bf += 1
113+
cur = 0
114+
exp_bf = range(num_exp_functions)
115+
for idx in numpy.ndindex(shape):
116+
cur_bf = range(cur, cur+num_exp_functions)
117+
coeffs[(cur_bf, *idx, exp_bf)] = 1.0
118+
cur += num_exp_functions
125119

126120
super().__init__(ref_el, degree, embedded_degree, expansion_set, coeffs)

FIAT/functional.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
from FIAT import polynomial_set, jacobi, quadrature_schemes
1919

2020

21-
def index_iterator(shp):
22-
"""Constructs a generator iterating over all indices in
23-
shp in generalized column-major order So if shp = (2,2), then we
24-
construct the sequence (0,0),(0,1),(1,0),(1,1)"""
25-
return numpy.ndindex(shp)
26-
27-
2821
class Functional(object):
2922
r"""Abstract class representing a linear functional.
3023
All FIAT functionals are discrete in the sense that
@@ -384,7 +377,7 @@ def __init__(self, ref_el, Q, f_at_qpts, nm=None):
384377
self.f_at_qpts = f_at_qpts
385378
qpts, qwts = Q.get_points(), Q.get_weights()
386379
weights = numpy.transpose(numpy.multiply(f_at_qpts, qwts), (-1,) + tuple(range(len(shp))))
387-
alphas = list(index_iterator(shp))
380+
alphas = list(numpy.ndindex(shp))
388381

389382
pt_dict = {tuple(pt): [(wt[alpha], alpha) for alpha in alphas] for pt, wt in zip(qpts, weights)}
390383
Functional.__init__(self, ref_el, shp, pt_dict, {}, nm or "FrobeniusIntegralMoment")
@@ -494,7 +487,7 @@ def __init__(self, ref_el, Q, f_at_qpts):
494487
weights = numpy.multiply(f_at_qpts, Q.get_weights()).T
495488

496489
alphas = tuple(map(tuple, numpy.eye(sd, dtype=int)))
497-
dpt_dict = {tuple(pt): [(wt[i], alphas[j], (i, j)) for i, j in index_iterator(shp)]
490+
dpt_dict = {tuple(pt): [(wt[i], alphas[j], (i, j)) for i, j in numpy.ndindex(shp)]
498491
for pt, wt in zip(points, weights)}
499492

500493
super().__init__(ref_el, tuple(), {}, dpt_dict, "IntegralMomentOfDivergence")
@@ -655,7 +648,7 @@ def __init__(self, ref_el, v, w, pt):
655648
wvT = numpy.outer(w, v)
656649
shp = wvT.shape
657650

658-
pt_dict = {tuple(pt): [(wvT[idx], idx) for idx in index_iterator(shp)]}
651+
pt_dict = {tuple(pt): [(wvT[idx], idx) for idx in numpy.ndindex(shp)]}
659652

660653
super().__init__(ref_el, shp, pt_dict, {}, "PointwiseInnerProductEval")
661654

FIAT/polynomial_set.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import numpy
1919
from itertools import chain
2020
from FIAT import expansions
21-
from FIAT.functional import index_iterator
2221

2322

2423
def mis(m, n):
@@ -113,25 +112,21 @@ class ONPolynomialSet(PolynomialSet):
113112
identity matrix of coefficients. Can be used to specify ON bases
114113
for vector- and tensor-valued sets as well.
115114
"""
116-
def __init__(self, ref_el, degree, shape=tuple(), **kwargs):
115+
def __init__(self, ref_el, degree, shape=(), **kwargs):
117116
expansion_set = expansions.ExpansionSet(ref_el, **kwargs)
118-
if shape == tuple():
119-
num_components = 1
120-
else:
121-
flat_shape = numpy.ravel(shape)
122-
num_components = numpy.prod(flat_shape)
117+
num_components = numpy.prod(shape, dtype=int)
123118
num_exp_functions = expansion_set.get_num_members(degree)
124119
num_members = num_components * num_exp_functions
125120
embedded_degree = degree
126121

127122
# set up coefficients
128-
if shape == tuple():
123+
if shape == ():
129124
coeffs = numpy.eye(num_members)
130125
else:
131126
coeffs = numpy.zeros((num_members, *shape, num_exp_functions))
132127
cur = 0
133128
exp_bf = range(num_exp_functions)
134-
for idx in index_iterator(shape):
129+
for idx in numpy.ndindex(shape):
135130
cur_bf = range(cur, cur+num_exp_functions)
136131
coeffs[(cur_bf, *idx, exp_bf)] = 1.0
137132
cur += num_exp_functions
@@ -243,7 +238,7 @@ def __init__(self, ref_el, degree, size=None, **kwargs):
243238
coeffs = numpy.zeros((num_members, *shape, num_exp_functions))
244239
cur = 0
245240
exp_bf = range(num_exp_functions)
246-
for i, j in index_iterator(shape):
241+
for i, j in numpy.ndindex(shape):
247242
if i > j:
248243
continue
249244
cur_bf = range(cur, cur+num_exp_functions)
@@ -275,7 +270,7 @@ def __init__(self, ref_el, degree, size=None, **kwargs):
275270
coeffs = numpy.zeros((num_members, *shape, num_exp_functions))
276271
cur = 0
277272
exp_bf = range(num_exp_functions)
278-
for i, j in index_iterator(shape):
273+
for i, j in numpy.ndindex(shape):
279274
if i == size-1 and j == size-1:
280275
continue
281276
cur_bf = range(cur, cur+num_exp_functions)

0 commit comments

Comments
 (0)