flint.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "flint.hpp"
  20. string
  21. display(fmpz* z){
  22. char* disp=fmpz_get_str(NULL,10,z);
  23. string res="\033[34m";
  24. res+=disp;
  25. res+="\033[0m";
  26. free(disp);
  27. return res;
  28. }
  29. string
  30. display(fmpz_poly_struct* P){
  31. slong len=fmpz_poly_length(P);
  32. if(len==0) return "\033[34m0\033[0m";
  33. if(len==1) return display(fmpz_poly_get_coeff_ptr(P,0));
  34. string str;
  35. fmpz_t temp;
  36. fmpz_init(temp);
  37. for(slong i=len-1;i>=0;--i){
  38. fmpz* coeff=fmpz_poly_get_coeff_ptr(P,i);
  39. int sgn=fmpz_sgn(coeff);
  40. //Display coeff
  41. if(i==len-1){
  42. if(sgn==-1){
  43. fmpz_neg(temp,coeff);
  44. str+="\033[34m-\033[36m";
  45. if(not fmpz_is_one(temp)){
  46. str+=display(temp);
  47. str+='.';
  48. }
  49. }
  50. else{
  51. if(not fmpz_is_one(coeff)){
  52. str+=display(coeff);
  53. str+='.';
  54. }
  55. }
  56. }
  57. else{
  58. if(sgn==-1){
  59. fmpz_neg(temp,coeff);
  60. str+="\033[34m-\033[0m";
  61. if(i==0) str+=display(temp);
  62. else if(not fmpz_is_one(temp)){
  63. str+=display(temp);
  64. str+='.';
  65. }
  66. }
  67. else if(sgn==1){
  68. str+="\033[34m+\033[0m";
  69. if(i==0) str+=display(coeff);
  70. else if(not fmpz_is_one(coeff)){
  71. str+=display(coeff);
  72. str+='.';
  73. }
  74. }
  75. }
  76. if(i>0 and not fmpz_is_zero(coeff)){
  77. str+="x";
  78. if(i>1){
  79. str+="^\033[35m"+to_string(i)+"\033[0m";
  80. }
  81. }
  82. }
  83. fmpz_clear(temp);
  84. return str;
  85. }
  86. string display(fmpz_poly_factor_struct* F){
  87. string str="";
  88. if(not fmpz_is_one(&F->c)){
  89. str+="\033[34m"+to_string(F->c)+"\033[0m.";
  90. }
  91. for(ulong i=0;i<(F->num);++i){
  92. if(i!=0) str+='.';
  93. fmpz_poly_struct* P=&F->p[i];
  94. if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) str+=display(P);
  95. else{
  96. str+='(';
  97. str+=display(P);
  98. str+=')';
  99. }
  100. slong e=F->exp[i];
  101. if(e!=1) str+="^\033[35m"+to_string(e)+"\033[0m";
  102. }
  103. return str;
  104. }
  105. string
  106. display(fmpz_poly_q_struct* R){
  107. char* disp=fmpz_poly_q_get_str_pretty(R,"x");
  108. string res=disp;
  109. free(disp);
  110. return res;
  111. }
  112. string
  113. display(fmpz_mat_struct* A){
  114. char* disp;
  115. string str;
  116. for(ulong i=0;i<A->r;++i){
  117. if(i!=0) str+='\n';
  118. str+="\033[37m[\033[0m";
  119. for(long j=0;j<A->c;++j){
  120. if(j!=0) str+=' ';
  121. str+=display(get(A,i,j));
  122. }
  123. str+="\033[37m]\033[0m";
  124. }
  125. return str;
  126. }
  127. string
  128. display(fmpz_poly_mat_struct* A){
  129. string str;
  130. for(ulong i=0;i<A->r;++i){
  131. if(i!=0) str+='\n';
  132. str+='[';
  133. for(ulong j=0;j<A->c;++j){
  134. if(j!=0) str+=' ';
  135. str+=" "+display(get(A,i,j));
  136. }
  137. str+=']';
  138. }
  139. return str;
  140. }
  141. ostream& operator<<(ostream& os,const fmpz_mat_t A){
  142. if(A->c==1){
  143. os<<'['<<get(A,0,0)<<']';
  144. for(ulong i=1;i<A->r;++i) os<<endl<<'['<<get(A,i,0)<<']';
  145. }
  146. else{
  147. os<<'['<<get(A,0,0);
  148. for(ulong j=1;j<A->c;++j) os<<' '<<get(A,0,j);
  149. os<<']';
  150. for(ulong i=1;i<A->r;++i){
  151. os<<endl<<'['<<get(A,i,0);
  152. for(ulong j=1;j<A->c;++j) os<<' '<<get(A,i,j);
  153. os<<']';
  154. }
  155. }
  156. return os;
  157. }
  158. ostream& operator<<(ostream& os,const fmpz_poly_factor_t F){
  159. if(not fmpz_is_one(&F->c)) os<<F->c<<'.';
  160. for(ulong i=0;i<(F->num);++i){
  161. if(i!=0) os<<'.';
  162. fmpz_poly_struct* P=&F->p[i];
  163. if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) os<<&F->p[i];
  164. else os<<'('<<&F->p[i]<<')';
  165. slong e=F->exp[i];
  166. if(e!=1) os<<'^'<<e;
  167. }
  168. return os;
  169. }
  170. string toHtml(const fmpz_poly_t P){
  171. fmpz_t temp;
  172. fmpz_init(temp);
  173. string str;
  174. slong length=fmpz_poly_length(P);
  175. if(length==0) return "0";
  176. if(length==1){
  177. fmpz* coeff=fmpz_poly_get_coeff_ptr(P,0);
  178. return toHtml(coeff);
  179. }
  180. for(slong i=length-1;i>=0;--i){
  181. fmpz* coeff=fmpz_poly_get_coeff_ptr(P,i);
  182. if(not fmpz_is_zero(coeff)){
  183. if(fmpz_is_one(coeff)){
  184. if(i==0) str+=" + 1";
  185. else if(i==1) str+="x";
  186. else if(i==length-1) str+="x<sup>"+to_string(i)+"</sup>";
  187. else str+=" + x<sup>"+to_string(i)+"</sup>";
  188. }
  189. else if(fmpz_is_pm1(coeff)){
  190. if(i==0) str+=" - 1";
  191. else if(i==1) str+=" - x";
  192. else str+=" - x<sup>"+to_string(i)+"</sup>";
  193. }
  194. else if(fmpz_sgn(coeff)==-1){
  195. fmpz_neg(temp,coeff);
  196. str+=" - "+toHtml(temp);
  197. if(i==1) str+=" x";
  198. if(i>1) str+=" x<sup>"+to_string(i)+"</sup>";
  199. }
  200. else{
  201. if(i<length-1) str+=" + ";
  202. str+=toHtml(coeff);
  203. if(i==1) str+=" x ";
  204. if(i>1) str+=" x<sup>"+to_string(i)+"</sup>";
  205. }
  206. }
  207. }
  208. return str;
  209. }
  210. string toHtml(const fmpz_poly_q_t R){
  211. return toHtml(fmpz_poly_q_numref(R))+"/"+toHtml(fmpz_poly_q_denref(R));
  212. }
  213. string toHtml(const fmpz_poly_factor_t F){
  214. string str;
  215. if(not fmpz_is_one(&F->c)) str=toHtml(&F->c)+" &middot; ";
  216. for(ulong i=0;i<(F->num);++i){
  217. if(i!=0) str+=" &middot; ";
  218. fmpz_poly_struct* P=&F->p[i];
  219. if(fmpz_is_zero(fmpz_poly_get_coeff_ptr(P,0))) str+=toHtml(&F->p[i]);
  220. else str+="("+toHtml(&F->p[i])+")";
  221. slong e=F->exp[i];
  222. if(e!=1) str+="<sup>"+to_string(e)+"</sup>";
  223. }
  224. return str;
  225. }
  226. int cmp(fmpz_poly_struct* P,fmpz_poly_struct* Q){
  227. if(fmpz_poly_length(P)==fmpz_poly_length(Q)){
  228. for(slong i=fmpz_poly_degree(P);i>=0;--i){
  229. int c=fmpz_cmp(fmpz_poly_get_coeff_ptr(P,i),fmpz_poly_get_coeff_ptr(Q,i));
  230. if(c!=0) return c;
  231. }
  232. return 0;
  233. }
  234. if(fmpz_poly_length(P)<fmpz_poly_length(Q)) return -1;
  235. return 1;
  236. }
  237. int cmp(fmpz_poly_q_struct* R,fmpz_poly_q_struct* Q){
  238. int c=cmp(fmpz_poly_q_denref(R),fmpz_poly_q_denref(Q));
  239. if(c!=0) return c;
  240. return cmp(fmpz_poly_q_numref(R),fmpz_poly_q_numref(Q));
  241. }
  242. int cmp(fmpz_poly_factor_struct* F1,fmpz_poly_factor_struct* F2){
  243. if(F1->num==F2->num){
  244. int c=fmpz_cmp(&F1->c,&F2->c);
  245. if(c==0){
  246. for(int i=0;i<F1->num;++i){
  247. int d=cmp(&F1->p[i],&F2->p[i]);
  248. if(d==0){
  249. if(F1->exp[i]!=F2->exp[i]){
  250. if(F1->exp[i]<F2->exp[i]) return -1;
  251. return 1;
  252. }
  253. }
  254. return d;
  255. }
  256. return 0;
  257. }
  258. return c;
  259. }
  260. if(F1->num<F2->num) return -1;
  261. return 1;
  262. }
  263. int cmp(fmpz_mat_struct* A,fmpz_mat_struct* B){
  264. if(A->r==B->r){
  265. if(A->c==B->c){
  266. for(slong i=0;i<A->r;++i){
  267. for(slong j=0;j<A->c;++j){
  268. int c=fmpz_cmp(fmpz_mat_entry(A,i,j),fmpz_mat_entry(B,i,j));
  269. if(c!=0) return c;
  270. }
  271. }
  272. return 0;
  273. }
  274. if(A->c<B->c) return -1;
  275. return 1;
  276. }
  277. if(A->r<B->r) return -1;
  278. return 1;
  279. }
  280. int cmp(fmpz_poly_mat_struct* A,fmpz_poly_mat_struct* B){
  281. if(A->r==B->r){
  282. if(A->c==B->c){
  283. for(slong i=0;i<A->r;++i){
  284. for(slong j=0;j<A->c;++j){
  285. int c=cmp(fmpz_poly_mat_entry(A,i,j),fmpz_poly_mat_entry(B,i,j));
  286. if(c!=0) return c;
  287. }
  288. }
  289. return 0;
  290. }
  291. if(A->c<B->c) return -1;
  292. return 1;
  293. }
  294. if(A->r<B->r) return -1;
  295. return 1;
  296. }
  297. void dispSage(ostream& os,const fmpz_mat_t A){
  298. os<<"matrix(QQ,"<<A->r<<','<<A->c<<",[";
  299. for(ulong i=0;i<A->r;++i){
  300. for(ulong j=0;j<A->c;++j){
  301. if(i!=0 or j!=0) os<<',';
  302. os<<get(A,i,j);
  303. }
  304. }
  305. os<<"])";
  306. }
  307. ostream& operator<<(ostream& os,const fmpz_poly_mat_t A){
  308. if(A->c==1){
  309. os<<'['<<get(A,0,0)<<']';
  310. for(ulong i=1;i<A->r;++i) os<<endl<<'['<<get(A,i,0)<<']';
  311. }
  312. else{
  313. os<<'['<<get(A,0,0);
  314. for(ulong j=1;j<A->c;++j) os<<' '<<get(A,0,j);
  315. os<<']';
  316. for(ulong i=1;i<A->r;++i){
  317. os<<endl<<'['<<get(A,i,0);
  318. for(ulong j=1;j<A->c;++j) os<<' '<<get(A,i,j);
  319. os<<']';
  320. }
  321. }
  322. return os;
  323. }
  324. void fmpz_poly_mat_init_set(fmpz_poly_mat_t A,fmpz_mat_t B){
  325. fmpz_poly_mat_init(A,B->r,B->c);
  326. for(slong i=0;i<B->r;++i){
  327. for(slong j=0;j<B->c;++j){
  328. fmpz_poly_set_coeff_fmpz(fmpz_poly_mat_entry(A,i,j),0,fmpz_mat_entry(B,i,j));
  329. }
  330. }
  331. }