Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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("))");
            }
        }
Esempio n. 3
0
 /// <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;
             Tuple <IBuildable, IBuildable> terms = (c as Msup).Values;
             if (terms.Item1 is Mrow)
             {
                 Mrow row1 = terms.Item1 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.Item2 is Mrow)
             {
                 Mrow          row2 = terms.Item2 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;
             }
         }
     }
 }