1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
#include "PolyRep.h"
#include <stdio.h>
#include <sstream>
//Loads a string by parsing it as a sum of monomials
//monomial sum: c_{1}*(x_{1}^e_{1}...x_{varCount}^e_{varCount}) + ...
//nested lists: [[c_{1}, [e_{1}, e_{2}, ..., e_{varCount}]], .. ]
void _loadMonomials(_monomialSum &monomials, const string &line)
{
monomials.termCount = 0;
_MonomialLoadConsumer<RationalNTL>* myLoader = new _MonomialLoadConsumer<
RationalNTL> ();
myLoader->setMonomialSum(monomials);
_parseMonomials(myLoader, line);
}
void _parseMonomials(_MonomialConsumer<RationalNTL>* consumer,
const string &line)
{
int varCount = 0;
for (int i = 0; line[i] != ']'; i++)
{
varCount += (line[i] == ',');
}
if (varCount < 1)
{
cout << "There are " << varCount << " variables, bailing." << endl;
return;
}
consumer->setDimension(varCount);
int termIndex, lastPos, expIndex, flag;
termIndex = lastPos = flag = 0; //0 means we expect coefficient, 1 means we expect exponent vector
int *exponents = new int[varCount];
RationalNTL coefficient;
for (size_t i = 1; i < line.length() - 1; i++) //ignore outermost square brackets
{
if (line[i] == '[')
{
switch (flag)
{
case 0: //coefficient
lastPos = i + 1;
for (; line[i] != ','; i++)
;
coefficient = RationalNTL(line.substr(lastPos, i - lastPos).c_str());
flag = 1;
break;
case 1: //exponent vector
expIndex = 0;
for (i++; line[i] != ']'; i++)
{
if (line[i] != ' ')
{
lastPos = i;
for (; line[i] != ',' && line[i] != ']'; i++)
;
exponents[expIndex++] = atoi(line.substr(lastPos, i
- lastPos).c_str());
}
}
consumer->ConsumeMonomial(coefficient, exponents);
flag = 0;
break;
default: //error
cout << "Flag is " << flag << ", bailing." << endl;
return;
}
}
}
delete[] exponents;
}
//Prints a nested list representation of our sum of monomials
//monomial sum: c_{1}*(x_{1}^e_{1}...x_{varCount}^e_{varCount}) + ...
//nested lists: [[c_{1}, [e_{1}, e_{2}, ..., e_{varCount}]], .. ]
string _printMonomials(const _monomialSum &myPoly)
{
stringstream output(stringstream::in | stringstream::out);
output << "[";
eBlock* expTmp = myPoly.eHead;
cBlock<RationalNTL>* coeffTmp = myPoly.cHead;
int termCount = 0;
do
{
for (int i = 0; i < BLOCK_SIZE && termCount < myPoly.termCount; i++)
{
output << "[" << coeffTmp->data[i] << ",[";
for (int j = (i * myPoly.varCount); j < ((i + 1) * myPoly.varCount); j++)
{
output << expTmp->data[j];
if (j + 1 < ((i + 1) * myPoly.varCount))
{
output << ",";
}
}
output << "]]";
if (termCount + 1 < myPoly.termCount)
{
output << ",";
}
termCount++;
}
coeffTmp = coeffTmp->next;
expTmp = expTmp->next;
} while (coeffTmp != NULL);
output << "]";
return output.str();
}
//Deallocates space and nullifies internal pointers and counters
void _destroyMonomials(_monomialSum &myPoly)
{
eBlock* expTmp = myPoly.eHead;
cBlock<RationalNTL>* coeffTmp = myPoly.cHead;
eBlock* oldExp = NULL;
cBlock<RationalNTL>* oldCoeff = NULL;
do
{
oldExp = expTmp;
oldCoeff = coeffTmp;
expTmp = expTmp->next;
coeffTmp = coeffTmp->next;
free(oldExp);
free(oldCoeff);
} while (coeffTmp != NULL);
myPoly.eHead = NULL;
myPoly.cHead = NULL;
myPoly.termCount = myPoly.varCount = 0;
}
void _loadLinForms(_linFormSum &forms, const string line)
{
forms.termCount = 0;
_FormLoadConsumer<RationalNTL>* myLoader = new _FormLoadConsumer<
RationalNTL> ();
myLoader->setFormSum(forms);
_parseLinForms(myLoader, line);
}
//Loads a string by parsing it as a sum of linear forms
//linear form: (c_{1} / d_{1}!)[(p_{1}*x_{1} + ... p_{varCount}*x_{varCount})^d_{1}] + ...
//nested list: [[c_{1}, [d_{1}, [p_{1}, p_{2}, ..., p_{varCount}]], .. ]
void _parseLinForms(_FormSumConsumer<RationalNTL>* consumer, const string& line)
{
int termIndex = 0;
int lastPos = 0;
int varCount = 0;
int k;
int flag = 0; //0 means we expect coefficient, 1 means we expect degree, 2 means we expect coefficient vector
for (int i = 0; line[i] != ']'; i++)
{
varCount += (line[i] == ',');
}
//varCount is now the number of commas in a linear form - there is 1 less variable;
varCount--;
if (varCount < 1)
{
cout << "There are " << varCount << " variables, bailing." << endl;
return;
}
consumer->setDimension(varCount);
vec_ZZ coefs;
coefs.SetLength(varCount);
int degree;
RationalNTL coefficient;
for (size_t i = 1; i < line.length() - 1; i++) //ignore outermost square brackets
{
if (line[i] == '[')
{
int degreeFactorial;
switch (flag)
{
case 0: //coefficient
lastPos = i + 1;
for (; line[i] != ','; i++)
;
coefficient = RationalNTL(line.substr(lastPos, i - lastPos).c_str());
flag = 1;
break;
case 1: //degree
lastPos = i + 1;
for (; line[i] != ','; i++)
;
degree = atoi(line.substr(lastPos, i - lastPos).c_str());
flag = 2;
break;
case 2: //coefficient vector
k = 0;
for (i++; line[i] != ']'; i++)
{
if (line[i] != ' ')
{
lastPos = i;
for (; line[i] != ',' && line[i] != ']'; i++)
;
coefs[k++] = to_ZZ(
line.substr(lastPos, i - lastPos).c_str());
}
}
degreeFactorial = 1;
for (int j = 1; j <= degree; j++)
{
degreeFactorial *= j;
//coefficient *= j;
} //in _linFormSum, coefficient is assumed to be divided by the factorial of the form degree
coefficient *= to_ZZ(degreeFactorial);
consumer->ConsumeLinForm(coefficient, degree, coefs);
flag = 0;
break;
default: //error
cout << "Flag is " << flag << ", bailing." << endl;
return;
}
}
}
}
//Prints a nested list representation of our sum of linear forms
//linear form: (c_{1} / d_{1}!)[(p_{1}*x_{1} + ... p_{varCount}*x_{varCount})^d_{1}] + ...
//nested list: [[c_{1}, [d_{1}, [p_{1}, p_{2}, ..., p_{varCount}]], .. ]
string _printLinForms(const _linFormSum &myForm)
{
stringstream output(stringstream::in | stringstream::out);
output << "[";
lBlock* formTmp = myForm.lHead;
cBlock<RationalNTL>* coeffTmp = myForm.cHead;
for (int i = 0; i < myForm.termCount; i++)
{
if (i > 0 && i % BLOCK_SIZE == 0)
{
formTmp = formTmp->next;
coeffTmp = coeffTmp->next;
}
output << "[" << coeffTmp->data[i % BLOCK_SIZE] << ", ["
<< formTmp->degree[i % BLOCK_SIZE] << ", [";
for (int j = 0; j < myForm.varCount; j++)
{
output << formTmp->data[i % BLOCK_SIZE][j];
if (j + 1 < myForm.varCount)
{
output << ", ";
}
}
output << "]]]";
if (i + 1 < myForm.termCount)
{
output << ", ";
}
}
output << "]";
return output.str();
}
//Deallocates space and nullifies internal pointers and counters
void _destroyLinForms(_linFormSum &myPoly)
{
lBlock* expTmp = myPoly.lHead;
cBlock<RationalNTL>* coeffTmp = myPoly.cHead;
lBlock* oldExp = NULL;
cBlock<RationalNTL>* oldCoeff = NULL;
int termCount = 0;
do
{
oldExp = expTmp;
oldCoeff = coeffTmp;
expTmp = expTmp->next;
coeffTmp = coeffTmp->next;
free(oldExp);
free(oldCoeff);
} while (coeffTmp != NULL);
myPoly.lHead = NULL;
myPoly.cHead = NULL;
myPoly.termCount = myPoly.varCount = 0;
}
//INPUT: monomial specified by myPoly.coefficientBlocks[mIndex / BLOCK_SIZE].data[mIndex % BLOCK_SIZE]
// and myPoly.exponentBlocks[mIndex / BLOCK_SIZE].data[mIndex % BLOCK_SIZE]
//OUTPUT: lForm now also contains the linear decomposition of this monomial
// note: all linear form coefficients assumed to be divided by their respective |M|!, and the form is assumed to be of power M
void _decompose(_monomialSum &myPoly, _linFormSum &lForm, int mIndex)
{
eBlock* expTmp = myPoly.eHead;
cBlock<RationalNTL>* coeffTmp = myPoly.cHead;
for (int i = 0; i < (mIndex / BLOCK_SIZE); i++)
{
expTmp = expTmp->next;
coeffTmp = coeffTmp->next;
}
bool constantTerm = true;
for (int i = (mIndex * myPoly.varCount); i < ((mIndex + 1)
* myPoly.varCount); i++)
{
if (expTmp->data[i] != 0)
{
constantTerm = false;
break;
}
}
vec_ZZ myExps;
myExps.SetLength(lForm.varCount);
if (constantTerm) //exponents are all 0, this is a constant term - linear form is already known
{
for (int j = 0; j < lForm.varCount; j++)
{
myExps[j] = 0;
}
_insertLinForm<RationalNTL> (coeffTmp->data[mIndex % BLOCK_SIZE], 0, myExps,
lForm);
return;
}
ZZ formsCount = to_ZZ(expTmp->data[(mIndex % BLOCK_SIZE) * myPoly.varCount]
+ 1);
int totalDegree = expTmp->data[(mIndex % BLOCK_SIZE) * myPoly.varCount];
for (int i = 1; i < myPoly.varCount; i++)
{
formsCount *= expTmp->data[(mIndex % BLOCK_SIZE) * myPoly.varCount + i]
+ 1;
totalDegree
+= expTmp->data[(mIndex % BLOCK_SIZE) * myPoly.varCount + i];
}
formsCount--;
//cout << "At most " << formsCount << " linear forms will be required for this decomposition." << endl;
//cout << "Total degree is " << totalDegree << endl;
int* p = new int[myPoly.varCount];
int* counter = new int[myPoly.varCount];
ZZ* binomCoeffs = new ZZ[myPoly.varCount]; //for calculating the product of binomial coefficients for each linear form
RationalNTL temp;
int g;
bool found;
int myIndex;
for (int i = 0; i < myPoly.varCount; i++)
{
counter[i] = 0;
binomCoeffs[i] = to_ZZ(1);
}
for (ZZ i = to_ZZ(1); i <= formsCount; i++)
{
//cout << "i is " << i << endl;
counter[0] += 1;
for (myIndex = 0; counter[myIndex] > expTmp->data[(mIndex % BLOCK_SIZE)
* myPoly.varCount + myIndex]; myIndex++)
{
counter[myIndex] = 0;
binomCoeffs[myIndex] = to_ZZ(1);
counter[myIndex + 1] += 1;
}
binomCoeffs[myIndex] *= expTmp->data[(mIndex % BLOCK_SIZE)
* myPoly.varCount + myIndex] - counter[myIndex] + 1; // [n choose k] = [n choose (k - 1) ] * (n - k + 1)/k
binomCoeffs[myIndex] /= counter[myIndex];
//cout << "counter is: " << counter << endl;
//find gcd of all elements and calculate the product of binomial coefficients for the linear form
g = counter[0];
int parity = totalDegree - counter[0];
p[0] = counter[0];
temp = binomCoeffs[0];
if (myPoly.varCount > 1)
{
for (int k = 1; k < myPoly.varCount; k++)
{
p[k] = counter[k];
g = GCD(g, p[k]);
parity -= p[k];
temp *= binomCoeffs[k];
}
}
//calculate coefficient
temp *= coeffTmp->data[mIndex % BLOCK_SIZE];
if ((parity % 2) == 1)
{
temp *= to_ZZ(-1);
} // -1 ^ [|M| - (p[0] + p[1] + ... p[n])], checks for odd parity using modulo 2
if (g != 1)
{
for (int k = 0; k < myPoly.varCount; k++)
{
p[k] /= g;
}
temp *= power_ZZ(g, totalDegree);
}
//cout << "coefficient is " << temp << endl;
for (int i = 0; i < myPoly.varCount; i++)
{
myExps[i] = p[i];
}
_insertLinForm<RationalNTL> (temp, totalDegree, myExps, lForm);
}
delete[] p;
delete[] counter;
delete[] binomCoeffs;
}
|