use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Times method convertTimesOperator.
private boolean convertTimesOperator(final StringBuffer buf, final IAST timesAST, final int precedence, final int caller) {
int size = timesAST.size();
if (size > 1) {
IExpr arg1 = timesAST.arg1();
if (arg1.isMinusOne()) {
if (size == 2) {
precedenceOpen(buf, precedence);
fFactory.convert(buf, arg1, fPrecedence);
} else {
if (caller == PLUS_CALL) {
buf.append("-");
if (size == 3) {
fFactory.convert(buf, timesAST.arg2(), fPrecedence);
return true;
}
} else {
precedenceOpen(buf, precedence);
buf.append(" - ");
}
}
} else if (arg1.isOne()) {
if (size == 2) {
precedenceOpen(buf, precedence);
fFactory.convert(buf, arg1, fPrecedence);
} else {
if (caller == PLUS_CALL) {
if (size == 3) {
buf.append("+");
fFactory.convert(buf, timesAST.arg2(), fPrecedence);
return true;
}
} else {
precedenceOpen(buf, precedence);
}
}
} else {
if (caller == PLUS_CALL) {
if ((arg1.isSignedNumber()) && (((ISignedNumber) arg1).isNegative())) {
buf.append(" - ");
arg1 = ((ISignedNumber) arg1).opposite();
} else {
buf.append("+");
}
} else {
precedenceOpen(buf, precedence);
}
fFactory.convert(buf, arg1, fPrecedence);
if (fOperator.compareTo("") != 0) {
if (size > 2) {
if (timesAST.arg1().isNumber() && isTeXNumberDigit(timesAST.arg2())) {
// Issue #67, #117: if we have 2 TeX number
// expressions we use
// the \cdot operator see
// http://tex.stackexchange.com/questions/40794/when-should-cdot-be-used-to-indicate-multiplication
buf.append("\\cdot ");
} else {
buf.append("\\,");
}
}
}
}
}
for (int i = 2; i < size; i++) {
fFactory.convert(buf, timesAST.get(i), fPrecedence);
if ((i < timesAST.size() - 1) && (fOperator.compareTo("") != 0)) {
if (timesAST.get(1).isNumber() && isTeXNumberDigit(timesAST.get(i + 1))) {
// Issue #67, #117: if we have 2 TeX number expressions we
// use
// the \cdot operator see
// http://tex.stackexchange.com/questions/40794/when-should-cdot-be-used-to-indicate-multiplication
buf.append("\\cdot ");
} else {
buf.append("\\,");
}
}
}
precedenceClose(buf, precedence);
return true;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Plus method convert.
/*
* (non-Javadoc)
*
* @see org.matheclipse.core.form.mathml.IConverter#convert(java.lang.StringBuffer, org.matheclipse.parser.interfaces.IAST, int)
*/
@Override
public boolean convert(final StringBuffer buf, final IAST f, final int precedence) {
IExpr expr;
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
final Times timesConverter = new org.matheclipse.core.form.mathml.reflection.Times();
timesConverter.setFactory(fFactory);
int size = f.size() - 1;
for (int i = size; i > 0; i--) {
expr = f.get(i);
if ((i < size) && (expr instanceof IAST) && ((IAST) expr).head().equals(F.Times)) {
timesConverter.convertTimesFraction(buf, (IAST) expr, fPrecedence, Times.PLUS_CALL);
} else {
if (i < size) {
if ((expr instanceof ISignedNumber) && (((ISignedNumber) expr).isNegative())) {
fFactory.tag(buf, "mo", "-");
expr = ((ISignedNumber) expr).negate();
} else {
fFactory.tag(buf, "mo", "+");
}
}
fFactory.convert(buf, expr, fPrecedence);
}
}
precedenceClose(buf, precedence);
fFactory.tagEnd(buf, fFirstTag);
return true;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class EvalComplex method evalSymbol.
public static double[] evalSymbol(final ISymbol symbol) {
if (symbol.hasLocalVariableStack()) {
final IExpr expr = symbol.get();
if (expr instanceof ISignedNumber) {
final double[] result = new double[2];
result[0] = ((ISignedNumber) expr).doubleValue();
result[1] = 0.0;
return result;
}
if (expr instanceof ComplexNum) {
final double[] result = new double[2];
result[0] = ((ComplexNum) expr).getRealPart();
result[1] = ((ComplexNum) expr).getImaginaryPart();
return result;
}
}
if (symbol.isSignedNumberConstant()) {
// fast evaluation path
final double[] result = new double[2];
result[0] = ((ISignedNumberConstant) ((IBuiltInSymbol) symbol).getEvaluator()).evalReal();
result[1] = 0.0;
return result;
}
if (symbol.isBuiltInSymbol()) {
final IEvaluator module = ((IBuiltInSymbol) symbol).getEvaluator();
if (module instanceof INumericComplexConstant) {
// fast evaluation path
return ((INumericComplexConstant) module).evalComplex();
}
}
// slow evaluation path
final IExpr result = F.evaln(symbol);
if (result instanceof ComplexNum) {
final double[] res = new double[2];
res[0] = ((ComplexNum) result).getRealPart();
res[1] = ((ComplexNum) result).getImaginaryPart();
return res;
}
if (result instanceof Num) {
final double[] res = new double[2];
res[0] = ((Num) result).doubleValue();
res[1] = 0.0;
return res;
}
throw new UnsupportedOperationException();
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class AbstractArg12 method binaryOperator.
public IExpr binaryOperator(final IExpr o0, final IExpr o1) {
IExpr result = F.NIL;
if (o0 instanceof ApcomplexNum) {
if (o1.isNumber()) {
result = e2ApcomplexArg((ApcomplexNum) o0, ((INumber) o1).apcomplexNumValue(((ApcomplexNum) o0).precision()));
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ApcomplexNum) {
if (o0.isNumber()) {
result = e2ApcomplexArg(((INumber) o0).apcomplexNumValue(((ApcomplexNum) o1).precision()), (ApcomplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o0 instanceof ComplexNum) {
if (o1.isNumber()) {
result = e2DblComArg((ComplexNum) o0, ((INumber) o1).complexNumValue());
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ComplexNum) {
if (o0.isNumber()) {
result = e2DblComArg(((INumber) o0).complexNumValue(), (ComplexNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
if (o0 instanceof ApfloatNum) {
if (o1.isSignedNumber()) {
result = e2ApfloatArg((ApfloatNum) o0, ((ISignedNumber) o1).apfloatNumValue(((ApfloatNum) o0).precision()));
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof ApfloatNum) {
if (o0.isSignedNumber()) {
result = e2ApfloatArg(((ISignedNumber) o0).apfloatNumValue(((ApfloatNum) o1).precision()), (ApfloatNum) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o0 instanceof Num) {
if (o1.isSignedNumber()) {
result = e2DblArg((Num) o0, ((ISignedNumber) o1).numValue());
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
} else if (o1 instanceof Num) {
if (o0.isSignedNumber()) {
result = e2DblArg(((ISignedNumber) o0).numValue(), (Num) o1);
}
if (result.isPresent()) {
return result;
}
return e2ObjArg(o0, o1);
}
result = e2ObjArg(o0, o1);
if (result.isPresent()) {
return result;
}
if (o0 instanceof IInteger) {
if (o1 instanceof IInteger) {
return e2IntArg((IInteger) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return e2FraArg(F.fraction((IInteger) o0, F.C1), (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IInteger) o0, F.C0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IFraction) {
if (o1 instanceof IInteger) {
return e2FraArg((IFraction) o0, F.fraction((IInteger) o1, F.C1));
}
if (o1 instanceof IFraction) {
return e2FraArg((IFraction) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg(F.complex((IFraction) o0), (IComplex) o1);
}
return F.NIL;
}
if (o0 instanceof IComplex) {
if (o1 instanceof IInteger) {
return eComIntArg((IComplex) o0, (IInteger) o1);
}
if (o1 instanceof IFraction) {
return eComFraArg((IComplex) o0, (IFraction) o1);
}
if (o1 instanceof IComplex) {
return e2ComArg((IComplex) o0, (IComplex) o1);
}
}
if (o0 instanceof ISymbol) {
if (o1 instanceof ISymbol) {
return e2SymArg((ISymbol) o0, (ISymbol) o1);
}
}
if (o0 instanceof IAST) {
if (o1 instanceof IInteger) {
return eFunIntArg((IAST) o0, (IInteger) o1);
}
if (o1 instanceof IAST) {
return e2FunArg((IAST) o0, (IAST) o1);
}
}
return F.NIL;
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class LinearProgramming method numericEval.
@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 4);
try {
if (ast.arg1().isList() && ast.arg2().isList() && ast.arg3().isList()) {
double[] arg1D = Expr2Object.toDoubleVector((IAST) ast.arg1());
LinearObjectiveFunction f = new LinearObjectiveFunction(arg1D, 0);
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
IAST arg2 = (IAST) ast.arg2();
IAST arg3 = (IAST) ast.arg3();
if (arg2.size() != arg3.size()) {
return F.NIL;
}
double[] arg2D;
double[] arg3D;
for (int i = 1; i < arg2.size(); i++) {
arg2D = Expr2Object.toDoubleVector((IAST) arg2.get(i));
if (arg2.get(i).isList()) {
if (arg3.get(i).isList()) {
arg3D = Expr2Object.toDoubleVector((IAST) arg3.get(i));
if (arg3D.length >= 2) {
double val = arg3D[1];
if (val == 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.EQ, arg3D[0]));
} else if (val < 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.LEQ, arg3D[0]));
} else if (val > 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
}
} else if (arg3D.length == 1) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
}
} else {
ISignedNumber sn = arg3.get(i).evalSignedNumber();
if (sn != null) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
} else {
throw new WrongArgumentType(arg3, arg3.get(i), i, "Numeric vector or number expected!");
}
}
} else {
throw new WrongArgumentType(ast, ast.get(i), i, "Numeric vector expected!");
}
}
SimplexSolver solver = new SimplexSolver();
// PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
double[] values = solution.getPointRef();
return F.List(values);
}
} catch (MathIllegalStateException oe) {
throw new WrappedException(oe);
// if (Config.SHOW_STACKTRACE) {
// oe.printStackTrace();
// }
}
return F.NIL;
}
Aggregations