KickJava   Java API By Example, From Geeks To Geeks.

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


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.nfunk.jep.*;
11 import org.lsmp.djep.djep.DJep;
12 import org.lsmp.djep.xjep.*;
13 import org.nfunk.jep.function.PostfixMathCommandI;
14
15
16 /**
17    * Rules are specfied by a set of strings or trees of nodes.
18    * The standard chain rule is applied
19    * <pre>diff(f(g(x),h(x)),x) -> df/dg dg/dx + df/dh dh/dx</pre>
20    * for example
21    * <pre>
22    * DifferentiationVisitor dv = new DifferentiationVisitor(new TreeUtils(jep));
23    * DiffRulesI rule = new MacroDiffRules(dv,"sin","cos(x)");
24    * </pre>
25    **/

26   public class MacroDiffRules extends ChainRuleDiffRules
27   {
28     /**
29      * Create a differention rule for function with 1 argument
30      * @param inName name of function
31      * @param node a tree represention differation of function wrt "x"
32      * @throws ParseException
33      */

34     
35     public MacroDiffRules(DJep djep,String JavaDoc inName,Node node) throws ParseException
36     {
37         name = inName;
38         pfmc = djep.getFunctionTable().get(inName);
39         if(pfmc!=null)
40         {
41             int nParam = pfmc.getNumberOfParameters();
42             if(nParam != 1)
43                 throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
44         }
45         rules = new Node[1];
46         rules[0] = node;
47         //fixVarNames();
48
}
49     
50     /**
51      * Create a differention rule for function with 1 argument
52      * @param inName name of function
53      * @param rule a string represention differation of a function wrt "x"
54      * @throws ParseException
55      */

56     public MacroDiffRules(DJep djep,String JavaDoc inName,String JavaDoc rule) throws ParseException
57     {
58         this(djep,inName,djep.getFunctionTable().get(inName),rule);
59     }
60
61     /**
62      * Create a differention rule for function with 1 argument
63      * @param inName name of function
64      * @param inPfmc PostfixMathCommandI for function
65      * @param rule a string represention differation of function wrt "x"
66      * @throws ParseException
67      */

68     public MacroDiffRules(DJep djep,String JavaDoc inName,PostfixMathCommandI inPfmc,String JavaDoc rule) throws ParseException
69     {
70         //super(dv);
71
name = inName;
72         pfmc = inPfmc;
73         if(pfmc!=null)
74         {
75             int nParam = pfmc.getNumberOfParameters();
76             if(nParam != 1)
77                 throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
78         }
79         XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); //new SymbolTable();
80
localSymTab.copyConstants(djep.getSymbolTable());
81         XJep localJep = djep.newInstance(localSymTab);
82         Node node = localJep.parse(rule);
83         rules = new Node[1];
84         rules[0] = node;
85         //fixVarNames();
86
}
87
88     /**
89      * Create a differention rule for function with 2 arguments.
90      * The rules must be in terms of "x" and "y"
91      * @param inName name of function
92      * @param inPfmc PostfixMathCommandI for function
93      * @param rule1 a string represention differation of function wrt "x"
94      * @param rule2 a string represention differation of function wrt "y"
95      * @throws ParseException
96      */

97     public MacroDiffRules(DJep djep,String JavaDoc inName,PostfixMathCommandI inPfmc,String JavaDoc rule1,String JavaDoc rule2) throws ParseException
98     {
99         //super(dv);
100
name = inName;
101         pfmc = inPfmc;
102         if(pfmc!=null)
103         {
104             int nParam = pfmc.getNumberOfParameters();
105             if(nParam != 2)
106             throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
107         }
108         XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); //new SymbolTable();
109
localSymTab.copyConstants(djep.getSymbolTable());
110         XJep localJep = djep.newInstance(localSymTab);
111         Node node1 = localJep.parse(rule1);
112         Node node2 = localJep.parse(rule2);
113         rules = new Node[2];
114         rules[0] = node1;
115         rules[1] = node2;
116         //fixVarNames();
117
}
118     
119     /**
120      * Create a differention rule for function with 2 arguments.
121      * The rules must be in terms of "x" and "y"
122      * @param inName name of function
123      * @param rule1 a string represention differation of function wrt "x"
124      * @param rule2 a string represention differation of function wrt "y"
125      * @throws ParseException
126      */

127     public MacroDiffRules(DJep djep,String JavaDoc inName,String JavaDoc rule1,String JavaDoc rule2) throws ParseException
128     {
129         this(djep,inName,djep.getFunctionTable().get(inName),rule1,rule2);
130     }
131
132     /**
133      * Create a differention rule for function with 2 arguments.
134      * The rules must be in terms of "x" and "y"
135      * @param inName name of function
136      * @param inPfmc PostfixMathCommandI for function
137      * @param node1 a expression tree represention differation of function wrt "x"
138      * @param node2 a expression tree represention differation of function wrt "y"
139      * @throws ParseException
140      */

141 /* public MacroDiffRules(DJep djep,String inName,PostfixMathCommandI inPfmc,Node node1,Node node2) throws ParseException
142     {
143       //super(dv);
144         name = inName;
145         pfmc = inPfmc;
146         if(pfmc!=null)
147         {
148             int nParam = pfmc.getNumberOfParameters();
149             if(nParam != 2)
150             throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
151         }
152         rules = new Node[2];
153         rules[0] = node1;
154         rules[1] = node2;
155         //fixVarNames();
156     }
157 */

158     /**
159      * Create a differention rule for function with 2 arguments.
160      * The rules must be in terms of "x" and "y"
161      * @param inName name of function
162      * @param node1 a expression tree represention differation of function wrt "x"
163      * @param node2 a expression tree represention differation of function wrt "y"
164      * @throws ParseException
165      */

166 /* public MacroDiffRules(DJep djep,String inName,Node node1,Node node2) throws ParseException
167     {
168         this(djep,inName,djep.getFunctionTable().get(inName),node1,node2);
169     }
170 */
/**
171      * Create a differention rule for function with n arguments.
172      * The rules must be in terms of "x1", "x2", ... "xn"
173      * @param inName name of function
174      * @param inPfmc PostfixMathCommandI for function
175      * @param inRule an array of strings represention differation of function wrt "x1",...
176      * @throws ParseException
177      */

178     public MacroDiffRules(DJep djep,String JavaDoc inName,PostfixMathCommandI inPfmc,String JavaDoc[] inRules) throws ParseException
179     {
180         //super(dv);
181
name = inName;
182         pfmc = inPfmc;
183         if(pfmc!=null)
184         {
185             int nParam = pfmc.getNumberOfParameters();
186             if(nParam != inRules.length)
187             throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
188         }
189         
190         XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); //new SymbolTable();
191
localSymTab.copyConstants(djep.getSymbolTable());
192         XJep localJep = djep.newInstance(localSymTab);
193
194         rules = new Node[inRules.length];
195         for(int i=0;i<inRules.length;++i)
196         {
197             rules[i] = localJep.parse(inRules[i]);
198         }
199         //fixVarNames();
200
}
201
202     /**
203      * Create a differention rule for function with n arguments.
204      * The rules must be in terms of "x1", "x2", ... "xn"
205      * @param inName name of function
206      * @param inPfmc PostfixMathCommandI for function
207      * @param inRule an array of strings represention differation of function wrt "x1",...
208      * @throws ParseException
209      */

210     public MacroDiffRules(DJep djep,String JavaDoc inName,String JavaDoc[] inRules) throws ParseException
211     {
212         this(djep,inName,djep.getFunctionTable().get(inName),inRules);
213     }
214     /**
215      * Create a differention rule for function with n arguments.
216      * The rules must be in terms of "x1", "x2", ... "xn"
217      * @param inName name of function
218      * @param inPfmc PostfixMathCommandI for function
219      * @param inRule an array of expression trees represention differation of function wrt "x1",...
220      * @throws ParseException
221      */

222 /* public MacroDiffRules(DJep djep,String inName,PostfixMathCommandI inPfmc,Node[] inRules) throws ParseException
223     {
224         //super(dv);
225         name = inName;
226         pfmc = inPfmc;
227         if(pfmc!=null)
228         {
229             int nParam = pfmc.getNumberOfParameters();
230             if(nParam != inRules.length)
231                 throw new ParseException("Number of rules must match number of parameters for "+inName+" which is "+nParam);
232         }
233         rules = inRules;
234         //fixVarNames();
235     }
236 */

237     /**
238      * Create a differention rule for function with n arguments.
239      * The rules must be in terms of "x1", "x2", ... "xn"
240      * @param inName name of function
241      * @param inRules an array of expression trees represention differation of function wrt "x1",...
242      * @throws ParseException
243      */

244 /* public MacroDiffRules(DJep djep,String inName,Node[] inRules) throws ParseException
245     {
246         this(djep,inName,djep.getFunctionTable().get(inName),inRules);
247     }
248 */

249   } /* end MacroDiffRules */
250
Popular Tags