Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type stability of adjoint(::Sym) #397

Closed
ranocha opened this issue Dec 7, 2020 · 2 comments
Closed

Type stability of adjoint(::Sym) #397

ranocha opened this issue Dec 7, 2020 · 2 comments

Comments

@ranocha
Copy link
Contributor

ranocha commented Dec 7, 2020

Currently, Julia v1.5.3 with SymPy v1.0.35 is not able to infer adjoint(::Sym) correctly as ::Sym. As a result, the return type of multiplying adjoint matrices of Sym by matrices of Sym are matrices with eltype Any. I recognized this issue since Matrix{Any} is really printed worse than Matrix{Sym} in Jupyter. Example:

julia> using SymPy

julia> A = ones(Sym, 1, 1)
1×1 Array{Sym,2}:
 1

julia> A * A # is inferred correctly
1×1 Array{Sym,2}:
 1

julia> A * A' # is inferred with eltype Any
1×1 Array{Any,2}:
 1

julia> A' * A # is inferred with eltype Any
1×1 Array{Any,2}:
 1

julia> adjoint(one(Sym)) |> typeof
Sym

julia> @code_warntype adjoint(one(Sym))
Variables
  #self#::Core.Compiler.Const(adjoint, false)
  ex::Sym
  args::Tuple{}

Body::Any
1%1 = Core.NamedTuple()::Core.Compiler.Const(NamedTuple(), false)
│   %2 = Base.pairs(%1)::Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(), false)
│   %3 = Core.tuple(%2, #self#, ex)::Core.Compiler.PartialStruct(Tuple{Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},typeof(adjoint),Sym}, Any[Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(), false), Core.Compiler.Const(adjoint, false), Sym])%4 = Core._apply_iterate(Base.iterate, SymPy.:(var"#adjoint#151"), %3, args)::Any
└──      return %4

Could an additional definition with type hint for the method adjoint(::Sym)::Sym help here?

@jverzani
Copy link
Collaborator

jverzani commented Dec 7, 2020

Yes, that seems like the right fix. Thanks for the report.

jverzani added a commit to jverzani/SymPy.jl that referenced this issue Dec 7, 2020
@ranocha
Copy link
Contributor Author

ranocha commented Dec 7, 2020

Thanks for this fast fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants