* Try to rewrite the specified node.
* @param parentNode The node to attempt rewrite.
* @return The rewritten node, or original node, if no rewrite could happen.
*/
private ProgramNode tryNodeRewrite(ProgramNode parentNode) {
ProgramNode result = null;
if (parentNode.isLeaf()) {
return null;
}
if (parentNode.allConstDescendants()) {
ExpressionValue v = parentNode.evaluate();
double ck = v.toFloatValue();
// do not rewrite if it produces a div by 0 or other bad result.
if( Double.isNaN(ck) || Double.isInfinite(ck) ) {
return result;
}
result = parentNode
.getOwner()
.getContext()
.getFunctions()
.factorProgramNode("#const", parentNode.getOwner(),
new ProgramNode[] {});
// is it an integer?
if( Math.abs( ck- ((int)ck))<Encog.DEFAULT_DOUBLE_EQUAL) {
result.getData()[0] = new ExpressionValue((int)ck);
} else {
result.getData()[0] = v;
}
}
return result;
}