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

Converting from Nemo.fmpq_mpoly to HomotopyContinuation.Expression #520

Open
iliailmer opened this issue Nov 16, 2022 · 3 comments
Open

Comments

@iliailmer
Copy link

I have an array of polynomials which are obtained through Nemo CAS. Is there a way to convert them into a HomotopyContinuation.Expression type so that I can find roots of the polynomial system? I've tried a few different things but no luck so far.

Thanks.

@PBrdng
Copy link
Collaborator

PBrdng commented Nov 16, 2022

I don't think there is a direct way so far. Sorry!

@saschatimme
Copy link
Member

saschatimme commented Nov 16, 2022

There seems to be a way to extract the exponent vectors and coefficient of the polynomial (see https://nemocas.github.io/AbstractAlgebra.jl/stable/mpolynomial/#Basic-manipulation).

If you have the exponents and the coefficients in Matrix and vector form you can rebuild the polynomial.
Here is an example.

julia> @var x y z
(x, y, z)

julia> E = [1 2 3; 0 2 1; 3 5 6; 1 0 2]
4×3 Matrix{Int64}:
 1  2  3
 0  2  1
 3  5  6
 1  0  2

julia> c = [5, -3, 2, 3]
4-element Vector{Int64}:
  5
 -3
  2
  3

julia> sum(coeff * prod([x,y,z] .^ row) for (row, coeff) in zip(eachrow(E), c))
3*x*z^2 - 3*y^2*z + 5*x*y^2*z^3 + 2*x^3*y^5*z^6

A little bit clumsy but should get the job done

@iliailmer
Copy link
Author

Thank you, @saschatimme @PBrdng! I also found another way using Meta:

function nemo2hc(expr_tree::Union{Expr,Symbol})
    #traverse expr_tree
    if typeof(expr_tree) == Symbol
        return HomotopyContinuation.variables(expr_tree)[1]
    end
    if typeof(expr_tree) == Expr
        if expr_tree.head == :call
            if expr_tree.args[1] in [:+, :-, :*, :/, :^]
                return reduce(eval(expr_tree.args[1]), map(nemo2hc, expr_tree.args[2:end]))
            end
        end
    end
end

called as

nemo2hc(Meta.parse(string(poly)))

I wonder which way will be faster.

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