Changeset 232f51c in git
- Timestamp:
- Feb 24, 1999, 2:25:19 PM (25 years ago)
- Branches:
- (u'spielwiese', '8e0ad00ce244dfd0756200662572aef8402f13d5')
- Children:
- 34cb4bea84226d98334d714b43a4556d8f1c5487
- Parents:
- d8e380c4561b7282d49138294559898e2ca63423
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/sparsmat.cc
rd8e380 r232f51c 1 /**************************************** 2 * Computer Algebra System SINGULAR * 3 ****************************************/ 4 /* $Id: sparsmat.cc,v 1.2 1999-02-24 13:25:19 pohl Exp $ */ 5 6 /* 7 * ABSTRACT: operations with sparse matrices (bareiss, ...) 8 */ 9 1 10 #include "mod2.h" 2 11 #include "tok.h" … … 5 14 #include "intvec.h" 6 15 #include "lists.h" 16 #include "ring.h" 7 17 #include "polys.h" 8 18 #include "ipid.h" 9 19 #include "ideals.h" 10 20 #include "numbers.h" 11 #include "ring.h"12 21 #include "sparsmat.h" 13 22 14 23 /* ----------------- macros ------------------ */ 15 #define OLD_DIV 24 /* #define OLD_DIV */ 16 25 #ifdef OLD_DIV 17 26 #define SM_MULT(A,B,C) smMult(A,B) … … 35 44 36 45 /* declare internal 'C' stuff */ 46 static void smDebug() { int k=0; } 37 47 static void smExactPolyDiv(poly, poly); 38 48 static BOOLEAN smIsScalar(const poly); 39 49 static BOOLEAN smIsNegQuot(poly, const poly, const poly); 40 50 static poly smEMult(poly, const poly); 51 static BOOLEAN smCheckLead(const poly, const poly); 41 52 static poly smDMult(poly, const poly); 42 53 static void smPolyDivN(poly a, const number); 43 54 static BOOLEAN smSmaller(poly, poly); 44 55 static void smCombineChain(poly *, poly); 56 static void smFindRef(poly *, poly *, poly); 45 57 46 58 static void smElemDelete(smpoly *); … … 89 101 void smRowToCol(); 90 102 void smFinalMult(); 91 void smFinalToCol();92 void smFinalClear();93 103 void smSparseHomog(); 94 104 void smWeights(); … … 109 119 ~sparse_mat(); 110 120 smpoly * smGetAct() { return m_act; } 121 int smGetRed() { return tored; } 111 122 ideal smRes2Mod(); 112 123 void smBareiss(int, int); … … 120 131 { 121 132 lists res=(lists)Alloc(sizeof(slists)); 122 sparse_mat *bareiss = new sparse_mat(I); 133 ideal II = idCopy(I); 134 sparse_mat *bareiss = new sparse_mat(II); 123 135 intvec *v; 124 136 ideal m; 125 137 138 idDelete(&II); 126 139 if (bareiss->smGetAct() == NULL) 127 140 { … … 131 144 bareiss->smBareiss(x, y); 132 145 m = bareiss->smRes2Mod(); 133 v = new intvec( m->rank);146 v = new intvec(bareiss->smGetRed()); 134 147 bareiss->smToIntvec(v); 135 148 delete bareiss; … … 177 190 ideal m; 178 191 192 idDelete(&II); 179 193 if (bareiss->smGetAct() == NULL) 180 194 { … … 192 206 bareiss->smNewBareiss(x, y); 193 207 m = bareiss->smRes2Mod(); 194 v = new intvec( m->rank);208 v = new intvec(bareiss->smGetRed()); 195 209 bareiss->smToIntvec(v); 196 210 delete bareiss; … … 228 242 { 229 243 int i; 244 polyset pmat; 230 245 231 246 ncols = smat->ncols; … … 243 258 i = tored+1; 244 259 perm = (unsigned short *)Alloc(sizeof(unsigned short)*i); 260 m_row = (smpoly *)Alloc0(sizeof(smpoly)*i); 245 261 wrw = (float *)Alloc(sizeof(float)*i); 246 262 i = ncols+1; 247 263 wcl = (float *)Alloc(sizeof(float)*i); 248 264 m_act = (smpoly *)Alloc(sizeof(smpoly)*i); 249 m_row = (smpoly *)Alloc0(sizeof(smpoly)*i);250 265 m_res = (smpoly *)Alloc0(sizeof(smpoly)*i); 251 266 dumm = (smpoly)Alloc(sizeof(smprec)); 252 267 m_res[0] = (smpoly)Alloc(sizeof(smprec)); 253 268 m_res[0]->m = NULL; 254 for(i=ncols; i; i--) m_act[i] = smPoly2Smpoly(smat->m[i-1]); 269 pmat = smat->m; 270 for(i=ncols; i; i--) 271 { 272 m_act[i] = smPoly2Smpoly(pmat[i-1]); 273 pmat[i-1] = NULL; 274 } 255 275 this->smZeroElim(); 256 276 oldpiv = NULL; … … 268 288 i = ncols+1; 269 289 Free((ADDRESS)m_res, sizeof(smpoly)*i); 270 Free((ADDRESS)m_row, sizeof(smpoly)*i);271 290 Free((ADDRESS)m_act, sizeof(smpoly)*i); 272 291 Free((ADDRESS)wcl, sizeof(float)*i); 273 292 i = nrows+1; 274 293 Free((ADDRESS)wrw, sizeof(float)*i); 294 Free((ADDRESS)m_row, sizeof(smpoly)*i); 275 295 Free((ADDRESS)perm, sizeof(unsigned short)*i); 276 296 } … … 356 376 this->smCopToRes(); 357 377 } 378 else 379 tored = crd; 358 380 return; 359 381 } … … 419 441 this->smCopToRes(); 420 442 } 443 else 444 tored = crd; 421 445 return; 422 446 } … … 1061 1085 } 1062 1086 1063 /* 1064 * store red, the pivot and the assosiated row in m_row 1065 * to m_res (result): 1066 * piv + m_row[rows][pos(cols)] => m_res[cols][pos(rows)] 1067 */ 1068 void sparse_mat::smFinalToCol() 1069 { 1070 smpoly r, a, ap, h; 1071 1072 perm[crd] = rpiv; 1073 piv->pos = crd; 1074 ap = m_res[crd]; 1075 if (ap == NULL) 1076 m_res[crd] = piv; 1077 else 1078 { 1079 loop 1080 { 1081 a = ap->n; 1082 if (a == NULL) 1083 { 1084 ap->n = piv; 1085 break; 1086 } 1087 ap = a; 1088 } 1089 } 1090 r = m_row[rpiv]; 1091 m_row[rpiv] = NULL; 1092 while (r != NULL) 1093 { 1094 ap = m_res[r->pos]; 1095 loop 1096 { 1097 a = ap->n; 1098 if (a == NULL) 1099 { 1100 ap->n = h = r; 1101 r = r->n; 1102 h->n = a; 1103 h->pos = crd; 1104 break; 1105 } 1106 ap = a; 1107 } 1108 } 1109 r = red; 1110 while (r != NULL) 1111 { 1112 ap = m_res[r->pos]; 1113 if (ap == NULL) 1114 { 1115 m_res[r->pos] = h = r; 1116 r = r->n; 1117 h->n = NULL; 1118 h->pos = crd; 1087 /* ----------------- C++ stuff ------------------ */ 1088 1089 /* 1090 * clean m_act from zeros (after elim) 1091 */ 1092 void sparse_mat::smZeroElim() 1093 { 1094 int i = 0; 1095 int j; 1096 1097 loop 1098 { 1099 i++; 1100 if (i > act) return; 1101 if (m_act[i] == NULL) break; 1102 } 1103 j = i; 1104 loop 1105 { 1106 j++; 1107 if (j > act) break; 1108 if (m_act[j] != NULL) 1109 { 1110 m_act[i] = m_act[j]; 1111 i++; 1112 } 1113 } 1114 act -= (j-i); 1115 sign = 0; 1116 } 1117 1118 /* 1119 * clean m_act from cols not to reduced (after elim) 1120 * put them to m_res 1121 */ 1122 void sparse_mat::smToredElim() 1123 { 1124 int i = 0; 1125 int j; 1126 1127 loop 1128 { 1129 i++; 1130 if (i > act) return; 1131 if (m_act[i]->pos > tored) 1132 { 1133 m_res[crd] = m_act[i]; 1134 crd++; 1135 break; 1136 } 1137 } 1138 j = i; 1139 loop 1140 { 1141 j++; 1142 if (j > act) break; 1143 if (m_act[i]->pos > tored) 1144 { 1145 m_res[crd] = m_act[i]; 1146 crd++; 1119 1147 } 1120 1148 else 1121 1149 { 1150 m_act[i] = m_act[j]; 1151 i++; 1152 } 1153 } 1154 act -= (j-i); 1155 sign = 0; 1156 } 1157 1158 /* 1159 * copy m_act to m_res 1160 */ 1161 void sparse_mat::smCopToRes() 1162 { 1163 smpoly p, r, ap, a, h; 1164 int c, i; 1165 1166 if (act == 0) return; 1167 if (act > 1) 1168 { 1169 Werror("Not implemented"); 1170 } 1171 crd++; 1172 c = crd; 1173 p = m_res[c] = m_act[act]; 1174 do 1175 { 1176 r = m_row[p->pos]; 1177 m_row[p->pos] = NULL; 1178 perm[c] = p->pos; 1179 p->pos = c; 1180 while (r != NULL) 1181 { 1182 ap = m_res[r->pos]; 1122 1183 loop 1123 1184 { … … 1128 1189 r = r->n; 1129 1190 h->n = a; 1130 h->pos = c rd;1191 h->pos = c; 1131 1192 break; 1132 1193 } … … 1134 1195 } 1135 1196 } 1136 } 1137 } 1138 1139 /* 1140 * final clear of m_row 1141 */ 1142 void sparse_mat::smFinalClear() 1143 { 1144 int i; 1145 smpoly r, a, ap, h; 1146 1147 for (i=nrows; i; i--) 1148 { 1149 if (m_row[i] != NULL) 1150 { 1151 crd++; 1152 perm[crd] = i; 1197 c++; 1198 p = p->n; 1199 } while (p != NULL); 1200 for (i=1;i<=nrows;i++) 1201 { 1202 if(m_row[i] != NULL) 1203 { 1153 1204 r = m_row[i]; 1154 while (r != NULL) 1205 m_row[i] = NULL; 1206 do 1155 1207 { 1156 1208 ap = m_res[r->pos]; 1157 if (ap == NULL) 1158 { 1159 m_res[r->pos] = h = r; 1160 r = r->n; 1161 h->n = NULL; 1162 h->pos = crd; 1209 loop 1210 { 1211 a = ap->n; 1212 if (a == NULL) 1213 { 1214 ap->n = h = r; 1215 r = r->n; 1216 h->n = a; 1217 h->pos = c; 1218 break; 1219 } 1220 ap = a; 1163 1221 } 1164 else 1165 { 1166 loop 1167 { 1168 a = ap->n; 1169 if (a == NULL) 1170 { 1171 ap->n = h = r; 1172 r = r->n; 1173 h->n = a; 1174 h->pos = crd; 1175 break; 1176 } 1177 ap = a; 1178 } 1179 } 1180 } 1181 } 1182 } 1183 } 1184 1185 /* ----------------- C++ stuff ------------------ */ 1186 1187 /* 1188 * clean m_act from zeros (after elim) 1189 */ 1190 void sparse_mat::smZeroElim() 1191 { 1192 int i = 0; 1193 int j; 1194 1195 loop 1196 { 1197 i++; 1198 if (i > act) return; 1199 if (m_act[i] == NULL) break; 1200 } 1201 j = i; 1202 loop 1203 { 1204 j++; 1205 if (j > act) break; 1206 if (m_act[j] != NULL) 1207 { 1208 m_act[i] = m_act[j]; 1209 i++; 1210 } 1211 } 1212 act -= (j-i); 1213 sign = 0; 1214 } 1215 1216 /* 1217 * clean m_act from cols not to reduced (after elim) 1218 * put them to m_res 1219 */ 1220 void sparse_mat::smToredElim() 1221 { 1222 int i = 0; 1223 int j; 1224 1225 loop 1226 { 1227 i++; 1228 if (i > act) return; 1229 if (m_act[i]->pos > tored) 1230 { 1231 m_res[crd] = m_act[i]; 1232 crd++; 1233 break; 1234 } 1235 } 1236 j = i; 1237 loop 1238 { 1239 j++; 1240 if (j > act) break; 1241 if (m_act[i]->pos > tored) 1242 { 1243 m_res[crd] = m_act[i]; 1244 crd++; 1245 } 1246 else 1247 { 1248 m_act[i] = m_act[j]; 1249 i++; 1250 } 1251 } 1252 act -= (j-i); 1253 sign = 0; 1254 } 1255 1256 /* 1257 * copy m_act to m_res 1258 */ 1259 void sparse_mat::smCopToRes() 1260 { 1261 while (act != 0) 1262 { 1263 rpiv = m_act[act]->pos; 1264 this->smSelectPR(); 1265 crd++; 1266 this->smColToRow(); 1267 act--; 1268 this->smFinalToCol(); 1269 if (act != 0) this->smZeroElim(); 1270 } 1271 // this->smFinalClear(); 1222 } while (r!=NULL); 1223 c++; 1224 } 1225 } 1226 tored = c-1; 1272 1227 } 1273 1228 … … 1400 1355 pSetm(a); 1401 1356 } 1402 y = nIntDiv(pGetCoeff(a),x); 1357 y = nDiv(pGetCoeff(a),x); 1358 nNormalize(y); 1403 1359 pSetCoeff(a,y); 1404 1360 pIter(a); … … 1412 1368 pSubExp(a,i,pGetExp(b,i)); 1413 1369 pSetm(a); 1414 y = nIntDiv(pGetCoeff(a),x); 1370 y = nDiv(pGetCoeff(a),x); 1371 nNormalize(y); 1415 1372 pSetCoeff(a,y); 1416 1373 yn = nNeg(nCopy(y)); … … 1440 1397 { 1441 1398 poly pa, e, res, r; 1399 BOOLEAN lead; 1442 1400 1443 1401 if (smSmaller(a, b)) … … 1466 1424 return res; 1467 1425 } 1426 res = NULL; 1468 1427 e = pNew(); 1469 loop 1428 lead = FALSE; 1429 while (!lead) 1470 1430 { 1471 1431 pSetCoeff0(e,pGetCoeff(b)); 1472 1432 if (smIsNegQuot(e, b, c)) 1473 res = smDMult(a, e); 1433 { 1434 lead = smCheckLead(a, e); 1435 r = smDMult(a, e); 1436 } 1474 1437 else 1475 res = smEMult(a, e); 1438 { 1439 lead = TRUE; 1440 r = smEMult(a, e); 1441 } 1442 if (lead) 1443 { 1444 if (res != NULL) 1445 { 1446 smFindRef(&pa, &res, r); 1447 if (pa == NULL) 1448 lead = FALSE; 1449 } 1450 else 1451 { 1452 pa = res = r; 1453 } 1454 } 1455 else 1456 res = pAdd(res, r); 1476 1457 pIter(b); 1477 1458 if (b == NULL) … … 1480 1461 return res; 1481 1462 } 1482 if (res != NULL) break;1483 1463 } 1484 1464 do … … 1488 1468 { 1489 1469 r = smDMult(a, e); 1490 if (r != NULL) 1491 res = pAdd(res,r); 1470 if (smCheckLead(a, e)) 1471 smCombineChain(&pa, r); 1472 else 1473 pa = pAdd(pa,r); 1492 1474 } 1493 1475 else 1494 1476 { 1495 1477 r = smEMult(a, e); 1496 res = pAdd(res,r);1478 smCombineChain(&pa, r); 1497 1479 } 1498 1480 pIter(b); … … 1527 1509 do 1528 1510 { 1529 y = nIntDiv(pGetCoeff(a), x); 1511 y = nDiv(pGetCoeff(a), x); 1512 nNormalize(y); 1530 1513 pSetCoeff(a,y); 1531 1514 yn = nNeg(nCopy(y)); … … 1592 1575 } 1593 1576 1577 static BOOLEAN smCheckLead(const poly t, const poly e) 1578 { 1579 int i; 1580 for (i=pVariables; i; i--) 1581 { 1582 if ((pGetExp(e,i)+pGetExp(t,i)) < 0) 1583 return FALSE; 1584 } 1585 return TRUE; 1586 } 1587 1594 1588 static poly smDMult(poly t, const poly e) 1595 1589 { … … 1642 1636 do 1643 1637 { 1644 y = nIntDiv(pGetCoeff(a),x); 1638 y = nDiv(pGetCoeff(a),x); 1639 nNormalize(y); 1645 1640 pSetCoeff(a,y); 1646 1641 pIter(a); … … 1705 1700 } 1706 1701 1702 static void smFindRef(poly *ref, poly *px, poly r) 1703 { 1704 number x; 1705 int i; 1706 poly pa = *px, pp = NULL; 1707 1708 loop 1709 { 1710 i = pComp0(pa, r); 1711 if (i > 0) 1712 { 1713 pp = pa; 1714 pIter(pa); 1715 if (pa==NULL) 1716 { 1717 pNext(pp) = r; 1718 break; 1719 } 1720 } 1721 else 1722 { 1723 if (i == 0) 1724 { 1725 x = nAdd(pGetCoeff(pa), pGetCoeff(r)); 1726 pDelete1(&r); 1727 if (nIsZero(x)) 1728 { 1729 pDelete1(&pa); 1730 if (pp!=NULL) 1731 pNext(pp) = pAdd(pa,r); 1732 else 1733 *px = pAdd(pa,r); 1734 } 1735 else 1736 { 1737 pp = pa; 1738 pSetCoeff(pp,x); 1739 pNext(pp) = pAdd(pNext(pp), r); 1740 } 1741 } 1742 else 1743 { 1744 if (pp!=NULL) 1745 pp = pNext(pp) = r; 1746 else 1747 *px = pp = r; 1748 pNext(pp) = pAdd(pa, pNext(r)); 1749 } 1750 break; 1751 } 1752 } 1753 *ref = pp; 1754 } 1755 1707 1756 /* ----------------- internal 'C' stuff ------------------ */ 1708 1757 … … 1729 1778 * do not destroy p 1730 1779 */ 1731 static smpoly smPoly2Smpoly(poly p)1732 { 1733 poly pp , q;1780 static smpoly smPoly2Smpoly(poly q) 1781 { 1782 poly pp; 1734 1783 smpoly res, a; 1735 1784 EXPONENT_TYPE x; 1736 1785 1737 if ( p== NULL)1786 if (q == NULL) 1738 1787 return NULL; 1739 q = pCopy(p);1740 1788 a = res = (smpoly)Alloc(sizeof(smprec)); 1741 1789 a->pos = x = pGetComp(q);
Note: See TracChangeset
for help on using the changeset viewer.