KickJava   Java API By Example, From Geeks To Geeks.

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


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.xjep.*;
12 import org.nfunk.jep.*;
13
14 /**
15    * If your really lazy, you don't even neeed to workout the derivatives
16    * of a function defined by a macro yourself.
17    * This class will automatically calculate the rules for you.
18    */

19   public class MacroFunctionDiffRules extends ChainRuleDiffRules
20   {
21     /**
22      * Calculates the rules for the given function.
23      */

24       public MacroFunctionDiffRules(DJep djep,MacroFunction fun) throws ParseException
25       {
26           //super(dv);
27
name = fun.getName();
28           pfmc = fun;
29           
30         XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); //new SymbolTable();
31
localSymTab.copyConstants(djep.getSymbolTable());
32         DJep localJep = (DJep) djep.newInstance(localSymTab);
33
34           int nargs = fun.getNumberOfParameters();
35           rules = new Node[nargs];
36           if(nargs == 1)
37               rules[0] = localJep.differentiate((Node) fun.getTopNode(),"x");
38           else if(nargs == 2)
39           {
40               rules[0] = localJep.differentiate(fun.getTopNode(),"x");
41               rules[1] = localJep.differentiate(fun.getTopNode(),"y");
42           }
43           else
44           {
45               for(int i=0;i<nargs;++i)
46                   rules[i] = localJep.differentiate(fun.getTopNode(),"x"+ String.valueOf(i));
47           }
48           for(int i=0;i<nargs;++i)
49                 rules[i] = localJep.simplify(rules[i]);
50           //fixVarNames();
51
}
52   }
53
Popular Tags