PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/Singular/ipconv.cc

https://github.com/ederc/Sources
C++ | 607 lines | 522 code | 46 blank | 39 comment | 76 complexity | 1162a67f09e313d94b3b01bc37a038b5 MD5 | raw file
  1. /****************************************
  2. * Computer Algebra System SINGULAR *
  3. ****************************************/
  4. /*
  5. * ABSTRACT: automatic type conversions
  6. */
  7. #include "kernel/mod2.h"
  8. #include "Singular/tok.h"
  9. #include "Singular/ipid.h"
  10. #include "misc/intvec.h"
  11. #include "misc/options.h"
  12. #include "kernel/polys.h"
  13. #include "kernel/ideals.h"
  14. #include "Singular/subexpr.h"
  15. #include "coeffs/numbers.h"
  16. #include "coeffs/coeffs.h"
  17. #include "coeffs/bigintmat.h"
  18. //#include "polys/ext_fields/longalg.h"
  19. #include "polys/matpol.h"
  20. #include "Singular/links/silink.h"
  21. #include "kernel/GBEngine/syz.h"
  22. #include "Singular/attrib.h"
  23. #include "polys/monomials/ring.h"
  24. #include "Singular/ipshell.h"
  25. #include "Singular/number2.h"
  26. #include "Singular/ipconv.h"
  27. typedef void * (*iiConvertProc)(void * data);
  28. typedef void (*iiConvertProcL)(leftv out,leftv in);
  29. struct sConvertTypes
  30. {
  31. int i_typ;
  32. int o_typ;
  33. iiConvertProc p;
  34. iiConvertProcL pl;
  35. };
  36. // all of these static conversion routines work destructive on their input
  37. static void * iiI2P(void *data)
  38. {
  39. poly p=pISet((int)(long)data);
  40. return (void *)p;
  41. }
  42. static void * iiBI2P(void *data)
  43. {
  44. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  45. if (nMap==NULL)
  46. {
  47. Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
  48. return NULL;
  49. }
  50. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  51. n_Delete((number *)&data, coeffs_BIGINT);
  52. poly p=p_NSet(n, currRing);
  53. return (void *)p;
  54. }
  55. static void iiBu2P(leftv out, leftv in)
  56. {
  57. sBucket_pt b=(sBucket_pt)in->CopyD();
  58. poly p; int l;
  59. sBucketDestroyAdd(b,&p,&l);
  60. out->data=(void*)p;
  61. }
  62. static void * iiI2V(void *data)
  63. {
  64. poly p=pISet((int)(long)data);
  65. if (p!=NULL) pSetComp(p,1);
  66. return (void *)p;
  67. }
  68. static void * iiBI2V(void *data)
  69. {
  70. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  71. if (nMap==NULL)
  72. {
  73. Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
  74. return NULL;
  75. }
  76. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  77. n_Delete((number *)&data, coeffs_BIGINT);
  78. poly p=p_NSet(n, currRing);
  79. if (p!=NULL) pSetComp(p,1);
  80. return (void *)p;
  81. }
  82. static void * iiI2Id(void *data)
  83. {
  84. ideal I=idInit(1,1);
  85. I->m[0]=pISet((int)(long)data);
  86. return (void *)I;
  87. }
  88. static void * iiBI2Id(void *data)
  89. {
  90. ideal I=idInit(1,1);
  91. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  92. if (nMap==NULL)
  93. {
  94. Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
  95. return NULL;
  96. }
  97. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  98. n_Delete((number *)&data,coeffs_BIGINT);
  99. poly p=pNSet(n);
  100. I->m[0]=p;
  101. return (void *)I;
  102. }
  103. static void * iiBu2V(void *data)
  104. {
  105. poly p=NULL;
  106. if (data!=NULL)
  107. {
  108. sBucket_pt b=(sBucket_pt)data;
  109. int l;
  110. sBucketDestroyAdd(b,&p,&l);
  111. if (p!=NULL) pSetCompP(p,1);
  112. }
  113. return (void *)p;
  114. }
  115. static void * iiP2V(void *data)
  116. {
  117. poly p=(poly)data;
  118. if (p!=NULL) pSetCompP(p,1);
  119. return (void *)p;
  120. }
  121. static void * iiBu2Id(void *data)
  122. {
  123. ideal I=idInit(1,1);
  124. if (data!=NULL)
  125. {
  126. sBucket_pt b=(sBucket_pt)data;
  127. poly p; int l;
  128. sBucketDestroyAdd(b,&p,&l);
  129. I->m[0]=p;
  130. }
  131. return (void *)I;
  132. }
  133. static void * iiP2Id(void *data)
  134. {
  135. ideal I=idInit(1,1);
  136. if (data!=NULL)
  137. {
  138. poly p=(poly)data;
  139. I->m[0]=p;
  140. if (pGetComp(p)!=0) I->rank=pMaxComp(p);
  141. }
  142. return (void *)I;
  143. }
  144. static void * iiV2Ma(void *data)
  145. {
  146. matrix m=(matrix)idVec2Ideal((poly)data);
  147. int h=MATCOLS(m);
  148. MATCOLS(m)=MATROWS(m);
  149. MATROWS(m)=h;
  150. m->rank=h;
  151. pDelete((poly *)&data);
  152. return (void *)m;
  153. }
  154. static void * iiN2P(void *data);
  155. static void * iiDummy(void *data)
  156. {
  157. return data;
  158. }
  159. static void * iiMo2Ma(void *data)
  160. {
  161. void *res=id_Module2Matrix((ideal)data,currRing);
  162. return res;
  163. }
  164. static void * iiMa2Mo(void *data)
  165. {
  166. void *res=id_Matrix2Module((matrix)data,currRing);
  167. return res;
  168. }
  169. static void * iiI2Iv(void *data)
  170. {
  171. int s=(int)(long)data;
  172. intvec *iv=new intvec(s,s);
  173. return (void *)iv;
  174. }
  175. static void * iiI2N(void *data)
  176. {
  177. number n=nInit((int)(long)data);
  178. return (void *)n;
  179. }
  180. static void * iiI2BI(void *data)
  181. {
  182. number n=n_Init((int)(long)data, coeffs_BIGINT);
  183. return (void *)n;
  184. }
  185. #ifdef SINGULAR_4_2
  186. static void * iiI2NN(void *data)
  187. {
  188. if (currRing==NULL)
  189. {
  190. WerrorS("missing basering while converting int to Number");
  191. return NULL;
  192. }
  193. number n=nInit((int)(long)data);
  194. number2 nn=(number2)omAlloc(sizeof(*nn));
  195. nn->cf=currRing->cf; nn->cf->ref++;
  196. nn->n=n;
  197. return (void *)nn;
  198. }
  199. static void * iiI2CP(void *data)
  200. {
  201. if (currRing==NULL)
  202. {
  203. WerrorS("missing basering while converting int to Poly");
  204. return NULL;
  205. }
  206. poly n=pISet((int)(long)data);
  207. poly2 nn=(poly2)omAlloc(sizeof(*nn));
  208. nn->cf=currRing; nn->cf->ref++;
  209. nn->n=n;
  210. return (void *)nn;
  211. }
  212. #endif
  213. static void * iiBI2N(void *data)
  214. {
  215. if (currRing==NULL) return NULL;
  216. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  217. if (nMap==NULL)
  218. {
  219. Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
  220. return NULL;
  221. }
  222. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  223. n_Delete((number *)&data, coeffs_BIGINT);
  224. return (void*)n;
  225. }
  226. #ifdef SINGULAR_4_2
  227. static void * iiBI2NN(void *data)
  228. {
  229. if (currRing==NULL)
  230. {
  231. WerrorS("missing basering while converting bigint to Number");
  232. return NULL;
  233. }
  234. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  235. if (nMap==NULL)
  236. {
  237. Werror("no conversion from bigint to %s",currRing->cf->cfCoeffName(currRing->cf));
  238. return NULL;
  239. }
  240. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  241. n_Delete((number *)&data, coeffs_BIGINT);
  242. number2 nn=(number2)omAlloc(sizeof(*nn));
  243. nn->cf=currRing->cf; nn->cf->ref++;
  244. nn->n=n;
  245. return (void*)nn;
  246. }
  247. static void * iiBI2CP(void *data)
  248. {
  249. if (currRing==NULL)
  250. {
  251. WerrorS("missing basering while converting bigint to Poly");
  252. return NULL;
  253. }
  254. nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
  255. if (nMap==NULL)
  256. {
  257. Werror("no conversion from bigint to %s",currRing->cf->cfCoeffName(currRing->cf));
  258. return NULL;
  259. }
  260. number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
  261. n_Delete((number *)&data, coeffs_BIGINT);
  262. poly2 nn=(poly2)omAlloc(sizeof(*nn));
  263. nn->cf=currRing; nn->cf->ref++;
  264. nn->n=pNSet(n);
  265. return (void*)nn;
  266. }
  267. static void * iiP2CP(void *data)
  268. {
  269. poly2 nn=(poly2)omAlloc(sizeof(*nn));
  270. nn->cf=currRing; nn->cf->ref++;
  271. nn->n=(poly)data;
  272. return (void*)nn;
  273. }
  274. #endif
  275. #ifdef SINGULAR_4_2
  276. static void * iiNN2N(void *data)
  277. {
  278. number2 d=(number2)data;
  279. if ((currRing==NULL)
  280. || (currRing->cf!=d->cf))
  281. {
  282. WerrorS("cannot convert: incompatible");
  283. return NULL;
  284. }
  285. number n = n_Copy(d->n, d->cf);
  286. n2Delete(d);
  287. return (void*)n;
  288. }
  289. #endif
  290. #ifdef SINGULAR_4_2
  291. static void * iiNN2P(void *data)
  292. {
  293. number2 d=(number2)data;
  294. if ((currRing==NULL)
  295. || (currRing->cf!=d->cf))
  296. {
  297. WerrorS("cannot convert: incompatible");
  298. return NULL;
  299. }
  300. number n = n_Copy(d->n, d->cf);
  301. n2Delete(d);
  302. return (void*)p_NSet(n,currRing);
  303. }
  304. #endif
  305. static void * iiIm2Ma(void *data)
  306. {
  307. int i, j;
  308. intvec *iv = (intvec *)data;
  309. matrix m = mpNew(iv->rows(), iv->cols());
  310. for (i=iv->rows(); i>0; i--)
  311. {
  312. for (j=iv->cols(); j>0; j--)
  313. {
  314. MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j));
  315. }
  316. }
  317. delete iv;
  318. return (void *)m;
  319. }
  320. static void * iiIm2Bim(void *data)
  321. {
  322. intvec *iv=(intvec*)data;
  323. void *r=(void *)iv2bim(iv,coeffs_BIGINT);
  324. delete iv;
  325. return r;
  326. }
  327. static void * iiN2P(void *data)
  328. {
  329. poly p=NULL;
  330. if (!nIsZero((number)data))
  331. {
  332. p=pNSet((number)data);
  333. }
  334. //else
  335. //{
  336. // nDelete((number *)&data);
  337. //}
  338. return (void *)p;
  339. }
  340. static void * iiN2Ma(void *data)
  341. {
  342. ideal I=idInit(1,1);
  343. if (!nIsZero((number)data))
  344. {
  345. poly p=pNSet((number)data);
  346. I->m[0]=p;
  347. }
  348. //else
  349. //{
  350. // nDelete((number *)&data);
  351. //}
  352. return (void *)I;
  353. }
  354. static void * iiS2Link(void *data)
  355. {
  356. si_link l=(si_link)omAlloc0Bin(ip_link_bin);
  357. slInit(l, (char *) data);
  358. omFree((ADDRESS)data);
  359. return (void *)l;
  360. }
  361. static void iiR2L_l(leftv out, leftv in)
  362. {
  363. int add_row_shift = 0;
  364. intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD);
  365. if (weights!=NULL) add_row_shift=weights->min_in();
  366. syStrategy tmp=(syStrategy)in->CopyD();
  367. out->data=(void *)syConvRes(tmp,TRUE,add_row_shift);
  368. }
  369. static void iiL2R(leftv out, leftv in)
  370. {
  371. //int add_row_shift = 0;
  372. lists l=(lists)in->Data();
  373. intvec *ww=NULL;
  374. if (l->nr>=0) ww=(intvec *)atGet(&(l->m[0]),"isHomog",INTVEC_CMD);
  375. out->data=(void *)syConvList(l);
  376. if (ww!=NULL)
  377. {
  378. intvec *weights=ivCopy(ww);
  379. atSet(out,omStrDup("isHomog"),weights,INTVEC_CMD);
  380. }
  381. }
  382. //
  383. // automatic conversions:
  384. //
  385. #define IPCONV
  386. #define D(A) A
  387. #define NULL_VAL NULL
  388. #include "Singular/table.h"
  389. /*2
  390. * try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
  391. * return FALSE on success
  392. */
  393. BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output,const struct sConvertTypes *dConvertTypes)
  394. {
  395. output->Init();
  396. if ((inputType==outputType)
  397. || (outputType==DEF_CMD)
  398. || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
  399. {
  400. memcpy(output,input,sizeof(*output));
  401. input->Init();
  402. return FALSE;
  403. }
  404. else if (outputType==ANY_TYPE)
  405. {
  406. output->rtyp=ANY_TYPE;
  407. output->data=(char *)(long)input->Typ();
  408. /* the name of the object:*/
  409. if (input->e==NULL)
  410. {
  411. if (input->rtyp==IDHDL)
  412. /* preserve name: copy it */
  413. output->name=omStrDup(IDID((idhdl)(input->data)));
  414. else if (input->name!=NULL)
  415. {
  416. if (input->rtyp==ALIAS_CMD)
  417. output->name=omStrDup(input->name);
  418. else
  419. {
  420. output->name=input->name;
  421. input->name=NULL;
  422. }
  423. }
  424. else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
  425. {
  426. if (input->data!=NULL)
  427. {
  428. int nr=pIsPurePower((poly)input->data);
  429. if (nr!=0)
  430. {
  431. if (pGetExp((poly)input->data,nr)==1)
  432. {
  433. output->name=omStrDup(currRing->names[nr-1]);
  434. }
  435. else
  436. {
  437. char *tmp=(char *)omAlloc(4);
  438. sprintf(tmp,"%c%d",*(currRing->names[nr-1]),
  439. (int)pGetExp((poly)input->data,nr));
  440. output->name=tmp;
  441. }
  442. }
  443. else if(pIsConstant((poly)input->data))
  444. {
  445. StringSetS("");
  446. number n=(pGetCoeff((poly)input->data));
  447. n_Write(n, currRing->cf);
  448. (pGetCoeff((poly)input->data))=n; // n_Write may have changed n
  449. output->name=StringEndS();
  450. }
  451. }
  452. }
  453. else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
  454. {
  455. StringSetS("");
  456. number n=(number)input->data;
  457. n_Write(n, currRing->cf);
  458. input->data=(void*)n; // n_Write may have changed n
  459. output->name=StringEndS();
  460. }
  461. else
  462. {
  463. /* no need to preserve name: use it */
  464. output->name=input->name;
  465. input->name=NULL;
  466. }
  467. }
  468. output->next=input->next;
  469. input->next=NULL;
  470. if (!errorreported) input->CleanUp();
  471. return errorreported;
  472. }
  473. if (index!=0) /* iiTestConvert does not returned 'failure' */
  474. {
  475. index--;
  476. if((dConvertTypes[index].i_typ==inputType)
  477. &&(dConvertTypes[index].o_typ==outputType))
  478. {
  479. if(traceit&TRACE_CONV)
  480. {
  481. Print("automatic conversion %s -> %s\n",
  482. Tok2Cmdname(inputType),Tok2Cmdname(outputType));
  483. }
  484. if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
  485. return TRUE;
  486. output->rtyp=outputType;
  487. if (dConvertTypes[index].p!=NULL)
  488. {
  489. output->data=dConvertTypes[index].p(input->CopyD());
  490. }
  491. else
  492. {
  493. dConvertTypes[index].pl(output,input);
  494. }
  495. if ((output->data==NULL)
  496. && ((outputType!=INT_CMD)
  497. &&(outputType!=POLY_CMD)
  498. &&(outputType!=VECTOR_CMD)
  499. &&(outputType!=NUMBER_CMD)))
  500. {
  501. return TRUE;
  502. }
  503. if (errorreported) return TRUE;
  504. output->next=input->next;
  505. input->next=NULL;
  506. if ((input->rtyp!=IDHDL) && (input->attribute!=NULL))
  507. {
  508. input->attribute->killAll(currRing);
  509. input->attribute=NULL;
  510. }
  511. if (input->e!=NULL)
  512. {
  513. Subexpr h;
  514. while (input->e!=NULL)
  515. {
  516. h=input->e->next;
  517. omFreeBin((ADDRESS)input->e, sSubexpr_bin);
  518. input->e=h;
  519. }
  520. }
  521. //input->Init(); // seems that input (rtyp?) is still needed
  522. return FALSE;
  523. }
  524. }
  525. return TRUE;
  526. }
  527. /*2
  528. * try to convert 'inputType' in 'outputType'
  529. * return 0 on failure, an index (<>0) on success
  530. */
  531. int iiTestConvert (int inputType, int outputType,const struct sConvertTypes *dConvertTypes)
  532. {
  533. if ((inputType==outputType)
  534. || (outputType==DEF_CMD)
  535. || (outputType==IDHDL)
  536. || (outputType==ANY_TYPE))
  537. {
  538. return -1;
  539. }
  540. if (inputType==UNKNOWN) return 0;
  541. if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
  542. return 0;
  543. //if ((currRing==NULL) && (outputType==CNUMBER_CMD))
  544. // return 0;
  545. // search the list
  546. int i=0;
  547. while (dConvertTypes[i].i_typ!=0)
  548. {
  549. if((dConvertTypes[i].i_typ==inputType)
  550. &&(dConvertTypes[i].o_typ==outputType))
  551. {
  552. //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
  553. //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
  554. return i+1;
  555. }
  556. i++;
  557. }
  558. //Print("test convert %d to %d (%s -> %s):0, tested:%d\n",inputType,outputType,
  559. // Tok2Cmdname(inputType), Tok2Cmdname(outputType),i);
  560. return 0;
  561. }