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