1 8 9 package org.lsmp.djepExamples; 10 import org.nfunk.jep.*; 11 import org.lsmp.djep.xjep.*; 12 import org.lsmp.djep.djep.*; 13 14 17 public class DiffExample { 18 19 public static void main(String args[]) 20 { 21 22 DJep j = new DJep(); 23 j.addStandardConstants(); 24 j.addStandardFunctions(); 25 j.addComplex(); 26 j.setAllowUndeclared(true); 27 j.setAllowAssignment(true); 28 j.setImplicitMul(true); 29 j.addStandardDiffRules(); 30 31 try 32 { 33 Node node = j.parse("sin(x^2)"); 35 Node diff = j.differentiate(node,"x"); 37 j.println(diff); 39 Node simp = j.simplify(diff); 41 j.println(simp); 43 44 Node node2 = j.parse("diff(cos(x^3),x)"); 47 Node processed = j.preprocess(node2); 50 j.println(processed); 51 Node simp2 = j.simplify(processed); 53 j.println(simp2); 54 55 Node node3 = j.parse("y=x^5"); 57 j.preprocess(node3); 58 Node node4 = j.parse("diff(y^2+x,x)"); 59 Node simp3 = j.simplify(j.preprocess(node4)); 60 61 j.println(simp3); 63 PrintVisitor pv = j.getPrintVisitor(); 64 pv.setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,false); 65 j.println(simp3); 67 pv.setMode(DPrintVisitor.PRINT_PARTIAL_EQNS,true); 68 pv.setMode(DPrintVisitor.PRINT_VARIABLE_EQNS,true); 69 j.println(simp3); 71 pv.setMode(DPrintVisitor.PRINT_VARIABLE_EQNS,false); 72 73 78 j.getSymbolTable().setVarValue("x",new Double (5)); 79 System.out.println(j.evaluate(simp3)); 80 j.evaluate(node3); 81 System.out.println(j.getSymbolTable().getVar("y").getValue()); 82 j.getSymbolTable().setVarValue("x",new Double (0)); 83 System.out.println(j.evaluate(simp)); 84 85 Node node10 = j.parse("x=3"); 86 Node node11 = j.preprocess(node10); 87 System.out.println(j.evaluate(node11)); 88 Node node12 = j.parse("y=x^2"); 89 Node node13 = j.preprocess(node12); 90 System.out.println(j.evaluate(node13)); 91 Node node14 = j.parse("z=diff(y,x)"); 92 Node node15 = j.simplify(j.preprocess(node14)); 93 System.out.println(j.evaluate(node15)); 94 95 j.setVarValue("x",new Double (4)); 99 System.out.println(j.evaluate(node13)); 100 System.out.println(j.evaluate(node15)); 101 System.out.println("z: "+j.getVarValue("z").toString()); 102 103 107 j.setVarValue("x",new Double (5)); 108 System.out.println("j.setVarValue(\"x\",new Double(5));"); 109 System.out.println("j.findVarValue(y): "+j.calcVarValue("y").toString()); 110 System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString()); 111 112 j.getSymbolTable().clearValues(); 119 j.setVarValue("x",new Double (6)); 120 System.out.println("j.setVarValue(\"x\",new Double(6));"); 121 System.out.println("j.findVarValue(z): "+j.calcVarValue("z").toString()); 122 123 j.getSymbolTable().clearValues(); 124 j.setVarValue("x",new Double (7)); 125 System.out.println(j.evaluate(node15)); 126 System.out.println("z: "+j.getVarValue("z").toString()); 127 } 128 catch(ParseException e) 129 { 130 System.out.println("Error with parsing"); 131 } 132 catch(Exception e) 133 { 134 System.out.println("Error with evaluation"); 135 } 136 } 137 } 138 | Popular Tags |