You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/abstracttensortrain.jl
+62-12Lines changed: 62 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -172,7 +172,11 @@ function sum(tt::AbstractTensorTrain{V}) where {V}
172
172
returnonly(v)
173
173
end
174
174
175
-
function_addtttensor(A::Array{V}, B::Array{V}; lefttensor=false, righttensor=false) where {V}
175
+
function_addtttensor(
176
+
A::Array{V}, B::Array{V};
177
+
factorA=one(V), factorB=one(V),
178
+
lefttensor=false, righttensor=false
179
+
) where {V}
176
180
ifndims(A) !=ndims(B)
177
181
throw(DimensionMismatch("Elementwise addition only works if both tensors have the same indices, but A and B have different numbers ($(ndims(A)) and $(ndims(B))) of indices."))
178
182
end
@@ -181,38 +185,84 @@ function _addtttensor(A::Array{V}, B::Array{V}; lefttensor=false, righttensor=fa
181
185
offset3 = righttensor ?0:size(A, nd)
182
186
localindices =fill(Colon(), nd -2)
183
187
C =zeros(V, offset1 +size(B, 1), size(A)[2:nd-1]..., offset3 +size(B, nd))
184
-
C[1:size(A, 1), localindices..., 1:size(A, nd)] = A
185
-
C[offset1+1:end, localindices..., offset3+1:end] = B
Addition of two tensor trains. If `c = add(a, b)`, then `c(v) ≈ a(v) + b(v)` at each index set `v`. Note that this function increases the bond dimension, i.e. ``\chi_{\text{result}} = \chi_1 + \chi_2`` if the original tensor trains had bond dimensions ``\chi_1`` and ``\chi_2``. In many cases, it is advisable to recompress/truncate the resulting tensor train afterwards.
200
+
Addition of two tensor trains. If `C = add(A, B)`, then `C(v) ≈ A(v) + B(v)` at each index set `v`. Note that this function increases the bond dimension, i.e. ``\chi_{\text{result}} = \chi_1 + \chi_2`` if the original tensor trains had bond dimensions ``\chi_1`` and ``\chi_2``.
201
+
202
+
Arguments:
203
+
- `lhs`, `rhs`: Tensor trains to be added.
204
+
- `factorlhs`, `factorrhs`: Factors to multiply each tensor train by before addition.
205
+
- `tolerance`, `maxbonddim`: Parameters to be used for the recompression step.
206
+
207
+
Returns:
208
+
A new `TensorTrain` representing the function `factorlhs * lhs(v) + factorrhs * rhs(v)`.
193
209
194
210
See also: [`+`](@ref)
195
211
"""
196
-
functionadd(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
Addition of two tensor trains. If `c = a + b`, then `c(v) ≈ a(v) + b(v)` at each index set `v`. Note that this function increases the bond dimension, i.e. ``\chi_{\text{result}} = \chi_1 + \chi_2`` if the original tensor trains had bond dimensions ``\chi_1`` and ``\chi_2``. In many cases, it is advisable to recompress/truncate the resulting tensor train afterwards.
252
+
@docraw"""
253
+
function (+)(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
213
254
214
-
See also: [`add`](@ref)
255
+
Addition of two tensor trains. If `c = a + b`, then `c(v) ≈ a(v) + b(v)` at each index set `v`. Note that this function increases the bond dimension, i.e. ``\chi_{\text{result}} = \chi_1 + \chi_2`` if the original tensor trains had bond dimensions ``\chi_1`` and ``\chi_2``. Can be combined with automatic recompression by calling [`add`](@ref).
215
256
"""
216
-
function(+)(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
257
+
functionBase.:+(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
217
258
returnadd(lhs, rhs)
218
259
end
260
+
261
+
@docraw"""
262
+
function (-)(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
263
+
264
+
Subtraction of two tensor trains. If `c = a - b`, then `c(v) ≈ a(v) - b(v)` at each index set `v`. Note that this function increases the bond dimension, i.e. ``\chi_{\text{result}} = \chi_1 + \chi_2`` if the original tensor trains had bond dimensions ``\chi_1`` and ``\chi_2``. Can be combined with automatic recompression by calling [`subtract`](@ref) (see documentation for [`add`](@ref)).
265
+
"""
266
+
function Base.:-(lhs::AbstractTensorTrain{V}, rhs::AbstractTensorTrain{V}) where {V}
0 commit comments