Package ket.math

Source Code of ket.math.Symbol

/*
* 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 java.util.regex.*;

import ket.display.*;
import ket.display.box.Box;
import ket.display.box.BoxWord;
import ket.math.purpose.SymbolicState;
import ket.math.purpose.Word;

// TODO: Why is symbol a state?
public class Symbol extends State implements SymbolicState {
  public static final char PREFIX_CHAR = '\\';


  // Regular expression groups
  // TODO: These cannot be correctly parsed as ?.a etc is the current format.
  public static final Symbol RE_ARGUMENT = new Symbol("?a", "^\\?a");
  public static final Symbol RE_BRANCH = new Symbol("?b", "^\\?b");
  public static final Symbol RE_COMPOUND = new Symbol("?c", "^\\?c");
  public static final Symbol RE_FUNCTION = new Symbol("?f", "^\\?f");
  public static final Symbol RE_INTEGER = new Symbol("?i", "^\\?i");
  public static final Symbol RE_NUMBER = new Symbol("?n", "^\\?n");
  public static final Symbol RE_REAL = new Symbol("?r", "^\\?r");
  public static final Symbol RE_SYMBOL = new Symbol("?s", "^\\?s");
  public static final Symbol RE_TOKEN = new Symbol("?t", "^\\?t");
  public static final Symbol RE_WORD = new Symbol("?w", "^\\?w");
  public static final Symbol RE_VARIABLE = new Symbol("?v", "^\\?v");


  public static final Symbol TO = new Symbol(new String[]{"to", "->"}, "^\\-\\>", "&rarr;", "\\rightarrow", "\u2192");
  //- public static final Symbol THUS = new Symbol(new String[]{"thus", "=>"}, "^\\=\\>", "&rArr;", "\u21D2", "\u21D2");
  public static final Symbol THUS = new Symbol(new String[]{"thus", "=>"}, "^\\=\\>", "&rArr;", "\\Rightarrow", "\u21D2");


  // Constructor: new Symbol(String name, String regex, String html, String latex);
  public static final Symbol UNKNOWN = new Symbol(new String[]{"unknown", "?"}, "^\\?", "?", "?", "?");
  public static final Symbol HASH = new Symbol(new String[]{"hash", "#"}, "^\\#", "#", "\\#", "#");
  public static final Symbol TILDE = new Symbol(new String[]{"tilde", "~"}, "^\\~", "~", "~", "~");
  // when parsing, should you use its full name, the HTML name or the LaTeX name?
  public static final Symbol H_BAR = new Symbol("hbar", null, "&#295;", "\\hbar ", "\u0127", new String[]{"h/", "/h"});
  public static final Symbol DEGREE = new Symbol("degree", null, "&deg;", "^\\circ", "\u00B0", "'0");
  public static final Symbol INFINITY = new Symbol("infinity", null, "&infin;", "\\infty ", "\u221E", "00");
  public static final Symbol EMPTY = new Symbol("empty", null, "&empty;", "\\emptyset ", "\u2205", "/0");
  public static final Symbol PARTIAL = new Symbol("partial", null, "&part;", " \\partial ", "\u2202");
  public static final Symbol RADICAL = new Symbol("radical", null, "&radic;", " \\partial ", "\u221A");
  public static final Symbol INTEGRAL = new Symbol("integral", null, "&int;", " \\int ", "\u222B", "In");
  public static final Symbol CONTOUR = new Symbol("contour", null, "&#8750;", " \\cint ", "\u222E");
  public static final Symbol HIDDEN = new Symbol("hidden", "^\\.\\.\\.", "&hellip;", "\\ldots", "\u2026"); // check
  public static final Symbol DOUBLE_LEFT_ANGLE = new Symbol(new String[]{"double_left_angle", "<<"}, "^\\<\\<", "&lt;&lt;", "<<", "\u00AB");
  public static final Symbol DOUBLE_RIGHT_ANGLE = new Symbol(new String[]{"double_right_angle", ">>"}, "^\\>\\>", "&gt;&gt;", ">>", "\u00BB");

  public static final Symbol NABLA = new Symbol("nabla", null, "&nabla;", "\\nabla", "\u2207", "NB");
  public static final Symbol DOT = new Symbol("dot", null, "&bull;", "\\cdot", "\u2022");

  public static final Symbol OTIMES = new Symbol("otimes", null, "&#8855;", "\\otimes", "\u2297");


  public static final Symbol AND = new Symbol("and", null, "&and;", "\\wedge", "\u2227");
  public static final Symbol NAND = new Symbol("nand", null, "&#8892;", "\\barwedge", "\u22BC");
  public static final Symbol OR = new Symbol("or", null, "&or;", "\\vee", "\u22C1");
  public static final Symbol NOR = new Symbol("nor", null, "&#8893;", "\\overline \\vee", "\u22BD"); // FIX LATEX SYMBOL
  public static final Symbol XOR = new Symbol("xor", null, "&#8891;", "\\veebar", "\u22BB");
  public static final Symbol NOT = new Symbol("not", null, "&not;", "\\neg", "\u00AC");



  public static final Symbol CROSS = new Symbol("cross", null, "&#x2a2f;", "\\times", "\u00D7")// \u2a2f didn't work

  public static final Symbol PM = new Symbol("pm", null, "&plusmn;", " \\pm ", "\u00B1", "pm");
  public static final Symbol MP = new Symbol("mp", null, "&#8723;", " \\mp ", "\u2213", "mp");

  public static final Symbol alpha   = new Symbol("alpha",        null,   "&alpha;",   "\\alpha ", "\u03B1", new String[]{"a*", "*a"});
  public static final Symbol beta    = new Symbol("beta",         null,    "&beta;",    "\\beta ", "\u03B2", new String[]{"b*", "*b"});
  public static final Symbol gamma   = new Symbol("gamma",        null,   "&gamma;",   "\\gamma ", "\u03B3", new String[]{"g*", "*g"});
  public static final Symbol delta   = new Symbol("delta",        null,   "&delta;",   "\\delta ", "\u03B4", new String[]{"d*", "*d"});
  public static final Symbol epsilon = new Symbol("epsilon",      null, "&epsilon;", "\\epsilon ", "\u03B5", new String[]{"e*", "*e"});
  public static final Symbol zeta    = new Symbol("zeta",         null,    "&zeta;",    "\\zeta ", "\u03B6", new String[]{"z*", "*z"});
  public static final Symbol eta     = new Symbol("eta",          null,     "&eta;",     "\\eta ", "\u03B7", new String[]{"y*", "*y"});
  public static final Symbol theta   = new Symbol("theta",        null,   "&theta;",   "\\theta ", "\u03B8", new String[]{"h*", "*h"});
  public static final Symbol iota    = new Symbol("iota",         null,    "&iota;",    "\\iota ", "\u03B9", new String[]{"i*", "*i"});
  public static final Symbol kappa   = new Symbol("kappa",        null,   "&kappa;",   "\\kappa ", "\u03BA", new String[]{"k*", "*k"});
  public static final Symbol lambda  = new Symbol("lambda",       null,  "&lambda;""\\lambda ", "\u03BB", new String[]{"l*", "*l"});
  public static final Symbol mu      = new Symbol("mu",           null,      "&mu;",      "\\mu ", "\u03BC", new String[]{"m*", "*m"});
  public static final Symbol nu      = new Symbol("nu",           null,      "&nu;",      "\\nu ", "\u03BD", new String[]{"n*", "*n"});
  public static final Symbol xi      = new Symbol("xi",           null,      "&xi;",      "\\xi ", "\u03BE", new String[]{"c*", "*c"});
  public static final Symbol omicron = new Symbol("omicron",      null, "&omicron;", "\\omicron ", "\u03BF", new String[]{"o*", "*o"});
  public static final Symbol pi      = new Symbol("pi",           null,      "&pi;",      "\\pi ", "\u03C0", new String[]{"p*", "*p"});
  public static final Symbol rho     = new Symbol("rho",          null,     "&rho;",     "\\rho ", "\u03C1", new String[]{"r*", "*r"});
  public static final Symbol sigma   = new Symbol("sigma",        null,   "&sigma;",   "\\sigma ", "\u03C3", new String[]{"s*", "*s"});
  public static final Symbol tau     = new Symbol("tau",          null,     "&tau;",     "\\tau ", "\u03C4", new String[]{"t*", "*t"});
  public static final Symbol upsilon = new Symbol("upsilon",      null, "&upsilon;", "\\upsilon ", "\u03C5", new String[]{"u*", "*u"});
  public static final Symbol phi     = new Symbol("phi",          null,     "&phi;",     "\\phi ", "\u03C6", new String[]{"f*", "*f"});
  public static final Symbol chi     = new Symbol("chi",          null,     "&chi;",     "\\chi ", "\u03C7", new String[]{"x*", "*x"});
  public static final Symbol psi     = new Symbol("psi",          null,     "&psi;",     "\\psi ", "\u03C8", new String[]{"q*", "*q"});
  public static final Symbol omega   = new Symbol("omega",        null,   "&omega;",   "\\omega ", "\u03C9", new String[]{"w*", "*w"});

  public static final Symbol Alpha   = new Symbol("Alpha",        null,   "&Alpha;",   "\\Alpha ", "\u0391", new String[]{"A*", "*A"});
  public static final Symbol Beta    = new Symbol("Beta",         null,    "&Beta;",    "\\Beta ", "\u0392", new String[]{"B*", "*B"});
  public static final Symbol Gamma   = new Symbol("Gamma",        null,   "&Gamma;",   "\\Gamma ", "\u0393", new String[]{"G*", "*G"});
  public static final Symbol Delta   = new Symbol("Delta",        null,   "&Delta;",   "\\Delta ", "\u0394", new String[]{"D*", "*D"});
  public static final Symbol Epsilon = new Symbol("Epsilon",      null, "&Epsilon;", "\\Epsilon ", "\u0395", new String[]{"E*", "*E"});
  public static final Symbol Zeta    = new Symbol("Zeta",         null,    "&Zeta;",    "\\Zeta ", "\u0396", new String[]{"Z*", "*Z"});
  public static final Symbol Eta     = new Symbol("Eta",          null,     "&Eta;",     "\\Eta ", "\u0397", new String[]{"Y*", "*Y"});
  public static final Symbol Theta   = new Symbol("Theta",        null,   "&Theta;",   "\\Theta ", "\u0398", new String[]{"H*", "*H"});
  public static final Symbol Iota    = new Symbol("Iota",         null,    "&Iota;",    "\\Iota ", "\u0399", new String[]{"I*", "*I"});
  public static final Symbol Kappa   = new Symbol("Kappa",        null,   "&Kappa;",   "\\Kappa ", "\u039A", new String[]{"K*", "*K"});
  public static final Symbol Lambda  = new Symbol("Lambda",       null,  "&Lambda;""\\Lambda ", "\u039B", new String[]{"L*", "*L"});
  public static final Symbol Mu      = new Symbol("Mu",           null,      "&Mu;",      "\\Mu ", "\u039C", new String[]{"M*", "*M"});
  public static final Symbol Nu      = new Symbol("Nu",           null,      "&Nu;",      "\\Nu ", "\u039D", new String[]{"N*", "*N"});
  public static final Symbol Xi      = new Symbol("Xi",           null,      "&Xi;",      "\\Xi ", "\u039E", new String[]{"X*", "*X"});
  public static final Symbol Omicron = new Symbol("Omicron",      null, "&Omicron;", "\\Omicron ", "\u039F", new String[]{"O*", "*O"});
  public static final Symbol Pi      = new Symbol("Pi",           null,      "&Pi;",      "\\Pi ", "\u03A0", new String[]{"P*", "*P"});
  public static final Symbol Rho     = new Symbol("Rho",          null,     "&Rho;",     "\\Rho ", "\u03A1", new String[]{"R*", "*R"});
  public static final Symbol Sigma   = new Symbol("Sigma",        null,   "&Sigma;",   "\\Sigma ", "\u03A3", new String[]{"S*", "*S"});
  public static final Symbol Tau     = new Symbol("Tau",          null,     "&Tau;",     "\\Tau ", "\u03A4", new String[]{"T*", "*T"});
  public static final Symbol Upsilon = new Symbol("Upsilon",      null, "&Upsilon;", "\\Upsilon ", "\u03A5", new String[]{"U*", "*U"});
  public static final Symbol Phi     = new Symbol("Phi",          null,     "&Phi;",     "\\Phi ", "\u03A6", new String[]{"F*", "*F"});
  public static final Symbol Chi     = new Symbol("Chi",          null,     "&Chi;",     "\\Chi ", "\u03A7", new String[]{"X*", "*X"});
  public static final Symbol Psi     = new Symbol("Psi",          null,     "&Psi;",     "\\Psi ", "\u03A8", new String[]{"Q*", "*Q"});
  public static final Symbol Omega   = new Symbol("Omega",        null,   "&Omega;",   "\\Omega ", "\u03A9", new String[]{"W*", "*W"});


  public static final Symbol PIPE = new Symbol(new String[]{"pipe", "|"}, "^(\\|)[^\\|]");
  public static final Symbol DOUBLE_PIPE = new Symbol(new String[]{"double_pipe", "||"}, "^\\|\\|", "||", "\\|", "\u2016");

  public static final Symbol BANG = new Symbol(new String[]{"bang", "!"}, "^(!)[^\\=]", "&#33;", "!", "!");
  public static final Symbol DOLLAR = new Symbol(new String[]{"dollar", "$"}, "^\\$");
  public static final Symbol STOP = new Symbol(new String[]{"stop", "."}, "^\\.");
  public static final Symbol CARET = new Symbol(new String[]{"caret", "^"}, "^(\\^)[^\\^]"); // Read this very carefully.
  public static final Symbol DOUBLE_CARET = new Symbol(new String[]{"double_caret", "^^"}, "^\\^\\^");
  public static final Symbol UNDERSCORE = new Symbol(new String[]{"underscore", "_"}, "^(_)[^_]");
  public static final Symbol DOUBLE_UNDERSCORE = new Symbol(new String[]{"double_underscore", "__"}, "^\\_\\_");
  public static final Symbol EQUALS = new Symbol(new String[]{"equals", "="}, "^(\\=)[^\\=\\>]");
  //? public static final Symbol APPROX = new Symbol(new String[]{"approx", "~="}, "^(\\=)[^\\=\\>]"); // TODO: Carefully think about the meaning and effect of ~=; could it denote inverses instead?
  public static final Symbol APPROX = new Symbol("approx", null, "&asymp;", " \\approx ", "\u2248");
  public static final Symbol EQUIVALENT = new Symbol(new String[]{"equiv", "=="}, "^(\\=\\=)", "&equiv;", "\\equiv", "\u2261");
  public static final Symbol NOT_EQUALS = new Symbol(new String[]{"not_equals", "!="}, "^\\!\\=", "&ne;", "\\ne", "\u2260");

  public static final Symbol LESS_THAN = new Symbol(new String[]{"less_than", "<"}, "^(\\<)[^\\=]", "&lt;", "<", "<");
  public static final Symbol LESS_EQUALS = new Symbol(new String[]{"less_equals", "<="}, "^\\<\\=", "&le;", "\\le", "\u2264");
  public static final Symbol GREATER_THAN = new Symbol(new String[]{"greater_than", ">"}, "^(\\>)[^\\=]", "&gt;", ">", ">");
  public static final Symbol GREATER_EQUALS = new Symbol(new String[]{"greater_equals", ">="}, "^\\>\\=", "&ge;", "\\ge", "\u2265");

  public static final Symbol PROP_TO = new Symbol("propTo", null, "&prop;", " \\propto ", "\u221D");

  //! public static final Symbol EQUALS = new Symbol(new String[]{"equals", "="}, "^(\\=)[^\\=\\>]");
  public static final Symbol AT = new Symbol(new String[]{"at", "@"}, "^(\\@)[^\\@\\>]", "@", "\\left.{\\1}\\right|_{\\2}", "@");

  //- public static final Symbol SLASH = new Symbol(new String[]{"slash", "/"}, "^(/)[^/]", " / ", null, "/"); 
  public static final Symbol WITH = new Symbol(new String[]{"with", "@@"}, "^\\@\\@");

  public static final Symbol ADD = new Symbol(new String[]{"plus", "+"}, "^\\+");
  public static final Symbol MINUS = new Symbol(new String[]{"minus", "-"}, "^(\\-)[^\\>]");

  // explicit HTML/latex spaces?
  public static final Symbol COLON = new Symbol(new String[]{"colon", ":"}, "^\\:");
  // TODO: Clarify: Change name as its use is for multiplication and its unicode value
  // is a small cross (note it is different to a large cross which could
  // be used for a cross product).  Also, multiplication is more often denoted by a space.
  public static final Symbol TIMES = new Symbol(new String[]{"times", "*"}, "^\\*", " * ", " \\times ", "\u00D7")// bullet: &#8226; &times;

  // HTML Works but not unicode.
  public static final Symbol IN = new Symbol("in", null, "&#8712;", " \\in ", "\u220A");
  public static final Symbol UNION = new Symbol("union", null, "&#8746;", " \\cup ", "\u222A");
  public static final Symbol INTERSECTION = new Symbol("intersection", null, "&#8745;", " \\cap ", "\u2229");

  public static final Symbol SUBSET = new Symbol("subset", null, "&sub;", " \\subset ", "\u2282");
  public static final Symbol SUPERSET = new Symbol("superset", null, "&sup;", " \\supset ", "\u2283");

  public static final Symbol OPEN_BRACKET = new Symbol(new String[]{"open_bracket", "("}, "^\\(", "(", " \\left( ", "(");
  public static final Symbol CLOSE_BRACKET = new Symbol(new String[]{"close_bracket", ")"}, "^\\)", ")", " \\right) ", ")");
  public static final Symbol OPEN_CURLY_BRACKET = new Symbol(new String[]{"open_curly_bracket", "{"}, "^\\{", "{", " \\left\\{ ", "{");
  public static final Symbol CLOSE_CURLY_BRACKET = new Symbol(new String[]{"close_curly_bracket", "}"}, "^\\}", "}", " \\right\\} ", "}");
  public static final Symbol OPEN_SQUARE_BRACKET = new Symbol(new String[]{"open_square_bracket", "["}, "^\\[", "[", " \\left [ ", "[");
  public static final Symbol CLOSE_SQUARE_BRACKET = new Symbol(new String[]{"close_square_bracket", "]"}, "^\\]", "]", " \\right ] ", "]");
  public static final Symbol COMMA = new Symbol(new String[]{"comma", ","}, "^\\,");


  // BUG: latex version is a prefix function!
  // The Unicode slash is different to divide which is an actual division
  // sign, but this is still ambiguous.
  // TODO: Distinguish in LaTeX and HTML.
  public static final Symbol SLASH = new Symbol(new String[]{"slash", "/"}, "^(/)[^/]", " / ", null, "/")
  public static final Symbol DOUBLE_SLASH = new Symbol(new String[]{"double_slash", "//"}, "^//", " / ", " / ", "/");  
  public static final Symbol DIVIDE = new Symbol(new String[]{"divide", "//"}, "^//", " / ", "/", "/");
  public static final Symbol PERCENT = new Symbol(new String[]{"percent", "%"}, "^%", "&#37;", "\\div ", "%");

  public static final Symbol BACKSLASH = new Symbol(new String[]{"backslash", "\\"}, "^\\\\", " \\ ", " \\ ", "\\")
  public static final Symbol AMPERSAND = new Symbol(new String[]{"ampersand", "&"}, "^\\&", " &amp; ", " & ", "&")

  public static final Symbol DASH = new Symbol(new String[]{"dash", "'"}, "^\\'");
  public static final Symbol SEMICOLON = new Symbol(new String[]{"semicolon", ";"}, "^\\;");

  public static final Vector<Symbol> CONSTANT_SYMBOLS =
      new Vector<Symbol>(Arrays.asList(
      PARTIAL, H_BAR, DEGREE, INTEGRAL, INFINITY, EMPTY,
      UNKNOWN, TO, THUS, NABLA /*, UNKNOWN*/));

  public static final Vector<Symbol> DEFAULT_OPERAND_SYMBOLS =
    new Vector<Symbol>(Arrays.asList(
      //-? RE_ARGUMENT, RE_BRANCH, RE_COMPOUND, RE_FUNCTION,
      //-? RE_INTEGER, RE_NUMBER, RE_REAL, RE_SYMBOL,
      //-? RE_TOKEN, RE_WORD, RE_VARIABLE,
      PARTIAL, H_BAR, DEGREE, INFINITY, EMPTY, UNKNOWN, NABLA, INTEGRAL));

  public static final Vector<Symbol> UPPER_CASE_GREEK_LETTERS =
    new Vector<Symbol>(Arrays.asList(
      Alpha,   Beta,  Gamma,   Delta,
      Epsilon, Zeta,  Eta,     Theta,
      Iota,    Kappa, Lambda,  Mu,
      Nu,      Xi,    Omicron, Pi,
      Rho,     Sigma, Tau,     Upsilon,
      Phi,     Chi,   Psi,     Omega));

  public static final Vector<Symbol> LOWER_CASE_GREEK_LETTERS =
    new Vector<Symbol>(Arrays.asList(
      alpha,   beta,  gamma,   delta,
      epsilon, zeta,  eta,     theta,
      iota,    kappa, lambda,  mu,
      nu,      xi,    omicron, pi,
      rho,     sigma, tau,     upsilon,
      phi,     chi,   psi,     omega));

  public static final Vector<Symbol> GREEK_LETTERS =
    new Vector<Symbol>(Arrays.asList(
      alpha,   beta,  gamma,   delta,
      epsilon, zeta,  eta,     theta,
      iota,    kappa, lambda,  mu,
      nu,      xi,    omicron, pi,
      rho,     sigma, tau,     upsilon,
      phi,     chi,   psi,     omega,
      Alpha,   Beta,  Gamma,   Delta,
      Epsilon, Zeta,  Eta,     Theta,
      Iota,    Kappa, Lambda,  Mu,
      Nu,      Xi,    Omicron, Pi,
      Rho,     Sigma, Tau,     Upsilon,
      Phi,     Chi,   Psi,     Omega));

  public static final Purpose[] ROMAN_LETTERS = {
    new Word("a"), new Word("A"), new Word("b"), new Word("B"), new Word("c"), new Word("C"), new Word("d"), new Word("D"),
    new Word("e"), new Word("E"), new Word("f"), new Word("F"), new Word("g"), new Word("G"), new Word("h"), new Word("H"),
    new Word("i"), new Word("I"), new Word("j"), new Word("J"), new Word("k"), new Word("K"), new Word("l"), new Word("L"),
    new Word("m"), new Word("M"), new Word("n"), new Word("N"), new Word("o"), new Word("O"), new Word("p"), new Word("P"),
    new Word("q"), new Word("Q"), new Word("r"), new Word("R"), new Word("s"), new Word("S"), new Word("t"), new Word("T"),
    new Word("u"), new Word("U"), new Word("v"), new Word("V"), new Word("w"), new Word("W"), new Word("x"), new Word("X"),
    new Word("y"), new Word("Y"), new Word("z"), new Word("Z")};


  public static final Set<Symbol> OPERAND_SYMBOL_SET = mathSymbols();

  static Set<Symbol> mathSymbols() {
    Set<Symbol> set = new HashSet<Symbol>();
    set.addAll(Symbol.DEFAULT_OPERAND_SYMBOLS);
    set.addAll(Symbol.GREEK_LETTERS);
    return set;
  }


  /**
   * Associate a map from English to Greek letter map.
   */
  public static final Map<Character, Symbol> ROMAN_TO_GREEK = createRomanToGreekMap();

  public static final Set<Symbol> MATCHES = new HashSet<Symbol>(Arrays.asList(new Symbol[]{Symbol.RE_ARGUMENT, Symbol.RE_TOKEN, Symbol.RE_SYMBOL}));

  private static Map<Character, Symbol> createRomanToGreekMap() {
    HashMap<Character, Symbol> greekToRoman = new HashMap<Character, Symbol>(); //! Was an identity hashmap, yet worked!

    greekToRoman.put('a', alpha);
    greekToRoman.put('b', beta);
    greekToRoman.put('g', gamma);
    greekToRoman.put('d', delta);
    greekToRoman.put('e', epsilon);
    greekToRoman.put('z', zeta);
    greekToRoman.put('n', eta);
    greekToRoman.put('h', eta);
    greekToRoman.put('q', theta);
    greekToRoman.put('i', iota);
    greekToRoman.put('k', kappa);
    greekToRoman.put('l', lambda);
    greekToRoman.put('m', mu);
    greekToRoman.put('v', nu);
    greekToRoman.put('x', xi);
    greekToRoman.put('w', omicron)
    greekToRoman.put('p', pi);
    greekToRoman.put('r', rho);
    greekToRoman.put('s', sigma);
    greekToRoman.put('t', tau);
    greekToRoman.put('u', upsilon);
    greekToRoman.put('f', phi);
    greekToRoman.put('c', chi);
    greekToRoman.put('y', psi);
    greekToRoman.put('j', psi);
    greekToRoman.put('o', omega);

    greekToRoman.put('A', Alpha);
    greekToRoman.put('B', Beta);
    greekToRoman.put('G', Gamma);
    greekToRoman.put('D', Delta);
    greekToRoman.put('E', Epsilon);
    greekToRoman.put('Z', Zeta);
    greekToRoman.put('N', Eta);
    greekToRoman.put('H', Eta);
    greekToRoman.put('Q', Theta);
    greekToRoman.put('I', Iota);
    greekToRoman.put('K', Kappa);
    greekToRoman.put('L', Lambda);
    greekToRoman.put('M', Mu);
    greekToRoman.put('V', Nu);
    greekToRoman.put('X', Xi);
    greekToRoman.put('W', Omicron)
    greekToRoman.put('P', Pi);
    greekToRoman.put('R', Rho);
    greekToRoman.put('S', Sigma);
    greekToRoman.put('T', Tau);
    greekToRoman.put('U', Upsilon);
    greekToRoman.put('F', Phi);
    greekToRoman.put('C', Chi);
    greekToRoman.put('Y', Psi);
    greekToRoman.put('J', Psi);
    greekToRoman.put('O', Omega);

    return greekToRoman;
  }

  public static final Vector<Symbol> ALL_SYMBOLS =
    new Vector<Symbol>(Arrays.asList(
      RE_ARGUMENT, RE_BRANCH, RE_COMPOUND, RE_FUNCTION, RE_INTEGER,
      RE_NUMBER, RE_REAL, RE_SYMBOL, RE_TOKEN, RE_WORD, TO, THUS,
      AMPERSAND, AND, WITH, AT, Alpha, BACKSLASH, BANG, Beta, CARET,
      CLOSE_BRACKET, CLOSE_CURLY_BRACKET, CLOSE_SQUARE_BRACKET,
      COLON, COMMA, CROSS, Chi, DASH, DIVIDE, DOLLAR, DOT, DOUBLE_CARET,
      DOUBLE_LEFT_ANGLE, DOUBLE_PIPE, DOUBLE_RIGHT_ANGLE, DOUBLE_SLASH,
      DOUBLE_UNDERSCORE, Delta, APPROX, EQUALS, Epsilon, Eta,
      GREATER_EQUALS, GREATER_THAN, Gamma, HASH, HIDDEN, H_BAR, DEGREE,
      IN, SUBSET, SUPERSET, INFINITY, EMPTY, INTEGRAL, INTERSECTION, Iota,
      Kappa, LESS_EQUALS, LESS_THAN, Lambda, MINUS, MP, Mu, NABLA, NAND,
      NOR, NOT, EQUIVALENT, NOT_EQUALS, Nu, OPEN_BRACKET,
      OPEN_CURLY_BRACKET, OPEN_SQUARE_BRACKET, OR, OTIMES, Omega,
      Omicron, PARTIAL, PIPE, ADD, PM, Phi, Pi, Psi, PROP_TO, RADICAL,
      Rho, SEMICOLON, SLASH, STOP, Sigma, TILDE, TIMES, Tau, Theta,
      UNDERSCORE, UNION, UNKNOWN, Upsilon, XOR, Xi, Zeta, alpha, beta,
      chi, delta, epsilon, eta, gamma, iota, kappa, lambda, mu, nu,
      omega, omicron, phi, pi, psi, rho, sigma, tau, theta, upsilon, xi,
      zeta));

  public static final Vector<Symbol> CLJ_SYMBOLS =
    new Vector<Symbol>(Arrays.asList(
          AND,
          BANG,
          CARET,
          CLOSE_BRACKET,
          CLOSE_CURLY_BRACKET,
          CLOSE_SQUARE_BRACKET,
          COMMA,
          DASH,
          DIVIDE,
          EQUALS,
          GREATER_EQUALS,
          GREATER_THAN,
          HIDDEN,
          LESS_EQUALS,
          LESS_THAN,
          MINUS,
          NOT,
          NOT_EQUALS,
          OPEN_BRACKET,
          OPEN_CURLY_BRACKET,
          OPEN_SQUARE_BRACKET,
          OR,
          ADD,
          SLASH,
          STOP,
          TIMES,
          UNKNOWN));

  /////////////
  // SYMBOLS //
  /////////////

  final String symbol;
  final String word;
  final String html;
  final Pattern pattern;
  final String latex;
  final String unicode;
  final String[] digraph;


  public Symbol(String[] wordAndSymbol, String regex) { // Format= "caret", "^"
    this.word = wordAndSymbol[0];
    this.symbol = wordAndSymbol[1];
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = this.symbol;
    this.latex = this.symbol;
    this.unicode = this.symbol;
    this.digraph = new String[0];
  }

  public Symbol(String word, String regex) {
    this.word = word;
    this.symbol = word;
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = word;
    this.latex = word;
    this.unicode = word;
    this.digraph = new String[0];
  }

  public Symbol(String[] wordAndSymbol, String regex, String html, String latex, String unicode) {
    this.word = wordAndSymbol[0]; // as a word
    this.symbol = wordAndSymbol[1]; // symbolically
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = html;
    this.latex = latex;
    this.unicode = unicode;
    this.digraph = new String[0];
  }

  public Symbol(String word, String regex, String html, String latex, String unicode) {
    this.word = word;
    this.symbol = word;
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = html;
    this.latex = latex;
    this.unicode = unicode;
    this.digraph = new String[0];
  }

  public Symbol(String word, String regex, String html, String latex, String unicode, String digraph) {
    this.word = word;
    this.symbol = word;
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = html;
    this.latex = latex;
    this.unicode = unicode;
    this.digraph = new String[]{digraph};
  }

  public Symbol(String word, String regex, String html, String latex, String unicode, String[] digraph) {
    this.word = word;
    this.symbol = word;
    this.pattern = regex!=null ? Pattern.compile(regex) : null;
    this.html = html;
    this.latex = latex;
    this.unicode = unicode;
    this.digraph = digraph;
  }



  public Pattern getPattern() {
    return pattern;
  }

  public String[] getDigraphs() {
    return digraph;
  }

  public Token matchNextToken(StringBuffer equation) {
    if (pattern==null) {
      return null;
    }
    Matcher matcher = pattern.matcher(equation);
    // TODO: Simplify.
    if (matcher.find() && matcher.start()==0) {
      if (matcher.groupCount()>0) {
        equation.delete(0, matcher.end(matcher.groupCount()));
      } else {
        equation.delete(0, matcher.end());
      }
      return new Token(this);
    } else {
      return null;
    }
  }

  //////////////////////////////
  // SYMBOL SEARCHING METHODS //
  //////////////////////////////

  @Override
  public int iterateToStateForwards(Vector<Argument> args, int index) {
    for (int i=index; i<args.size(); i++) {
      if (args.get(i)!=null && args.get(i).getState()==this) {
        return i;
      }
    }
    return -1;
  }

  @Override
  public int iterateToStateBackwards(Vector<Argument> args) {
    for (int i=args.size()-1; i>=0; i--) {
      if (args.get(i).getState()==this) {
        return i;
      }
    }
    return -1;
  }

  @Override
  public int[] findAllStateIndices(Vector<Argument> args, boolean includeLeft, boolean includeRight) {
    Vector<Integer> indices = new Vector<Integer>();
    if (includeLeft) {
      indices.add(new Integer(-1));
    }
    int index = this.iterateToStateForwards(args);
    while (index!=-1) {
      indices.add(new Integer(index));
      index = this.iterateToStateForwards(args, index+1);
    }
    if (includeRight) {
      indices.add(new Integer(args.size()));
    }
    return toIntegerArray(indices);
  }


  /////////////////
  // INFORMATION //
  /////////////////

  public String toVerboseString() {
    return getFullName();
  }

  public String toHTML() {
    return " " + html + " ";
  }

  public String toUnicode() {
    return unicode;
  }

  public Box toBox(Argument argument, long settings, ColourScheme colourScheme) {
    // Get bold / regular settings from here.
    //settings |= Box.PLAIN_FONT;
    return new BoxWord(argument, toUnicode(), settings);
  }

  @Override
  public String toLatex() {
    return latex;
  }

  /**
   * While usually represented as symbols, it can be useful to associate a word with it.
   */
  public String getWord() {
    return word;
  }

  @Override
  public String getFullName() {
    return PREFIX_CHAR + word;
  }

  @Override
  public String getName() { // TODO: Rename.
    return symbol;
  }

  protected boolean hasTextSymbol() {
    return !word.equals(symbol);
  }

  @Override
  public String toString() {
    if (hasTextSymbol()) {
      return symbol;  // TODO: Just use symbol=?null.
    } else {
      //- return PREFIX_CHAR + word; // OLD: If getFullName() is changed to use word then call it here.
      return getFullName();
    }
  }

  @Override
  public boolean matches(String string) {
    if (symbol.equals(string) || getFullName().equals(string)) {
      return true;
    }
    if (pattern!=null) {
      return pattern.matcher(string).matches();
    }
    return false;
  }

  @Override
  public boolean matchTypeChar(char type) {
    return type==Purpose.DEFAULT_PREFIX_CHAR || PREFIX_CHAR==type;
  }

  @Override
  public boolean equals(Purpose purpose) {
    if (purpose instanceof Symbol) {
      // TODO: Are symbols unique, and if so how do you
      // restrict their creation to avoid duplicates?
      return this==purpose;
    } else {
      return false;
    }
  }


  @Override
  public Set<Symbol> getMatchSymbols() {
    return MATCHES;
  }
  @Override
  public boolean isVariable() {
    return ! CONSTANT_SYMBOLS.contains(this);
  }

TOP

Related Classes of ket.math.Symbol

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.