View Javadoc

1   package fr.ove.openmath.jome.model;
2   
3   import java.util.*;
4   import fr.ove.openmath.jome.model.*;
5   import fr.ove.openmath.jome.model.events.ModelEvent;
6   
7   /***
8   * The differentiation operator.<BR>
9   * The first child corresponds to the function. The next child(ren) to the bound variable(s).
10  *
11  * @author © 2000 DIRAT Laurent
12  * @version 2.1 10/01/2000
13  */
14  public class Differentiation extends Function {
15      private boolean gotoVariable = true;
16      
17      /***
18      * <CODE>true</CODE> if the instance represents a partial differentiation.
19      * <CODE>false</CODE> otherwise.
20      */
21      private boolean isPartial = false;
22      
23      /***
24      * The order of the differentiation
25      */
26      private int order = 1;
27      
28      /***
29      * Returns <CODE>true</CODE> if the instance represents a partial differentiation.
30      * <CODE>false</CODE> otherwise.
31      */
32      public boolean isPartial() {
33          return isPartial;
34      }
35      
36      /***
37      * Sets if the instance repressents a partial differentiation
38      * @param isPartial <CODE>true</CODE> if the instance represents a partial differentiation.
39      * <CODE>false</CODE> otherwise.
40      */
41      public void setIsPartial(boolean isPartial) {
42          this.isPartial = isPartial;
43      }
44  
45      /*** 
46      * Inserts the instance in the formula tree structure.<BR>
47      * @param current the position in the formula tree where the operator is to be insert.
48      * @return the new current position int hte formula tree.
49      */
50      public FormulaTreeStructure insert(FormulaTreeStructure current) {
51          current = super.insert(current);
52          
53          if (isPartial) {
54              Constant delta = new Constant();
55              delta.setResourceIdentifier("delta");
56              delta.setValue("delta");
57              current = delta.insert(current);        
58          }
59          else
60              current = (new Variable("d")).insert(current);
61          
62          current = super.addElement();
63          super.addElement();
64          
65          return current;
66      }
67      
68      /***
69      * Computes the order of the differentiation.
70      */
71      public void computeOrder() {
72          Hashtable ht = new Hashtable();
73          FormulaTreeStructure fts, degree;
74          String varName;
75          
76          for (int i = 2; i < getNbChildren(); i++) {
77              fts = (FormulaTreeStructure) getChild(i).getChild(0);
78              if (!fts.isOperator()) {
79                  // donc on dit que c'est une variable
80                  varName = ((VariableOrNumber) fts).getValue();
81                  degree = (FormulaTreeStructure) ht.get(varName);
82                  if (degree == null) {
83                      degree = new Addition();
84                      degree.addChild(new AnInteger("1"));
85                      ht.put(varName, degree);
86                  }
87                  else
88                      degree.addChild(new AnInteger("1"));
89              }
90              else {
91                  // donc on dit que c'est une ^
92                  varName = ((VariableOrNumber) fts.getChild(0)).getValue();
93                  degree = (FormulaTreeStructure) ht.get(varName);
94                  if (degree == null) {
95                      degree = new Addition();
96                      degree.addChild((FormulaTreeStructure) fts.getChild(1).clone());
97                      ht.put(varName, degree);
98                  }
99                  else 
100                     degree.addChild((FormulaTreeStructure) fts.getChild(1).clone());
101             }
102         }
103         
104         
105         if (ht.size() > 1) {
106             isPartial = true;
107             fts = (FormulaTreeStructure) getChild(0);
108             fts.removeChild(0);
109             
110             Constant delta = new Constant();
111             delta.setResourceIdentifier("delta");
112             delta.setValue("delta");
113             fts.addChild(delta);
114         }
115         
116         fts = new Addition();
117         for (Enumeration e = ht.elements(); e.hasMoreElements(); )
118             fts.addChild((FormulaTreeStructure) e.nextElement());
119             
120         String order_s = fts.evaluate();
121         
122         try {
123             order = Integer.parseInt(order_s);
124         }
125         catch(NumberFormatException nfe) {
126             order = 0;
127         }
128         
129         if (!order_s.equals("1")) {
130             fts = (FormulaTreeStructure) getChild(0).getChild(0);
131             fts = (new Superscript()).insert(fts);
132             fts = (new Variable(order_s)).insert(fts);
133         }
134     }
135     
136     /***
137     * Returns the order of the differentiation.
138     * @return an positive number if the order is a numeric evaluation, 0 otherwise.
139     */
140     public int getOrder() {
141         return order;
142     }
143     
144     /***
145     * Adds a new element (template) to the end of the list.
146     * Returns the new element.
147     */
148     public FormulaTreeStructure addElement() {
149         if (gotoVariable) {
150             gotoVariable = false;
151             return (FormulaTreeStructure) getChild(2).getChild(0);
152         }
153         else
154             return super.addElement();
155     }
156     
157     /***
158     * The Creation of the corresponding linear expression of the formula.
159     */
160     public String createLinear(String linear) {
161         FormulaTreeStructure child;
162         // mÍme chose que super.createLinear(String linear), sauf que l'indice de boucle part de 1,
163         // au lieu de 0. (le premier fils correspondant ? l'ordre de la dČrivČe, qui n'intervient pas dans la
164         // syntaxe)
165         linear += getTheOperator();
166         for (int i = 1; i < getNbChildren(); i++) {
167             child = (FormulaTreeStructure) getChild(i);
168             if (i == 1)
169                 linear += child.createLinear(linear);
170             else {
171                 linear += "," + child.createLinear(linear);
172             }
173         }
174         return linear + getEnding();
175     }
176 }