use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Max method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 1);
if (ast.isAST0()) {
return F.CNInfinity;
}
if (ast.arg1().isInterval1()) {
IAST list = (IAST) ast.arg1().getAt(1);
try {
return (ISignedNumber) list.arg2();
} catch (ClassCastException cca) {
// do nothing
}
}
IAST resultList = EvalAttributes.flatten(F.List, ast);
if (resultList.isPresent()) {
return maximum(resultList, true);
}
return maximum(ast, false);
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Interval method evaluate.
@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
Validate.checkRange(ast, 2);
if (ast.isInterval1()) {
IAST list = (IAST) ast.arg1();
try {
ISignedNumber min = (ISignedNumber) list.arg1();
ISignedNumber max = (ISignedNumber) list.arg2();
if (min.greaterThan(max).isTrue()) {
throw new WrongArgumentType(ast, ast.get(1), 1, "Min > Mac in interval");
}
} catch (ClassCastException cca) {
// do nothing
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISignedNumber 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.ISignedNumber in project symja_android_library by axkr.
the class Expr2Object method toDoubleMatrix.
/**
*
* @param ast
* @return <code>null</code> if ast is no matrix
* @throws WrongArgumentType
* @deprecated use {@link IExpr#toDoubleMatrix()}
*/
public static double[][] toDoubleMatrix(IAST ast) throws WrongArgumentType {
int[] dim = ast.isMatrix();
if (dim == null) {
return null;
}
double[][] result = new double[dim[0]][dim[1]];
ISignedNumber signedNumber;
for (int i = 1; i <= dim[0]; i++) {
IAST row = (IAST) ast.get(i);
for (int j = 1; j <= dim[1]; j++) {
signedNumber = row.get(j).evalSignedNumber();
if (signedNumber != null) {
result[i - 1][j - 1] = signedNumber.doubleValue();
} else {
throw new WrongArgumentType(ast, ast.get(i), i, "Conversion into a matrix of double values not possible!");
}
}
}
return result;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Expr2Object method toDoubleVector.
/**
*
* @param ast
* @return
* @throws WrongArgumentType
* @deprecated use {@link IExpr#toDoubleVector()}
*/
public static double[] toDoubleVector(IAST ast) throws WrongArgumentType {
double[] result = new double[ast.size() - 1];
ISignedNumber signedNumber;
for (int i = 1; i < ast.size(); i++) {
signedNumber = ast.get(i).evalSignedNumber();
if (signedNumber != null) {
result[i - 1] = signedNumber.doubleValue();
} else {
throw new WrongArgumentType(ast, ast.get(i), i, "Conversion into a vector of double values not possible!");
}
}
return result;
}
Aggregations