Skip to content

Commit 47d69a8

Browse files
fs446hagenw
authored andcommitted
Update wfs.py in fd/td, util.py
inner1d -> einsum pep8 corr
1 parent bc0eb0f commit 47d69a8

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

sfs/fd/wfs.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def plot(d, selection, secondary_source):
3232
3333
"""
3434
import numpy as _np
35-
from numpy.core.umath_tests import inner1d as _inner1d
3635
from scipy.special import hankel2 as _hankel2
3736

3837
from . import secondary_source_line as _secondary_source_line
@@ -91,7 +90,7 @@ def line_2d(omega, x0, n0, xs, *, c=None):
9190
k = _util.wavenumber(omega, c)
9291
ds = x0 - xs
9392
r = _np.linalg.norm(ds, axis=1)
94-
d = -1j/2 * k * _inner1d(ds, n0) / r * _hankel2(1, k * r)
93+
d = -1j / 2 * k * _np.einsum('ij,ij->i', ds, n0) / r * _hankel2(1, k * r)
9594
selection = _util.source_selection_line(n0, x0, xs)
9695
return d, selection, _secondary_source_line(omega, c)
9796

@@ -147,7 +146,8 @@ def _point(omega, x0, n0, xs, *, c=None):
147146
k = _util.wavenumber(omega, c)
148147
ds = x0 - xs
149148
r = _np.linalg.norm(ds, axis=1)
150-
d = 1j * k * _inner1d(ds, n0) / r ** (3 / 2) * _np.exp(-1j * k * r)
149+
d = 1j * k * _np.einsum('ij,ij->i', ds, n0) / r ** (3 / 2) * _np.exp(
150+
-1j * k * r)
151151
selection = _util.source_selection_point(n0, x0, xs)
152152
return d, selection, _secondary_source_point(omega, c)
153153

@@ -235,7 +235,7 @@ def point_25d(omega, x0, n0, xs, xref=[0, 0, 0], c=None, omalias=None):
235235
preeq_25d(omega, omalias, c) *
236236
_np.sqrt(8 * _np.pi) *
237237
_np.sqrt((r * s) / (r + s)) *
238-
_inner1d(n0, ds) / s *
238+
_np.einsum('ij,ij->i', ds, n0) / s *
239239
_np.exp(-1j * k * s) / (4 * _np.pi * s))
240240
selection = _util.source_selection_point(n0, x0, xs)
241241
return d, selection, _secondary_source_point(omega, c)
@@ -317,7 +317,7 @@ def point_25d_legacy(omega, x0, n0, xs, xref=[0, 0, 0], c=None, omalias=None):
317317
r = _np.linalg.norm(ds, axis=1)
318318
d = (
319319
preeq_25d(omega, omalias, c) *
320-
_np.sqrt(_np.linalg.norm(xref - x0)) * _inner1d(ds, n0) /
320+
_np.sqrt(_np.linalg.norm(xref - x0)) * _np.einsum('ij,ij->i', ds, n0) /
321321
r ** (3 / 2) * _np.exp(-1j * k * r))
322322
selection = _util.source_selection_point(n0, x0, xs)
323323
return d, selection, _secondary_source_point(omega, c)
@@ -500,7 +500,8 @@ def _focused(omega, x0, n0, xs, ns, *, c=None):
500500
k = _util.wavenumber(omega, c)
501501
ds = x0 - xs
502502
r = _np.linalg.norm(ds, axis=1)
503-
d = 1j * k * _inner1d(ds, n0) / r ** (3 / 2) * _np.exp(1j * k * r)
503+
d = 1j * k * _np.einsum('ij,ij->i', ds, n0) / r ** (3 / 2) * _np.exp(
504+
1j * k * r)
504505
selection = _util.source_selection_focused(ns, x0, xs)
505506
return d, selection, _secondary_source_point(omega, c)
506507

@@ -570,7 +571,7 @@ def focused_25d(omega, x0, n0, xs, ns, *, xref=[0, 0, 0], c=None,
570571
r = _np.linalg.norm(ds, axis=1)
571572
d = (
572573
preeq_25d(omega, omalias, c) *
573-
_np.sqrt(_np.linalg.norm(xref - x0)) * _inner1d(ds, n0) /
574+
_np.sqrt(_np.linalg.norm(xref - x0)) * _np.einsum('ij,ij->i', ds, n0) /
574575
r ** (3 / 2) * _np.exp(1j * k * r))
575576
selection = _util.source_selection_focused(ns, x0, xs)
576577
return d, selection, _secondary_source_point(omega, c)
@@ -683,22 +684,22 @@ def soundfigure_3d(omega, x0, n0, figure, npw=[0, 0, 1], *, c=None):
683684
figure = _np.fft.fftshift(figure, axes=(0, 1)) # sign of spatial DFT
684685
figure = _np.fft.fft2(figure)
685686
# wavenumbers
686-
kx = _np.fft.fftfreq(nx, 1./nx)
687-
ky = _np.fft.fftfreq(ny, 1./ny)
687+
kx = _np.fft.fftfreq(nx, 1. / nx)
688+
ky = _np.fft.fftfreq(ny, 1. / ny)
688689
# shift spectrum due to desired plane wave
689-
figure = _np.roll(figure, int(k*npw[0]), axis=0)
690-
figure = _np.roll(figure, int(k*npw[1]), axis=1)
690+
figure = _np.roll(figure, int(k * npw[0]), axis=0)
691+
figure = _np.roll(figure, int(k * npw[1]), axis=1)
691692
# search and iterate over propagating plane wave components
692693
kxx, kyy = _np.meshgrid(kx, ky, sparse=True)
693694
rho = _np.sqrt((kxx) ** 2 + (kyy) ** 2)
694695
d = 0
695696
for n in range(nx):
696697
for m in range(ny):
697-
if(rho[n, m] < k):
698+
if (rho[n, m] < k):
698699
# dispertion relation
699-
kz = _np.sqrt(k**2 - rho[n, m]**2)
700+
kz = _np.sqrt(k ** 2 - rho[n, m] ** 2)
700701
# normal vector of plane wave
701-
npw = 1/k * _np.asarray([kx[n], ky[m], kz])
702+
npw = 1 / k * _np.asarray([kx[n], ky[m], kz])
702703
npw = npw / _np.linalg.norm(npw)
703704
# driving function of plane wave with positive kz
704705
d_component, selection, secondary_source = plane_3d(

sfs/td/wfs.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def plot(d, selection, secondary_source, t=0):
4343
4444
"""
4545
import numpy as _np
46-
from numpy.core.umath_tests import inner1d as _inner1d
4746

4847
from . import apply_delays as _apply_delays
4948
from . import secondary_source_point as _secondary_source_point
@@ -118,8 +117,8 @@ def plane_25d(x0, n0, n=[0, 1, 0], xref=[0, 0, 0], c=None):
118117
n = _util.normalize_vector(n)
119118
xref = _util.asarray_1d(xref)
120119
g0 = _np.sqrt(2 * _np.pi * _np.linalg.norm(xref - x0, axis=1))
121-
delays = _inner1d(n, x0) / c
122-
weights = 2 * g0 * _inner1d(n, n0)
120+
delays = _np.einsum('i,ji->j', n, x0) / c
121+
weights = 2 * g0 * _np.einsum('i,ji->j', n, n0)
123122
selection = _util.source_selection_plane(n0, n)
124123
return delays, weights, selection, _secondary_source_point(c)
125124

@@ -195,8 +194,8 @@ def point_25d(x0, n0, xs, xref=[0, 0, 0], c=None):
195194
g0 = _np.sqrt(2 * _np.pi * _np.linalg.norm(xref - x0, axis=1))
196195
ds = x0 - xs
197196
r = _np.linalg.norm(ds, axis=1)
198-
delays = r/c
199-
weights = g0 * _inner1d(ds, n0) / (2 * _np.pi * r**(3/2))
197+
delays = r / c
198+
weights = g0 * _np.einsum('ij,ij->i', ds, n0) / (2 * _np.pi * r ** (3 / 2))
200199
selection = _util.source_selection_point(n0, x0, xs)
201200
return delays, weights, selection, _secondary_source_point(c)
202201

@@ -278,8 +277,8 @@ def focused_25d(x0, n0, xs, ns, xref=[0, 0, 0], c=None):
278277
r = _np.linalg.norm(ds, axis=1)
279278
g0 = _np.sqrt(_np.linalg.norm(xref - x0, axis=1)
280279
/ (_np.linalg.norm(xref - x0, axis=1) + r))
281-
delays = -r/c
282-
weights = g0 * _inner1d(ds, n0) / (2 * _np.pi * r**(3/2))
280+
delays = -r / c
281+
weights = g0 * _np.einsum('ij,ij->i', ds, n0) / (2 * _np.pi * r ** (3 / 2))
283282
selection = _util.source_selection_focused(ns, x0, xs)
284283
return delays, weights, selection, _secondary_source_point(c)
285284

sfs/util.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import collections
88
import numpy as np
9-
from numpy.core.umath_tests import inner1d
109
from scipy.special import spherical_jn, spherical_yn
1110
from . import default
1211

@@ -41,7 +40,7 @@ def rotation_matrix(n1, n2):
4140
vx = [[0, -v2, v1],
4241
[v2, 0, -v0],
4342
[-v1, v0, 0]] # skew-symmetric cross-product matrix
44-
return I + vx + np.dot(vx, vx) * (1 - c) / s**2
43+
return I + vx + np.dot(vx, vx) * (1 - c) / s ** 2
4544

4645

4746
def wavenumber(omega, c=None):
@@ -51,7 +50,7 @@ def wavenumber(omega, c=None):
5150
return omega / c
5251

5352

54-
def direction_vector(alpha, beta=np.pi/2):
53+
def direction_vector(alpha, beta=np.pi / 2):
5554
"""Compute normal vector from azimuth, colatitude."""
5655
return sph2cart(alpha, beta, 1)
5756

@@ -122,7 +121,7 @@ def cart2sph(x, y, z):
122121
Radius
123122
124123
"""
125-
r = np.sqrt(x**2 + y**2 + z**2)
124+
r = np.sqrt(x ** 2 + y ** 2 + z ** 2)
126125
alpha = np.arctan2(y, x)
127126
beta = np.arccos(z / r)
128127
return alpha, beta, r
@@ -503,19 +502,20 @@ def image_sources_for_box(x, L, N, *, prune=True):
503502
number of reflections at individual walls for each source.
504503
505504
"""
505+
506506
def _images_1d_unit_box(x, N):
507507
result = np.arange(-N, N + 1, dtype=x.dtype)
508508
result[N % 2::2] += x
509509
result[1 - (N % 2)::2] += 1 - x
510510
return result
511511

512512
def _count_walls_1d(a):
513-
b = np.floor(a/2)
514-
c = np.ceil((a-1)/2)
513+
b = np.floor(a / 2)
514+
c = np.ceil((a - 1) / 2)
515515
return np.abs(np.stack([b, c], axis=1)).astype(int)
516516

517517
L = asarray_1d(L)
518-
x = asarray_1d(x)/L
518+
x = asarray_1d(x) / L
519519
D = len(x)
520520
xs = [_images_1d_unit_box(coord, N) for coord in x]
521521
xs = np.reshape(np.transpose(np.meshgrid(*xs, indexing='ij')), (-1, D))
@@ -576,7 +576,7 @@ def source_selection_point(n0, x0, xs):
576576
x0 = asarray_of_rows(x0)
577577
xs = asarray_1d(xs)
578578
ds = x0 - xs
579-
return inner1d(ds, n0) >= default.selection_tolerance
579+
return np.einsum('ij,ij->i', ds, n0) >= default.selection_tolerance
580580

581581

582582
def source_selection_line(n0, x0, xs):
@@ -598,7 +598,7 @@ def source_selection_focused(ns, x0, xs):
598598
xs = asarray_1d(xs)
599599
ns = normalize_vector(ns)
600600
ds = xs - x0
601-
return inner1d(ns, ds) >= default.selection_tolerance
601+
return np.einsum('i,ji->j', ns, ds) >= default.selection_tolerance
602602

603603

604604
def source_selection_all(N):

0 commit comments

Comments
 (0)