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