Package ket.math

Source Code of ket.math.FunctionForm

/*
* 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;

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

import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxWord;
import ket.display.box.BoxTools;
import ket.math.purpose.function.*;
import ket.math.purpose.SymbolicState;
import ket.math.purpose.symbolicFunctions.*;


/**
* This class is responsible for displaying functions as HTML, LaTeX or box instances.
*/
public class FunctionForm implements Form {

  Function f;

  public FunctionForm() {
    f = null;
  }

  public FunctionForm(Function f) {
    this.f = f;
  }

  public void setFunction(Function f) {
    this.f = f;
  }

  public int getArgUpperLimit() { // <--- use individual symbolic function property.
    return f!=null ? f.getArgUpperLimit() : -1; // <--- why is a null test needed here but not for getInfix(), toBox() etc?
  }

  public int getArgLowerLimit(int actualNumberOfArguments) { // <--- use individual symbolic function property.
    return f!=null ? f.getArgLowerLimit(actualNumberOfArguments) : 0;
  }

  @Override
  public String toHTML(Vector<Argument> args) {
    String string = f.getName() + Symbol.OPEN_BRACKET.toHTML();
    if (args.size()>0) {
      string += (args.size()==1) ? args.get(0).toHTML() : Function.COMMA.toHTML(args);
    }
    return string + Symbol.CLOSE_BRACKET.toHTML();
  }

  @Override
  public String toLatex(Vector<Argument> args) {
    switch (args.size()) {
      case 0:
        return "\\mathrm{" + f.getName() + "} \\! \\left(\\right)";

      case 1:
        return "\\mathrm{" + f.getName() + "} \\! \\left(" + args.firstElement().toLatex() + "\\right)";

      default:
        Vector<String> latexArgs = new Vector<String>();
        for (Argument argument : args) {
          latexArgs.add(argument.toLatex());
        }
        return "\\mathrm{" + f.getName() + "} \\! "+ TextTools.operatorFactory("\\left(", ", ", "\\right)", latexArgs);
    }
  }

  @Override
  public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
    Vector<Box> boxArgs = BoxTools.padArgs(args, f.getArgLowerLimit(args.size()), colourScheme);
    return this.toBox(argument, boxArgs, settings, colourScheme);
  }

  @Override
  public Box toBox(Argument argument, Vector<Box> boxArgs, long settings, ColourScheme colourScheme) {
    BoxWord boxText = new BoxWord(argument, f.getName(), Box.PLAIN_FONT);
    if ((settings&Box.BOLD_FONT)==Box.BOLD_FONT) {
      boxText.setProperty(Box.BOLD_FONT);
    }
    BoxWord comma = new BoxWord(argument, ",", Box.PLAIN_FONT);
    switch (boxArgs.size()) {
      case 0:
        Box blank = new BoxWord(argument, "", 0L);
        Box brackets = BoxTools.roundBrackets(argument, blank, 0L, colourScheme);
        return BoxTools.centredHorizontalBoxList(argument, settings, boxText, brackets);

      case 1:
        Box brackettedArgument = BoxTools.roundBrackets(argument, boxArgs.firstElement(), 0L, colourScheme);
        Box singleArgumentFunction = BoxTools.centredHorizontalBoxList(argument, settings, boxText, brackettedArgument);
        boxText.setProperty(Box.RIGHT_ALIGN|Box.Y_CENTRE_ALIGN);
        brackettedArgument.setProperty(Box.LEFT_ALIGN|Box.Y_CENTRE_ALIGN);
        singleArgumentFunction.setProperty(Box.X_CENTRE_ALIGN|Box.Y_CENTRE_ALIGN);
        return singleArgumentFunction;

      default:
        Box separatedArgs = BoxTools.interpose(argument, comma, boxArgs);
        boxText.setProperty(Box.RIGHT_ALIGN|Box.Y_CENTRE_ALIGN);
        comma.setProperty(Box.LEFT_ALIGN|Box.Y_CENTRE_ALIGN);
        Box brackettedArgs = BoxTools.roundBrackets(argument, separatedArgs, 0L, colourScheme);
        return BoxTools.centredHorizontalBoxList(argument, settings, boxText, brackettedArgs);
    }
  }

  @Override
  public String toString(Vector<Argument> args) {
    switch (args.size()) {
      case 0:
        return f.getName() + "()";
      case 1:
        return f.getName() + "(" + args.firstElement() + ")";
      default:
        Vector<String> textArgs = TextTools.toString(args);
        return f.getName() + TextTools.operatorFactory("(", ", ", ")", textArgs);
    }
  }

}
TOP

Related Classes of ket.math.FunctionForm

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.