/*
* 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.function;
import java.awt.*;
import java.util.*;
import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxWord;
import ket.display.box.BoxList;
import ket.display.box.BoxTools;
import ket.math.*;
import ket.math.purpose.NumberValue;
import ket.math.purpose.SymbolicState;
import ket.math.convert.Like;
import ket.display.LatexTools;
/**
* A specific mathematical operation that acts on arguments in a case-specific
* way.
*/
public class FunctionMatrix extends FunctionForm {
@Override
public String toLatex(Vector<Argument> args) {
if (args.size()<2) {
return super.toLatex(args);
}
Argument[][] table = asTable(args);
if (table==null) {
table = new Argument[1][args.size()];
for (int i=0; i<args.size(); i++) {
table[0][i] = args.get(i);
}
}
return LatexTools.tableToLatex(table, "(", ")");
}
@Override
public String toHTML(Vector<Argument> args) {
Argument[][] table = asTable(args);
if (table==null) { // (1,2,3,4)
table = asRow(args);
}
if (table!=null) { // TODO: Brackets?
return HTMLTools.toHTMLMatrix(table);
} else {
return super.toHTML(args);
}
}
private Argument[][] asRow(Vector<Argument> args) {
if (args.size()==0) return null;
Argument[][] column = new Argument[1][args.size()];
for (int i=0; i<args.size(); i++) {
column[0][i] = args.get(i);
}
return column;
}
private Argument[][] asTable(Vector<Argument> args) {
return ArgumentTools.asTable(args, true);
}
@Override
public Box toBox(Argument argument, long settings, ColourScheme colourScheme, Vector<Argument> args) {
if (args.size()==0) {
//- return super.toBox(argument, settings, colourScheme, args);
BoxWord gap = new BoxWord(null, "", Box.X_CENTRE_ALIGN|Box.Y_CENTRE_ALIGN);
//- Box arg = new BoxWord(Argument.PAD_ARG, "?", 0L);
return BoxTools.roundBrackets(argument, gap, settings, colourScheme);
}
Argument[][] transpose = asTable(args);
if (transpose!=null) {
Box[][] boxTable = BoxTools.toBoxTable(transpose, colourScheme);
Box[][] grid = BoxTools.padBoxTable(boxTable);
Box matrixWithoutBrackets = BoxTools.gridToBox(argument, grid);
matrixWithoutBrackets.setArgument(argument);
return BoxTools.roundBrackets(argument, matrixWithoutBrackets, settings, colourScheme);
} else {
// Default to displaying args as a single row.
BoxWord gap = new BoxWord(null, " ", Box.X_CENTRE_ALIGN|Box.Y_CENTRE_ALIGN);
Vector<Box> boxArgs = BoxTools.padArgs(args, getArgLowerLimit(args.size()), colourScheme);
Box singleRow;
if (boxArgs.size()>1) {
singleRow = BoxTools.interpose(argument, gap, boxArgs);
singleRow.setArgument(argument);
} else {
singleRow = boxArgs.firstElement();
}
return BoxTools.roundBrackets(argument, singleRow, settings, colourScheme);
/*!
Vector<Box> boxArgs = BoxTools.padArgs(args, getArgLowerLimit(args.size()), colourScheme);
Box box = BoxTools.centredVerticalBoxList(argument, 0L, boxArgs);
return BoxTools.squareBrackets(box, settings, colourScheme);
*/
}
}
}