public RatingStars() : base()
        {
            Grid grid = new Grid();

            grid.ColumnSpacing = 28;

            //Create Star Image Placeholders
            StarImages = new List <CachedImage>();
            for (int i = 0; i < 5; i++)
            {
                StarImages.Add(new CachedImage()
                {
                    HorizontalOptions = LayoutOptions.Start
                });

                // add image
                grid.Children.Add(StarImages[i], i + 1, 0);
            }

            // add slider for pan gesture
            Slider               = new TransparentSlider();
            Slider.Maximum       = Precision.Equals(PrecisionType.Half) ? 10 : 5;
            Slider.Opacity       = Device.OnPlatform(1, 0, 0);
            Slider.ValueChanged += OnRatingValueChanged;
            Grid.SetColumnSpan(Slider, 6);
            grid.Children.Add(Slider);

            this.Content = grid;
        }
Пример #2
0
 /// <summary>
 /// Show a message if the specified value is 0.
 /// </summary>
 /// <param name="value">The value to check.</param>
 /// <param name="precision">The acceptable difference when comparing the floating point value.</param>
 /// <param name="message">The message to show.</param>
 /// <param name="args">The arguments to the message to display.</param>
 /// <returns>
 /// True only if the assert condition passed. Otherwise false.
 /// </returns>
 public bool IsNotZero(double value, double precision, string message, params object[] args)
 {
     return(this.Check(
                AssertType.IsNotZero,
                !Precision.Equals(value, 0, precision),
                message,
                args,
                "IsNotZero check failed."));
 }
Пример #3
0
 /// <summary>
 /// Show a message if the specified values are equal.
 /// </summary>
 /// <param name="a">The first value to compare.</param>
 /// <param name="b">The second value to compare.</param>
 /// <param name="precision">The acceptable difference when comparing the floating point value.</param>
 /// <param name="message">The message to show.</param>
 /// <param name="args">The arguments to the message to display.</param>
 /// <returns>
 /// True only if the assert condition passed. Otherwise false.
 /// </returns>
 public bool AreNotEqual(double a, double b, double precision, string message, params object[] args)
 {
     return(this.Check(
                AssertType.AreNotEqual,
                !Precision.Equals(a, b, precision),
                message,
                args,
                "AreNotEqual check failed. [{0}, {1}]",
                a,
                b));
 }
Пример #4
0
 /// <summary>
 /// Show a message if the specified values are not equal.
 /// </summary>
 /// <param name="a">The first value to compare.</param>
 /// <param name="b">The second value to compare.</param>
 /// <param name="precision">The acceptable difference when comparing the floating point value.</param>
 /// <param name="message">The message to show.</param>
 /// <param name="args">The arguments to the message to display.</param>
 /// <returns>
 /// True only if the assert condition passed. Otherwise false.
 /// </returns>
 public bool AreEqual(float a, float b, float precision, string message, params object[] args)
 {
     return(this.Check(
                AssertType.AreEqual,
                Precision.Equals(a, b, precision),
                message,
                args,
                "AreEqual check failed. [{0}, {1}]",
                a,
                b));
 }
Пример #5
0
        /// <summary>
        /// Solves Phase 1 of the Simplex method.
        /// </summary>
        /// <param name="tableau"></param>
        protected void SolvePhase1(SimplexTableau <T, TPolicy> tableau)
        {
            // make sure we're in Phase 1
            if (tableau.NumArtificialVariables == 0)
            {
                return;
            }

            while (!tableau.IsOptimal())
            {
                DoIteration(tableau);
            }

            // if W is not zero then we have no feasible solution
            if (!Precision <T, TPolicy> .Equals(tableau.GetEntry(0, tableau.RhsOffset), Policy.Zero(), epsilon))
            {
                throw new NoFeasibleSolutionException();
            }
        }
Пример #6
0
        private ImageSource GetStarSource(int position)
        {
            int currentStarMaxRating = (position + 1);

            if (Precision.Equals(PrecisionType.Half))
            {
                currentStarMaxRating *= 2;
            }

            if (Rating >= currentStarMaxRating)
            {
                return(ImageFullStar);
            }
            else if (Rating >= currentStarMaxRating - 1 && Precision.Equals(PrecisionType.Half))
            {
                return(ImageHalfStar);
            }
            else
            {
                return(ImageEmptyStar);
            }
        }
        public static bool Compare(this CompareOperation operation, float a, float b, float precision)
        {
            switch (operation)
            {
            case CompareOperation.Equal:
                return(Precision.Equals(a, b, precision));

            case CompareOperation.Greater:
                return(Precision.CompareTo(a, b, precision) > 0);

            case CompareOperation.GreaterEqual:
                return(Precision.CompareTo(a, b, precision) >= 0);

            case CompareOperation.Less:
                return(Precision.CompareTo(a, b, precision) < 0);

            case CompareOperation.LessEqual:
                return(Precision.CompareTo(a, b, precision) <= 0);

            default:
                throw new ArgumentOutOfRangeException(nameof(operation), operation, null);
            }
        }
        /// <summary>
        /// {@inheritDoc}
        /// </summary>
        protected internal override double DoSolve()
        {
            // prepare arrays with the first points
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] x = new double[maximalOrder + 1];
            double[] x = new double[maximalOrder + 1];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] y = new double[maximalOrder + 1];
            double[] y = new double[maximalOrder + 1];
            x[0] = GetMin();
            x[1] = GetStartValue();
            x[2] = GetMax();
            VerifySequence(x[0], x[1], x[2]);

            // evaluate initial guess
            y[1] = ComputeObjectiveValue(x[1]);
            if (Precision.Equals(y[1], 0.0, 1))
            {
                // return the initial guess if it is a perfect root.
                return(x[1]);
            }

            // evaluate first  endpoint
            y[0] = ComputeObjectiveValue(x[0]);
            if (Precision.Equals(y[0], 0.0, 1))
            {
                // return the first endpoint if it is a perfect root.
                return(x[0]);
            }

            int nbPoints;
            int signChangeIndex;

            if (y[0] * y[1] < 0)
            {
                // reduce interval if it brackets the root
                nbPoints        = 2;
                signChangeIndex = 1;
            }
            else
            {
                // evaluate second endpoint
                y[2] = ComputeObjectiveValue(x[2]);
                if (Precision.Equals(y[2], 0.0, 1))
                {
                    // return the second endpoint if it is a perfect root.
                    return(x[2]);
                }

                if (y[1] * y[2] < 0)
                {
                    // use all computed point as a start sampling array for solving
                    nbPoints        = 3;
                    signChangeIndex = 2;
                }
                else
                {
                    throw new NoBracketingException(x[0], x[2], y[0], y[2]);
                }
            }

            // prepare a work array for inverse polynomial interpolation
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] tmpX = new double[x.length];
            double[] tmpX = new double[x.Length];

            // current tightest bracketing of the root
            double xA     = x[signChangeIndex - 1];
            double yA     = y[signChangeIndex - 1];
            double absYA  = FastMath.Abs(yA);
            int    agingA = 0;
            double xB     = x[signChangeIndex];
            double yB     = y[signChangeIndex];
            double absYB  = FastMath.Abs(yB);
            int    agingB = 0;

            // search loop
            while (true)
            {
                // check convergence of bracketing interval
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double xTol = getAbsoluteAccuracy() + getRelativeAccuracy() * org.apache.commons.math3.util.FastMath.max(org.apache.commons.math3.util.FastMath.abs(xA), org.apache.commons.math3.util.FastMath.abs(xB));
                double xTol = GetAbsoluteAccuracy() + GetRelativeAccuracy() * FastMath.Max(FastMath.Abs(xA), FastMath.Abs(xB));
                if (((xB - xA) <= xTol) || (FastMath.Max(absYA, absYB) < GetFunctionValueAccuracy()))
                {
                    switch (allowed)
                    {
                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.ANY_SIDE:
                        return(absYA < absYB ? xA : xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.LEFT_SIDE:
                        return(xA);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.RIGHT_SIDE:
                        return(xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.BELOW_SIDE:
                        return((yA <= 0) ? xA : xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.ABOVE_SIDE:
                        return((yA < 0) ? xB : xA);

                    default:
                        // this should never happen
                        throw new MathInternalError();
                    }
                }

                // target for the next evaluation point
                double targetY;
                if (agingA >= MAXIMAL_AGING)
                {
                    // we keep updating the high bracket, try to compensate this
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int p = agingA - MAXIMAL_AGING;
                    int p = agingA - MAXIMAL_AGING;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double weightA = (1 << p) - 1;
                    double weightA = (1 << p) - 1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double weightB = p + 1;
                    double weightB = p + 1;
                    targetY = (weightA * yA - weightB * REDUCTION_FACTOR * yB) / (weightA + weightB);
                }
                else if (agingB >= MAXIMAL_AGING)
                {
                    // we keep updating the low bracket, try to compensate this
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int p = agingB - MAXIMAL_AGING;
                    int p = agingB - MAXIMAL_AGING;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double weightA = p + 1;
                    double weightA = p + 1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double weightB = (1 << p) - 1;
                    double weightB = (1 << p) - 1;
                    targetY = (weightB * yB - weightA * REDUCTION_FACTOR * yA) / (weightA + weightB);
                }
                else
                {
                    // bracketing is balanced, try to find the root itself
                    targetY = 0;
                }

                // make a few attempts to guess a root,
                double nextX;
                int    start = 0;
                int    end   = nbPoints;
                do
                {
                    // guess a value for current target, using inverse polynomial interpolation
                    Array.Copy(x, start, tmpX, start, end - start);
                    nextX = GuessX(targetY, tmpX, y, start, end);

                    if (!((nextX > xA) && (nextX < xB)))
                    {
                        // the guessed root is not strictly inside of the tightest bracketing interval

                        // the guessed root is either not strictly inside the interval or it
                        // is a NaN (which occurs when some sampling points share the same y)
                        // we try again with a lower interpolation order
                        if (signChangeIndex - start >= end - signChangeIndex)
                        {
                            // we have more points before the sign change, drop the lowest point
                            ++start;
                        }
                        else
                        {
                            // we have more points after sign change, drop the highest point
                            --end;
                        }

                        // we need to do one more attempt
                        nextX = Double.NaN;
                    }
                } while (double.IsNaN(nextX) && (end - start > 1));

                if (double.IsNaN(nextX))
                {
                    // fall back to bisection
                    nextX = xA + 0.5 * (xB - xA);
                    start = signChangeIndex - 1;
                    end   = signChangeIndex;
                }

                // evaluate the function at the guessed root
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double nextY = computeObjectiveValue(nextX);
                double nextY = ComputeObjectiveValue(nextX);
                if (Precision.Equals(nextY, 0.0, 1))
                {
                    // we have found an exact root, since it is not an approximation
                    // we don't need to bother about the allowed solutions setting
                    return(nextX);
                }

                if ((nbPoints > 2) && (end - start != nbPoints))
                {
                    // we have been forced to ignore some points to keep bracketing,
                    // they are probably too far from the root, drop them from now on
                    nbPoints = end - start;
                    Array.Copy(x, start, x, 0, nbPoints);
                    Array.Copy(y, start, y, 0, nbPoints);
                    signChangeIndex -= start;
                }
                else if (nbPoints == x.Length)
                {
                    // we have to drop one point in order to insert the new one
                    nbPoints--;

                    // keep the tightest bracketing interval as centered as possible
                    if (signChangeIndex >= (x.Length + 1) / 2)
                    {
                        // we drop the lowest point, we have to shift the arrays and the index
                        Array.Copy(x, 1, x, 0, nbPoints);
                        Array.Copy(y, 1, y, 0, nbPoints);
                        --signChangeIndex;
                    }
                }

                // insert the last computed point
                //(by construction, we know it lies inside the tightest bracketing interval)
                Array.Copy(x, signChangeIndex, x, signChangeIndex + 1, nbPoints - signChangeIndex);
                x[signChangeIndex] = nextX;
                Array.Copy(y, signChangeIndex, y, signChangeIndex + 1, nbPoints - signChangeIndex);
                y[signChangeIndex] = nextY;
                ++nbPoints;

                // update the bracketing interval
                if (nextY * yA <= 0)
                {
                    // the sign change occurs before the inserted point
                    xB    = nextX;
                    yB    = nextY;
                    absYB = FastMath.Abs(yB);
                    ++agingA;
                    agingB = 0;
                }
                else
                {
                    // the sign change occurs after the inserted point
                    xA     = nextX;
                    yA     = nextY;
                    absYA  = FastMath.Abs(yA);
                    agingA = 0;
                    ++agingB;

                    // update the sign change index
                    signChangeIndex++;
                }
            }
        }
Пример #9
0
 public void Equals_double(double a, double b, double precision, int ulp, bool expected)
 {
     Validate.Value.AreEqual(Precision.Equals(a, b, precision), expected);
     Validate.Value.AreEqual(Precision.Equals(a, b, ulp), expected);
 }
Пример #10
0
 public void Equals_float(float a, float b, float precision, int ulp, bool expected)
 {
     Validate.Value.AreEqual(Precision.Equals(a, b, precision), expected);
     Validate.Value.AreEqual(Precision.Equals(a, b, ulp), expected);
 }
        /// <summary>
        /// Solve for a zero in the given interval, start at {@code startValue}.
        /// A solver may require that the interval brackets a single zero root.
        /// Solvers that do require bracketing should be able to handle the case
        /// where one of the endpoints is itself a root.
        /// </summary>
        /// <param name="maxEval"> Maximum number of evaluations. </param>
        /// <param name="f"> Function to solve. </param>
        /// <param name="min"> Lower bound for the interval. </param>
        /// <param name="max"> Upper bound for the interval. </param>
        /// <param name="startValue"> Start value to use. </param>
        /// <param name="allowedSolution"> The kind of solutions that the root-finding algorithm may
        /// accept as solutions. </param>
        /// <returns> a value where the function is zero. </returns>
        /// <exception cref="NullArgumentException"> if f is null. </exception>
        /// <exception cref="NoBracketingException"> if root cannot be bracketed </exception>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: public T solve(final int maxEval, final org.apache.commons.math3.analysis.RealFieldUnivariateFunction<T> f, final T min, final T max, final T startValue, final AllowedSolution allowedSolution) throws org.apache.commons.math3.exception.NullArgumentException, org.apache.commons.math3.exception.NoBracketingException
        public virtual T Solve(int maxEval, RealFieldUnivariateFunction <T> f, T min, T max, T startValue, AllowedSolution allowedSolution)
        {
            // Checks.
            MathUtils.CheckNotNull(f);

            // Reset.
            evaluations = evaluations.WithMaximalCount(maxEval).withStart(0);
            T zero = field.GetZero();
            T nan  = zero.add(Double.NaN);

            // prepare arrays with the first points
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final T[] x = org.apache.commons.math3.util.MathArrays.buildArray(field, maximalOrder + 1);
            T[] x = MathArrays.BuildArray(field, maximalOrder + 1);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final T[] y = org.apache.commons.math3.util.MathArrays.buildArray(field, maximalOrder + 1);
            T[] y = MathArrays.BuildArray(field, maximalOrder + 1);
            x[0] = min;
            x[1] = startValue;
            x[2] = max;

            // evaluate initial guess
            evaluations.Increment();
            y[1] = f.Value(x[1]);
            if (Precision.Equals(y[1].getReal(), 0.0, 1))
            {
                // return the initial guess if it is a perfect root.
                return(x[1]);
            }

            // evaluate first endpoint
            evaluations.Increment();
            y[0] = f.Value(x[0]);
            if (Precision.Equals(y[0].getReal(), 0.0, 1))
            {
                // return the first endpoint if it is a perfect root.
                return(x[0]);
            }

            int nbPoints;
            int signChangeIndex;

            if (y[0].multiply(y[1]).getReal() < 0)
            {
                // reduce interval if it brackets the root
                nbPoints        = 2;
                signChangeIndex = 1;
            }
            else
            {
                // evaluate second endpoint
                evaluations.Increment();
                y[2] = f.Value(x[2]);
                if (Precision.Equals(y[2].getReal(), 0.0, 1))
                {
                    // return the second endpoint if it is a perfect root.
                    return(x[2]);
                }

                if (y[1].multiply(y[2]).getReal() < 0)
                {
                    // use all computed point as a start sampling array for solving
                    nbPoints        = 3;
                    signChangeIndex = 2;
                }
                else
                {
                    throw new NoBracketingException(x[0].getReal(), x[2].getReal(), y[0].getReal(), y[2].getReal());
                }
            }

            // prepare a work array for inverse polynomial interpolation
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final T[] tmpX = org.apache.commons.math3.util.MathArrays.buildArray(field, x.length);
            T[] tmpX = MathArrays.BuildArray(field, x.Length);

            // current tightest bracketing of the root
            T   xA     = x[signChangeIndex - 1];
            T   yA     = y[signChangeIndex - 1];
            T   absXA  = xA.abs();
            T   absYA  = yA.abs();
            int agingA = 0;
            T   xB     = x[signChangeIndex];
            T   yB     = y[signChangeIndex];
            T   absXB  = xB.abs();
            T   absYB  = yB.abs();
            int agingB = 0;

            // search loop
            while (true)
            {
                // check convergence of bracketing interval
                T maxX = absXA.subtract(absXB).getReal() < 0 ? absXB : absXA;
                T maxY = absYA.subtract(absYB).getReal() < 0 ? absYB : absYA;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final T xTol = absoluteAccuracy.add(relativeAccuracy.multiply(maxX));
                T xTol = absoluteAccuracy.add(relativeAccuracy.multiply(maxX));
                if (xB.subtract(xA).subtract(xTol).getReal() <= 0 || maxY.subtract(functionValueAccuracy).getReal() < 0)
                {
                    switch (allowedSolution)
                    {
                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.ANY_SIDE:
                        return(absYA.subtract(absYB).getReal() < 0 ? xA : xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.LEFT_SIDE:
                        return(xA);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.RIGHT_SIDE:
                        return(xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.BELOW_SIDE:
                        return(yA.getReal() <= 0 ? xA : xB);

                    case org.apache.commons.math3.analysis.solvers.AllowedSolution.ABOVE_SIDE:
                        return(yA.getReal() < 0 ? xB : xA);

                    default:
                        // this should never happen
                        throw new MathInternalError(null);
                    }
                }

                // target for the next evaluation point
                T targetY;
                if (agingA >= MAXIMAL_AGING)
                {
                    // we keep updating the high bracket, try to compensate this
                    targetY = yB.divide(16).negate();
                }
                else if (agingB >= MAXIMAL_AGING)
                {
                    // we keep updating the low bracket, try to compensate this
                    targetY = yA.divide(16).negate();
                }
                else
                {
                    // bracketing is balanced, try to find the root itself
                    targetY = zero;
                }

                // make a few attempts to guess a root,
                T   nextX;
                int start = 0;
                int end   = nbPoints;
                do
                {
                    // guess a value for current target, using inverse polynomial interpolation
                    Array.Copy(x, start, tmpX, start, end - start);
                    nextX = GuessX(targetY, tmpX, y, start, end);

                    if (!((nextX.subtract(xA).getReal() > 0) && (nextX.subtract(xB).getReal() < 0)))
                    {
                        // the guessed root is not strictly inside of the tightest bracketing interval

                        // the guessed root is either not strictly inside the interval or it
                        // is a NaN (which occurs when some sampling points share the same y)
                        // we try again with a lower interpolation order
                        if (signChangeIndex - start >= end - signChangeIndex)
                        {
                            // we have more points before the sign change, drop the lowest point
                            ++start;
                        }
                        else
                        {
                            // we have more points after sign change, drop the highest point
                            --end;
                        }

                        // we need to do one more attempt
                        nextX = nan;
                    }
                } while (double.IsNaN(nextX.getReal()) && (end - start > 1));

                if (double.IsNaN(nextX.getReal()))
                {
                    // fall back to bisection
                    nextX = xA.add(xB.subtract(xA).divide(2));
                    start = signChangeIndex - 1;
                    end   = signChangeIndex;
                }

                // evaluate the function at the guessed root
                evaluations.Increment();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final T nextY = f.value(nextX);
                T nextY = f.Value(nextX);
                if (Precision.Equals(nextY.getReal(), 0.0, 1))
                {
                    // we have found an exact root, since it is not an approximation
                    // we don't need to bother about the allowed solutions setting
                    return(nextX);
                }

                if ((nbPoints > 2) && (end - start != nbPoints))
                {
                    // we have been forced to ignore some points to keep bracketing,
                    // they are probably too far from the root, drop them from now on
                    nbPoints = end - start;
                    Array.Copy(x, start, x, 0, nbPoints);
                    Array.Copy(y, start, y, 0, nbPoints);
                    signChangeIndex -= start;
                }
                else if (nbPoints == x.Length)
                {
                    // we have to drop one point in order to insert the new one
                    nbPoints--;

                    // keep the tightest bracketing interval as centered as possible
                    if (signChangeIndex >= (x.Length + 1) / 2)
                    {
                        // we drop the lowest point, we have to shift the arrays and the index
                        Array.Copy(x, 1, x, 0, nbPoints);
                        Array.Copy(y, 1, y, 0, nbPoints);
                        --signChangeIndex;
                    }
                }

                // insert the last computed point
                //(by construction, we know it lies inside the tightest bracketing interval)
                Array.Copy(x, signChangeIndex, x, signChangeIndex + 1, nbPoints - signChangeIndex);
                x[signChangeIndex] = nextX;
                Array.Copy(y, signChangeIndex, y, signChangeIndex + 1, nbPoints - signChangeIndex);
                y[signChangeIndex] = nextY;
                ++nbPoints;

                // update the bracketing interval
                if (nextY.multiply(yA).getReal() <= 0)
                {
                    // the sign change occurs before the inserted point
                    xB    = nextX;
                    yB    = nextY;
                    absYB = yB.abs();
                    ++agingA;
                    agingB = 0;
                }
                else
                {
                    // the sign change occurs after the inserted point
                    xA     = nextX;
                    yA     = nextY;
                    absYA  = yA.abs();
                    agingA = 0;
                    ++agingB;

                    // update the sign change index
                    signChangeIndex++;
                }
            }
        }
Пример #12
0
        /// <summary>
        /// Returns the row with the minimum ratio as given by the minimum ratio test (MRT).
        /// </summary>
        /// <param name="tableau"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        private int?GetPivotRow(SimplexTableau <T, TPolicy> tableau, int col)
        {
            // create a list of all the rows that tie for the lowest score in the minimum ratio test
            List <int?> minRatioPositions   = new List <int?>();
            T           minRatio            = default(T);
            bool        minRationUnassigned = true;

            for (int i = tableau.NumObjectiveFunctions; i < tableau.Height; i++)
            {
                T rhs   = tableau.GetEntry(i, tableau.Width - 1);
                T entry = tableau.GetEntry(i, col);

                // only consider pivot elements larger than the cutOff threshold
                // selecting others may lead to degeneracy or numerical instabilities
                if (Precision <T, TPolicy> .CompareTo(entry, Policy.Zero(), cutOff) > 0)
                {
                    T ratio = Policy.Abs(Policy.Div(rhs, entry));
                    // check if the entry is strictly equal to the current min ratio
                    // do not use a ulp/epsilon check
                    int cmp;
                    if (minRationUnassigned)
                    {
                        cmp = -1;
                    }
                    else
                    {
                        cmp = Policy.Compare(ratio, minRatio);
                    }
                    if (cmp == 0)
                    {
                        minRatioPositions.Add(i);
                    }
                    else if (cmp < 0)
                    {
                        minRatio            = ratio;
                        minRationUnassigned = false;
                        minRatioPositions.Clear();
                        minRatioPositions.Add(i);
                    }
                }
            }

            if (minRatioPositions.Count == 0)
            {
                return(null);
            }
            else if (minRatioPositions.Count > 1)
            {
                // there's a degeneracy as indicated by a tie in the minimum ratio test

                // 1. check if there's an artificial variable that can be forced out of the basis
                if (tableau.NumArtificialVariables > 0)
                {
                    foreach (int?row in minRatioPositions)
                    {
                        for (int i = 0; i < tableau.NumArtificialVariables; i++)
                        {
                            int column = i + tableau.ArtificialVariableOffset;
                            T   entry  = tableau.GetEntry(row.Value, column);
                            if (Precision <T, TPolicy> .Equals(entry, Policy.One(), epsilon) && row.Equals(tableau.GetBasicRow(column)))
                            {
                                return(row);
                            }
                        }
                    }
                }

                // 2. apply Bland's rule to prevent cycling:
                //    take the row for which the corresponding basic variable has the smallest index
                //
                // see http://www.stanford.edu/class/msande310/blandrule.pdf
                // see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper)

                int?minRow   = null;
                int minIndex = tableau.Width;
                foreach (int?row in minRatioPositions)
                {
                    int basicVar = tableau.GetBasicVariable(row.Value);
                    if (basicVar < minIndex)
                    {
                        minIndex = basicVar;
                        minRow   = row;
                    }
                }
                return(minRow);
            }
            return(minRatioPositions[0]);
        }
Пример #13
0
        /// <summary>
        /// Returns true if CoinInfo instances are equal
        /// </summary>
        /// <param name="other">Instance of CoinInfo to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(CoinInfo other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return
                ((
                     CoinType == other.CoinType ||
                     CoinType != null &&
                     CoinType.Equals(other.CoinType)
                     ) &&
                 (
                     WalletName == other.WalletName ||
                     WalletName != null &&
                     WalletName.Equals(other.WalletName)
                 ) &&
                 (
                     Name == other.Name ||
                     Name != null &&
                     Name.Equals(other.Name)
                 ) &&
                 (
                     Symbol == other.Symbol ||
                     Symbol != null &&
                     Symbol.Equals(other.Symbol)
                 ) &&
                 (
                     WalletSymbol == other.WalletSymbol ||
                     WalletSymbol != null &&
                     WalletSymbol.Equals(other.WalletSymbol)
                 ) &&
                 (
                     WalletType == other.WalletType ||
                     WalletType != null &&
                     WalletType.Equals(other.WalletType)
                 ) &&
                 (
                     TransactionFee == other.TransactionFee ||
                     TransactionFee != null &&
                     TransactionFee.Equals(other.TransactionFee)
                 ) &&
                 (
                     Precision == other.Precision ||
                     Precision != null &&
                     Precision.Equals(other.Precision)
                 ) &&
                 (
                     BackingCoinType == other.BackingCoinType ||
                     BackingCoinType != null &&
                     BackingCoinType.Equals(other.BackingCoinType)
                 ) &&
                 (
                     SupportsOutputMemos == other.SupportsOutputMemos ||
                     SupportsOutputMemos != null &&
                     SupportsOutputMemos.Equals(other.SupportsOutputMemos)
                 ) &&
                 (
                     Restricted == other.Restricted ||
                     Restricted != null &&
                     Restricted.Equals(other.Restricted)
                 ) &&
                 (
                     Authorized == other.Authorized ||
                     Authorized != null &&
                     Authorized.Equals(other.Authorized)
                 ) &&
                 (
                     NotAuthorizedReasons == other.NotAuthorizedReasons ||
                     NotAuthorizedReasons != null &&
                     NotAuthorizedReasons.SequenceEqual(other.NotAuthorizedReasons)
                 ));
        }
 /// <inheritdoc />
 public bool Equals(WbGlobeCoordinate other)
 {
     return(Latitude.Equals(other.Latitude) && Longitude.Equals(other.Longitude) &&
            Precision.Equals(other.Precision) && Globe == other.Globe);
 }