Skip to content

Commit

Permalink
Merge 182e412 into 6b6b362
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlevy authored Oct 19, 2023
2 parents 6b6b362 + 182e412 commit d5f3168
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/tensor_operations/matrix_decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ Perform a factorization of `A` into ITensors `L` and `R` such that `A ≈ L * R`
For truncation arguments, see: [`svd`](@ref)
"""
function factorize(A::ITensor, Linds...; kwargs...)
function factorize(A::ITensor, Linds...; maxdim=nothing, kwargs...)
ortho::String = get(kwargs, :ortho, "left")
tags::TagSet = get(kwargs, :tags, "Link,fact")
plev::Int = get(kwargs, :plev, 0)
Expand Down Expand Up @@ -661,9 +661,14 @@ function factorize(A::ITensor, Linds...; kwargs...)
# Determines when to use eigen vs. svd (eigen is less precise,
# so eigen should only be used if a larger cutoff is requested)
automatic_cutoff = 1e-12
Lis = indices(Linds...)
dL, dR = dim(Lis), dim(indices(setdiff(inds(A), Lis)))
maxdim = get(kwargs, :maxdim, min(dL, dR))
Lis = commoninds(A, indices(Linds...))
Ris = uniqueinds(A, Lis)
dL, dR = dim(Lis), dim(Ris)
# maxdim is forced to be at most the max given SVD
if isnothing(maxdim)
maxdim = min(dL, dR)
end
maxdim = min(maxdim, min(dL, dR))
might_truncate = !isnothing(cutoff) || maxdim < min(dL, dR)

if isnothing(which_decomp)
Expand All @@ -677,13 +682,13 @@ function factorize(A::ITensor, Linds...; kwargs...)
end

if which_decomp == "svd"
LR = factorize_svd(A, Linds...; kwargs...)
LR = factorize_svd(A, Linds...; kwargs..., maxdim=maxdim)
if isnothing(LR)
return nothing
end
L, R, spec = LR
elseif which_decomp == "eigen"
L, R, spec = factorize_eigen(A, Linds...; kwargs...)
L, R, spec = factorize_eigen(A, Linds...; kwargs..., maxdim=maxdim)
elseif which_decomp == "qr"
L, R = factorize_qr(A, Linds...; kwargs...)
spec = Spectrum(nothing, 0.0)
Expand Down
14 changes: 14 additions & 0 deletions test/base/test_decomp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,20 @@ end
@test blockdim(u, b) == blockdim(i, b) || blockdim(u, b) >= min_blockdim
end
end

@testset "factorize with mindim" begin
l = Index(8, "l")
s1 = Index(2, "s1")
s2 = Index(2, "s2")
r = Index(2, "r")

phi = randomITensor(l, s1, s2, r)

U, B = factorize(phi, (l, s1); ortho="left", mindim=8, which_decomp="eigen")

@test norm(U * B - phi) < 1E-5
@test dim(commonind(U, B)) <= 4
end
end

nothing

0 comments on commit d5f3168

Please sign in to comment.