📄 parsefunction.java
字号:
} return token; } /* ** Starting at the passed node evaluate everything below this node */ private double evaluate(Node node) throws Exception { double value = 0.0; if( node == null) { throw new Exception("evaluate: Failed because of null node!"); } switch (node.type) { case Node.GROUP: value = evaluate(node.left); break; case Node.OP: value = evaluateOp(node); break; case Node.INTRINSIC: value = evaluateIntrinsic(node); break; case Node.VALUE: value = node.value; break; case Node.INDEPENDENT: if(node.op == X) value = x; else if(node.op == Y) value = y; else if(node.op == Z) value = z; break; default: throw new Exception("evaluate: Unknown type!"); } return value; } /* ** Parsed node is an Operator node. Evaluate both the left and right ** nodes and then excecute the operator on the values. */ private double evaluateOp(Node node) throws Exception { double value = 0.0; switch (node.op) { case ADD: if(node.left != null) value = evaluate(node.left); value += evaluate(node.right); break; case SUBTRACT: if(node.left != null) value = evaluate(node.left); value -= evaluate(node.right); break; case DIVIDE: value = evaluate(node.left); value /= evaluate(node.right); break; case MULTIPLY: value = evaluate(node.left); value *= evaluate(node.right); break; case POWER: value = Math.pow(evaluate(node.left), evaluate(node.right)); break; default: throw new Exception( "evaluate: Failed because of Unknown operator!"); } return value; } /* ** Parsed node is an instrinsic function. Evaluate the parameters ** then call the intrinsic and return the result */ private double evaluateIntrinsic(Node node) throws Exception { double value = 0.0; switch (node.op) { case SIN: value = Math.sin(evaluate(node.left)); break; case COS: value = Math.cos(evaluate(node.left)); break; case TAN: value = Math.tan(evaluate(node.left)); break; case ASIN: value = Math.asin(evaluate(node.left)); break; case ACOS: value = Math.acos(evaluate(node.left)); break; case ATAN: value = Math.atan(evaluate(node.left)); break; case LOG: value = Math.log(evaluate(node.left)); break; case SQRT: value = Math.sqrt(evaluate(node.left)); break; case LOG10: value = Math.log(evaluate(node.left))/ Math.E; case EXP: value = Math.exp(evaluate(node.left)); break; case J0: value = SpecialFunction.j0( evaluate(node.left)); break; case J1: value = SpecialFunction.j1( evaluate(node.left)); break; case Y0: value = SpecialFunction.y0( evaluate(node.left)); break; case Y1: value = SpecialFunction.y1( evaluate(node.left)); break; case FAC: value = SpecialFunction.fac( evaluate(node.left)); break; case GAMMA: value = SpecialFunction.gamma( evaluate(node.left)); break; case SINH: value = SpecialFunction.sinh( evaluate(node.left)); break; case COSH: value = SpecialFunction.cosh( evaluate(node.left)); break; case TANH: value = SpecialFunction.tanh( evaluate(node.left)); break; case ASINH: value = SpecialFunction.asinh( evaluate(node.left)); break; case ACOSH: value = SpecialFunction.acosh( evaluate(node.left)); break; case ATANH: value = SpecialFunction.atanh( evaluate(node.left)); break; case ERF: value = SpecialFunction.erf( evaluate(node.left)); break; case ERFC: value = SpecialFunction.erfc( evaluate(node.left)); break; case NORMAL: value = SpecialFunction.normal( evaluate(node.left)); break; case POISSON: value = SpecialFunction.poisson( (int)(evaluate(node.left)+0.01), evaluate(node.right)); break; case POISSONC: value = SpecialFunction.poissonc( (int)(evaluate(node.left)+0.01), evaluate(node.right)); break; case CHISQ: value = SpecialFunction.chisq( evaluate(node.left), evaluate(node.right)); break; case CHISQC: value = SpecialFunction.chisqc( evaluate(node.left), evaluate(node.right)); break; case IGAM: value = SpecialFunction.igam( evaluate(node.left), evaluate(node.right)); break; case IGAMC: value = SpecialFunction.igamc( evaluate(node.left), evaluate(node.right)); break; case ATAN2: value = Math.atan2(evaluate(node.left), evaluate(node.right)); break; case JN: value = SpecialFunction.jn( (int)(evaluate(node.left)+0.01), evaluate(node.right)); break; case YN: value = SpecialFunction.yn( (int)(evaluate(node.left)+0.01), evaluate(node.right)); break; default: throw new Exception( "evaluate: Failed because of an unknown intrinsic!"); } return value; } /* ** The basic parsing mechanism failes to recognise precedence. ** this method reorganises the nodes into ascending precedence. ** That is nodes with higher precedence are pushed further down the tree ** this means they get evaluated first. ** ** Precedence ** Grouping ** Unary minus/plus ** Power ** Multiplication/Division ** Addition/Subtraction ** Variables, Intrinsic Functions, Constants */ private void reOrderNodes(Node node) { Node right = null; Node left = null; if(node == null) return; right = node.right; left = node.left; if(right != null && right.type == Node.GROUP) { reOrderNodes(right); } else if(left != null && left.type == Node.GROUP) { reOrderNodes(left); } if( node.type == Node.GROUP ) { reOrderNodes(left); } else if( node.type == Node.OP && right.type == Node.OP ) { if(node.precedence >= right.precedence) { Node newnode = new Node(node); newnode.right = right.left; node.replace(right); node.left = newnode; right = null; reOrderNodes(node); } } }}class Node extends Object { public static final int OP = 0; public static final int VALUE = 1; public static final int INTRINSIC = 2; public static final int NULL = 3; public static final int INDEPENDENT = 4; public static final int GROUP = 5; public static final int PARAMETER = 6; public static final int P0 = 0; public static final int P1 = 1; public static final int P2 = 2; public static final int P3 = 3; public static final int P4 = 4; public static final int P5 = 5; int type; Node left; Node right; int op; double value; int precedence; public Node() { type = NULL; left = null; right = null; op = NULL; value = 0.0; precedence = P0; } public Node(Node n) { replace(n); } public void replace(Node n ) { if(n == null) return; op = n.op; type = n.type; left = n.left; right = n.right; value = n.value; precedence = n.precedence; } public void indent(int ind) { for (int i = 0; i < ind; i++) System.out.print(" "); } public void print(int indentLevel) { char l[] = new char[1]; indent(indentLevel); System.out.println("NODE type=" + type); indent(indentLevel); System.out.println(" prec="+ precedence); indent(indentLevel); switch (type) { case Node.VALUE: System.out.println(" value=" + value); break; case Node.INDEPENDENT: System.out.println(" variable=" + op); break; default: System.out.println(" op=" + op); if(left != null) left.print(indentLevel + 5); if(right != null) right.print(indentLevel + 5); break; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -