示例#1
0
        private void PushParameter(TextReader reader)
        {
            var parameter = string.Empty;

            int  peek;
            bool nextParenthesis = false;

            while ((peek = reader.Peek()) > -1)
            {
                var next = (char)peek;

                if (char.IsLetter(next) || (parameter.Length > 0 && char.IsDigit(next)))
                {
                    reader.Read();
                    parameter += next;
                }
                else if (next == ParenthesisRight)
                {
                    nextParenthesis = true;
                    break;
                }
                else
                {
                    break;
                }
            }
            if (nextParenthesis && Operator.IsDefinedFunction(parameter))
            {
                operatorStack.Push((Operator)parameter);
            }
            else
            {
                if (parameter == PiConstant)
                {
                    nodeStack.Push(new NodeConstant(Math.PI));
                }
                else if (parameter == ExponentConstant)
                {
                    nodeStack.Push(new NodeConstant(Math.E));
                }
                else
                {
                    var found = nodeParameters.FirstOrDefault(x => x.Parameter == parameter);

                    if (found != null)
                    {
                        found.Count++;
                        nodeStack.Push(found);
                    }
                    else
                    {
                        var nodeParameter = new NodeParameter(parameter, nodeParameters.Count, parameterExpression);
                        nodeParameters.Add(nodeParameter);
                        nodeStack.Push(nodeParameter);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Performs propagation of distributions set as arguments.
        /// </summary>
        /// <param name="arguments">Dictionary of pairs of distributions and arguments.</param>
        /// <param name="correlations">Correlation coefficients for distribution arguments.</param>
        /// <returns>Propagation result.</returns>
        public BaseDistribution EvaluateDistributions(Dictionary <string, BaseDistribution> arguments, List <CorrelatedPair> correlations)
        {
            correlations?.ForEach(x => x.Used = false);

            arguments = arguments ?? new Dictionary <string, BaseDistribution>();

            int length = nodeParameters.Count;

            for (int i = 0; i < length; i++)
            {
                NodeParameter parameter = nodeParameters[i];

                string arg = parameter.Parameter;

                if (parameter.Count > 1)
                {
                    throw new DistributionsInvalidOperationException(DistributionsInvalidOperationExceptionType.ImpossibeToUseRandomAlgebraParameterSetMoreThenOnce);
                }

                if (arguments.TryGetValue(arg, out BaseDistribution value))
                {
                    parameter.Value = value;
                }
                else
                {
                    throw new DistributionsArgumentException(DistributionsArgumentExceptionType.ParameterValueIsMissing, arg);
                }
            }

            BaseDistribution result = Parsed.EvaluateExtended(correlations);

            if (correlations.Any(x => !x.Used))
            {
                throw new DistributionsInvalidOperationException(DistributionsInvalidOperationExceptionType.CorrelationParamtersIgnored);
            }

            return(result);
        }