Пример #1
0
 public static Parser GetInstance()
 {
     if (singleton == null)
     {
         singleton = new SillyParser(null);
     }
     return(singleton);
 }
Пример #2
0
        /// <summary>
        /// Interpret a matrix, where semicolons ";" seperate rows and commas "," seperate columns.
        /// The cell values can be any sort of expression, including variables.
        /// The cell values may contain operators that use commas "," eg "add(a, b)".
        /// The cell values can also use the more standard forms of algebraic expressions eg "a + b".
        ///
        /// Valid operators are:
        /// <code>plus, minus, mul, div, remainder, negate, exp, sin, cos, tan, asin, acos, atan, atan2</code>
        /// </summary>
        public Matrix InterpretMatrix(string matrixStr)
        {
            string[] rows = matrixStr.Split(';');
            Node[,] values = null;
            Parser p         = SillyParser.GetInstance();
            int    colLength = 0;

            for (int rowIndex = 0; rowIndex < rows.Length; rowIndex++)
            {
                try
                {
                    Node[] rowVals      = InterpretListOfNodes(rows[rowIndex]);
                    int    newColLength = rowVals.Length;
                    if (values == null)
                    {
                        values    = new Node[rows.Length, rowVals.Length];
                        colLength = values.GetLength(1);
                    }
                    else if (colLength != newColLength)
                    {
                        throw new FormatException($"Expected '{colLength}' columns on every row, but found '{newColLength}' columns in '{rows[rowIndex]}'");
                    }

                    for (int colIndex = 0; colIndex < colLength; colIndex++)
                    {
                        values[rowIndex, colIndex] = rowVals[colIndex];
                    }
                }
                catch (FormatException e)
                {
                    throw new FormatException($"Error at row index '{rowIndex}'!", e);
                }
            }

            return(new Matrix(values));
        }