Beispiel #1
0
            public MultiRepresentationWeightFunction <TDictionary> Product(MultiRepresentationWeightFunction <TDictionary> weightFunction)
            {
                if (IsCanonicZero() || weightFunction.IsCanonicZero())
                {
                    return(Zero());
                }

                PointMassWeightFunction pointMass = null;
                IWeightFunction         other     = null;

                if (this.weightFunction is PointMassWeightFunction thisPointMass)
                {
                    pointMass = thisPointMass;
                    other     = weightFunction.weightFunction;
                }
                else if (weightFunction.weightFunction is PointMassWeightFunction otherPointMass)
                {
                    pointMass = otherPointMass;
                    other     = this.weightFunction;
                }
                if (pointMass != null && !other.UsesGroups)
                {
                    var logValue = other.GetLogValue(pointMass.Point);
                    if (double.IsNegativeInfinity(logValue))
                    {
                        return(Zero());
                    }
                    else if (logValue == 0.0)
                    {
                        return(FromPointMass(pointMass));
                    }
                    else
                    {
                        return(FromDictionary(
                                   DictionaryWeightFunction <TDictionary> .FromDistinctWeights(
                                       new[] { new KeyValuePair <TSequence, Weight>(pointMass.Point, Weight.FromLogValue(logValue)) })));
                    }
                }

                TDictionary dictionary = null;

                if (this.weightFunction is TDictionary thisDictionary)
                {
                    if (weightFunction.weightFunction is TDictionary secondDictionary)
                    {
                        return(FromDictionary(thisDictionary.Product(secondDictionary)));
                    }

                    dictionary = thisDictionary;
                    other      = weightFunction.weightFunction;
                }
                else if (weightFunction.weightFunction is TDictionary otherDictionary)
                {
                    dictionary = otherDictionary;
                    other      = this.weightFunction;
                }

                if (dictionary != null && !other.UsesGroups)
                {
                    var resultList = new List <KeyValuePair <TSequence, Weight> >(dictionary.Dictionary.Count);
                    foreach (var kvp in dictionary.Dictionary)
                    {
                        if (!kvp.Value.IsZero)
                        {
                            var otherLogValue = other.GetLogValue(kvp.Key);
                            if (!double.IsNegativeInfinity(otherLogValue))
                            {
                                resultList.Add(new KeyValuePair <TSequence, Weight>(kvp.Key, kvp.Value * Weight.FromLogValue(otherLogValue)));
                            }
                        }
                    }
                    if (resultList.Count == 0)
                    {
                        return(Zero());
                    }
                    else if (resultList.Count == 1 && resultList[0].Value.LogValue == 0.0)
                    {
                        return(FromPoint(resultList[0].Key));
                    }
                    else
                    {
                        return(FromDictionary(
                                   DictionaryWeightFunction <TDictionary> .FromDistinctWeights(resultList)));
                    }
                }

                return(FromAutomaton(AsAutomaton().Product(weightFunction.AsAutomaton())));
            }
Beispiel #2
0
 public double GetLogValue(TSequence sequence) => weightFunction?.GetLogValue(sequence) ?? double.NegativeInfinity;