diff --git a/src/tensors/linalg.jl b/src/tensors/linalg.jl index 40a0e0024..f07f1b174 100644 --- a/src/tensors/linalg.jl +++ b/src/tensors/linalg.jl @@ -421,7 +421,23 @@ function exp!(t::TensorMap) domain(t) == codomain(t) || error("Exponential of a tensor only exist when domain == codomain.") for (c, b) in blocks(t) - copy!(b, LinearAlgebra.exp!(b)) + MatrixAlgebraKit.exponential!(b, b) + end + return t +end + +function exp!((τ, t)::Tuple{Number, TensorMap}) + domain(t) == codomain(t) || + error("Exponential of a tensor only exist when domain == codomain.") + if eltype(t) <: Real && eltype(τ) <: Complex + t_complex = complex(t) + for ((cr, br), (cc, bc)) in zip(blocks(t), blocks(t_complex)) + MatrixAlgebraKit.exponential!((τ, br), bc) + end + return t_complex + end + for (c, b) in blocks(t) + MatrixAlgebraKit.exponential!((τ, b), b) end return t end diff --git a/test/tensors/exponential.jl b/test/tensors/exponential.jl new file mode 100644 index 000000000..51fe1072b --- /dev/null +++ b/test/tensors/exponential.jl @@ -0,0 +1,25 @@ +using Test, TestExtras +using TensorKit +using Random + +spaces = [ℂ^4, Vect[U1Irrep](0 => 1, 1 => 2), Vect[SU2Irrep](0 => 1, 1 // 2 => 1)] +scalartypes = [Float64, ComplexF64] + +@timedtestset "exp!(τ,A) for $space, scalartype(A) = $st1, scalartype(τ) = $st2" for space in spaces, st1 in scalartypes, st2 in scalartypes + A = randn(st1, space, space) + τ = rand(st2) + + @test exp!(copy(A)) == exp!((1.0, copy(A))) + + A2 = exp!((τ, A)) + if st1 <: Real && st2 <: Complex + @test objectid(A2) != objectid(A) + else + @test objectid(A2) == objectid(A) + end + + expτA = exp!((τ, copy(A))) + expminτA = exp!((-τ, copy(A))) + @test expτA * expminτA ≈ id(scalartype(expτA), space) + @test expτA ≈ inv(expminτA) +end