}
public ConsCell polynomialDivision(ConsCell dividend, ConsCell divisor, ConsCell var, boolean flagRemainder) throws ParserException {
BigDec[] dividendCoefficients = getSyntheticCoefficients(dividend, var);
if (dividendCoefficients == null)
return new ConsCell("{false}", ConsType.OBJECT);
BigDec[] divisorCoefficients = getSyntheticCoefficients(divisor, var);
if (divisorCoefficients == null)
return new ConsCell("{false}", ConsType.OBJECT);
int resultOrder = dividendCoefficients.length - divisorCoefficients.length;
ConsCell result = new ConsCell();
for (int i = 0; i <= resultOrder; i++) {
BigDec resultCoefficient = dividendCoefficients[i].divide(divisorCoefficients[0]);
dividendCoefficients[i] = BigDec.ZERO;
for (int j = 1; j < divisorCoefficients.length; j++)
dividendCoefficients[i + j] = dividendCoefficients[i + j].subtract(divisorCoefficients[j].multiply(resultCoefficient));
result = result.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(resultCoefficient, ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR, new ConsCell(var, ConsType.CONS_CELL,
new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(resultOrder - i), ConsType.NUMBER)))))));
}
ConsCell remainder = new ConsCell();
for (int i = 0; i < dividendCoefficients.length; i++)
if (dividendCoefficients[i].neq(BigDec.ZERO))
remainder = remainder.append(new ConsCell('+', ConsType.OPERATOR, new ConsCell(dividendCoefficients[i], ConsType.NUMBER, new ConsCell('*', ConsType.OPERATOR,
new ConsCell(var, ConsType.CONS_CELL, new ConsCell('^', ConsType.OPERATOR, new ConsCell(new BigDec(dividendCoefficients.length - 1 - i), ConsType.NUMBER)))))));
remainder = remainder.getFirstConsCell();
if (!remainder.isNull()) {
remainder = remainder.remove();
remainder = new ConsCell(remainder, ConsType.CONS_CELL, new ConsCell('/', ConsType.OPERATOR, new ConsCell(divisor.clone(), ConsType.CONS_CELL), ConsType.CONS_CELL), ConsType.CONS_CELL);
result = result.append(new ConsCell('+', ConsType.OPERATOR, remainder, ConsType.CONS_CELL));
}
result = result.getFirstConsCell();
if (!result.isNull())
result = simplifyTerms(result.remove());
if (flagRemainder)
result.getLastConsCell().append(new ConsCell("{" + !remainder.isNull() + "}", ConsType.OBJECT));
return result;
}