Пример #1
0
        public ProbabilityTable pointwiseProductPOS(ProbabilityTable multiplier, params IRandomVariable[] prodVarOrder)
        {
            ProbabilityTable product = new ProbabilityTable(prodVarOrder);

            if (!product.randomVarInfo.GetKeys()
                .SequenceEqual(
                    SetOps.union(CollectionFactory.CreateSet <IRandomVariable>(randomVarInfo.GetKeys()), CollectionFactory.CreateSet <IRandomVariable>(multiplier.randomVarInfo.GetKeys()))))
            {
                throw new IllegalArgumentException("Specified list deatailing order of mulitplier is inconsistent.");
            }

            // If no variables in the product
            if (1 == product.getValues().Length)
            {
                product.getValues()[0] = getValues()[0] * multiplier.getValues()[0];
            }
            else
            {
                // Otherwise need to iterate through the product
                // to calculate its values based on the terms.
                object[] term1Values        = new object[randomVarInfo.Size()];
                object[] term2Values        = new object[multiplier.randomVarInfo.Size()];
                ProbabilityTableIterator di = new ProbabilityTablecIteratorImpl3(term1Values,
                                                                                 term2Values, product, multiplier, this);
                product.iterateOverTable(di);
            }

            return(product);
        }
Пример #2
0
        public ProbabilityTable divideBy(ProbabilityTable divisor)
        {
            if (!randomVarInfo.GetKeys().ContainsAll(divisor.randomVarInfo.GetKeys()))
            {
                throw new IllegalArgumentException("Divisor must be a subset of the dividend.");
            }

            ProbabilityTable quotient = new ProbabilityTable(randomVarInfo.GetKeys());

            if (1 == divisor.getValues().Length)
            {
                double d = divisor.getValues()[0];
                for (int i = 0; i < quotient.getValues().Length; ++i)
                {
                    if (0 == d)
                    {
                        quotient.getValues()[i] = 0;
                    }
                    else
                    {
                        quotient.getValues()[i] = getValues()[i] / d;
                    }
                }
            }
            else
            {
                ISet <IRandomVariable> dividendDivisorDiff = SetOps
                                                             .difference(
                    CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys()),
                    CollectionFactory.CreateSet <IRandomVariable>(divisor.randomVarInfo.GetKeys()));
                IMap <IRandomVariable, RVInfo> tdiff = null;
                MixedRadixNumber tdMRN = null;
                if (dividendDivisorDiff.Size() > 0)
                {
                    tdiff = CollectionFactory.CreateInsertionOrderedMap <IRandomVariable, RVInfo>();
                    foreach (IRandomVariable rv in dividendDivisorDiff)
                    {
                        tdiff.Put(rv, new RVInfo(rv));
                    }
                    tdMRN = new MixedRadixNumber(0, createRadixs(tdiff));
                }
                IMap <IRandomVariable, RVInfo> diff      = tdiff;
                MixedRadixNumber         dMRN            = tdMRN;
                int[]                    qRVs            = new int[quotient.radices.Length];
                MixedRadixNumber         qMRN            = new MixedRadixNumber(0, quotient.radices);
                ProbabilityTableIterator divisorIterator = new ProbabilityTableIteratorImp2(quotient, qRVs, qMRN, dMRN, diff, this);

                divisor.iterateOverTable(divisorIterator);
            }

            return(quotient);
        }