FactorTerms.java :  » Algebra » symja » org » matheclipse » core » reflection » system » Java Open Source

Java Open Source » Algebra » symja 
symja » org » matheclipse » core » reflection » system » FactorTerms.java
package org.matheclipse.core.reflection.system;

import org.matheclipse.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.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 org.matheclipse.core.interfaces.ISymbol;

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

public class FactorTerms extends AbstractFunctionEvaluator {

  public FactorTerms() {
  }

  @Override
  public IExpr evaluate(final IAST lst) {
    if (lst.size() != 2 && lst.size() != 3) {
      return null;
    }

    ExprVariables eVar = new ExprVariables(lst.get(1));
    if (!eVar.isSize(1)) {
      // partial fraction only possible for univariate polynomials
      return null;
    }
    IAST variableList = eVar.getVarList();
    if (lst.size() == 3) {
      if (lst.get(2) instanceof ISymbol) {
        ISymbol variable = (ISymbol) lst.get(2);
        variableList = F.List(variable);
      } else if (lst.get(2).isList()) {
        variableList = (IAST) lst.get(2);
      } else {
        return null;
      }
    }
    if (variableList.size() != 2) {
      // FactorTerms only possible for univariate polynomials
      return null;
    }
    // IExpr variable = variableList.get(1);
    try {
      IExpr expr = F.evalExpandAll(lst.get(1));
      ASTRange r = new ASTRange(variableList, 1);
      JASConvert<BigRational> jas = new JASConvert<BigRational>(r.toList(), BigRational.ZERO);
      GenPolynomial<BigRational> poly = jas.expr2JAS(expr);
      Object[] objects = jas.factorTerms(poly);
      java.math.BigInteger gcd = (java.math.BigInteger) objects[0];
      java.math.BigInteger lcm = (java.math.BigInteger) objects[1];
      if (lcm.equals(java.math.BigInteger.ZERO)) {
        // no changes
        return expr;
      }
      GenPolynomial<edu.jas.arith.BigInteger> iPoly = (GenPolynomial<edu.jas.arith.BigInteger>) objects[2];
      IAST result = F.Times();
      result.add(F.fraction(gcd, lcm));
      result.add(jas.integerPoly2Expr(iPoly));
      return result;
    } catch (JASConversionException e) {
      if (Config.DEBUG) {
        e.printStackTrace();
      }
    }
    return null;
  }

  @Override
  public void setUp(final ISymbol symbol) {
    symbol.setAttributes(ISymbol.FLAT);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.