From 5e3e5e855e1825012fe4af4afd114e1e8f53697e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Barth=C3=A9l=C3=A9my?= <80510638+jfbarthelemy@users.noreply.github.com> Date: Sun, 14 Jan 2024 20:35:20 +0100 Subject: [PATCH] Corrections for compatibility with Julia 1.10 and SymPy 2 --- .github/workflows/CI.yml | 1 + .gitignore | 3 ++ Project.toml | 6 +-- setuplib/README.md | 103 --------------------------------------- src/array_utils.jl | 10 ++-- src/bases.jl | 11 +++-- src/coorsystems.jl | 10 ++-- src/tens.jl | 42 ++++++++-------- src/tens_isotropic.jl | 14 +++--- 9 files changed, 51 insertions(+), 149 deletions(-) delete mode 100644 setuplib/README.md diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index fb9bf83..8fb9832 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -13,6 +13,7 @@ jobs: - '1.7' - '1.8' - '1.9' + - '1.10' - 'nightly' os: - ubuntu-latest diff --git a/.gitignore b/.gitignore index 106d64c..0cd144b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ /docs/build/ .vscode/* + +local + diff --git a/Project.toml b/Project.toml index 59ec0c3..960a8cb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TensND" uuid = "474b5345-2588-4284-8a58-2430a1a40cb0" authors = ["Jean-François Barthélémy"] -version = "0.1.1" +version = "0.1.2" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" @@ -15,11 +15,11 @@ TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" [compat] OMEinsum = "0.6, 0.7" Rotations = "1" -SymPy = "1" +SymPy = "2" Symbolics = "4, 5" Tensors = "1" TimerOutputs = "0.5" -julia = "1.7, 1.8, 1.9" +julia = "1.7, 1.8, 1.9, 1.10" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/setuplib/README.md b/setuplib/README.md deleted file mode 100644 index de8dc44..0000000 --- a/setuplib/README.md +++ /dev/null @@ -1,103 +0,0 @@ -# Construction of the package TensND - -## Construction from [`PkgTemplates.jl`](https://github.com/invenia/PkgTemplates.jl) - -The package [`TensND.jl`](https://github.com/jfbarthelemy/TensND.jl) has been created using [`PkgTemplates.jl`](https://github.com/invenia/PkgTemplates.jl): - -```julia -julia> using PkgTemplates -julia> t=Template(interactive=true) -julia> t("TensND.jl") -``` - -Note that the chosen plugins are the default ones as well as **Documenter** and **GitHubActions**. - -It is then necessary to change the environment to that of the present package `TensND.jl` and to add the following packages: - -```julia -julia> cd("Path_To_TensND_folder") -julia> ] -(@v1.6) pkg> activate . -(TensND) pkg> add LinearAlgebra -(TensND) pkg> add OMEinsum -(TensND) pkg> add SymPy -(TensND) pkg> add Tensors -``` - -## Manual modification of `Project.toml` - -In order to ensure that CompactHelper runs correctly, it is necessary to manually modify `Project.toml` by completing the `[compat]` section: - -```toml -[compat] -OMEinsum = "0.4" -Tensors = "1.6" -SymPy = "1" -julia = "1.6" -``` - -## Adaptations related to the use of [`SymPy.jl`](https://github.com/JuliaPy/SymPy.jl) - -The use of [`SymPy.jl`](https://github.com/JuliaPy/SymPy.jl) requires some adaptations of the configuration files of **GitHubActions** for *tests* and *documentation*. - -- Concerning *tests*, two steps are needed: - - 1. Create a file `install_dependencies.jl` (copied and modified from [install_dependencies.jl](https://github.com/tkf/IPython.jl/blob/master/test/install_dependencies.jl)) under `test` beside `runtests.jl` containing: - - ```julia - Pkg = Base.require(Base.PkgId(Base.UUID(0x44cfe95a1eb252eab672e2afdf69b78f), "Pkg")) - ENV["PYTHON"] = "" - Pkg.build("PyCall") - ``` - - 1. Add the following lines at the beginning of `runtests.jl`: - - ```julia - if lowercase(get(ENV, "CI", "false")) == "true" - include("install_dependencies.jl") - end - ``` - -- Concerning *documentation*, three actions are required: - - 1. Add [`SymPy.jl`](https://github.com/JuliaPy/SymPy.jl) to the `Project.toml` file located in the folder `docs` - - ```julia - julia> cd("Path_To_TensND_folder/docs") - julia> ] - (@v1.6) pkg> activate . - (docs) pkg> add SymPy - (docs) pkg> ⇐ # type Backspace - julia> cd("..") - julia> ] - (docs) pkg> activate . # to activate the environment TensND - ``` - - 1. Add `using SymPy` at the beginning of `docs/make.jl` - - 1. Modify the file `CI.yml` located in `.github/workflows`. - - In `jobs: docs: steps:`, change the run of precompilation in - - ```yml - - run: | - julia --project=docs -e ' - using Pkg - Pkg = Base.require(Base.PkgId(Base.UUID(0x44cfe95a1eb252eab672e2afdf69b78f), "Pkg")) - Pkg.develop(PackageSpec(path=pwd())) - ENV["PYTHON"] = "" - Pkg.build("PyCall") - Pkg.instantiate()' - ``` - - and complete with `using SymPy` in - - ```yml - - run: | - julia --project=docs -e ' - using Documenter: DocMeta, doctest - using SymPy # **line to add** - using TensND - DocMeta.setdocmeta!(TensND, :DocTestSetup, :(using TensND); recursive=true) - doctest(TensND)' - ``` diff --git a/src/array_utils.jl b/src/array_utils.jl index 6480aad..cbcee0c 100644 --- a/src/array_utils.jl +++ b/src/array_utils.jl @@ -17,15 +17,15 @@ for OP in (:(tsimplify), :(tfactor), :(tsubs), :(ttrigsimp), :(texpand_trig)) end for OP in (:(simplify), :(factor), :(subs), :(diff)) - @eval $(Symbol("t",OP))(x::Sym, args...; kwargs...) = SymPy.$OP(x, args...; kwargs...) + @eval $(Symbol("t",OP))(x::T, args...; kwargs...) where {T<:Sym} = SymPy.$OP(x, args...; kwargs...) end for OP in (:(trigsimp), :(expand_trig)) - @eval $(Symbol("t",OP))(x::Sym, args...; kwargs...) = sympy.$OP(x, args...; kwargs...) + @eval $(Symbol("t",OP))(x::T, args...; kwargs...) where {T<:Sym} = sympy.$OP(x, args...; kwargs...) end for OP in (:(tsimplify), :(tfactor), :(tsubs), :(tdiff), :(ttrigsimp), :(texpand_trig)) - @eval $OP(m::AbstractArray{Sym}, args...; kwargs...) = $OP.(m, args...; kwargs...) - @eval $OP(m::Array{Sym}, args...; kwargs...) = $OP.(m, args...; kwargs...) - @eval $OP(m::Symmetric{Sym}, args...; kwargs...) = Symmetric($OP.(m, args...; kwargs...)) + @eval $OP(m::AbstractArray{T}, args...; kwargs...) where {T<:Sym} = $OP.(m, args...; kwargs...) + @eval $OP(m::Array{T}, args...; kwargs...) where {T<:Sym} = $OP.(m, args...; kwargs...) + @eval $OP(m::Symmetric{T}, args...; kwargs...) where {T<:Sym} = Symmetric($OP.(m, args...; kwargs...)) end tsimplify(x::Num, args...; kwargs...) = Symbolics.simplify(x, args...; kwargs...) diff --git a/src/bases.jl b/src/bases.jl index 125cdeb..569eccc 100644 --- a/src/bases.jl +++ b/src/bases.jl @@ -33,6 +33,7 @@ function superscriptnumber(i::Integer) return join(c) end +diago(M::AbstractMatrix) = [M[i,i] for i in 1:min(size(M)...)] """ Basis(v::AbstractMatrix{T}, ::Val{:cov}) @@ -99,7 +100,7 @@ struct Basis{dim,T} <: AbstractBasis{dim,T} elseif isidentity(gᵢⱼ) return RotatedBasis(eᵢ) elseif isdiag(gᵢⱼ) - χ = sqrt.(diag(gᵢⱼ)) + χ = sqrt.(diago(gᵢⱼ)) return OrthogonalBasis(RotatedBasis(eᵢ .* transpose(inv.(χ))), χ) else eᵢ = Matrix(eᵢ) @@ -137,7 +138,7 @@ struct Basis{dim,T} <: AbstractBasis{dim,T} if isidentity(gᵢⱼ) return RotatedBasis(eᵢ) elseif isdiagonal(gᵢⱼ) - χ = sqrt.(diag(gᵢⱼ)) + χ = sqrt.(diago(gᵢⱼ)) return OrthogonalBasis(RotatedBasis(eᵢ .* transpose(inv.(χ))), χ) else if T <: SymType @@ -161,7 +162,7 @@ struct Basis{dim,T} <: AbstractBasis{dim,T} if isidentity(gⁱʲ) return RotatedBasis(eⁱ) elseif isdiagonal(gⁱʲ) - uχ = inv.(sqrt.(diag(gⁱʲ))) + uχ = inv.(sqrt.(diago(gⁱʲ))) return OrthogonalBasis(RotatedBasis(eⁱ .* transpose(uχ)), uχ) else if T <: SymType @@ -507,9 +508,9 @@ isorthonormal(ℬ::AbstractBasis) = isidentity(metric(ℬ)) isorthonormal(::OrthonormalBasis) = true for OP in (:(tsimplify), :(tfactor), :(tsubs), :(tdiff), :(ttrigsimp), :(texpand_trig)) - @eval $OP(b::AbstractBasis{dim,Sym}, args...; kwargs...) where {dim} = + @eval $OP(b::AbstractBasis{dim,<:Sym}, args...; kwargs...) where {dim} = Basis($OP(b.eᵢ, args...; kwargs...)) - @eval $OP(b::CanonicalBasis{dim,Sym}, args...; kwargs...) where {dim} = b + @eval $OP(b::CanonicalBasis{dim,<:Sym}, args...; kwargs...) where {dim} = b end for OP in (:(tsimplify), :(tsubs), :(tdiff)) @eval $OP(b::AbstractBasis{dim,Num}, args...; kwargs...) where {dim} = diff --git a/src/coorsystems.jl b/src/coorsystems.jl index 41b8804..a7b1c11 100644 --- a/src/coorsystems.jl +++ b/src/coorsystems.jl @@ -357,7 +357,7 @@ function coorsys_cartesian(coords::NTuple{3,T}=symbols("x y z", real=true)) wher χᵢ = ntuple(_ -> one(eltype(coords)), dim) return CoorSystemSym(OM, coords, ℬ, χᵢ) end -coorsys_cartesian(::Val{Sym}, coords=symbols("x y z", real=true)) = coorsys_cartesian(coords) +coorsys_cartesian(::Val{<:Sym}, coords=symbols("x y z", real=true)) = coorsys_cartesian(coords) coorsys_cartesian(::Val{Num}, coords=Tuple(@variables x y z)) = coorsys_cartesian(coords) @@ -393,7 +393,7 @@ function coorsys_polar( OM = r * 𝐞ʳ return CoorSystemSym(OM, coords, ℬᵖ, (one(eltype(coords)), r)) end -coorsys_polar(::Val{Sym}, coords=(symbols("r", positive=true), +coorsys_polar(::Val{<:Sym}, coords=(symbols("r", positive=true), symbols("θ", real=true)); canonical=false) = coorsys_polar(coords; canonical=canonical) coorsys_polar(::Val{Num}, coords=Tuple(@variables r θ); canonical=false) = coorsys_polar(coords; canonical=canonical) @@ -430,7 +430,7 @@ function coorsys_cylindrical( return CoorSystemSym(OM, coords, ℬᶜ, (one(eltype(coords)), r, one(eltype(coords)))) end -coorsys_cylindrical(::Val{Sym}, coords=(symbols("r", positive=true), +coorsys_cylindrical(::Val{<:Sym}, coords=(symbols("r", positive=true), symbols("θ", real=true), symbols("z", real=true)); canonical=false) = coorsys_cylindrical(coords; canonical=canonical) coorsys_cylindrical(::Val{Num}, coords=Tuple(@variables r θ z); canonical=false) = coorsys_cylindrical(coords; canonical=canonical) @@ -479,7 +479,7 @@ function coorsys_spherical( rules = Dict(abs(sin(θ)) => sin(θ), transpose(tan(θ)) => tan(θ), 1 // 1 => 1, 2 // 1 => 2) return CoorSystemSym(OM, coords, ℬˢ, (r, r * sin(θ), one(eltype(coords))); rules=rules) end -coorsys_spherical(::Val{Sym}, coords=(symbols("θ", real=true), +coorsys_spherical(::Val{<:Sym}, coords=(symbols("θ", real=true), symbols("ϕ", real=true), symbols("r", positive=true)); canonical=false) = coorsys_spherical(coords; canonical=canonical) coorsys_spherical(::Val{Num}, coords=Tuple(@variables θ ϕ r); canonical=false) = coorsys_spherical(coords; canonical=canonical) @@ -546,7 +546,7 @@ function coorsys_spheroidal( to_coords=to_coords ) end -coorsys_spheroidal(::Val{Sym}, coords=( +coorsys_spheroidal(::Val{<:Sym}, coords=( symbols("ϕ", real=true), symbols("p", real=true), symbols("q", positive=true), diff --git a/src/tens.jl b/src/tens.jl index c33204c..01f19de 100644 --- a/src/tens.jl +++ b/src/tens.jl @@ -339,7 +339,7 @@ for B in (AbstractBasis, OrthogonalBasis, OrthonormalBasis) ℬ::$B{dim}, var::NTuple{order,Symbol}, ) where {order,dim,T} - if isequal(ℬ , getbasis(t)) + if ℬ == getbasis(t) return components(t, var) else return components(TensOrthonormal(t), ℬ, var) @@ -359,7 +359,7 @@ function components(t::Tens{order,dim,T}, var::NTuple{order,Symbol}) where {orde g_or_G = metric(getbasis(t), var[i]) ec2 = (i, newcp) ec3 = ntuple(j -> j ≠ i ? j : newcp, Val(order)) - m = einsum(EinCode((ec1, ec2), ec3), (m, g_or_G)) + m = T.(einsum(EinCode((ec1, ec2), ec3), (m, g_or_G))) end end return m @@ -371,7 +371,7 @@ function components( ℬ::AbstractBasis{dim}, var::NTuple{order,Symbol}, ) where {order,dim,T} - if isequal(ℬ , getbasis(t)) + if ℬ == getbasis(t) return components(t, var) else bb = Dict() @@ -388,7 +388,7 @@ function components( if c ≠ I ec2 = (i, newcp) ec3 = ntuple(j -> j ≠ i ? j : newcp, Val(order)) - m = einsum(EinCode((ec1, ec2), ec3), (m, c)) + m = T.(einsum(EinCode((ec1, ec2), ec3), (m, c))) end end return m @@ -400,7 +400,7 @@ function components( ℬ::OrthogonalBasis{dim}, var::NTuple{order,Symbol}, ) where {order,dim,T} - if isequal(ℬ , getbasis(t)) + if ℬ == getbasis(t) return components(t, var) else m = Array(components(t, relevant_OrthonormalBasis(ℬ))) @@ -422,7 +422,7 @@ function components( t::TensOrthonormal{order,dim,T}, ℬ::OrthonormalBasis{dim}, ) where {order,dim,T} - if isequal(ℬ , getbasis(t)) + if ℬ == getbasis(t) return getarray(t) else bb = vecbasis(getbasis(t))' * vecbasis(ℬ) @@ -433,7 +433,7 @@ function components( if bb ≠ I ec2 = (i, newcp) ec3 = ntuple(j -> j ≠ i ? j : newcp, Val(order)) - m = einsum(EinCode((ec1, ec2), ec3), (m, bb)) + m = T.(einsum(EinCode((ec1, ec2), ec3), (m, bb))) end end return m @@ -500,7 +500,7 @@ Tens{1, 3, Sym, Vec{3, Sym}} ``` """ function change_tens(t::AbstractTens, ℬ::AbstractBasis, newvar::NTuple) - if isequal(ℬ, getbasis(t)) && newvar == getvar(t) + if ℬ == getbasis(t) && newvar == getvar(t) return t else return Tens(components(t, ℬ, newvar), ℬ, newvar) @@ -508,7 +508,7 @@ function change_tens(t::AbstractTens, ℬ::AbstractBasis, newvar::NTuple) end function change_tens(t::AbstractTens, newbasis::AbstractBasis) - if isequal(newbasis, getbasis(t)) + if newbasis == getbasis(t) return t else return Tens(components(t, newbasis, getvar(t)), newbasis, getvar(t)) @@ -560,18 +560,18 @@ change_tens_canon(t::AbstractTens) = change_tens(t, CanonicalBasis{getdim(t),elt for OP in (:(tsimplify), :(tfactor), :(tsubs), :(ttrigsimp), :(texpand_trig)) - @eval $OP(t::Tensors.AllTensors{dim,Sym}, args...; kwargs...) where {dim} = + @eval $OP(t::Tensors.AllTensors{dim,T}, args...; kwargs...) where {dim,T<:Sym} = Tensors.get_base(typeof(t))($OP.(Tensors.get_data(t), args...; kwargs...)) - @eval $OP(t::AbstractTens{order,dim,Sym}, args...; kwargs...) where {order,dim} = + @eval $OP(t::AbstractTens{order,dim,T}, args...; kwargs...) where {order,dim,T<:Sym} = Tens($OP(getarray(t), args...; kwargs...), $OP(getbasis(t), args...; kwargs...), getvar(t)) end for OP in (:(tdiff),) - @eval $OP(t::Tensors.AllTensors{dim,Sym}, args...; kwargs...) where {dim} = + @eval $OP(t::Tensors.AllTensors{dim,T}, args...; kwargs...) where {dim,T<:Sym} = Tensors.get_base(typeof(t))($OP.(Tensors.get_data(t), args...; kwargs...)) - @eval $OP(t::AbstractTens{order,dim,Sym}, args...; kwargs...) where {order,dim} = + @eval $OP(t::AbstractTens{order,dim,T}, args...; kwargs...) where {order,dim,T<:Sym} = Tens($OP(getarray(t), args...; kwargs...), getbasis(t), getvar(t)) end -diff_with_basis(t::AbstractTens{order,dim,Sym}, args...; kwargs...) where {order,dim} = +diff_with_basis(t::AbstractTens{order,dim,T}, args...; kwargs...) where {order,dim,T<:Sym} = change_tens(Tens(diff(components_canon(t), args...; kwargs...)), getbasis(t), getvar(t)) @@ -661,14 +661,14 @@ for OP in (:+, :-) @eval @inline function Base.$OP( t1::AllTensOrthogonal{order,dim,T}, t2::UniformScaling{T}, - ) where {order,dim,T<:SymPy.SymbolicObject} + ) where {order,dim,T<:Sym} nt1 = TensOrthonormal(t1) return Tens($OP(getarray(nt1), t2), getbasis(nt1), getvar(nt1)) end @eval @inline function Base.$OP( t1::UniformScaling{T}, t2::AllTensOrthogonal{order,dim,T}, - ) where {order,dim,T<:SymPy.SymbolicObject} + ) where {order,dim,T<:Sym} nt2 = TensOrthonormal(t2) return Tens($OP(t1, getarray(nt2)), getbasis(nt2), getvar(nt2)) end @@ -776,7 +776,7 @@ function Tensors.otimes( t1::AbstractTens{order1,dim}, t2::AbstractTens{order2,dim}, ) where {order1,order2,dim} - if isequal(t1, t2) + if t1 == t2 return otimes(t1) else nt1, nt2 = same_basis(t1, t2) @@ -790,7 +790,7 @@ function Tensors.otimes( t1::TensOrthonormal{order1,dim}, t2::TensOrthonormal{order2,dim}, ) where {order1,order2,dim} - if isequal(t1, t2) + if t1 == t2 return otimes(t1) else nt1, nt2 = same_basis(t1, t2) @@ -825,7 +825,7 @@ function scontract( ec1 = ntuple(i -> i, order1) ec2 = ntuple(i -> order1 - 1 + i, order2) ec3 = (ec1[begin:end-1]..., ec2[begin+1:end]...) - return einsum(EinCode((ec1, ec2), ec3), (AbstractArray{T1}(t1), AbstractArray{T2}(t2))) + return T1.(einsum(EinCode((ec1, ec2), ec3), (AbstractArray{T1}(t1), AbstractArray{T2}(t2)))) end scontract(t1::AbstractArray{T1,1}, t2::AbstractArray{T2,1}) where {T1,T2} = @@ -1211,7 +1211,7 @@ function sotimes( t1::AbstractTens{1,dim}, t2::AbstractTens{1,dim}, ) where {dim} - if isequal(t1, t2) + if t1 == t2 return otimes(t1) else nt1, nt2 = same_basis(t1, t2) @@ -1236,7 +1236,7 @@ function sotimes( t1::TensOrthonormal{1,dim}, t2::TensOrthonormal{1,dim}, ) where {dim} - if isequal(t1, t2) + if t1 == t2 return otimes(t1) else nt1, nt2 = same_basis(t1, t2) diff --git a/src/tens_isotropic.jl b/src/tens_isotropic.jl index bd7ea98..80bfc52 100644 --- a/src/tens_isotropic.jl +++ b/src/tens_isotropic.jl @@ -238,7 +238,7 @@ intrinsic(A::TensISO{4}) = println("(", getdata(A)[1], ") 𝕁 + (", getdata(A)[ intrinsic(A::TensISO{2}) = println("(", getdata(A)[1], ") 𝟏") for OP in (:(tsimplify), :(tfactor), :(tsubs), :(tdiff), :(ttrigsimp), :(texpand_trig)) - @eval $OP(A::TensISO{order,dim,Sym}, args...; kwargs...) where {order,dim} = + @eval $OP(A::TensISO{order,dim}, args...; kwargs...) where {order,dim} = TensISO{dim}($OP(getdata(A), args...; kwargs...)) end @@ -339,13 +339,13 @@ for order ∈ (2, 4) A2::AbstractTensor{$order,dim,T}, ) where {dim,T<:Number} = $OP(A1.λ * one(A2), A2) @eval @inline Base.$OP( - A1::AbstractTensor{$order,dim,Sym}, - A2::UniformScaling{Sym}, - ) where {dim} = $OP(A1, A2.λ * one(A1)) + A1::AbstractTensor{$order,dim,T}, + A2::UniformScaling{T}, + ) where {dim,T<:SymType} = $OP(A1, A2.λ * one(A1)) @eval @inline Base.$OP( - A1::UniformScaling{Sym}, - A2::AbstractTensor{$order,dim,Sym}, - ) where {dim} = $OP(A1.λ * one(A2), A2) + A1::UniformScaling{T}, + A2::AbstractTensor{$order,dim,T}, + ) where {dim,T<:SymType} = $OP(A1.λ * one(A2), A2) end end