MathTest.java

Go to the documentation of this file.
00001 
00004 package com.graphbuilder.test;
00005 
00006 import com.graphbuilder.math.Expression;
00007 import com.graphbuilder.math.ExpressionTree;
00008 import com.graphbuilder.math.FuncMap;
00009 import com.graphbuilder.math.VarMap;
00010 
00011 public class MathTest {
00012 
00013     public static void main(String[] args) {
00014         String s = "&pi*r^2";
00015         Expression x = ExpressionTree.parse(s);
00016 
00017         VarMap vm = new VarMap(false /* case sensitive */);
00018         vm.setValue("&pi", Math.PI);
00019         vm.setValue("r", 5);
00020 
00021         FuncMap fm = null; // no functions in expression
00022 
00023         System.out.println(x); // (pi*(r^2))
00024         System.out.println(x.eval(vm, fm)); // 78.53981633974483
00025 
00026         vm.setValue("r", 10);
00027         System.out.println(x.eval(vm, fm)); // 314.1592653589793
00028         
00029         x = ExpressionTree.parse("4==1");
00030         final double r = x.eval(vm, fm);
00031         System.out.println("4 == 1 --> " + r);
00032 
00033         x = ExpressionTree.parse("4==4");
00034         System.out.println("4 == 4 --> " + x.eval(vm, fm));
00035 
00036         x = ExpressionTree.parse("r==5");
00037         System.out.println("r<-10; r == 5 --> " + x.eval(vm, fm));
00038 
00039         x = ExpressionTree.parse("r==10");
00040         System.out.println("r<-10; r == 10 --> " + x.eval(vm, fm));
00041         
00042         fm = new FuncMap();
00043         fm.loadDefaultFunctions();
00044         
00045         x = ExpressionTree.parse("if(r==10,10.0,-10.0)");
00046         System.out.println("if(r==10,10.0,-10.0) --> " + x.eval(vm, fm));
00047 
00048         x = ExpressionTree.parse("if(r==1,10.0,-10.0)");
00049         System.out.println("if(r==1,10.0,-10.0) --> " + x.eval(vm, fm));
00050     }
00051 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1