public QsValue Average(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3)
        {
            FixIndices(ref fromIndex, ref toIndex);

            var tot   = SumElements(fromIndex, toIndex, arg0, arg1, arg2, arg3);
            var n     = toIndex - fromIndex + 1;
            var count = new QsScalar {
                NumericalQuantity = Qs.ToQuantity((double)n)
            };

            return(tot / count);
        }
        public QsValue StdDeviation(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2)
        {
            var n = toIndex - fromIndex + 1;

            FixIndices(ref fromIndex, ref toIndex);
            var     mean  = Average(fromIndex, toIndex, arg0, arg1, arg2);
            var     Two   = "2".ToScalarValue();
            QsValue Total = (GetElementValue(fromIndex, arg0, arg1, arg2) - mean).PowerOperation(Two);

            for (int i = fromIndex + 1; i <= toIndex; i++)
            {
                var p   = GetElementValue(i, arg0, arg1, arg2) - mean;
                var pp2 = p.PowerOperation(Two);
                Total = Total + pp2;
            }
            var count = new QsScalar {
                NumericalQuantity = Qs.ToQuantity((double)n)
            };

            return(Total / count);
        }
Esempio n. 3
0
        /// <summary>
        /// Get factorial for the <see>QsValue</see> whether it is Scalar, Vector, Matrix, and later Tensor.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static QsValue Factorial(QsValue value)
        {
            if (value is QsBoolean)
            {
                var b = (QsBoolean)value;
                return(new QsBoolean {
                    Value = !b.Value
                });
            }

            if (value is QsScalar)
            {
                QsScalar sv = (QsScalar)value;
                if (sv.ScalarType == ScalarTypes.NumericalQuantity)
                {
                    AnyQuantity <double> number = sv.NumericalQuantity;
                    return(new QsScalar {
                        NumericalQuantity = QuantityFactorial(number)
                    });
                }
                else
                {
                    throw new QsException("Unsupported scalar object");
                }
            }
            else if (value is QsVector)
            {
                var vec = value as QsVector;

                QsVector rvec = new QsVector(vec.Count);

                foreach (var v in vec)
                {
                    rvec.AddComponent((QsScalar)Factorial(v));
                }

                return(rvec);
            }
            else if (value is QsMatrix)
            {
                var      mat   = value as QsMatrix;
                QsMatrix Total = new QsMatrix();

                for (int IY = 0; IY < mat.RowsCount; IY++)
                {
                    List <QsScalar> row = new List <QsScalar>(mat.ColumnsCount);

                    for (int IX = 0; IX < mat.ColumnsCount; IX++)
                    {
                        row.Add((QsScalar)Factorial(mat[IY, IX]));
                    }

                    Total.AddRow(row.ToArray());
                }
                return(Total);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        /// <summary>
        /// This is a tricky functions
        /// it returns Vector if components are Scalars.
        /// Matrix if components are Vectors
        /// </summary>
        /// <param name="fromIndex"></param>
        /// <param name="toIndex"></param>
        /// <returns></returns>
        public QsValue QsValueElements(int fromIndex, int toIndex)
        {
            if (this.Parameters.Length > 0)
            {
                List <string> ProcessedElements = new List <string>();

                #region symbolic representation

                if (fromIndex > toIndex)
                {
                    for (int e_ix = fromIndex; e_ix >= toIndex; e_ix--)
                    {
                        var se = GetElement(e_ix);

                        // s[n](x) ..> $x*x^n+$n     # symbolic variables shouldn't be changed ($x, $n) we should take care.

                        // first preserve the symbolic variable with the same index name that we are going to change.
                        string se_text = se.ElementDeclaration.Replace("$" + this.SequenceIndexName, "`");

                        // replace the index name with the
                        se_text = se_text.Replace(this.SequenceIndexName, e_ix.ToString(CultureInfo.InvariantCulture));

                        // get back the symbolic
                        se_text = se_text.Replace("`", "$" + this.SequenceIndexName);

                        if (!string.IsNullOrEmpty(SequenceRangeStartName))
                        {
                            se_text = se_text.Replace("$" + SequenceRangeStartName, "`");
                            se_text = se_text.Replace(SequenceRangeStartName, StartIndex.ToString(CultureInfo.InvariantCulture));
                            se_text = se_text.Replace("`", "$" + SequenceRangeStartName);
                        }

                        if (!string.IsNullOrEmpty(SequenceRangeEndName))
                        {
                            se_text = se_text.Replace("$" + SequenceRangeEndName, "`");
                            se_text = se_text.Replace(SequenceRangeEndName, EndIndex.ToString(CultureInfo.InvariantCulture));
                            se_text = se_text.Replace("`", "$" + SequenceRangeEndName);
                        }


                        // replace the parameters in declaration with the same
                        foreach (var param in this.Parameters)
                        {
                            se_text = se_text.Replace("$" + param.Name, "`");

                            se_text = se_text.Replace(param.Name, "$" + param.Name);

                            se_text = se_text.Replace("`", "$" + param.Name);
                        }

                        ProcessedElements.Add(se_text);
                    }
                }
                else
                {
                    for (int e_ix = fromIndex; e_ix <= toIndex; e_ix++)
                    {
                        var se = GetElement(e_ix);

                        string se_text = se.ElementDeclaration.Replace("$" + this.SequenceIndexName, "`");
                        se_text = se_text.Replace(this.SequenceIndexName, e_ix.ToString(CultureInfo.InvariantCulture));
                        se_text = se_text.Replace("`", "$" + this.SequenceIndexName);
                        if (!string.IsNullOrEmpty(SequenceRangeStartName))
                        {
                            se_text = se_text.Replace("$" + SequenceRangeStartName, "`");
                            se_text = se_text.Replace(SequenceRangeStartName, StartIndex.ToString(CultureInfo.InvariantCulture));
                            se_text = se_text.Replace("`", "$" + SequenceRangeStartName);
                        }

                        if (!string.IsNullOrEmpty(SequenceRangeEndName))
                        {
                            se_text = se_text.Replace("$" + SequenceRangeEndName, "`");
                            se_text = se_text.Replace(SequenceRangeEndName, EndIndex.ToString(CultureInfo.InvariantCulture));
                            se_text = se_text.Replace("`", "$" + SequenceRangeEndName);
                        }


                        // replace the parameters with names
                        foreach (var param in this.Parameters)
                        {
                            se_text = se_text.Replace("$" + param.Name, "`");

                            se_text = se_text.Replace(param.Name, "$" + param.Name);

                            se_text = se_text.Replace("`", "$" + param.Name);
                        }

                        ProcessedElements.Add(se_text);
                    }
                }
                var     ee = QsEvaluator.CurrentEvaluator.SilentEvaluate(ProcessedElements[0]);
                QsValue Total;

                if (ee is QsScalar)
                {
                    Total = new QsVector(System.Math.Abs(toIndex - fromIndex) + 1);
                    foreach (string pel in ProcessedElements)
                    {
                        ((QsVector)Total).AddComponent((QsScalar)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel));
                    }
                }
                else if (ee is QsVector)
                {
                    Total = new QsMatrix();
                    foreach (string pel in ProcessedElements)
                    {
                        ((QsMatrix)Total).AddVector((QsVector)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel));
                    }
                }
                else if (ee is QsMatrix)
                {
                    Total = new QsTensor();
                    foreach (string pel in ProcessedElements)
                    {
                        ((QsTensor)Total).AddMatrix((QsMatrix)QsEvaluator.CurrentEvaluator.SilentEvaluate(pel));
                    }
                }
                else
                {
                    throw new QsException("This is enough, no more than matrix values please.");
                }



                #endregion


                return(Total);
            }

            QsValue firstElement = (QsValue)GetElementValue(fromIndex);
            if (firstElement is QsScalar)
            {
                //return vector
                QsVector Total = new QsVector(System.Math.Abs(toIndex - fromIndex) + 1);


                #region Numerical Representation
                Total.AddComponent((QsScalar)firstElement);

                if (toIndex >= fromIndex)
                {
                    for (int i = fromIndex + 1; i <= toIndex; i++)
                    {
                        Total.AddComponent((QsScalar)GetElementValue(i));
                    }
                }
                else
                {
                    for (int i = fromIndex - 1; i >= toIndex; i--)
                    {
                        Total.AddComponent((QsScalar)GetElementValue(i));
                    }
                }
                #endregion


                return(Total);
            }
            else if (firstElement is QsVector)
            {
                //return vector
                QsMatrix Total = new QsMatrix();
                Total.AddVector((QsVector)firstElement);

                if (toIndex >= fromIndex)
                {
                    for (int i = fromIndex + 1; i <= toIndex; i++)
                    {
                        Total.AddVector((QsVector)GetElementValue(i));
                    }
                }
                else
                {
                    for (int i = fromIndex - 1; i >= toIndex; i--)
                    {
                        Total.AddVector((QsVector)GetElementValue(i));
                    }
                }


                return(Total);
            }
            else if (firstElement is QsMatrix)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        public QsValue QsValueElements(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3, QsValue arg4, QsValue arg5)
        {
            QsValue firstElement = GetElementValue(fromIndex, arg0, arg1, arg2, arg3, arg4, arg5);

            if (firstElement is QsScalar)
            {
                //return vector
                QsVector Total = new QsVector(System.Math.Abs(toIndex - fromIndex) + 1);

                Total.AddComponent((QsScalar)firstElement);

                if (toIndex >= fromIndex)
                {
                    for (int i = fromIndex + 1; i <= toIndex; i++)
                    {
                        Total.AddComponent((QsScalar)GetElementValue(i, arg0, arg1, arg2, arg3, arg4, arg5));
                    }
                }
                else
                {
                    for (int i = fromIndex - 1; i >= toIndex; i--)
                    {
                        Total.AddComponent((QsScalar)GetElementValue(i, arg0, arg1, arg2, arg3, arg4, arg5));
                    }
                }

                return(Total);
            }
            else if (firstElement is QsVector)
            {
                //return vector
                QsMatrix Total = new QsMatrix();
                Total.AddVector((QsVector)firstElement);

                if (toIndex >= fromIndex)
                {
                    for (int i = fromIndex + 1; i <= toIndex; i++)
                    {
                        Total.AddVector((QsVector)GetElementValue(i, arg0, arg1, arg2, arg3, arg4, arg5));
                    }
                }
                else
                {
                    for (int i = fromIndex - 1; i >= toIndex; i++)
                    {
                        Total.AddVector((QsVector)GetElementValue(i, arg0, arg1, arg2, arg3, arg4, arg5));
                    }
                }

                return(Total);
            }
            else if (firstElement is QsMatrix)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        public QsValue MulElements(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3, QsValue arg4, QsValue arg5, QsValue arg6)
        {
            FixIndices(ref fromIndex, ref toIndex);

            QsValue Total = GetElementValue(fromIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);

            for (int i = fromIndex + 1; i <= toIndex; i++)
            {
                Total = Total * GetElementValue(i, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
            }

            return(Total);
        }
        public QsValue SumElements(int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2)
        {
            FixIndices(ref fromIndex, ref toIndex);

            QsValue Total = GetElementValue(fromIndex, arg0, arg1, arg2);

            for (int i = fromIndex + 1; i <= toIndex; i++)
            {
                Total = Total + GetElementValue(i, arg0, arg1, arg2);
            }

            return(Total);
        }
Esempio n. 8
0
        public QsValue RangeOperation(string operation, int fromIndex, int toIndex, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3, QsValue arg4, QsValue arg5, QsValue arg6)
        {
            BeginRangeOperation(fromIndex, toIndex);
            QsValue result = default(QsValue);

            try
            {
                if (operation.Equals("SumElements", StringComparison.OrdinalIgnoreCase))
                {
                    result = SumElements(fromIndex, toIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
                }
                else if (operation.Equals("MulElements", StringComparison.OrdinalIgnoreCase))
                {
                    result = MulElements(fromIndex, toIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
                }
                else if (operation.Equals("Average", StringComparison.OrdinalIgnoreCase))
                {
                    result = Average(fromIndex, toIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
                }
                else if (operation.Equals("StdDeviation", StringComparison.OrdinalIgnoreCase))
                {
                    result = StdDeviation(fromIndex, toIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
                }
                else if (operation.Equals("QsValueElements", StringComparison.OrdinalIgnoreCase))
                {
                    result = QsValueElements(fromIndex, toIndex, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
                }
                else
                {
                    EndRangeOperation();
                    throw new QsException(operation + " range operation is not known");
                }

                EndRangeOperation();
                return(result);
            }
            catch (Exception ex)
            {
                EndRangeOperation();
                throw new QsException("Un Handled Exception", ex);
            }
        }
Esempio n. 9
0
        public QsValue GetElementValue(int index, QsValue arg0, QsValue arg1, QsValue arg2, QsValue arg3, QsValue arg4, QsValue arg5, QsValue arg6)
        {
            QsValue val = (QsValue)GetElement(index).Execute(index, arg1, arg1, arg2, arg3, arg4, arg5, arg6);

            return(val);
        }
Esempio n. 10
0
        public QsValue GetElementValue(int index, QsValue arg0, QsValue arg1, QsValue arg2)
        {
            QsValue val = (QsValue)GetElement(index).Execute(index, arg0, arg1, arg2);

            return(val);
        }