KickJava   Java API By Example, From Geeks To Geeks.

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


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.djep.DiffRulesI;
13 import org.lsmp.djep.xjep.*;
14
15 /**
16    * Diffrentiates a product with respect to var.
17    * diff(y*z,x) -> diff(y,x)*z+y*diff(z,x)
18    */

19   public class MultiplyDiffRule implements DiffRulesI
20   {
21     private String JavaDoc name;
22     //private DifferentationVisitor dv;
23

24     private MultiplyDiffRule() {}
25     public MultiplyDiffRule(String JavaDoc inName)
26     {
27       //dv = inDv;
28
name = inName;
29     }
30     Operator mulOp=null;
31     public MultiplyDiffRule(String JavaDoc inName,Operator op)
32     {
33       //dv = inDv;
34
name = inName;
35       mulOp = op;
36     }
37
38     public String JavaDoc toString()
39     { return name + " \t\tdiff(f*g,x) -> diff(f,x)*g+f*diff(g,x)"; }
40     public String JavaDoc getName() { return name; }
41     
42     public Node differentiate(ASTFunNode node,String JavaDoc var,Node [] children,Node [] dchildren,DJep djep) throws ParseException
43     {
44         OperatorSet opset = djep.getOperatorSet();
45         NodeFactory nf = djep.getNodeFactory();
46         Operator op = opset.getMultiply();
47         if(mulOp!=null) op = mulOp;
48         
49       int nchild = node.jjtGetNumChildren();
50       if(nchild==2)
51           return nf.buildOperatorNode(opset.getAdd(),
52             nf.buildOperatorNode(op,
53               dchildren[0],
54               djep.deepCopy(children[1])),
55             nf.buildOperatorNode(op,
56               djep.deepCopy(children[0]),
57               dchildren[1]));
58       else
59           throw new ParseException("Too many children "+nchild+" for "+node+"\n");
60     }
61   } /* end MultiplyDiffRule */
62
Popular Tags