/*
* 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.display.*;
import ket.display.box.Box;
import ket.display.box.BoxTools;
import ket.math.*;
import ket.math.purpose.SymbolicState;
public class SymbolicFunctionTensor extends SymbolicFunctionForm {
@Override
public String toLatex(Vector<Argument> args) {
switch (args.size()) {
case 0:
case 1:
return super.toLatex(args);
case 2:
return args.get(0).toLatex() + "_{ " + args.get(1).toLatex() + " }";
default:
String rest = "";
for (int i=1; i<args.size(); i++) {
rest += args.get(i).toLatex() + " ";
}
return args.get(0).toLatex() + " _ { " + rest + " }";
}
}
@Override
public String toHTML(Vector<Argument> args) {
switch (args.size()) {
case 0:
case 1:
return super.toHTML(args);
case 2:
return args.get(0).toHTML() + "<SUB>" + args.get(1).toHTML() + "</SUB>";
default:
String rest = "";
for (int i=1; i<args.size(); i++) {
rest += " " + args.get(i).toHTML() + " ";
}
return args.get(0).toHTML() + "<SUB>" + rest + "</SUB>";
}
}
@Override
public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
if (args.size()>1) {
// Create a new list of arguments that skips inverse functions and hash functions.
Vector<Argument> cleanedArgs = new Vector<Argument>();
boolean[] contravariant = new boolean[args.size()];
for (int i=0; i<args.size(); i++) {
Argument arg = args.get(i);
// Keep track of which arguments are contravariant indices (i.e. superscript).
Function function = arg.getFunction();
if (function==Function.REFERENCE || function==Function.TILDE) {
contravariant[i] = true;
Branch argumentBranch = (Branch) arg;
Argument child = argumentBranch.getChild(0);
cleanedArgs.add(child);
} else {
contravariant[i] = false;
cleanedArgs.add(arg);
}
}
Vector<Box> boxArgs = BoxTools.padArgs(cleanedArgs, getArgLowerLimit(args.size()), colourScheme);
Box box = BoxTools.centredHorizontalBoxList(argument, settings, boxArgs);
// Align arguments.
boxArgs.firstElement().setProperty(Box.RIGHT_ALIGN|Box.Y_CENTRE_ALIGN);
for (int i=1; i<args.size(); i++) {
boolean isContravariant = (i<contravariant.length) ? contravariant[i] : false;
if (isContravariant) {
// a^^b
boxArgs.get(i).setProperty(Box.LEFT_ALIGN|Box.TOP_ALIGN|Box.SMALL_FONT);
} else {
// a__b
boxArgs.get(i).setProperty(Box.LEFT_ALIGN|Box.BOTTOM_ALIGN|Box.SMALL_FONT);
}
}
return box;
} else {
return super.toBox(argument, settings, colourScheme, args);
}
}
}