Skip to content

Commit 1a5a6f8

Browse files
authored
Changed order of arguments to spherical_harm (#969)
* Changed order of arguments to spherical_harm Since scipy 1.15 the sph_harm has been deprecated in favor of sph_harm_y (with changed argument order). Now sisl obeys the same order of arguments. * added change-logs
1 parent f879aaf commit 1a5a6f8

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

changes/968.fix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed import error for scipy deprecations

changes/969.change.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Changed spherical_harm argument order to match scipy 1.17
2+
3+
Since this is generally not really used, it should
4+
be safe to do. Sorry for the inconvenience.

src/sisl/_core/orbital.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _rfact(l, m):
6565
del _rfact
6666

6767

68-
def _rspherical_harm(m, l, theta, cos_phi):
68+
def _rspherical_harm(l, m, theta, cos_phi):
6969
r""" Calculates the real spherical harmonics using :math:`Y_l^m(\theta, \varphi)` with :math:`\mathbf r\to \{r, \theta, \varphi\}`.
7070
7171
These real spherical harmonics are via these equations:
@@ -79,10 +79,10 @@ def _rspherical_harm(m, l, theta, cos_phi):
7979
8080
Parameters
8181
----------
82-
m : int
83-
order of the spherical harmonics
8482
l : int
8583
degree of the spherical harmonics
84+
m : int
85+
order of the spherical harmonics
8686
theta : array_like
8787
angle in :math:`xy` plane (azimuthal)
8888
cos_phi : array_like
@@ -697,8 +697,8 @@ def spher(self, theta, phi, m: int = 0, cos_phi: bool = False):
697697
spherical harmonics at angles :math:`\theta` and :math:`\phi` and given quantum number `m`
698698
"""
699699
if cos_phi:
700-
return _rspherical_harm(m, self.l, theta, phi)
701-
return _rspherical_harm(m, self.l, theta, cos(phi))
700+
return _rspherical_harm(self.l, m, theta, phi)
701+
return _rspherical_harm(self.l, m, theta, cos(phi))
702702

703703
def psi(self, r, m: int = 0):
704704
r"""Calculate :math:`\phi(\mathbf r)` at a given point (or more points)
@@ -1610,8 +1610,8 @@ def spher(self, theta, phi, cos_phi: bool = False):
16101610
spherical harmonics at angles :math:`\theta` and :math:`\phi`
16111611
"""
16121612
if cos_phi:
1613-
return _rspherical_harm(self.m, self.l, theta, phi)
1614-
return _rspherical_harm(self.m, self.l, theta, cos(phi))
1613+
return _rspherical_harm(self.l, self.m, theta, phi)
1614+
return _rspherical_harm(self.l, self.m, theta, cos(phi))
16151615

16161616
def psi_spher(self, r, theta, phi, cos_phi: bool = False):
16171617
r"""Calculate :math:`\phi(|\mathbf r|, \theta, \phi)` at a given point (in spherical coordinates)

src/sisl/utils/mathematics.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,23 @@
2525
take,
2626
zeros_like,
2727
)
28-
from scipy.special import sph_harm
28+
29+
try:
30+
# from 1.15 and onwards
31+
from scipy.special import sph_harm_y
32+
33+
def _sph_harm(n, m, theta, phi, diff_n: int = 0):
34+
return sph_harm_y(n, m, theta, phi, diff_n=diff_n)
35+
36+
except ImportError:
37+
# deprecated since 1.15
38+
# note the order has changed of the arguments
39+
from scipy.special import sph_harm
40+
41+
def _sph_harm(n, m, theta, phi, diff_n: int = 0):
42+
assert diff_n == 0
43+
return sph_harm(m, n, theta, phi)
44+
2945

3046
from sisl import _array as _a
3147
from sisl._core.quaternion import Quaternion
@@ -219,7 +235,7 @@ def cart2spher(r, theta: bool = True, cos_phi: bool = False, maxR=None):
219235
return idx, rr, theta, phi
220236

221237

222-
def spherical_harm(m, l, theta, phi):
238+
def spherical_harm(l, m, theta, phi, diff_n: int = 0):
223239
r"""Calculate the spherical harmonics using :math:`Y_l^m(\theta, \varphi)` with :math:`\mathbf r\to \{r, \theta, \varphi\}`.
224240
225241
.. math::
@@ -230,19 +246,21 @@ def spherical_harm(m, l, theta, phi):
230246
231247
Parameters
232248
----------
233-
m : int
234-
order of the spherical harmonics
235249
l : int
236250
degree of the spherical harmonics
251+
m : int
252+
order of the spherical harmonics
237253
theta : array_like
238254
angle in :math:`xy` plane (azimuthal)
239255
phi : array_like
240256
angle from :math:`z` axis (polar)
257+
diff_n : int
258+
if a recent enough scipy (>=1.16), one can calculate the derivatives.
241259
"""
242260
# Probably same as:
243261
# return (-1) ** m * ( (2*l+1)/(4*pi) * factorial(l-m) / factorial(l+m) ) ** 0.5 \
244262
# * lpmv(m, l, cos(theta)) * exp(1j * m * phi)
245-
return sph_harm(m, l, theta, phi) * (-1) ** m
263+
return _sph_harm(l, m, theta, phi, diff_n=diff_n) * (-1) ** m
246264

247265

248266
def curl(M, axis=-2, axisv=-1):

0 commit comments

Comments
 (0)