Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update linalg.py #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class SciPySolver(Solver):

Attributes:
scipy (importlib.type.ModuleType): SciPy library
numpy (importlib.type.ModuleType): NumPy library
"""

def __init__(self, *args, **kwargs):
Expand All @@ -117,6 +118,7 @@ def __init__(self, *args, **kwargs):
"""
super().__init__(*args, **kwargs)
self.scipy = importlib.import_module('scipy')
self.numpy = importlib.import_module('numpy')
importlib.import_module('scipy.sparse.linalg')

def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
Expand All @@ -133,21 +135,20 @@ def solve(self, A: Dict[Tuple[int], float], b: List[List[float]]):
x = None
n = len(b)

# Attempt to solve the linear system with SciPy libary.
# Attempt to solve the linear system with SciPy library.
try:
A_scipy = self.scipy.sparse.dok_matrix((n, n), dtype = 'd')
A_scipy._update(A)
A_scipy = self.scipy.sparse.dok_matrix((n, n), dtype='d')
for key, val in A.items():
A_scipy[key] = val
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)
b = self.numpy.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
Expand Down