00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <basix/string.hpp>
00014 #include <basix/table.hpp>
00015 #include <basix/literal.hpp>
00016 #include <basix/compound.hpp>
00017
00018 namespace mmx {
00019
00020 #define SET_OPNAME(var, val) \
00021 t [literal_to_string (var)] = val;
00022
00023 static table<string, string, exact_eq_table>
00024 math_symbol_table () {
00025 table<string, string, exact_eq_table> t;
00026
00027 SET_OPNAME (GEN_ACCESS, "_");
00028 SET_OPNAME (GEN_AND, "/\\");
00029 SET_OPNAME (GEN_CATALAN, "%mathcatalan");
00030 SET_OPNAME (GEN_COMPOSE, "%circ");
00031 SET_OPNAME (GEN_APPEND, "%join");
00032 SET_OPNAME (GEN_DELTA, "%delta");
00033 SET_OPNAME (GEN_DERIVATIVE, "D");
00034 SET_OPNAME (GEN_DOT, "%dotaccess");
00035 SET_OPNAME (GEN_DIV, "div");
00036 SET_OPNAME (GEN_E, "%mathe");
00037 SET_OPNAME (GEN_EQUAL, "=");
00038 SET_OPNAME (GEN_EQUIV, "<=>");
00039 SET_OPNAME (GEN_EULER, "%matheuler");
00040 SET_OPNAME (GEN_FACTORIAL, "factorial");
00041 SET_OPNAME (GEN_GTR, ">");
00042 SET_OPNAME (GEN_GTREQ, ">=");
00043 SET_OPNAME (GEN_GTRGTR, ">>");
00044 SET_OPNAME (GEN_I, "%mathi");
00045 SET_OPNAME (GEN_IMPLIES, "=>");
00046 SET_OPNAME (GEN_IN, "%in");
00047 SET_OPNAME (GEN_INFINITY, "%infty");
00048 SET_OPNAME (GEN_INTO, "->");
00049 SET_OPNAME (GEN_LESS, "<");
00050 SET_OPNAME (GEN_LESSEQ, "<=");
00051 SET_OPNAME (GEN_LESSLESS, "<<");
00052 SET_OPNAME (GEN_MAPSTO, "#{:->}#");
00053 SET_OPNAME (GEN_MINUS, "-");
00054 SET_OPNAME (GEN_MOD, "mod");
00055 SET_OPNAME (GEN_NOT, "!");
00056 SET_OPNAME (GEN_OR, "\\/");
00057 SET_OPNAME (GEN_OVER, "/");
00058 SET_OPNAME (GEN_PARTIAL, "%partial");
00059 SET_OPNAME (GEN_PI, "%mathpi");
00060 SET_OPNAME (GEN_PLUS, "+&");
00061 SET_OPNAME (GEN_POWER, "^");
00062 SET_OPNAME (GEN_PRIME, "%prime");
00063 SET_OPNAME (GEN_RANGE, "%ldots");
00064 SET_OPNAME (GEN_TO, "to");
00065 SET_OPNAME (GEN_DOWNTO, "downto");
00066 SET_OPNAME (GEN_ROW, "row");
00067 SET_OPNAME (GEN_SEQAND, "and");
00068 SET_OPNAME (GEN_SEQOR, "or");
00069 SET_OPNAME (GEN_SIZE, "%card");
00070 SET_OPNAME (GEN_SQTUPLE, "list");
00071 SET_OPNAME (GEN_TIMES, "*&");
00072 SET_OPNAME (GEN_VARTYPE, "%colons");
00073 SET_OPNAME (GEN_TRANSTYPE, "#{:>}#");
00074 SET_OPNAME (GEN_VARTRANSTYPE, "#{::>}#");
00075 SET_OPNAME (GEN_TUPLE, "tuple");
00076
00077 SET_OPNAME (GEN_UNEQUAL, "!=");
00078 SET_OPNAME (GEN_VWHERE, "|");
00079 SET_OPNAME (GEN_WHERE, "||");
00080 SET_OPNAME (GEN_XOR, "%veebar");
00081
00082 t["alpha"] = "%alpha";
00083 t["beta"] = "%beta";
00084 t["gamma"] = "%gamma";
00085 t["delta"] = "%delta";
00086 t["epsilon"] = "%varepsilon";
00087 t["zeta"] = "%zeta";
00088 t["eta"] = "%eta";
00089 t["theta"] = "%theta";
00090 t["iota"] = "%iota";
00091 t["kappa"] = "%kappa";
00092 t["lambda"] = "%lambda";
00093 t["mu"] = "%mu";
00094 t["nu"] = "%nu";
00095 t["xi"] = "%xi";
00096 t["omicron"] = "%omicron";
00097 t["pi"] = "%pi";
00098 t["rho"] = "%rho";
00099 t["sigma"] = "%sigma";
00100 t["tau"] = "%tau";
00101 t["upsilon"] = "%upsilon";
00102 t["phi"] = "%varphi";
00103 t["chi"] = "%chi";
00104 t["psi"] = "%psi";
00105 t["omega"] = "%omega";
00106
00107 t["Alpha"] = "%Alpha";
00108 t["Beta"] = "%Beta";
00109 t["Gamma"] = "%Gamma";
00110 t["Delta"] = "%Delta";
00111 t["Epsilon"] = "%Epsilon";
00112 t["Zeta"] = "%Zeta";
00113 t["Eta"] = "%Eta";
00114 t["Theta"] = "%Theta";
00115 t["Iota"] = "%Iota";
00116 t["Kappa"] = "%Kappa";
00117 t["Lambda"] = "%Lambda";
00118 t["Mu"] = "%Mu";
00119 t["Nu"] = "%Nu";
00120 t["Xi"] = "%Xi";
00121 t["Omicron"] = "%Omicron";
00122 t["Pi"] = "%Pi";
00123 t["Rho"] = "%Rho";
00124 t["Sigma"] = "%Sigma";
00125 t["Tau"] = "%Tau";
00126 t["Upsilon"] = "%Upsilon";
00127 t["Phi"] = "%Phi";
00128 t["Chi"] = "%Chi";
00129 t["Psi"] = "%Psi";
00130 t["Omega"] = "%Omega";
00131 return t;
00132 }
00133
00134 static void
00135 as_math (string& out, const generic& g) {
00136 static table<string, string, exact_eq_table> t (math_symbol_table ());
00137 if (is<literal> (g)) {
00138 string tmp;
00139 string s (literal_to_string (g));
00140 if (t->get (s, tmp))
00141 out << tmp;
00142 else if (N(s) > 0 && s[0]>='0' && s[0] <= '9')
00143 out << "\"" << s << "\"";
00144 else
00145 out << s;
00146 }
00147 else if (is<compound> (g)) {
00148 nat i, n= N (g);
00149 out << "(";
00150 if (n>0) {
00151 if (exact_eq (g[0], GEN_SQTUPLE) && n>1 &&
00152 is<compound> (g[1]) && exact_eq (g[1][0], GEN_ROW))
00153 out << "matrix";
00154 else as_math (out, g[0]);
00155 }
00156 for (i=1; i<n; i++) {
00157 out << " ";
00158 as_math (out, g[i]);
00159 }
00160 out << ")";
00161 }
00162 else {
00163 generic f= as_generic (flatten (g));
00164 as_math (out, f);
00165 }
00166 }
00167
00168 string
00169 as_math (const generic& g) {
00170 string out;
00171 as_math (out, g);
00172 return out;
00173 }
00174
00175 }