use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class ParserTestCase method testParser24.
public void testParser24() {
try {
Parser p = new Parser();
ASTNode obj = p.parse("x'[t] == 10*(y[t] - x[t])");
assertEquals(obj.toString(), "Equal(Derivative(1)[x][t], Times(10, Plus(y(t), Times(-1, x(t)))))");
} catch (Exception e) {
e.printStackTrace();
assertEquals("", e.getMessage());
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class ParserTestCase method testParser12.
public void testParser12() {
try {
Parser p = new Parser();
ASTNode obj = p.parse("-(Pi/4)");
assertEquals(obj.toString(), "Times(-1, Times(1/4, Pi))");
} catch (Exception e) {
e.printStackTrace();
assertEquals("", e.getMessage());
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class LowercaseTestCase method testParserFixedPoint.
public void testParserFixedPoint() {
try {
Parser p = new Parser(true);
ASTNode obj = p.parse("{28, 21} /. {a_, b_} /; b != 0 -> {b, Mod(a, b)}");
assertEquals(obj.toString(), "ReplaceAll(List(28, 21), Rule(Condition(List(a_, b_), Unequal(b, 0)), List(b, Mod(a, b))))");
} catch (Exception e) {
e.printStackTrace();
assertEquals("", e.getMessage());
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class MathUtils method tangentLine.
// (y-y0)=m(x-x0)
public static String tangentLine(String f, String x, String v) throws MathException {
// Variable var = new Variable(v);
// Parser parser = new Parser(Parser.STANDARD_FUNCTIONS |
// Parser.OPTIONAL_PARENS | Parser.OPTIONAL_STARS |
// Parser.OPTIONAL_SPACES
// | Parser.BRACES | Parser.BRACKETS | Parser.BOOLEANS);
// parser.add(var);
// setUpParser(parser);
// Expression fun;
ASTNode fun;
String var = v;
EvalDouble parser = new EvalDouble(true);
parser.defineVariable(var);
try {
fun = parser.parse(f);
} catch (MathException e) {
// + e.getMessage(), e.context);
throw e;
}
try {
parser.defineVariable(var, parser.evaluateNode(parser.parse(x)));
try {
double m = parser.evaluateNode(parser.derivative(fun, var));
String out;
if (m == 1) {
out = var;
} else if (m == -1) {
out = "-" + var;
} else if (m == 0) {
out = "";
} else {
out = m + "*" + var;
}
double b = parser.evaluateNode(fun) - m * parser.evaluate(var);
if (b > 0) {
if (!out.equals("")) {
out += "+" + b;
} else {
out += b;
}
} else if (b < 0) {
b *= -1;
out += "-" + b;
}
if (out.equals("")) {
out = "0";
}
return out;
} catch (MathException e) {
// e.context);
throw e;
}
} catch (MathException e) {
// e.context);
throw e;
}
}
use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.
the class MathUtils method integrate.
/**
* TODO use LegendreGauss method
*
* @param fun
* @param v1
* @param v2
* @param bounds
* @return
*/
public static double integrate(String fun, String v1, String v2, String[] bounds) {
double x1, x2;
// Expression y1, y2, integFun;
// Variable integVarX = new Variable(v1);
// Variable integVarY = new Variable(v2);
// Parser integParser = new Parser(Parser.STANDARD_FUNCTIONS |
// Parser.OPTIONAL_PARENS | Parser.OPTIONAL_STARS
// | Parser.OPTIONAL_SPACES | Parser.BRACES | Parser.BRACKETS |
// Parser.BOOLEANS);
// integParser.add(integVarX);
// integParser.add(integVarY);
// setUpParser(integParser);
ASTNode y1, y2, integFun;
String integVarX = v1;
String integVarY = v2;
EvalDouble integParser = new EvalDouble(true);
integParser.defineVariable(integVarX);
integParser.defineVariable(integVarY);
try {
x1 = integParser.evaluate(bounds[0]);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
try {
// x2 = integParser.parse(bounds[1]).getVal();
x2 = integParser.evaluate(bounds[1]);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
try {
y1 = integParser.parse(bounds[2]);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
try {
y2 = integParser.parse(bounds[3]);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
try {
integFun = integParser.parse(fun);
} catch (MathException e) {
// e.getMessage(), e.context);
throw e;
}
int romI = 9;
Double[][] romX = new Double[romI][romI];
for (int i = 0; i < romI; i++) {
double step = (x2 - x1) / Math.pow(2, i);
double aTemp = x1;
rom = new Double[romI][romI];
// integVarX.setVal(aTemp);
integParser.defineVariable(integVarX, aTemp);
double a = integParser.evaluateNode(y1);
double b = integParser.evaluateNode(y2);
for (int j = 0; j < romI; j++) {
double stepY = (b - a) / Math.pow(2, j);
double aTempY = a;
// integVarY.setVal(a);
// double valueY = integFun.getVal() / 2;
integParser.defineVariable(integVarY, a);
double valueY = integParser.evaluateNode(integFun) / 2;
for (int k = 0; k < Math.pow(2, j) - 1; k++) {
aTempY += stepY;
// integVarY.setVal(aTempY);
// valueY += integFun.getVal();
integParser.defineVariable(integVarY, aTempY);
valueY += integParser.evaluateNode(integFun);
}
// integVarY.setVal(b);
// valueY += integFun.getVal() / 2;
integParser.defineVariable(integVarY, b);
valueY += integParser.evaluateNode(integFun) / 2;
valueY *= stepY;
rom[j][0] = Double.valueOf(valueY);
}
double valueX = romberg(romI - 1, romI - 1) / 2;
for (int l = 0; l < Math.pow(2, i) - 1; l++) {
rom = new Double[romI][romI];
aTemp += step;
// integVarX.setVal(aTemp);
// a = y1.getVal();
// b = y2.getVal();
integParser.defineVariable(integVarX, aTemp);
a = integParser.evaluateNode(y1);
b = integParser.evaluateNode(y2);
for (int j = 0; j < romI; j++) {
double stepY = (b - a) / Math.pow(2, j);
double aTempY = a;
// integVarY.setVal(a);
// double valueY = integFun.getVal() / 2;
integParser.defineVariable(integVarY, a);
double valueY = integParser.evaluateNode(integFun) / 2;
for (int k = 0; k < Math.pow(2, j) - 1; k++) {
aTempY += stepY;
// integVarY.setVal(aTempY);
// valueY += integFun.getVal();
integParser.defineVariable(integVarY, aTempY);
valueY += integParser.evaluateNode(integFun);
}
// integVarY.setVal(b);
// valueY += integFun.getVal() / 2;
integParser.defineVariable(integVarY, b);
valueY += integParser.evaluateNode(integFun) / 2;
valueY *= stepY;
rom[j][0] = Double.valueOf(valueY);
}
valueX += romberg(romI - 1, romI - 1);
}
// integVarX.setVal(x2);
// a = y1.getVal();
// b = y2.getVal();
integParser.defineVariable(integVarX, x2);
a = integParser.evaluateNode(y1);
b = integParser.evaluateNode(y2);
rom = new Double[romI][romI];
for (int j = 0; j < romI; j++) {
double stepY = (b - a) / Math.pow(2, j);
double aTempY = a;
// integVarY.setVal(a);
// double valueY = integFun.getVal() / 2;
integParser.defineVariable(integVarY, a);
double valueY = integParser.evaluateNode(integFun) / 2;
for (int k = 0; k < Math.pow(2, j) - 1; k++) {
aTempY += stepY;
// integVarY.setVal(aTempY);
// valueY += integFun.getVal();
integParser.defineVariable(integVarY, aTempY);
valueY += integParser.evaluateNode(integFun);
}
// integVarY.setVal(b);
// valueY += integFun.getVal() / 2;
integParser.defineVariable(integVarY, b);
valueY += integParser.evaluateNode(integFun) / 2;
valueY *= stepY;
rom[j][0] = Double.valueOf(valueY);
}
valueX += romberg(romI - 1, romI - 1) / 2;
valueX *= step;
romX[i][0] = Double.valueOf(valueX);
}
rom = romX;
return romberg(romI - 1, romI - 1);
}
Aggregations