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

How are variables attributed to polynomials? #79

Open
tweisser opened this issue Dec 29, 2020 · 3 comments
Open

How are variables attributed to polynomials? #79

tweisser opened this issue Dec 29, 2020 · 3 comments

Comments

@tweisser
Copy link
Contributor

using DynamicPolynomials

@polyvar x[1:4]

polys = [1 0 1 0; 0 1 0 1]*x
variables.(polys)
"""
2-element Array{Array{PolyVar{true},1},1}:
 [x₁, x₃]
 [x₁, x₂, x₃, x₄]
"""

Why is the first polynomial a polynomial in two variables whereas the second polynomial has all four variables?

@blegat
Copy link
Member

blegat commented Feb 3, 2021

Indeed, it should be

2-element Array{Array{PolyVar{true},1},1}:
 [x₁, x₂, x₃, x₄]
 [x₁, x₂, x₃, x₄]

@manuelbb-upb
Copy link
Contributor

One fix is to overwrite the method _multconstant_to! (exported by MultivariatePolynomials, abbreviated as MP).
The signature reads as _multconstant_to!(output, α, f, p) where output is a polynomial-like buffer.
By default, if α == 0, the method performs operate_to!(zero, output).
This creates an empty polynomial which only takes into account the variables already stored in output but disregards the variable(s) of p.

So in the file mult.jl I did define

function MP._multconstant_to!(output, α, f, p :: DMonomialLike)
    MP.mapcoefficientsnz_to!(output, f, p)
end

Now all variables are returned by variables.(polys).

@manuelbb-upb
Copy link
Contributor

Ok, this leads to the tests of MP failing. I was looking at the right place but misinterpreted the output buffer.
By the above method, zero coefficients get introduced.
This seems to work fine though:

function MP._multconstant_to!(output::Polynomial, α, f, p :: DMonomialLike)
    if iszero(α)
        empty!(output.a)
        empty!(output.x.vars)
        push!(output.x.vars, variables(p)...)
        empty!(output.x.Z)
        return output
    else
        MP.mapcoefficientsnz_to!(output, f, p)
    end
end

(effectively changing output to zero polynomial)

blegat pushed a commit that referenced this issue Apr 9, 2021
* fix of issue #81 (merged commits)

* 1) fix of issue #79; also extended  to work with terms

* concerning #79: Now mutating output buffer; concerning #81: removed overspecialization of last commit - that broke tests too

* Implemented review suggestions;
added tests from issue descriptions

* `.` instead of accidental `comma` in
file `mult.jl`, function `_multconstant_to!`

Co-authored-by: manuelbb-upb <[email protected]>
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

3 participants