Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/matlib.py
7756 views
import warnings12# 2018-05-29, PendingDeprecationWarning added to matrix.__new__3# 2020-01-23, numpy 1.19.0 PendingDeprecatonWarning4warnings.warn("Importing from numpy.matlib is deprecated since 1.19.0. "5"The matrix subclass is not the recommended way to represent "6"matrices or deal with linear algebra (see "7"https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). "8"Please adjust your code to use regular ndarray. ",9PendingDeprecationWarning, stacklevel=2)1011import numpy as np12from numpy.matrixlib.defmatrix import matrix, asmatrix13# Matlib.py contains all functions in the numpy namespace with a few14# replacements. See doc/source/reference/routines.matlib.rst for details.15# Need * as we're copying the numpy namespace.16from numpy import * # noqa: F4031718__version__ = np.__version__1920__all__ = np.__all__[:] # copy numpy namespace21__all__ += ['rand', 'randn', 'repmat']2223def empty(shape, dtype=None, order='C'):24"""Return a new matrix of given shape and type, without initializing entries.2526Parameters27----------28shape : int or tuple of int29Shape of the empty matrix.30dtype : data-type, optional31Desired output data-type.32order : {'C', 'F'}, optional33Whether to store multi-dimensional data in row-major34(C-style) or column-major (Fortran-style) order in35memory.3637See Also38--------39empty_like, zeros4041Notes42-----43`empty`, unlike `zeros`, does not set the matrix values to zero,44and may therefore be marginally faster. On the other hand, it requires45the user to manually set all the values in the array, and should be46used with caution.4748Examples49--------50>>> import numpy.matlib51>>> np.matlib.empty((2, 2)) # filled with random data52matrix([[ 6.76425276e-320, 9.79033856e-307], # random53[ 7.39337286e-309, 3.22135945e-309]])54>>> np.matlib.empty((2, 2), dtype=int)55matrix([[ 6600475, 0], # random56[ 6586976, 22740995]])5758"""59return ndarray.__new__(matrix, shape, dtype, order=order)6061def ones(shape, dtype=None, order='C'):62"""63Matrix of ones.6465Return a matrix of given shape and type, filled with ones.6667Parameters68----------69shape : {sequence of ints, int}70Shape of the matrix71dtype : data-type, optional72The desired data-type for the matrix, default is np.float64.73order : {'C', 'F'}, optional74Whether to store matrix in C- or Fortran-contiguous order,75default is 'C'.7677Returns78-------79out : matrix80Matrix of ones of given shape, dtype, and order.8182See Also83--------84ones : Array of ones.85matlib.zeros : Zero matrix.8687Notes88-----89If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``,90`out` becomes a single row matrix of shape ``(1,N)``.9192Examples93--------94>>> np.matlib.ones((2,3))95matrix([[1., 1., 1.],96[1., 1., 1.]])9798>>> np.matlib.ones(2)99matrix([[1., 1.]])100101"""102a = ndarray.__new__(matrix, shape, dtype, order=order)103a.fill(1)104return a105106def zeros(shape, dtype=None, order='C'):107"""108Return a matrix of given shape and type, filled with zeros.109110Parameters111----------112shape : int or sequence of ints113Shape of the matrix114dtype : data-type, optional115The desired data-type for the matrix, default is float.116order : {'C', 'F'}, optional117Whether to store the result in C- or Fortran-contiguous order,118default is 'C'.119120Returns121-------122out : matrix123Zero matrix of given shape, dtype, and order.124125See Also126--------127numpy.zeros : Equivalent array function.128matlib.ones : Return a matrix of ones.129130Notes131-----132If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``,133`out` becomes a single row matrix of shape ``(1,N)``.134135Examples136--------137>>> import numpy.matlib138>>> np.matlib.zeros((2, 3))139matrix([[0., 0., 0.],140[0., 0., 0.]])141142>>> np.matlib.zeros(2)143matrix([[0., 0.]])144145"""146a = ndarray.__new__(matrix, shape, dtype, order=order)147a.fill(0)148return a149150def identity(n,dtype=None):151"""152Returns the square identity matrix of given size.153154Parameters155----------156n : int157Size of the returned identity matrix.158dtype : data-type, optional159Data-type of the output. Defaults to ``float``.160161Returns162-------163out : matrix164`n` x `n` matrix with its main diagonal set to one,165and all other elements zero.166167See Also168--------169numpy.identity : Equivalent array function.170matlib.eye : More general matrix identity function.171172Examples173--------174>>> import numpy.matlib175>>> np.matlib.identity(3, dtype=int)176matrix([[1, 0, 0],177[0, 1, 0],178[0, 0, 1]])179180"""181a = array([1]+n*[0], dtype=dtype)182b = empty((n, n), dtype=dtype)183b.flat = a184return b185186def eye(n,M=None, k=0, dtype=float, order='C'):187"""188Return a matrix with ones on the diagonal and zeros elsewhere.189190Parameters191----------192n : int193Number of rows in the output.194M : int, optional195Number of columns in the output, defaults to `n`.196k : int, optional197Index of the diagonal: 0 refers to the main diagonal,198a positive value refers to an upper diagonal,199and a negative value to a lower diagonal.200dtype : dtype, optional201Data-type of the returned matrix.202order : {'C', 'F'}, optional203Whether the output should be stored in row-major (C-style) or204column-major (Fortran-style) order in memory.205206.. versionadded:: 1.14.0207208Returns209-------210I : matrix211A `n` x `M` matrix where all elements are equal to zero,212except for the `k`-th diagonal, whose values are equal to one.213214See Also215--------216numpy.eye : Equivalent array function.217identity : Square identity matrix.218219Examples220--------221>>> import numpy.matlib222>>> np.matlib.eye(3, k=1, dtype=float)223matrix([[0., 1., 0.],224[0., 0., 1.],225[0., 0., 0.]])226227"""228return asmatrix(np.eye(n, M=M, k=k, dtype=dtype, order=order))229230def rand(*args):231"""232Return a matrix of random values with given shape.233234Create a matrix of the given shape and propagate it with235random samples from a uniform distribution over ``[0, 1)``.236237Parameters238----------239\\*args : Arguments240Shape of the output.241If given as N integers, each integer specifies the size of one242dimension.243If given as a tuple, this tuple gives the complete shape.244245Returns246-------247out : ndarray248The matrix of random values with shape given by `\\*args`.249250See Also251--------252randn, numpy.random.RandomState.rand253254Examples255--------256>>> np.random.seed(123)257>>> import numpy.matlib258>>> np.matlib.rand(2, 3)259matrix([[0.69646919, 0.28613933, 0.22685145],260[0.55131477, 0.71946897, 0.42310646]])261>>> np.matlib.rand((2, 3))262matrix([[0.9807642 , 0.68482974, 0.4809319 ],263[0.39211752, 0.34317802, 0.72904971]])264265If the first argument is a tuple, other arguments are ignored:266267>>> np.matlib.rand((2, 3), 4)268matrix([[0.43857224, 0.0596779 , 0.39804426],269[0.73799541, 0.18249173, 0.17545176]])270271"""272if isinstance(args[0], tuple):273args = args[0]274return asmatrix(np.random.rand(*args))275276def randn(*args):277"""278Return a random matrix with data from the "standard normal" distribution.279280`randn` generates a matrix filled with random floats sampled from a281univariate "normal" (Gaussian) distribution of mean 0 and variance 1.282283Parameters284----------285\\*args : Arguments286Shape of the output.287If given as N integers, each integer specifies the size of one288dimension. If given as a tuple, this tuple gives the complete shape.289290Returns291-------292Z : matrix of floats293A matrix of floating-point samples drawn from the standard normal294distribution.295296See Also297--------298rand, numpy.random.RandomState.randn299300Notes301-----302For random samples from :math:`N(\\mu, \\sigma^2)`, use:303304``sigma * np.matlib.randn(...) + mu``305306Examples307--------308>>> np.random.seed(123)309>>> import numpy.matlib310>>> np.matlib.randn(1)311matrix([[-1.0856306]])312>>> np.matlib.randn(1, 2, 3)313matrix([[ 0.99734545, 0.2829785 , -1.50629471],314[-0.57860025, 1.65143654, -2.42667924]])315316Two-by-four matrix of samples from :math:`N(3, 6.25)`:317318>>> 2.5 * np.matlib.randn((2, 4)) + 3319matrix([[1.92771843, 6.16484065, 0.83314899, 1.30278462],320[2.76322758, 6.72847407, 1.40274501, 1.8900451 ]])321322"""323if isinstance(args[0], tuple):324args = args[0]325return asmatrix(np.random.randn(*args))326327def repmat(a, m, n):328"""329Repeat a 0-D to 2-D array or matrix MxN times.330331Parameters332----------333a : array_like334The array or matrix to be repeated.335m, n : int336The number of times `a` is repeated along the first and second axes.337338Returns339-------340out : ndarray341The result of repeating `a`.342343Examples344--------345>>> import numpy.matlib346>>> a0 = np.array(1)347>>> np.matlib.repmat(a0, 2, 3)348array([[1, 1, 1],349[1, 1, 1]])350351>>> a1 = np.arange(4)352>>> np.matlib.repmat(a1, 2, 2)353array([[0, 1, 2, 3, 0, 1, 2, 3],354[0, 1, 2, 3, 0, 1, 2, 3]])355356>>> a2 = np.asmatrix(np.arange(6).reshape(2, 3))357>>> np.matlib.repmat(a2, 2, 3)358matrix([[0, 1, 2, 0, 1, 2, 0, 1, 2],359[3, 4, 5, 3, 4, 5, 3, 4, 5],360[0, 1, 2, 0, 1, 2, 0, 1, 2],361[3, 4, 5, 3, 4, 5, 3, 4, 5]])362363"""364a = asanyarray(a)365ndim = a.ndim366if ndim == 0:367origrows, origcols = (1, 1)368elif ndim == 1:369origrows, origcols = (1, a.shape[0])370else:371origrows, origcols = a.shape372rows = origrows * m373cols = origcols * n374c = a.reshape(1, a.size).repeat(m, 0).reshape(rows, origcols).repeat(n, 0)375return c.reshape(rows, cols)376377378