@@ -145,6 +145,15 @@ def Lanczos_PRO(A, q, m=None, toll=np.sqrt(np.finfo(float).eps)):
145
145
Raises:
146
146
ValueError: If the input matrix A is not square or if m is greater than the size of A.
147
147
"""
148
+ if m == None :
149
+ m = A .shape [0 ]
150
+
151
+ if A .shape [0 ] != A .shape [1 ]:
152
+ raise ValueError ("Input matrix A must be square." )
153
+
154
+ if A .shape [0 ] != q .shape [0 ]:
155
+ raise ValueError ("Input vector q must have the same size as the matrix A." )
156
+
148
157
q = q / np .linalg .norm (q )
149
158
Q = np .array ([q ])
150
159
r = A @ q
@@ -172,7 +181,6 @@ def Lanczos_PRO(A, q, m=None, toll=np.sqrt(np.finfo(float).eps)):
172
181
if np .abs (beta [j ]) < 1e-15 :
173
182
return Q , alpha , beta [:- 1 ]
174
183
return Q , alpha , beta [:- 1 ]
175
-
176
184
177
185
178
186
def QR_method (A_copy , tol = 1e-10 , max_iter = 100 ):
@@ -197,32 +205,43 @@ def QR_method(A_copy, tol=1e-10, max_iter=100):
197
205
ValueError: If the input matrix A_copy is not square.
198
206
"""
199
207
200
- A = A_copy .copy ()
201
- T = A .copy ()
202
- A = np .array (A )
203
- iter = 0
204
-
205
- while np .linalg .norm (np .diag (A , - 1 ), np .inf ) > tol and iter < max_iter :
206
- Matrix_trigonometry = np .array ([])
207
- QQ , RR = np .linalg .qr (T )
208
- T = RR @QQ
209
- for i in range (A .shape [0 ]- 1 ):
210
- c = A [i , i ]/ np .sqrt (A [i , i ]** 2 + A [i + 1 , i ]** 2 )
211
- s = - A [i + 1 , i ]/ np .sqrt (A [i , i ]** 2 + A [i + 1 , i ]** 2 )
212
- Matrix_trigonometry = np .vstack ((Matrix_trigonometry , [c , s ])) if Matrix_trigonometry .size else np .array ([[c , s ]])
213
-
214
- R = np .array ([[c , - s ], [s , c ]])
215
- A [i :i + 2 , i :i + 3 ]= R @A [i :i + 2 , i :i + 3 ]
216
- A [i + 1 , i ]= 0
217
- Q = np .eye (A .shape [0 ])
218
- i = 0
219
- Q [0 :2 , 0 :2 ]= np .array ([[Matrix_trigonometry [i , 0 ], Matrix_trigonometry [i , 1 ]], [- Matrix_trigonometry [i , 1 ], Matrix_trigonometry [i , 0 ]]])
220
- for i in range (1 , A .shape [0 ]- 1 ):
221
- R = np .eye (A .shape [0 ])
222
- R [i : i + 2 , i :i + 2 ]= np .array ([[Matrix_trigonometry [i , 0 ], Matrix_trigonometry [i , 1 ]], [- Matrix_trigonometry [i , 1 ], Matrix_trigonometry [i , 0 ]]])
223
- Q = Q @R
224
- A = A @Q
225
- iter += 1
226
-
227
-
228
- return np .diag (A ), Q
208
+ A = A_copy .copy ()
209
+ A = np .array (A )
210
+ iter = 0
211
+ Q = np .array ([])
212
+
213
+ while np .linalg .norm (np .diag (A , - 1 ), np .inf ) > tol and iter < max_iter :
214
+ Matrix_trigonometry = np .array ([])
215
+ for i in range (A .shape [0 ] - 1 ):
216
+ c = A [i , i ] / np .sqrt (A [i , i ] ** 2 + A [i + 1 , i ] ** 2 )
217
+ s = - A [i + 1 , i ] / np .sqrt (A [i , i ] ** 2 + A [i + 1 , i ] ** 2 )
218
+ Matrix_trigonometry = (
219
+ np .vstack ((Matrix_trigonometry , [c , s ]))
220
+ if Matrix_trigonometry .size
221
+ else np .array ([[c , s ]])
222
+ )
223
+
224
+ R = np .array ([[c , - s ], [s , c ]])
225
+ A [i : i + 2 , i : i + 3 ] = R @ A [i : i + 2 , i : i + 3 ]
226
+ A [i + 1 , i ] = 0
227
+ Q = np .eye (A .shape [0 ])
228
+ i = 0
229
+ Q [0 :2 , 0 :2 ] = np .array (
230
+ [
231
+ [Matrix_trigonometry [i , 0 ], Matrix_trigonometry [i , 1 ]],
232
+ [- Matrix_trigonometry [i , 1 ], Matrix_trigonometry [i , 0 ]],
233
+ ]
234
+ )
235
+ for i in range (1 , A .shape [0 ] - 1 ):
236
+ R = np .eye (A .shape [0 ])
237
+ R [i : i + 2 , i : i + 2 ] = np .array (
238
+ [
239
+ [Matrix_trigonometry [i , 0 ], Matrix_trigonometry [i , 1 ]],
240
+ [- Matrix_trigonometry [i , 1 ], Matrix_trigonometry [i , 0 ]],
241
+ ]
242
+ )
243
+ Q = Q @ R
244
+ A = A @ Q
245
+ iter += 1
246
+
247
+ return np .diag (A ), Q
0 commit comments