Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: import numpy as np Chris@87: from numpy.matrixlib.defmatrix import matrix, asmatrix Chris@87: # need * as we're copying the numpy namespace Chris@87: from numpy import * Chris@87: Chris@87: __version__ = np.__version__ Chris@87: Chris@87: __all__ = np.__all__[:] # copy numpy namespace Chris@87: __all__ += ['rand', 'randn', 'repmat'] Chris@87: Chris@87: def empty(shape, dtype=None, order='C'): Chris@87: """ Chris@87: Return a new matrix of given shape and type, without initializing entries. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: shape : int or tuple of int Chris@87: Shape of the empty matrix. Chris@87: dtype : data-type, optional Chris@87: Desired output data-type. Chris@87: order : {'C', 'F'}, optional Chris@87: Whether to store multi-dimensional data in C (row-major) or Chris@87: Fortran (column-major) order in memory. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: empty_like, zeros Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: `empty`, unlike `zeros`, does not set the matrix values to zero, Chris@87: and may therefore be marginally faster. On the other hand, it requires Chris@87: the user to manually set all the values in the array, and should be Chris@87: used with caution. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.empty((2, 2)) # filled with random data Chris@87: matrix([[ 6.76425276e-320, 9.79033856e-307], Chris@87: [ 7.39337286e-309, 3.22135945e-309]]) #random Chris@87: >>> np.matlib.empty((2, 2), dtype=int) Chris@87: matrix([[ 6600475, 0], Chris@87: [ 6586976, 22740995]]) #random Chris@87: Chris@87: """ Chris@87: return ndarray.__new__(matrix, shape, dtype, order=order) Chris@87: Chris@87: def ones(shape, dtype=None, order='C'): Chris@87: """ Chris@87: Matrix of ones. Chris@87: Chris@87: Return a matrix of given shape and type, filled with ones. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: shape : {sequence of ints, int} Chris@87: Shape of the matrix Chris@87: dtype : data-type, optional Chris@87: The desired data-type for the matrix, default is np.float64. Chris@87: order : {'C', 'F'}, optional Chris@87: Whether to store matrix in C- or Fortran-contiguous order, Chris@87: default is 'C'. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : matrix Chris@87: Matrix of ones of given shape, dtype, and order. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: ones : Array of ones. Chris@87: matlib.zeros : Zero matrix. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, Chris@87: `out` becomes a single row matrix of shape ``(1,N)``. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> np.matlib.ones((2,3)) Chris@87: matrix([[ 1., 1., 1.], Chris@87: [ 1., 1., 1.]]) Chris@87: Chris@87: >>> np.matlib.ones(2) Chris@87: matrix([[ 1., 1.]]) Chris@87: Chris@87: """ Chris@87: a = ndarray.__new__(matrix, shape, dtype, order=order) Chris@87: a.fill(1) Chris@87: return a Chris@87: Chris@87: def zeros(shape, dtype=None, order='C'): Chris@87: """ Chris@87: Return a matrix of given shape and type, filled with zeros. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: shape : int or sequence of ints Chris@87: Shape of the matrix Chris@87: dtype : data-type, optional Chris@87: The desired data-type for the matrix, default is float. Chris@87: order : {'C', 'F'}, optional Chris@87: Whether to store the result in C- or Fortran-contiguous order, Chris@87: default is 'C'. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : matrix Chris@87: Zero matrix of given shape, dtype, and order. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.zeros : Equivalent array function. Chris@87: matlib.ones : Return a matrix of ones. Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, Chris@87: `out` becomes a single row matrix of shape ``(1,N)``. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.zeros((2, 3)) Chris@87: matrix([[ 0., 0., 0.], Chris@87: [ 0., 0., 0.]]) Chris@87: Chris@87: >>> np.matlib.zeros(2) Chris@87: matrix([[ 0., 0.]]) Chris@87: Chris@87: """ Chris@87: a = ndarray.__new__(matrix, shape, dtype, order=order) Chris@87: a.fill(0) Chris@87: return a Chris@87: Chris@87: def identity(n,dtype=None): Chris@87: """ Chris@87: Returns the square identity matrix of given size. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: n : int Chris@87: Size of the returned identity matrix. Chris@87: dtype : data-type, optional Chris@87: Data-type of the output. Defaults to ``float``. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : matrix Chris@87: `n` x `n` matrix with its main diagonal set to one, Chris@87: and all other elements zero. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.identity : Equivalent array function. Chris@87: matlib.eye : More general matrix identity function. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.identity(3, dtype=int) Chris@87: matrix([[1, 0, 0], Chris@87: [0, 1, 0], Chris@87: [0, 0, 1]]) Chris@87: Chris@87: """ Chris@87: a = array([1]+n*[0], dtype=dtype) Chris@87: b = empty((n, n), dtype=dtype) Chris@87: b.flat = a Chris@87: return b Chris@87: Chris@87: def eye(n,M=None, k=0, dtype=float): Chris@87: """ Chris@87: Return a matrix with ones on the diagonal and zeros elsewhere. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: n : int Chris@87: Number of rows in the output. Chris@87: M : int, optional Chris@87: Number of columns in the output, defaults to `n`. Chris@87: k : int, optional Chris@87: Index of the diagonal: 0 refers to the main diagonal, Chris@87: a positive value refers to an upper diagonal, Chris@87: and a negative value to a lower diagonal. Chris@87: dtype : dtype, optional Chris@87: Data-type of the returned matrix. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: I : matrix Chris@87: A `n` x `M` matrix where all elements are equal to zero, Chris@87: except for the `k`-th diagonal, whose values are equal to one. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: numpy.eye : Equivalent array function. Chris@87: identity : Square identity matrix. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.eye(3, k=1, dtype=float) Chris@87: matrix([[ 0., 1., 0.], Chris@87: [ 0., 0., 1.], Chris@87: [ 0., 0., 0.]]) Chris@87: Chris@87: """ Chris@87: return asmatrix(np.eye(n, M, k, dtype)) Chris@87: Chris@87: def rand(*args): Chris@87: """ Chris@87: Return a matrix of random values with given shape. Chris@87: Chris@87: Create a matrix of the given shape and propagate it with Chris@87: random samples from a uniform distribution over ``[0, 1)``. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: \\*args : Arguments Chris@87: Shape of the output. Chris@87: If given as N integers, each integer specifies the size of one Chris@87: dimension. Chris@87: If given as a tuple, this tuple gives the complete shape. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The matrix of random values with shape given by `\\*args`. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: randn, numpy.random.rand Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.rand(2, 3) Chris@87: matrix([[ 0.68340382, 0.67926887, 0.83271405], Chris@87: [ 0.00793551, 0.20468222, 0.95253525]]) #random Chris@87: >>> np.matlib.rand((2, 3)) Chris@87: matrix([[ 0.84682055, 0.73626594, 0.11308016], Chris@87: [ 0.85429008, 0.3294825 , 0.89139555]]) #random Chris@87: Chris@87: If the first argument is a tuple, other arguments are ignored: Chris@87: Chris@87: >>> np.matlib.rand((2, 3), 4) Chris@87: matrix([[ 0.46898646, 0.15163588, 0.95188261], Chris@87: [ 0.59208621, 0.09561818, 0.00583606]]) #random Chris@87: Chris@87: """ Chris@87: if isinstance(args[0], tuple): Chris@87: args = args[0] Chris@87: return asmatrix(np.random.rand(*args)) Chris@87: Chris@87: def randn(*args): Chris@87: """ Chris@87: Return a random matrix with data from the "standard normal" distribution. Chris@87: Chris@87: `randn` generates a matrix filled with random floats sampled from a Chris@87: univariate "normal" (Gaussian) distribution of mean 0 and variance 1. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: \\*args : Arguments Chris@87: Shape of the output. Chris@87: If given as N integers, each integer specifies the size of one Chris@87: dimension. If given as a tuple, this tuple gives the complete shape. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: Z : matrix of floats Chris@87: A matrix of floating-point samples drawn from the standard normal Chris@87: distribution. Chris@87: Chris@87: See Also Chris@87: -------- Chris@87: rand, random.randn Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: For random samples from :math:`N(\\mu, \\sigma^2)`, use: Chris@87: Chris@87: ``sigma * np.matlib.randn(...) + mu`` Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> np.matlib.randn(1) Chris@87: matrix([[-0.09542833]]) #random Chris@87: >>> np.matlib.randn(1, 2, 3) Chris@87: matrix([[ 0.16198284, 0.0194571 , 0.18312985], Chris@87: [-0.7509172 , 1.61055 , 0.45298599]]) #random Chris@87: Chris@87: Two-by-four matrix of samples from :math:`N(3, 6.25)`: Chris@87: Chris@87: >>> 2.5 * np.matlib.randn((2, 4)) + 3 Chris@87: matrix([[ 4.74085004, 8.89381862, 4.09042411, 4.83721922], Chris@87: [ 7.52373709, 5.07933944, -2.64043543, 0.45610557]]) #random Chris@87: Chris@87: """ Chris@87: if isinstance(args[0], tuple): Chris@87: args = args[0] Chris@87: return asmatrix(np.random.randn(*args)) Chris@87: Chris@87: def repmat(a, m, n): Chris@87: """ Chris@87: Repeat a 0-D to 2-D array or matrix MxN times. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: a : array_like Chris@87: The array or matrix to be repeated. Chris@87: m, n : int Chris@87: The number of times `a` is repeated along the first and second axes. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: out : ndarray Chris@87: The result of repeating `a`. Chris@87: Chris@87: Examples Chris@87: -------- Chris@87: >>> import numpy.matlib Chris@87: >>> a0 = np.array(1) Chris@87: >>> np.matlib.repmat(a0, 2, 3) Chris@87: array([[1, 1, 1], Chris@87: [1, 1, 1]]) Chris@87: Chris@87: >>> a1 = np.arange(4) Chris@87: >>> np.matlib.repmat(a1, 2, 2) Chris@87: array([[0, 1, 2, 3, 0, 1, 2, 3], Chris@87: [0, 1, 2, 3, 0, 1, 2, 3]]) Chris@87: Chris@87: >>> a2 = np.asmatrix(np.arange(6).reshape(2, 3)) Chris@87: >>> np.matlib.repmat(a2, 2, 3) Chris@87: matrix([[0, 1, 2, 0, 1, 2, 0, 1, 2], Chris@87: [3, 4, 5, 3, 4, 5, 3, 4, 5], Chris@87: [0, 1, 2, 0, 1, 2, 0, 1, 2], Chris@87: [3, 4, 5, 3, 4, 5, 3, 4, 5]]) Chris@87: Chris@87: """ Chris@87: a = asanyarray(a) Chris@87: ndim = a.ndim Chris@87: if ndim == 0: Chris@87: origrows, origcols = (1, 1) Chris@87: elif ndim == 1: Chris@87: origrows, origcols = (1, a.shape[0]) Chris@87: else: Chris@87: origrows, origcols = a.shape Chris@87: rows = origrows * m Chris@87: cols = origcols * n Chris@87: c = a.reshape(1, a.size).repeat(m, 0).reshape(rows, origcols).repeat(n, 0) Chris@87: return c.reshape(rows, cols)