forked from mrychlik/GaussianElimination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
172 lines (142 loc) · 5.61 KB
/
test.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
167
168
169
170
171
172
import unittest
import random
from helpers2 import print_matrix, hilbert_matrix, random_matrix
from gauss_solve import lu, plu
class TestLUDecomposition(unittest.TestCase):
def test_lu_3x3_python(self):
""" Test LU decomposition for a simple 3x3 matrix using Python implementation. """
A = [[2.0, 3.0, -1.0],
[4.0, 1.0, 2.0],
[-2.0, 7.0, 2.0]]
L, U = lu(A, use_c=False) # Python implementation
print("Python: LU decomposition for 3x3 matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_lu_3x3_c(self):
""" Test LU decomposition for a simple 3x3 matrix using C implementation. """
A = [[2.0, 3.0, -1.0],
[4.0, 1.0, 2.0],
[-2.0, 7.0, 2.0]]
L, U = lu(A, use_c=True) # C implementation
print("C: LU decomposition for 3x3 matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_lu_hilbert_python(self):
""" Test LU decomposition for a 20x20 Hilbert matrix using Python implementation. """
A = hilbert_matrix(20)
L, U = lu(A, use_c=False) # Python implementation
print("Python: LU decomposition for 20x20 Hilbert matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_lu_hilbert_c(self):
""" Test LU decomposition for a 20x20 Hilbert matrix using C implementation. """
A = hilbert_matrix(20)
L, U = lu(A, use_c=True) # C implementation
print("C: LU decomposition for 20x20 Hilbert matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_lu_singular_python(self):
""" Test LU decomposition for a singular matrix using Python implementation. """
A = [[1.0, 2.0, 3.0],
[0.0, 0.0, 0.0], # Singular matrix with zero row
[4.0, 5.0, 6.0]]
try:
L, U = lu(A, use_c=False) # Python implementation
print("Python: LU decomposition for singular matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
except Exception as e:
print(f"Python: LU decomposition failed for singular matrix: {e}")
def test_lu_singular_c(self):
""" Test LU decomposition for a singular matrix using C implementation. """
A = [[1.0, 2.0, 3.0],
[0.0, 0.0, 0.0], # Singular matrix with zero row
[4.0, 5.0, 6.0]]
try:
L, U = lu(A, use_c=True) # C implementation
print("C: LU decomposition for singular matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
except Exception as e:
print(f"C: LU decomposition failed for singular matrix: {e}")
def test_lu_random_python(self):
""" Test LU decomposition for a 10x10 random dense matrix using Python implementation. """
A = random_matrix(10)
L, U = lu(A, use_c=False) # Python implementation
print("Python: LU decomposition for 10x10 random dense matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_lu_random_c(self):
""" Test LU decomposition for a 10x10 random dense matrix using C implementation. """
A = random_matrix(10)
L, U = lu(A, use_c=True) # C implementation
print("C: LU decomposition for 10x10 random dense matrix")
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
class TestPLUDecomposition(unittest.TestCase):
def test_plu_3x3_python(self):
""" Test PLU decomposition for a simple 3x3 matrix using Python implementation. """
A = [[2.0, 3.0, -1.0],
[4.0, 1.0, 2.0],
[-2.0, 7.0, 2.0]]
P, L, U = plu(A, use_c=False) # Python implementation
print("Python: PLU decomposition for 3x3 matrix")
print("P matrix:")
print_matrix(P)
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_plu_3x3_c(self):
""" Test PLU decomposition for a simple 3x3 matrix using C implementation. """
A = [[2.0, 3.0, -1.0],
[4.0, 1.0, 2.0],
[-2.0, 7.0, 2.0]]
P, L, U = plu(A, use_c=True) # C implementation
print("C: PLU decomposition for 3x3 matrix")
print("P matrix:")
print_matrix(P)
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_plu_hilbert_python(self):
""" Test PLU decomposition for a 20x20 Hilbert matrix using Python implementation. """
A = hilbert_matrix(20)
P, L, U = plu(A, use_c=False) # Python implementation
print("Python: PLU decomposition for 20x20 Hilbert matrix")
print("P matrix:")
print_matrix(P)
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
def test_plu_hilbert_c(self):
""" Test PLU decomposition for a 20x20 Hilbert matrix using C implementation. """
A = hilbert_matrix(20)
P, L, U = plu(A, use_c=True) # C implementation
print("C: PLU decomposition for 20x20 Hilbert matrix")
print("P matrix:")
print_matrix(P)
print("L matrix:")
print_matrix(L)
print("U matrix:")
print_matrix(U)
if __name__ == '__main__':
unittest.main()