Back to index

wims  3.65+svn20090927
Differentiation.java
Go to the documentation of this file.
00001 /*
00002 $Id: Differentiation.java,v 1.3 2003/02/18 11:48:47 sander Exp $
00003 */
00004 
00005 
00006 /*
00007 Copyright (C) 2001-2002 Mainline Project (I3S - ESSI - CNRS -UNSA)
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU Lesser General Public
00011 License as published by the Free Software Foundation; either
00012 version 2.1 of the License, or (at your option) any later version.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public
00020 License along with this library; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 For further information on the GNU Lesser General Public License,
00024 see: http://www.gnu.org/copyleft/lesser.html
00025 For further information on this library, contact: mainline@essi.fr
00026 */
00027 
00028 
00029 package fr.ove.openmath.jome.model;
00030 
00031 import java.util.*;
00032 import fr.ove.openmath.jome.model.*;
00033 import fr.ove.openmath.jome.model.events.ModelEvent;
00034 
00042 public class Differentiation extends Function {
00043     private boolean gotoVariable = true;
00044     
00049     private boolean isPartial = false;
00050     
00054     private int order = 1;
00055     
00060     public boolean isPartial() {
00061         return isPartial;
00062     }
00063     
00069     public void setIsPartial(boolean isPartial) {
00070         this.isPartial = isPartial;
00071     }
00072 
00078     public FormulaTreeStructure insert(FormulaTreeStructure current) {
00079         current = super.insert(current);
00080         
00081         if (isPartial) {
00082             Constant delta = new Constant();
00083             delta.setResourceIdentifier("delta");
00084             delta.setValue("delta");
00085             current = delta.insert(current);        
00086         }
00087         else
00088             current = (new Variable("d")).insert(current);
00089         
00090         current = super.addElement();
00091         super.addElement();
00092         
00093         return current;
00094     }
00095     
00099     public void computeOrder() {
00100         Hashtable ht = new Hashtable();
00101         FormulaTreeStructure fts, degree;
00102         String varName;
00103         
00104         for (int i = 2; i < getNbChildren(); i++) {
00105             fts = (FormulaTreeStructure) getChild(i).getChild(0);
00106             if (!fts.isOperator()) {
00107                 // donc on dit que c'est une variable
00108                 varName = ((VariableOrNumber) fts).getValue();
00109                 degree = (FormulaTreeStructure) ht.get(varName);
00110                 if (degree == null) {
00111                     degree = new Addition();
00112                     degree.addChild(new AnInteger("1"));
00113                     ht.put(varName, degree);
00114                 }
00115                 else
00116                     degree.addChild(new AnInteger("1"));
00117             }
00118             else {
00119                 // donc on dit que c'est une ^
00120                 varName = ((VariableOrNumber) fts.getChild(0)).getValue();
00121                 degree = (FormulaTreeStructure) ht.get(varName);
00122                 if (degree == null) {
00123                     degree = new Addition();
00124                     degree.addChild((FormulaTreeStructure) fts.getChild(1).clone());
00125                     ht.put(varName, degree);
00126                 }
00127                 else 
00128                     degree.addChild((FormulaTreeStructure) fts.getChild(1).clone());
00129             }
00130         }
00131         
00132         
00133         if (ht.size() > 1) {
00134             isPartial = true;
00135             fts = (FormulaTreeStructure) getChild(0);
00136             fts.removeChild(0);
00137             
00138             Constant delta = new Constant();
00139             delta.setResourceIdentifier("delta");
00140             delta.setValue("delta");
00141             fts.addChild(delta);
00142         }
00143         
00144         fts = new Addition();
00145         for (Enumeration e = ht.elements(); e.hasMoreElements(); )
00146             fts.addChild((FormulaTreeStructure) e.nextElement());
00147             
00148         String order_s = fts.evaluate();
00149         
00150         try {
00151             order = Integer.parseInt(order_s);
00152         }
00153         catch(NumberFormatException nfe) {
00154             order = 0;
00155         }
00156         
00157         if (!order_s.equals("1")) {
00158             fts = (FormulaTreeStructure) getChild(0).getChild(0);
00159             fts = (new Superscript()).insert(fts);
00160             fts = (new Variable(order_s)).insert(fts);
00161         }
00162     }
00163     
00168     public int getOrder() {
00169         return order;
00170     }
00171     
00176     public FormulaTreeStructure addElement() {
00177         if (gotoVariable) {
00178             gotoVariable = false;
00179             return (FormulaTreeStructure) getChild(2).getChild(0);
00180         }
00181         else
00182             return super.addElement();
00183     }
00184     
00188     public String createLinear(String linear) {
00189         FormulaTreeStructure child;
00190         // même chose que super.createLinear(String linear), sauf que l'indice de boucle part de 1,
00191         // au lieu de 0. (le premier fils correspondant à l'ordre de la dérivée, qui n'intervient pas dans la
00192         // syntaxe)
00193         linear += getTheOperator();
00194         for (int i = 1; i < getNbChildren(); i++) {
00195             child = (FormulaTreeStructure) getChild(i);
00196             if (i == 1)
00197                 linear += child.createLinear(linear);
00198             else {
00199                 linear += "," + child.createLinear(linear);
00200             }
00201         }
00202         return linear + getEnding();
00203     }
00204 }