home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / python / Lib / site-packages / numpy / matlib.py < prev    next >
Encoding:
Python Source  |  2010-05-29  |  9.1 KB  |  357 lines

  1. import numpy as np
  2. from numpy.matrixlib.defmatrix import matrix, asmatrix
  3. # need * as we're copying the numpy namespace
  4. from numpy import *
  5.  
  6. __version__ = np.__version__
  7.  
  8. __all__ = np.__all__[:] # copy numpy namespace
  9. __all__ += ['rand', 'randn', 'repmat']
  10.  
  11. def empty(shape, dtype=None, order='C'):
  12.     """
  13.     Return a new matrix of given shape and type, without initializing entries.
  14.  
  15.     Parameters
  16.     ----------
  17.     shape : int or tuple of int
  18.         Shape of the empty matrix.
  19.     dtype : data-type, optional
  20.         Desired output data-type.
  21.     order : {'C', 'F'}, optional
  22.         Whether to store multi-dimensional data in C (row-major) or
  23.         Fortran (column-major) order in memory.
  24.  
  25.     See Also
  26.     --------
  27.     empty_like, zeros
  28.  
  29.     Notes
  30.     -----
  31.     `empty`, unlike `zeros`, does not set the matrix values to zero,
  32.     and may therefore be marginally faster.  On the other hand, it requires
  33.     the user to manually set all the values in the array, and should be
  34.     used with caution.
  35.  
  36.     Examples
  37.     --------
  38.     >>> import numpy.matlib
  39.     >>> np.matlib.empty((2, 2))    # filled with random data
  40.     matrix([[  6.76425276e-320,   9.79033856e-307],
  41.             [  7.39337286e-309,   3.22135945e-309]])
  42.     >>> np.matlib.empty((2, 2), dtype=int)
  43.     matrix([[ 6600475,        0],
  44.             [ 6586976, 22740995]])
  45.  
  46.     """
  47.     return ndarray.__new__(matrix, shape, dtype, order=order)
  48.  
  49. def ones(shape, dtype=None, order='C'):
  50.     """
  51.     Matrix of ones.
  52.  
  53.     Return a matrix of given shape and type, filled with ones.
  54.  
  55.     Parameters
  56.     ----------
  57.     shape : {sequence of ints, int}
  58.         Shape of the matrix
  59.     dtype : data-type, optional
  60.         The desired data-type for the matrix, default is np.float64.
  61.     order : {'C', 'F'}, optional
  62.         Whether to store matrix in C- or Fortran-contiguous order,
  63.         default is 'C'.
  64.  
  65.     Returns
  66.     -------
  67.     out : matrix
  68.         Matrix of ones of given shape, dtype, and order.
  69.  
  70.     See Also
  71.     --------
  72.     ones : Array of ones.
  73.     matlib.zeros : Zero matrix.
  74.  
  75.     Notes
  76.     -----
  77.     If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``,
  78.     `out` becomes a single row matrix of shape ``(1,N)``.
  79.  
  80.     Examples
  81.     --------
  82.     >>> np.matlib.ones((2,3))
  83.     matrix([[ 1.,  1.,  1.],
  84.             [ 1.,  1.,  1.]])
  85.  
  86.     >>> np.matlib.ones(2)
  87.     matrix([[ 1.,  1.]])
  88.  
  89.     """
  90.     a = ndarray.__new__(matrix, shape, dtype, order=order)
  91.     a.fill(1)
  92.     return a
  93.  
  94. def zeros(shape, dtype=None, order='C'):
  95.     """
  96.     Return a matrix of given shape and type, filled with zeros.
  97.  
  98.     Parameters
  99.     ----------
  100.     shape : int or sequence of ints
  101.         Shape of the matrix
  102.     dtype : data-type, optional
  103.         The desired data-type for the matrix, default is float.
  104.     order : {'C', 'F'}, optional
  105.         Whether to store the result in C- or Fortran-contiguous order,
  106.         default is 'C'.
  107.  
  108.     Returns
  109.     -------
  110.     out : matrix
  111.         Zero matrix of given shape, dtype, and order.
  112.  
  113.     See Also
  114.     --------
  115.     numpy.zeros : Equivalent array function.
  116.     matlib.ones : Return a matrix of ones.
  117.  
  118.     Notes
  119.     -----
  120.     If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``,
  121.     `out` becomes a single row matrix of shape ``(1,N)``.
  122.  
  123.     Examples
  124.     --------
  125.     >>> import numpy.matlib
  126.     >>> np.matlib.zeros((2, 3))
  127.     matrix([[ 0.,  0.,  0.],
  128.             [ 0.,  0.,  0.]])
  129.  
  130.     >>> np.matlib.zeros(2)
  131.     matrix([[ 0.,  0.]])
  132.  
  133.     """
  134.     a = ndarray.__new__(matrix, shape, dtype, order=order)
  135.     a.fill(0)
  136.     return a
  137.  
  138. def identity(n,dtype=None):
  139.     """
  140.     Returns the square identity matrix of given size.
  141.  
  142.     Parameters
  143.     ----------
  144.     n : int
  145.         Size of the returned identity matrix.
  146.     dtype : data-type, optional
  147.         Data-type of the output. Defaults to ``float``.
  148.  
  149.     Returns
  150.     -------
  151.     out : matrix
  152.         `n` x `n` matrix with its main diagonal set to one,
  153.         and all other elements zero.
  154.  
  155.     See Also
  156.     --------
  157.     numpy.identity : Equivalent array function.
  158.     matlib.eye : More general matrix identity function.
  159.  
  160.     Examples
  161.     --------
  162.     >>> import numpy.matlib
  163.     >>> np.identity(3, dtype=int)
  164.     array([[1, 0, 0],
  165.            [0, 1, 0],
  166.            [0, 0, 1]])
  167.  
  168.     """
  169.     a = array([1]+n*[0],dtype=dtype)
  170.     b = empty((n,n),dtype=dtype)
  171.     b.flat = a
  172.     return b
  173.  
  174. def eye(n,M=None, k=0, dtype=float):
  175.     """
  176.     Return a matrix with ones on the diagonal and zeros elsewhere.
  177.  
  178.     Parameters
  179.     ----------
  180.     n : int
  181.         Number of rows in the output.
  182.     M : int, optional
  183.         Number of columns in the output, defaults to `n`.
  184.     k : int, optional
  185.         Index of the diagonal: 0 refers to the main diagonal,
  186.         a positive value refers to an upper diagonal,
  187.         and a negative value to a lower diagonal.
  188.     dtype : dtype, optional
  189.         Data-type of the returned matrix.
  190.  
  191.     Returns
  192.     -------
  193.     I : matrix
  194.         A `n` x `M` matrix where all elements are equal to zero,
  195.         except for the `k`-th diagonal, whose values are equal to one.
  196.  
  197.     See Also
  198.     --------
  199.     numpy.eye : Equivalent array function.
  200.     identity : Square identity matrix.
  201.  
  202.     Examples
  203.     --------
  204.     >>> import numpy.matlib
  205.     >>> np.matlib.eye(3, k=1, dtype=float)
  206.     matrix([[ 0.,  1.,  0.],
  207.             [ 0.,  0.,  1.],
  208.             [ 0.,  0.,  0.]])
  209.  
  210.     """
  211.     return asmatrix(np.eye(n,M,k,dtype))
  212.  
  213. def rand(*args):
  214.     """
  215.     Return a matrix of random values with given shape.
  216.  
  217.     Create a matrix of the given shape and propagate it with
  218.     random samples from a uniform distribution over ``[0, 1)``.
  219.  
  220.     Parameters
  221.     ----------
  222.     \\*args : Arguments
  223.         Shape of the output.
  224.         If given as N integers, each integer specifies the size of one
  225.         dimension.
  226.         If given as a tuple, this tuple gives the complete shape.
  227.  
  228.     Returns
  229.     -------
  230.     out : ndarray
  231.         The matrix of random values with shape given by `\\*args`.
  232.  
  233.     See Also
  234.     --------
  235.     randn, numpy.random.rand
  236.  
  237.     Examples
  238.     --------
  239.     >>> import numpy.matlib
  240.     >>> np.matlib.rand(2, 3)
  241.     matrix([[ 0.68340382,  0.67926887,  0.83271405],
  242.             [ 0.00793551,  0.20468222,  0.95253525]])
  243.     >>> np.matlib.rand((2, 3))
  244.     matrix([[ 0.84682055,  0.73626594,  0.11308016],
  245.             [ 0.85429008,  0.3294825 ,  0.89139555]])
  246.  
  247.     If the first argument is a tuple, other arguments are ignored:
  248.  
  249.     >>> np.matlib.rand((2, 3), 4)
  250.     matrix([[ 0.46898646,  0.15163588,  0.95188261],
  251.             [ 0.59208621,  0.09561818,  0.00583606]])
  252.  
  253.     """
  254.     if isinstance(args[0], tuple):
  255.         args = args[0]
  256.     return asmatrix(np.random.rand(*args))
  257.  
  258. def randn(*args):
  259.     """
  260.     Return a random matrix with data from the "standard normal" distribution.
  261.  
  262.     `randn` generates a matrix filled with random floats sampled from a
  263.     univariate "normal" (Gaussian) distribution of mean 0 and variance 1.
  264.  
  265.     Parameters
  266.     ----------
  267.     \\*args : Arguments
  268.         Shape of the output.
  269.         If given as N integers, each integer specifies the size of one
  270.         dimension. If given as a tuple, this tuple gives the complete shape.
  271.  
  272.     Returns
  273.     -------
  274.     Z : matrix of floats
  275.         A matrix of floating-point samples drawn from the standard normal
  276.         distribution.
  277.  
  278.     See Also
  279.     --------
  280.     rand, random.randn
  281.  
  282.     Notes
  283.     -----
  284.     For random samples from :math:`N(\\mu, \\sigma^2)`, use:
  285.  
  286.     ``sigma * np.matlib.randn(...) + mu``
  287.  
  288.     Examples
  289.     --------
  290.     >>> import numpy.matlib
  291.     >>> np.matlib.randn(1)
  292.     matrix([[-0.09542833]])
  293.     >>> np.matlib.randn(1, 2, 3)
  294.     matrix([[ 0.16198284,  0.0194571 ,  0.18312985],
  295.             [-0.7509172 ,  1.61055   ,  0.45298599]])
  296.  
  297.     Two-by-four matrix of samples from :math:`N(3, 6.25)`:
  298.  
  299.     >>> 2.5 * np.matlib.randn((2, 4)) + 3
  300.     matrix([[ 4.74085004,  8.89381862,  4.09042411,  4.83721922],
  301.             [ 7.52373709,  5.07933944, -2.64043543,  0.45610557]])
  302.  
  303.     """
  304.     if isinstance(args[0], tuple):
  305.         args = args[0]
  306.     return asmatrix(np.random.randn(*args))
  307.  
  308. def repmat(a, m, n):
  309.     """
  310.     Repeat a 0-D to 2-D array or matrix MxN times.
  311.  
  312.     Parameters
  313.     ----------
  314.     a : array_like
  315.         The array or matrix to be repeated.
  316.     m, n : int
  317.         The number of times `a` is repeated along the first and second axes.
  318.  
  319.     Returns
  320.     -------
  321.     out : ndarray
  322.         The result of repeating `a`.
  323.  
  324.     Examples
  325.     --------
  326.     >>> import numpy.matlib
  327.     >>> a0 = np.array(1)
  328.     >>> np.matlib.repmat(a0, 2, 3)
  329.     array([[1, 1, 1],
  330.            [1, 1, 1]])
  331.  
  332.     >>> a1 = np.arange(4)
  333.     >>> np.matlib.repmat(a1, 2, 2)
  334.     array([[0, 1, 2, 3, 0, 1, 2, 3],
  335.            [0, 1, 2, 3, 0, 1, 2, 3]])
  336.  
  337.     >>> a2 = np.asmatrix(np.arange(6).reshape(2, 3))
  338.     >>> np.matlib.repmat(a2, 2, 3)
  339.     matrix([[0, 1, 2, 0, 1, 2, 0, 1, 2],
  340.             [3, 4, 5, 3, 4, 5, 3, 4, 5],
  341.             [0, 1, 2, 0, 1, 2, 0, 1, 2],
  342.             [3, 4, 5, 3, 4, 5, 3, 4, 5]])
  343.  
  344.     """
  345.     a = asanyarray(a)
  346.     ndim = a.ndim
  347.     if ndim == 0:
  348.         origrows, origcols = (1,1)
  349.     elif ndim == 1:
  350.         origrows, origcols = (1, a.shape[0])
  351.     else:
  352.         origrows, origcols = a.shape
  353.     rows = origrows * m
  354.     cols = origcols * n
  355.     c = a.reshape(1,a.size).repeat(m, 0).reshape(rows, origcols).repeat(n,0)
  356.     return c.reshape(rows, cols)
  357.