8 static map<pid_t, Fermat> fermat_map;
10 if((fermat_map.find(pid)==fermat_map.end())) {
11 fermat_map[pid].Init();
12 fermat_map[pid].vmax = 0;
14 return fermat_map[pid];
25 bool use_ncheck =
false;
28 int &v_max = fermat.
vmax;
32 expr_in = expr_in.to_rational(map_rat);
36 map<ex,long long,ex_is_less> s2c;
37 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
38 if(is_a<symbol>(*i)) s2c[*i]++;
40 exvector sv1, sv2, sv3;
43 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
44 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
45 else sv1.push_back(lst{kv.second, kv.first});
50 for(
auto sv : sv1) rep_vs.append(sv.op(1));
51 for(
auto sv : sv2) rep_vs.append(sv.op(1));
52 for(
auto sv : sv3) rep_vs.append(sv.op(1));
60 for(
auto vi : rep_vs) {
61 auto name =
"v" + to_string(fvi);
68 nn_map[s] = nn_pi1/nn_pi2;
73 cout << rep_vs << endl;
74 throw Error(
"Fermat: Too many variables.");
77 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
84 ex nn_chk=0, num, den;
86 if(!is_a<add>(expr_in)) item = lst{ expr_in };
87 else for(
auto ii : expr_in) item.append(ii);
89 if(item.nops()>999999) _fermat_using_array = 0;
90 if(_fermat_using_array) ss <<
"Array m[" << item.nops() <<
"];" << endl;
91 else ss <<
"res:=0;" << endl;
96 for(
int i=0; i<item.nops(); i++) {
97 ex tt = item.op(i).subs(v2f, subs_options::no_pattern);
98 if(use_ncheck) nn_chk += tt.subs(nn_map, subs_options::no_pattern);
99 if(_fermat_using_array) ss <<
"m[" << (i+1) <<
"]:=";
101 ss << tt <<
";" << endl;
102 if(!_fermat_using_array) ss <<
"res:=*res+*item;" << endl;
107 if(_fermat_using_array) {
108 if(_fermat_using_array==1) ss <<
"res:=Sumup([m]);" << endl;
109 else if(_fermat_using_array==2) ss <<
"res:=Sigma<i=1,"<<item.nops()<<
">(*m[i]);" << endl;
111 ss <<
"n:=" << item.nops() <<
";" << endl;
112 ss <<
"while n>1 do n2:=n\\2; for i=1,n2 do m[i]:=*m[i]+*m[n+1-i] od; &_G; if (n|2)=0 then n:=n2 else n:=n2+1 fi od;" << endl;
113 ss <<
"res:=*m[1];" << endl;
120 static string bstr(
"[-begin-]"), estr(
"[-end-]");
121 ss <<
"&(U=1);" << endl;
122 ss <<
"!('" <<bstr<<
"','{',Numer(^res),',',Denom(^res),'}','" <<estr<<
"')" << endl;
123 auto ostr = fermat.
Execute(ss.str());
128 ss <<
"&(U=0);" << endl;
129 if(_fermat_using_array) ss <<
"@(res,[m]);" << endl;
130 else ss <<
"@(res,item);" << endl;
131 ss <<
"&_G;" << endl;
137 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"fermat_together: last char is NOT 0.");
138 ostr = ostr.substr(0, ostr.length()-1);
139 auto cpos = ostr.find(bstr);
140 if(cpos==string::npos)
throw Error(bstr+
" NOT Found.");
141 ostr = ostr.substr(cpos+bstr.length(),string::npos);
142 cpos = ostr.find(estr);
143 if(cpos==string::npos)
throw Error(estr+
" NOT Found.");
144 ostr = ostr.substr(0,cpos);
149 auto ret = fp.
Read(ostr);
153 else den = ret.op(1);
157 auto nn_ret =
subs(num/den,nn_map);
158 if(nn_chk-nn_ret!=0) {
159 cout << nn_chk <<
" : " << nn_ret << endl;
160 throw Error(
"fermat_together: N Check Failed.");
164 num = num.subs(f2v,subs_options::no_pattern).subs(map_rat,
nopat);
165 den = den.subs(f2v,subs_options::no_pattern).subs(map_rat,
nopat);
166 return lst{num, den};
172 bool use_ncheck =
false;
175 int &v_max = fermat.
vmax;
179 expr_in = expr_in.to_polynomial(map_rat);
183 map<ex,long long,ex_is_less> s2c;
184 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
185 if(is_a<symbol>(*i)) s2c[*i]++;
187 exvector sv1, sv2, sv3;
190 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
191 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
192 else sv1.push_back(lst{kv.second, kv.first});
197 for(
auto sv : sv1) rep_vs.append(sv.op(1));
198 for(
auto sv : sv2) rep_vs.append(sv.op(1));
199 for(
auto sv : sv3) rep_vs.append(sv.op(1));
207 for(
auto vi : rep_vs) {
208 auto name =
"v" + to_string(fvi);
215 nn_map[s] = nn_pi1/nn_pi2;
220 cout << rep_vs << endl;
221 throw Error(
"Fermat: Too many variables.");
224 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
233 if(!is_a<add>(expr_in)) item = lst{ expr_in };
234 else for(
auto ii : expr_in) item.append(ii);
236 if(item.nops()>999999) _fermat_using_array = 0;
237 if(_fermat_using_array) ss <<
"Array m[" << item.nops() <<
"];" << endl;
238 else ss <<
"res:=0;" << endl;
243 for(
int i=0; i<item.nops(); i++) {
244 ex tt = item.op(i).subs(v2f, subs_options::no_pattern);
245 if(use_ncheck) nn_chk += tt.subs(nn_map, subs_options::no_pattern);
246 if(_fermat_using_array) ss <<
"m[" << (i+1) <<
"]:=";
248 ss << tt <<
";" << endl;
249 if(!_fermat_using_array) ss <<
"res:=*res+*item;" << endl;
254 if(_fermat_using_array) {
255 if(_fermat_using_array==1) ss <<
"res:=Sumup([m]);" << endl;
256 else if(_fermat_using_array==2) ss <<
"res:=Sigma<i=1,"<<item.nops()<<
">(*m[i]);" << endl;
258 ss <<
"n:=" << item.nops() <<
";" << endl;
259 ss <<
"while n>1 do n2:=n\\2; for i=1,n2 do m[i]:=*m[i]+*m[n+1-i] od; &_G; if (n|2)=0 then n:=n2 else n:=n2+1 fi od;" << endl;
260 ss <<
"res:=*m[1];" << endl;
267 static string bstr(
"[-begin-]"), estr(
"[-end-]");
268 ss <<
"&(U=1);" << endl;
269 ss <<
"!('" <<bstr<<
"',res,'" <<estr<<
"')" << endl;
270 auto ostr = fermat.
Execute(ss.str());
275 ss <<
"&(U=0);" << endl;
276 if(_fermat_using_array) ss <<
"@(res,[m]);" << endl;
277 else ss <<
"@(res,item);" << endl;
278 ss <<
"&_G;" << endl;
284 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"fermat_together: last char is NOT 0.");
285 ostr = ostr.substr(0, ostr.length()-1);
286 auto cpos = ostr.find(bstr);
287 if(cpos==string::npos)
throw Error(bstr+
" NOT Found.");
288 ostr = ostr.substr(cpos+bstr.length(),string::npos);
289 cpos = ostr.find(estr);
290 if(cpos==string::npos)
throw Error(estr+
" NOT Found.");
291 ostr = ostr.substr(0,cpos);
296 auto ret = fp.
Read(ostr);
300 auto nn_ret =
subs(ret,nn_map);
301 if(nn_chk-nn_ret!=0) {
302 cout << nn_chk <<
" : " << nn_ret << endl;
303 throw Error(
"fermat_together: N Check Failed.");
307 ret = ret.subs(f2v,subs_options::no_pattern).subs(map_rat,subs_options::no_pattern);
319 int &v_max = fermat.
vmax;
323 expr_in = expr_in.to_rational(map_rat);
327 map<ex,long long,ex_is_less> s2c;
328 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
329 if(is_a<symbol>(*i)) s2c[*i]++;
331 exvector sv1, sv2, sv3;
334 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
335 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
336 else sv1.push_back(lst{kv.second, kv.first});
341 for(
auto sv : sv1) rep_vs.append(sv.op(1));
342 for(
auto sv : sv2) rep_vs.append(sv.op(1));
343 for(
auto sv : sv3) rep_vs.append(sv.op(1));
348 for(
auto vi : rep_vs) {
349 auto name =
"v" + to_string(fvi);
355 expr_in = expr_in.
subs(v2f);
359 cout << rep_vs << endl;
360 throw Error(
"Fermat: Too many variables.");
363 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
371 ss <<
"res:=" << expr_in <<
";" << endl;
376 static string bstr(
"[-begin-]"), estr(
"[-end-]");
377 ss <<
"&(U=1);" << endl;
378 ss <<
"!('" <<bstr<<
"',res,'" <<estr<<
"')" << endl;
379 auto ostr = fermat.
Execute(ss.str());
384 ss <<
"&(U=0);" << endl;
385 ss <<
"@(res);" << endl;
386 ss <<
"&_G;" << endl;
392 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"fermat_together: last char is NOT 0.");
393 ostr = ostr.substr(0, ostr.length()-1);
394 auto cpos = ostr.find(bstr);
395 if(cpos==string::npos)
throw Error(bstr+
" NOT Found.");
396 ostr = ostr.substr(cpos+bstr.length(),string::npos);
397 cpos = ostr.find(estr);
398 if(cpos==string::npos)
throw Error(estr+
" NOT Found.");
399 ostr = ostr.substr(0,cpos);
405 res = res.subs(f2v,
nopat);
406 return res.subs(map_rat,
nopat);
411 int &v_max = fermat.
vmax;
414 int nrow = mat_in.rows();
415 int ncol = mat_in.cols();
416 matrix mat(nrow, ncol);
417 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
421 map<ex,long long,ex_is_less> s2c;
423 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
424 if(is_a<symbol>(*i)) s2c[*i]++;
426 exvector sv1, sv2, sv3;
429 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
430 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
431 else sv1.push_back(lst{kv.second, kv.first});
436 for(
auto sv : sv1) rep_vs.append(sv.op(1));
437 for(
auto sv : sv2) rep_vs.append(sv.op(1));
438 for(
auto sv : sv3) rep_vs.append(sv.op(1));
444 for(
auto vi : rep_vs) {
445 auto name =
"v" + to_string(fvi);
453 cout << rep_vs << endl;
454 throw Error(
"Fermat: Too many variables.");
457 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
464 ss <<
"Array m[" << nrow <<
"," << ncol <<
"];" << endl;
470 for(
int c=0; c<ncol; c++) {
471 for(
int r=0; r<nrow; r++) {
476 ss <<
"Redrowech([m]);" << endl;
483 ss <<
"&(U=1);" << endl;
485 auto ostr = fermat.
Execute(ss.str());
491 ss <<
"&(U=0);" << endl;
492 ss <<
"@([m]);" << endl;
493 ss <<
"&_G;" << endl;
499 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"Direc::Export, last char is NOT 0.");
500 ostr = ostr.substr(0, ostr.length()-1);
503 ostr.erase(0, ostr.find(
":=")+2);
507 matrix mr(nrow, ncol);
508 auto res = fp.
Read(ostr);
509 for(
int r=0; r<nrow; r++) {
510 auto cur = res.op(r);
511 for(
int c=0; c<ncol; c++) mr(r,c) = cur.op(c).subs(map_rat);
518 int &v_max = fermat.
vmax;
521 int nrow = mat_in.rows();
522 int ncol = mat_in.cols();
523 matrix mat(nrow, ncol);
524 matrix ret_mat(nrow, ncol);
525 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
529 map<ex,long long,ex_is_less> s2c;
531 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
532 if(is_a<symbol>(*i)) s2c[*i]++;
534 exvector sv1, sv2, sv3;
537 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
538 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
539 else sv1.push_back(lst{kv.second, kv.first});
544 for(
auto sv : sv1) rep_vs.append(sv.op(1));
545 for(
auto sv : sv2) rep_vs.append(sv.op(1));
546 for(
auto sv : sv3) rep_vs.append(sv.op(1));
552 for(
auto vi : rep_vs) {
553 auto name =
"v" + to_string(fvi);
561 cout << rep_vs << endl;
562 throw Error(
"Fermat: Too many variables.");
565 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
575 for(
int r=0; r<nrow; r++) {
577 for(
int c=0; c<ncol; c++) {
578 if(mat(r,c).is_zero())
continue;
580 if(!r1) oss <<
"`" << endl;
581 oss <<
"[" << r+1 <<
",";
584 oss <<
"[" << c+1 <<
"," << mat(r,c).subs(v2f) <<
"]";
590 ss <<
"Array m[" << nrow <<
"," << ncol+1 <<
"] Sparse;" << endl;
595 ss <<
"[m]:=" << oss.str();
600 fermat.
Execute(
"Redrowech([m]);");
601 ss <<
"&(U=1);" << endl;
604 ss <<
"![m]" << endl;
605 auto ostr = fermat.
Execute(ss.str());
610 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"RowReduce, last char is NOT 0.");
611 ostr = ostr.substr(0, ostr.length()-1);
614 ostr.erase(0, ostr.find(
":=")+2);
615 size_t sn = ostr.length();
617 for(
size_t i=0; i<sn; i++) {
621 if(i>0 && lc==
'}') ostr[i-1] =
',';
622 }
else if(c==
']') c =
'}';
623 else if(c==
' '||c==
'\t'||c==
'\n'||c==
'\r')
continue;
628 auto res = fp.
Read(ostr);
629 for(
auto const & item : res) {
631 for(
auto const & it : item) {
632 if(r==-1) r =
ex2int(it)-1;
634 int c =
ex2int(it.op(0))-1;
635 ret_mat(r,c) = it.op(1).subs(map_rat);
641 ss <<
"&(U=0);" << endl;
642 ss <<
"@([m]);" << endl;
643 ss <<
"&_G;" << endl;
652 int &v_max = fermat.
vmax;
655 int nrow = mat_in.rows();
656 int ncol = mat_in.cols();
657 matrix mat(nrow, ncol);
658 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
662 map<ex,long long,ex_is_less> s2c;
664 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
665 if(is_a<symbol>(*i)) s2c[*i]++;
667 exvector sv1, sv2, sv3;
670 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
671 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
672 else sv1.push_back(lst{kv.second, kv.first});
677 for(
auto sv : sv1) rep_vs.append(sv.op(1));
678 for(
auto sv : sv2) rep_vs.append(sv.op(1));
679 for(
auto sv : sv3) rep_vs.append(sv.op(1));
685 for(
auto vi : rep_vs) {
686 auto name =
"v" + to_string(fvi);
694 cout << rep_vs << endl;
695 throw Error(
"Fermat: Too many variables.");
698 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
705 ss <<
"Array m[" << nrow <<
"," << ncol <<
"];" << endl;
711 for(
int c=0; c<ncol; c++) {
712 for(
int r=0; r<nrow; r++) {
717 ss <<
"res:=Det[m];" << endl;
724 static string bstr(
"[-begin-]"), estr(
"[-end-]");
725 ss <<
"&(U=1);" << endl;
726 ss <<
"!('" <<bstr<<
"',res,'" <<estr<<
"')" << endl;
727 auto ostr = fermat.
Execute(ss.str());
732 ss <<
"&(U=0);" << endl;
733 ss <<
"@res;" << endl;
734 ss <<
"@[**];" << endl;
735 ss <<
"&_G;" << endl;
741 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"fermat_together: last char is NOT 0.");
742 ostr = ostr.substr(0, ostr.length()-1);
743 auto cpos = ostr.find(bstr);
744 if(cpos==string::npos)
throw Error(bstr+
" NOT Found.");
745 ostr = ostr.substr(cpos+bstr.length(),string::npos);
746 cpos = ostr.find(estr);
747 if(cpos==string::npos)
throw Error(estr+
" NOT Found.");
748 ostr = ostr.substr(0,cpos);
752 auto res = fp.
Read(ostr);
753 res = res.subs(map_rat);
759 int &v_max = fermat.
vmax;
762 int nrow = mat_in.rows();
763 int ncol = mat_in.cols();
764 matrix mat(nrow, ncol);
765 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
769 map<ex,long long,ex_is_less> s2c;
771 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
772 if(is_a<symbol>(*i)) s2c[*i]++;
774 exvector sv1, sv2, sv3;
777 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
778 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
779 else sv1.push_back(lst{kv.second, kv.first});
784 for(
auto sv : sv1) rep_vs.append(sv.op(1));
785 for(
auto sv : sv2) rep_vs.append(sv.op(1));
786 for(
auto sv : sv3) rep_vs.append(sv.op(1));
792 for(
auto vi : rep_vs) {
793 auto name =
"v" + to_string(fvi);
801 cout << rep_vs << endl;
802 throw Error(
"Fermat: Too many variables.");
805 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
815 for(
int r=0; r<nrow; r++) {
817 for(
int c=0; c<ncol; c++) {
818 if(mat(r,c).is_zero())
continue;
820 if(!r1) oss <<
"`" << endl;
821 oss <<
"[" << r+1 <<
",";
824 oss <<
"[" << c+1 <<
"," << mat(r,c).subs(v2f) <<
"]";
830 ss <<
"Array m[" << nrow <<
"," << ncol <<
"] Sparse;" << endl;
835 ss <<
"[m]:=" << oss.str();
840 ss <<
"res:=Det[m];" << endl;
847 static string bstr(
"[-begin-]"), estr(
"[-end-]");
848 ss <<
"&(U=1);" << endl;
849 ss <<
"!('" <<bstr<<
"',res,'" <<estr<<
"')" << endl;
850 auto ostr = fermat.
Execute(ss.str());
855 ss <<
"&(U=0);" << endl;
856 ss <<
"@res;" << endl;
857 ss <<
"@[**];" << endl;
858 ss <<
"&_G;" << endl;
864 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"fermat_together: last char is NOT 0.");
865 ostr = ostr.substr(0, ostr.length()-1);
866 auto cpos = ostr.find(bstr);
867 if(cpos==string::npos)
throw Error(bstr+
" NOT Found.");
868 ostr = ostr.substr(cpos+bstr.length(),string::npos);
869 cpos = ostr.find(estr);
870 if(cpos==string::npos)
throw Error(estr+
" NOT Found.");
871 ostr = ostr.substr(0,cpos);
875 auto res = fp.
Read(ostr);
876 res = res.subs(map_rat);
882 int &v_max = fermat.
vmax;
885 int nrow = mat_in.rows();
886 int ncol = mat_in.cols();
887 matrix mat(nrow, ncol);
888 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
892 map<ex,long long,ex_is_less> s2c;
894 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
895 if(is_a<symbol>(*i)) s2c[*i]++;
897 exvector sv1, sv2, sv3;
900 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
901 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
902 else sv1.push_back(lst{kv.second, kv.first});
907 for(
auto sv : sv1) rep_vs.append(sv.op(1));
908 for(
auto sv : sv2) rep_vs.append(sv.op(1));
909 for(
auto sv : sv3) rep_vs.append(sv.op(1));
915 for(
auto vi : rep_vs) {
916 auto name =
"v" + to_string(fvi);
924 cout << rep_vs << endl;
925 throw Error(
"Fermat: Too many variables.");
928 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
935 ss <<
"Array m[" << nrow <<
"," << ncol <<
"];" << endl;
941 for(
int c=0; c<ncol; c++) {
942 for(
int r=0; r<nrow; r++) {
947 ss <<
"[m]:=1/[m];" << endl;
954 ss <<
"&(U=1);" << endl;
956 auto ostr = fermat.
Execute(ss.str());
962 ss <<
"&(U=0);" << endl;
963 ss <<
"@([m]);" << endl;
964 ss <<
"&_G;" << endl;
970 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"Direc::Export, last char is NOT 0.");
971 ostr = ostr.substr(0, ostr.length()-1);
974 ostr.erase(0, ostr.find(
":=")+2);
978 matrix mr(nrow, ncol);
979 auto res = fp.
Read(ostr);
980 for(
int r=0; r<nrow; r++) {
981 auto cur = res.op(r);
982 for(
int c=0; c<ncol; c++) mr(r,c) = cur.op(c).subs(map_rat);
989 int &v_max = fermat.
vmax;
994 matrix mat1(nr1, nc1);
995 for(
int r=0; r<nr1; r++)
for(
int c=0; c<nc1; c++) mat1(r,c) = m1(r,c).to_rational(map_rat);
998 matrix mat2(nr2, nc2);
999 for(
int r=0; r<nr2; r++)
for(
int c=0; c<nc2; c++) mat2(r,c) = m2(r,c).to_rational(map_rat);
1003 map<ex,long long,ex_is_less> s2c;
1005 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
1006 if(is_a<symbol>(*i)) s2c[*i]++;
1009 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
1010 if(is_a<symbol>(*i)) s2c[*i]++;
1012 exvector sv1, sv2, sv3;
1013 for(
auto kv : s2c) {
1015 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
1016 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
1017 else sv1.push_back(lst{kv.second, kv.first});
1022 for(
auto sv : sv1) rep_vs.append(sv.op(1));
1023 for(
auto sv : sv2) rep_vs.append(sv.op(1));
1024 for(
auto sv : sv3) rep_vs.append(sv.op(1));
1030 for(
auto vi : rep_vs) {
1031 auto name =
"v" + to_string(fvi);
1039 cout << rep_vs << endl;
1040 throw Error(
"Fermat: Too many variables.");
1043 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
1050 ss <<
"Array m1[" << nr1 <<
"," << nc1 <<
"];" << endl;
1051 ss <<
"Array m2[" << nr2 <<
"," << nc2 <<
"];" << endl;
1057 for(
int c=0; c<nc1; c++) {
1058 for(
int r=0; r<nr1; r++) {
1062 ss <<
")];" << endl;
1065 for(
int c=0; c<nc2; c++) {
1066 for(
int r=0; r<nr2; r++) {
1070 ss <<
")];" << endl;
1072 ss <<
"[m]:=[m1]*[m2];" << endl;
1073 auto tmp = ss.str();
1079 ss <<
"&(U=1);" << endl;
1080 ss <<
"![m" << endl;
1081 auto ostr = fermat.
Execute(ss.str());
1087 ss <<
"&(U=0);" << endl;
1088 ss <<
"@([m1],[m2],[m]);" << endl;
1089 ss <<
"&_G;" << endl;
1095 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"Direc::Export, last char is NOT 0.");
1096 ostr = ostr.substr(0, ostr.length()-1);
1099 ostr.erase(0, ostr.find(
":=")+2);
1103 matrix mr(nr1, nc2);
1104 auto res = fp.
Read(ostr);
1105 for(
int r=0; r<nr1; r++) {
1106 auto cur = res.op(r);
1107 for(
int c=0; c<nc2; c++) mr(r,c) = cur.op(c).subs(map_rat);
1114 int &v_max = fermat.
vmax;
1117 int nrow = mat_in.rows();
1118 int ncol = mat_in.cols();
1119 matrix mat(nrow, ncol);
1120 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(map_rat);
1124 map<ex,long long,ex_is_less> s2c;
1126 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
1127 if(is_a<symbol>(*i)) s2c[*i]++;
1129 exvector sv1, sv2, sv3;
1130 for(
auto kv : s2c) {
1132 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
1133 else if(map_rat.find(kv.first)!=map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
1134 else sv1.push_back(lst{kv.second, kv.first});
1139 for(
auto sv : sv1) rep_vs.append(sv.op(1));
1140 for(
auto sv : sv2) rep_vs.append(sv.op(1));
1141 for(
auto sv : sv3) rep_vs.append(sv.op(1));
1147 for(
auto vi : rep_vs) {
1148 auto name =
"v" + to_string(fvi);
1156 cout << rep_vs << endl;
1157 throw Error(
"Fermat: Too many variables.");
1160 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
1167 ss <<
"Array m[" << nrow <<
"," << ncol <<
"];" << endl;
1173 for(
int c=0; c<ncol; c++) {
1174 for(
int r=0; r<nrow; r++) {
1178 ss <<
")];" << endl;
1179 ss <<
"[m]:=[m]^(" << n <<
");" << endl;
1180 auto tmp = ss.str();
1186 ss <<
"&(U=1);" << endl;
1187 ss <<
"![m" << endl;
1188 auto ostr = fermat.
Execute(ss.str());
1194 ss <<
"&(U=0);" << endl;
1195 ss <<
"@([m]);" << endl;
1196 ss <<
"&_G;" << endl;
1202 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"Direc::Export, last char is NOT 0.");
1203 ostr = ostr.substr(0, ostr.length()-1);
1206 ostr.erase(0, ostr.find(
":=")+2);
1210 matrix mr(nrow, ncol);
1211 auto res = fp.
Read(ostr);
1212 for(
int r=0; r<nrow; r++) {
1213 auto cur = res.op(r);
1214 for(
int c=0; c<ncol; c++) mr(r,c) = cur.op(c).subs(map_rat);
1225 string name = _name;
1230 int &v_max = fermat.
vmax;
1232 int nrow = mat_in.rows();
1233 int ncol = mat_in.cols();
1234 matrix mat(nrow, ncol);
1235 for(
int r=0; r<nrow; r++)
for(
int c=0; c<ncol; c++) mat(r,c) = mat_in(r,c).to_rational(mat_map_rat);
1239 map<ex,long long,ex_is_less> s2c;
1241 for(const_preorder_iterator i = expr_in.preorder_begin(); i != expr_in.preorder_end(); ++i) {
1242 if(is_a<symbol>(*i)) s2c[*i]++;
1244 exvector sv1, sv2, sv3;
1245 for(
auto kv : s2c) {
1247 if(fw!=
fermat_weight.end()) sv2.push_back(lst{fw->second, fw->first});
1248 else if(mat_map_rat.find(kv.first)!=mat_map_rat.end()) sv3.push_back(lst{kv.second, kv.first});
1249 else sv1.push_back(lst{kv.second, kv.first});
1254 for(
auto sv : sv1) rep_vs.append(sv.op(1));
1255 for(
auto sv : sv2) rep_vs.append(sv.op(1));
1256 for(
auto sv : sv3) rep_vs.append(sv.op(1));
1261 for(
auto vi : rep_vs) {
1262 auto name =
"v" + to_string(fvi);
1270 cout << rep_vs << endl;
1271 throw Error(
"Fermat: Too many variables.");
1274 for(
int i=v_max; i<fvi; i++) ss <<
"&(J=v" << i <<
");" << endl;
1281 ss <<
"Array " << name <<
"[" << nrow <<
"," << ncol <<
"];" << endl;
1286 ss <<
"["<<name<<
"]:=[(";
1287 for(
int c=0; c<ncol; c++) {
1288 for(
int r=0; r<nrow; r++) {
1292 ss <<
")];" << endl;
1294 auto tmp = ss.str();
1302 string name = _name;
1307 int &v_max = fermat.
vmax;
1310 ss <<
"&(U=1);" << endl;
1311 ss <<
"![" << name << endl;
1312 auto ostr = fermat.
Execute(ss.str());
1318 ss <<
"&(U=0);" << endl;
1319 ss <<
"&_G;" << endl;
1325 if(ostr[ostr.length()-1]!=
'0')
throw Error(
"Direc::Export, last char is NOT 0.");
1326 ostr = ostr.substr(0, ostr.length()-1);
1328 ostr.erase(0, ostr.find(
":=")+2);
1332 auto res = fp.
Read(ostr);
1333 int nrow = res.nops();
1334 int ncol = res.op(0).nops();
1335 matrix mr(nrow, ncol);
1336 for(
int r=0; r<nrow; r++) {
1337 auto cur = res.op(r);
1338 for(
int c=0; c<ncol; c++) mr(r,c) = cur.op(c).subs(mat_map_rat);
1345 int &v_max = fermat.
vmax;
1349 auto ostr = fermat.
Execute(ss.str());
class used to wrap error message
interface to communicate with Fermat program
class to parse for string or file, helpful with Symbol class
ex Read(const string &instr, bool s2S=true)
class extended to GiNaC symbol class, represent a positive symbol
ex subs(const exmap &m, unsigned options=0) const override
matrix fermat_Redrowech_Sparse(const matrix &mat)
matrix fermat_mul(const matrix &m1, const matrix &m2)
map< ex, long long, ex_is_less > fermat_weight
ex factor_form(const ex &expr, bool nd)
factorize a expression using FORM
void fermat_mat(const matrix &mat_in, const string &name)
ex numer_denom_fermat(const ex &expr, bool dfactor=false)
return the numerator and denominator after normalization
matrix fermat_Redrowech(const matrix &mat)
ex nextprime(const ex &n)
ex fermat_eval(const ex &expr)
return the numerator and denominator after normalization
ex fermat_Det(const matrix &mat)
matrix fermat_inv(const matrix &mat)
void sort_vec(exvector &ivec, bool less=true)
sort the list in less order, or the reverse
void string_replace_all(string &str, const string &from, const string &to)
ex fermat_Det_Sparse(const matrix &mat)
void string_trim(string &str)
ex numer_fermat(const ex &expr)
matrix fermat_pow(const matrix &mat_in, int n)
int ex2int(ex num)
ex to integer
ex subs(const ex &e, init_list sl)