fmpq_mpoly evaluate

29 views
Skip to first unread message

Hjalte Frellesvig

unread,
Apr 17, 2025, 3:29:28 AMApr 17
to flint-devel
Dear all.
I am trying to evaluate the polynomial "1/2 + 1/3*x - 1/4*y" in (x,y) = (5,6)
using fmpq_mpoly_evaluate_all_fmpq
Flint claims the result is 11/12 whereas I would strongly claim the result should be 2/3.
I copy a somewhat minimal code example below.
Is flint making a mistake here or is the mistake on my side?
I should say that for more complicated cases I get not just wrong results but segmentation faults.

I hope you can help.
Best wishes,
Hjalte



void testeval(){
   
    int i, ok;
    fmpq_mpoly_ctx_t ctx;
    fmpq_mpoly_t poly;
   
    fmpq_t out, exp;
    fmpq **in;
   
    const char *varnamesor[2] = {"x", "y"};
    const char **varnames;
    varnames = varnamesor;
   
    fmpq_mpoly_ctx_init(ctx, 2, ORD_DEGLEX);
    fmpq_mpoly_init(poly, ctx);
  
    fmpq_mpoly_set_str_pretty(poly, "1/2 + 1/3*x - 1/4*y", varnames, ctx);
   
    fmpq_mpoly_print_pretty(poly, varnames, ctx);  // works fine
    printf("\n");
   
    in = malloc(2*sizeof(fmpq_t));
    for(i=0; i<2; ++i){
        fmpq_init((*in)+i);
    }
    fmpq_init(out);
    fmpq_init(exp);
   
    fmpq_set_si((*in)+0, 5, 1);
    fmpq_set_si((*in)+1, 6, 1);
    for(i=0; i<2; ++i){
        printf("%d: ", i);
        fmpq_print((*in)+i);     // works fine
        printf("\n");
    }
   
    ok = fmpq_mpoly_evaluate_all_fmpq(out, poly, in, ctx);
   
    printf("ok = %d\n", ok);     // it claims it's fine
   
    printf("Result = ");
    fmpq_print(out);             // It gives 11/12
    printf("\n");
   
    //  5/3 - 6/4 + 1/2  =  2/3
    fmpq_set_si(exp, 2, 3);
    printf("I expect ");
    fmpq_print(exp);
    printf("\n");
   
    for(i=0; i<2; ++i){
        fmpq_clear((*in)+i);
    }
    free(in);
    fmpq_clear(out);
    fmpq_clear(exp);
    fmpq_mpoly_clear(poly, ctx);
    fmpq_mpoly_ctx_clear(ctx);
}

Albin Ahlbäck

unread,
Apr 17, 2025, 4:26:58 AMApr 17
to flint...@googlegroups.com, Hjalte Frellesvig
It is a mistake on your side. Here is a working version.

#include <stdio.h>
#include <stdlib.h>
#include <flint/fmpq_mpoly.h>

int main(void){
int i, ok;
fmpq_mpoly_ctx_t ctx;
fmpq_mpoly_t poly;

fmpq_t out, exp;
fmpq **in;

const char *varnamesor[2] = {"x", "y"};
const char **varnames;
varnames = varnamesor;

fmpq_mpoly_ctx_init(ctx, 2, ORD_DEGLEX);
fmpq_mpoly_init(poly, ctx);

fmpq_mpoly_set_str_pretty(poly, "1/2 + 1/3*x - 1/4*y", varnames, ctx);

fmpq_mpoly_print_pretty(poly, varnames, ctx);
printf("\n");

in = malloc(2 * sizeof(fmpq *));
for(i=0; i<2; ++i){
in[i] = malloc(sizeof(fmpq));
fmpq_init(in[i]);
}
fmpq_init(out);
fmpq_init(exp);

fmpq_set_si(in[0], 5, 1);
fmpq_set_si(in[1], 6, 1);
for(i=0; i<2; ++i){
printf("%d: ", i);
fmpq_print(in[i]);
printf("\n");
}

ok = fmpq_mpoly_evaluate_all_fmpq(out, poly, in, ctx);

printf("ok = %d\n", ok);

printf("Result = ");
fmpq_print(out);
printf("\n");

// 5/3 - 6/4 + 1/2 = 2/3
fmpq_set_si(exp, 2, 3);
printf("I expect ");
fmpq_print(exp);
printf("\n");

for(i=0; i<2; ++i){
fmpq_clear(in[i]);
free(in[i]);
}
free(in);
fmpq_clear(out);
fmpq_clear(exp);
fmpq_mpoly_clear(poly, ctx);
fmpq_mpoly_ctx_clear(ctx);
}

Best,
Albin
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "flint-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to flint-devel...@googlegroups.com <mailto:flint-
> devel+un...@googlegroups.com>.
> To view this discussion, visit https://groups.google.com/d/msgid/flint-
> devel/9a9b711f-d97e-4dad-bda9-5c2995f4bb16n%40googlegroups.com <https://
> groups.google.com/d/msgid/flint-devel/9a9b711f-d97e-4dad-
> bda9-5c2995f4bb16n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Hjalte Frellesvig

unread,
Apr 18, 2025, 6:44:56 AMApr 18
to Albin Ahlbäck, flint...@googlegroups.com
Thank you!
Now I am feeling like a noob.
Best, Hjalte
Reply all
Reply to author
Forward
0 new messages