Exemplo n.º 1
0
        /// <summary>
        /// Returns the smallest value.
        /// </summary>
        /// <param name="Matrix">Matrix of values. Must not be empty.</param>
        /// <param name="Node">Node performing the evaluation.</param>
        /// <returns>Smallest value.</returns>
        public static IElement CalcMin(IMatrix Matrix, ScriptNode Node)
        {
            IElement    Result = null;
            IOrderedSet S      = null;

            foreach (IElement E in Matrix.ChildElements)
            {
                if (E.AssociatedObjectValue is null)
                {
                    continue;
                }

                if (Result is null || S.Compare(Result, E) > 0)
                {
                    Result = E;
                    S      = Result.AssociatedSet as IOrderedSet;
                    if (S is null)
                    {
                        throw new ScriptRuntimeException("Cannot compare operands.", Node);
                    }
                }
            }

            if (Result is null)
            {
                return(ObjectValue.Null);
            }
            else
            {
                return(Result);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluates the node, using the variables provided in the <paramref name="Variables"/> collection.
        /// </summary>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result.</returns>
        public override IElement Evaluate(Variables Variables)
        {
            ICommutativeRingWithIdentityElement From = this.left.Evaluate(Variables) as ICommutativeRingWithIdentityElement;

            if (From is null)
            {
                throw new ScriptRuntimeException("Invalid range.", this);
            }

            ICommutativeRingWithIdentityElement To = this.middle.Evaluate(Variables) as ICommutativeRingWithIdentityElement;

            if (To is null)
            {
                throw new ScriptRuntimeException("Invalid range.", this);
            }

            IOrderedSet S = From.AssociatedSet as IOrderedSet;

            if (S is null)
            {
                throw new ScriptRuntimeException("Cannot compare range.", this);
            }

            IElement Step, Last;
            int      Direction = S.Compare(From, To);
            bool     Done;

            if (!(this.middle2 is null))
            {
                Step = this.middle2.Evaluate(Variables);

                if (Direction < 0)
                {
                    if (S.Compare(Step, From.Zero) <= 0)
                    {
                        throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
                    }
                }
                else if (Direction > 0)
                {
                    if (S.Compare(Step, From.Zero) >= 0)
                    {
                        throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
                    }
                }
            }
Exemplo n.º 3
0
        private int Compare(IElement e1, IElement e2)
        {
            IOrderedSet S1 = e1.AssociatedSet as IOrderedSet;
            IOrderedSet S2 = e2.AssociatedSet as IOrderedSet;

            if (S1 == null || S2 == null || S1 != S2)
            {
                throw new ScriptRuntimeException("Cannot order elements.", this);
            }

            return(S1.Compare(e1, e2));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Evaluates the function on a scalar argument.
        /// </summary>
        /// <param name="Argument1">Function argument 1.</param>
        /// <param name="Argument2">Function argument 2.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Function result.</returns>
        public override IElement EvaluateScalar(IElement Argument1, IElement Argument2, Variables Variables)
        {
            IOrderedSet S = Argument1.AssociatedSet as IOrderedSet;

            if (S == null)
            {
                throw new ScriptRuntimeException("Unable to compare elements.", this);
            }

            if (S.Compare(Argument1, Argument2) > 0)
            {
                return(Argument1);
            }
            else
            {
                return(Argument2);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Evaluates the operator on scalar operands.
        /// </summary>
        /// <param name="Left">Left value.</param>
        /// <param name="Right">Right value.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result</returns>
        public override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
        {
            IOrderedSet S = Left.AssociatedSet as IOrderedSet;

            if (S == null)
            {
                throw new ScriptRuntimeException("Cannot compare operands.", this);
            }

            if (S.Compare(Left, Right) > 0)
            {
                return(BooleanValue.True);
            }
            else
            {
                return(BooleanValue.False);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Evaluates the node, using the variables provided in the <paramref name="Variables"/> collection.
        /// </summary>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result.</returns>
        public override IElement Evaluate(Variables Variables)
        {
            ICommutativeRingWithIdentityElement From = this.left.Evaluate(Variables) as ICommutativeRingWithIdentityElement;

            if (From == null)
            {
                throw new ScriptRuntimeException("Invalid range.", this);
            }

            ICommutativeRingWithIdentityElement To = this.middle.Evaluate(Variables) as ICommutativeRingWithIdentityElement;

            if (To == null)
            {
                throw new ScriptRuntimeException("Invalid range.", this);
            }

            IOrderedSet S = From.AssociatedSet as IOrderedSet;

            if (S == null)
            {
                throw new ScriptRuntimeException("Cannot compare range.", this);
            }

            IElement Step;
            int      Direction = S.Compare(From, To);
            bool     Done;

            if (this.middle2 != null)
            {
                Step = this.middle2.Evaluate(Variables);

                if (Direction < 0)
                {
                    if (S.Compare(Step, From.Zero) <= 0)
                    {
                        throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
                    }
                }
                else if (Direction > 0)
                {
                    if (S.Compare(Step, From.Zero) >= 0)
                    {
                        throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
                    }
                }
            }
            else
            {
                if (Direction <= 0)
                {
                    Step = From.One;
                }
                else
                {
                    Step = From.One.Negate();
                }
            }

            LinkedList <IElement> Elements = new LinkedList <IElement>();

            do
            {
                Variables[this.variableName] = From;
                Elements.AddLast(this.right.Evaluate(Variables));

                if (Direction == 0)
                {
                    Done = true;
                }
                else
                {
                    From = Operators.Arithmetics.Add.EvaluateAddition(From, Step, this) as ICommutativeRingWithIdentityElement;
                    if (From == null)
                    {
                        throw new ScriptRuntimeException("Invalid step size.", this);
                    }

                    if (Direction > 0)
                    {
                        Done = S.Compare(From, To) < 0;
                    }
                    else
                    {
                        Done = S.Compare(From, To) > 0;
                    }
                }
            }while (!Done);

            return(this.Encapsulate(Elements));
        }