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

Substitution of matrix values #117

Open
kbarros opened this issue Sep 30, 2022 · 4 comments
Open

Substitution of matrix values #117

kbarros opened this issue Sep 30, 2022 · 4 comments
Labels

Comments

@kbarros
Copy link

kbarros commented Sep 30, 2022

How can I make a polynomial matrix-valued? I tried this:

using DynamicPolynomials
@ncpolyvar u
subs(u, u => 2.0) # works
subs(u, u => ones(3,3)) # ERROR: MethodError: no method matching one(::Type{Any})

The problem may be related to the promote_rules defined in MultivariatePolynomials?

import MultivariatePolynomials as MP
promote_type(Float64, DynamicPolynomials.PolyVar{false}) # Term{false, Float64}
promote_type(Matrix{Float64}, DynamicPolynomials.PolyVar{false}) # Term{false, Any}

I suspect the latter case would need to be Term{false, Matrix{Float64}}. Help would be appreciated!

@blegat blegat added the bug label Sep 30, 2022
@blegat
Copy link
Member

blegat commented Sep 30, 2022

Thanks for reporting this bug. I'm afk right now so a stacktrace could help

@kbarros
Copy link
Author

kbarros commented Sep 30, 2022

Input:

using DynamicPolynomials
@ncpolyvar u
subs(u, u => ones(3,3))

Output stack trace:

ERROR: MethodError: no method matching one(::Type{Any})
Closest candidates are:
  one(::Type{Union{Missing, T}}) where T at missing.jl:105
  one(::Union{Type{T}, T}) where T<:AbstractString at strings/basic.jl:262
  one(::Union{Type{P}, P}) where P<:Dates.Period at ~/.julia/juliaup/julia-1.8.1+0.aarch64/share/julia/stdlib/v1.8/Dates/src/periods.jl:54
  ...
Stacktrace:
  [1] one(#unused#::Type{Any})
    @ Base ./missing.jl:106
  [2] convert(#unused#::Type{Term{false, Any}}, x::Monomial{false})
    @ DynamicPolynomials ~/.julia/packages/DynamicPolynomials/juS7t/src/term.jl:18
  [3] convert(#unused#::Type{Term{false, Any}}, x::PolyVar{false})
    @ DynamicPolynomials ~/.julia/packages/DynamicPolynomials/juS7t/src/term.jl:24
  [4] setindex!(A::Vector{Term{false, Any}}, x::PolyVar{false}, i1::Int64)
    @ Base ./array.jl:966
  [5] _unsafe_copyto!(dest::Vector{Term{false, Any}}, doffs::Int64, src::Vector{PolyVar{false}}, soffs::Int64, n::Int64)
    @ Base ./array.jl:253
  [6] unsafe_copyto!
    @ ./array.jl:307 [inlined]
  [7] _copyto_impl!
    @ ./array.jl:331 [inlined]
  [8] copyto!
    @ ./array.jl:317 [inlined]
  [9] copyto!
    @ ./array.jl:343 [inlined]
 [10] copy!(dst::Vector{Term{false, Any}}, src::Vector{PolyVar{false}})
    @ Base ./abstractarray.jl:887
 [11] copy!
    @ ~/.julia/juliaup/julia-1.8.1+0.aarch64/share/julia/stdlib/v1.8/Future/src/Future.jl:25 [inlined]
 [12] _subsmap
    @ ~/.julia/packages/DynamicPolynomials/juS7t/src/subs.jl:54 [inlined]
 [13] subsmap
    @ ~/.julia/packages/DynamicPolynomials/juS7t/src/subs.jl:59 [inlined]
 [14] substitute(st::MultivariatePolynomials.Subs, p::PolyVar{false}, s::Tuple{Pair{PolyVar{false}, Matrix{Float64}}})
    @ DynamicPolynomials ~/.julia/packages/DynamicPolynomials/juS7t/src/subs.jl:105
 [15] subs(p::PolyVar{false}, s::Pair{PolyVar{false}, Matrix{Float64}})
    @ MultivariatePolynomials ~/.julia/packages/MultivariatePolynomials/1bIGc/src/substitution.jl:113
 [16] top-level scope
    @ ~/Desktop/scratch.jl:43

I suspect that the fundamental problem is this:

promote_type(Matrix{Float64}, DynamicPolynomials.PolyVar{false}) == Term{false, Any}

Somehow the type Matrix{Float64} is getting lost.

@blegat
Copy link
Member

blegat commented Sep 30, 2022

Indeed, as a workaround, you can try u(u => ones(3,3)) which assumes that all variables are substituted which is the case here

@kbarros
Copy link
Author

kbarros commented Sep 30, 2022

Perfect, that solves the problem for me.

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

No branches or pull requests

2 participants