-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathImplicitEu.py
30 lines (24 loc) · 1.03 KB
/
ImplicitEu.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 17 22:33:27 2017
@author: Quintus
"""
import numpy as np
import scipy.linalg as linalg
from ExplicitEu import ExplicitEu
class ImplicitEu(ExplicitEu):
def _setup_coefficients_(self):
self.alpha = 0.5*self.dt * (self.r * self.iValues - self.sigma**2 * self.iValues**2)
self.beta = self.dt * (self.r + self.sigma**2 * self.iValues**2)
self.gamma = -0.5*self.dt * (self.r * self.iValues + self.sigma**2 * self.iValues**2)
self.coeffs = np.diag(self.alpha[1:], -1) + \
np.diag(1 + self.beta) + \
np.diag(self.gamma[:-1], 1)
def _traverse_grid_(self):
P, L, U = linalg.lu(self.coeffs)
for j in reversed(self.jValues):
Ux = linalg.solve(L, self.grid[1:-1, j+1])
self.grid[1:-1, j] = linalg.solve(U, Ux)
self.grid[0, j] = 2 * self.grid[1, j] - self.grid[2, j]
self.grid[-1, j] = 2 * self.grid[-2, j] - self.grid[-3, j]