Skip to content

Commit 9fe22c1

Browse files
committed
Assume numpy
1 parent 9006a6c commit 9fe22c1

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

mathics/core/atoms.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
from typing import Any, Dict, Generic, Optional, Tuple, TypeVar, Union
88

99
import mpmath
10+
import numpy
1011
import sympy
1112
from sympy.core import numbers as sympy_numbers
1213

13-
try: # pragma: no cover - optional dependency handled at runtime
14-
import numpy
15-
except ImportError: # pragma: no cover - numpy is optional at import time
16-
numpy = None
17-
1814
from mathics.core.element import BoxElementMixin, ImmutableValueMixin
1915
from mathics.core.keycomparable import (
2016
BASIC_ATOM_BYTEARRAY_ELT_ORDER,
@@ -1108,27 +1104,24 @@ def is_zero(self) -> bool:
11081104
# NumericArray
11091105
#
11101106

1111-
if numpy is not None:
1112-
NUMERIC_ARRAY_TYPE_MAP = {
1113-
"UnsignedInteger8": numpy.dtype("uint8"),
1114-
"UnsignedInteger16": numpy.dtype("uint16"),
1115-
"UnsignedInteger32": numpy.dtype("uint32"),
1116-
"UnsignedInteger64": numpy.dtype("uint64"),
1117-
"Integer8": numpy.dtype("int8"),
1118-
"Integer16": numpy.dtype("int16"),
1119-
"Integer32": numpy.dtype("int32"),
1120-
"Integer64": numpy.dtype("int64"),
1121-
"Real32": numpy.dtype("float32"),
1122-
"Real64": numpy.dtype("float64"),
1123-
"ComplexReal32": numpy.dtype("complex64"),
1124-
"ComplexReal64": numpy.dtype("complex128"),
1125-
}
1126-
NUMERIC_ARRAY_DTYPE_TO_NAME = {
1127-
dtype: name for name, dtype in NUMERIC_ARRAY_TYPE_MAP.items()
1128-
}
1129-
else: # pragma: no cover - executed only when numpy is absent
1130-
NUMERIC_ARRAY_TYPE_MAP = {}
1131-
NUMERIC_ARRAY_DTYPE_TO_NAME = {}
1107+
NUMERIC_ARRAY_TYPE_MAP = {
1108+
"UnsignedInteger8": numpy.dtype("uint8"),
1109+
"UnsignedInteger16": numpy.dtype("uint16"),
1110+
"UnsignedInteger32": numpy.dtype("uint32"),
1111+
"UnsignedInteger64": numpy.dtype("uint64"),
1112+
"Integer8": numpy.dtype("int8"),
1113+
"Integer16": numpy.dtype("int16"),
1114+
"Integer32": numpy.dtype("int32"),
1115+
"Integer64": numpy.dtype("int64"),
1116+
"Real32": numpy.dtype("float32"),
1117+
"Real64": numpy.dtype("float64"),
1118+
"ComplexReal32": numpy.dtype("complex64"),
1119+
"ComplexReal64": numpy.dtype("complex128"),
1120+
}
1121+
1122+
NUMERIC_ARRAY_DTYPE_TO_NAME = {
1123+
dtype: name for name, dtype in NUMERIC_ARRAY_TYPE_MAP.items()
1124+
}
11321125

11331126

11341127
class NumericArray(Atom, ImmutableValueMixin):
@@ -1140,8 +1133,6 @@ class NumericArray(Atom, ImmutableValueMixin):
11401133
class_head_name = "NumericArray"
11411134

11421135
def __init__(self, value, dtype=None):
1143-
if numpy is None:
1144-
raise ImportError("numpy is required for NumericArray")
11451136

11461137
# compute value
11471138
if not isinstance(value, numpy.ndarray):

mathics/core/convert/python.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
Conversions between Python and Mathics3
44
"""
55

6+
import numpy
67
from typing import Any
78

8-
try: # pragma: no cover - numpy is optional
9-
import numpy
10-
except ImportError: # pragma: no cover - optional dependency missing
11-
numpy = None
12-
139
from mathics.core.atoms import Complex, Integer, NumericArray, Rational, Real, String
1410
from mathics.core.number import get_type
1511
from mathics.core.symbols import (
@@ -118,7 +114,7 @@ def from_python(arg: Any) -> BaseElement:
118114
from mathics.builtin.binary.bytearray import ByteArray
119115

120116
return Expression(SymbolByteArray, ByteArray(arg))
121-
elif numpy is not None and isinstance(arg, numpy.ndarray):
117+
elif isinstance(arg, numpy.ndarray):
122118
return NumericArray(arg)
123119
else:
124120
raise NotImplementedError

0 commit comments

Comments
 (0)