public Msubsup(IBuildable[] contents) { // handle star superscript List <IBuildable> localCopy = new List <IBuildable>(contents); if (localCopy.Count == 3) { Mo mo = localCopy[2] as Mo; if (mo != null && mo.IsTimesOrStar) { Mi subscript = localCopy[1] as Mi; if (subscript != null) { subscript.Content += Semantics.StarPrefix; localCopy.RemoveAt(2); } else { // maybe the subscript is an mrow Mrow row = localCopy[1] as Mrow; if (row != null && row.LastElement != null && row.LastElement is WithTextContent) { WithTextContent lastElem = (WithTextContent)row.LastElement; lastElem.Content += Semantics.StarPrefix; localCopy.RemoveAt(2); } } } } base.contents = localCopy.ToArray(); }
public override void Visit(StringBuilder sb, BuildContext bc) { bc.Tokens.Add(this); bool needReduce = false; // note: the use of double is a judgement call here double result = 0.0; if (bc.Options.ReduceFractions) { Mrow row1 = first as Mrow; Mrow row2 = second as Mrow; if (row1 != null && row2 != null && row1.Contents.Length > 0 && row2.Contents.Length > 0) { Mn mn1 = row1.Contents[0] as Mn; Mn mn2 = row2.Contents[0] as Mn; if (mn1 != null && mn2 != null) { try { double _1, _2; if (double.TryParse(mn1.Content, out _1) && double.TryParse(mn2.Content, out _2) && _2 != 0.0) { result = _1 / _2; needReduce = true; } } catch { } } } } if (needReduce) { sb.Append(result); } else { sb.Append("(("); first.Visit(sb, bc); sb.Append(") / ("); second.Visit(sb, bc); sb.Append("))"); // add to checks bc.PossibleDivisionsByZero.Add(second); } }
/// <summary> /// This function replaces all known inverse trig functions (in the msup blocks) /// by inverse names, so that sin^-1 becomes arcsin. /// </summary> /// <param name="contents">Buidlable contents</param> private static void ReplaceInverseTrigFunctions(IBuildable[] contents) { for (int i = 0; i < contents.Length; i++) { IBuildable c = contents[i]; string trigFunction = string.Empty; if (c is Msup) { bool funcIsTrig = false, radixIsNeg1 = false; Pair <IBuildable, IBuildable> terms = (c as Msup).Values; if (terms.First is Mrow) { Mrow row1 = terms.First as Mrow; if (row1.Contents.Length > 0 && row1.Contents[0] is Mi) { if (Semantics.inverseTrigs.ContainsKey((row1.Contents[0] as Mi).Content)) { trigFunction = (row1.Contents[0] as Mi).Content; funcIsTrig = true; } } } if (terms.Second is Mrow) { Mrow row2 = terms.Second as Mrow; StringBuilder sb = new StringBuilder(); BuildContext bc = new BuildContext(); row2.Visit(sb, bc); if (sb.ToString() == "-1") { radixIsNeg1 = true; } } // if this is an inverse function, replace an <msup> with an inverse <mi> if (funcIsTrig && radixIsNeg1) { Mi mi = new Mi(Semantics.inverseTrigs[trigFunction]); contents[i] = mi; } } } }