Changeset bf6a4d in git
- Timestamp:
- Jul 26, 2011, 7:37:35 PM (12 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
- Children:
- 73ad0c1da96d24bf6cc659867bd85c7b02df2b6d
- Parents:
- 441a2ebc3368fca13446fd60801db4c09580a110
- git-author:
- Hans Schoenemann <hannes@mathematik.uni-kl.de>2011-07-26 19:37:35+02:00
- git-committer:
- Mohamed Barakat <mohamed.barakat@rwth-aachen.de>2011-11-09 13:01:15+01:00
- Location:
- libpolys/polys
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
libpolys/polys/Makefile.am
r441a2e rbf6a4d 50 50 pDebug.cc polys0.cc prCopy.cc prCopyMacros.h \ 51 51 kbuckets.cc sbuckets.cc weight.cc weight0.c simpleideals.cc matpol.cc \ 52 sparsmat.cc \ 52 53 ${USE_P_PROCS_STATIC_CC} ${USE_P_PROCS_DYNAMIC_CC} mod_raw.cc \ 53 54 ext_fields/algext.cc ext_fields/algext.h \ … … 68 69 monomials/p_polys.h monomials/maps.h polys.h prCopy.h \ 69 70 kbuckets.h sbuckets.h simpleideals.h weight.h matpol.h \ 70 clapsing.h clapconv.h71 sparsmat.h clapsing.h clapconv.h 71 72 72 73 ### nobase_include_HEADERS = $(LIBPOLYSHEADERS) -
libpolys/polys/matpol.cc
r441a2e rbf6a4d 813 813 }; 814 814 815 row_col_weight::row_col_weight(int i, int j) 816 { 817 ym = i; 818 yn = j; 819 wrow = (float *)omAlloc(i*sizeof(float)); 820 wcol = (float *)omAlloc(j*sizeof(float)); 821 } 822 row_col_weight::~row_col_weight() 823 { 824 if (ym!=0) 825 { 826 omFreeSize((ADDRESS)wcol, yn*sizeof(float)); 827 omFreeSize((ADDRESS)wrow, ym*sizeof(float)); 828 } 829 } 830 815 831 /*2 816 832 * a submatrix M of a matrix X[m,n]: … … 829 845 int *qrow, *qcol; 830 846 poly *Xarray; 847 ring _R; 831 848 void mpInitMat(); 832 poly * mpRowAdr(int); 833 poly * mpColAdr(int); 849 poly * mpRowAdr(int r) 850 { return &(Xarray[a_n*qrow[r]]); } 851 poly * mpColAdr(int c) 852 { return &(Xarray[qcol[c]]); } 834 853 void mpRowWeight(float *); 835 854 void mpColWeight(float *); … … 838 857 public: 839 858 mp_permmatrix() : a_m(0) {} 840 mp_permmatrix(matrix );859 mp_permmatrix(matrix, ring); 841 860 mp_permmatrix(mp_permmatrix *); 842 861 ~mp_permmatrix(); 843 862 int mpGetRow(); 844 863 int mpGetCol(); 845 int mpGetRdim() ;846 int mpGetCdim() ;847 int mpGetSign() ;864 int mpGetRdim() { return s_m; } 865 int mpGetCdim() { return s_n; } 866 int mpGetSign() { return sign; } 848 867 void mpSetSearch(int s); 849 void mpSaveArray() ;868 void mpSaveArray() { Xarray = NULL; } 850 869 poly mpGetElem(int, int); 851 870 void mpSetElem(poly, int, int); … … 858 877 void mpColReorder(); 859 878 }; 860 879 mp_permmatrix::mp_permmatrix(matrix A, ring R) : sign(1) 880 { 881 a_m = A->nrows; 882 a_n = A->ncols; 883 this->mpInitMat(); 884 Xarray = A->m; 885 _R=R; 886 } 887 888 mp_permmatrix::mp_permmatrix(mp_permmatrix *M) 889 { 890 poly p, *athis, *aM; 891 int i, j; 892 893 _R=M->_R; 894 a_m = M->s_m; 895 a_n = M->s_n; 896 sign = M->sign; 897 this->mpInitMat(); 898 Xarray = (poly *)omAlloc0(a_m*a_n*sizeof(poly)); 899 for (i=a_m-1; i>=0; i--) 900 { 901 athis = this->mpRowAdr(i); 902 aM = M->mpRowAdr(i); 903 for (j=a_n-1; j>=0; j--) 904 { 905 p = aM[M->qcol[j]]; 906 if (p) 907 { 908 athis[j] = p_Copy(p,_R); 909 } 910 } 911 } 912 } 913 914 mp_permmatrix::~mp_permmatrix() 915 { 916 int k; 917 918 if (a_m != 0) 919 { 920 omFreeSize((ADDRESS)qrow,a_m*sizeof(int)); 921 omFreeSize((ADDRESS)qcol,a_n*sizeof(int)); 922 if (Xarray != NULL) 923 { 924 for (k=a_m*a_n-1; k>=0; k--) 925 p_Delete(&Xarray[k],_R); 926 omFreeSize((ADDRESS)Xarray,a_m*a_n*sizeof(poly)); 927 } 928 } 929 } 930 931 932 static float mp_PolyWeight(poly p, const ring r); 933 void mp_permmatrix::mpColWeight(float *wcol) 934 { 935 poly p, *a; 936 int i, j; 937 float count; 938 939 for (j=s_n; j>=0; j--) 940 { 941 a = this->mpColAdr(j); 942 count = 0.0; 943 for(i=s_m; i>=0; i--) 944 { 945 p = a[a_n*qrow[i]]; 946 if (p) 947 count += mp_PolyWeight(p,_R); 948 } 949 wcol[j] = count; 950 } 951 } 952 void mp_permmatrix::mpRowWeight(float *wrow) 953 { 954 poly p, *a; 955 int i, j; 956 float count; 957 958 for (i=s_m; i>=0; i--) 959 { 960 a = this->mpRowAdr(i); 961 count = 0.0; 962 for(j=s_n; j>=0; j--) 963 { 964 p = a[qcol[j]]; 965 if (p) 966 count += mp_PolyWeight(p,_R); 967 } 968 wrow[i] = count; 969 } 970 } 971 972 void mp_permmatrix::mpRowSwap(int i1, int i2) 973 { 974 poly p, *a1, *a2; 975 int j; 976 977 a1 = &(Xarray[a_n*i1]); 978 a2 = &(Xarray[a_n*i2]); 979 for (j=a_n-1; j>= 0; j--) 980 { 981 p = a1[j]; 982 a1[j] = a2[j]; 983 a2[j] = p; 984 } 985 } 986 987 void mp_permmatrix::mpColSwap(int j1, int j2) 988 { 989 poly p, *a1, *a2; 990 int i, k = a_n*a_m; 991 992 a1 = &(Xarray[j1]); 993 a2 = &(Xarray[j2]); 994 for (i=0; i< k; i+=a_n) 995 { 996 p = a1[i]; 997 a1[i] = a2[i]; 998 a2[i] = p; 999 } 1000 } 1001 void mp_permmatrix::mpInitMat() 1002 { 1003 int k; 1004 1005 s_m = a_m; 1006 s_n = a_n; 1007 piv_s = 0; 1008 qrow = (int *)omAlloc(a_m*sizeof(int)); 1009 qcol = (int *)omAlloc(a_n*sizeof(int)); 1010 for (k=a_m-1; k>=0; k--) qrow[k] = k; 1011 for (k=a_n-1; k>=0; k--) qcol[k] = k; 1012 } 1013 1014 void mp_permmatrix::mpColReorder() 1015 { 1016 int k, j, j1, j2; 1017 1018 if (a_n > a_m) 1019 k = a_n - a_m; 1020 else 1021 k = 0; 1022 for (j=a_n-1; j>=k; j--) 1023 { 1024 j1 = qcol[j]; 1025 if (j1 != j) 1026 { 1027 this->mpColSwap(j1, j); 1028 j2 = 0; 1029 while (qcol[j2] != j) j2++; 1030 qcol[j2] = j1; 1031 } 1032 } 1033 } 1034 1035 void mp_permmatrix::mpRowReorder() 1036 { 1037 int k, i, i1, i2; 1038 1039 if (a_m > a_n) 1040 k = a_m - a_n; 1041 else 1042 k = 0; 1043 for (i=a_m-1; i>=k; i--) 1044 { 1045 i1 = qrow[i]; 1046 if (i1 != i) 1047 { 1048 this->mpRowSwap(i1, i); 1049 i2 = 0; 1050 while (qrow[i2] != i) i2++; 1051 qrow[i2] = i1; 1052 } 1053 } 1054 } 1055 1056 /* 1057 * perform replacement for pivot strategy in Bareiss algorithm 1058 * change sign of determinant 1059 */ 1060 static void mpReplace(int j, int n, int &sign, int *perm) 1061 { 1062 int k; 1063 1064 if (j != n) 1065 { 1066 k = perm[n]; 1067 perm[n] = perm[j]; 1068 perm[j] = k; 1069 sign = -sign; 1070 } 1071 } 1072 /*2 1073 * pivot strategy for Bareiss algorithm 1074 */ 1075 int mp_permmatrix::mpPivotBareiss(row_col_weight *C) 1076 { 1077 poly p, *a; 1078 int i, j, iopt, jopt; 1079 float sum, f1, f2, fo, r, ro, lp; 1080 float *dr = C->wrow, *dc = C->wcol; 1081 1082 fo = 1.0e20; 1083 ro = 0.0; 1084 iopt = jopt = -1; 1085 1086 s_n--; 1087 s_m--; 1088 if (s_m == 0) 1089 return 0; 1090 if (s_n == 0) 1091 { 1092 for(i=s_m; i>=0; i--) 1093 { 1094 p = this->mpRowAdr(i)[qcol[0]]; 1095 if (p) 1096 { 1097 f1 = mp_PolyWeight(p,_R); 1098 if (f1 < fo) 1099 { 1100 fo = f1; 1101 if (iopt >= 0) 1102 p_Delete(&(this->mpRowAdr(iopt)[qcol[0]]),_R); 1103 iopt = i; 1104 } 1105 else 1106 p_Delete(&(this->mpRowAdr(i)[qcol[0]]),_R); 1107 } 1108 } 1109 if (iopt >= 0) 1110 mpReplace(iopt, s_m, sign, qrow); 1111 return 0; 1112 } 1113 this->mpRowWeight(dr); 1114 this->mpColWeight(dc); 1115 sum = 0.0; 1116 for(i=s_m; i>=0; i--) 1117 sum += dr[i]; 1118 for(i=s_m; i>=0; i--) 1119 { 1120 r = dr[i]; 1121 a = this->mpRowAdr(i); 1122 for(j=s_n; j>=0; j--) 1123 { 1124 p = a[qcol[j]]; 1125 if (p) 1126 { 1127 lp = mp_PolyWeight(p,_R); 1128 ro = r - lp; 1129 f1 = ro * (dc[j]-lp); 1130 if (f1 != 0.0) 1131 { 1132 f2 = lp * (sum - ro - dc[j]); 1133 f2 += f1; 1134 } 1135 else 1136 f2 = lp-r-dc[j]; 1137 if (f2 < fo) 1138 { 1139 fo = f2; 1140 iopt = i; 1141 jopt = j; 1142 } 1143 } 1144 } 1145 } 1146 if (iopt < 0) 1147 return 0; 1148 mpReplace(iopt, s_m, sign, qrow); 1149 mpReplace(jopt, s_n, sign, qcol); 1150 return 1; 1151 } 1152 poly mp_permmatrix::mpGetElem(int r, int c) 1153 { 1154 return Xarray[a_n*qrow[r]+qcol[c]]; 1155 } 1156 1157 /* 1158 * the Bareiss-type elimination with division by div (div != NULL) 1159 */ 1160 void mp_permmatrix::mpElimBareiss(poly div) 1161 { 1162 poly piv, elim, q1, q2, *ap, *a; 1163 int i, j, jj; 1164 1165 ap = this->mpRowAdr(s_m); 1166 piv = ap[qcol[s_n]]; 1167 for(i=s_m-1; i>=0; i--) 1168 { 1169 a = this->mpRowAdr(i); 1170 elim = a[qcol[s_n]]; 1171 if (elim != NULL) 1172 { 1173 elim = p_Neg(elim,_R); 1174 for (j=s_n-1; j>=0; j--) 1175 { 1176 q2 = NULL; 1177 jj = qcol[j]; 1178 if (ap[jj] != NULL) 1179 { 1180 q2 = SM_MULT(ap[jj], elim, div,_R); 1181 if (a[jj] != NULL) 1182 { 1183 q1 = SM_MULT(a[jj], piv, div,_R); 1184 p_Delete(&a[jj],_R); 1185 q2 = p_Add_q(q2, q1, _R); 1186 } 1187 } 1188 else if (a[jj] != NULL) 1189 { 1190 q2 = SM_MULT(a[jj], piv, div, _R); 1191 } 1192 if ((q2!=NULL) && div) 1193 SM_DIV(q2, div, _R); 1194 a[jj] = q2; 1195 } 1196 p_Delete(&a[qcol[s_n]], _R); 1197 } 1198 else 1199 { 1200 for (j=s_n-1; j>=0; j--) 1201 { 1202 jj = qcol[j]; 1203 if (a[jj] != NULL) 1204 { 1205 q2 = SM_MULT(a[jj], piv, div, _R); 1206 p_Delete(&a[jj], _R); 1207 if (div) 1208 SM_DIV(q2, div, _R); 1209 a[jj] = q2; 1210 } 1211 } 1212 } 1213 } 1214 } 861 1215 /* 862 1216 * weigth of a polynomial, for pivot strategy … … 1200 1554 } 1201 1555 matrix c = mp_Copy(a,r); 1202 mp_permmatrix *Bareiss = new mp_permmatrix(c );1556 mp_permmatrix *Bareiss = new mp_permmatrix(c,r); 1203 1557 row_col_weight w(Bareiss->mpGetRdim(), Bareiss->mpGetCdim()); 1204 1558 -
libpolys/polys/sparsmat.cc
r441a2e rbf6a4d 147 147 smpoly piv, oldpiv; // pivot and previous pivot 148 148 smpoly dumm; // allocated dummy 149 constring _R;149 ring _R; 150 150 151 151 void smColToRow(); … … 261 261 262 262 /* ----------------- ops with rings ------------------ */ 263 ring sm_RingChange(const ring currRing, long bound)263 ring sm_RingChange(const ring origR, long bound) 264 264 { 265 265 // *origR =currRing; 266 ring tmpR=rCopy0( currRing,FALSE,FALSE);266 ring tmpR=rCopy0(origR,FALSE,FALSE); 267 267 int *ord=(int*)omAlloc0(3*sizeof(int)); 268 268 int *block0=(int*)omAlloc(3*sizeof(int)); … … 283 283 284 284 rComplete(tmpR,1); 285 if ((*origR)->qideal!=NULL) 286 { 287 tmpR->qideal= idrCopyR_NoSort((*origR)->qideal, currRing, tmpR); 288 } 289 // rChangeCurrRing(tmpR); 285 if (origR->qideal!=NULL) 286 { 287 tmpR->qideal= idrCopyR_NoSort(origR->qideal, origR, tmpR); 288 } 290 289 if (TEST_OPT_PROT) 291 290 Print("[%ld:%d]", (long) tmpR->bitmask, tmpR->ExpL_Size); … … 307 306 * FALSE -> same Type 308 307 */ 309 BOOLEAN sm CheckDet(ideal I, int d, BOOLEAN sw)308 BOOLEAN sm_CheckDet(ideal I, int d, BOOLEAN sw, const ring r) 310 309 { 311 310 int s,t,i; 312 311 poly p; 313 312 314 if ((d>100) || ( currRing->parameter!=NULL))313 if ((d>100) || (rIsExtension(r))) 315 314 return sw; 316 if ( rField_is_Q(currRing)==FALSE)315 if (!rField_is_Q(r)) 317 316 return sw; 318 317 s = t = 0; … … 324 323 if (p!=NULL) 325 324 { 326 if(!p IsConstant(p))325 if(!p_IsConstant(p,r)) 327 326 return sw; 328 327 s++; 329 t+=n Size(pGetCoeff(p));328 t+=n_Size(pGetCoeff(p),r->cf); 330 329 } 331 330 } … … 336 335 { 337 336 p=I->m[i]; 338 if (!p IsConstantPoly(p))337 if (!p_IsConstantPoly(p,r)) 339 338 return sw; 340 339 while (p!=NULL) 341 340 { 342 341 s++; 343 t+=n Size(pGetCoeff(p));342 t+=n_Size(pGetCoeff(p),r->cf); 344 343 pIter(p); 345 344 } … … 408 407 *uses Bareiss algorithm 409 408 */ 410 poly sm CallDet(ideal I)409 poly sm_CallDet(ideal I,const ring R) 411 410 { 412 411 if (I->ncols != I->rank) … … 415 414 return NULL; 416 415 } 417 int r=id RankFreeModule(I);416 int r=id_RankFreeModule(I,R); 418 417 if (I->ncols != r) // some 0-lines at the end 419 418 { 420 419 return NULL; 421 420 } 422 long bound=sm ExpBound(I,r,r,r);423 number diag,h=n Init(1);421 long bound=sm_ExpBound(I,r,r,r,R); 422 number diag,h=n_Init(1,R->cf); 424 423 poly res; 425 ring origR;426 424 ring tmpR; 427 425 sparse_mat *det; 428 426 ideal II; 429 427 430 tmpR=sm RingChange(&origR,bound);431 II = idrCopyR(I, origR);432 diag = sm Cleardenom(II);433 det = new sparse_mat(II );434 id Delete(&II);428 tmpR=sm_RingChange(R,bound); 429 II = idrCopyR(I, R, tmpR); 430 diag = sm_Cleardenom(II,tmpR); 431 det = new sparse_mat(II,tmpR); 432 id_Delete(&II,tmpR); 435 433 if (det->smGetAct() == NULL) 436 434 { 437 435 delete det; 438 rChangeCurrRing(origR); 439 smKillModifiedRing(tmpR); 436 sm_KillModifiedRing(tmpR); 440 437 return NULL; 441 438 } 442 439 res=det->smDet(); 443 if(det->smGetSign()<0) res=p Neg(res);440 if(det->smGetSign()<0) res=p_Neg(res,tmpR); 444 441 delete det; 445 rChangeCurrRing(origR); 446 res = prMoveR(res, tmpR); 447 smKillModifiedRing(tmpR); 448 if (nEqual(diag,h) == FALSE) 449 { 450 pMult_nn(res,diag); 451 pNormalize(res); 452 } 453 nDelete(&diag); 454 nDelete(&h); 442 res = prMoveR(res, tmpR, R); 443 sm_KillModifiedRing(tmpR); 444 if (!n_Equal(diag,h,R->cf)) 445 { 446 p_Mult_nn(res,diag,R); 447 p_Normalize(res,R); 448 } 449 n_Delete(&diag,R->cf); 450 n_Delete(&h,R->cf); 455 451 return res; 456 452 } 457 453 458 void sm CallBareiss(ideal I, int x, int y, ideal & M, intvec **iv)459 { 460 int r=id RankFreeModule(I),t=r;454 void sm_CallBareiss(ideal I, int x, int y, ideal & M, intvec **iv, const ring R) 455 { 456 int r=id_RankFreeModule(I,R),t=r; 461 457 int c=IDELEMS(I),s=c; 462 458 long bound; 463 ring origR;464 459 ring tmpR; 465 460 sparse_mat *bareiss; … … 470 465 s-=y; 471 466 if (t>s) t=s; 472 bound=sm ExpBound(I,c,r,t);473 tmpR=sm RingChange(&origR,bound);474 ideal II = idrCopyR(I, origR);475 bareiss = new sparse_mat(II );467 bound=sm_ExpBound(I,c,r,t,R); 468 tmpR=sm_RingChange(R,bound); 469 ideal II = idrCopyR(I, R, tmpR); 470 bareiss = new sparse_mat(II,tmpR); 476 471 if (bareiss->smGetAct() == NULL) 477 472 { 478 473 delete bareiss; 479 *iv=new intvec(1,pVariables); 480 rChangeCurrRing(origR); 474 *iv=new intvec(1,rVar(tmpR)); 481 475 } 482 476 else 483 477 { 484 id Delete(&II);478 id_Delete(&II,tmpR); 485 479 bareiss->smNewBareiss(x, y); 486 480 II = bareiss->smRes2Mod(); … … 488 482 bareiss->smToIntvec(*iv); 489 483 delete bareiss; 490 rChangeCurrRing(origR); 491 II = idrMoveR(II,tmpR); 492 } 493 smKillModifiedRing(tmpR); 484 II = idrMoveR(II,tmpR,R); 485 } 486 sm_KillModifiedRing(tmpR); 494 487 M=II; 495 488 } … … 535 528 * constructor 536 529 */ 537 sparse_mat::sparse_mat(ideal smat )530 sparse_mat::sparse_mat(ideal smat, const ring RR) 538 531 { 539 532 int i; 540 polyset pmat; 533 poly* pmat; 534 _R=RR; 541 535 542 536 ncols = smat->ncols; 543 nrows = id RankFreeModule(smat);537 nrows = id_RankFreeModule(smat,RR); 544 538 if (nrows <= 0) 545 539 { … … 566 560 for(i=ncols; i; i--) 567 561 { 568 m_act[i] = sm Poly2Smpoly(pmat[i-1]);562 m_act[i] = sm_Poly2Smpoly(pmat[i-1], RR); 569 563 pmat[i-1] = NULL; 570 564 } … … 637 631 * transform the result to a module 638 632 */ 633 639 634 ideal sparse_mat::smRes2Mod() 640 635 { … … 642 637 int i; 643 638 644 for (i=crd; i; i--) res->m[i-1] = sm Smpoly2Poly(m_res[i]);645 res->rank = id RankFreeModule(res);639 for (i=crd; i; i--) res->m[i-1] = sm_Smpoly2Poly(m_res[i],_R); 640 res->rank = id_RankFreeModule(res,_R); 646 641 return res; 647 642 } … … 845 840 if (a->pos > tored) 846 841 break; 847 w = a->f = sm PolyWeight(a);842 w = a->f = sm_PolyWeight(a,_R); 848 843 wc += w; 849 844 wrw[a->pos] += w; … … 1044 1039 if ((c == NULL) || (r == NULL)) 1045 1040 { 1046 while (r!=NULL) sm ElemDelete(&r);1041 while (r!=NULL) sm_ElemDelete(&r,_R); 1047 1042 return; 1048 1043 } … … 1061 1056 { 1062 1057 res = res->n = smElemCopy(b); 1063 res->m = pp Mult_qq(b->m, w);1058 res->m = pp_Mult_qq(b->m, w,_R); 1064 1059 res->e = 1; 1065 res->f = sm PolyWeight(res);1060 res->f = sm_PolyWeight(res,_R); 1066 1061 b = b->n; 1067 1062 } while (b != NULL); … … 1076 1071 { 1077 1072 res = res->n = smElemCopy(b); 1078 res->m = pp Mult_qq(b->m, w);1073 res->m = pp_Mult_qq(b->m, w,_R); 1079 1074 res->e = 1; 1080 res->f = sm PolyWeight(res);1075 res->f = sm_PolyWeight(res,_R); 1081 1076 b = b->n; 1082 1077 } 1083 1078 else 1084 1079 { 1085 ha = pp Mult_qq(a->m, p);1086 p Delete(&a->m);1087 hb = pp Mult_qq(b->m, w);1088 ha = p Add(ha, hb);1080 ha = pp_Mult_qq(a->m, p,_R); 1081 p_Delete(&a->m,_R); 1082 hb = pp_Mult_qq(b->m, w,_R); 1083 ha = p_Add_q(ha, hb,_R); 1089 1084 if (ha != NULL) 1090 1085 { 1091 1086 a->m = ha; 1092 1087 a->e = 1; 1093 a->f = sm PolyWeight(a);1088 a->f = sm_PolyWeight(a,_R); 1094 1089 res = res->n = a; 1095 1090 a = a->n; … … 1097 1092 else 1098 1093 { 1099 sm ElemDelete(&a);1094 sm_ElemDelete(&a,_R); 1100 1095 } 1101 1096 b = b->n; … … 1108 1103 } 1109 1104 m_act[r->pos] = dumm->n; 1110 sm ElemDelete(&r);1105 sm_ElemDelete(&r,_R); 1111 1106 } while (r != NULL); 1112 1107 } … … 1125 1120 if ((c == NULL) || (r == NULL)) 1126 1121 { 1127 while(r!=NULL) sm ElemDelete(&r);1128 p Delete(&hp);1122 while(r!=NULL) sm_ElemDelete(&r,_R); 1123 p_Delete(&hp,_R); 1129 1124 return; 1130 1125 } … … 1146 1141 { 1147 1142 res = res->n = smElemCopy(b); 1148 x = SM_MULT(b->m, hr, m_res[ir]->m );1143 x = SM_MULT(b->m, hr, m_res[ir]->m,_R); 1149 1144 b = b->n; 1150 if(ir) SM_DIV(x, m_res[ir]->m );1145 if(ir) SM_DIV(x, m_res[ir]->m,_R); 1151 1146 res->m = x; 1152 1147 res->e = e; 1153 res->f = sm PolyWeight(res);1148 res->f = sm_PolyWeight(res,_R); 1154 1149 } while (b != NULL); 1155 1150 break; … … 1163 1158 { 1164 1159 res = res->n = smElemCopy(b); 1165 x = SM_MULT(b->m, hr, m_res[ir]->m );1160 x = SM_MULT(b->m, hr, m_res[ir]->m,_R); 1166 1161 b = b->n; 1167 if(ir) SM_DIV(x, m_res[ir]->m );1162 if(ir) SM_DIV(x, m_res[ir]->m,_R); 1168 1163 res->m = x; 1169 1164 res->e = e; 1170 res->f = sm PolyWeight(res);1165 res->f = sm_PolyWeight(res,_R); 1171 1166 } 1172 1167 else … … 1178 1173 if (ir > ia) 1179 1174 { 1180 x = SM_MULT(ha, m_res[ir]->m, m_res[ia]->m );1181 p Delete(&ha);1175 x = SM_MULT(ha, m_res[ir]->m, m_res[ia]->m,_R); 1176 p_Delete(&ha,_R); 1182 1177 ha = x; 1183 if (ia) SM_DIV(ha, m_res[ia]->m );1178 if (ia) SM_DIV(ha, m_res[ia]->m,_R); 1184 1179 ia = ir; 1185 1180 } 1186 x = SM_MULT(ha, gp, m_res[ia]->m );1187 p Delete(&ha);1188 y = SM_MULT(b->m, hr, m_res[ia]->m );1181 x = SM_MULT(ha, gp, m_res[ia]->m,_R); 1182 p_Delete(&ha,_R); 1183 y = SM_MULT(b->m, hr, m_res[ia]->m,_R); 1189 1184 } 1190 1185 else if (ir >= ip) … … 1192 1187 if (ia < crd) 1193 1188 { 1194 x = SM_MULT(ha, m_res[crd]->m, m_res[ia]->m );1195 p Delete(&ha);1189 x = SM_MULT(ha, m_res[crd]->m, m_res[ia]->m,_R); 1190 p_Delete(&ha,_R); 1196 1191 ha = x; 1197 SM_DIV(ha, m_res[ia]->m );1192 SM_DIV(ha, m_res[ia]->m,_R); 1198 1193 } 1199 1194 y = hp; 1200 1195 if(ir > ip) 1201 1196 { 1202 y = SM_MULT(y, m_res[ir]->m, m_res[ip]->m );1203 if (ip) SM_DIV(y, m_res[ip]->m );1197 y = SM_MULT(y, m_res[ir]->m, m_res[ip]->m,_R); 1198 if (ip) SM_DIV(y, m_res[ip]->m,_R); 1204 1199 } 1205 1200 ia = ir; 1206 x = SM_MULT(ha, y, m_res[ia]->m );1207 if (y != hp) p Delete(&y);1208 p Delete(&ha);1209 y = SM_MULT(b->m, hr, m_res[ia]->m );1201 x = SM_MULT(ha, y, m_res[ia]->m,_R); 1202 if (y != hp) p_Delete(&y,_R); 1203 p_Delete(&ha,_R); 1204 y = SM_MULT(b->m, hr, m_res[ia]->m,_R); 1210 1205 } 1211 1206 else 1212 1207 { 1213 x = SM_MULT(hr, m_res[ia]->m, m_res[ir]->m );1214 if (ir) SM_DIV(x, m_res[ir]->m );1215 y = SM_MULT(b->m, x, m_res[ia]->m );1216 p Delete(&x);1217 x = SM_MULT(ha, gp, m_res[ia]->m );1218 p Delete(&ha);1208 x = SM_MULT(hr, m_res[ia]->m, m_res[ir]->m,_R); 1209 if (ir) SM_DIV(x, m_res[ir]->m,_R); 1210 y = SM_MULT(b->m, x, m_res[ia]->m,_R); 1211 p_Delete(&x,_R); 1212 x = SM_MULT(ha, gp, m_res[ia]->m,_R); 1213 p_Delete(&ha,_R); 1219 1214 } 1220 ha = p Add(x, y);1215 ha = p_Add_q(x, y,_R); 1221 1216 if (ha != NULL) 1222 1217 { 1223 if (ia) SM_DIV(ha, m_res[ia]->m );1218 if (ia) SM_DIV(ha, m_res[ia]->m,_R); 1224 1219 a->m = ha; 1225 1220 a->e = e; 1226 a->f = sm PolyWeight(a);1221 a->f = sm_PolyWeight(a,_R); 1227 1222 res = res->n = a; 1228 1223 a = a->n; … … 1231 1226 { 1232 1227 a->m = NULL; 1233 sm ElemDelete(&a);1228 sm_ElemDelete(&a,_R); 1234 1229 } 1235 1230 b = b->n; … … 1242 1237 } 1243 1238 m_act[r->pos] = dumm->n; 1244 sm ElemDelete(&r);1239 sm_ElemDelete(&r,_R); 1245 1240 } while (r != NULL); 1246 p Delete(&hp);1241 p_Delete(&hp,_R); 1247 1242 } 1248 1243 … … 1293 1288 { 1294 1289 ap->n = a->n; 1295 a->m = p Neg(a->m);1290 a->m = p_Neg(a->m,_R); 1296 1291 b = b->n = a; 1297 1292 b->pos = i; … … 1303 1298 { 1304 1299 m_act[i] = a->n; 1305 a->m = p Neg(a->m);1300 a->m = p_Neg(a->m,_R); 1306 1301 b = b->n = a; 1307 1302 b->pos = i; … … 1606 1601 if (f < e) 1607 1602 { 1608 ha = SM_MULT(a->m, m_res[e]->m, m_res[f]->m );1609 p Delete(&a->m);1610 if (f) SM_DIV(ha, m_res[f]->m );1603 ha = SM_MULT(a->m, m_res[e]->m, m_res[f]->m,_R); 1604 p_Delete(&a->m,_R); 1605 if (f) SM_DIV(ha, m_res[f]->m,_R); 1611 1606 a->m = ha; 1612 if (normalize) p Normalize(a->m);1607 if (normalize) p_Normalize(a->m,_R); 1613 1608 } 1614 1609 a = a->n; … … 1634 1629 if (f < e) 1635 1630 { 1636 ha = SM_MULT(a->m, m_res[e]->m, m_res[f]->m );1637 p Delete(&a->m);1638 if (f) SM_DIV(ha, m_res[f]->m );1631 ha = SM_MULT(a->m, m_res[e]->m, m_res[f]->m, _R); 1632 p_Delete(&a->m,_R); 1633 if (f) SM_DIV(ha, m_res[f]->m, _R); 1639 1634 a->m = ha; 1640 1635 } 1641 if (normalize) p Normalize(a->m);1636 if (normalize) p_Normalize(a->m, _R); 1642 1637 a = a->n; 1643 1638 } while (a != NULL); … … 1658 1653 do 1659 1654 { 1660 if(sm HaveDenom(a->m)) return 1;1655 if(sm_HaveDenom(a->m,_R)) return 1; 1661 1656 a = a->n; 1662 1657 } while (a != NULL); … … 1679 1674 do 1680 1675 { 1681 if (e == a->e) p Normalize(a->m);1676 if (e == a->e) p_Normalize(a->m,_R); 1682 1677 a = a->n; 1683 1678 } while (a != NULL); … … 1696 1691 { 1697 1692 h = r = a->m; 1698 h = SM_MULT(h, m_res[crd]->m, m_res[f]->m );1699 if (f) SM_DIV(h, m_res[f]->m );1693 h = SM_MULT(h, m_res[crd]->m, m_res[f]->m, _R); 1694 if (f) SM_DIV(h, m_res[f]->m, _R); 1700 1695 a->m = h; 1701 if (normalize) p Normalize(a->m);1702 a->f = sm PolyWeight(a);1696 if (normalize) p_Normalize(a->m,_R); 1697 a->f = sm_PolyWeight(a,_R); 1703 1698 return r; 1704 1699 } … … 1720 1715 do 1721 1716 { 1722 sm ElemDelete(&a);1717 sm_ElemDelete(&a,_R); 1723 1718 } while (a != NULL); 1724 1719 } … … 1734 1729 while (a != NULL) 1735 1730 { 1736 sm ElemDelete(&a);1731 sm_ElemDelete(&a,_R); 1737 1732 } 1738 1733 } … … 1747 1742 while (i != 0) 1748 1743 { 1749 sm ElemDelete(&m_res[i]);1744 sm_ElemDelete(&m_res[i],_R); 1750 1745 i--; 1751 1746 } … … 1794 1789 * a destroyed, b NOT destroyed 1795 1790 */ 1796 void sm PolyDiv(poly a, poly b)1791 void sm_PolyDiv(poly a, poly b, const ring R) 1797 1792 { 1798 1793 const number x = pGetCoeff(b); … … 1805 1800 do 1806 1801 { 1807 if (!p LmIsConstantComp(b))1808 { 1809 for (i= pVariables; i; i--)1810 p SubExp(a,i,pGetExp(b,i));1811 p Setm(a);1812 } 1813 y = n Div(pGetCoeff(a),x);1814 n Normalize(y);1815 p SetCoeff(a,y);1802 if (!p_LmIsConstantComp(b,R)) 1803 { 1804 for (i=rVar(R); i; i--) 1805 p_SubExp(a,i,p_GetExp(b,i,R),R); 1806 p_Setm(a,R); 1807 } 1808 y = n_Div(pGetCoeff(a),x,R->cf); 1809 n_Normalize(y,R->cf); 1810 p_SetCoeff(a,y,R); 1816 1811 pIter(a); 1817 1812 } while (a != NULL); 1818 1813 return; 1819 1814 } 1820 dummy = p Init();1815 dummy = p_Init(R); 1821 1816 do 1822 1817 { 1823 for (i= pVariables; i; i--)1824 p SubExp(a,i,pGetExp(b,i));1825 p Setm(a);1826 y = n Div(pGetCoeff(a),x);1827 n Normalize(y);1828 p SetCoeff(a,y);1829 yn = n Neg(nCopy(y));1818 for (i=rVar(R); i; i--) 1819 p_SubExp(a,i,p_GetExp(b,i,R),R); 1820 p_Setm(a,R); 1821 y = n_Div(pGetCoeff(a),x,R->cf); 1822 n_Normalize(y,R->cf); 1823 p_SetCoeff(a,y,R); 1824 yn = n_Neg(n_Copy(y,R->cf),R->cf); 1830 1825 t = pNext(b); 1831 1826 h = dummy; 1832 1827 do 1833 1828 { 1834 h = pNext(h) = p Init();1829 h = pNext(h) = p_Init(R); 1835 1830 //pSetComp(h,0); 1836 for (i= pVariables; i; i--)1837 p SetExp(h,i,pGetExp(a,i)+pGetExp(t,i));1838 p Setm(h);1839 pSetCoeff0(h,n Mult(yn, pGetCoeff(t)));1831 for (i=rVar(R); i; i--) 1832 p_SetExp(h,i,p_GetExp(a,i,R)+p_GetExp(t,i,R),R); 1833 p_Setm(h,R); 1834 pSetCoeff0(h,n_Mult(yn, pGetCoeff(t),R->cf)); 1840 1835 pIter(t); 1841 1836 } while (t != NULL); 1842 n Delete(&yn);1837 n_Delete(&yn,R->cf); 1843 1838 pNext(h) = NULL; 1844 a = pNext(a) = p Add(pNext(a), pNext(dummy));1839 a = pNext(a) = p_Add_q(pNext(a), pNext(dummy),R); 1845 1840 } while (a!=NULL); 1846 p LmFree(dummy);1841 p_LmFree(dummy, R); 1847 1842 } 1848 1843 … … 2016 2011 } 2017 2012 res = NULL; 2018 e = p Init();2013 e = p_Init(); 2019 2014 lead = FALSE; 2020 2015 while (!lead) … … 2029 2024 { 2030 2025 lead = TRUE; 2031 r = pp Mult_mm(a, e);2026 r = pp_Mult__mm(a, e); 2032 2027 } 2033 2028 if (lead) … … 2045 2040 } 2046 2041 else 2047 res = p Add(res, r);2042 res = p_Add_q(res, r); 2048 2043 pIter(b); 2049 2044 if (b == NULL) … … 2090 2085 else 2091 2086 { 2092 r = pp Mult_mm(a, e);2087 r = pp_Mult__mm(a, e); 2093 2088 append = kBucket_ExtractLarger_Add_q(bucket, append, r, &la); 2094 2089 } … … 2111 2106 smCombineChain(&pa, r); 2112 2107 else 2113 pa = p Add(pa,r);2108 pa = p_Add_q(pa,r); 2114 2109 } 2115 2110 else 2116 2111 { 2117 r = pp Mult_mm(a, e);2112 r = pp_Mult__mm(a, e); 2118 2113 smCombineChain(&pa, r); 2119 2114 } … … 2130 2125 * returns the part of (a*b)/exp(lead(c)) with nonegative exponents 2131 2126 */ 2132 poly sm MultDiv(poly a, poly b, const poly c)2127 poly sm_MultDiv(poly a, poly b, const poly c, const ring R) 2133 2128 { 2134 2129 poly pa, e, res, r; 2135 2130 BOOLEAN lead; 2136 2131 2137 if ((c == NULL) || p LmIsConstantComp(c))2138 { 2139 return pp Mult_qq(a, b);2132 if ((c == NULL) || p_LmIsConstantComp(c,R)) 2133 { 2134 return pp_Mult_qq(a, b, R); 2140 2135 } 2141 2136 if (smSmaller(a, b)) … … 2146 2141 } 2147 2142 res = NULL; 2148 e = p Init();2143 e = p_Init(R); 2149 2144 lead = FALSE; 2150 2145 while (!lead) 2151 2146 { 2152 2147 pSetCoeff0(e,pGetCoeff(b)); 2153 if (sm IsNegQuot(e, b, c))2154 { 2155 lead = p LmDivisibleByNoComp(e, a);2156 r = sm SelectCopy_ExpMultDiv(a, e, b, c);2148 if (sm_IsNegQuot(e, b, c, R)) 2149 { 2150 lead = p_LmDivisibleByNoComp(e, a, R); 2151 r = sm_SelectCopy_ExpMultDiv(a, e, b, c, R); 2157 2152 } 2158 2153 else 2159 2154 { 2160 2155 lead = TRUE; 2161 r = pp Mult_mm(a, e);2156 r = pp_Mult_mm(a, e,R); 2162 2157 } 2163 2158 if (lead) … … 2165 2160 if (res != NULL) 2166 2161 { 2167 sm FindRef(&pa, &res, r);2162 sm_FindRef(&pa, &res, r, R); 2168 2163 if (pa == NULL) 2169 2164 lead = FALSE; … … 2175 2170 } 2176 2171 else 2177 res = p Add(res, r);2172 res = p_Add_q(res, r, R); 2178 2173 pIter(b); 2179 2174 if (b == NULL) 2180 2175 { 2181 p LmFree(e);2176 p_LmFree(e, R); 2182 2177 return res; 2183 2178 } … … 2186 2181 { 2187 2182 pSetCoeff0(e,pGetCoeff(b)); 2188 if (sm IsNegQuot(e, b, c))2189 { 2190 r = sm SelectCopy_ExpMultDiv(a, e, b, c);2191 if (p LmDivisibleByNoComp(e, a))2192 sm CombineChain(&pa, r);2183 if (sm_IsNegQuot(e, b, c, R)) 2184 { 2185 r = sm_SelectCopy_ExpMultDiv(a, e, b, c, R); 2186 if (p_LmDivisibleByNoComp(e, a,R)) 2187 sm_CombineChain(&pa, r, R); 2193 2188 else 2194 pa = p Add(pa,r);2189 pa = p_Add_q(pa,r,R); 2195 2190 } 2196 2191 else 2197 2192 { 2198 r = pp Mult_mm(a, e);2199 sm CombineChain(&pa, r);2193 r = pp_Mult_mm(a, e, R); 2194 sm_CombineChain(&pa, r, R); 2200 2195 } 2201 2196 pIter(b); 2202 2197 } while (b != NULL); 2203 p LmFree(e);2198 p_LmFree(e, R); 2204 2199 return res; 2205 2200 } 2206 2201 #endif 2207 /* ------------ internals arithmetic ------------- */2208 static void smExactPolyDiv(poly a, poly b)2209 {2210 const number x = pGetCoeff(b);2211 poly tail = pNext(b), e = pInit();2212 poly h;2213 number y, yn;2214 int lt = pLength(tail);2215 2216 if (lt + 1 >= SM_MIN_LENGTH_BUCKET && !TEST_OPT_NOT_BUCKETS)2217 {2218 kBucket_pt bucket = kBucketCreate(currRing);2219 kBucketInit(bucket, pNext(a), 0);2220 int lh = 0;2221 do2222 {2223 y = nDiv(pGetCoeff(a), x);2224 nNormalize(y);2225 pSetCoeff(a,y);2226 yn = nNeg(nCopy(y));2227 pSetCoeff0(e,yn);2228 lh = lt;2229 if (smIsNegQuot(e, a, b))2230 {2231 h = pp_Mult_Coeff_mm_DivSelect_MultDiv(tail, lh, e, a, b);2232 }2233 else2234 h = ppMult_mm(tail, e);2235 nDelete(&yn);2236 kBucket_Add_q(bucket, h, &lh);2237 2238 a = pNext(a) = kBucketExtractLm(bucket);2239 } while (a!=NULL);2240 kBucketDestroy(&bucket);2241 }2242 else2243 {2244 do2245 {2246 y = nDiv(pGetCoeff(a), x);2247 nNormalize(y);2248 pSetCoeff(a,y);2249 yn = nNeg(nCopy(y));2250 pSetCoeff0(e,yn);2251 if (smIsNegQuot(e, a, b))2252 h = smSelectCopy_ExpMultDiv(tail, e, a, b);2253 else2254 h = ppMult_mm(tail, e);2255 nDelete(&yn);2256 a = pNext(a) = pAdd(pNext(a), h);2257 } while (a!=NULL);2258 }2259 pLmFree(e);2260 }2261 2262 static void smPolyDivN(poly a, const number x)2263 {2264 number y;2265 2266 do2267 {2268 y = nDiv(pGetCoeff(a),x);2269 nNormalize(y);2270 pSetCoeff(a,y);2271 pIter(a);2272 } while (a != NULL);2273 }2274 2275 2202 /*n 2276 2203 * exact division a/b … … 2278 2205 * a destroyed, b NOT destroyed 2279 2206 */ 2280 void sm SpecialPolyDiv(poly a, poly b)2207 void sm_SpecialPolyDiv(poly a, poly b, const ring R) 2281 2208 { 2282 2209 if (pNext(b) == NULL) 2283 2210 { 2284 sm PolyDivN(a, pGetCoeff(b));2211 sm_PolyDivN(a, pGetCoeff(b),R); 2285 2212 return; 2213 } 2214 sm_ExactPolyDiv(a, b, R); 2215 } 2216 2217 2218 /* ------------ internals arithmetic ------------- */ 2219 static void sm_ExactPolyDiv(poly a, poly b, const ring R) 2220 { 2221 const number x = pGetCoeff(b); 2222 poly tail = pNext(b), e = p_Init(R); 2223 poly h; 2224 number y, yn; 2225 int lt = pLength(tail); 2226 2227 if (lt + 1 >= SM_MIN_LENGTH_BUCKET && !TEST_OPT_NOT_BUCKETS) 2228 { 2229 kBucket_pt bucket = kBucketCreate(R); 2230 kBucketInit(bucket, pNext(a), 0); 2231 int lh = 0; 2232 do 2233 { 2234 y = n_Div(pGetCoeff(a), x, R->cf); 2235 n_Normalize(y, R->cf); 2236 p_SetCoeff(a,y, R); 2237 yn = n_Neg(n_Copy(y, R->cf), R->cf); 2238 pSetCoeff0(e,yn); 2239 lh = lt; 2240 if (sm_IsNegQuot(e, a, b, R)) 2241 { 2242 h = pp_Mult_Coeff_mm_DivSelect_MultDiv(tail, lh, e, a, b, R); 2243 } 2244 else 2245 h = pp_Mult_mm(tail, e, R); 2246 n_Delete(&yn, R->cf); 2247 kBucket_Add_q(bucket, h, &lh); 2248 2249 a = pNext(a) = kBucketExtractLm(bucket); 2250 } while (a!=NULL); 2251 kBucketDestroy(&bucket); 2252 } 2253 else 2254 { 2255 do 2256 { 2257 y = n_Div(pGetCoeff(a), x, R->cf); 2258 n_Normalize(y, R->cf); 2259 p_SetCoeff(a,y, R); 2260 yn = n_Neg(n_Copy(y, R->cf), R->cf); 2261 pSetCoeff0(e,yn); 2262 if (sm_IsNegQuot(e, a, b, R)) 2263 h = sm_SelectCopy_ExpMultDiv(tail, e, a, b, R); 2264 else 2265 h = pp_Mult_mm(tail, e, R); 2266 n_Delete(&yn, R->cf); 2267 a = pNext(a) = p_Add_q(pNext(a), h, R); 2268 } while (a!=NULL); 2269 } 2270 p_LmFree(e, R); 2271 } 2272 2273 // obachman --> Wilfried: check the following 2274 static BOOLEAN sm_IsNegQuot(poly a, const poly b, const poly c, const ring R) 2275 { 2276 if (p_LmDivisibleByNoComp(c, b,R)) 2277 { 2278 p_ExpVectorDiff(a, b, c,R); 2279 // Hmm: here used to be a p_Setm(a): but it is unnecessary, 2280 // if b and c are correct 2281 return FALSE; 2282 } 2283 else 2284 { 2285 int i; 2286 for (i=rVar(R); i>0; i--) 2287 { 2288 if(p_GetExp(c,i,R) > p_GetExp(b,i,R)) 2289 p_SetExp(a,i,p_GetExp(c,i,R)-p_GetExp(b,i,R),R); 2290 else 2291 p_SetExp(a,i,0,R); 2292 } 2293 // here we actually might need a p_Setm, if a is to be used in 2294 // comparisons 2295 return TRUE; 2296 } 2297 } 2298 2299 static void sm_ExpMultDiv(poly t, const poly b, const poly c, const ring R) 2300 { 2301 int i; 2302 p_Test(t,R); 2303 p_LmTest(b,R); 2304 p_LmTest(c,R); 2305 poly bc = p_New(R); 2306 2307 p_ExpVectorDiff(bc, b, c, R); 2308 2309 while(t!=NULL) 2310 { 2311 p_ExpVectorAdd(t, bc, R); 2312 pIter(t); 2313 } 2314 p_LmFree(bc, R); 2315 } 2316 2317 2318 static void sm_PolyDivN(poly a, const number x, const ring R) 2319 { 2320 number y; 2321 2322 do 2323 { 2324 y = n_Div(pGetCoeff(a),x, R->cf); 2325 n_Normalize(y, R->cf); 2326 p_SetCoeff(a,y, R); 2327 pIter(a); 2328 } while (a != NULL); 2329 } 2330 2331 static BOOLEAN smSmaller(poly a, poly b) 2332 { 2333 loop 2334 { 2335 pIter(b); 2336 if (b == NULL) return TRUE; 2337 pIter(a); 2338 if (a == NULL) return FALSE; 2339 } 2340 } 2341 2342 static void sm_CombineChain(poly *px, poly r, const ring R) 2343 { 2344 poly pa = *px, pb; 2345 number x; 2346 int i; 2347 2348 loop 2349 { 2350 pb = pNext(pa); 2351 if (pb == NULL) 2352 { 2353 pa = pNext(pa) = r; 2354 break; 2355 } 2356 i = p_LmCmp(pb, r,R); 2357 if (i > 0) 2358 pa = pb; 2359 else 2360 { 2361 if (i == 0) 2362 { 2363 x = n_Add(pGetCoeff(pb), pGetCoeff(r),R->cf); 2364 p_LmDelete(&r,R); 2365 if (n_IsZero(x,R->cf)) 2366 { 2367 p_LmDelete(&pb,R); 2368 pNext(pa) = p_Add_q(pb,r,R); 2369 } 2370 else 2371 { 2372 pa = pb; 2373 p_SetCoeff(pa,x,R); 2374 pNext(pa) = p_Add_q(pNext(pa), r, R); 2375 } 2376 } 2377 else 2378 { 2379 pa = pNext(pa) = r; 2380 pNext(pa) = p_Add_q(pb, pNext(pa),R); 2381 } 2382 break; 2383 } 2384 } 2385 *px = pa; 2386 } 2387 2388 2389 static void sm_FindRef(poly *ref, poly *px, poly r, const ring R) 2390 { 2391 number x; 2392 int i; 2393 poly pa = *px, pp = NULL; 2394 2395 loop 2396 { 2397 i = p_LmCmp(pa, r,R); 2398 if (i > 0) 2399 { 2400 pp = pa; 2401 pIter(pa); 2402 if (pa==NULL) 2403 { 2404 pNext(pp) = r; 2405 break; 2406 } 2407 } 2408 else 2409 { 2410 if (i == 0) 2411 { 2412 x = n_Add(pGetCoeff(pa), pGetCoeff(r),R->cf); 2413 p_LmDelete(&r,R); 2414 if (n_IsZero(x,R->cf)) 2415 { 2416 p_LmDelete(&pa,R); 2417 if (pp!=NULL) 2418 pNext(pp) = p_Add_q(pa,r,R); 2419 else 2420 *px = p_Add_q(pa,r,R); 2421 } 2422 else 2423 { 2424 pp = pa; 2425 p_SetCoeff(pp,x,R); 2426 pNext(pp) = p_Add_q(pNext(pp), r, R); 2427 } 2428 } 2429 else 2430 { 2431 if (pp!=NULL) 2432 pp = pNext(pp) = r; 2433 else 2434 *px = pp = r; 2435 pNext(pp) = p_Add_q(pa, pNext(r),R); 2436 } 2437 break; 2438 } 2439 } 2440 *ref = pp; 2441 } 2442 2443 /* ----------------- internal 'C' stuff ------------------ */ 2444 2445 static void sm_ElemDelete(smpoly *r, const ring R) 2446 { 2447 smpoly a = *r, b = a->n; 2448 2449 p_Delete(&a->m, R); 2450 omFreeBin((void *)a, smprec_bin); 2451 *r = b; 2452 } 2453 2454 static smpoly smElemCopy(smpoly a) 2455 { 2456 smpoly r = (smpoly)omAllocBin(smprec_bin); 2457 memcpy(r, a, sizeof(smprec)); 2458 /* r->m = pCopy(r->m); */ 2459 return r; 2460 } 2461 2462 /* 2463 * from poly to smpoly 2464 * do not destroy p 2465 */ 2466 static smpoly sm_Poly2Smpoly(poly q, const ring R) 2467 { 2468 poly pp; 2469 smpoly res, a; 2470 long x; 2471 2472 if (q == NULL) 2473 return NULL; 2474 a = res = (smpoly)omAllocBin(smprec_bin); 2475 a->pos = x = p_GetComp(q,R); 2476 a->m = q; 2477 a->e = 0; 2478 loop 2479 { 2480 p_SetComp(q,0,R); 2481 pp = q; 2482 pIter(q); 2483 if (q == NULL) 2484 { 2485 a->n = NULL; 2486 return res; 2487 } 2488 if (p_GetComp(q,R) != x) 2489 { 2490 a = a->n = (smpoly)omAllocBin(smprec_bin); 2491 pNext(pp) = NULL; 2492 a->pos = x = p_GetComp(q,R); 2493 a->m = q; 2494 a->e = 0; 2495 } 2496 } 2497 } 2498 2499 /* 2500 * from smpoly to poly 2501 * destroy a 2502 */ 2503 static poly sm_Smpoly2Poly(smpoly a, const ring R) 2504 { 2505 smpoly b; 2506 poly res, pp, q; 2507 long x; 2508 2509 if (a == NULL) 2510 return NULL; 2511 x = a->pos; 2512 q = res = a->m; 2513 loop 2514 { 2515 p_SetComp(q,x,R); 2516 pp = q; 2517 pIter(q); 2518 if (q == NULL) 2519 break; 2520 } 2521 loop 2522 { 2523 b = a; 2524 a = a->n; 2525 omFreeBin((void *)b, smprec_bin); 2526 if (a == NULL) 2527 return res; 2528 x = a->pos; 2529 q = pNext(pp) = a->m; 2530 loop 2531 { 2532 p_SetComp(q,x,R); 2533 pp = q; 2534 pIter(q); 2535 if (q == NULL) 2536 break; 2537 } 2538 } 2539 } 2540 2541 /* 2542 * weigth of a polynomial, for pivot strategy 2543 */ 2544 static float sm_PolyWeight(smpoly a, const ring R) 2545 { 2546 poly p = a->m; 2547 int i; 2548 float res = (float)n_Size(pGetCoeff(p),R->cf); 2549 2550 if (pNext(p) == NULL) 2551 { 2552 for(i=rVar(R); i>0; i--) 2553 { 2554 if (p_GetExp(p,i,R) != 0) return res+1.0; 2555 } 2556 return res; 2557 } 2558 else 2559 { 2560 i = 0; 2561 res = 0.0; 2562 do 2563 { 2564 i++; 2565 res += (float)n_Size(pGetCoeff(p),R->cf); 2566 pIter(p); 2567 } 2568 while (p); 2569 return res+(float)i; 2570 } 2571 } 2572 2573 static BOOLEAN sm_HaveDenom(poly a, const ring R) 2574 { 2575 BOOLEAN sw; 2576 number x; 2577 2578 while (a != NULL) 2579 { 2580 x = n_GetDenom(pGetCoeff(a),R->cf); 2581 sw = n_IsOne(x,R->cf); 2582 n_Delete(&x,R->cf); 2583 if (!sw) 2584 { 2585 return TRUE; 2586 } 2587 pIter(a); 2588 } 2589 return FALSE; 2590 } 2591 2592 static number sm_Cleardenom(ideal id, const ring R) 2593 { 2594 poly a; 2595 number x,y,res=n_Init(1,R->cf); 2596 BOOLEAN sw=FALSE; 2597 2598 for (int i=0; i<IDELEMS(id); i++) 2599 { 2600 a = id->m[i]; 2601 sw = sm_HaveDenom(a,R); 2602 if (sw) break; 2603 } 2604 if (!sw) return res; 2605 for (int i=0; i<IDELEMS(id); i++) 2606 { 2607 a = id->m[i]; 2608 if (a!=NULL) 2609 { 2610 x = n_Copy(pGetCoeff(a),R->cf); 2611 p_Cleardenom(a, R); 2612 y = n_Div(x,pGetCoeff(a),R->cf); 2613 n_Delete(&x,R->cf); 2614 x = n_Mult(res,y,R->cf); 2615 n_Normalize(x,R->cf); 2616 n_Delete(&res,R->cf); 2617 res = x; 2618 } 2286 2619 } 2287 2620 smExactPolyDiv(a, b); … … 2301 2634 2302 2635 /* declare internal 'C' stuff */ 2303 static void sm NumberDelete(smnumber *);2636 static void sm_NumberDelete(smnumber *, const ring R); 2304 2637 static smnumber smNumberCopy(smnumber); 2305 static smnumber sm Poly2Smnumber(poly);2306 static poly sm Smnumber2Poly(number);2638 static smnumber sm_Poly2Smnumber(poly, const ring); 2639 static poly sm_Smnumber2Poly(number,const ring); 2307 2640 static BOOLEAN smCheckSolv(ideal); 2308 2641 … … 2326 2659 smnumber piv; // pivot 2327 2660 smnumber dumm; // allocated dummy 2661 ring _R; 2328 2662 void smColToRow(); 2329 2663 void smRowToCol(); … … 2334 2668 void smAllDel(); 2335 2669 public: 2336 sparse_number_mat(ideal );2670 sparse_number_mat(ideal, const ring); 2337 2671 ~sparse_number_mat(); 2338 2672 int smIsSing() { return sing; } … … 2349 2683 * uses Gauss-elimination 2350 2684 */ 2351 ideal sm CallSolv(ideal I)2685 ideal sm_CallSolv(ideal I, const ring R) 2352 2686 { 2353 2687 sparse_number_mat *linsolv; … … 2356 2690 ideal rr; 2357 2691 2358 if (id IsConstant(I)==FALSE)2692 if (id_IsConstant(I,R)==FALSE) 2359 2693 { 2360 2694 WerrorS("symbol in equation"); 2361 2695 return NULL; 2362 2696 } 2363 I->rank = id RankFreeModule(I);2697 I->rank = id_RankFreeModule(I,R); 2364 2698 if (smCheckSolv(I)) return NULL; 2365 tmpR=sm RingChange(&origR,1);2366 rr=idrCopyR(I, origR);2367 linsolv = new sparse_number_mat(rr );2699 tmpR=sm_RingChange(R,1); 2700 rr=idrCopyR(I,R, tmpR); 2701 linsolv = new sparse_number_mat(rr,tmpR); 2368 2702 rr=NULL; 2369 2703 linsolv->smTriangular(); … … 2376 2710 WerrorS("singular problem for linsolv"); 2377 2711 delete linsolv; 2378 rChangeCurrRing(origR);2379 2712 if (rr!=NULL) 2380 rr = idrMoveR(rr,tmpR );2381 sm KillModifiedRing(tmpR);2713 rr = idrMoveR(rr,tmpR,R); 2714 sm_KillModifiedRing(tmpR); 2382 2715 return rr; 2383 2716 } … … 2386 2719 * constructor, destroy smat 2387 2720 */ 2388 sparse_number_mat::sparse_number_mat(ideal smat )2721 sparse_number_mat::sparse_number_mat(ideal smat, const ring R) 2389 2722 { 2390 2723 int i; 2391 polyset pmat; 2724 poly* pmat; 2725 _R=R; 2392 2726 2393 2727 crd = sing = 0; … … 2406 2740 for(i=ncols; i; i--) 2407 2741 { 2408 m_act[i] = sm Poly2Smnumber(pmat[i-1]);2742 m_act[i] = sm_Poly2Smnumber(pmat[i-1],_R); 2409 2743 } 2410 2744 omFreeSize((ADDRESS)pmat,smat->ncols*sizeof(poly)); … … 2482 2816 { 2483 2817 x = sol[i]; 2484 sol[i] = n Div(x, m_res[i]->m);2485 n Delete(&x);2818 sol[i] = n_Div(x, m_res[i]->m,_R->cf); 2819 n_Delete(&x,_R->cf); 2486 2820 } 2487 2821 i--; … … 2496 2830 if (sol[j] != NULL) 2497 2831 { 2498 z = n Mult(sol[j], s->m);2832 z = n_Mult(sol[j], s->m,_R->cf); 2499 2833 if (x != NULL) 2500 2834 { 2501 2835 y = x; 2502 x = n Sub(y, z);2503 n Delete(&y);2504 n Delete(&z);2836 x = n_Sub(y, z,_R->cf); 2837 n_Delete(&y,_R->cf); 2838 n_Delete(&z,_R->cf); 2505 2839 } 2506 2840 else 2507 x = n Neg(z);2841 x = n_Neg(z,_R->cf); 2508 2842 } 2509 2843 s = s->n; … … 2513 2847 if (x != NULL) 2514 2848 { 2515 y = n Add(x, sol[i]);2516 n Delete(&x);2517 if (n IsZero(y))2518 { 2519 n Delete(&sol[i]);2849 y = n_Add(x, sol[i],_R->cf); 2850 n_Delete(&x,_R->cf); 2851 if (n_IsZero(y,_R->cf)) 2852 { 2853 n_Delete(&sol[i],_R->cf); 2520 2854 sol[i] = NULL; 2521 2855 } … … 2529 2863 { 2530 2864 x = sol[i]; 2531 sol[i] = n Div(x, d->m);2532 n Delete(&x);2865 sol[i] = n_Div(x, d->m,_R->cf); 2866 n_Delete(&x,_R->cf); 2533 2867 } 2534 2868 i--; … … 2548 2882 { 2549 2883 j = perm[i]-1; 2550 res->m[j] = sm Smnumber2Poly(sol[i]);2884 res->m[j] = sm_Smnumber2Poly(sol[i],_R); 2551 2885 } 2552 2886 omFreeSize((ADDRESS)sol, sizeof(number)*(crd+1)); … … 2565 2899 int i, copt, ropt; 2566 2900 2567 xo=n Init(0);2901 xo=n_Init(0,_R->cf); 2568 2902 for (i=act; i; i--) 2569 2903 { … … 2572 2906 { 2573 2907 x = a->m; 2574 if (n GreaterZero(x))2575 { 2576 if (n Greater(x,xo))2577 { 2578 n Delete(&xo);2579 xo = n Copy(x);2908 if (n_GreaterZero(x,_R->cf)) 2909 { 2910 if (n_Greater(x,xo,_R->cf)) 2911 { 2912 n_Delete(&xo,_R->cf); 2913 xo = n_Copy(x,_R->cf); 2580 2914 copt = i; 2581 2915 ropt = a->pos; … … 2584 2918 else 2585 2919 { 2586 xo = n Neg(xo);2587 if (n Greater(xo,x))2588 { 2589 n Delete(&xo);2590 xo = n Copy(x);2920 xo = n_Neg(xo,_R->cf); 2921 if (n_Greater(xo,x,_R->cf)) 2922 { 2923 n_Delete(&xo,_R->cf); 2924 xo = n_Copy(x,_R->cf); 2591 2925 copt = i; 2592 2926 ropt = a->pos; 2593 2927 } 2594 xo = n Neg(xo);2928 xo = n_Neg(xo,_R->cf); 2595 2929 } 2596 2930 a = a->n; … … 2604 2938 m_act[copt] = a; 2605 2939 } 2606 n Delete(&xo);2940 n_Delete(&xo,_R->cf); 2607 2941 } 2608 2942 … … 2612 2946 void sparse_number_mat::smGElim() 2613 2947 { 2614 number p = n Invers(piv->m); // pivotelement2948 number p = n_Invers(piv->m,_R->cf); // pivotelement 2615 2949 smnumber c = m_act[act]; // pivotcolumn 2616 2950 smnumber r = red; // row to reduce … … 2620 2954 if ((c == NULL) || (r == NULL)) 2621 2955 { 2622 while (r!=NULL) sm NumberDelete(&r);2956 while (r!=NULL) sm_NumberDelete(&r,_R); 2623 2957 return; 2624 2958 } … … 2629 2963 res->n = NULL; 2630 2964 b = c; 2631 w = n Mult(r->m, p);2632 n Delete(&r->m);2965 w = n_Mult(r->m, p,_R->cf); 2966 n_Delete(&r->m,_R->cf); 2633 2967 r->m = w; 2634 2968 loop // combine the chains a and b: a + w*b … … 2639 2973 { 2640 2974 res = res->n = smNumberCopy(b); 2641 res->m = n Mult(b->m, w);2975 res->m = n_Mult(b->m, w,_R->cf); 2642 2976 b = b->n; 2643 2977 } while (b != NULL); … … 2652 2986 { 2653 2987 res = res->n = smNumberCopy(b); 2654 res->m = n Mult(b->m, w);2988 res->m = n_Mult(b->m, w,_R->cf); 2655 2989 b = b->n; 2656 2990 } 2657 2991 else 2658 2992 { 2659 hb = n Mult(b->m, w);2660 ha = n Add(a->m, hb);2661 n Delete(&hb);2662 n Delete(&a->m);2663 if (n IsZero(ha))2664 { 2665 sm NumberDelete(&a);2993 hb = n_Mult(b->m, w,_R->cf); 2994 ha = n_Add(a->m, hb,_R->cf); 2995 n_Delete(&hb,_R->cf); 2996 n_Delete(&a->m,_R->cf); 2997 if (n_IsZero(ha,_R->cf)) 2998 { 2999 sm_NumberDelete(&a,_R); 2666 3000 } 2667 3001 else … … 2680 3014 } 2681 3015 m_act[r->pos] = dumm->n; 2682 sm NumberDelete(&r);3016 sm_NumberDelete(&r,_R); 2683 3017 } while (r != NULL); 2684 n Delete(&p);3018 n_Delete(&p,_R->cf); 2685 3019 } 2686 3020 … … 2731 3065 { 2732 3066 ap->n = a->n; 2733 a->m = n Neg(a->m);3067 a->m = n_Neg(a->m,_R); 2734 3068 b = b->n = a; 2735 3069 b->pos = i; … … 2741 3075 { 2742 3076 m_act[i] = a->n; 2743 a->m = n Neg(a->m);3077 a->m = n_Neg(a->m,_R); 2744 3078 b = b->n = a; 2745 3079 b->pos = i; … … 2838 3172 a = m_act[i]; 2839 3173 while (a != NULL) 2840 sm NumberDelete(&a);3174 sm_NumberDelete(&a,_R); 2841 3175 } 2842 3176 for (i=crd; i; i--) … … 2844 3178 a = m_res[i]; 2845 3179 while (a != NULL) 2846 sm NumberDelete(&a);3180 sm_NumberDelete(&a,_R); 2847 3181 } 2848 3182 if (act) … … 2852 3186 a = m_row[i]; 2853 3187 while (a != NULL) 2854 sm NumberDelete(&a);3188 sm_NumberDelete(&a,_R); 2855 3189 } 2856 3190 } … … 2859 3193 /* ----------------- internal 'C' stuff ------------------ */ 2860 3194 2861 static void sm NumberDelete(smnumber *r)3195 static void sm_NumberDelete(smnumber *r, const ring R) 2862 3196 { 2863 3197 smnumber a = *r, b = a->n; 2864 3198 2865 n Delete(&a->m);3199 n_Delete(&a->m,R->cf); 2866 3200 omFreeBin((ADDRESS)a, smnrec_bin); 2867 3201 *r = b; … … 2879 3213 * do not destroy p 2880 3214 */ 2881 static smnumber sm Poly2Smnumber(poly q)3215 static smnumber sm_Poly2Smnumber(poly q, const ring R) 2882 3216 { 2883 3217 smnumber a, res; … … 2887 3221 return NULL; 2888 3222 a = res = (smnumber)omAllocBin(smnrec_bin); 2889 a->pos = p GetComp(p);3223 a->pos = p_GetComp(p,R); 2890 3224 a->m = pGetCoeff(p); 2891 n New(&pGetCoeff(p));3225 n_New(&pGetCoeff(p),R->cf); 2892 3226 loop 2893 3227 { … … 2895 3229 if (p == NULL) 2896 3230 { 2897 p Delete(&q);3231 p_Delete(&q,R); 2898 3232 a->n = NULL; 2899 3233 return res; 2900 3234 } 2901 3235 a = a->n = (smnumber)omAllocBin(smnrec_bin); 2902 a->pos = p GetComp(p);3236 a->pos = p_GetComp(p,R); 2903 3237 a->m = pGetCoeff(p); 2904 n New(&pGetCoeff(p));3238 n_New(&pGetCoeff(p),R->cf); 2905 3239 } 2906 3240 } … … 2910 3244 * destroy a 2911 3245 */ 2912 static poly sm Smnumber2Poly(number a)3246 static poly sm_Smnumber2Poly(number a, const ring R) 2913 3247 { 2914 3248 poly res; 2915 3249 2916 3250 if (a == NULL) return NULL; 2917 res = p Init();3251 res = p_Init(R); 2918 3252 pSetCoeff0(res, a); 2919 3253 return res;
Note: See TracChangeset
for help on using the changeset viewer.