Skip to content

Commit 17fbef7

Browse files
alexfiklinducer
authored andcommitted
feat: remove simple usages of scipy
1 parent 469be9f commit 17fbef7

3 files changed

Lines changed: 48 additions & 31 deletions

File tree

sumpy/point_calculus.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ def __init__(self,
117117
weights_1d = None
118118

119119
elif nodes == "legendre":
120-
from scipy.special import legendre
121-
points_1d, weights_1d, _ = legendre(npoints).weights.T
120+
from numpy.polynomial.legendre import leggauss
121+
122+
points_1d, weights_1d = leggauss(npoints)
122123
points_1d = points_1d * (h/2)
123124
weights_1d = weights_1d * (h/2)
124125

@@ -166,10 +167,13 @@ def basis(self) -> Sequence[
166167
a high-order interpolation basis on the :py:attr:`points`.
167168
"""
168169

169-
from scipy.special import eval_chebyt
170-
171170
from pytools import indices_in_shape
172171

172+
def eval_chebyt(n: int,
173+
x: Array1D[np.floating[Any]]) -> Array1D[np.floating[Any]]:
174+
# T_n(x) = cos(n * arccos(x)), valid for x in [-1, 1]
175+
return np.cos(n * np.arccos(x))
176+
173177
def eval_basis(
174178
ind: tuple[int, ...],
175179
x: Array2D[np.floating[Any]]

sumpy/test/curve.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,45 @@
2323
THE SOFTWARE.
2424
"""
2525

26-
from typing import TYPE_CHECKING, final
26+
from typing import TYPE_CHECKING, Any, final
2727

2828
import numpy as np
29-
from scipy import fftpack
3029

3130

3231
if TYPE_CHECKING:
33-
from numpy.typing import NDArray
32+
import optype.numpy as onp
33+
34+
35+
def fftdiff(x: onp.Array1D[np.floating[Any]], *,
36+
period: float = 1.0) -> onp.Array1D[np.floating[Any]]:
37+
n = len(x)
38+
return np.fft.ifft(
39+
2j * np.pi * np.fft.fftfreq(n, d=period/n) * np.fft.fft(x)
40+
).real
3441

3542

3643
@final
3744
class CurveGrid:
38-
pos: NDArray[np.floating]
39-
mean_curvature: NDArray[np.floating]
40-
normal: NDArray[np.floating]
45+
pos: onp.Array2D[np.floating[Any]]
46+
mean_curvature: onp.Array1D[np.floating[Any]]
47+
normal: onp.Array2D[np.floating[Any]]
4148

42-
def __init__(self, x: NDArray[np.floating], y: NDArray[np.floating]):
49+
def __init__(self,
50+
x: onp.Array1D[np.floating[Any]],
51+
y: onp.Array1D[np.floating[Any]]) -> None:
4352
self.pos = np.vstack([x, y]).copy()
44-
xp = self.xp = fftpack.diff(x, period=1)
45-
yp = self.yp = fftpack.diff(y, period=1)
46-
xpp = self.xpp = fftpack.diff(xp, period=1)
47-
ypp = self.ypp = fftpack.diff(yp, period=1)
53+
xp = self.xp = fftdiff(x, period=1)
54+
yp = self.yp = fftdiff(y, period=1)
55+
xpp = self.xpp = fftdiff(xp, period=1)
56+
ypp = self.ypp = fftdiff(yp, period=1)
4857
self.mean_curvature = (xp*ypp-yp*xpp)/((xp**2+yp**2)**(3/2))
4958

5059
speed = self.speed = np.sqrt(xp**2+yp**2)
5160
self.normal = (np.vstack([yp, -xp])/speed).copy()
5261

53-
def __len__(self):
62+
def __len__(self) -> int:
5463
return len(self.pos)
5564

56-
def plot(self):
65+
def plot(self) -> None:
5766
import matplotlib.pyplot as pt
5867
pt.plot(self.pos[:, 0], self.pos[:, 1])

sumpy/test/geometries.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@
2424
"""
2525

2626
from dataclasses import dataclass
27-
from typing import TYPE_CHECKING
27+
from typing import TYPE_CHECKING, Any
2828

2929
import numpy as np
3030
import numpy.linalg as la
3131

3232

3333
if TYPE_CHECKING:
34-
from numpy.typing import NDArray
34+
import optype.numpy as onp
3535

3636

3737
@dataclass(frozen=True)
3838
class Geometry:
39-
nodes: NDArray[np.floating]
40-
normals: NDArray[np.floating]
41-
weights: NDArray[np.floating]
42-
area_elements: NDArray[np.floating]
39+
nodes: onp.ArrayND[np.floating[Any]]
40+
normals: onp.ArrayND[np.floating[Any]]
41+
weights: onp.Array1D[np.floating[Any]]
42+
area_elements: onp.Array1D[np.floating[Any]]
4343

4444

4545
def _geometry_from_curve_nodes(
46-
x: NDArray[np.floating],
47-
y: NDArray[np.floating]
46+
x: onp.Array1D[np.floating[Any]],
47+
y: onp.Array1D[np.floating[Any]]
4848
):
4949
npts: int
5050
npts, = x.shape
@@ -95,12 +95,16 @@ def make_torus(
9595
r_minor * np.sin(v)
9696
])
9797

98-
def diff2d(ary: NDArray[np.floating]):
99-
from scipy import fftpack
100-
return np.array([
101-
fftpack.diff(ary[idx])
102-
for idx in np.ndindex(ary.shape[:-1])
103-
])
98+
def fftdiff(x: onp.Array1D[np.floating[Any]], *,
99+
period: float = 2.0 * np.pi) -> onp.Array1D[np.floating[Any]]:
100+
n = len(x)
101+
return np.fft.ifft(
102+
2j * np.pi * np.fft.fftfreq(n, d=period/n) * np.fft.fft(x)
103+
).real
104+
105+
def diff2d(ary: onp.Array2D[np.floating[Any]]) -> onp.Array2D[np.floating[Any]]:
106+
return np.array([fftdiff(ary[idx]) for idx in np.ndindex(ary.shape[:-1])])
107+
104108
dnodes_du = np.array(
105109
[diff2d(nodes[i].T).T for i in range(3)])
106110
dnodes_dv = np.array(

0 commit comments

Comments
 (0)