use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class JavaScriptFormFactory method convertAST.
@Override
public void convertAST(final StringBuilder buf, final IAST function) {
if (function.isNumericFunction(true)) {
try {
double value = EvalEngine.get().evalDouble(function);
buf.append("(" + value + ")");
return;
} catch (RuntimeException rex) {
//
}
}
IExpr head = function.head();
if (head.isSymbol()) {
String str = functionHead((ISymbol) head);
if (str != null) {
if (function.isASTSizeGE(S.Round, 3)) {
throw new ArgumentTypeException("Cannot convert to JavaScript: " + function.toString());
}
if (function.isAST(S.ArcTan, 3)) {
// swap arguments for atan2() convention in JavaScript
IASTMutable arcTan2 = function.copy();
arcTan2.set(1, function.arg2());
arcTan2.set(2, function.arg1());
buf.append("Math.atan2");
convertArgs(buf, head, arcTan2);
return;
}
buf.append(str);
convertArgs(buf, head, function);
return;
}
if (javascriptFlavor == USE_MATHCELL && function.headID() < 0) {
if (Config.FUZZY_PARSER) {
throw new ArgumentTypeException("Cannot convert to JavaScript. Function head: " + function.head());
}
// avoid generating JavaScript eval(head) here
buf.append("(window[");
convertInternal(buf, head);
buf.append("](");
convertArgs(buf, head, function);
buf.append("))");
return;
}
}
if (function.isList()) {
// interpret List() as javascript array
buf.append("[");
for (int i = 1; i < function.size(); i++) {
convertInternal(buf, function.get(i));
if (i < function.size() - 1) {
buf.append(",");
}
}
buf.append("]");
return;
}
if (function.isAST(S.Defer, 2) || function.isAST(S.Evaluate, 2) || function.isAST(S.Hold, 2) || function.isUnevaluated()) {
convertInternal(buf, function.first());
return;
}
if (javascriptFlavor == USE_MATHCELL) {
if (function.isPlus() || function.isTimes()) {
if (function.size() >= 3) {
for (int i = 1; i < function.size() - 1; i++) {
if (function.isPlus()) {
buf.append("add(");
} else {
buf.append("mul(");
}
}
convertInternal(buf, function.arg1());
buf.append(",");
for (int i = 2; i < function.size(); i++) {
convertInternal(buf, function.get(i));
buf.append(")");
if (i < function.size() - 1) {
buf.append(",");
}
}
return;
}
} else if (function.isPower()) {
convertPowerMathcell(buf, function);
return;
} else if (function.isInfinity()) {
buf.append("Number.POSITIVE_INFINITY");
return;
} else if (function.isNegativeInfinity()) {
buf.append("Number.NEGATIVE_INFINITY");
return;
} else if (function.head() == S.Log) {
if (function.isAST1()) {
IExpr arg1 = function.first();
buf.append("log(");
convertInternal(buf, arg1);
buf.append(", Math.E)");
return;
} else if (function.isAST2()) {
IExpr arg1 = function.first();
IExpr arg2 = function.second();
buf.append("log(");
convertInternal(buf, arg1);
buf.append(", ");
convertInternal(buf, arg2);
buf.append(")");
return;
}
} else if (function.head() == S.Piecewise && function.size() > 1) {
int[] dim = function.isPiecewise();
if (dim != null && convertPiecewise(dim, function, buf)) {
return;
}
} else if (function.head() == S.ConditionalExpression && function.size() == 3) {
convertConditionalExpression(function, buf);
return;
} else if (function.head() == S.HeavisideTheta && function.size() >= 2) {
convertHeavisideTheta(function, buf);
return;
}
IAST piecewiseExpand = Arithmetic.piecewiseExpand(function, S.Reals);
int[] dim = piecewiseExpand.isPiecewise();
if (dim != null && convertPiecewise(dim, piecewiseExpand, buf)) {
return;
}
} else {
if (function.isPower()) {
convertPower(buf, function);
return;
} else if (function.isInfinity()) {
buf.append("Number.POSITIVE_INFINITY");
return;
} else if (function.isNegativeInfinity()) {
buf.append("Number.NEGATIVE_INFINITY");
return;
} else if (function.head() == S.Piecewise && function.size() > 1) {
int[] dim = function.isPiecewise();
if (dim != null && convertPiecewise(dim, function, buf)) {
return;
}
} else if (function.head() == S.ConditionalExpression && function.size() == 3) {
convertConditionalExpression(function, buf);
return;
} else if (function.head() == S.Cot && function.size() == 2) {
buf.append("(1/Math.tan(");
convertInternal(buf, function.arg1());
buf.append("))");
return;
} else if (function.head() == S.ArcCot && function.size() == 2) {
buf.append("((Math.PI/2.0)-Math.atan(");
convertInternal(buf, function.arg1());
buf.append("))");
return;
}
IAST piecewiseExpand = Arithmetic.piecewiseExpand(function, S.Reals);
int[] dim = piecewiseExpand.isPiecewise();
if (dim != null && convertPiecewise(dim, piecewiseExpand, buf)) {
return;
}
}
if (function.head() == S.If && function.size() >= 3 && function.size() <= 4) {
// use the ternary operator
buf.append("((");
convertInternal(buf, function.arg1());
buf.append(") ? (");
convertInternal(buf, function.arg2());
buf.append(") : ( ");
if (function.size() == 4) {
convertInternal(buf, function.arg3());
} else {
buf.append("Number.NaN");
}
buf.append(" ))");
return;
}
if (function.isAST(S.Missing)) {
// Missing value
buf.append("Number.NaN");
return;
}
if (function.headID() > 0) {
throw new ArgumentTypeException("Cannot convert to JavaScript. Function head: " + function.head());
}
convertInternal(buf, head);
convertArgs(buf, head, function);
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class NumericArrayExpr method normalRecursive.
private static void normalRecursive(int[] intArray, boolean unsigned, IASTMutable list, int[] dimension, int position, int[] index) {
int size = dimension[position];
if (dimension.length - 1 == position) {
for (int i = 1; i <= size; i++) {
long value = //
unsigned ? //
Integer.toUnsignedLong(intArray[index[0]++]) : intArray[index[0]++];
list.set(i, F.ZZ(value));
}
return;
}
int size2 = dimension[position + 1];
for (int i = 1; i <= size; i++) {
IASTMutable currentList = F.astMutable(S.List, size2);
list.set(i, currentList);
normalRecursive(intArray, unsigned, currentList, dimension, position + 1, index);
}
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class NumericArrayExpr method normalRecursiveComplex.
private static void normalRecursiveComplex(float[] floatArray, IASTMutable list, int[] dimension, int position, int[] index) {
int size = dimension[position];
if (dimension.length - 1 == position) {
for (int i = 1; i <= size; i++) {
list.set(i, F.complexNum(floatArray[index[0]++], floatArray[index[0]++]));
}
return;
}
int size2 = dimension[position + 1];
for (int i = 1; i <= size; i++) {
IASTMutable currentList = F.astMutable(S.List, size2);
list.set(i, currentList);
normalRecursiveComplex(floatArray, currentList, dimension, position + 1, index);
}
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class NumericArrayExpr method normalRecursiveComplex.
private static void normalRecursiveComplex(double[] doubleArray, IASTMutable list, int[] dimension, int position, int[] index) {
int size = dimension[position];
if (dimension.length - 1 == position) {
for (int i = 1; i <= size; i++) {
list.set(i, F.complexNum(doubleArray[index[0]++], doubleArray[index[0]++]));
}
return;
}
int size2 = dimension[position + 1];
for (int i = 1; i <= size; i++) {
IASTMutable currentList = F.astMutable(S.List, size2);
list.set(i, currentList);
normalRecursiveComplex(doubleArray, currentList, dimension, position + 1, index);
}
}
use of org.matheclipse.core.interfaces.IASTMutable in project symja_android_library by axkr.
the class Integrate method integratePiecewise.
private static IExpr integratePiecewise(int[] dim, final IAST piecewiseFunction, final IAST integrateFunction) {
IAST list = (IAST) piecewiseFunction.arg1();
if (list.size() > 1) {
IASTAppendable pwResult = F.ListAlloc(list.size());
for (int i = 1; i < list.size(); i++) {
IASTMutable integrate = integrateFunction.copy();
integrate.set(1, list.get(i).first());
pwResult.append(F.list(integrate, list.get(i).second()));
}
IASTMutable piecewise = piecewiseFunction.copy();
piecewise.set(1, pwResult);
if (piecewiseFunction.size() > 2) {
IASTMutable integrate = integrateFunction.copy();
integrate.set(1, piecewiseFunction.arg2());
piecewise.set(2, integrate);
}
return piecewise;
}
return F.NIL;
}
Aggregations