KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > djep > diffRules > ChainRuleDiffRules


1 /* @author rich
2  * Created on 04-Jul-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

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 /**
19  * Common methods used when the rules are specified by node trees or strings.
20  * All subclasses use the chain rule to differentiate.
21  * df(g(x),h(x))/dx -> df/dg * dg/dx + df/dh * dh/dx
22  * Variable names MUST be x,y for 1 to 2 variables or
23  * x1,x2,x3 for 3 for more variables.
24  */

25 public abstract class ChainRuleDiffRules implements DiffRulesI
26 {
27     protected String JavaDoc name;
28     protected PostfixMathCommandI pfmc;
29     protected Node rules[]=null;
30 // protected DifferentationVisitor dv;
31
// protected OperatorSet opSet;
32
// protected NodeFactory nf;
33
/** Cannot construct outside the context of a differentation visitor. */
34     public ChainRuleDiffRules() {}
35     
36     /** returns the name of the function */
37     public String JavaDoc getName() { return name; }
38     /** returns the PostfixMathCommandI for the function. */
39     public PostfixMathCommandI getPfmc() { return pfmc; }
40     /** Returns the number of rules which should be number of arguments of function */
41     public int getNumRules() { return rules.length; }
42     /** returns the i-th rule as an expression tree. */
43     public Node getRule(int i) { return rules[i]; }
44     
45     /**
46      * Use the chain rule to differentiate.
47      * df(g(x),h(x))/dx -> df/dg * dg/dx + df/dh * dh/dx
48      */

49     public Node differentiate(ASTFunNode node,String JavaDoc 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             // df(g(x))/dx -> f'(g(x)) * g'(x)
62
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             // df(g(x),h(x))/dx -> df/dg * dg/dx + df/dh * dh/dx
71
Node df_dg = djep.deepCopy(rules[0]);
72             Node df_dh = djep.deepCopy(rules[1]);
73             //Node replacements[] = new Node[]{children[0],children[1]};
74
Node gprime = dchildren[0];
75             Node hprime = dchildren[1];
76             df_dg = djep.substitute(df_dg,new String JavaDoc[]{"x","y"},children);
77             df_dh = djep.substitute(df_dh,new String JavaDoc[]{"x","y"},children);
78     // df_dg = dv.djep.substitute(df_dg,"@2",h);
79
// df_dh = dv.djep.substitute(df_dh,"@1",g);
80
// df_dh = dv.djep.substitute(df_dh,"@2",h);
81

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 JavaDoc names[]=new String JavaDoc[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 JavaDoc toString()
110     {
111         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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                 //sb.append(dv.djep.toString(getRule(i)));
120
//TODO print the rule.
121
}
122         return sb.toString();
123     }
124     /**
125      * Changes the names of variables in the rules to make the chain rule easier.
126      * Substitutes the variable x,y with @1,@2 and x1,x2,x3,... with @1,@2,@3,...
127      * @throws ParseException
128      */

129 /* protected void fixVarNames() throws ParseException
130     {
131         NameChangeVisitor nc = new NameChangeVisitor(dv.djep);
132         int nrules = this.getNumRules();
133         if(nrules==0) return;
134         else if(nrules == 1)
135             rules[0] = nc.changeNames(rules[0],new String[]{"x"},new String[]{"@1"});
136         else if(nrules == 2)
137         {
138             rules[0] = nc.changeNames(rules[0],new String []{"x","y"},new String []{"@1","@2"});
139             rules[1] = nc.changeNames(rules[1],new String []{"x","y"},new String []{"@1","@2"});
140         }
141         else
142         {
143             String[] oldvars = new String[nrules];
144             String[] newvars = new String[nrules];
145             for(int i=0;i<nrules;++i)
146             {
147                 oldvars[i] = "x" + String.valueOf(i);
148                 newvars[i] = "@" + String.valueOf(i);
149             }
150             rules[0] = nc.changeNames(rules[0],oldvars,newvars);
151         }
152      }*/

153 } /* end ChainRuleDiffRules */
154
Popular Tags