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);
        }
        public ProbabilityTable sumOut(params IRandomVariable[] vars)
        {
            ISet <IRandomVariable> soutVars = CollectionFactory.CreateSet <IRandomVariable>(this.randomVarInfo.GetKeys());

            foreach (IRandomVariable rv in vars)
            {
                soutVars.Remove(rv);
            }
            ProbabilityTable summedOut = new ProbabilityTable(soutVars);

            if (1 == summedOut.getValues().Length)
            {
                summedOut.getValues()[0] = getSum();
            }
            else
            {
                // Otherwise need to iterate through this distribution
                // to calculate the summed out distribution.
                object[] termValues         = new object[summedOut.randomVarInfo.Size()];
                ProbabilityTableIterator di = new ProbabilityTableIteratorImpl(summedOut, termValues);

                iterateOverTable(di);
            }

            return(summedOut);
        }
        public ProbabilityTable pointwiseProduct(ProbabilityTable multiplier)
        {
            ISet <IRandomVariable> prodVars = SetOps.union(CollectionFactory.CreateSet <IRandomVariable>(randomVarInfo.GetKeys()),
                                                           CollectionFactory.CreateSet <IRandomVariable>(multiplier.randomVarInfo.GetKeys()));

            return(pointwiseProductPOS(multiplier, prodVars.ToArray()));
        }
 public ProbabilityTablecIteratorImpl3(object[] term1Values, object[] term2Values, ProbabilityTable product, ProbabilityTable multiplier, ProbabilityTable probabilityTable)
 {
     this.term1Values      = term1Values;
     this.term2Values      = term2Values;
     this.product          = product;
     this.multiplier       = multiplier;
     this.probabilityTable = probabilityTable;
 }
 public ProbabilityTableIteratorImp2(ProbabilityTable quotient, int[] qRVs, MixedRadixNumber qMRN, MixedRadixNumber dMRN, IMap <IRandomVariable, RVInfo> diff, ProbabilityTable probabilityTable)
 {
     this.quotient         = quotient;
     this.qRVs             = qRVs;
     this.qMRN             = qMRN;
     this.dMRN             = dMRN;
     this.diff             = diff;
     this.probabilityTable = probabilityTable;
 }
        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);
        }
            private int termIdx(object[] termValues, ProbabilityTable d, IMap <IRandomVariable, object> possibleWorld)
            {
                if (0 == termValues.Length)
                {
                    // The term has no variables so always position 0.
                    return(0);
                }

                int i = 0;

                foreach (IRandomVariable rv in d.randomVarInfo.GetKeys())
                {
                    termValues[i] = possibleWorld.Get(rv);
                    ++i;
                }

                return(d.getIndex(termValues));
            }
 public ProbabilityTableIteratorImpl(ProbabilityTable summedOut, object[] termValues)
 {
     this.summedOut  = summedOut;
     this.termValues = termValues;
 }