1 8 package org.lsmp.djep.djep.diffRules; 9 10 import org.lsmp.djep.djep.DJep; 11 import org.lsmp.djep.djep.DiffRulesI; 12 import org.lsmp.djep.xjep.*; 13 import org.nfunk.jep.ASTFunNode; 14 import org.nfunk.jep.*; 15 import org.nfunk.jep.function.PostfixMathCommandI; 16 17 18 25 public abstract class ChainRuleDiffRules implements DiffRulesI 26 { 27 protected String name; 28 protected PostfixMathCommandI pfmc; 29 protected Node rules[]=null; 30 34 public ChainRuleDiffRules() {} 35 36 37 public String getName() { return name; } 38 39 public PostfixMathCommandI getPfmc() { return pfmc; } 40 41 public int getNumRules() { return rules.length; } 42 43 public Node getRule(int i) { return rules[i]; } 44 45 49 public Node differentiate(ASTFunNode node,String var,Node [] children,Node [] dchildren,DJep djep) throws ParseException 50 { 51 XOperatorSet opSet= (XOperatorSet) djep.getOperatorSet(); 52 NodeFactory nf=djep.getNodeFactory(); 53 FunctionTable funTab = djep.getFunctionTable(); 54 55 int nRules = rules.length; 56 if(nRules != children.length) 57 throw new ParseException("Error differentiating "+name+" number of rules "+nRules+" != number of arguments "+children.length); 58 59 if(nRules ==1) 60 { 61 Node fprime = djep.deepCopy(rules[0]); 63 Node g = children[0]; 64 Node fprimesub = djep.substitute(fprime,"x",g); 65 Node gprime = dchildren[0]; 66 return nf.buildOperatorNode(opSet.getMultiply(),fprimesub,gprime); 67 } 68 else if(nRules == 2) 69 { 70 Node df_dg = djep.deepCopy(rules[0]); 72 Node df_dh = djep.deepCopy(rules[1]); 73 Node gprime = dchildren[0]; 75 Node hprime = dchildren[1]; 76 df_dg = djep.substitute(df_dg,new String []{"x","y"},children); 77 df_dh = djep.substitute(df_dh,new String []{"x","y"},children); 78 82 return nf.buildOperatorNode(opSet.getAdd(), 83 nf.buildOperatorNode(opSet.getMultiply(),df_dg,gprime), 84 nf.buildOperatorNode(opSet.getMultiply(),df_dh,hprime)); 85 } 86 else if(nRules < 1) 87 { 88 throw new ParseException("Error differentiating "+name+" zero differention rules!"); 89 } 90 else 91 { 92 String names[]=new String [nRules]; 93 Node[] df_dg = new Node[nRules]; 94 Node[] products = new Node[nRules]; 95 for(int i=0;i<nRules;++i) 96 { 97 df_dg[i]=djep.deepCopy(rules[i]); 98 names[i]="x"+i; 99 } 100 for(int i=0;i<nRules;++i) 101 { 102 df_dg[i] = djep.substitute(df_dg[i],names,children); 103 products[i] = nf.buildOperatorNode(opSet.getMultiply(),df_dg[i],dchildren[i]); 104 } 105 return nf.buildFunctionNode("sum",funTab.get("sum"),products); 106 } 107 } 108 109 public String toString() 110 { 111 StringBuffer sb = new StringBuffer (); 112 sb.append(name + " \t"); 113 if(rules==null) 114 sb.append("no diff rules possible parse error?"); 115 else 116 for(int i=0;i<getNumRules();++i) 117 { 118 sb.append("\t"); 119 } 122 return sb.toString(); 123 } 124 129 153 } 154 | Popular Tags |