0
# Linear Algebra
1
2
GPU-accelerated linear algebra operations powered by cuBLAS and cuSOLVER libraries. These functions provide high-performance matrix operations, decompositions, eigenvalue problems, and linear system solving with NumPy-compatible interfaces.
3
4
## Capabilities
5
6
### Matrix and Vector Products
7
8
Core linear algebra operations for matrix multiplication, dot products, and tensor operations.
9
10
```python { .api }
11
def dot(a, b, out=None):
12
"""
13
Dot product of two arrays.
14
15
Parameters:
16
- a: array-like, first input array
17
- b: array-like, second input array
18
- out: array, output array, optional
19
20
Returns:
21
cupy.ndarray: Dot product on GPU
22
"""
23
24
def vdot(a, b):
25
"""
26
Vector dot product.
27
28
Parameters:
29
- a: array-like, first input vector
30
- b: array-like, second input vector
31
32
Returns:
33
scalar: Dot product of flattened arrays on GPU
34
"""
35
36
def inner(a, b):
37
"""
38
Inner product of vectors.
39
40
Parameters:
41
- a: array-like, first input array
42
- b: array-like, second input array
43
44
Returns:
45
cupy.ndarray: Inner product on GPU
46
"""
47
48
def outer(a, b, out=None):
49
"""
50
Outer product of vectors.
51
52
Parameters:
53
- a: array-like, first input vector
54
- b: array-like, second input vector
55
- out: array, output array, optional
56
57
Returns:
58
cupy.ndarray: Outer product matrix on GPU
59
"""
60
61
def matmul(x1, x2, out=None, **kwargs):
62
"""
63
Matrix multiplication.
64
65
Parameters:
66
- x1: array-like, first input array
67
- x2: array-like, second input array
68
- out: array, output array, optional
69
70
Returns:
71
cupy.ndarray: Matrix product on GPU
72
"""
73
74
def tensordot(a, b, axes=2):
75
"""
76
Tensor dot product along specified axes.
77
78
Parameters:
79
- a: array-like, first input array
80
- b: array-like, second input array
81
- axes: int or tuple, axes to sum over
82
83
Returns:
84
cupy.ndarray: Tensor dot product on GPU
85
"""
86
87
def kron(a, b):
88
"""
89
Kronecker product of two arrays.
90
91
Parameters:
92
- a: array-like, first input array
93
- b: array-like, second input array
94
95
Returns:
96
cupy.ndarray: Kronecker product on GPU
97
"""
98
99
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
100
"""
101
Cross product of vectors.
102
103
Parameters:
104
- a: array-like, first input array
105
- b: array-like, second input array
106
- axisa: int, axis of first array
107
- axisb: int, axis of second array
108
- axisc: int, axis of output
109
- axis: int, axis for cross product
110
111
Returns:
112
cupy.ndarray: Cross product on GPU
113
"""
114
115
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
116
"""
117
Einstein summation convention.
118
119
Parameters:
120
- subscripts: str, Einstein summation subscripts
121
- operands: arrays, input arrays
122
- out: array, output array, optional
123
- dtype: data type, optional
124
- order: memory layout, optional
125
- casting: casting rule, optional
126
- optimize: optimization strategy, optional
127
128
Returns:
129
cupy.ndarray: Einstein sum result on GPU
130
"""
131
```
132
133
### Matrix Decompositions
134
135
Advanced matrix decomposition methods for numerical analysis and solving linear systems.
136
137
```python { .api }
138
def cholesky(a):
139
"""
140
Cholesky decomposition of positive definite matrix.
141
142
Parameters:
143
- a: array-like, positive definite matrix
144
145
Returns:
146
cupy.ndarray: Lower triangular Cholesky factor on GPU
147
"""
148
149
def qr(a, mode='reduced'):
150
"""
151
QR factorization.
152
153
Parameters:
154
- a: array-like, input matrix
155
- mode: str, decomposition mode ('reduced', 'complete', 'r', 'raw')
156
157
Returns:
158
tuple: Q and R matrices on GPU
159
"""
160
161
def svd(a, full_matrices=True, compute_uv=True, hermitian=False):
162
"""
163
Singular Value Decomposition.
164
165
Parameters:
166
- a: array-like, input matrix
167
- full_matrices: bool, compute full-sized U and Vh
168
- compute_uv: bool, compute U and Vh matrices
169
- hermitian: bool, assume Hermitian matrix
170
171
Returns:
172
tuple: U, s, Vh matrices and singular values on GPU
173
"""
174
```
175
176
### Eigenvalue Problems
177
178
Compute eigenvalues and eigenvectors for symmetric and Hermitian matrices.
179
180
```python { .api }
181
def eigh(a, UPLO='L'):
182
"""
183
Eigenvalues and eigenvectors of Hermitian matrix.
184
185
Parameters:
186
- a: array-like, Hermitian matrix
187
- UPLO: str, upper ('U') or lower ('L') triangle
188
189
Returns:
190
tuple: eigenvalues and eigenvectors on GPU
191
"""
192
193
def eigvalsh(a, UPLO='L'):
194
"""
195
Eigenvalues of Hermitian matrix.
196
197
Parameters:
198
- a: array-like, Hermitian matrix
199
- UPLO: str, upper ('U') or lower ('L') triangle
200
201
Returns:
202
cupy.ndarray: eigenvalues on GPU
203
"""
204
```
205
206
### Matrix Norms and Properties
207
208
Functions for computing matrix norms, determinants, and other matrix properties.
209
210
```python { .api }
211
def norm(x, ord=None, axis=None, keepdims=False):
212
"""
213
Matrix or vector norm.
214
215
Parameters:
216
- x: array-like, input array
217
- ord: norm order, optional
218
- axis: int or tuple, axes for norm computation
219
- keepdims: bool, keep dimensions
220
221
Returns:
222
cupy.ndarray: norm value on GPU
223
"""
224
225
def det(a):
226
"""
227
Determinant of matrix.
228
229
Parameters:
230
- a: array-like, square matrix
231
232
Returns:
233
cupy.ndarray: determinant on GPU
234
"""
235
236
def slogdet(a):
237
"""
238
Sign and logarithm of determinant.
239
240
Parameters:
241
- a: array-like, square matrix
242
243
Returns:
244
tuple: sign and log determinant on GPU
245
"""
246
247
def matrix_rank(M, tol=None, hermitian=False):
248
"""
249
Matrix rank using SVD.
250
251
Parameters:
252
- M: array-like, input matrix
253
- tol: float, tolerance for rank determination
254
- hermitian: bool, assume Hermitian matrix
255
256
Returns:
257
int: matrix rank
258
"""
259
260
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
261
"""
262
Sum of diagonal elements.
263
264
Parameters:
265
- a: array-like, input array
266
- offset: int, diagonal offset
267
- axis1: int, first axis
268
- axis2: int, second axis
269
- dtype: data type, optional
270
- out: array, output array, optional
271
272
Returns:
273
cupy.ndarray: trace on GPU
274
"""
275
```
276
277
### Solving Linear Systems
278
279
Functions for solving linear equations and matrix inversion.
280
281
```python { .api }
282
def solve(a, b):
283
"""
284
Solve linear system ax = b.
285
286
Parameters:
287
- a: array-like, coefficient matrix
288
- b: array-like, dependent variable values
289
290
Returns:
291
cupy.ndarray: solution x on GPU
292
"""
293
294
def tensorsolve(a, b, axes=None):
295
"""
296
Solve tensor equation ax = b for x.
297
298
Parameters:
299
- a: array-like, coefficient tensor
300
- b: array-like, dependent variable tensor
301
- axes: tuple, axes to sum over
302
303
Returns:
304
cupy.ndarray: solution tensor on GPU
305
"""
306
307
def lstsq(a, b, rcond=None):
308
"""
309
Least squares solution to linear system.
310
311
Parameters:
312
- a: array-like, coefficient matrix
313
- b: array-like, dependent variable values
314
- rcond: float, cutoff for small singular values
315
316
Returns:
317
tuple: solution, residuals, rank, singular values on GPU
318
"""
319
320
def inv(a):
321
"""
322
Matrix inverse.
323
324
Parameters:
325
- a: array-like, square matrix
326
327
Returns:
328
cupy.ndarray: matrix inverse on GPU
329
"""
330
331
def pinv(a, rcond=1e-15, hermitian=False):
332
"""
333
Moore-Penrose pseudoinverse.
334
335
Parameters:
336
- a: array-like, input matrix
337
- rcond: float, cutoff for small singular values
338
- hermitian: bool, assume Hermitian matrix
339
340
Returns:
341
cupy.ndarray: pseudoinverse on GPU
342
"""
343
344
def tensorinv(a, ind=2):
345
"""
346
Tensor inverse.
347
348
Parameters:
349
- a: array-like, input tensor
350
- ind: int, number of indices
351
352
Returns:
353
cupy.ndarray: tensor inverse on GPU
354
"""
355
```
356
357
### Advanced Linear Algebra Operations
358
359
Specialized linear algebra functions for advanced mathematical computations.
360
361
```python { .api }
362
def matrix_power(a, n):
363
"""
364
Raise matrix to integer power.
365
366
Parameters:
367
- a: array-like, square matrix
368
- n: int, power exponent
369
370
Returns:
371
cupy.ndarray: matrix power on GPU
372
"""
373
```
374
375
## Usage Examples
376
377
### Basic Matrix Operations
378
379
```python
380
import cupy as cp
381
382
# Create matrices
383
A = cp.random.random((3, 3)).astype(cp.float32)
384
B = cp.random.random((3, 3)).astype(cp.float32)
385
x = cp.random.random(3).astype(cp.float32)
386
387
# Matrix multiplication
388
C = cp.dot(A, B)
389
C_matmul = A @ B # Equivalent using @ operator
390
391
# Vector operations
392
dot_product = cp.dot(x, x)
393
outer_product = cp.outer(x, x)
394
395
# Matrix-vector multiplication
396
y = cp.dot(A, x)
397
```
398
399
### Linear System Solving
400
401
```python
402
# Solve linear system Ax = b
403
A = cp.array([[3, 2, -1], [2, -2, 4], [-1, 0.5, -1]], dtype=cp.float32)
404
b = cp.array([1, -2, 0], dtype=cp.float32)
405
406
# Direct solution
407
x = cp.linalg.solve(A, b)
408
409
# Least squares solution for overdetermined system
410
A_over = cp.random.random((5, 3)).astype(cp.float32)
411
b_over = cp.random.random(5).astype(cp.float32)
412
x_lstsq, residuals, rank, s = cp.linalg.lstsq(A_over, b_over)
413
414
# Matrix inversion
415
A_inv = cp.linalg.inv(A)
416
identity_check = cp.dot(A, A_inv) # Should be close to identity
417
```
418
419
### Matrix Decompositions
420
421
```python
422
# Create symmetric positive definite matrix
423
A_spd = cp.random.random((4, 4)).astype(cp.float32)
424
A_spd = cp.dot(A_spd, A_spd.T) + cp.eye(4) * 0.1
425
426
# Cholesky decomposition
427
L = cp.linalg.cholesky(A_spd)
428
429
# QR decomposition
430
A_rect = cp.random.random((5, 3)).astype(cp.float32)
431
Q, R = cp.linalg.qr(A_rect)
432
433
# SVD decomposition
434
U, s, Vh = cp.linalg.svd(A_rect)
435
436
# Eigenvalue decomposition for symmetric matrix
437
eigenvals, eigenvecs = cp.linalg.eigh(A_spd)
438
```
439
440
### Advanced Operations
441
442
```python
443
# Einstein summation for complex tensor operations
444
A = cp.random.random((3, 4, 5))
445
B = cp.random.random((4, 5, 6))
446
447
# Matrix multiplication using einsum
448
C = cp.einsum('ijk,jkl->il', A, B)
449
450
# Batch matrix multiplication
451
batch_A = cp.random.random((10, 3, 3))
452
batch_B = cp.random.random((10, 3, 3))
453
batch_C = cp.einsum('bij,bjk->bik', batch_A, batch_B)
454
455
# Matrix properties
456
det_A = cp.linalg.det(A_spd)
457
norm_A = cp.linalg.norm(A_spd, ord='fro') # Frobenius norm
458
trace_A = cp.trace(A_spd)
459
```