Package ket.math.purpose.symbolicFunctions

Source Code of ket.math.purpose.symbolicFunctions.SymbolicFunctionDerivative

/*
* Copyright (C) 2011  Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

package ket.math.purpose.symbolicFunctions;

import java.awt.*;
import java.util.*;

import ket.math.*;
import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxFactory;
import ket.display.box.BoxGraphic;
import ket.display.box.BoxTools;
import ket.display.box.BoxWord;
import ket.math.purpose.IntegerValue;
import ket.math.purpose.IntegerValue;
import ket.math.purpose.SymbolicState;

/**
* A specific mathematical operation that acts on arguments in a case-specific
* way.
*/
public class SymbolicFunctionDerivative extends SymbolicFunctionForm {

  @Override
  public String toLatex(Vector<Argument> args) {
    int lowestDerivativeIndex = (args.size()<2) ? 0 : 1;
    Argument orderArgument = ArgumentFactory.getOrderArgument(lowestDerivativeIndex, args);
    String orderLatex = orderArgument!=null ? "^"+orderArgument.toLatex()+" " : " ";
    switch (args.size()) {
      case 0:
        return "\\frac{\\mathrm{d}}{\\mathrm{d}}";
      case 1:
        return "\\frac{\\mathrm{d}"+orderLatex+"}{\\mathrm{d}"+args.get(0).toLatex()+"}";
      case 2:
        return "\\frac{\\mathrm{d}"+orderLatex+args.get(0).toLatex()+"}{\\mathrm{d}"+args.get(1).toLatex()+"}";
      default:
        String numerator = "\\mathrm{d}"+orderLatex+args.get(0).toLatex();
        String denomonator = "\\mathrm{d}"+args.get(1).toLatex();
        for (int i=2; i<args.size(); i++) {
          denomonator += "\\, \\mathrm{d}" + args.get(i).toLatex();
        }
        return "\\frac{"+numerator+"}{"+denomonator+"}";
    }
  }

  @Override
  public String toHTML(Vector<Argument> args) {
    int lowestDerivativeIndex = (args.size()<2) ? 0 : 1;
    Argument orderArgument = ArgumentFactory.getOrderArgument(lowestDerivativeIndex, args);
    String orderHTML = orderArgument!=null ? "<SUP>"+orderArgument.toHTML()+"</SUP>" : "";
    String numerator, denomonator;
    switch (args.size()) { // TODO: When are brackets required d(sin(x)) via dx?
      case 0:
        return HTMLTools.toFraction("d", "d");
      case 1:
        numerator = "d"+orderHTML;
        denomonator = "d"+args.firstElement().toHTML();
        return HTMLTools.toFraction(numerator, denomonator);
      case 2:
        numerator = "d"+orderHTML+args.firstElement().toHTML();
        denomonator = "d"+ args.lastElement().toHTML();
        return HTMLTools.toFraction(numerator, denomonator);
      default:
        numerator = "d"+ orderHTML + args.firstElement().toHTML();
        denomonator = "d" + args.get(1).toHTML();
        for (int i=2; i<=args.size()-1; i++) {
          denomonator += "d"+ args.get(i).toHTML();
        }
        return HTMLTools.toFraction(numerator, denomonator);
    }
  }

  @Override
  public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
    if (args.size()>0) {
      BoxWord dBox = new BoxWord(null, "d", 0L);
      return BoxFactory.derivative(argument, dBox, getArgUpperLimit(), args, settings, colourScheme);
    } else {
      return super.toBox(argument, settings, colourScheme, args);
    }
  }
}
TOP

Related Classes of ket.math.purpose.symbolicFunctions.SymbolicFunctionDerivative

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.