use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Expr2LP method expr2ObjectiveFunction.
public LinearObjectiveFunction expr2ObjectiveFunction() {
double[] coefficients = new double[fVariables.size()];
ISignedNumber num = expr2ObjectiveFunction(fExpr, coefficients);
if (num == null) {
return new LinearObjectiveFunction(coefficients, 0);
}
return new LinearObjectiveFunction(coefficients, num.doubleValue());
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Expr2LP method expr2Constraint.
public LinearConstraint expr2Constraint() {
double[] coefficients = new double[fVariables.size()];
if (fExpr.isAST()) {
IAST ast = (IAST) fExpr;
if (ast.isAST(F.Equal, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.EQ, 0);
}
return new LinearConstraint(coefficients, Relationship.EQ, -1 * num.doubleValue());
}
if (ast.isAST(F.GreaterEqual, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.GEQ, 0);
}
return new LinearConstraint(coefficients, Relationship.GEQ, -1 * num.doubleValue());
}
if (ast.isAST(F.LessEqual, 3)) {
IExpr expr = F.eval(F.Subtract(ast.arg1(), ast.arg2()));
ISignedNumber num = expr2ObjectiveFunction(expr, coefficients);
if (num == null) {
return new LinearConstraint(coefficients, Relationship.LEQ, 0);
}
return new LinearConstraint(coefficients, Relationship.LEQ, -1 * num.doubleValue());
}
}
throw new WrongArgumentType(fExpr, "Conversion from expression to linear programming expression failed");
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class OutputFormFactory method convert.
public void convert(final Appendable buf, final IExpr o, final int precedence, boolean isASTHead) throws IOException {
if (o instanceof IAST) {
final IAST list = (IAST) o;
IExpr header = list.head();
if (!header.isSymbol()) {
// print expressions like: f(#1, y)& [x]
IAST[] derivStruct = list.isDerivativeAST1();
if (derivStruct != null) {
IAST a1Head = derivStruct[0];
IAST headAST = derivStruct[1];
if (a1Head.isAST1() && a1Head.arg1().isInteger() && headAST.isAST1() && headAST.arg1().isSymbol() && derivStruct[2] != null) {
try {
int n = ((IInteger) a1Head.arg1()).toInt();
// IExpr arg1 = listArg1.arg1();
if (n == 1 || n == 2) {
ISymbol f = (ISymbol) headAST.arg1();
convertSymbol(buf, f);
if (n == 1) {
append(buf, "'");
} else if (n == 2) {
append(buf, "''");
}
convertArgs(buf, f, list);
return;
}
} catch (ArithmeticException ae) {
}
}
}
convert(buf, header, Integer.MIN_VALUE, true);
convertFunctionArgs(buf, list);
return;
}
ISymbol head = list.topHead();
final Operator operator = getOperator(head);
if (operator != null) {
if (operator instanceof PostfixOperator) {
if (list.isAST1()) {
convertPostfixOperator(buf, list, (PostfixOperator) operator, precedence);
return;
}
} else {
if (convertOperator(operator, list, buf, isASTHead ? Integer.MAX_VALUE : precedence, head)) {
return;
}
}
}
if (list.isList() || list instanceof ASTRealVector || list instanceof ASTRealMatrix) {
convertList(buf, list);
return;
}
if (head.equals(F.Part) && (list.size() >= 3)) {
convertPart(buf, list);
return;
}
if (head.equals(F.Slot) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlot(buf, list);
return;
}
if (head.equals(F.SlotSequence) && (list.isAST1()) && (list.arg1() instanceof IInteger)) {
convertSlotSequence(buf, list);
return;
}
if ((head.equals(F.HoldForm) || head.equals(F.Defer)) && (list.isAST1())) {
convert(buf, list.arg1());
return;
}
if (head.equals(F.SeriesData) && (list.size() == 7)) {
if (convertSeriesData(buf, list, precedence)) {
return;
}
}
if (list.isDirectedInfinity()) {
// {
if (list.isAST0()) {
append(buf, "ComplexInfinity");
return;
}
if (list.isAST1()) {
if (list.arg1().isOne()) {
append(buf, "Infinity");
return;
} else if (list.arg1().isMinusOne()) {
if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
append(buf, "(");
}
append(buf, "-Infinity");
if (ASTNodeFactory.PLUS_PRECEDENCE < precedence) {
append(buf, ")");
}
return;
} else if (list.arg1().isImaginaryUnit()) {
append(buf, "I*Infinity");
return;
} else if (list.arg1().isNegativeImaginaryUnit()) {
append(buf, "-I*Infinity");
return;
}
}
}
convertAST(buf, list);
return;
}
if (o instanceof ISignedNumber) {
convertNumber(buf, (ISignedNumber) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplexNum) {
convertDoubleComplex(buf, (IComplexNum) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof IComplex) {
convertComplex(buf, (IComplex) o, precedence, NO_PLUS_CALL);
return;
}
if (o instanceof ISymbol) {
convertSymbol(buf, (ISymbol) o);
return;
}
if (o instanceof IPatternObject) {
convertPattern(buf, (IPatternObject) o);
return;
}
convertString(buf, o.toString());
}
use of org.matheclipse.core.interfaces.ISignedNumber in project symja_android_library by axkr.
the class Times method convertTimesOperator.
/**
* Converts a given <code>Times[...]</code> function into the corresponding MathML output.
*
* @param buf
* @param timesAST
* @param precedence
* @param caller
* @return
*/
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) {
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
fFactory.convert(buf, arg1, fPrecedence);
} else {
if (caller == PLUS_CALL) {
fFactory.tag(buf, "mo", "-");
if (size == 3) {
fFactory.convert(buf, timesAST.arg2(), fPrecedence);
return true;
}
fFactory.tagStart(buf, fFirstTag);
} else {
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
fFactory.tag(buf, "mo", "-");
}
}
} else if (arg1.isOne()) {
if (size == 2) {
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
fFactory.convert(buf, arg1, fPrecedence);
} else {
if (caller == PLUS_CALL) {
if (size == 3) {
fFactory.convert(buf, timesAST.arg2(), fPrecedence);
return true;
}
fFactory.tagStart(buf, fFirstTag);
} else {
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
}
}
} else {
if (caller == PLUS_CALL) {
if ((arg1 instanceof ISignedNumber) && (((ISignedNumber) arg1).isNegative())) {
fFactory.tag(buf, "mo", "-");
fFactory.tagStart(buf, fFirstTag);
arg1 = ((ISignedNumber) arg1).opposite();
} else {
fFactory.tag(buf, "mo", "+");
fFactory.tagStart(buf, fFirstTag);
}
} else {
fFactory.tagStart(buf, fFirstTag);
precedenceOpen(buf, precedence);
}
fFactory.convert(buf, arg1, fPrecedence);
if (fOperator.compareTo("") != 0) {
fFactory.tag(buf, "mo", fOperator);
}
}
}
for (int i = 2; i < size; i++) {
fFactory.convert(buf, timesAST.get(i), fPrecedence);
if ((i < timesAST.size() - 1) && (fOperator.compareTo("") != 0)) {
fFactory.tag(buf, "mo", fOperator);
}
}
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 Fit method numericEval.
@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
Validate.checkSize(ast, 4);
if (ast.arg2().isSignedNumber() && ast.arg3().isSymbol()) {
int degree = ((ISignedNumber) ast.arg2()).toInt();
double[] initialGuess = new double[degree];
for (int i = 0; i < degree; i++) {
initialGuess[i] = 1.0;
}
AbstractCurveFitter fitter = PolynomialCurveFitter.create(degree);
int[] isMatrix = ast.arg1().isMatrix();
WeightedObservedPoints obs = new WeightedObservedPoints();
if (isMatrix != null && isMatrix[1] == 2) {
final double[][] elements = Expr2Object.toDoubleMatrix((IAST) ast.arg1());
if (elements == null) {
return F.NIL;
}
for (int i = 0; i < elements.length; i++) {
obs.add(1.0, elements[i][0], elements[i][1]);
}
} else {
int rowSize = ast.arg1().isVector();
if (rowSize < 0) {
return F.NIL;
}
final double[] elements = Expr2Object.toDoubleVector((IAST) ast.arg1());
for (int i = 0; i < elements.length; i++) {
obs.add(1.0, i + 1, elements[i]);
}
}
return Convert.polynomialFunction2Expr(fitter.fit(obs.toList()), (ISymbol) ast.arg3());
}
return F.NIL;
}
Aggregations