bsc-quantic / Tenet.jl

Composable Tensor Network library in Julia
https://bsc-quantic.github.io/Tenet.jl/
Apache License 2.0
17 stars 1 forks source link

Add `lu` decomposition for `Tensor`s #94

Closed jofrevalles closed 8 months ago

jofrevalles commented 9 months ago

Summary

This PR adds the new lu function for tensors, extending the LinearAlgebra.lu function (resolve #26). The new lu function returns the LU decomposition of a Tensor, where the tensor can be recovered by contracting the permutation tensor P, the tensor L, and tensor U. The tensors L and U are reshaped versions of the original lower and upper triangular matrices obtained during the decomposition process, respectively.

This implementation is inspired by the LU decomposition in the scipy library, as it returns the permutation tensor P allowing the original tensor A to be recovered with the contraction A = P * L * U. This contrasts with LinearAlgebra, where the permutation vector p is returned, and the original matrix can be recovered with P' * A = L * U (where P' is the permutation matrix built from p).

Please let me know if there are any concerns or issues with extending the LinearAlgebra library in this manner.

We have also added tests for this new function.

Example

A usage example of the lu function:

julia> using Tenet; using LinearAlgebra; using Test

julia> tensor = Tensor(rand(4, 4, 4), (:i, :j, :k))
4×4×4 Tensor{Float64, 3, Array{Float64, 3}}:
...

julia> P, L, U = lu(tensor, left_inds = labels(tensor)[1:2])
...

julia> @test contract(contract(P, L), U) ≈ tensor
Test Passed
codecov[bot] commented 9 months ago

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (5b3e11b) 87.71% compared to head (525f14e) 88.18%. Report is 1 commits behind head on develop.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #94 +/- ## =========================================== + Coverage 87.71% 88.18% +0.46% =========================================== Files 10 10 Lines 570 584 +14 =========================================== + Hits 500 515 +15 + Misses 70 69 -1 ``` | [Files](https://app.codecov.io/gh/bsc-quantic/Tenet.jl/pull/94?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bsc-quantic) | Coverage Δ | | |---|---|---| | [src/Tensor.jl](https://app.codecov.io/gh/bsc-quantic/Tenet.jl/pull/94?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bsc-quantic#diff-c3JjL1RlbnNvci5qbA==) | `88.99% <100.00%> (ø)` | | | [src/Transformations.jl](https://app.codecov.io/gh/bsc-quantic/Tenet.jl/pull/94?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bsc-quantic#diff-c3JjL1RyYW5zZm9ybWF0aW9ucy5qbA==) | `98.41% <100.00%> (ø)` | | | [src/Numerics.jl](https://app.codecov.io/gh/bsc-quantic/Tenet.jl/pull/94?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bsc-quantic#diff-c3JjL051bWVyaWNzLmps) | `92.20% <97.72%> (+3.31%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

mofeing commented 8 months ago

@jofrevalles I've updated the code to the recent changes. Also, I've refactored the SVD and QR factorizations. Would you mind giving a quick review?

jofrevalles commented 8 months ago

@jofrevalles I've updated the code to the recent changes. Also, I've refactored the SVD and QR factorizations. Would you mind giving a quick review?

I have checked it and everything looks good to me!