/** * This file is part of Gomu. * * Copyright 2016 by Jean Fromentin * * Gomu is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Gomu is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Gomu. If not, see . */ #include "flint.hpp" string display(fmpz* z){ char* disp=fmpz_get_str(NULL,10,z); string res="\033[34m"; res+=disp; res+="\033[0m"; free(disp); return res; } string display(fmpz_poly_struct* P){ slong len=fmpz_poly_length(P); if(len==0) return "\033[34m0\033[0m"; if(len==1) return display(fmpz_poly_get_coeff_ptr(P,0)); string str; fmpz_t temp; fmpz_init(temp); for(slong i=len-1;i>=0;--i){ fmpz* coeff=fmpz_poly_get_coeff_ptr(P,i); int sgn=fmpz_sgn(coeff); //Display coeff if(i==len-1){ if(sgn==-1){ fmpz_neg(temp,coeff); str+="\033[34m-\033[36m"; if(not fmpz_is_one(temp)){ str+=display(temp); str+='.'; } } else{ if(not fmpz_is_one(coeff)){ str+=display(coeff); str+='.'; } } } else{ if(sgn==-1){ fmpz_neg(temp,coeff); str+="\033[34m-\033[0m"; if(i==0) str+=display(temp); else if(not fmpz_is_one(temp)){ str+=display(temp); str+='.'; } } else if(sgn==1){ str+="\033[34m+\033[0m"; if(i==0) str+=display(coeff); else if(not fmpz_is_one(coeff)){ str+=display(coeff); str+='.'; } } } if(i>0 and not fmpz_is_zero(coeff)){ str+="x"; if(i>1){ str+="^\033[35m"+to_string(i)+"\033[0m"; } } } fmpz_clear(temp); return str; } string display(fmpz_poly_factor_struct* F){ string str=""; if(not fmpz_is_one(&F->c)){ str+="\033[34m"+to_string(F->c)+"\033[0m."; } for(ulong i=0;i<(F->num);++i){ if(i!=0) str+='.'; fmpz_poly_struct* P=&F->p[i]; if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) str+=display(P); else{ str+='('; str+=display(P); str+=')'; } slong e=F->exp[i]; if(e!=1) str+="^\033[35m"+to_string(e)+"\033[0m"; } return str; } string display(fmpz_poly_q_struct* R){ char* disp=fmpz_poly_q_get_str_pretty(R,"x"); string res=disp; free(disp); return res; } string display(fmpz_mat_struct* A){ char* disp; string str; for(ulong i=0;ir;++i){ if(i!=0) str+='\n'; str+="\033[37m[\033[0m"; for(long j=0;jc;++j){ if(j!=0) str+=' '; str+=display(get(A,i,j)); } str+="\033[37m]\033[0m"; } return str; } string display(fmpz_poly_mat_struct* A){ string str; for(ulong i=0;ir;++i){ if(i!=0) str+='\n'; str+='['; for(ulong j=0;jc;++j){ if(j!=0) str+=' '; str+=" "+display(get(A,i,j)); } str+=']'; } return str; } ostream& operator<<(ostream& os,const fmpz_mat_t A){ if(A->c==1){ os<<'['<r;++i) os<c;++j) os<<' '<r;++i){ os<c;++j) os<<' '<c)) os<c<<'.'; for(ulong i=0;i<(F->num);++i){ if(i!=0) os<<'.'; fmpz_poly_struct* P=&F->p[i]; if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) os<<&F->p[i]; else os<<'('<<&F->p[i]<<')'; slong e=F->exp[i]; if(e!=1) os<<'^'<=0;--i){ fmpz* coeff=fmpz_poly_get_coeff_ptr(P,i); if(not fmpz_is_zero(coeff)){ if(fmpz_is_one(coeff)){ if(i==0) str+=" + 1"; else if(i==1) str+="x"; else if(i==length-1) str+="x"+to_string(i)+""; else str+=" + x"+to_string(i)+""; } else if(fmpz_is_pm1(coeff)){ if(i==0) str+=" - 1"; else if(i==1) str+=" - x"; else str+=" - x"+to_string(i)+""; } else if(fmpz_sgn(coeff)==-1){ fmpz_neg(temp,coeff); str+=" - "+toHtml(temp); if(i==1) str+=" x"; if(i>1) str+=" x"+to_string(i)+""; } else{ if(i1) str+=" x"+to_string(i)+""; } } } return str; } string toHtml(const fmpz_poly_q_t R){ return toHtml(fmpz_poly_q_numref(R))+"/"+toHtml(fmpz_poly_q_denref(R)); } string toHtml(const fmpz_poly_factor_t F){ string str; if(not fmpz_is_one(&F->c)) str=toHtml(&F->c)+" · "; for(ulong i=0;i<(F->num);++i){ if(i!=0) str+=" · "; fmpz_poly_struct* P=&F->p[i]; if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) str+=toHtml(&F->p[i]); else str+="("+toHtml(&F->p[i])+")"; slong e=F->exp[i]; if(e!=1) str+=""+to_string(e)+""; } return str; } int cmp(fmpz_poly_struct* P,fmpz_poly_struct* Q){ if(fmpz_poly_length(P)==fmpz_poly_length(Q)){ for(slong i=fmpz_poly_degree(P);i>=0;--i){ int c=fmpz_cmp(fmpz_poly_get_coeff_ptr(P,i),fmpz_poly_get_coeff_ptr(Q,i)); if(c!=0) return c; } return 0; } if(fmpz_poly_length(P)num==F2->num){ int c=fmpz_cmp(&F1->c,&F2->c); if(c==0){ for(int i=0;inum;++i){ int d=cmp(&F1->p[i],&F2->p[i]); if(d==0){ if(F1->exp[i]!=F2->exp[i]){ if(F1->exp[i]exp[i]) return -1; return 1; } } return d; } return 0; } return c; } if(F1->numnum) return -1; return 1; } int cmp(fmpz_mat_struct* A,fmpz_mat_struct* B){ if(A->r==B->r){ if(A->c==B->c){ for(slong i=0;ir;++i){ for(slong j=0;jc;++j){ int c=fmpz_cmp(fmpz_mat_entry(A,i,j),fmpz_mat_entry(B,i,j)); if(c!=0) return c; } } return 0; } if(A->cc) return -1; return 1; } if(A->rr) return -1; return 1; } int cmp(fmpz_poly_mat_struct* A,fmpz_poly_mat_struct* B){ if(A->r==B->r){ if(A->c==B->c){ for(slong i=0;ir;++i){ for(slong j=0;jc;++j){ int c=cmp(fmpz_poly_mat_entry(A,i,j),fmpz_poly_mat_entry(B,i,j)); if(c!=0) return c; } } return 0; } if(A->cc) return -1; return 1; } if(A->rr) return -1; return 1; } void dispSage(ostream& os,const fmpz_mat_t A){ os<<"matrix(QQ,"<r<<','<c<<",["; for(ulong i=0;ir;++i){ for(ulong j=0;jc;++j){ if(i!=0 or j!=0) os<<','; os<c==1){ os<<'['<r;++i) os<c;++j) os<<' '<r;++i){ os<c;++j) os<<' '<r,B->c); for(slong i=0;ir;++i){ for(slong j=0;jc;++j){ fmpz_poly_set_coeff_fmpz(fmpz_poly_mat_entry(A,i,j),0,fmpz_mat_entry(B,i,j)); } } }