-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers.py
40 lines (33 loc) · 1010 Bytes
/
solvers.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# author: A. Pezzotta -- pezzota [AT] crick.ac.uk
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import LinearConstraint
from scipy.sparse.linalg import splu, lsqr, lsmr
from scipy.sparse import coo_matrix
def linsolver_SVD (A, b):
u, s, vh = np.linalg.svd(A, full_matrices=False)
c = np.dot(u.T, b)
w = np.linalg.solve(np.diag(s), c)
z = np.dot(vh.T, w)
return z
def directSolve (pt, end, method='lsqr'):
N = len(pt)
M = pt.T - np.eye(N)
mat = np.delete(M, end, axis=1) # delete columns corresponding to target nodes
vec = - np.sum(M[:,end], axis=1) # vector implementing "boundary" conditions
# print(M,"\n")
# print(mat,"\n")
# print(vec,"\n")
Z = np.ones(N)
if method == 'svd':
Zrec = np.squeeze(linsolver_SVD(mat, vec))
elif method == 'lsqr':
mat = coo_matrix(mat)
Zrec = lsqr(mat, vec)[0]
else:
raise ValueError("directSolve: unknown method")
Z[np.delete(np.arange(N), end)] = Zrec
return Z