@@ -93,25 +93,44 @@ function tensortrain(tci)
9393end
9494
9595function _factorize (
96- A:: AbstractMatrix{V} , method:: Symbol ; tolerance:: Float64 , maxbonddim:: Int
96+ A:: AbstractMatrix{V} , method:: Symbol ; tolerance:: Float64 , maxbonddim:: Int , leftorthogonal :: Bool = false , normalizeerror = true
9797):: Tuple{Matrix{V},Matrix{V},Int} where {V}
98+ reltol = 1e-14
99+ abstol = 0.0
100+ if normalizeerror
101+ reltol = tolerance
102+ else
103+ abstol = tolerance
104+ end
98105 if method === :LU
99- factorization = rrlu (A, abstol= tolerance, maxrank= maxbonddim)
106+ factorization = rrlu (A, abstol= abstol, reltol = reltol, maxrank= maxbonddim, leftorthogonal = leftorthogonal )
100107 return left (factorization), right (factorization), npivots (factorization)
101108 elseif method === :CI
102- factorization = MatrixLUCI (A, abstol= tolerance, maxrank= maxbonddim)
109+ factorization = MatrixLUCI (A, abstol= abstol, reltol = reltol, maxrank= maxbonddim, leftorthogonal = leftorthogonal )
103110 return left (factorization), right (factorization), npivots (factorization)
104111 elseif method === :SVD
105112 factorization = LinearAlgebra. svd (A)
113+ err = [sum (factorization. S[n+ 1 : end ] .^ 2 ) for n in 1 : length (factorization. S)]
114+ normalized_err = err ./ sum (factorization. S .^ 2 )
115+
106116 trunci = min (
107- replacenothing (findlast (> (tolerance), factorization. S), 1 ),
117+ replacenothing (findfirst (< (abstol^ 2 ), err), length (err)),
118+ replacenothing (findfirst (< (reltol^ 2 ), normalized_err), length (normalized_err)),
108119 maxbonddim
109120 )
110- return (
111- factorization. U[:, 1 : trunci],
112- Diagonal (factorization. S[1 : trunci]) * factorization. Vt[1 : trunci, :],
113- trunci
114- )
121+ if leftorthogonal
122+ return (
123+ factorization. U[:, 1 : trunci],
124+ Diagonal (factorization. S[1 : trunci]) * factorization. Vt[1 : trunci, :],
125+ trunci
126+ )
127+ else
128+ return (
129+ factorization. U[:, 1 : trunci] * Diagonal (factorization. S[1 : trunci]),
130+ factorization. Vt[1 : trunci, :],
131+ trunci
132+ )
133+ end
115134 else
116135 error (" Not implemented yet." )
117136 end
@@ -131,25 +150,28 @@ function compress!(
131150 tt:: TensorTrain{V,N} ,
132151 method:: Symbol = :LU ;
133152 tolerance:: Float64 = 1e-12 ,
134- maxbonddim:: Int = typemax (Int)
153+ maxbonddim:: Int = typemax (Int),
154+ normalizeerror:: Bool = true
135155) where {V,N}
156+ # From left to right
136157 for ell in 1 : length (tt)- 1
137158 shapel = size (tt. sitetensors[ell])
138159 left, right, newbonddim = _factorize (
139160 reshape (tt. sitetensors[ell], prod (shapel[1 : end - 1 ]), shapel[end ]),
140- method; tolerance, maxbonddim
161+ method; tolerance= 0.0 , maxbonddim= typemax (Int), leftorthogonal = true # no truncation
141162 )
142163 tt. sitetensors[ell] = reshape (left, shapel[1 : end - 1 ]. .. , newbonddim)
143164 shaper = size (tt. sitetensors[ell+ 1 ])
144165 nexttensor = right * reshape (tt. sitetensors[ell+ 1 ], shaper[1 ], prod (shaper[2 : end ]))
145166 tt. sitetensors[ell+ 1 ] = reshape (nexttensor, newbonddim, shaper[2 : end ]. .. )
146167 end
147168
169+ # From right to left
148170 for ell in length (tt): - 1 : 2
149171 shaper = size (tt. sitetensors[ell])
150172 left, right, newbonddim = _factorize (
151173 reshape (tt. sitetensors[ell], shaper[1 ], prod (shaper[2 : end ])),
152- method; tolerance, maxbonddim
174+ method; tolerance, maxbonddim, normalizeerror, leftorthogonal = false
153175 )
154176 tt. sitetensors[ell] = reshape (right, newbonddim, shaper[2 : end ]. .. )
155177 shapel = size (tt. sitetensors[ell- 1 ])
@@ -212,6 +234,7 @@ function Base.reverse(tt::AbstractTensorTrain{V}) where {V}
212234 ]))
213235end
214236
237+
215238"""
216239Fitting data with a TensorTrain object.
217240This may be useful when the interpolated function is noisy.
@@ -266,4 +289,4 @@ function fulltensor(obj::TensorTrain{T,N})::Array{T} where {T,N}
266289 end
267290 returnsize = collect (Iterators. flatten (sitedims_))
268291 return reshape (result, returnsize... )
269- end
292+ end
0 commit comments