PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/test.cc

https://github.com/YueRen/Sources
C++ | 592 lines | 372 code | 180 blank | 40 comment | 48 complexity | 00c94b8ed1ecdef2ab2b5dd82141ee8b MD5 | raw file
  1. #include "kernel/mod2.h"
  2. #include "omalloc/omalloc.h"
  3. #include "factory/factory.h" // :(
  4. #include "misc/intvec.h"
  5. #include "misc/int64vec.h"
  6. #include "misc/mylimits.h"
  7. #include "misc/options.h"
  8. #include "reporter/reporter.h"
  9. #include "resources/feFopen.h"
  10. #include "resources/feResource.h"
  11. #include "coeffs/coeffs.h"
  12. #include "coeffs/si_gmp.h"
  13. #include "polys/kbuckets.h"
  14. #include "polys/matpol.h"
  15. #include "polys/mod_raw.h"
  16. #include "polys/prCopy.h"
  17. #include "polys/sbuckets.h"
  18. #include "polys/simpleideals.h"
  19. #include "polys/weight.h"
  20. #include "polys/monomials/maps.h"
  21. #include "polys/monomials/monomials.h"
  22. #include "polys/monomials/p_polys.h"
  23. #include "polys/monomials/ring.h"
  24. #include "polys/nc/nc.h"
  25. #include "polys/nc/ncSACache.h"
  26. #include "polys/nc/ncSAFormula.h"
  27. #include "polys/nc/ncSAMult.h"
  28. #include "polys/nc/sca.h"
  29. #include "polys/nc/summator.h"
  30. #include "polys/templates/p_MemAdd.h"
  31. #include "polys/templates/p_Procs.h"
  32. #include "polys/operations/pShallowCopyDelete.h"
  33. #include "polys/clapsing.h"
  34. #include "coeffs/numbers.h"
  35. #include "structs.h"
  36. // HEADERS:
  37. #include "kernel/combinatorics/hutil.h"
  38. #include "kernel/combinatorics/stairc.h"
  39. #include "kernel/ideals.h"
  40. #include "kernel/GBEngine/syz.h"
  41. #include "kernel/maps/fast_maps.h"
  42. #include "kernel/groebner_walk/walkProc.h"
  43. #include "kernel/groebner_walk/walkMain.h"
  44. #include "kernel/groebner_walk/walkSupport.h"
  45. #include "kernel/GBEngine/khstd.h"
  46. /// #include "kernel/sparsmat.h" // TODO: install polys/this!
  47. //+
  48. #include "kernel/fglm/fglm.h"
  49. #include "kernel/GBEngine/kstd1.h"
  50. #include "kernel/fglm/fglmgauss.h"
  51. #include "kernel/fglm/fglmvec.h"
  52. #include "kernel/GBEngine/kstdfac.h"
  53. #include "kernel/spectrum/kmatrix.h"
  54. #include "kernel/spectrum/GMPrat.h"
  55. #include "kernel/spectrum/multicnt.h"
  56. #include "kernel/spectrum/npolygon.h"
  57. #include "kernel/spectrum/semic.h"
  58. #include "kernel/spectrum/spectrum.h"
  59. #include "kernel/spectrum/splist.h"
  60. #include "kernel/spectrum/multicnt.h"
  61. #include "kernel/linear_algebra/eigenval.h"
  62. #include "kernel/GBEngine/units.h"
  63. #include "kernel/GBEngine/ratgring.h"
  64. #include "kernel/GBEngine/shiftgb.h"
  65. #include "kernel/GBEngine/kutil.h"
  66. // #include "CCRing.h" // Too old!
  67. #include "kernel/digitech.h"
  68. #include "kernel/linear_algebra/eigenval.h"
  69. #include "kernel/maps/fast_maps.h"
  70. #include "kernel/fast_mult.h"
  71. #include "kernel/fglm/fglmgauss.h"
  72. #include "kernel/fglm/fglm.h"
  73. #include "kernel/fglm/fglmvec.h"
  74. ////////#include "kernel/F5cData.h"
  75. #include "kernel/GBEngine/f5data.h"
  76. #include "kernel/GBEngine/f5gb.h"
  77. #include "kernel/GBEngine/f5lists.h"
  78. ////////#include "kernel/F5cLists.h"
  79. #include "kernel/ideals.h"
  80. #include "kernel/spectrum/kmatrix.h"
  81. #include "kernel/GBEngine/kstd1.h"
  82. #include "kernel/GBEngine/kstdfac.h"
  83. #include "kernel/GBEngine/khstd.h"
  84. #include "kernel/linear_algebra/linearAlgebra.h"
  85. #include "kernel/spectrum/multicnt.h"
  86. #include "kernel/spectrum/npolygon.h"
  87. #include "kernel/preimage.h"
  88. #include "kernel/GBEngine/nc.h"
  89. #include "kernel/GBEngine/ratgring.h"
  90. #include "kernel/GBEngine/ringgb.h"
  91. #include "kernel/spectrum/semic.h"
  92. #include "kernel/GBEngine/shiftgb.h"
  93. #include "kernel/spectrum/spectrum.h"
  94. #include "kernel/spectrum/splist.h"
  95. #include "kernel/structs.h"
  96. #include "kernel/GBEngine/syz.h"
  97. // #include "kernel/testpoly.h" // Too old?
  98. #include "kernel/GBEngine/tgbgauss.h"
  99. #include "kernel/GBEngine/tgb.h"
  100. #include "kernel/GBEngine/units.h"
  101. #include "kernel/groebner_walk/walkMain.h"
  102. #include "kernel/groebner_walk/walkProc.h"
  103. #include "kernel/groebner_walk/walkSupport.h"
  104. #include "kernel/GBEngine/janet.h"
  105. #include "kernel/linear_algebra/interpolation.h"
  106. #include "kernel/linear_algebra/minpoly.h"
  107. #include "kernel/linear_algebra/Minor.h"
  108. #include "kernel/linear_algebra/MinorInterface.h"
  109. #include "kernel/linear_algebra/MinorProcessor.h"
  110. #include "kernel/linear_algebra/Cache.h"
  111. #include "kernel/linear_algebra/CacheImplementation.h"
  112. // #include "polys/clapconv.h" // due to factory? :(
  113. // #include "kernel/tgb_internal.h" // :(
  114. // #include "kernel/fglm/fglmzero.cc" // looks like <factory/templates/ftmpl_list.h> must be installed!
  115. // TODO: looks like "coeffs/mpr_complex.h" must be installed!
  116. #include "kernel/polys.h"
  117. void TestGBEngine()
  118. {
  119. // R = MPolynomialRing_polydict(QQ,5,'w,x,y,z,C', order='degrevlex')
  120. // J = (w*w - x*z, w*x - y*z, x*x - w*y, x*y - z*z, y*y - w*z)
  121. const short w = 1;
  122. const short x = 2;
  123. const short y = 3;
  124. const short z = 4;
  125. const short N = (z - w + 1);
  126. char **n=(char**)omalloc(N*sizeof(char*));
  127. n[w-1]=omStrDup("w");
  128. n[x-1]=omStrDup("x");
  129. n[y-1]=omStrDup("y");
  130. n[z-1]=omStrDup("z");
  131. const int D = 3;
  132. rRingOrder_t *order = (rRingOrder_t *) omAlloc0(D* sizeof(rRingOrder_t));
  133. int *block0 = (int *)omAlloc0(D * sizeof(int));
  134. int *block1 = (int *)omAlloc0(D * sizeof(int));
  135. order[0] = ringorder_dp;
  136. block0[0] = 1;
  137. block1[0] = N;
  138. order[1] = ringorder_C;
  139. block0[1] = 1;
  140. block1[1] = N;
  141. ring R = rDefault(0, N, n, D, order, block0, block1);
  142. // ring R = rDefault(0, N, n);
  143. rWrite(R); PrintLn();
  144. #ifdef RDEBUG
  145. rDebugPrint(R);
  146. #endif
  147. ideal I = idInit(5, 1);
  148. int gen = 0;
  149. {
  150. // -xz
  151. poly p = p_ISet(-1,R);
  152. p_SetExp(p, x, 1, R);
  153. p_SetExp(p, z, 1, R);
  154. p_Setm(p, R);
  155. assume( p_GetExp(p, x, R) == 1 );
  156. assume( p_GetExp(p, z, R) == 1 );
  157. assume( p_GetExp(p, w, R) == 0 );
  158. assume( p_GetExp(p, y, R) == 0 );
  159. // +w2
  160. poly lp = p_ISet(1,R);
  161. p_SetExp(lp, w, 2, R);
  162. p_Setm(lp, R);
  163. assume( p_GetExp(lp, w, R) == 2 );
  164. assume( p_GetExp(lp, x, R) == 0 );
  165. assume( p_GetExp(lp, y, R) == 0 );
  166. assume( p_GetExp(lp, z, R) == 0 );
  167. MATELEM(I, 1, ++gen) = p_Add_q(lp, p, R); // w2 - xz
  168. }
  169. {
  170. // -yz
  171. poly p = p_ISet(-1,R);
  172. p_SetExp(p, y, 1, R);
  173. p_SetExp(p, z, 1, R);
  174. p_Setm(p, R);
  175. assume( p_GetExp(p, y, R) == 1 );
  176. assume( p_GetExp(p, z, R) == 1 );
  177. assume( p_GetExp(p, w, R) == 0 );
  178. assume( p_GetExp(p, x, R) == 0 );
  179. // +wx
  180. poly lp = p_ISet(1,R);
  181. p_SetExp(lp, w, 1, R);
  182. p_SetExp(lp, x, 1, R);
  183. p_Setm(lp, R);
  184. assume( p_GetExp(lp, w, R) == 1 );
  185. assume( p_GetExp(lp, x, R) == 1 );
  186. assume( p_GetExp(lp, y, R) == 0 );
  187. assume( p_GetExp(lp, z, R) == 0 );
  188. MATELEM(I, 1, ++gen) = p_Add_q(lp, p, R); // wx - yz
  189. }
  190. {
  191. // -wy
  192. poly p = p_ISet(-1,R);
  193. p_SetExp(p, y, 1, R);
  194. p_SetExp(p, w, 1, R);
  195. p_Setm(p, R);
  196. assume( p_GetExp(p, y, R) == 1 );
  197. assume( p_GetExp(p, w, R) == 1 );
  198. assume( p_GetExp(p, z, R) == 0 );
  199. assume( p_GetExp(p, x, R) == 0 );
  200. // +x2
  201. poly lp = p_ISet(1,R);
  202. p_SetExp(lp, x, 2, R);
  203. p_Setm(lp, R);
  204. assume( p_GetExp(lp, w, R) == 0 );
  205. assume( p_GetExp(lp, x, R) == 2 );
  206. assume( p_GetExp(lp, y, R) == 0 );
  207. assume( p_GetExp(lp, z, R) == 0 );
  208. MATELEM(I, 1, ++gen) = p_Add_q(lp, p, R); // x2 - wy
  209. }
  210. {
  211. // -z2
  212. poly p = p_ISet(-1,R);
  213. p_SetExp(p, z, 2, R);
  214. p_Setm(p, R);
  215. assume( p_GetExp(p, y, R) == 0 );
  216. assume( p_GetExp(p, w, R) == 0 );
  217. assume( p_GetExp(p, z, R) == 2 );
  218. assume( p_GetExp(p, x, R) == 0 );
  219. // +xy
  220. poly lp = p_ISet(1,R);
  221. p_SetExp(lp, x, 1, R);
  222. p_SetExp(lp, y, 1, R);
  223. p_Setm(lp, R);
  224. assume( p_GetExp(lp, w, R) == 0 );
  225. assume( p_GetExp(lp, x, R) == 1 );
  226. assume( p_GetExp(lp, y, R) == 1 );
  227. assume( p_GetExp(lp, z, R) == 0 );
  228. MATELEM(I, 1, ++gen) = p_Add_q(lp, p, R); // xy - z2
  229. }
  230. {
  231. // -wz
  232. poly p = p_ISet(-1,R);
  233. p_SetExp(p, w, 1, R);
  234. p_SetExp(p, z, 1, R);
  235. p_Setm(p, R);
  236. assume( p_GetExp(p, y, R) == 0 );
  237. assume( p_GetExp(p, w, R) == 1 );
  238. assume( p_GetExp(p, z, R) == 1 );
  239. assume( p_GetExp(p, x, R) == 0 );
  240. // +y2
  241. poly lp = p_ISet(1,R);
  242. p_SetExp(lp, y, 2, R);
  243. p_Setm(lp, R);
  244. assume( p_GetExp(lp, w, R) == 0 );
  245. assume( p_GetExp(lp, x, R) == 0 );
  246. assume( p_GetExp(lp, y, R) == 2 );
  247. assume( p_GetExp(lp, z, R) == 0 );
  248. MATELEM(I, 1, ++gen) = p_Add_q(lp, p, R); // y2 - wz
  249. }
  250. #ifdef PDEBUG
  251. PrintS("I: ");
  252. idShow(I, R, R, 0);
  253. #endif
  254. // ideal kStd(ideal F, ideal Q, tHomog h, intvec ** mw,intvec *hilb=NULL,
  255. // int syzComp=0,int newIdeal=0, intvec *vw=NULL);
  256. // make R the default ring:
  257. rChangeCurrRing(R);
  258. {
  259. ideal G = kStd(I, currRing->qideal, testHomog, NULL);
  260. #ifdef PDEBUG
  261. PrintS("GB: ");
  262. idShow(G, R, R, 0);
  263. #endif
  264. id_Delete( &G, R);
  265. }
  266. {
  267. intvec *weights = NULL;
  268. ideal SYZ = idSyzygies(I, testHomog, &weights);
  269. #ifdef PDEBUG
  270. PrintS("SYZ: ");
  271. idShow(SYZ, R, R, 0);
  272. #endif
  273. id_Delete(&SYZ, R);
  274. if (weights!=NULL) { PrintS("weights: "); weights->show(); delete weights; }
  275. }
  276. {
  277. PrintS("\n**********************************\n");
  278. PrintS("lres: \n");
  279. int dummy;
  280. syStrategy r = syLaScala3(I,&dummy);
  281. intvec *b = syBettiOfComputation(r, FALSE);
  282. PrintS("non-min. betti: \n"); b->show(); PrintLn();
  283. delete b;
  284. Print("length: %d\n", sySize(r));
  285. syPrint(r, "R");
  286. r = syMinimize(r); // syzstr->references ++ ==> memory leak :(((
  287. b = syBettiOfComputation(r, TRUE);
  288. PrintS("min. betti: \n"); b->show(); PrintLn();
  289. delete b;
  290. Print("length: %d\n", sySize(r));
  291. syPrint(r, "R");
  292. syKillComputation(r, R);
  293. }
  294. {
  295. PrintS("\n**********************************\n");
  296. PrintS("sres: \n");
  297. const int maxl = rVar(R)-1; // +2*(1);
  298. syStrategy r = sySchreyer(I, rVar(R));
  299. intvec *b = syBettiOfComputation(r, FALSE);
  300. PrintS("non-min. betti: \n"); b->show(); PrintLn();
  301. delete b;
  302. Print("length: %d\n", sySize(r));
  303. syPrint(r, "R");
  304. r = syMinimize(r); // syzstr->references ++ ==> memory leak :(((
  305. b = syBettiOfComputation(r, TRUE);
  306. PrintS("min. betti: \n"); b->show(); PrintLn();
  307. delete b;
  308. Print("length: %d\n", sySize(r));
  309. syPrint(r, "R");
  310. syKillComputation(r, R);
  311. }
  312. {
  313. PrintS("\n**********************************\n");
  314. PrintS("nres: \n");
  315. intvec *weights=NULL;
  316. // const int maxl = rVar(R)-1 + 2*(1);
  317. syStrategy r = syResolution(I, rVar(R)-1, weights, FALSE/*iiOp==MRES_CMD*/);
  318. intvec *b = syBettiOfComputation(r, FALSE);
  319. PrintS("non-min. betti: \n"); b->show(); PrintLn();
  320. delete b;
  321. Print("length: %d\n", sySize(r));
  322. syPrint(r, "R");
  323. r = syMinimize(r); // syzstr->references ++ ==> memory leak :(((
  324. b = syBettiOfComputation(r, TRUE);
  325. PrintS("min. betti: \n"); b->show(); PrintLn();
  326. delete b;
  327. Print("length: %d\n", sySize(r));
  328. syPrint(r, "R");
  329. syKillComputation(r, R);
  330. }
  331. {
  332. PrintS("\n**********************************\n");
  333. PrintS("mres: \n");
  334. intvec *weights=NULL;
  335. // const int maxl = rVar(R)-1 + 2*(1);
  336. syStrategy r = syResolution(I, rVar(R)+1, weights, TRUE/*iiOp==MRES_CMD*/);
  337. intvec *b = syBettiOfComputation(r, FALSE);
  338. PrintS("non-min. betti: \n"); b->show(); PrintLn();
  339. delete b;
  340. Print("length: %d\n", sySize(r));
  341. syPrint(r, "R");
  342. r = syMinimize(r); // syzstr->references ++ ==> memory leak :(((
  343. b = syBettiOfComputation(r, TRUE);
  344. PrintS("min. betti: \n"); b->show(); PrintLn();
  345. delete b;
  346. Print("length: %d\n", sySize(r));
  347. syPrint(r, "R");
  348. syKillComputation(r, R);
  349. }
  350. id_Delete( &I, R);
  351. rDelete(R); // should cleanup every belonging polynomial, right!?
  352. }
  353. void TestSimpleRingArithmetcs()
  354. {
  355. // Libpolys tests:
  356. // construct the ring Z/32003[x,y,z]
  357. // the variable names
  358. char **n=(char**)omalloc(3*sizeof(char*));
  359. n[0]=omStrDup("x");
  360. n[1]=omStrDup("y");
  361. n[2]=omStrDup("z2");
  362. ring R = rDefault(32003,3,n); // ring R = rDefault(0,3,n);
  363. rWrite(R); PrintLn();
  364. #ifdef RDEBUG
  365. rDebugPrint(R);
  366. #endif
  367. poly p = p_ISet(1,R); p_SetExp(p,1,1, R); p_Setm(p, R);
  368. assume( p_GetExp(p,1, R) == 1 );
  369. poly pp = pp_Mult_qq( p, p, R);
  370. PrintS("p: "); p_Write0(p, R); Print(", deg(p): %ld", p_Totaldegree(p, R)); assume( 1 == p_Totaldegree(p, R) );
  371. PrintS("; p*p : "); p_Write0(pp, R); Print("deg(pp): %ld\n", p_Totaldegree(pp, R)); assume( 2 == p_Totaldegree(pp, R) );
  372. p_Delete(&p, R);
  373. assume( p_GetExp(pp,1, R) == 2 );
  374. p_Delete(&pp, R);
  375. // rDelete(R);
  376. // make R the default ring:
  377. rChangeCurrRing(R);
  378. // create the polynomial 1
  379. poly p1=pISet(1);
  380. // create tthe polynomial 2*x^3*z^2
  381. poly p2=p_ISet(2,R);
  382. pSetExp(p2,1,3);
  383. pSetExp(p2,3,2);
  384. pSetm(p2);
  385. // print p1 + p2
  386. PrintS("p1: "); pWrite0(p1);
  387. PrintS(" + p2: "); pWrite0(p2);
  388. PrintS(" ---- >>>> ");
  389. // compute p1+p2
  390. p1=p_Add_q(p1,p2,R); p2=NULL;
  391. pWrite(p1);
  392. // clean up:
  393. // pDelete(&p1);
  394. rDelete(R); // should cleanup every belonging polynomial, right!?
  395. }
  396. int main( int, char *argv[] )
  397. {
  398. assume( sizeof(long) == SIZEOF_LONG );
  399. if( sizeof(long) != SIZEOF_LONG )
  400. {
  401. WerrorS("Bad config.h: wrong size of long!");
  402. return(1);
  403. }
  404. feInitResources(argv[0]);
  405. StringSetS("ressources in use (as reported by feStringAppendResources(0):\n");
  406. feStringAppendResources(0);
  407. PrintLn();
  408. { char* s = StringEndS(); PrintS(s); omFree(s); }
  409. TestGBEngine();
  410. TestSimpleRingArithmetcs();
  411. return 0;
  412. }