Skip to content

Commit fa785a5

Browse files
committed
API: Remove np.mat from matrixlib module
1 parent 195e5b4 commit fa785a5

File tree

13 files changed

+58
-48
lines changed

13 files changed

+58
-48
lines changed

benchmarks/benchmarks/bench_shape_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Kron(Benchmark):
141141

142142
def setup(self):
143143
self.large_arr = np.random.random((10,) * 4)
144-
self.large_mat = np.mat(np.random.random((100, 100)))
144+
self.large_mat = np.asmatrix(np.random.random((100, 100)))
145145
self.scalar = 7
146146

147147
def time_arr_kron(self):

benchmarks/benchmarks/bench_ufunc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ def time_less_than_scalar2(self, dtype):
391391

392392

393393
class CustomScalarFloorDivideInt(Benchmark):
394-
params = (np.core.sctypes['int'] + np.core.sctypes['uint'], [8, -8, 43, -43])
394+
params = (np.core.sctypes['int'] +
395+
np.core.sctypes['uint'], [8, -8, 43, -43])
395396
param_names = ['dtype', 'divisors']
396397

397398
def setup(self, dtype, divisor):
@@ -407,7 +408,8 @@ def time_floor_divide_int(self, dtype, divisor):
407408
self.x // divisor
408409

409410
class CustomArrayFloorDivideInt(Benchmark):
410-
params = (np.core.sctypes['int'] + np.core.sctypes['uint'], [100, 10000, 1000000])
411+
params = (np.core.sctypes['int'] +
412+
np.core.sctypes['uint'], [100, 10000, 1000000])
411413
param_names = ['dtype', 'size']
412414

413415
def setup(self, dtype, size):

doc/source/reference/arrays.classes.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -407,21 +407,21 @@ alias for "matrix "in NumPy.
407407

408408
Example 1: Matrix creation from a string
409409

410-
>>> a = np.mat('1 2 3; 4 5 3')
410+
>>> a = np.asmatrix('1 2 3; 4 5 3')
411411
>>> print((a*a.T).I)
412412
[[ 0.29239766 -0.13450292]
413413
[-0.13450292 0.08187135]]
414414

415415

416416
Example 2: Matrix creation from nested sequence
417417

418-
>>> np.mat([[1,5,10],[1.0,3,4j]])
418+
>>> np.asmatrix([[1,5,10],[1.0,3,4j]])
419419
matrix([[ 1.+0.j, 5.+0.j, 10.+0.j],
420420
[ 1.+0.j, 3.+0.j, 0.+4.j]])
421421

422422
Example 3: Matrix creation from an array
423423

424-
>>> np.mat(np.random.rand(3,3)).T
424+
>>> np.asmatrix(np.random.rand(3,3)).T
425425
matrix([[4.17022005e-01, 3.02332573e-01, 1.86260211e-01],
426426
[7.20324493e-01, 1.46755891e-01, 3.45560727e-01],
427427
[1.14374817e-04, 9.23385948e-02, 3.96767474e-01]])

numpy/core/_add_newdocs.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -898,11 +898,11 @@
898898
899899
Creating an array from sub-classes:
900900
901-
>>> np.array(np.mat('1 2; 3 4'))
901+
>>> np.array(np.asmatrix('1 2; 3 4'))
902902
array([[1, 2],
903903
[3, 4]])
904904
905-
>>> np.array(np.mat('1 2; 3 4'), subok=True)
905+
>>> np.array(np.asmatrix('1 2; 3 4'), subok=True)
906906
matrix([[1, 2],
907907
[3, 4]])
908908

numpy/core/tests/test_numeric.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from decimal import Decimal
88

99
import numpy as np
10-
from numpy.core import umath
10+
from numpy.core import umath, sctypes
1111
from numpy.core.numerictypes import obj2sctype
1212
from numpy.core.arrayprint import set_string_function
1313
from numpy.exceptions import AxisError
@@ -1423,14 +1423,14 @@ def test_can_cast_structured_to_simple(self):
14231423

14241424
def test_can_cast_values(self):
14251425
# gh-5917
1426-
for dt in np.core.sctypes['int'] + np.core.sctypes['uint']:
1426+
for dt in sctypes['int'] + sctypes['uint']:
14271427
ii = np.iinfo(dt)
14281428
assert_(np.can_cast(ii.min, dt))
14291429
assert_(np.can_cast(ii.max, dt))
14301430
assert_(not np.can_cast(ii.min - 1, dt))
14311431
assert_(not np.can_cast(ii.max + 1, dt))
14321432

1433-
for dt in np.core.sctypes['float']:
1433+
for dt in sctypes['float']:
14341434
fi = np.finfo(dt)
14351435
assert_(np.can_cast(fi.min, dt))
14361436
assert_(np.can_cast(fi.max, dt))
@@ -2974,7 +2974,7 @@ class TestCreationFuncs:
29742974
# Test ones, zeros, empty and full.
29752975

29762976
def setup_method(self):
2977-
dtypes = {np.dtype(tp) for tp in itertools.chain(*np.core.sctypes.values())}
2977+
dtypes = {np.dtype(tp) for tp in itertools.chain(*sctypes.values())}
29782978
# void, bytes, str
29792979
variable_sized = {tp for tp in dtypes if tp.str.endswith('0')}
29802980
keyfunc = lambda dtype: dtype.str

numpy/core/tests/test_scalar_methods.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010
import numpy as np
1111

12+
from numpy.core import sctypes
1213
from numpy.testing import assert_equal, assert_raises, IS_MUSL
1314

1415

@@ -189,7 +190,7 @@ def test_subscript_scalar(self) -> None:
189190
class TestBitCount:
190191
# derived in part from the cpython test "test_bit_count"
191192

192-
@pytest.mark.parametrize("itype", np.core.sctypes['int']+np.core.sctypes['uint'])
193+
@pytest.mark.parametrize("itype", sctypes['int']+sctypes['uint'])
193194
def test_small(self, itype):
194195
for a in range(max(np.iinfo(itype).min, 0), 128):
195196
msg = f"Smoke test for {itype}({a}).bit_count()"

numpy/core/tests/test_umath.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections import namedtuple
1212

1313
import numpy.core.umath as ncu
14-
from numpy.core import _umath_tests as ncu_tests
14+
from numpy.core import _umath_tests as ncu_tests, sctypes
1515
import numpy as np
1616
from numpy.testing import (
1717
assert_, assert_equal, assert_raises, assert_raises_regex,
@@ -267,8 +267,8 @@ def __array_wrap__(self, arr, context):
267267
class TestComparisons:
268268
import operator
269269

270-
@pytest.mark.parametrize('dtype', np.core.sctypes['uint'] + np.core.sctypes['int'] +
271-
np.core.sctypes['float'] + [np.bool_])
270+
@pytest.mark.parametrize('dtype', sctypes['uint'] + sctypes['int'] +
271+
sctypes['float'] + [np.bool_])
272272
@pytest.mark.parametrize('py_comp,np_comp', [
273273
(operator.lt, np.less),
274274
(operator.le, np.less_equal),
@@ -454,7 +454,7 @@ def test_division_int(self):
454454

455455
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
456456
@pytest.mark.parametrize("dtype,ex_val", itertools.product(
457-
np.core.sctypes['int'] + np.core.sctypes['uint'], (
457+
sctypes['int'] + sctypes['uint'], (
458458
(
459459
# dividend
460460
"np.array(range(fo.max-lsize, fo.max)).astype(dtype),"
@@ -540,7 +540,7 @@ def test_division_int_boundary(self, dtype, ex_val):
540540

541541
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
542542
@pytest.mark.parametrize("dtype,ex_val", itertools.product(
543-
np.core.sctypes['int'] + np.core.sctypes['uint'], (
543+
sctypes['int'] + sctypes['uint'], (
544544
"np.array([fo.max, 1, 2, 1, 1, 2, 3], dtype=dtype)",
545545
"np.array([fo.min, 1, -2, 1, 1, 2, -3]).astype(dtype)",
546546
"np.arange(fo.min, fo.min+(100*10), 10, dtype=dtype)",
@@ -986,10 +986,8 @@ def test_divide_by_zero(self, dtype):
986986
assert extractor(res2) == 0
987987

988988
@pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
989-
@pytest.mark.parametrize("dividend_dtype",
990-
np.core.sctypes['int'])
991-
@pytest.mark.parametrize("divisor_dtype",
992-
np.core.sctypes['int'])
989+
@pytest.mark.parametrize("dividend_dtype", sctypes['int'])
990+
@pytest.mark.parametrize("divisor_dtype", sctypes['int'])
993991
@pytest.mark.parametrize("operation",
994992
[np.remainder, np.fmod, np.divmod, np.floor_divide,
995993
operator.mod, operator.floordiv])
@@ -4176,7 +4174,10 @@ def test_against_cmath(self):
41764174
for p in points:
41774175
a = complex(func(np.cdouble(p)))
41784176
b = cfunc(p)
4179-
assert_(abs(a - b) < atol, "%s %s: %s; cmath: %s" % (fname, p, a, b))
4177+
assert_(
4178+
abs(a - b) < atol,
4179+
"%s %s: %s; cmath: %s" % (fname, p, a, b)
4180+
)
41804181

41814182
@pytest.mark.xfail(
41824183
# manylinux2014 uses glibc2.17
@@ -4185,7 +4186,9 @@ def test_against_cmath(self):
41854186
)
41864187
@pytest.mark.xfail(IS_MUSL, reason="gh23049")
41874188
@pytest.mark.xfail(IS_WASM, reason="doesn't work")
4188-
@pytest.mark.parametrize('dtype', [np.complex64, np.cdouble, np.clongdouble])
4189+
@pytest.mark.parametrize('dtype', [
4190+
np.complex64, np.cdouble, np.clongdouble
4191+
])
41894192
def test_loss_of_precision(self, dtype):
41904193
"""Check loss of precision in complex arc* functions"""
41914194

numpy/doc/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
def add_newdoc(module, name, doc):
2121
constants.append((name, doc))
2222

23+
2324
add_newdoc('numpy', 'pi',
2425
"""
2526
``pi = 3.1415926535897932384626433...``

numpy/lib/tests/test_io.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import numpy.ma as ma
2020
from numpy.exceptions import VisibleDeprecationWarning
2121
from numpy.lib._iotools import ConverterError, ConversionWarning
22+
from numpy.lib.npyio import recfromcsv, recfromtxt
2223
from numpy.ma.testutils import assert_equal
2324
from numpy.testing import (
2425
assert_warns, assert_, assert_raises_regex, assert_raises,
@@ -1657,13 +1658,13 @@ def test_dtype_with_converters_and_usecols(self):
16571658
dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3}
16581659
dtyp = [('e1','i4'),('e2','i4'),('e3','i2'),('n', 'i1')]
16591660
conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
1660-
test = np.lib.npyio.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1661-
names=None, converters=conv)
1661+
test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1662+
names=None, converters=conv)
16621663
control = np.rec.array([(1,5,-1,0), (2,8,-1,1), (3,3,-2,3)], dtype=dtyp)
16631664
assert_equal(test, control)
1664-
dtyp = [('e1','i4'),('e2','i4'),('n', 'i1')]
1665-
test = np.lib.npyio.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1666-
usecols=(0,1,3), names=None, converters=conv)
1665+
dtyp = [('e1', 'i4'), ('e2', 'i4'), ('n', 'i1')]
1666+
test = recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
1667+
usecols=(0, 1, 3), names=None, converters=conv)
16671668
control = np.rec.array([(1,5,0), (2,8,1), (3,3,3)], dtype=dtyp)
16681669
assert_equal(test, control)
16691670

@@ -2297,14 +2298,14 @@ def test_recfromtxt(self):
22972298
#
22982299
data = TextIO('A,B\n0,1\n2,3')
22992300
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
2300-
test = np.lib.npyio.recfromtxt(data, **kwargs)
2301+
test = recfromtxt(data, **kwargs)
23012302
control = np.array([(0, 1), (2, 3)],
23022303
dtype=[('A', int), ('B', int)])
23032304
assert_(isinstance(test, np.recarray))
23042305
assert_equal(test, control)
23052306
#
23062307
data = TextIO('A,B\n0,1\n2,N/A')
2307-
test = np.lib.npyio.recfromtxt(data, dtype=None, usemask=True, **kwargs)
2308+
test = recfromtxt(data, dtype=None, usemask=True, **kwargs)
23082309
control = ma.array([(0, 1), (2, -1)],
23092310
mask=[(False, False), (False, True)],
23102311
dtype=[('A', int), ('B', int)])
@@ -2317,14 +2318,14 @@ def test_recfromcsv(self):
23172318
#
23182319
data = TextIO('A,B\n0,1\n2,3')
23192320
kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)
2320-
test = np.lib.npyio.recfromcsv(data, dtype=None, **kwargs)
2321+
test = recfromcsv(data, dtype=None, **kwargs)
23212322
control = np.array([(0, 1), (2, 3)],
23222323
dtype=[('A', int), ('B', int)])
23232324
assert_(isinstance(test, np.recarray))
23242325
assert_equal(test, control)
23252326
#
23262327
data = TextIO('A,B\n0,1\n2,N/A')
2327-
test = np.lib.npyio.recfromcsv(data, dtype=None, usemask=True, **kwargs)
2328+
test = recfromcsv(data, dtype=None, usemask=True, **kwargs)
23282329
control = ma.array([(0, 1), (2, -1)],
23292330
mask=[(False, False), (False, True)],
23302331
dtype=[('A', int), ('B', int)])
@@ -2333,23 +2334,23 @@ def test_recfromcsv(self):
23332334
assert_equal(test.A, [0, 2])
23342335
#
23352336
data = TextIO('A,B\n0,1\n2,3')
2336-
test = np.lib.npyio.recfromcsv(data, missing_values='N/A',)
2337+
test = recfromcsv(data, missing_values='N/A',)
23372338
control = np.array([(0, 1), (2, 3)],
23382339
dtype=[('a', int), ('b', int)])
23392340
assert_(isinstance(test, np.recarray))
23402341
assert_equal(test, control)
23412342
#
23422343
data = TextIO('A,B\n0,1\n2,3')
23432344
dtype = [('a', int), ('b', float)]
2344-
test = np.lib.npyio.recfromcsv(data, missing_values='N/A', dtype=dtype)
2345+
test = recfromcsv(data, missing_values='N/A', dtype=dtype)
23452346
control = np.array([(0, 1), (2, 3)],
23462347
dtype=dtype)
23472348
assert_(isinstance(test, np.recarray))
23482349
assert_equal(test, control)
23492350

23502351
#gh-10394
23512352
data = TextIO('color\n"red"\n"blue"')
2352-
test = np.lib.npyio.recfromcsv(data, converters={0: lambda x: x.strip(b'\"')})
2353+
test = recfromcsv(data, converters={0: lambda x: x.strip(b'\"')})
23532354
control = np.array([('red',), ('blue',)], dtype=[('color', (bytes, 4))])
23542355
assert_equal(test.dtype, control.dtype)
23552356
assert_equal(test, control)
@@ -2621,7 +2622,7 @@ def test_recfromtxt(self, filename_type):
26212622
f.write('A,B\n0,1\n2,3')
26222623

26232624
kwargs = dict(delimiter=",", missing_values="N/A", names=True)
2624-
test = np.lib.npyio.recfromtxt(path, **kwargs)
2625+
test = recfromtxt(path, **kwargs)
26252626
control = np.array([(0, 1), (2, 3)],
26262627
dtype=[('A', int), ('B', int)])
26272628
assert_(isinstance(test, np.recarray))
@@ -2635,8 +2636,10 @@ def test_recfromcsv(self, filename_type):
26352636
with open(path, 'w') as f:
26362637
f.write('A,B\n0,1\n2,3')
26372638

2638-
kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)
2639-
test = np.lib.npyio.recfromcsv(path, dtype=None, **kwargs)
2639+
kwargs = dict(
2640+
missing_values="N/A", names=True, case_sensitive=True
2641+
)
2642+
test = recfromcsv(path, dtype=None, **kwargs)
26402643
control = np.array([(0, 1), (2, 3)],
26412644
dtype=[('A', int), ('B', int)])
26422645
assert_(isinstance(test, np.recarray))

numpy/linalg/linalg.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2055,8 +2055,9 @@ def slogdet(a):
20552055
logabsdet : (...) array_like
20562056
The natural log of the absolute value of the determinant.
20572057
2058-
If the determinant is zero, then `sign` will be 0 and `logabsdet` will be
2059-
-inf. In all cases, the determinant is equal to ``sign * np.exp(logabsdet)``.
2058+
If the determinant is zero, then `sign` will be 0 and `logabsdet`
2059+
will be -inf. In all cases, the determinant is equal to
2060+
``sign * np.exp(logabsdet)``.
20602061
20612062
See Also
20622063
--------

numpy/matrixlib/defmatrix.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1063,10 +1063,10 @@ def bmat(obj, ldict=None, gdict=None):
10631063
10641064
Examples
10651065
--------
1066-
>>> A = np.mat('1 1; 1 1')
1067-
>>> B = np.mat('2 2; 2 2')
1068-
>>> C = np.mat('3 4; 5 6')
1069-
>>> D = np.mat('7 8; 9 0')
1066+
>>> A = np.asmatrix('1 1; 1 1')
1067+
>>> B = np.asmatrix('2 2; 2 2')
1068+
>>> C = np.asmatrix('3 4; 5 6')
1069+
>>> D = np.asmatrix('7 8; 9 0')
10701070
10711071
All the following expressions construct the same block matrix:
10721072

numpy/matrixlib/tests/test_defmatrix.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
assert_array_almost_equal, assert_raises
88
)
99
from numpy.linalg import matrix_power
10-
from numpy.matrixlib import mat
1110

1211
class TestCtor:
1312
def test_basic(self):
@@ -389,7 +388,7 @@ class TestPower:
389388
def test_returntype(self):
390389
a = np.array([[0, 1], [0, 0]])
391390
assert_(type(matrix_power(a, 2)) is np.ndarray)
392-
a = mat(a)
391+
a = asmatrix(a)
393392
assert_(type(matrix_power(a, 2)) is matrix)
394393

395394
def test_list(self):

numpy/matrixlib/tests/test_regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_matrix_properties(self):
2020
def test_matrix_multiply_by_1d_vector(self):
2121
# Ticket #473
2222
def mul():
23-
np.mat(np.eye(2))*np.ones(2)
23+
np.asmatrix(np.eye(2))*np.ones(2)
2424

2525
assert_raises(ValueError, mul)
2626

0 commit comments

Comments
 (0)