-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForwardSub.py
58 lines (42 loc) · 1.02 KB
/
ForwardSub.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
#########################
# ForwardSub.py
#
# Implements forward substitution
# for solving a lower triangular
# system of linear equations
#
# Taylor J. Smith - CSCI 554 (Winter 2022)
#########################
import math
import numpy as np
from scipy import linalg as splg
import sys
import time
k = int(input("Dimension k? "))
A = np.random.randint(1, 10, size=(k,k))
A = np.tril(A)
print("A =\n", A)
b = np.random.randint(1, 10, size=k)
x = b.astype(float)
print("b =\n", b)
#########################
# Our implementation
#########################
start1 = time.time()
for i in range(0, k):
for j in range(0, i):
x[i] = x[i] - A[i][j]*x[j]
if A[i][i] == 0:
sys.exit("Error!")
x[i] = x[i] / A[i][i]
end1 = time.time()
print(" x =\n", x)
print(" Time =", end1 - start1)
#########################
# "Good" implementation
#########################
start2 = time.time()
x = splg.solve_triangular(A, b, lower=True)
end2 = time.time()
print("\"Good\" x =\n", x)
print(" Time =", end2 - start2)