Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TensorAlgebra"
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.3"
version = "0.3.4"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Reference

```@autodocs
Modules = [TensorAlgebra]
Modules = [TensorAlgebra, TensorAlgebra.MatrixAlgebra]
```
42 changes: 41 additions & 1 deletion src/MatrixAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
svd,
svd!,
svdvals,
svdvals!
svdvals!,
truncerr

using LinearAlgebra: LinearAlgebra
using MatrixAlgebraKit
Expand Down Expand Up @@ -133,4 +134,43 @@
end
end

using MatrixAlgebraKit: MatrixAlgebraKit, TruncationStrategy

struct TruncationError{T<:Real} <: TruncationStrategy
atol::T
rtol::T
p::Int
end

"""
truncerr(; atol::Real=0, rtol::Real=0, p::Int=2)

Create a truncation strategy for truncating such that the error in the factorization
is smaller than `max(atol, rtol * norm)`, where the error is determined using the `p`-norm.
"""
function truncerr(; atol::Real=0, rtol::Real=0, p::Int=2)
return TruncationError(promote(atol, rtol)..., p)

Check warning on line 152 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L151-L152

Added lines #L151 - L152 were not covered by tests
end

function MatrixAlgebraKit.findtruncated(values::AbstractVector, strategy::TruncationError)
Base.require_one_based_indexing(values)
issorted(values; rev=true) || error("Not sorted.")

Check warning on line 157 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L155-L157

Added lines #L155 - L157 were not covered by tests
# norm(values, p) ^ p
normᵖ = sum(Base.Fix2(^, strategy.p) ∘ abs, values)
ϵᵖ = max(strategy.atol ^ strategy.p, strategy.rtol ^ strategy.p * normᵖ)
if ϵᵖ ≥ normᵖ
return Base.OneTo(0)

Check warning on line 162 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L159-L162

Added lines #L159 - L162 were not covered by tests
end
truncerrᵖ = zero(real(eltype(values)))
rank = length(values)
for i in reverse(eachindex(values))
truncerrᵖ += abs(values[i]) ^ strategy.p
if truncerrᵖ ≥ ϵᵖ
rank = i
break

Check warning on line 170 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L164-L170

Added lines #L164 - L170 were not covered by tests
end
end
return Base.OneTo(rank)

Check warning on line 173 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L172-L173

Added lines #L172 - L173 were not covered by tests
end

end
1 change: 1 addition & 0 deletions test/test_exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ using TensorAlgebra: TensorAlgebra
:svd!,
:svdvals,
:svdvals!,
:truncerr,
]
@test issetequal(names(TensorAlgebra.MatrixAlgebra), exports)
end
Loading
Loading