/*
* 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.math.convert.Like;
import ket.display.box.BoxWord;
import ket.display.box.BoxTools;
import ket.math.purpose.NumberValue;
import ket.math.purpose.SymbolicState;
/**
* A specific mathematical operation that acts on arguments in a case-specific
* way.
*/
public class SymbolicFunctionTimes extends SymbolicFunctionForm {
@Override
public String toLatex(Vector<Argument> args) {
switch (args.size()) {
case 0:
return Symbol.CROSS.toLatex() + "()";
case 1:
return Symbol.CROSS.toLatex() + args.firstElement().toLatex();
default :
// Only explicitly display a multiplication sign when displaying no arguments,
// one argument or side-by-side number pairs.
if (Like.numberLikeList(args)) {
String latexString = "";
latexString += args.get(0).toLatex();
for (int i=1; i<args.size(); i++) {
latexString += Symbol.TIMES.toLatex() + " " + args.get(i).toLatex();
}
return latexString;
} else {
String latexString = ""; // Compress this repeated code into a single method (see Text...)
latexString += args.get(0).toLatex();
for (int i=1; i<args.size(); i++) {
latexString += " " + args.get(i).toLatex();
}
return latexString;
}
}
}
@Override
public String toHTML(Vector<Argument> args) {
//- return super.toHTML(args);
boolean explicit = Like.numberLikeList(args); // Show * symbols rather than spaces?
String operation = explicit ? Symbol.TIMES.toHTML() : " ";
switch (args.size()) {
case 0:
return getInfix().toHTML();
case 1:
return /*getInfix().toHTML() +*/ operation + args.firstElement().toHTML();
default:
String html = "";
for (int i=0; i<args.size()-1; i++) {
html += args.get(i).toHTML() + operation /*+ getInfix().toHTML() + operation*/;
}
html += args.lastElement().toHTML();
return html;
}
}
@Override
public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
long[] childFlags = new long[args.size()];
for (int i=0; i<args.size(); i++) {
Argument arg = args.get(i);
if (arg.getFunction()==Function.FRACTION) {
childFlags[i] = Box.SUPPRESS_BRACKETS;
}
}
Vector<Box> boxArgs = BoxTools.padArgs(args, getArgLowerLimit(args.size()), colourScheme, childFlags);
if (boxArgs.size()<2) {
return super.toBox(argument, settings, colourScheme, args);
}
// TODO: Rename boxTools to BoxFactory, and
// operatorFactory to something more specific.
// Only explicitly display a multiplication sign when displaying no arguments,
// one argument or side-by-side number pairs.
boolean explicit = Like.numberLikeList(args);
Box nameBox;
if (explicit) {
nameBox = new BoxWord(argument, Symbol.TIMES.toUnicode(), Box.PLAIN_FONT);
} else {
nameBox = new BoxWord(argument, " ", Box.PLAIN_FONT);
}
return BoxTools.operatorFactory(argument, null, nameBox, null, boxArgs, settings);
}
}