jpayne@69: import warnings jpayne@69: jpayne@69: # 2018-05-29, PendingDeprecationWarning added to matrix.__new__ jpayne@69: # 2020-01-23, numpy 1.19.0 PendingDeprecatonWarning jpayne@69: warnings.warn("Importing from numpy.matlib is deprecated since 1.19.0. " jpayne@69: "The matrix subclass is not the recommended way to represent " jpayne@69: "matrices or deal with linear algebra (see " jpayne@69: "https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). " jpayne@69: "Please adjust your code to use regular ndarray. ", jpayne@69: PendingDeprecationWarning, stacklevel=2) jpayne@69: jpayne@69: import numpy as np jpayne@69: from numpy.matrixlib.defmatrix import matrix, asmatrix jpayne@69: # Matlib.py contains all functions in the numpy namespace with a few jpayne@69: # replacements. See doc/source/reference/routines.matlib.rst for details. jpayne@69: # Need * as we're copying the numpy namespace. jpayne@69: from numpy import * # noqa: F403 jpayne@69: jpayne@69: __version__ = np.__version__ jpayne@69: jpayne@69: __all__ = np.__all__[:] # copy numpy namespace jpayne@69: __all__ += ['rand', 'randn', 'repmat'] jpayne@69: jpayne@69: def empty(shape, dtype=None, order='C'): jpayne@69: """Return a new matrix of given shape and type, without initializing entries. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: shape : int or tuple of int jpayne@69: Shape of the empty matrix. jpayne@69: dtype : data-type, optional jpayne@69: Desired output data-type. jpayne@69: order : {'C', 'F'}, optional jpayne@69: Whether to store multi-dimensional data in row-major jpayne@69: (C-style) or column-major (Fortran-style) order in jpayne@69: memory. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: empty_like, zeros jpayne@69: jpayne@69: Notes jpayne@69: ----- jpayne@69: `empty`, unlike `zeros`, does not set the matrix values to zero, jpayne@69: and may therefore be marginally faster. On the other hand, it requires jpayne@69: the user to manually set all the values in the array, and should be jpayne@69: used with caution. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.empty((2, 2)) # filled with random data jpayne@69: matrix([[ 6.76425276e-320, 9.79033856e-307], # random jpayne@69: [ 7.39337286e-309, 3.22135945e-309]]) jpayne@69: >>> np.matlib.empty((2, 2), dtype=int) jpayne@69: matrix([[ 6600475, 0], # random jpayne@69: [ 6586976, 22740995]]) jpayne@69: jpayne@69: """ jpayne@69: return ndarray.__new__(matrix, shape, dtype, order=order) jpayne@69: jpayne@69: def ones(shape, dtype=None, order='C'): jpayne@69: """ jpayne@69: Matrix of ones. jpayne@69: jpayne@69: Return a matrix of given shape and type, filled with ones. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: shape : {sequence of ints, int} jpayne@69: Shape of the matrix jpayne@69: dtype : data-type, optional jpayne@69: The desired data-type for the matrix, default is np.float64. jpayne@69: order : {'C', 'F'}, optional jpayne@69: Whether to store matrix in C- or Fortran-contiguous order, jpayne@69: default is 'C'. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: out : matrix jpayne@69: Matrix of ones of given shape, dtype, and order. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: ones : Array of ones. jpayne@69: matlib.zeros : Zero matrix. jpayne@69: jpayne@69: Notes jpayne@69: ----- jpayne@69: If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, jpayne@69: `out` becomes a single row matrix of shape ``(1,N)``. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> np.matlib.ones((2,3)) jpayne@69: matrix([[1., 1., 1.], jpayne@69: [1., 1., 1.]]) jpayne@69: jpayne@69: >>> np.matlib.ones(2) jpayne@69: matrix([[1., 1.]]) jpayne@69: jpayne@69: """ jpayne@69: a = ndarray.__new__(matrix, shape, dtype, order=order) jpayne@69: a.fill(1) jpayne@69: return a jpayne@69: jpayne@69: def zeros(shape, dtype=None, order='C'): jpayne@69: """ jpayne@69: Return a matrix of given shape and type, filled with zeros. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: shape : int or sequence of ints jpayne@69: Shape of the matrix jpayne@69: dtype : data-type, optional jpayne@69: The desired data-type for the matrix, default is float. jpayne@69: order : {'C', 'F'}, optional jpayne@69: Whether to store the result in C- or Fortran-contiguous order, jpayne@69: default is 'C'. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: out : matrix jpayne@69: Zero matrix of given shape, dtype, and order. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: numpy.zeros : Equivalent array function. jpayne@69: matlib.ones : Return a matrix of ones. jpayne@69: jpayne@69: Notes jpayne@69: ----- jpayne@69: If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, jpayne@69: `out` becomes a single row matrix of shape ``(1,N)``. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.zeros((2, 3)) jpayne@69: matrix([[0., 0., 0.], jpayne@69: [0., 0., 0.]]) jpayne@69: jpayne@69: >>> np.matlib.zeros(2) jpayne@69: matrix([[0., 0.]]) jpayne@69: jpayne@69: """ jpayne@69: a = ndarray.__new__(matrix, shape, dtype, order=order) jpayne@69: a.fill(0) jpayne@69: return a jpayne@69: jpayne@69: def identity(n,dtype=None): jpayne@69: """ jpayne@69: Returns the square identity matrix of given size. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: n : int jpayne@69: Size of the returned identity matrix. jpayne@69: dtype : data-type, optional jpayne@69: Data-type of the output. Defaults to ``float``. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: out : matrix jpayne@69: `n` x `n` matrix with its main diagonal set to one, jpayne@69: and all other elements zero. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: numpy.identity : Equivalent array function. jpayne@69: matlib.eye : More general matrix identity function. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.identity(3, dtype=int) jpayne@69: matrix([[1, 0, 0], jpayne@69: [0, 1, 0], jpayne@69: [0, 0, 1]]) jpayne@69: jpayne@69: """ jpayne@69: a = array([1]+n*[0], dtype=dtype) jpayne@69: b = empty((n, n), dtype=dtype) jpayne@69: b.flat = a jpayne@69: return b jpayne@69: jpayne@69: def eye(n,M=None, k=0, dtype=float, order='C'): jpayne@69: """ jpayne@69: Return a matrix with ones on the diagonal and zeros elsewhere. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: n : int jpayne@69: Number of rows in the output. jpayne@69: M : int, optional jpayne@69: Number of columns in the output, defaults to `n`. jpayne@69: k : int, optional jpayne@69: Index of the diagonal: 0 refers to the main diagonal, jpayne@69: a positive value refers to an upper diagonal, jpayne@69: and a negative value to a lower diagonal. jpayne@69: dtype : dtype, optional jpayne@69: Data-type of the returned matrix. jpayne@69: order : {'C', 'F'}, optional jpayne@69: Whether the output should be stored in row-major (C-style) or jpayne@69: column-major (Fortran-style) order in memory. jpayne@69: jpayne@69: .. versionadded:: 1.14.0 jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: I : matrix jpayne@69: A `n` x `M` matrix where all elements are equal to zero, jpayne@69: except for the `k`-th diagonal, whose values are equal to one. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: numpy.eye : Equivalent array function. jpayne@69: identity : Square identity matrix. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.eye(3, k=1, dtype=float) jpayne@69: matrix([[0., 1., 0.], jpayne@69: [0., 0., 1.], jpayne@69: [0., 0., 0.]]) jpayne@69: jpayne@69: """ jpayne@69: return asmatrix(np.eye(n, M=M, k=k, dtype=dtype, order=order)) jpayne@69: jpayne@69: def rand(*args): jpayne@69: """ jpayne@69: Return a matrix of random values with given shape. jpayne@69: jpayne@69: Create a matrix of the given shape and propagate it with jpayne@69: random samples from a uniform distribution over ``[0, 1)``. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: \\*args : Arguments jpayne@69: Shape of the output. jpayne@69: If given as N integers, each integer specifies the size of one jpayne@69: dimension. jpayne@69: If given as a tuple, this tuple gives the complete shape. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: out : ndarray jpayne@69: The matrix of random values with shape given by `\\*args`. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: randn, numpy.random.RandomState.rand jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> np.random.seed(123) jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.rand(2, 3) jpayne@69: matrix([[0.69646919, 0.28613933, 0.22685145], jpayne@69: [0.55131477, 0.71946897, 0.42310646]]) jpayne@69: >>> np.matlib.rand((2, 3)) jpayne@69: matrix([[0.9807642 , 0.68482974, 0.4809319 ], jpayne@69: [0.39211752, 0.34317802, 0.72904971]]) jpayne@69: jpayne@69: If the first argument is a tuple, other arguments are ignored: jpayne@69: jpayne@69: >>> np.matlib.rand((2, 3), 4) jpayne@69: matrix([[0.43857224, 0.0596779 , 0.39804426], jpayne@69: [0.73799541, 0.18249173, 0.17545176]]) jpayne@69: jpayne@69: """ jpayne@69: if isinstance(args[0], tuple): jpayne@69: args = args[0] jpayne@69: return asmatrix(np.random.rand(*args)) jpayne@69: jpayne@69: def randn(*args): jpayne@69: """ jpayne@69: Return a random matrix with data from the "standard normal" distribution. jpayne@69: jpayne@69: `randn` generates a matrix filled with random floats sampled from a jpayne@69: univariate "normal" (Gaussian) distribution of mean 0 and variance 1. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: \\*args : Arguments jpayne@69: Shape of the output. jpayne@69: If given as N integers, each integer specifies the size of one jpayne@69: dimension. If given as a tuple, this tuple gives the complete shape. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: Z : matrix of floats jpayne@69: A matrix of floating-point samples drawn from the standard normal jpayne@69: distribution. jpayne@69: jpayne@69: See Also jpayne@69: -------- jpayne@69: rand, numpy.random.RandomState.randn jpayne@69: jpayne@69: Notes jpayne@69: ----- jpayne@69: For random samples from the normal distribution with mean ``mu`` and jpayne@69: standard deviation ``sigma``, use:: jpayne@69: jpayne@69: sigma * np.matlib.randn(...) + mu jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> np.random.seed(123) jpayne@69: >>> import numpy.matlib jpayne@69: >>> np.matlib.randn(1) jpayne@69: matrix([[-1.0856306]]) jpayne@69: >>> np.matlib.randn(1, 2, 3) jpayne@69: matrix([[ 0.99734545, 0.2829785 , -1.50629471], jpayne@69: [-0.57860025, 1.65143654, -2.42667924]]) jpayne@69: jpayne@69: Two-by-four matrix of samples from the normal distribution with jpayne@69: mean 3 and standard deviation 2.5: jpayne@69: jpayne@69: >>> 2.5 * np.matlib.randn((2, 4)) + 3 jpayne@69: matrix([[1.92771843, 6.16484065, 0.83314899, 1.30278462], jpayne@69: [2.76322758, 6.72847407, 1.40274501, 1.8900451 ]]) jpayne@69: jpayne@69: """ jpayne@69: if isinstance(args[0], tuple): jpayne@69: args = args[0] jpayne@69: return asmatrix(np.random.randn(*args)) jpayne@69: jpayne@69: def repmat(a, m, n): jpayne@69: """ jpayne@69: Repeat a 0-D to 2-D array or matrix MxN times. jpayne@69: jpayne@69: Parameters jpayne@69: ---------- jpayne@69: a : array_like jpayne@69: The array or matrix to be repeated. jpayne@69: m, n : int jpayne@69: The number of times `a` is repeated along the first and second axes. jpayne@69: jpayne@69: Returns jpayne@69: ------- jpayne@69: out : ndarray jpayne@69: The result of repeating `a`. jpayne@69: jpayne@69: Examples jpayne@69: -------- jpayne@69: >>> import numpy.matlib jpayne@69: >>> a0 = np.array(1) jpayne@69: >>> np.matlib.repmat(a0, 2, 3) jpayne@69: array([[1, 1, 1], jpayne@69: [1, 1, 1]]) jpayne@69: jpayne@69: >>> a1 = np.arange(4) jpayne@69: >>> np.matlib.repmat(a1, 2, 2) jpayne@69: array([[0, 1, 2, 3, 0, 1, 2, 3], jpayne@69: [0, 1, 2, 3, 0, 1, 2, 3]]) jpayne@69: jpayne@69: >>> a2 = np.asmatrix(np.arange(6).reshape(2, 3)) jpayne@69: >>> np.matlib.repmat(a2, 2, 3) jpayne@69: matrix([[0, 1, 2, 0, 1, 2, 0, 1, 2], jpayne@69: [3, 4, 5, 3, 4, 5, 3, 4, 5], jpayne@69: [0, 1, 2, 0, 1, 2, 0, 1, 2], jpayne@69: [3, 4, 5, 3, 4, 5, 3, 4, 5]]) jpayne@69: jpayne@69: """ jpayne@69: a = asanyarray(a) jpayne@69: ndim = a.ndim jpayne@69: if ndim == 0: jpayne@69: origrows, origcols = (1, 1) jpayne@69: elif ndim == 1: jpayne@69: origrows, origcols = (1, a.shape[0]) jpayne@69: else: jpayne@69: origrows, origcols = a.shape jpayne@69: rows = origrows * m jpayne@69: cols = origcols * n jpayne@69: c = a.reshape(1, a.size).repeat(m, 0).reshape(rows, origcols).repeat(n, 0) jpayne@69: return c.reshape(rows, cols)