Skip to content

matrix

dup_matrix(m, M, n1, n2)

Copy the contents of matrix M to matrix m.

Parameters:

Name Type Description Default
m ndarray

The destination matrix.

required
M ndarray

The source matrix.

required
n1 int

The number of rows in the matrices.

required
n2 int

The number of columns in the matrices.

required

Returns:

Name Type Description
ndarray ndarray

The updated destination matrix m.

Examples:

>>> M = np.array([[1, 2], [3, 4], [5, 6]])
>>> m = np.zeros((3, 2))
>>> dup_matrix(m, M, 3, 2)
>>> print(m)
[[1. 2.]
 [3. 4.]
 [5. 6.]]
Source code in spotpython/gp/matrix.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
def dup_matrix(m, M, n1, n2) -> np.ndarray:
    """
    Copy the contents of matrix M to matrix m.

    Args:
        m (ndarray): The destination matrix.
        M (ndarray): The source matrix.
        n1 (int): The number of rows in the matrices.
        n2 (int): The number of columns in the matrices.

    Returns:
        ndarray: The updated destination matrix m.

    Examples:
        >>> M = np.array([[1, 2], [3, 4], [5, 6]])
        >>> m = np.zeros((3, 2))
        >>> dup_matrix(m, M, 3, 2)
        >>> print(m)
        [[1. 2.]
         [3. 4.]
         [5. 6.]]
    """
    for i in range(n1):
        for j in range(n2):
            m[i, j] = M[i, j]
    return m

dupv(v, vold, n)

Copies vold to v (assumes v has already been allocated).

Parameters:

Name Type Description Default
v ndarray

The array to copy to.

required
vold ndarray

The original array to copy from.

required
n int

The size of the arrays.

required

Returns:

Name Type Description
ndarray ndarray

The updated array v.

Examples:

>>> v = np.empty(3)
>>> vold = np.array([1.0, 2.0, 3.0])
>>> n = 3
>>> dupv(v, vold, n)
>>> print(v)
[1. 2. 3.]
Source code in spotpython/gp/matrix.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def dupv(v, vold, n) -> np.ndarray:
    """
    Copies vold to v (assumes v has already been allocated).

    Args:
        v (ndarray): The array to copy to.
        vold (ndarray): The original array to copy from.
        n (int): The size of the arrays.

    Returns:
        ndarray: The updated array v.

    Examples:
        >>> v = np.empty(3)
        >>> vold = np.array([1.0, 2.0, 3.0])
        >>> n = 3
        >>> dupv(v, vold, n)
        >>> print(v)
        [1. 2. 3.]
    """
    for i in range(n):
        v[i] = vold[i]
    return v

new_dup_matrix(M, n1, n2)

Create a new n1 x n2 matrix which is allocated like an n1*n2 array, and copy the contents of n1 x n2 matrix M into it.

Parameters:

Name Type Description Default
M ndarray

The source matrix.

required
n1 int

The number of rows in the matrix.

required
n2 int

The number of columns in the matrix.

required

Returns:

Name Type Description
ndarray ndarray

The new n1 x n2 matrix with copied contents.

Examples:

>>> M = np.array([[1, 2], [3, 4], [5, 6]])
>>> n1 = 3
>>> n2 = 2
>>> m = new_dup_matrix(M, n1, n2)
>>> print(m)
[[1. 2.]
 [3. 4.]
 [5. 6.]]
Source code in spotpython/gp/matrix.py
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def new_dup_matrix(M, n1, n2) -> np.ndarray:
    """
    Create a new n1 x n2 matrix which is allocated like an n1*n2 array,
    and copy the contents of n1 x n2 matrix M into it.

    Args:
        M (ndarray): The source matrix.
        n1 (int): The number of rows in the matrix.
        n2 (int): The number of columns in the matrix.

    Returns:
        ndarray: The new n1 x n2 matrix with copied contents.

    Examples:
        >>> M = np.array([[1, 2], [3, 4], [5, 6]])
        >>> n1 = 3
        >>> n2 = 2
        >>> m = new_dup_matrix(M, n1, n2)
        >>> print(m)
        [[1. 2.]
         [3. 4.]
         [5. 6.]]
    """
    if n1 <= 0 or n2 <= 0:
        return None

    m = new_matrix(n1, n2)
    dup_matrix(m, M, n1, n2)
    return m

new_dup_vector(vold, n)

Allocates a new numpy array of size n and fills it with the contents of vold.

Parameters:

Name Type Description Default
vold ndarray

The original array to duplicate.

required
n int

The size of the new array.

required

Returns:

Name Type Description
ndarray ndarray

The new array filled with the contents of vold.

Examples:

>>> vold = np.array([1.0, 2.0, 3.0])
>>> n = 3
>>> v = new_dup_vector(vold, n)
>>> print(v)
[1. 2. 3.]
Source code in spotpython/gp/matrix.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def new_dup_vector(vold, n) -> np.ndarray:
    """
    Allocates a new numpy array of size n and fills it with the contents of vold.

    Args:
        vold (ndarray): The original array to duplicate.
        n (int): The size of the new array.

    Returns:
        ndarray: The new array filled with the contents of vold.

    Examples:
        >>> vold = np.array([1.0, 2.0, 3.0])
        >>> n = 3
        >>> v = new_dup_vector(vold, n)
        >>> print(v)
        [1. 2. 3.]
    """
    v = np.empty(n)
    v = dupv(v, vold, n)
    return v

new_id_matrix(n)

Create a new n x n identity matrix.

Parameters:

Name Type Description Default
n int

The size of the identity matrix.

required

Returns:

Name Type Description
ndarray ndarray

The new n x n identity matrix.

Examples:

>>> n = 3
>>> m = new_id_matrix(n)
>>> print(m)
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Source code in spotpython/gp/matrix.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
def new_id_matrix(n) -> np.ndarray:
    """
    Create a new n x n identity matrix.

    Args:
        n (int): The size of the identity matrix.

    Returns:
        ndarray: The new n x n identity matrix.

    Examples:
        >>> n = 3
        >>> m = new_id_matrix(n)
        >>> print(m)
        [[1. 0. 0.]
         [0. 1. 0.]
         [0. 0. 1.]]
    """
    return np.eye(n)

new_matrix(n1, n2)

Create a new n1 x n2 matrix which is allocated like an n1*n2 array, but can be referenced as a 2-d array.

Parameters:

Name Type Description Default
n1 int

The number of rows in the matrix.

required
n2 int

The number of columns in the matrix.

required

Returns:

Name Type Description
ndarray ndarray

The new n1 x n2 matrix.

Examples:

>>> n1 = 3
>>> n2 = 2
>>> m = new_matrix(n1, n2)
>>> print(m)
[[0. 0.]
 [0. 0.]
 [0. 0.]]
Source code in spotpython/gp/matrix.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def new_matrix(n1, n2) -> np.ndarray:
    """
    Create a new n1 x n2 matrix which is allocated like an n1*n2 array,
    but can be referenced as a 2-d array.

    Args:
        n1 (int): The number of rows in the matrix.
        n2 (int): The number of columns in the matrix.

    Returns:
        ndarray: The new n1 x n2 matrix.

    Examples:
        >>> n1 = 3
        >>> n2 = 2
        >>> m = new_matrix(n1, n2)
        >>> print(m)
        [[0. 0.]
         [0. 0.]
         [0. 0.]]
    """
    if n1 == 0 or n2 == 0:
        return None

    m = np.zeros((n1, n2))
    return m

new_matrix_bones(v, n1, n2)

Create a 2D numpy array (matrix) from a 1D numpy array (vector). The resulting matrix shares the same memory as the input vector.

Parameters:

Name Type Description Default
v ndarray

The input 1D numpy array (vector).

required
n1 int

The number of rows in the resulting matrix.

required
n2 int

The number of columns in the resulting matrix.

required

Returns:

Name Type Description
ndarray ndarray

The resulting 2D numpy array (matrix).

Examples:

>>> v = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
>>> n1 = 2
>>> n2 = 3
>>> M = new_matrix_bones(v, n1, n2)
>>> print(M)
[[1. 2. 3.]
 [4. 5. 6.]]
Source code in spotpython/gp/matrix.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def new_matrix_bones(v, n1, n2) -> np.ndarray:
    """
    Create a 2D numpy array (matrix) from a 1D numpy array (vector).
    The resulting matrix shares the same memory as the input vector.

    Args:
        v (ndarray): The input 1D numpy array (vector).
        n1 (int): The number of rows in the resulting matrix.
        n2 (int): The number of columns in the resulting matrix.

    Returns:
        ndarray: The resulting 2D numpy array (matrix).

    Examples:
        >>> v = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        >>> n1 = 2
        >>> n2 = 3
        >>> M = new_matrix_bones(v, n1, n2)
        >>> print(M)
        [[1. 2. 3.]
         [4. 5. 6.]]
    """
    M = np.empty((n1, n2), dtype=v.dtype)
    M[0] = v[:n2]
    for i in range(1, n1):
        M[i] = v[i * n2 : (i + 1) * n2]
    return M

new_p_submatrix_rows(p, v, nrows, ncols, row_offset)

Create a new matrix from the rows of v, specified by p. Must have ncol(v) == ncol(V) and nrow(V) >= nrows and nrow(v) >= max(p).

Parameters:

Name Type Description Default
p ndarray

The array of row indices to copy.

required
v ndarray

The source matrix.

required
nrows int

The number of rows in the new matrix.

required
ncols int

The number of columns in the new matrix.

required
row_offset int

The row offset in the new matrix.

required

Returns:

Name Type Description
ndarray ndarray

The new matrix with specified rows copied from v.

Examples:

>>> p = np.array([0, 2])
>>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> nrows = 2
>>> ncols = 3
>>> row_offset = 1
>>> V = new_p_submatrix_rows(p, v, nrows, ncols, row_offset)
>>> print(V)
[[0. 0. 0.]
 [1. 2. 3.]
 [0. 0. 0.]
 [7. 8. 9.]
 [0. 0. 0.]]
Source code in spotpython/gp/matrix.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def new_p_submatrix_rows(p, v, nrows, ncols, row_offset) -> np.ndarray:
    """
    Create a new matrix from the rows of v, specified by p.
    Must have ncol(v) == ncol(V) and nrow(V) >= nrows and nrow(v) >= max(p).

    Args:
        p (ndarray): The array of row indices to copy.
        v (ndarray): The source matrix.
        nrows (int): The number of rows in the new matrix.
        ncols (int): The number of columns in the new matrix.
        row_offset (int): The row offset in the new matrix.

    Returns:
        ndarray: The new matrix with specified rows copied from v.

    Examples:
        >>> p = np.array([0, 2])
        >>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        >>> nrows = 2
        >>> ncols = 3
        >>> row_offset = 1
        >>> V = new_p_submatrix_rows(p, v, nrows, ncols, row_offset)
        >>> print(V)
        [[0. 0. 0.]
         [1. 2. 3.]
         [0. 0. 0.]
         [7. 8. 9.]
         [0. 0. 0.]]
    """
    if nrows + row_offset == 0 or ncols == 0:
        return None

    V = np.zeros((nrows + row_offset, ncols))
    if nrows > 0:
        V = sub_p_matrix_rows(V, p, v, ncols, nrows, row_offset)
    return V

new_vector(n)

Allocates a new numpy array of size n.

Parameters:

Name Type Description Default
n int

The size of the new array.

required

Returns:

Name Type Description
ndarray ndarray

The new array of size n.

Examples:

>>> n = 3
>>> v = new_vector(n)
>>> print(v)
[0. 0. 0.]
Source code in spotpython/gp/matrix.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def new_vector(n) -> np.ndarray:
    """
    Allocates a new numpy array of size n.

    Args:
        n (int): The size of the new array.

    Returns:
        ndarray: The new array of size n.

    Examples:
        >>> n = 3
        >>> v = new_vector(n)
        >>> print(v)
        [0. 0. 0.]
    """
    if n == 0:
        return None
    v = np.empty(n)
    return v

sub_p_matrix(V, p, v, nrows, lenp, col_offset)

Copy the columns v[1:n1][p[n2]] to V. Must have nrow(v) == nrow(V) and ncol(V) >= lenp and ncol(v) >= max(p).

Parameters:

Name Type Description Default
V ndarray

The destination matrix.

required
p ndarray

The array of column indices to copy.

required
v ndarray

The source matrix.

required
nrows int

The number of rows in the matrices.

required
lenp int

The length of the array p.

required
col_offset int

The column offset in the destination matrix.

required

Returns:

Name Type Description
ndarray ndarray

The updated destination matrix V.

Examples:

>>> V = np.zeros((3, 5))
>>> p = np.array([0, 2])
>>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> nrows = 3
>>> lenp = 2
>>> col_offset = 1
>>> V = sub_p_matrix(V, p, v, nrows, lenp, col_offset)
>>> print(V)
[[0. 1. 3. 0. 0.]
 [0. 4. 6. 0. 0.]
 [0. 7. 9. 0. 0.]]
Source code in spotpython/gp/matrix.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def sub_p_matrix(V, p, v, nrows, lenp, col_offset) -> np.ndarray:
    """
    Copy the columns `v[1:n1][p[n2]]` to V.
    Must have nrow(v) == nrow(V) and ncol(V) >= lenp and ncol(v) >= max(p).

    Args:
        V (ndarray): The destination matrix.
        p (ndarray): The array of column indices to copy.
        v (ndarray): The source matrix.
        nrows (int): The number of rows in the matrices.
        lenp (int): The length of the array p.
        col_offset (int): The column offset in the destination matrix.

    Returns:
        ndarray: The updated destination matrix V.

    Examples:
        >>> V = np.zeros((3, 5))
        >>> p = np.array([0, 2])
        >>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        >>> nrows = 3
        >>> lenp = 2
        >>> col_offset = 1
        >>> V = sub_p_matrix(V, p, v, nrows, lenp, col_offset)
        >>> print(V)
        [[0. 1. 3. 0. 0.]
         [0. 4. 6. 0. 0.]
         [0. 7. 9. 0. 0.]]
    """
    assert V is not None and p is not None and v is not None
    assert nrows > 0 and lenp > 0

    for i in range(nrows):
        for j in range(lenp):
            V[i, j + col_offset] = v[i, p[j]]
    return V

sub_p_matrix_rows(V, p, v, ncols, lenp, row_offset)

Copy the rows v[1:n1][p[n2]] to V. Must have ncol(v) == ncol(V) and nrow(V) >= lenp and nrow(v) >= max(p).

Parameters:

Name Type Description Default
V ndarray

The destination matrix.

required
p ndarray

The array of row indices to copy.

required
v ndarray

The source matrix.

required
ncols int

The number of columns in the matrices.

required
lenp int

The length of the array p.

required
row_offset int

The row offset in the destination matrix.

required

Returns:

Name Type Description
ndarray ndarray

The updated destination matrix V.

Examples:

>>> V = np.zeros((5, 3))
>>> p = np.array([0, 2])
>>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> ncols = 3
>>> lenp = 2
>>> row_offset = 1
>>> sub_p_matrix_rows(V, p, v, ncols, lenp, row_offset)
>>> print(V)
[[0. 0. 0.]
 [1. 2. 3.]
 [0. 0. 0.]
 [7. 8. 9.]
 [0. 0. 0.]]
Source code in spotpython/gp/matrix.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def sub_p_matrix_rows(V, p, v, ncols, lenp, row_offset) -> np.ndarray:
    """
    Copy the rows `v[1:n1][p[n2]]` to V.
    Must have ncol(v) == ncol(V) and nrow(V) >= lenp and nrow(v) >= max(p).

    Args:
        V (ndarray): The destination matrix.
        p (ndarray): The array of row indices to copy.
        v (ndarray): The source matrix.
        ncols (int): The number of columns in the matrices.
        lenp (int): The length of the array p.
        row_offset (int): The row offset in the destination matrix.

    Returns:
        ndarray: The updated destination matrix V.

    Examples:
        >>> V = np.zeros((5, 3))
        >>> p = np.array([0, 2])
        >>> v = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        >>> ncols = 3
        >>> lenp = 2
        >>> row_offset = 1
        >>> sub_p_matrix_rows(V, p, v, ncols, lenp, row_offset)
        >>> print(V)
        [[0. 0. 0.]
         [1. 2. 3.]
         [0. 0. 0.]
         [7. 8. 9.]
         [0. 0. 0.]]
    """
    assert V is not None and p is not None and v is not None
    assert ncols > 0 and lenp > 0

    for i in range(lenp):
        V[i + row_offset, :ncols] = v[p[i], :ncols]
    return V