bsc-quantic / Tensors.jl

Tensors and Einstein Summation in Julia
https://bsc-quantic.github.io/Tensors.jl/
Apache License 2.0
0 stars 1 forks source link

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

Open jofrevalles opened 1 year ago

jofrevalles commented 1 year ago

Summary

This PR adds a new lu function for tensors, extending the LinearAlgebra.lu function (resolve #3). 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 1 year ago

Codecov Report

Merging #26 (dc51736) into master (bff2de5) will increase coverage by 1.56%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #26      +/-   ##
==========================================
+ Coverage   82.67%   84.23%   +1.56%     
==========================================
  Files           5        5              
  Lines         202      222      +20     
==========================================
+ Hits          167      187      +20     
  Misses         35       35              
Impacted Files Coverage Δ
src/Numerics.jl 95.71% <100.00%> (+1.71%) :arrow_up:
mofeing commented 1 year ago

I need more time to check why we need P and the details of the implementation. Since this is not a hurry, I will check it out whenever I have some free time.