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