use of edu.jas.poly.ExpVector in project symja_android_library by axkr.
the class JASConvert method complexPoly2Expr.
/**
* Convert a JAS complex polynomial to <code>IExpr</code>.
*
* @param poly
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
public IExpr complexPoly2Expr(final GenPolynomial<Complex<BigRational>> poly) throws ArithmeticException, ClassCastException {
if (poly.length() == 0) {
return F.C0;
}
IAST result = F.Plus();
for (Monomial<Complex<BigRational>> monomial : poly) {
Complex<BigRational> coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
IAST monomTimes = F.Times();
monomialToExpr(coeff, exp, monomTimes);
result.append(monomTimes.getOneIdentity(F.C1));
}
return result.getOneIdentity(F.C0);
}
use of edu.jas.poly.ExpVector in project symja_android_library by axkr.
the class JASIExpr method exprPoly2Expr.
/**
* Converts a <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a>
* polynomial to a MathEclipse AST with head <code>Plus</code>
*
* @param poly
* a JAS polynomial
* @param variable
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
public IExpr exprPoly2Expr(final GenPolynomial<IExpr> poly, IExpr variable) {
if (poly.length() == 0) {
return F.C0;
}
IAST result = F.Plus();
for (Monomial<IExpr> monomial : poly) {
IExpr coeff = monomial.coefficient();
ExpVector exp = monomial.exponent();
IAST monomTimes = F.Times();
monomialToExpr(coeff, exp, monomTimes);
result.append(monomTimes.getOneIdentity(F.C1));
}
return result.getOneIdentity(F.C0);
}
use of edu.jas.poly.ExpVector in project symja_android_library by axkr.
the class JASModInteger method expr2Poly.
/**
* Convert the given expression into a
* <a href="http://krum.rz.uni-mannheim.de/jas/">JAS</a> polynomial
*
* @param exprPoly
* @param numeric2Rational
* if <code>true</code>, <code>INum</code> double values are
* converted to <code>BigRational</code> internally
*
* @return
* @throws ArithmeticException
* @throws ClassCastException
*/
private GenPolynomial<ModLong> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
GenPolynomial<ModLong> result = fPolyFactory.getZERO();
GenPolynomial<ModLong> p = fPolyFactory.getZERO();
if (ast.isPlus()) {
IExpr expr = ast.arg1();
result = expr2Poly(expr, numeric2Rational);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2Poly(expr, numeric2Rational);
result = result.sum(p);
}
return result;
} else if (ast.isTimes()) {
IExpr expr = ast.arg1();
result = expr2Poly(expr, numeric2Rational);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2Poly(expr, numeric2Rational);
result = result.multiply(p);
}
return result;
} else if (ast.isPower()) {
final IExpr expr = ast.arg1();
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(expr)) {
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
ExpVector e = ExpVector.create(fVariables.size(), i, exponent);
return fPolyFactory.valueOf(e);
}
}
}
} else if (exprPoly instanceof ISymbol) {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
// class cast exception
} else if (exprPoly instanceof IInteger) {
return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
}
throw new ClassCastException(exprPoly.toString());
}
use of edu.jas.poly.ExpVector in project symja_android_library by axkr.
the class JASModInteger method monomialToExpr.
public boolean monomialToExpr(IInteger coeff, ExpVector exp, IAST monomTimes) {
long lExp;
ExpVector leer = fPolyFactory.evzero;
if (!coeff.isOne()) {
monomTimes.append(coeff);
}
for (int i = 0; i < exp.length(); i++) {
lExp = exp.getVal(i);
if (lExp != 0) {
int ix = leer.varIndex(i);
if (ix >= 0) {
if (lExp == 1L) {
monomTimes.append(fVariables.get(ix));
} else {
monomTimes.append(F.Power(fVariables.get(ix), F.integer(lExp)));
}
} else {
return false;
}
}
}
return true;
}
use of edu.jas.poly.ExpVector in project symja_android_library by axkr.
the class JASModInteger method expr2IExprPoly.
private GenPolynomial<ModLong> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
GenPolynomial<ModLong> result = fPolyFactory.getZERO();
GenPolynomial<ModLong> p = fPolyFactory.getZERO();
if (ast.isPlus()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.sum(p);
}
return result;
} else if (ast.isTimes()) {
IExpr expr = ast.arg1();
result = expr2IExprPoly(expr);
for (int i = 2; i < ast.size(); i++) {
expr = ast.get(i);
p = expr2IExprPoly(expr);
result = result.multiply(p);
}
return result;
} else if (ast.isPower()) {
final IExpr expr = ast.arg1();
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(expr)) {
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
//
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
ExpVector e = ExpVector.create(fVariables.size(), i, exponent);
return fPolyFactory.getONE().multiply(e);
}
}
}
} else if (exprPoly instanceof ISymbol) {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
return new GenPolynomial(fPolyFactory, exprPoly);
} else if (exprPoly instanceof IInteger) {
return fPolyFactory.fromInteger((java.math.BigInteger) ((IInteger) exprPoly).asType(java.math.BigInteger.class));
} else if (exprPoly instanceof IFraction) {
return fraction2Poly((IFraction) exprPoly);
}
if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
return new GenPolynomial(fPolyFactory, exprPoly);
} else {
for (int i = 0; i < fVariables.size(); i++) {
if (fVariables.get(i).equals(exprPoly)) {
ExpVector e = ExpVector.create(fVariables.size(), i, 1L);
return fPolyFactory.getONE().multiply(e);
}
}
}
throw new ClassCastException(exprPoly.toString());
}
Aggregations