-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: constants: add array api support (#20593)
Co-authored-by: Ralf Gommers <ralf.gommers@gmail.com>
- Loading branch information
Showing
5 changed files
with
116 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,89 @@ | ||
from numpy.testing import assert_equal, assert_allclose | ||
import pytest | ||
|
||
import scipy.constants as sc | ||
from scipy.conftest import array_api_compatible | ||
from scipy._lib._array_api import xp_assert_equal, xp_assert_close | ||
|
||
|
||
pytestmark = [array_api_compatible, pytest.mark.usefixtures("skip_xp_backends")] | ||
skip_xp_backends = pytest.mark.skip_xp_backends | ||
|
||
|
||
class TestConvertTemperature: | ||
def test_convert_temperature(self, xp): | ||
xp_assert_equal(sc.convert_temperature(xp.asarray(32.), 'f', 'Celsius'), | ||
xp.asarray(0.0)) | ||
xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), | ||
'celsius', 'Kelvin'), | ||
xp.asarray([273.15, 273.15])) | ||
xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'kelvin', 'c'), | ||
xp.asarray([-273.15, -273.15])) | ||
xp_assert_equal(sc.convert_temperature(xp.asarray([32., 32.]), 'f', 'k'), | ||
xp.asarray([273.15, 273.15])) | ||
xp_assert_equal(sc.convert_temperature(xp.asarray([273.15, 273.15]), | ||
'kelvin', 'F'), | ||
xp.asarray([32., 32.])) | ||
xp_assert_equal(sc.convert_temperature(xp.asarray([0., 0.]), 'C', 'fahrenheit'), | ||
xp.asarray([32., 32.])) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([0., 0.], dtype=xp.float64), | ||
'c', 'r'), | ||
xp.asarray([491.67, 491.67], dtype=xp.float64), | ||
rtol=0., atol=1e-13) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67], | ||
dtype=xp.float64), | ||
'Rankine', 'C'), | ||
xp.asarray([0., 0.], dtype=xp.float64), rtol=0., atol=1e-13) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 491.67], | ||
dtype=xp.float64), | ||
'r', 'F'), | ||
xp.asarray([32., 32.], dtype=xp.float64), rtol=0., atol=1e-13) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([32., 32.], dtype=xp.float64), | ||
'fahrenheit', 'R'), | ||
xp.asarray([491.67, 491.67], dtype=xp.float64), | ||
rtol=0., atol=1e-13) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([273.15, 273.15], | ||
dtype=xp.float64), | ||
'K', 'R'), | ||
xp.asarray([491.67, 491.67], dtype=xp.float64), | ||
rtol=0., atol=1e-13) | ||
xp_assert_close(sc.convert_temperature(xp.asarray([491.67, 0.], | ||
dtype=xp.float64), | ||
'rankine', 'kelvin'), | ||
xp.asarray([273.15, 0.], dtype=xp.float64), rtol=0., atol=1e-13) | ||
|
||
@skip_xp_backends(np_only=True, reasons=['Python list input uses NumPy backend']) | ||
def test_convert_temperature_array_like(self): | ||
xp_assert_close(sc.convert_temperature([491.67, 0.], 'rankine', 'kelvin'), | ||
[273.15, 0.], rtol=0., atol=1e-13) | ||
|
||
|
||
@skip_xp_backends(np_only=True, reasons=['Python int input uses NumPy backend']) | ||
def test_convert_temperature_errors(self, xp): | ||
with pytest.raises(NotImplementedError, match="old_scale="): | ||
sc.convert_temperature(1, old_scale="cheddar", new_scale="kelvin") | ||
with pytest.raises(NotImplementedError, match="new_scale="): | ||
sc.convert_temperature(1, old_scale="kelvin", new_scale="brie") | ||
|
||
|
||
class TestLambdaToNu: | ||
def test_lambda_to_nu(self, xp): | ||
xp_assert_equal(sc.lambda2nu(xp.asarray([sc.speed_of_light, 1])), | ||
xp.asarray([1, sc.speed_of_light])) | ||
|
||
|
||
@skip_xp_backends(np_only=True, reasons=['Python list input uses NumPy backend']) | ||
def test_lambda_to_nu_array_like(self, xp): | ||
xp_assert_equal(sc.lambda2nu([sc.speed_of_light, 1]), | ||
[1, sc.speed_of_light]) | ||
|
||
|
||
class TestNuToLambda: | ||
def test_nu_to_lambda(self, xp): | ||
xp_assert_equal(sc.nu2lambda(xp.asarray([sc.speed_of_light, 1])), | ||
xp.asarray([1, sc.speed_of_light])) | ||
|
||
def test_convert_temperature(): | ||
assert_equal(sc.convert_temperature(32, 'f', 'Celsius'), 0) | ||
assert_equal(sc.convert_temperature([0, 0], 'celsius', 'Kelvin'), | ||
[273.15, 273.15]) | ||
assert_equal(sc.convert_temperature([0, 0], 'kelvin', 'c'), | ||
[-273.15, -273.15]) | ||
assert_equal(sc.convert_temperature([32, 32], 'f', 'k'), [273.15, 273.15]) | ||
assert_equal(sc.convert_temperature([273.15, 273.15], 'kelvin', 'F'), | ||
[32, 32]) | ||
assert_equal(sc.convert_temperature([0, 0], 'C', 'fahrenheit'), [32, 32]) | ||
assert_allclose(sc.convert_temperature([0, 0], 'c', 'r'), [491.67, 491.67], | ||
rtol=0., atol=1e-13) | ||
assert_allclose(sc.convert_temperature([491.67, 491.67], 'Rankine', 'C'), | ||
[0., 0.], rtol=0., atol=1e-13) | ||
assert_allclose(sc.convert_temperature([491.67, 491.67], 'r', 'F'), | ||
[32., 32.], rtol=0., atol=1e-13) | ||
assert_allclose(sc.convert_temperature([32, 32], 'fahrenheit', 'R'), | ||
[491.67, 491.67], rtol=0., atol=1e-13) | ||
assert_allclose(sc.convert_temperature([273.15, 273.15], 'K', 'R'), | ||
[491.67, 491.67], rtol=0., atol=1e-13) | ||
assert_allclose(sc.convert_temperature([491.67, 0.], 'rankine', 'kelvin'), | ||
[273.15, 0.], rtol=0., atol=1e-13) | ||
|
||
|
||
def test_lambda_to_nu(): | ||
assert_equal(sc.lambda2nu([sc.speed_of_light, 1]), [1, sc.speed_of_light]) | ||
|
||
|
||
def test_nu_to_lambda(): | ||
assert_equal(sc.nu2lambda([sc.speed_of_light, 1]), [1, sc.speed_of_light]) | ||
@skip_xp_backends(np_only=True, reasons=['Python list input uses NumPy backend']) | ||
def test_nu_to_lambda_array_like(self, xp): | ||
xp_assert_equal(sc.nu2lambda([sc.speed_of_light, 1]), | ||
[1, sc.speed_of_light]) | ||
|