use of org.matheclipse.core.expression.ASTRange in project symja_android_library by axkr.
the class Algebra method partialFractionDecompositionRational.
/**
* Returns an AST with head <code>Plus</code>, which contains the partial fraction decomposition of the numerator
* and denominator parts.
*
* @param pf
* partial fraction generator
* @param parts
* @param variable
* a variable
* @return <code>F.NIL</code> if the partial fraction decomposition wasn't constructed
*/
public static IExpr partialFractionDecompositionRational(IPartialFractionGenerator pf, IExpr[] parts, ISymbol variable) {
try {
IAST variableList = F.List(variable);
IExpr exprNumerator = F.evalExpandAll(parts[0]);
IExpr exprDenominator = F.evalExpandAll(parts[1]);
ASTRange r = new ASTRange(variableList, 1);
List<IExpr> varList = r;
String[] varListStr = new String[1];
varListStr[0] = variableList.arg1().toString();
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
GenPolynomial<BigRational> numerator = jas.expr2JAS(exprNumerator, false);
GenPolynomial<BigRational> denominator = jas.expr2JAS(exprDenominator, false);
// get factors
FactorAbstract<BigRational> factorAbstract = FactorFactory.getImplementation(BigRational.ZERO);
SortedMap<GenPolynomial<BigRational>, Long> sfactors = factorAbstract.baseFactors(denominator);
List<GenPolynomial<BigRational>> D = new ArrayList<GenPolynomial<BigRational>>(sfactors.keySet());
SquarefreeAbstract<BigRational> sqf = SquarefreeFactory.getImplementation(BigRational.ZERO);
List<List<GenPolynomial<BigRational>>> Ai = sqf.basePartialFraction(numerator, sfactors);
if (Ai.size() > 0) {
// IAST result = F.Plus();
pf.allocPlus(Ai.size() * 2);
pf.setJAS(jas);
if (!Ai.get(0).get(0).isZERO()) {
pf.addNonFractionalPart(Ai.get(0).get(0));
}
for (int i = 1; i < Ai.size(); i++) {
List<GenPolynomial<BigRational>> list = Ai.get(i);
int j = 0;
for (GenPolynomial<BigRational> genPolynomial : list) {
if (!genPolynomial.isZERO()) {
GenPolynomial<BigRational> Di_1 = D.get(i - 1);
pf.addSinglePartialFraction(genPolynomial, Di_1, j);
}
j++;
}
}
return pf.getResult();
}
} catch (JASConversionException e) {
if (Config.DEBUG) {
e.printStackTrace();
}
}
return F.NIL;
}
use of org.matheclipse.core.expression.ASTRange in project symja_android_library by axkr.
the class Roots method rootsOfVariable.
/**
*
* @param expr
* @param denominator
* @param variables
* @param numericSolutions
* @param engine
* @return <code>F.NIL</code> if no evaluation was possible.
*/
protected static IAST rootsOfVariable(final IExpr expr, final IExpr denominator, final IAST variables, boolean numericSolutions, EvalEngine engine) {
IAST result = F.NIL;
ASTRange r = new ASTRange(variables, 1);
List<IExpr> varList = r;
try {
IExpr temp;
IAST list = rootsOfQuadraticExprPolynomial(expr, variables);
if (list.isPresent()) {
return list;
}
JASConvert<BigRational> jas = new JASConvert<BigRational>(varList, BigRational.ZERO);
GenPolynomial<BigRational> polyRat = jas.expr2JAS(expr, numericSolutions);
// if (polyRat.degree(0) <= 2) {
result = rootsOfExprPolynomial(expr, variables, false);
if (result.isPresent()) {
return result;
}
// }
result = F.List();
IAST factorRational = Algebra.factorRational(polyRat, jas, varList, F.List);
for (int i = 1; i < factorRational.size(); i++) {
temp = F.evalExpand(factorRational.get(i));
IAST quarticResultList = QuarticSolver.solve(temp, variables.arg1());
if (quarticResultList.isPresent()) {
for (int j = 1; j < quarticResultList.size(); j++) {
if (numericSolutions) {
result.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
} else {
result.append(quarticResultList.get(j));
}
}
} else {
polyRat = jas.expr2JAS(temp, numericSolutions);
IAST factorComplex = Algebra.factorComplex(polyRat, jas, varList, F.List, true);
for (int k = 1; k < factorComplex.size(); k++) {
temp = F.evalExpand(factorComplex.get(k));
quarticResultList = QuarticSolver.solve(temp, variables.arg1());
if (quarticResultList.isPresent()) {
for (int j = 1; j < quarticResultList.size(); j++) {
if (numericSolutions) {
result.append(F.chopExpr(engine.evalN(quarticResultList.get(j)), Config.DEFAULT_ROOTS_CHOP_DELTA));
} else {
result.append(quarticResultList.get(j));
}
}
} else {
double[] coefficients = CoefficientList.coefficientList(temp, (ISymbol) variables.arg1());
if (coefficients == null) {
return F.NIL;
}
IAST resultList = findRoots(coefficients);
// true);
if (resultList.size() > 0) {
result.appendArgs(resultList);
}
}
}
}
}
result = QuarticSolver.createSet(result);
return result;
} catch (JASConversionException e) {
result = rootsOfExprPolynomial(expr, variables, true);
}
if (result.isPresent()) {
if (!denominator.isNumber()) {
// eliminate roots from the result list, which occur in the
// denominator
int i = 1;
while (i < result.size()) {
IExpr temp = denominator.replaceAll(F.Rule(variables.arg1(), result.get(i)));
if (temp.isPresent() && engine.evaluate(temp).isZero()) {
result.remove(i);
continue;
}
i++;
}
}
return result;
}
return F.NIL;
}
use of org.matheclipse.core.expression.ASTRange in project symja_android_library by axkr.
the class Algebra method cancelGCD.
/**
* Calculate the 3 elements result array
*
* <pre>
* [
* commonFactor,
* numeratorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial)),
* denominatorPolynomial.divide(gcd(numeratorPolynomial, denominatorPolynomial))
* ]
* </pre>
*
* for the given expressions <code>numeratorPolynomial</code> and <code>denominatorPolynomial</code>.
*
*
* @param numeratorPolynomial
* a <code>BigRational</code> polynomial which could be converted to JAS polynomial
* @param denominatorPolynomial
* a <code>BigRational</code> polynomial which could be converted to JAS polynomial
* @return <code>null</code> if the expressions couldn't be converted to JAS polynomials or gcd equals 1
* @throws JASConversionException
*/
public static IExpr[] cancelGCD(IExpr numeratorPolynomial, IExpr denominatorPolynomial) throws JASConversionException {
try {
if (denominatorPolynomial.isInteger() && numeratorPolynomial.isPlus()) {
IExpr[] result = Cancel.cancelPlusIntegerGCD((IAST) numeratorPolynomial, (IInteger) denominatorPolynomial);
if (result != null) {
return result;
}
}
VariablesSet eVar = new VariablesSet(numeratorPolynomial);
eVar.addVarList(denominatorPolynomial);
if (eVar.size() == 0) {
return null;
}
IAST vars = eVar.getVarList();
ExprPolynomialRing ring = new ExprPolynomialRing(vars);
ExprPolynomial pol1 = ring.create(numeratorPolynomial);
ExprPolynomial pol2 = ring.create(denominatorPolynomial);
ASTRange r = new ASTRange(eVar.getVarList(), 1);
JASIExpr jas = new JASIExpr(r, true);
GenPolynomial<IExpr> p1 = jas.expr2IExprJAS(pol1);
GenPolynomial<IExpr> p2 = jas.expr2IExprJAS(pol2);
GreatestCommonDivisor<IExpr> engine;
engine = GCDFactory.getImplementation(ExprRingFactory.CONST);
GenPolynomial<IExpr> gcd = engine.gcd(p1, p2);
IExpr[] result = new IExpr[3];
if (gcd.isONE()) {
result[0] = jas.exprPoly2Expr(gcd);
result[1] = jas.exprPoly2Expr(p1);
result[2] = jas.exprPoly2Expr(p2);
} else {
result[0] = F.C1;
result[1] = F.eval(jas.exprPoly2Expr(p1.divide(gcd)));
result[2] = F.eval(jas.exprPoly2Expr(p2.divide(gcd)));
}
return result;
} catch (RuntimeException e) {
if (Config.DEBUG) {
e.printStackTrace();
}
}
return null;
}
use of org.matheclipse.core.expression.ASTRange in project symja_android_library by axkr.
the class RootIntervals method croots.
/**
* Complex numeric roots intervals.
*
* @param ast
* @return
*/
public static IAST croots(final IExpr arg, boolean numeric) {
try {
VariablesSet eVar = new VariablesSet(arg);
if (!eVar.isSize(1)) {
// only possible for univariate polynomials
return F.NIL;
}
IExpr expr = F.evalExpandAll(arg);
ASTRange r = new ASTRange(eVar.getVarList(), 1);
List<IExpr> varList = r;
ComplexRing<BigRational> cfac = new ComplexRing<BigRational>(new BigRational(1));
ComplexRootsAbstract<BigRational> cr = new ComplexRootsSturm<BigRational>(cfac);
JASConvert<Complex<BigRational>> jas = new JASConvert<Complex<BigRational>>(varList, cfac);
GenPolynomial<Complex<BigRational>> poly = jas.numericExpr2JAS(expr);
Squarefree<Complex<BigRational>> engine = SquarefreeFactory.<Complex<BigRational>>getImplementation(cfac);
poly = engine.squarefreePart(poly);
List<Rectangle<BigRational>> roots = cr.complexRoots(poly);
BigRational len = new BigRational(1, 100000L);
IAST resultList = F.List();
if (numeric) {
for (Rectangle<BigRational> root : roots) {
Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
resultList.append(JASConvert.jas2Numeric(refine.getCenter(), Config.DEFAULT_ROOTS_CHOP_DELTA));
}
} else {
IAST rectangleList;
for (Rectangle<BigRational> root : roots) {
rectangleList = F.List();
Rectangle<BigRational> refine = cr.complexRootRefinement(root, poly, len);
rectangleList.append(JASConvert.jas2Complex(refine.getNW()));
rectangleList.append(JASConvert.jas2Complex(refine.getSW()));
rectangleList.append(JASConvert.jas2Complex(refine.getSE()));
rectangleList.append(JASConvert.jas2Complex(refine.getNE()));
resultList.append(rectangleList);
// System.out.println("refine = " + refine);
}
}
return resultList;
} catch (InvalidBoundaryException e) {
if (Config.SHOW_STACKTRACE) {
e.printStackTrace();
}
} catch (JASConversionException e) {
if (Config.SHOW_STACKTRACE) {
e.printStackTrace();
}
}
return F.NIL;
}
Aggregations