internal Interval EvalArray(SparseRationalArray array, Dictionary <int, Variable> dimsToVar, int keyForConstant)
        {
            Interval i;
            int      j, k;

            return(EvalArray(array, dimsToVar, keyForConstant, out i, out j, out k));
        }
        /// <summary>
        /// Evaluating a row from Karr with the map <paramref name="varsToDimensions"/>. Given a row a1*x1 + ... + an*xn = b, it evaluates a1*x1 + ... + an*xn - b
        /// </summary>
        /// <param name="array">The row to evaluate</param>
        /// <param name="varsToDimensions">The bijective map from variables to positions</param>
        /// <param name="keyForConstant">The last index of the row</param>
        internal Interval EvalArray(SparseRationalArray array, Dictionary <int, Variable> dimsToVar,
                                    int keyForConstant, out Interval finiteBounds, out int infLower, out int infUpper)
        {
            infLower     = 0;
            infUpper     = 0;
            finiteBounds = Interval.For(0);
            var tmpValue = Interval.For(0);

            foreach (var pair in array.GetElements())
            {
                Interval tmpInt;
                if (pair.Key == keyForConstant)
                {
                    tmpInt = Interval.For(-pair.Value); // take the opposite as we want to evaluate f(x) = 0
                }
                else if (!this.TryGetValue(dimsToVar[pair.Key], out tmpInt))
                {
                    tmpInt = Interval.UnknownInterval;
                }
                else
                {
                    tmpInt = pair.Value * tmpInt;
                }
                tmpValue = tmpValue + tmpInt;

                var newLower = finiteBounds.LowerBound;
                var newUpper = finiteBounds.UpperBound;

                if (tmpInt.UpperBound.IsPlusInfinity)
                {
                    infUpper++;
                }
                else
                {
                    if (!Rational.TryAdd(newUpper, tmpInt.UpperBound, out newUpper))
                    {
                        newUpper = Rational.PlusInfinity;
                    }
                }
                if (tmpInt.LowerBound.IsMinusInfinity)
                {
                    infLower++;
                }
                else
                {
                    if (!Rational.TryAdd(newLower, tmpInt.LowerBound, out newLower))
                    {
                        newLower = Rational.MinusInfinity;
                    }
                }
                finiteBounds = Interval.For(newLower, newUpper);
            }

            return(tmpValue);
        }
Пример #3
0
        public SparseRationalArray[] GetClonedEquations()
        {
            var result = new SparseRationalArray[matrix.Length];

            for (int row = 0; row < matrix.Length; row++)
            {
                var currRow = matrix[row];
                if (currRow != null)
                {
                    result[row] = SparseRationalArray.New(currRow.Length, 0);
                    result[row].CopyFrom(currRow, currRow.Length);
                }
                else
                {
                    result[row] = null;
                }
            }

            return(result);
        }
Пример #4
0
        static public bool IsConstantRow(this SparseRationalArray row)
        {
            Contract.Requires(row != null);

            int count = row.Count;

            switch (count)
            {
            case 0:
            case 1:
                return(true);

            case 2:
                Contract.Assume(row.Length >= 1); // There is an invariant on sparse arrays: Length >= Count
                return(row.IsIndexOfNonDefaultElement(row.Length - 1));

            default:
                return(false);
            }
        }
Пример #5
0
 public void CopyRowTo(int from, SparseRationalArray to, int len)
 {
     to.CopyFrom(matrix[from], len);
 }