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

leading coefficient with respect to a given variable #171

Open
lucaferranti opened this issue Aug 9, 2021 · 5 comments
Open

leading coefficient with respect to a given variable #171

lucaferranti opened this issue Aug 9, 2021 · 5 comments

Comments

@lucaferranti
Copy link

lucaferranti commented Aug 9, 2021

Browsing the source code, I didn't find a function to compute the "leading coefficient" of a multivariate polynomial with respect to a given variable, for example if I have the polynomial x^2*y + x*y the expected behavior would be

leadingcoefficient(x^2*y + x*y, x) = y
leadingcoefficient(x^2*y + x*y, y) = x^2 + x

(sometimes that's also called initial of the polynomial)

this functionality is used e.g. when computing the pseudodivision with multivariate polynomials, which is used e.g. in the Ritt-Wu method of characteristic sets.

Would it be a nice addition to the package?

@lucaferranti
Copy link
Author

lucaferranti commented Aug 9, 2021

Currently I have been using the following patch, but since I'm not very familiar with the package internals, I suspect my solution is way far from optimal 😄

function LC!(p, var)

    d = maxdegree(p, var)
    d == 0 && return zero(p)

    idx = findfirst(x -> x == var, p.x.vars)
    lc = zero(p)
    @inbounds  for term in p
        if degree(term, var) == d
            term.x.z[idx] = 0
            lc += term
        end
    end
    return lc
end

LC(p, var) = LC!(copy(p), var)

@lucaferranti
Copy link
Author

bump 😄

@blegat
Copy link
Member

blegat commented Sep 21, 2021

Here is a one line:

coefficient(leadingterm(isolate_variable(p, var)))

but it's not the fastest as it computes the coefficient for each degree of var, not just the maximal one.
This might be faster:

function LC(p, var)
    d = maxdegree(p, var)
    d == 0 && return zero(p)
    polynomial([subs(t, var => 1) for t in terms(p) if degree(t, var) == d)
end

Your suggestion uses internals of DynamicPolynomials and will be slow because of the line: lc += term. It's best to either mutate lc using MutableArithmetics or to build a list of term and then turn it into a polynomial with polynomial.

@lucaferranti
Copy link
Author

First of all, apologies for never answering you after even bumping the issue, it wasn't very polite from my side. 🤦‍♂️

Anyway, your suggested implementation seemed to give a good speedup to my naive approach! Thanks!

@blegat
Copy link
Member

blegat commented Jan 16, 2022

No worry, I'll leave the issue open as it might be a nice additional feature of the package.

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