~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/matlib.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-10-07 10:19:13 UTC
  • mfrom: (7.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20101007101913-8b1kmt8ho4upcl9s
Tags: 1:1.4.1-5
* debian/patches/10_use_local_python.org_object.inv_sphinx.diff
  - fixed small typo in description
* debian/patches/changeset_r8364.diff
  - fix memory corruption (double free); thanks to Joseph Barillari for the
    report and to Michael Gilbert for pushing resolution; Closes: #581058

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import numpy as np
2
 
from numpy.core.defmatrix import matrix, asmatrix
 
2
from numpy.matrixlib.defmatrix import matrix, asmatrix
3
3
# need * as we're copying the numpy namespace
4
4
from numpy import *
5
5
 
9
9
__all__ += ['rand', 'randn', 'repmat']
10
10
 
11
11
def empty(shape, dtype=None, order='C'):
12
 
    """return an empty matrix of the given shape
 
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
 
13
46
    """
14
47
    return ndarray.__new__(matrix, shape, dtype, order=order)
15
48
 
60
93
 
61
94
def zeros(shape, dtype=None, order='C'):
62
95
    """
63
 
    Zero matrix.
64
 
 
65
 
    Return a matrix of given shape and type, filled with zeros
 
96
    Return a matrix of given shape and type, filled with zeros.
66
97
 
67
98
    Parameters
68
99
    ----------
69
 
    shape : {sequence of ints, int}
 
100
    shape : int or sequence of ints
70
101
        Shape of the matrix
71
102
    dtype : data-type, optional
72
 
        The desired data-type for the matrix, default is np.float64.
 
103
        The desired data-type for the matrix, default is float.
73
104
    order : {'C', 'F'}, optional
74
105
        Whether to store the result in C- or Fortran-contiguous order,
75
106
        default is 'C'.
81
112
 
82
113
    See Also
83
114
    --------
84
 
    zeros : Zero array.
85
 
    matlib.ones : Matrix of ones.
 
115
    numpy.zeros : Equivalent array function.
 
116
    matlib.ones : Return a matrix of ones.
86
117
 
87
118
    Notes
88
119
    -----
91
122
 
92
123
    Examples
93
124
    --------
94
 
    >>> np.matlib.zeros((2,3))
 
125
    >>> import numpy.matlib
 
126
    >>> np.matlib.zeros((2, 3))
95
127
    matrix([[ 0.,  0.,  0.],
96
128
            [ 0.,  0.,  0.]])
97
129
 
110
142
    Parameters
111
143
    ----------
112
144
    n : int
113
 
        Size of identity matrix
114
 
 
 
145
        Size of the returned identity matrix.
115
146
    dtype : data-type, optional
116
147
        Data-type of the output. Defaults to ``float``.
117
148
 
123
154
 
124
155
    See Also
125
156
    --------
126
 
    identity : Equivalent array function.
 
157
    numpy.identity : Equivalent array function.
127
158
    matlib.eye : More general matrix identity function.
128
159
 
129
 
    Notes
130
 
    -----
131
 
    For more detailed documentation, see the docstring of the equivalent
132
 
    array function ``np.identity``
 
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]])
133
167
 
134
168
    """
135
169
    a = array([1]+n*[0],dtype=dtype)
146
180
    n : int
147
181
        Number of rows in the output.
148
182
    M : int, optional
149
 
        Number of columns in the output, defaults to n.
 
183
        Number of columns in the output, defaults to `n`.
150
184
    k : int, optional
151
185
        Index of the diagonal: 0 refers to the main diagonal,
152
186
        a positive value refers to an upper diagonal,
158
192
    -------
159
193
    I : matrix
160
194
        A `n` x `M` matrix where all elements are equal to zero,
161
 
        except for the k-th diagonal, whose values are equal to one.
 
195
        except for the `k`-th diagonal, whose values are equal to one.
162
196
 
163
197
    See Also
164
198
    --------
165
 
    eye : Equivalent array function
166
 
    matlib.identity : Square identity matrix
 
199
    numpy.eye : Equivalent array function.
 
200
    identity : Square identity matrix.
167
201
 
168
 
    Notes
169
 
    -----
170
 
    For more detailed docuemtation, see the docstring of the equivalent
171
 
    array function ``np.eye``.
 
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.]])
172
209
 
173
210
    """
174
211
    return asmatrix(np.eye(n,M,k,dtype))
175
212
 
176
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
    """
177
254
    if isinstance(args[0], tuple):
178
255
        args = args[0]
179
256
    return asmatrix(np.random.rand(*args))
180
257
 
181
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
    """
182
304
    if isinstance(args[0], tuple):
183
305
        args = args[0]
184
306
    return asmatrix(np.random.randn(*args))
185
307
 
186
308
def repmat(a, m, n):
187
 
    """Repeat a 0-d to 2-d array mxn times
 
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
 
188
344
    """
189
345
    a = asanyarray(a)
190
346
    ndim = a.ndim