Skip to content

Commit a7adeed

Browse files
author
John Halloran
committed
refactor: use broadcasting instead of np.tile in apply_interpolation_matrix
1 parent b909dcb commit a7adeed

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/diffpy/snmf/snmf_class.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,12 @@ def apply_interpolation_matrix(self, components=None, weights=None, stretch=None
389389

390390
# Compute scaled indices
391391
stretch_flat = stretch.reshape(1, self.n_signals * self.n_components) ** -1
392-
stretch_tiled = np.tile(stretch_flat, (self.signal_length, 1))
393392

394393
# Compute `fractional_indices`
395-
fractional_indices = (
396-
np.tile(np.arange(self.signal_length)[:, None], (1, self.n_signals * self.n_components))
397-
* stretch_tiled
398-
)
394+
fractional_indices = np.arange(self.signal_length)[:, None] * stretch_flat
399395

400396
# Weighting matrix
401397
weights_flat = weights.reshape(1, self.n_signals * self.n_components)
402-
weights_tiled = np.tile(weights_flat, (self.signal_length, 1))
403398

404399
# Bias for indexing into reshaped components
405400
# TODO break this up or describe what it does better
@@ -439,17 +434,17 @@ def apply_interpolation_matrix(self, components=None, weights=None, stretch=None
439434
unweighted_stretched_comps = (
440435
comp_values_1 * (1 - fractional_floor_indices) + comp_values_2 * fractional_floor_indices
441436
)
442-
stretched_components = unweighted_stretched_comps * weights_tiled # Apply weighting
437+
stretched_components = unweighted_stretched_comps * weights_flat # Apply weighting
443438

444439
# Compute first derivative
445-
di = -fractional_indices * stretch_tiled
440+
di = -fractional_indices * stretch_flat
446441
d_comps_unweighted = comp_values_1 * (-di) + comp_values_2 * di
447-
d_stretched_components = d_comps_unweighted * weights_tiled
442+
d_stretched_components = d_comps_unweighted * weights_flat
448443

449444
# Compute second derivative
450-
ddi = -di * stretch_tiled * 2
445+
ddi = -di * stretch_flat * 2
451446
dd_comps_unweighted = comp_values_1 * (-ddi) + comp_values_2 * ddi
452-
dd_stretched_components = dd_comps_unweighted * weights_tiled
447+
dd_stretched_components = dd_comps_unweighted * weights_flat
453448

454449
return stretched_components, d_stretched_components, dd_stretched_components
455450

0 commit comments

Comments
 (0)