summaryrefslogtreecommitdiff
path: root/numpy/matrixlib/defmatrix.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-10-15 20:20:20 +0100
committerGitHub <noreply@github.com>2019-10-15 20:20:20 +0100
commit10a7a4a815105e16828fe83fb89778c3bbafe692 (patch)
tree2c73effc6bf4b8404e63564f78661caff034b255 /numpy/matrixlib/defmatrix.py
parentd0731e118a5c40d866702f1b5da2be4d4f52ded9 (diff)
parent83da5faca3a313c5d37226b86fa781956f8d162b (diff)
downloadnumpy-10a7a4a815105e16828fe83fb89778c3bbafe692.tar.gz
Merge branch 'master' into master
Diffstat (limited to 'numpy/matrixlib/defmatrix.py')
-rw-r--r--numpy/matrixlib/defmatrix.py78
1 files changed, 45 insertions, 33 deletions
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py
index 7baa401a8..3c7e8ffc2 100644
--- a/numpy/matrixlib/defmatrix.py
+++ b/numpy/matrixlib/defmatrix.py
@@ -7,6 +7,7 @@ import warnings
import ast
import numpy.core.numeric as N
from numpy.core.numeric import concatenate, isscalar
+from numpy.core.overrides import set_module
# While not in __all__, matrix_power used to be defined here, so we import
# it for backward compatibility.
from numpy.linalg import matrix_power
@@ -33,6 +34,8 @@ def _convert_from_string(data):
newdata.append(newrow)
return newdata
+
+@set_module('numpy')
def asmatrix(data, dtype=None):
"""
Interpret the input as a matrix.
@@ -67,6 +70,8 @@ def asmatrix(data, dtype=None):
"""
return matrix(data, dtype=dtype, copy=False)
+
+@set_module('numpy')
class matrix(N.ndarray):
"""
matrix(data, dtype=None, copy=True)
@@ -99,9 +104,9 @@ class matrix(N.ndarray):
Examples
--------
>>> a = np.matrix('1 2; 3 4')
- >>> print(a)
- [[1 2]
- [3 4]]
+ >>> a
+ matrix([[1, 2],
+ [3, 4]])
>>> np.matrix([[1, 2], [3, 4]])
matrix([[1, 2],
@@ -305,12 +310,12 @@ class matrix(N.ndarray):
matrix([[3],
[7]])
>>> x.sum(axis=1, dtype='float')
- matrix([[ 3.],
- [ 7.]])
- >>> out = np.zeros((1, 2), dtype='float')
- >>> x.sum(axis=1, dtype='float', out=out)
- matrix([[ 3.],
- [ 7.]])
+ matrix([[3.],
+ [7.]])
+ >>> out = np.zeros((2, 1), dtype='float')
+ >>> x.sum(axis=1, dtype='float', out=np.asmatrix(out))
+ matrix([[3.],
+ [7.]])
"""
return N.ndarray.sum(self, axis, dtype, out, keepdims=True)._collapse(axis)
@@ -432,7 +437,7 @@ class matrix(N.ndarray):
>>> x.mean()
5.5
>>> x.mean(0)
- matrix([[ 4., 5., 6., 7.]])
+ matrix([[4., 5., 6., 7.]])
>>> x.mean(1)
matrix([[ 1.5],
[ 5.5],
@@ -464,9 +469,9 @@ class matrix(N.ndarray):
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x.std()
- 3.4520525295346629
+ 3.4520525295346629 # may vary
>>> x.std(0)
- matrix([[ 3.26598632, 3.26598632, 3.26598632, 3.26598632]])
+ matrix([[ 3.26598632, 3.26598632, 3.26598632, 3.26598632]]) # may vary
>>> x.std(1)
matrix([[ 1.11803399],
[ 1.11803399],
@@ -500,11 +505,11 @@ class matrix(N.ndarray):
>>> x.var()
11.916666666666666
>>> x.var(0)
- matrix([[ 10.66666667, 10.66666667, 10.66666667, 10.66666667]])
+ matrix([[ 10.66666667, 10.66666667, 10.66666667, 10.66666667]]) # may vary
>>> x.var(1)
- matrix([[ 1.25],
- [ 1.25],
- [ 1.25]])
+ matrix([[1.25],
+ [1.25],
+ [1.25]])
"""
return N.ndarray.var(self, axis, dtype, out, ddof, keepdims=True)._collapse(axis)
@@ -786,7 +791,8 @@ class matrix(N.ndarray):
"""
return N.ndarray.ptp(self, axis, out)._align(axis)
- def getI(self):
+ @property
+ def I(self):
"""
Returns the (multiplicative) inverse of invertible `self`.
@@ -819,7 +825,7 @@ class matrix(N.ndarray):
matrix([[-2. , 1. ],
[ 1.5, -0.5]])
>>> m.getI() * m
- matrix([[ 1., 0.],
+ matrix([[ 1., 0.], # may vary
[ 0., 1.]])
"""
@@ -830,7 +836,8 @@ class matrix(N.ndarray):
from numpy.dual import pinv as func
return asmatrix(func(self))
- def getA(self):
+ @property
+ def A(self):
"""
Return `self` as an `ndarray` object.
@@ -859,7 +866,8 @@ class matrix(N.ndarray):
"""
return self.__array__()
- def getA1(self):
+ @property
+ def A1(self):
"""
Return `self` as a flattened `ndarray`.
@@ -881,7 +889,8 @@ class matrix(N.ndarray):
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> x.getA1()
- array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
+ array([ 0, 1, 2, ..., 9, 10, 11])
+
"""
return self.__array__().ravel()
@@ -925,8 +934,8 @@ class matrix(N.ndarray):
"""
return N.ndarray.ravel(self, order=order)
-
- def getT(self):
+ @property
+ def T(self):
"""
Returns the transpose of the matrix.
@@ -958,7 +967,8 @@ class matrix(N.ndarray):
"""
return self.transpose()
- def getH(self):
+ @property
+ def H(self):
"""
Returns the (complex) conjugate transpose of `self`.
@@ -981,10 +991,10 @@ class matrix(N.ndarray):
[ 4. -4.j, 5. -5.j, 6. -6.j, 7. -7.j],
[ 8. -8.j, 9. -9.j, 10.-10.j, 11.-11.j]])
>>> z.getH()
- matrix([[ 0. +0.j, 4. +4.j, 8. +8.j],
- [ 1. +1.j, 5. +5.j, 9. +9.j],
- [ 2. +2.j, 6. +6.j, 10.+10.j],
- [ 3. +3.j, 7. +7.j, 11.+11.j]])
+ matrix([[ 0. -0.j, 4. +4.j, 8. +8.j],
+ [ 1. +1.j, 5. +5.j, 9. +9.j],
+ [ 2. +2.j, 6. +6.j, 10.+10.j],
+ [ 3. +3.j, 7. +7.j, 11.+11.j]])
"""
if issubclass(self.dtype.type, N.complexfloating):
@@ -992,11 +1002,12 @@ class matrix(N.ndarray):
else:
return self.transpose()
- T = property(getT, None)
- A = property(getA, None)
- A1 = property(getA1, None)
- H = property(getH, None)
- I = property(getI, None)
+ # kept for compatibility
+ getT = T.fget
+ getA = A.fget
+ getA1 = A1.fget
+ getH = H.fget
+ getI = I.fget
def _from_string(str, gdict, ldict):
rows = str.split(';')
@@ -1023,6 +1034,7 @@ def _from_string(str, gdict, ldict):
return concatenate(rowtup, axis=0)
+@set_module('numpy')
def bmat(obj, ldict=None, gdict=None):
"""
Build a matrix object from a string, nested sequence, or array.