Skip to content

[bug] discriminant uses default context for degree-1 polynomials #95

@daniel-larraz

Description

@daniel-larraz

When p has degree 1, discriminant(p) returns a polynomial built in the default context rather than in p's context:

Polynomial discriminant(const Polynomial& p) {
if (degree(p) == 1) {
// Derivative is constant, making the resultant trivial (and resultant()
// does not cope with that)
return Polynomial(Integer(1));
}

This can cause an assertion failure when the returned polynomial is combined with polynomials from the original context:

#include <poly/polyxx.h>
#include <iostream>

int main()
{
    poly::Context ctx;
    poly::Variable x(ctx, "x");
    poly::Polynomial p1(ctx, x);
    poly::Polynomial d = poly::discriminant(p1); // d built in the default context
    poly::Polynomial p2 = p1 + d; // assertion failure!
    std::cout << p2 << std::endl;
    return 0;
}

A possible fix is to add a constructor that takes a C context and an integer:

Polynomial::Polynomial(const lp_polynomial_context_t* c, Integer i)
      : mPoly(lp_polynomial_new(c), polynomial_deleter) {
    lp_polynomial_construct_simple(get_internal(), c,
                                   i.get_internal(), lp_variable_null, 0);
  }

Then, construct the polynomial in the desired context:

return Polynomial(detail::context(p), Integer(1));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions