private object EvaluateMatrixExpression(MatrixExpression e, VariableContext context)
        {
            List <double> items = new List <double>();

            foreach (Expression[] row in e.Rows)
            {
                foreach (Expression item in row)
                {
                    items.Add(Convert.ToDouble(Evaluate(item, context)));
                }
            }


            return(new Matrix(e.RowCount, e.ColumnCount, items.ToArray()));
        }
Exemplo n.º 2
0
        private void CheckMatrixExpression(MatrixExpression e, TypeCheckingContext context)
        {
            foreach (Expression[] row in e.Rows)
            {
                foreach (Expression item in row)
                {
                    PerformTypeChecking(item, context);
                    if (!Types.IsNumberType(item.Type))
                    {
                        context.ErrorProvider.ThrowException("An element of a matrix must be convertialbe to double.", item);
                    }
                }
            }

            e.Type = typeof(Matrix);
        }
Exemplo n.º 3
0
        private Expression CompileMatrix(int lambdaLevel)
        {
            PushPosition();

            Move();

            List <List <Expression> > elements = new List <List <Expression> >();

            while (true)
            {
                Expression element = Compile(lambdaLevel);

                if (elements.Count == 0)
                {
                    elements.Add(new List <Expression>());
                }

                elements.Last().Add(element);

                if (IsPunctuationOf(","))
                {
                    Move();
                }
                else if (IsPunctuationOf(";"))
                {
                    Move();
                    elements.Add(new List <Expression>());
                }
                else if (IsPunctuationOf("]"))
                {
                    Move();
                    break;
                }
                else
                {
                    ThrowExpects(", ; or ]");
                }
            }

            elements = elements.Where(i => i.Count > 0).ToList();


            if (elements.Count == 0)
            {
                ThrowException("A matrix must have at least one row.");
            }
            else if (elements[0].Count == 0)
            {
                ThrowException("A matrix must have at least one column.");
            }
            else if (!elements.Skip(1).All(i => i.Count == elements[0].Count))
            {
                ThrowException("Each row of the matrix must have the same count of elements.");
            }


            MatrixExpression result = new MatrixExpression(elements.Select(i => i.ToArray()).ToArray(), PeekPos(), Pos);

            PopPosition();

            return(result);
        }