use of org.matheclipse.core.interfaces.INum in project symja_android_library by axkr.
the class MathMLContentFormFactory method convert.
public void convert(final StringBuffer buf, final IExpr o, final int precedence) {
if (o instanceof IAST) {
final IAST f = ((IAST) o);
// System.out.println(f.getHeader().toString());
// IConverter converter = (IConverter)
// operTab.get(f.getHeader().toString());
// if (converter == null) {
// converter = reflection(f.getHeader().toString());
// if (converter == null || (converter.convert(buf, f, 0) == false))
// {
// convertHeadList(buf, f);
// }
// } else {
// if (converter.convert(buf, f, precedence) == false) {
// convertHeadList(buf, f);
// }
// }
IAST ast = f;
IAST temp;
if (f.topHead().hasFlatAttribute()) {
// associative
if ((temp = EvalAttributes.flatten(f)).isPresent()) {
ast = temp;
}
}
IExpr h = ast.head();
if (h.isSymbol()) {
IConverter converter = reflection(((ISymbol) h).getSymbolName());
if (converter != null) {
StringBuffer sb = new StringBuffer();
if (converter.convert(sb, ast, precedence)) {
buf.append(sb);
return;
}
}
}
convertAST(buf, ast);
return;
}
if (o instanceof INum) {
convertDouble(buf, (INum) o, precedence);
return;
}
if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence);
return;
}
if (o instanceof IInteger) {
convertInteger(buf, (IInteger) o, precedence);
return;
}
if (o instanceof IFraction) {
convertFraction(buf, (IFraction) o, precedence);
return;
}
if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence);
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
convertString(buf, o.toString());
}
use of org.matheclipse.core.interfaces.INum in project symja_android_library by axkr.
the class MathMLFormFactory method convert.
public void convert(final StringBuffer buf, final IExpr o, final int precedence) {
String str = CONSTANT_EXPRS.get(o);
if (str != null) {
buf.append(str);
return;
}
if (o instanceof IAST) {
final IAST f = ((IAST) o);
IAST ast = f;
IAST temp;
if (f.topHead().hasFlatAttribute()) {
// associative
if ((temp = EvalAttributes.flatten(f)).isPresent()) {
ast = temp;
}
}
IExpr h = ast.head();
if (h.isSymbol()) {
IConverter converter = reflection(((ISymbol) h).getSymbolName());
if (converter != null) {
StringBuffer sb = new StringBuffer();
if (converter.convert(sb, ast, precedence)) {
buf.append(sb);
return;
}
}
}
convertAST(buf, ast);
return;
}
if (o instanceof INum) {
convertDouble(buf, (INum) o, precedence);
return;
}
if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence);
return;
}
if (o instanceof IInteger) {
convertInteger(buf, (IInteger) o, precedence);
return;
}
if (o instanceof IFraction) {
convertFraction(buf, (IFraction) o, precedence);
return;
}
if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence);
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
convertString(buf, o.toString());
}
use of org.matheclipse.core.interfaces.INum 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.INum in project symja_android_library by axkr.
the class Iterator method create.
/**
* Iterator specification for functions like <code>Table()</code> or
* <code>Sum()</code> or <code>Product()</code>
*
* @param list
* a list representing an iterator specification
* @param engine
* the evaluation engine
* @return the iterator
*/
public static IIterator<IExpr> create(final IAST list, final EvalEngine engine) {
EvalEngine evalEngine = engine;
IExpr lowerLimit;
IExpr upperLimit;
IExpr step;
ISymbol variable;
boolean fNumericMode;
// fNumericMode = evalEngine.isNumericMode() ||
// list.isMember(Predicates.isNumeric(), false);
boolean localNumericMode = evalEngine.isNumericMode();
try {
if (list.hasNumericArgument()) {
evalEngine.setNumericMode(true);
}
fNumericMode = evalEngine.isNumericMode();
switch(list.size()) {
case 2:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg1());
step = F.C1;
variable = null;
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 3:
lowerLimit = F.C1;
upperLimit = evalEngine.evalWithoutNumericReset(list.arg2());
step = F.C1;
if (list.arg1() instanceof ISymbol) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (upperLimit instanceof Num) {
return new DoubleIterator(variable, 1.0, ((INum) upperLimit).doubleValue(), 1.0);
}
if (upperLimit.isInteger()) {
try {
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, 1, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isRational()) {
try {
return new RationalIterator(variable, F.C1, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (upperLimit.isSignedNumber()) {
return new ISignedNumberIterator(variable, F.C1, (ISignedNumber) upperLimit, F.C1);
}
break;
case 4:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = F.C1;
if (list.arg1().isSymbol()) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (lowerLimit instanceof Num && upperLimit instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), 1.0);
}
if (lowerLimit.isInteger() && upperLimit.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, 1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, F.C1);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber()) {
ISignedNumber iLowerLimit = (ISignedNumber) lowerLimit;
ISignedNumber iUpperLimit = (ISignedNumber) upperLimit;
return new ISignedNumberIterator(variable, iLowerLimit, iUpperLimit, F.C1);
}
break;
case 5:
lowerLimit = evalEngine.evalWithoutNumericReset(list.arg2());
upperLimit = evalEngine.evalWithoutNumericReset(list.arg3());
step = evalEngine.evalWithoutNumericReset(list.arg4());
if (list.arg1() instanceof ISymbol) {
variable = (ISymbol) list.arg1();
} else {
variable = null;
}
if (lowerLimit instanceof Num && upperLimit instanceof Num && step instanceof Num) {
return new DoubleIterator(variable, ((INum) lowerLimit).doubleValue(), ((INum) upperLimit).doubleValue(), ((INum) step).doubleValue());
}
if (lowerLimit.isInteger() && upperLimit.isInteger() && step.isInteger()) {
try {
int iLowerLimit = ((IInteger) lowerLimit).toInt();
int iUpperLimit = ((IInteger) upperLimit).toInt();
int iStep = ((IInteger) step).toInt();
return new IntIterator(variable, iLowerLimit, iUpperLimit, iStep);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isRational() && upperLimit.isRational() && step.isRational()) {
try {
return new RationalIterator(variable, (IRational) lowerLimit, (IRational) upperLimit, (IRational) step);
} catch (ArithmeticException ae) {
//
}
} else if (lowerLimit.isSignedNumber() && upperLimit.isSignedNumber() && step.isSignedNumber()) {
return new ISignedNumberIterator(variable, (ISignedNumber) lowerLimit, (ISignedNumber) upperLimit, (ISignedNumber) step);
}
break;
default:
lowerLimit = null;
upperLimit = null;
step = null;
variable = null;
}
return new ExprIterator(variable, evalEngine, lowerLimit, upperLimit, step, fNumericMode);
} finally {
evalEngine.setNumericMode(localNumericMode);
}
}
use of org.matheclipse.core.interfaces.INum in project symja_android_library by axkr.
the class AbstractAST method isRealMatrix.
/** {@inheritDoc} */
@Override
public boolean isRealMatrix() {
if (isList()) {
final int[] dim = new int[2];
dim[0] = size() - 1;
if (dim[0] > 0) {
dim[1] = 0;
if (arg1().isList()) {
IAST row = (IAST) arg1();
dim[1] = row.size() - 1;
boolean containsNum = false;
for (int j = 1; j < row.size(); j++) {
if (row.get(j).isSignedNumber()) {
if (row.get(j) instanceof INum) {
if (!(row.get(j) instanceof Num)) {
// Apfloat number
return false;
}
containsNum = true;
}
} else {
return false;
}
}
for (int i = 2; i < size(); i++) {
if (!get(i).isList()) {
// this row is not a list
return false;
}
row = (IAST) get(i);
if (dim[1] != row.size() - 1) {
// this row has another dimension
return false;
}
for (int j = 1; j < row.size(); j++) {
if (row.get(j).isSignedNumber()) {
if (row.get(j) instanceof INum) {
if (!(row.get(j) instanceof Num)) {
// Apfloat number
return false;
}
containsNum = true;
}
} else {
return false;
}
}
}
addEvalFlags(IAST.IS_MATRIX);
return containsNum;
}
}
}
return false;
}
Aggregations