0
# Linear Algebra
1
2
GPU-accelerated linear algebra operations leveraging optimized CUDA libraries (cuBLAS, cuSOLVER) for matrix operations, decompositions, and solving linear systems. Provides comprehensive functionality for numerical linear algebra computations.
3
4
## Capabilities
5
6
### Matrix Products
7
8
High-performance matrix multiplication and related 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: ndarray, optional output array
19
20
Returns:
21
cupy.ndarray: Dot product of a and b
22
"""
23
24
def matmul(x1, x2, out=None):
25
"""Matrix product of two arrays."""
26
27
def inner(a, b):
28
"""Inner product of two arrays."""
29
30
def outer(a, b, out=None):
31
"""Compute outer product of two vectors."""
32
33
def tensordot(a, b, axes=2):
34
"""Compute tensor dot product along specified axes."""
35
36
def kron(a, b):
37
"""Kronecker product of two arrays."""
38
39
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
40
"""Return cross product of two (arrays) vectors."""
41
42
def vdot(a, b):
43
"""Return dot product of two vectors."""
44
```
45
46
### Matrix Decompositions
47
48
Advanced matrix factorization methods for numerical analysis.
49
50
```python { .api }
51
def svd(a, full_matrices=True, compute_uv=True):
52
"""
53
Singular Value Decomposition.
54
55
Parameters:
56
- a: array_like, input matrix to decompose
57
- full_matrices: bool, compute full U and Vh matrices
58
- compute_uv: bool, compute U and Vh in addition to s
59
60
Returns:
61
U, s, Vh: ndarrays, SVD factorization matrices
62
"""
63
64
def qr(a, mode='reduced'):
65
"""Compute QR factorization of matrix."""
66
67
def cholesky(a):
68
"""Compute Cholesky decomposition of positive-definite matrix."""
69
70
def eig(a):
71
"""Compute eigenvalues and eigenvectors of square array."""
72
73
def eigh(a, UPLO='L'):
74
"""Compute eigenvalues and eigenvectors of Hermitian matrix."""
75
76
def eigvals(a):
77
"""Compute eigenvalues of general matrix."""
78
79
def eigvalsh(a, UPLO='L'):
80
"""Compute eigenvalues of Hermitian matrix."""
81
```
82
83
### Linear Systems
84
85
Solve linear equations and matrix inversions.
86
87
```python { .api }
88
def solve(a, b):
89
"""
90
Solve linear matrix equation ax = b.
91
92
Parameters:
93
- a: array_like, coefficient matrix
94
- b: array_like, ordinate values
95
96
Returns:
97
cupy.ndarray: Solution to system ax = b
98
"""
99
100
def lstsq(a, b, rcond=None):
101
"""Return least-squares solution to linear matrix equation."""
102
103
def inv(a):
104
"""Compute multiplicative inverse of matrix."""
105
106
def pinv(a, rcond=1e-15):
107
"""Compute Moore-Penrose pseudoinverse of matrix."""
108
109
def tensorinv(a, ind=2):
110
"""Compute tensor multiplicative inverse."""
111
112
def tensorsolve(a, b, axes=None):
113
"""Solve tensor equation a x = b for x."""
114
```
115
116
### Matrix Properties
117
118
Compute various matrix properties and norms.
119
120
```python { .api }
121
def norm(x, ord=None, axis=None, keepdims=False):
122
"""
123
Matrix or vector norm.
124
125
Parameters:
126
- x: array_like, input array
127
- ord: {non-zero int, inf, -inf, 'fro', 'nuc'}, order of norm
128
- axis: int/tuple, axis along which to compute norm
129
- keepdims: bool, keep dimensions of input
130
131
Returns:
132
cupy.ndarray: Norm of matrix or vector
133
"""
134
135
def det(a):
136
"""Compute determinant of array."""
137
138
def slogdet(a):
139
"""Compute sign and logarithm of determinant."""
140
141
def trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None):
142
"""Return sum along diagonals of array."""
143
144
def matrix_rank(M, tol=None, hermitian=False):
145
"""Return matrix rank using SVD method."""
146
147
def cond(x, p=None):
148
"""Compute condition number of matrix."""
149
```
150
151
### Einstein Summation
152
153
Efficient tensor operations using Einstein summation notation.
154
155
```python { .api }
156
def einsum(subscripts, *operands, out=None, dtype=None, order='K', casting='safe', optimize=False):
157
"""
158
Evaluates Einstein summation convention on operands.
159
160
Parameters:
161
- subscripts: str, specifies subscripts for summation
162
- operands: list of array_like, arrays for operation
163
- out: ndarray, optional output array
164
- optimize: {False, True, 'greedy', 'optimal'}, optimization strategy
165
166
Returns:
167
cupy.ndarray: Calculation based on Einstein summation convention
168
"""
169
170
def einsum_path(subscripts, *operands, optimize='greedy'):
171
"""Evaluates lowest cost contraction order for einsum expression."""
172
```
173
174
## Usage Examples
175
176
### Basic Matrix Operations
177
178
```python
179
import cupy as cp
180
181
# Create matrices
182
A = cp.random.random((1000, 1000))
183
B = cp.random.random((1000, 500))
184
x = cp.random.random(1000)
185
186
# Matrix multiplication
187
C = cp.dot(A, A.T) # A @ A.T
188
D = cp.matmul(A, B) # Matrix multiplication
189
190
# Vector operations
191
inner_prod = cp.inner(x, x)
192
outer_prod = cp.outer(x, x)
193
```
194
195
### Solving Linear Systems
196
197
```python
198
# Solve linear system Ax = b
199
A = cp.random.random((100, 100))
200
b = cp.random.random(100)
201
202
# Direct solution
203
x = cp.linalg.solve(A, b)
204
205
# Verify solution
206
residual = cp.linalg.norm(cp.dot(A, x) - b)
207
208
# Least squares for overdetermined system
209
A_over = cp.random.random((200, 100))
210
b_over = cp.random.random(200)
211
x_lstsq, residuals, rank, s = cp.linalg.lstsq(A_over, b_over)
212
```
213
214
### Matrix Decompositions
215
216
```python
217
# Singular Value Decomposition
218
A = cp.random.random((100, 50))
219
U, s, Vh = cp.linalg.svd(A, full_matrices=False)
220
221
# Reconstruct matrix
222
A_reconstructed = cp.dot(U * s, Vh)
223
224
# Eigendecomposition
225
symmetric_matrix = cp.random.random((100, 100))
226
symmetric_matrix = (symmetric_matrix + symmetric_matrix.T) / 2
227
228
eigenvals, eigenvecs = cp.linalg.eigh(symmetric_matrix)
229
230
# QR decomposition
231
Q, R = cp.linalg.qr(A)
232
```
233
234
### Advanced Linear Algebra
235
236
```python
237
# Matrix norms and condition numbers
238
A = cp.random.random((100, 100))
239
240
frobenius_norm = cp.linalg.norm(A, 'fro')
241
spectral_norm = cp.linalg.norm(A, 2)
242
condition_number = cp.linalg.cond(A)
243
244
# Determinant and trace
245
det_A = cp.linalg.det(A)
246
trace_A = cp.trace(A)
247
248
# Matrix inverse and pseudoinverse
249
A_inv = cp.linalg.inv(A)
250
A_pinv = cp.linalg.pinv(A)
251
252
# Verify inverse
253
identity_check = cp.allclose(cp.dot(A, A_inv), cp.eye(100))
254
```
255
256
### Einstein Summation Examples
257
258
```python
259
# Matrix multiplication using einsum
260
A = cp.random.random((10, 15))
261
B = cp.random.random((15, 20))
262
C = cp.einsum('ij,jk->ik', A, B) # Equivalent to cp.dot(A, B)
263
264
# Batch matrix multiplication
265
batch_A = cp.random.random((5, 10, 15))
266
batch_B = cp.random.random((5, 15, 20))
267
batch_C = cp.einsum('bij,bjk->bik', batch_A, batch_B)
268
269
# Trace of product
270
A = cp.random.random((100, 100))
271
B = cp.random.random((100, 100))
272
trace_AB = cp.einsum('ij,ji->', A, B) # Trace of A @ B
273
274
# Complex tensor operations
275
tensor = cp.random.random((5, 10, 15, 20))
276
result = cp.einsum('ijkl,jl->ik', tensor, cp.random.random((10, 20)))
277
```