Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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]
```
43 changes: 42 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,44 @@
end
end

using MatrixAlgebraKit: MatrixAlgebraKit, TruncationStrategy

struct TruncationError{T<:Real} <: TruncationStrategy
ϵ::T
p::Int
relative::Bool
end

"""
truncerr(epsilon::Real, p::Int=2; relative=true)
Create a truncation strategy for truncating such that the error in the factorization
is smaller than `epsilon`, where the error is determined using the `p`-norm.
The keyword argument `relative` specifies if the error `epsilon` is a relative error
or not.
"""
truncerr(epsilon::Real, p::Int=2; relative=true) = TruncationError(epsilon, p, relative)

Check warning on line 154 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L154

Added line #L154 was not covered by tests

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

Check warning on line 158 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L156-L158

Added lines #L156 - L158 were not covered by tests
# norm(values, p) ^ p
normᵖ = sum(Base.Fix2(^, strategy.p) abs, values)
ϵᵖ = strategy.relative ? strategy.ϵ ^ strategy.p * normᵖ : strategy.ϵ ^ strategy.p
if ϵᵖ normᵖ
return Base.OneTo(0)

Check warning on line 163 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L160-L163

Added lines #L160 - L163 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 171 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L165-L171

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

Check warning on line 174 in src/MatrixAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/MatrixAlgebra.jl#L173-L174

Added lines #L173 - L174 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