forked from fedackb/mesh-fairing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinalg.py
166 lines (132 loc) · 4.73 KB
/
linalg.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# ##### BEGIN GPL LICENSE BLOCK #####
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
import importlib
import logging
from typing import Dict, List, Tuple
from . import moduleutil
class Solver():
"""
Linear system solver interface
"""
def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
"""
Solves the Ax=b linear system of equations
Parameters:
A (Dict[Tuple[int], float]): Coefficient matrix A
b (List<List<float>>): Right hand side of the linear system
Returns:
numpy.ndarray: Variables matrix (x); None if unsuccessful
Raises: NotImplementedError
"""
raise NotImplementedError
class NullSolver(Solver):
"""
Linear system solver implemented as a null object
"""
def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
"""
Solves the Ax=b linear system of equations
Parameters:
A (Dict[Tuple[int], float]): Coefficient matrix A
b (List[List[float]]): Right hand side of the linear system
Returns:
None: Indication of no-op
"""
return None
class NumPySolver(Solver):
"""
Linear system solver implemented with NumPy library
Attributes:
numpy (importlib.type.ModuleType): NumPy library
"""
def __init__(self, *args, **kwargs):
"""
Initializes this linear system solver
"""
super().__init__(*args, **kwargs)
self.numpy = importlib.import_module('numpy')
def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
"""
Solves the Ax=b linear system of equations using NumPy library
Parameters:
A (Dict[Tuple[int], float]): Coefficient matrix A
b (List[List[float]]): Right hand side of the linear system
Returns:
numpy.ndarray: Variables matrix (x); None if unsuccessful
"""
x = None
n = len(b)
# Attempt to solve the linear system with NumPy library.
try:
A_numpy = self.numpy.zeros((n, n), dtype = 'd')
b = self.numpy.asarray(b, dtype = 'd')
for key, val in A.items():
A_numpy[key] = val
x = self.numpy.linalg.solve(A_numpy, b)
except Exception as e:
logging.warn(e)
return x
class SciPySolver(Solver):
"""
Linear system solver implemented with SciPy library
Attributes:
scipy (importlib.type.ModuleType): SciPy library
"""
def __init__(self, *args, **kwargs):
"""
Initializes this linear system solver
"""
super().__init__(*args, **kwargs)
self.scipy = importlib.import_module('scipy')
importlib.import_module('scipy.sparse.linalg')
def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
"""
Solves the Ax=b linear system of equations using SciPy library
Parameters:
A (Dict[Tuple[int], float]): Coefficient matrix A
b (List[List[float]]): Right hand side of the linear system
Returns:
numpy.ndarray: Variables matrix (x); None if unsuccessful
"""
x = None
n = len(b)
# Attempt to solve the linear system with SciPy libary.
try:
A_scipy = self.scipy.sparse.dok_matrix((n, n), dtype = 'd')
A_scipy._update(A)
A_scipy = A_scipy.tocsc()
b = self.scipy.array(b, dtype = 'd')
factor = self.scipy.sparse.linalg.splu(
A_scipy, diag_pivot_thresh = 0.00001)
x = factor.solve(b)
except Exception as e:
logging.warn(e)
return x
def init():
"""
Initializes this module's linear algebra solver
"""
global solver
if not moduleutil.is_installed('numpy'):
solver = NullSolver()
logging.debug('Using NullSolver')
elif moduleutil.is_installed('scipy'):
solver = SciPySolver()
logging.debug('Using SciPySolver')
else:
solver = NumPySolver()
logging.debug('Using NumPySolver')
solver = NullSolver()