annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/numpy/matlib.py @ 68:5028fdace37b

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