package org.matheclipse.core.reflection.system;

import org.matheclipse.core.basic.Config;
import org.matheclipse.core.convert.ExprVariables;
import org.matheclipse.core.convert.JASConvert;
import org.matheclipse.core.eval.exception.JASConversionException;
import org.matheclipse.core.eval.exception.Validate;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
import org.matheclipse.core.expression.ASTRange;
import org.matheclipse.core.expression.F;
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;

import edu.jas.arith.BigRational;
import edu.jas.poly.GenPolynomial;

/**
 * 
 * See: Wikipedia
 * :Polynomial long division
 * 
 * @see org.matheclipse.core.reflection.system.PolynomialQuotient
 * @see org.matheclipse.core.reflection.system.PolynomialRemainder
 */
public class PolynomialQuotientRemainder extends AbstractFunctionEvaluator {

	public PolynomialQuotientRemainder() {
	}

	@Override
	public IExpr evaluate(final IAST ast) {
		Validate.checkSize(ast, 3);
		IExpr[] result = quotientRemainder(ast);
		if (result == null) {
			return null;
		}
		IAST list = F.List();
		list.add(result[0]);
		list.add(result[1]);
		return list;
	}

	public static IExpr[] quotientRemainder(final IAST lst) {
		try {
			ExprVariables eVar = new ExprVariables(lst.arg1());
			eVar.addVarList(lst.get(2));
			IExpr expr = F.evalExpandAll(lst.arg1());
			ASTRange r = new ASTRange(eVar.getVarList(), 1);
			JASConvert jas = new JASConvert(r.toList(), BigRational.ZERO);
			GenPolynomial poly1 = jas.expr2JAS(expr);
			expr = F.evalExpandAll(lst.get(2));
			GenPolynomial poly2 = jas.expr2JAS(expr);
			GenPolynomial[] divRem = poly1.quotientRemainder(poly2);
			IExpr[] result = new IExpr[2];
			result[0] = jas.rationalPoly2Expr(divRem[0]);
			result[1] = jas.rationalPoly2Expr(divRem[1]);
			return result;
		} catch (JASConversionException e) {
			if (Config.DEBUG) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
 

Ads help maintain this website.