-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
42 lines (31 loc) · 870 Bytes
/
Copy pathutils.py
File metadata and controls
42 lines (31 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def normsq(x):
return (x * x).sum()
def mnls(A, AT, b, x0, num_steps=10, tol=1e-14):
"""
Returns the solution to ``argmin_x || Ax - b ||_2`` closest to ``x_0``.
By default, ``x_0`` is zero and therefore returns the minimum norm
solution.
Parameters
----------
A : LinearOperator, shape (m, n)?
b : vector
x0 : vector
References
----------
Kammerer and Nashed 'On the convergence...'
"""
r = p = AT((A(x0) - b))
if normsq(r) < tol:
return x0
alpha = normsq(r) / normsq(A(p))
x = x0 - alpha * p
# main loop
for step in range(num_steps):
r = AT(A(x) - b)
if normsq(r) < tol:
return x
beta = - (r * AT(A(p))).sum() / normsq(A(p))
p = r + beta * p
alpha = (r * p).sum() / normsq(A(p))
x = x + alpha * p
return x