9
9
__all__ += ['rand', 'randn', 'repmat']
11
11
def empty(shape, dtype=None, order='C'):
12
"""return an empty matrix of the given shape
13
Return a new matrix of given shape and type, without initializing entries.
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.
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
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]])
14
47
return ndarray.__new__(matrix, shape, dtype, order=order)
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.
165
eye : Equivalent array function
166
matlib.identity : Square identity matrix
199
numpy.eye : Equivalent array function.
200
identity : Square identity matrix.
170
For more detailed docuemtation, see the docstring of the equivalent
171
array function ``np.eye``.
204
>>> import numpy.matlib
205
>>> np.matlib.eye(3, k=1, dtype=float)
206
matrix([[ 0., 1., 0.],
174
211
return asmatrix(np.eye(n,M,k,dtype))
215
Return a matrix of random values with given shape.
217
Create a matrix of the given shape and propagate it with
218
random samples from a uniform distribution over ``[0, 1)``.
224
If given as N integers, each integer specifies the size of one
226
If given as a tuple, this tuple gives the complete shape.
231
The matrix of random values with shape given by `\\*args`.
235
randn, numpy.random.rand
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]])
247
If the first argument is a tuple, other arguments are ignored:
249
>>> np.matlib.rand((2, 3), 4)
250
matrix([[ 0.46898646, 0.15163588, 0.95188261],
251
[ 0.59208621, 0.09561818, 0.00583606]])
177
254
if isinstance(args[0], tuple):
179
256
return asmatrix(np.random.rand(*args))
181
258
def randn(*args):
260
Return a random matrix with data from the "standard normal" distribution.
262
`randn` generates a matrix filled with random floats sampled from a
263
univariate "normal" (Gaussian) distribution of mean 0 and variance 1.
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.
275
A matrix of floating-point samples drawn from the standard normal
284
For random samples from :math:`N(\\mu, \\sigma^2)`, use:
286
``sigma * np.matlib.randn(...) + mu``
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]])
297
Two-by-four matrix of samples from :math:`N(3, 6.25)`:
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]])
182
304
if isinstance(args[0], tuple):
184
306
return asmatrix(np.random.randn(*args))
186
308
def repmat(a, m, n):
187
"""Repeat a 0-d to 2-d array mxn times
310
Repeat a 0-D to 2-D array or matrix MxN times.
315
The array or matrix to be repeated.
317
The number of times `a` is repeated along the first and second axes.
322
The result of repeating `a`.
326
>>> import numpy.matlib
328
>>> np.matlib.repmat(a0, 2, 3)
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]])
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]])
189
345
a = asanyarray(a)