示例#1
0
                public MultiRepresentationWeightFunction <TDictionary> ConstantLog(double logValue, TElementDistribution allowedElements)
                {
                    if (double.IsNegativeInfinity(allowedElements.GetLogAverageOf(allowedElements)))
                    {
                        return(Zero());
                    }
                    var automaton = new TAutomaton();

                    automaton.SetToConstantLog(logValue, allowedElements);
                    return(FromAutomaton(automaton));
                }
示例#2
0
                public MultiRepresentationWeightFunction <TDictionary> ConstantOnSupportOfLog(double logValue, MultiRepresentationWeightFunction <TDictionary> weightFunction)
                {
                    if (weightFunction.TryEnumerateSupport(MaxDictionarySize, out var support, false))
                    {
                        if (!support.Any())
                        {
                            return(Zero());
                        }

                        if (logValue == 0 && !support.Skip(1).Any())
                        {
                            return(FromPoint(support.Single()));
                        }

                        var weight = Weight.FromLogValue(logValue);
                        return(FromDictionary(DictionaryWeightFunction <TDictionary> .FromDistinctWeights(
                                                  support.Select(sequence => new KeyValuePair <TSequence, Weight>(sequence, weight)))));
                    }
                    var automaton = new TAutomaton();

                    automaton.SetToConstantOnSupportOfLog(logValue, weightFunction.AsAutomaton());
                    return(FromAutomaton(automaton));
                }
示例#3
0
 public MultiRepresentationWeightFunction <TDictionary> FromAutomaton(TAutomaton automaton) =>
 MultiRepresentationWeightFunction <TDictionary> .FromAutomaton(automaton);
示例#4
0
 public static MultiRepresentationWeightFunction <TDictionary> FromAutomaton(TAutomaton automaton) =>
 new MultiRepresentationWeightFunction <TDictionary>()
 {
     weightFunction = automaton
 };
示例#5
0
                public MultiRepresentationWeightFunction <TDictionary> Sum(IEnumerable <MultiRepresentationWeightFunction <TDictionary> > weightFunctions)
                {
                    var  dictionary           = new Dictionary <TSequence, Weight>(MaxDictionarySize, SequenceManipulator.SequenceEqualityComparer);
                    bool resultFitsDictionary = true;

                    foreach (var weightFunction in weightFunctions)
                    {
                        if (weightFunction.IsCanonicZero())
                        {
                            continue;
                        }
                        if (weightFunction.weightFunction is PointMassWeightFunction pointMass)
                        {
                            if (dictionary.TryGetValue(pointMass.Point, out Weight oldWeight))
                            {
                                dictionary[pointMass.Point] = oldWeight + Weight.One;
                            }
                            else if (dictionary.Count < MaxDictionarySize)
                            {
                                dictionary.Add(pointMass.Point, Weight.One);
                            }
                            else
                            {
                                resultFitsDictionary = false;
                                break;
                            }
                        }
                        else if (weightFunction.weightFunction is TDictionary wfDictionary)
                        {
                            foreach (var kvp in wfDictionary.Dictionary)
                            {
                                if (dictionary.TryGetValue(kvp.Key, out Weight oldWeight))
                                {
                                    dictionary[kvp.Key] = oldWeight + kvp.Value;
                                }
                                else if (dictionary.Count < MaxDictionarySize)
                                {
                                    dictionary.Add(kvp.Key, kvp.Value);
                                }
                                else
                                {
                                    resultFitsDictionary = false;
                                    break;
                                }
                            }
                            if (!resultFitsDictionary)
                            {
                                break;
                            }
                        }
                        else
                        {
                            resultFitsDictionary = false;
                            break;
                        }
                    }

                    if (resultFitsDictionary)
                    {
                        if (dictionary.Count == 0)
                        {
                            return(Zero());
                        }
                        if (dictionary.Count == 1)
                        {
                            var singleKvp = dictionary.Single();
                            if (singleKvp.Value.LogValue == 0.0)
                            {
                                return(FromPoint(singleKvp.Key));
                            }
                        }
                        return(FromDictionary(DictionaryWeightFunction <TDictionary> .FromDistinctWeights(dictionary)));
                    }

                    var automaton = new TAutomaton();

                    automaton.SetToSum(weightFunctions.Select(wf => wf.AsAutomaton()));
                    return(FromAutomaton(automaton));
                }