0
# Linear Algebra Operations
1
2
Core linear algebra functionality through numpy.linalg and basic array operations. Provides matrix products, decompositions, eigenvalue problems, and solving linear systems for scientific computing applications.
3
4
## Core Array Products
5
6
### Matrix and Vector Products
7
8
Basic linear algebra operations available in the main numpy namespace.
9
10
```python { .api }
11
def dot(a, b, out=None):
12
"""
13
Dot product of two arrays.
14
15
Parameters:
16
- a, b: array_like, input arrays
17
- out: ndarray, output array
18
19
Returns:
20
ndarray: Dot product of a and b
21
"""
22
23
def vdot(a, b):
24
"""
25
Return dot product of two vectors.
26
27
Parameters:
28
- a, b: array_like, input arrays
29
30
Returns:
31
complex: Dot product (with complex conjugation)
32
"""
33
34
def inner(a, b):
35
"""
36
Inner product of two arrays.
37
38
Parameters:
39
- a, b: array_like, input arrays
40
41
Returns:
42
ndarray: Inner product
43
"""
44
45
def outer(a, b, out=None):
46
"""
47
Compute outer product of two vectors.
48
49
Parameters:
50
- a, b: array_like, input vectors
51
- out: ndarray, output array
52
53
Returns:
54
ndarray: Outer product
55
"""
56
57
def matmul(x1, x2, out=None, **kwargs):
58
"""
59
Matrix product of two arrays.
60
61
Parameters:
62
- x1, x2: array_like, input arrays
63
- out: ndarray, output array
64
- **kwargs: additional arguments
65
66
Returns:
67
ndarray: Matrix product
68
"""
69
70
def tensordot(a, b, axes=2):
71
"""
72
Compute tensor dot product along specified axes.
73
74
Parameters:
75
- a, b: array_like, input arrays
76
- axes: int or (2,) array_like, axes to sum over
77
78
Returns:
79
ndarray: Tensor dot product
80
"""
81
82
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
83
"""
84
Evaluates Einstein summation convention on operands.
85
86
Parameters:
87
- subscripts: str, subscripts for summation
88
- *operands: array_like, input arrays
89
- out: ndarray, output array
90
- dtype: data-type, output data type
91
- order: {'C', 'F', 'A', 'K'}, memory layout
92
- casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, casting rule
93
- optimize: {False, True, 'greedy', 'optimal'}, optimization strategy
94
95
Returns:
96
ndarray: Calculation based on Einstein summation
97
"""
98
99
def kron(a, b):
100
"""
101
Kronecker product of two arrays.
102
103
Parameters:
104
- a, b: array_like, input arrays
105
106
Returns:
107
ndarray: Kronecker product
108
"""
109
110
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
111
"""
112
Return cross product of two (arrays of) vectors.
113
114
Parameters:
115
- a, b: array_like, input arrays
116
- axisa, axisb, axisc: int, axes of a, b, and output
117
- axis: int, axis along which to take cross product
118
119
Returns:
120
ndarray: Cross product
121
"""
122
```
123
124
## numpy.linalg Functions
125
126
### Matrix Decompositions
127
128
Advanced matrix decomposition algorithms.
129
130
```python { .api }
131
def linalg.cholesky(a):
132
"""
133
Cholesky decomposition.
134
135
Parameters:
136
- a: array_like, Hermitian positive-definite matrix
137
138
Returns:
139
ndarray: Lower triangular Cholesky factor
140
"""
141
142
def linalg.qr(a, mode='reduced'):
143
"""
144
Compute QR decomposition of matrix.
145
146
Parameters:
147
- a: array_like, input matrix
148
- mode: {'reduced', 'complete', 'r', 'raw'}, decomposition mode
149
150
Returns:
151
ndarray or tuple: Q and R matrices
152
"""
153
154
def linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False):
155
"""
156
Singular Value Decomposition.
157
158
Parameters:
159
- a: array_like, input matrix
160
- full_matrices: bool, compute full or reduced matrices
161
- compute_uv: bool, compute U and Vh matrices
162
- hermitian: bool, assume hermitian matrix
163
164
Returns:
165
tuple: (U, s, Vh) singular value decomposition
166
"""
167
168
def linalg.svdvals(a):
169
"""
170
Compute singular values only.
171
172
Parameters:
173
- a: array_like, input matrix
174
175
Returns:
176
ndarray: Singular values in descending order
177
"""
178
```
179
180
### Eigenvalues and Eigenvectors
181
182
Eigenvalue decomposition functions.
183
184
```python { .api }
185
def linalg.eig(a):
186
"""
187
Compute eigenvalues and eigenvectors.
188
189
Parameters:
190
- a: array_like, square matrix
191
192
Returns:
193
tuple: (eigenvalues, eigenvectors)
194
"""
195
196
def linalg.eigh(a, UPLO='L'):
197
"""
198
Compute eigenvalues and eigenvectors of Hermitian matrix.
199
200
Parameters:
201
- a: array_like, Hermitian matrix
202
- UPLO: {'L', 'U'}, use upper or lower triangle
203
204
Returns:
205
tuple: (eigenvalues, eigenvectors)
206
"""
207
208
def linalg.eigvals(a):
209
"""
210
Compute eigenvalues.
211
212
Parameters:
213
- a: array_like, square matrix
214
215
Returns:
216
ndarray: Eigenvalues
217
"""
218
219
def linalg.eigvalsh(a, UPLO='L'):
220
"""
221
Compute eigenvalues of Hermitian matrix.
222
223
Parameters:
224
- a: array_like, Hermitian matrix
225
- UPLO: {'L', 'U'}, use upper or lower triangle
226
227
Returns:
228
ndarray: Eigenvalues in ascending order
229
"""
230
```
231
232
### Matrix Norms
233
234
Various matrix and vector norms.
235
236
```python { .api }
237
def linalg.norm(x, ord=None, axis=None, keepdims=False):
238
"""
239
Matrix or vector norm.
240
241
Parameters:
242
- x: array_like, input array
243
- ord: {non-zero int, inf, -inf, 'fro', 'nuc'}, norm order
244
- axis: None or int or 2-tuple of ints, axes for norm calculation
245
- keepdims: bool, keep reduced dimensions
246
247
Returns:
248
ndarray or float: Norm of input
249
"""
250
251
def linalg.matrix_norm(x, ord='fro', axis=(-2, -1), keepdims=False):
252
"""
253
Matrix norm.
254
255
Parameters:
256
- x: array_like, input matrix
257
- ord: {1, -1, 2, -2, inf, -inf, 'fro', 'nuc'}, norm order
258
- axis: 2-tuple of ints, axes for matrix
259
- keepdims: bool, keep reduced dimensions
260
261
Returns:
262
ndarray or float: Matrix norm
263
"""
264
265
def linalg.vector_norm(x, ord=2, axis=None, keepdims=False):
266
"""
267
Vector norm.
268
269
Parameters:
270
- x: array_like, input vector
271
- ord: {non-zero int, inf, -inf}, norm order
272
- axis: None or int, axis for norm calculation
273
- keepdims: bool, keep reduced dimensions
274
275
Returns:
276
ndarray or float: Vector norm
277
"""
278
```
279
280
### Linear System Solvers
281
282
Solving linear equations and matrix inversion.
283
284
```python { .api }
285
def linalg.solve(a, b):
286
"""
287
Solve linear system ax = b for x.
288
289
Parameters:
290
- a: array_like, coefficient matrix
291
- b: array_like, ordinate values
292
293
Returns:
294
ndarray: Solution to system ax = b
295
"""
296
297
def linalg.lstsq(a, b, rcond=None):
298
"""
299
Least-squares solution to linear system.
300
301
Parameters:
302
- a: array_like, coefficient matrix
303
- b: array_like, ordinate values
304
- rcond: float, cutoff for small singular values
305
306
Returns:
307
tuple: (solution, residuals, rank, singular_values)
308
"""
309
310
def linalg.inv(a):
311
"""
312
Compute multiplicative inverse of matrix.
313
314
Parameters:
315
- a: array_like, square matrix to invert
316
317
Returns:
318
ndarray: Multiplicative inverse of a
319
"""
320
321
def linalg.pinv(a, rcond=1e-15, hermitian=False):
322
"""
323
Compute Moore-Penrose pseudoinverse.
324
325
Parameters:
326
- a: array_like, matrix to pseudoinvert
327
- rcond: float, cutoff for small singular values
328
- hermitian: bool, assume hermitian matrix
329
330
Returns:
331
ndarray: Moore-Penrose pseudoinverse
332
"""
333
334
def linalg.tensorsolve(a, b, axes=None):
335
"""
336
Solve tensor equation ax = b for x.
337
338
Parameters:
339
- a: array_like, coefficient tensor
340
- b: array_like, right-hand side tensor
341
- axes: tuple of ints, axes in a to reorder
342
343
Returns:
344
ndarray: Solution tensor
345
"""
346
347
def linalg.tensorinv(a, ind=2):
348
"""
349
Compute inverse of N-dimensional array.
350
351
Parameters:
352
- a: array_like, tensor to invert
353
- ind: int, number of first indices forming square matrix
354
355
Returns:
356
ndarray: Inverse of input tensor
357
"""
358
```
359
360
### Matrix Properties
361
362
Calculate properties and characteristics of matrices.
363
364
```python { .api }
365
def linalg.det(a):
366
"""
367
Compute determinant of array.
368
369
Parameters:
370
- a: array_like, square matrix
371
372
Returns:
373
ndarray or float: Determinant of a
374
"""
375
376
def linalg.slogdet(a):
377
"""
378
Compute sign and natural logarithm of determinant.
379
380
Parameters:
381
- a: array_like, square matrix
382
383
Returns:
384
tuple: (sign, logdet) sign and log of absolute determinant
385
"""
386
387
def linalg.matrix_rank(a, tol=None, hermitian=False):
388
"""
389
Return matrix rank using SVD method.
390
391
Parameters:
392
- a: array_like, input matrix
393
- tol: float, threshold for singular values
394
- hermitian: bool, assume hermitian matrix
395
396
Returns:
397
ndarray or int: Rank of matrix
398
"""
399
400
def linalg.cond(x, p=None):
401
"""
402
Compute condition number of matrix.
403
404
Parameters:
405
- x: array_like, input matrix
406
- p: {None, 1, -1, 2, -2, inf, -inf, 'fro'}, norm order
407
408
Returns:
409
ndarray or float: Condition number
410
"""
411
412
def linalg.matrix_power(a, n):
413
"""
414
Raise square matrix to integer power.
415
416
Parameters:
417
- a: array_like, square matrix
418
- n: int, exponent
419
420
Returns:
421
ndarray: Matrix raised to power n
422
"""
423
424
def linalg.multi_dot(arrays, out=None):
425
"""
426
Compute dot product of two or more arrays in single function call.
427
428
Parameters:
429
- arrays: sequence of array_like, matrices to multiply
430
- out: ndarray, output array
431
432
Returns:
433
ndarray: Dot product of all input arrays
434
"""
435
```
436
437
### Matrix Utilities
438
439
Additional matrix manipulation functions from linalg.
440
441
```python { .api }
442
def linalg.trace(x, offset=0, axis1=0, axis2=1, dtype=None, out=None):
443
"""
444
Return sum along diagonals of array.
445
446
Parameters:
447
- x: array_like, input array
448
- offset: int, offset from main diagonal
449
- axis1, axis2: int, axes to trace over
450
- dtype: data-type, output data type
451
- out: ndarray, output array
452
453
Returns:
454
ndarray or scalar: Sum along diagonal
455
"""
456
457
def linalg.diagonal(x, offset=0, axis1=0, axis2=1):
458
"""
459
Return specified diagonals.
460
461
Parameters:
462
- x: array_like, input array
463
- offset: int, offset from main diagonal
464
- axis1, axis2: int, axes for diagonal extraction
465
466
Returns:
467
ndarray: Diagonal elements
468
"""
469
470
def linalg.matrix_transpose(x):
471
"""
472
Transpose last two dimensions of array.
473
474
Parameters:
475
- x: array_like, input array
476
477
Returns:
478
ndarray: Array with last two dimensions transposed
479
"""
480
```
481
482
### Exception
483
484
```python { .api }
485
class linalg.LinAlgError(Exception):
486
"""
487
Generic Python-exception-derived object raised by linalg functions.
488
"""
489
```
490
491
## Usage Examples
492
493
### Basic Matrix Operations
494
495
```python
496
import numpy as np
497
498
# Matrix multiplication
499
A = np.array([[1, 2], [3, 4]])
500
B = np.array([[5, 6], [7, 8]])
501
502
# Different ways to multiply matrices
503
dot_product = np.dot(A, B) # [[19, 22], [43, 50]]
504
matmul_product = np.matmul(A, B) # [[19, 22], [43, 50]]
505
at_product = A @ B # [[19, 22], [43, 50]]
506
507
# Vector operations
508
v1 = np.array([1, 2, 3])
509
v2 = np.array([4, 5, 6])
510
dot_prod = np.dot(v1, v2) # 32
511
outer_prod = np.outer(v1, v2) # [[4, 5, 6], [8, 10, 12], [12, 15, 18]]
512
```
513
514
### Linear System Solving
515
516
```python
517
import numpy as np
518
519
# Solve linear system Ax = b
520
A = np.array([[3, 1], [1, 2]])
521
b = np.array([9, 8])
522
523
# Solve for x
524
x = np.linalg.solve(A, b) # [2.0, 3.0]
525
526
# Matrix inversion
527
A_inv = np.linalg.inv(A) # Inverse of A
528
identity = A @ A_inv # Should be identity matrix
529
530
# Least squares for overdetermined systems
531
A_over = np.array([[1, 1], [1, 2], [1, 3]])
532
b_over = np.array([6, 8, 10])
533
x_lstsq, residuals, rank, s = np.linalg.lstsq(A_over, b_over, rcond=None)
534
```
535
536
### Matrix Decompositions
537
538
```python
539
import numpy as np
540
541
# Sample matrix
542
A = np.array([[4, 2], [2, 3]], dtype=float)
543
544
# Eigenvalue decomposition
545
eigenvals, eigenvecs = np.linalg.eig(A)
546
print(f'Eigenvalues: {eigenvals}')
547
print(f'Eigenvectors:\n{eigenvecs}')
548
549
# SVD decomposition
550
U, s, Vh = np.linalg.svd(A)
551
reconstructed = U @ np.diag(s) @ Vh
552
553
# QR decomposition
554
Q, R = np.linalg.qr(A)
555
reconstructed_qr = Q @ R
556
```
557
558
### Matrix Properties
559
560
```python
561
import numpy as np
562
563
A = np.array([[1, 2], [3, 4]])
564
565
# Matrix properties
566
det_A = np.linalg.det(A) # -2.0
567
rank_A = np.linalg.matrix_rank(A) # 2
568
cond_A = np.linalg.cond(A) # 14.93 (condition number)
569
570
# Matrix norms
571
frob_norm = np.linalg.norm(A, 'fro') # Frobenius norm
572
spectral_norm = np.linalg.norm(A, 2) # Spectral norm (largest singular value)
573
```