Skip to content

Commit

Permalink
Escape entire expression in einsum (#240)
Browse files Browse the repository at this point in the history
* Escape entire expression in einsum

* Bump version
  • Loading branch information
KeitaNakamura authored Oct 31, 2024
1 parent 49fdfaf commit a816a13
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tensorial"
uuid = "98f94333-fa9f-48a9-ad80-1c66397b2b38"
authors = ["Keita Nakamura <keita.nakamura.1109@gmail.com>"]
version = "0.18.1"
version = "0.18.2"

[deps]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand Down
8 changes: 4 additions & 4 deletions src/einsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ end
function einsum_exprssion(TT, expr)
varname, freeinds, body = split_defexpr(expr)
einex = einsum_instantiate(body, TT)
isnothing(freeinds) && return :($(esc(varname)) = $(einex.ex))
isempty(freeinds) && return :($(esc(varname)) = Tensor{Tuple{}}($(einex.ex)))
isnothing(freeinds) && return esc(:($varname = $(einex.ex)))
isempty(freeinds) && return esc(:($varname = Tensor{Tuple{}}($(einex.ex))))
perm = find_perm(einex.freeinds => freeinds)
:($(esc(varname)) = permutedims($(einex.ex), $(ValTuple(perm...))))
esc(:($varname = permutedims($(einex.ex), $(ValTuple(perm...)))))
end

ValTuple(x...) = Val(x)
Expand Down Expand Up @@ -103,7 +103,7 @@ function einsum_instantiate(expr, TT) # TT is :Any if not given in @einsum or no
end
end
elseif Meta.isexpr(expr, :ref)
ex = esc(expr.args[1])
ex = expr.args[1]
allinds = expr.args[2:end]
freeinds = find_freeindices(allinds)
return einsum_instantiate_tensor(EinsumExpr(ex, freeinds, allinds))
Expand Down
1 change: 1 addition & 0 deletions test/einsum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ end
@einsum ans[σ,μ,ν] := A[σp,σ]*A[μp,μ]*A[νp,ν]*B[σp,μp,νp]
@test (@einsum Tensor{Tuple{4,@Symmetry{4,4}}, Float64} C[σ,μ,ν] := A[σp,σ]*A[μp,μ]*A[νp,ν]*B[σp,μp,νp])::Tensor{Tuple{4,@Symmetry{4,4}}, Float64} ans
@test (@einsum Tensor{Tuple{4,@Symmetry{4,4}}, Float32} C[σ,μ,ν] := A[σp,σ]*A[μp,μ]*A[νp,ν]*B[σp,μp,νp])::Tensor{Tuple{4,@Symmetry{4,4}}, Float32} ans
@test (@einsum typeof(B) C[σ,μ,ν] := A[σp,σ]*A[μp,μ]*A[νp,ν]*B[σp,μp,νp])::Tensor{Tuple{4,@Symmetry{4,4}}, Float64} ans
end
@testset "errors" begin
S1 = rand(SymmetricSecondOrderTensor{3})
Expand Down

2 comments on commit a816a13

@KeitaNakamura
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118444

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.2 -m "<description of version>" a816a1377a9aaf44ee2813fe7e04a2740c686078
git push origin v0.18.2

Please sign in to comment.