use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class PatternMap method setValue.
public boolean setValue(IPatternObject pattern, IExpr expr) {
ISymbol sym = pattern.getSymbol();
IExpr temp = pattern;
if (sym != null) {
temp = sym;
}
int indx = get(temp);
if (indx >= 0) {
fPatternValuesArray[indx] = expr;
return true;
}
throw new IllegalStateException("Pattern:" + pattern + " is not available");
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class JASIExpr method expr2IExprPoly.
private GenPolynomial<IExpr> expr2IExprPoly(final IExpr exprPoly) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
GenPolynomial<IExpr> result = fPolyFactory.getZERO();
GenPolynomial<IExpr> 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();
if (expr instanceof ISymbol) {
ExpVector leer = fPolyFactory.evzero;
int ix = leer.indexVar(expr.toString(), fPolyFactory.getVars());
if (ix >= 0) {
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(), ix, exponent);
return fPolyFactory.getONE().multiply(e);
}
}
} else if (fNumericFunction) {
if (ast.isNumericFunction()) {
return new GenPolynomial<IExpr>(fPolyFactory, ast);
}
}
} else if (exprPoly instanceof ISymbol) {
ExpVector leer = fPolyFactory.evzero;
int ix = leer.indexVar(exprPoly.toString(), fPolyFactory.getVars());
if (ix >= 0) {
ExpVector e = ExpVector.create(fVariables.size(), ix, 1L);
return fPolyFactory.getONE().multiply(e);
}
if (fNumericFunction) {
if (exprPoly.isNumericFunction()) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
throw new ClassCastException(exprPoly.toString());
} else {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
} else if (exprPoly instanceof IInteger) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
} else if (exprPoly instanceof IFraction) {
return new GenPolynomial<IExpr>(fPolyFactory, exprPoly);
}
if (exprPoly.isFree(t -> fVariables.contains(t), true)) {
return new GenPolynomial<IExpr>(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());
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class Expr2LP method expr2ObjectiveFunction.
private ISignedNumber expr2ObjectiveFunction(final IExpr expr, double[] coefficients) throws ArithmeticException, ClassCastException {
if (expr instanceof IAST) {
final IAST ast = (IAST) expr;
if (ast.isPlus()) {
double constantTerm = 0.0;
for (int i = 1; i < ast.size(); i++) {
IExpr temp = ast.get(i);
ISignedNumber num = expr2ObjectiveFunction(temp, coefficients);
if (num != null) {
constantTerm += num.doubleValue();
}
}
return F.num(constantTerm);
} else if (ast.isTimes()) {
ISymbol variable = null;
double value = 1.0;
for (int i = 1; i < ast.size(); i++) {
IExpr temp = ast.get(i);
if (temp.isVariable()) {
if (variable != null) {
throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
}
variable = (ISymbol) temp;
continue;
}
ISignedNumber num = temp.evalSignedNumber();
if (num != null) {
value *= num.doubleValue();
continue;
}
throw new WrongArgumentType(temp, "Conversion from expression to linear programming expression failed");
}
if (variable != null) {
for (int i = 0; i < coefficients.length; i++) {
if (variable.equals(fVariables.get(i))) {
coefficients[i] += value;
return null;
}
}
throw new WrongArgumentType(ast, "Conversion from expression to linear programming expression failed");
}
return F.num(value);
}
} else if (expr.isVariable()) {
ISymbol variable = (ISymbol) expr;
for (int i = 0; i < coefficients.length; i++) {
if (variable.equals(fVariables.get(i))) {
coefficients[i] += 1.0d;
return null;
}
}
throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
}
ISignedNumber num = expr.evalSignedNumber();
if (num != null) {
return num;
}
throw new WrongArgumentType(expr, "Conversion from expression to linear programming expression failed");
}
use of org.matheclipse.core.interfaces.ISymbol in project symja_android_library by axkr.
the class JASConvert 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<C> expr2Poly(final IExpr exprPoly, boolean numeric2Rational) throws ArithmeticException, ClassCastException {
if (exprPoly instanceof IAST) {
final IAST ast = (IAST) exprPoly;
if (ast.isSlot()) {
try {
return fPolyFactory.univariate(ast.toString(), 1L);
} catch (IllegalArgumentException iae) {
// fall through
}
} else {
GenPolynomial<C> result = fPolyFactory.getZERO();
GenPolynomial<C> 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() && ast.arg1().isSymbol()) {
final ISymbol expr = (ISymbol) ast.arg1();
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
try {
return fPolyFactory.univariate(expr.getSymbolName(), exponent);
} catch (IllegalArgumentException iae) {
// fall through
}
} else if (ast.isPower() && ast.arg1().isSlot()) {
final IAST expr = (IAST) ast.arg1();
int exponent = -1;
try {
exponent = Validate.checkPowerExponent(ast);
} catch (WrongArgumentType e) {
}
if (exponent < 0) {
throw new ArithmeticException("JASConvert:expr2Poly - invalid exponent: " + ast.arg2().toString());
}
try {
return fPolyFactory.univariate(expr.toString(), exponent);
} catch (IllegalArgumentException iae) {
// fall through
}
}
}
} else if (exprPoly instanceof ISymbol) {
try {
return fPolyFactory.univariate(((ISymbol) exprPoly).getSymbolName(), 1L);
} catch (IllegalArgumentException iae) {
// fall through
}
} 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);
} else if (exprPoly instanceof INum && numeric2Rational) {
IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
return fraction2Poly(frac);
} else if (exprPoly instanceof IComplexNum && numeric2Rational) {
if (F.isZero(((IComplexNum) exprPoly).getImaginaryPart())) {
// the imaginary part is zero
IFraction frac = F.fraction(((INum) exprPoly).getRealPart());
return fraction2Poly(frac);
}
}
throw new ClassCastException(exprPoly.toString());
}
use of org.matheclipse.core.interfaces.ISymbol 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());
}
Aggregations