6
6
from numpy .random import seed , rand
7
7
from scikit_tt .tensor_train import TT , build_core , residual_error , uniform , residual_error
8
8
from scikit_tt .solvers .sle import als
9
+ from time import time
9
10
10
11
class MatrixCollection (object ):
11
12
@@ -167,9 +168,13 @@ def _update_approximation(x_TT: "TT", V: "MatrixCollection", y_TT: "TT"):
167
168
for s in range (x_TT .order ):
168
169
169
170
x_TT .cores [s ] = np .sum (V [s ][None , :, :, None , None ] @ y_TT .cores [s ][:, None , :, :, :], axis = 2 )
171
+ x_TT .ranks [s ] = x_TT .cores [s ].shape [0 ]
172
+ x_TT .ranks [s + 1 ] = x_TT .cores [s ].shape [3 ]
173
+
170
174
171
175
return
172
176
177
+ #def _residual_norm()
173
178
174
179
def symmetric_tensorkrylov (A : "MatrixCollection" , b : List [np .ndarray ], rank : int , nmax : int , tol = 1e-9 ):
175
180
@@ -213,11 +218,9 @@ def symmetric_tensorkrylov(A: "MatrixCollection", b: List[np.ndarray], rank: int
213
218
214
219
y_TT = als (TT_operator , TT_guess , TT_rhs )
215
220
_update_approximation (x_TT , V_minors , y_TT )
216
- print (A_TT )
217
- print (x_TT )
218
- print (b_TT )
219
- r_norm = residual_error (A_TT , x_TT , b_TT )
221
+ #r_norm = residual_error(A_TT, x_TT, b_TT)
220
222
223
+ #print(r_norm)
221
224
if r_norm <= tol :
222
225
223
226
return x_TT
@@ -239,8 +242,23 @@ def random_rhs(n: int):
239
242
240
243
A = MatrixCollection ([ As for _ in range (d ) ])
241
244
b = [ bs for _ in range (d ) ]
242
- rank = 5
245
+ rank = 8
243
246
ranks = [1 ] + ([rank ] * (d - 1 )) + [1 ]
244
247
248
+ row_dims = [n for _ in range (d )]
249
+ col_dims = [1 for _ in range (d )]
250
+
251
+ x_TT = scikit_tt .tensor_train .rand (row_dims , col_dims , ranks )
252
+ A_TT = _TT_operator (A , n - 1 )
253
+ b_TT = _TT_rhs (b )
254
+
255
+
256
+ start = time ()
257
+ x_TT = als (A_TT , x_TT , b_TT )
258
+ end = time ()
259
+ print ("Done" , end - start )
260
+
261
+ print (residual_error (A_TT , x_TT , b_TT ))
262
+
245
263
246
264
print (symmetric_tensorkrylov (A , b , rank , n , tol = 1e-9 ))
0 commit comments