Пример #1
0
 private void GetByIndexButton_Click(object sender, EventArgs e)
 {
     try
     {
         long     index  = Convert.ToInt64(IndexBox.Text);
         Vector2D result = MinimalEquation.ByIndex(index);
         XBox.Text = result.x.ToString();
         YBox.Text = result.y.ToString();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error");
     }
 }
Пример #2
0
        public static BoundPair GetByX(double x)
        {
            long lowerIndex = 0;
            long upperIndex = -1;

            Vector2D lower           = MinimalEquation.ByIndex(lowerIndex);
            Vector2D upper           = MinimalEquation.ByIndex(upperIndex);
            long     mostSignificant = 1;
            long     rest            = 0;
            long     current         = mostSignificant | rest;
            double   lowerRange      = 0;
            double   upperRange      = 1;
            double   midRange        = 0.5;
            double   incrementRange  = 0.25;

            if (x < lower.x || x > upper.x)
            {
                throw new Exception("Out of bounds (" + lower.x.ToString() + " to " + upper.x.ToString() + ")");
            }

            Vector2D mid = MinimalEquation.ByIndex(1);

            if (x < mid.x)
            {
                upper      = mid;
                upperIndex = 1;
                upperRange = midRange;
            }
            else
            {
                lower      = mid;
                lowerIndex = 1;
                lowerRange = midRange;
            }

            //counter for testing performance
            int n = 0;

            for (int i = 0; i < SearchDepth; i++)
            {
                long flipBit = mostSignificant;
                mostSignificant <<= 1;
                long i1 = rest | mostSignificant;
                long i2 = i1 | flipBit;
                midRange        = lowerRange + incrementRange;
                incrementRange /= 2;

                Vector2D v1 = MinimalEquation.ByIndex(i1);
                Vector2D v2 = MinimalEquation.ByIndex(i2);
                n += 2 * i + 2;
                if (v1.x > lower.x && v1.x < upper.x)
                {
                    if (x < v1.x)
                    {
                        upper      = v1;
                        upperIndex = i1;
                        upperRange = midRange;
                    }
                    else
                    {
                        lower      = v1;
                        lowerIndex = i1;
                        lowerRange = midRange;
                    }
                }
                else if (v2.x > lower.x && v2.x < upper.x)
                {
                    if (x < v2.x)
                    {
                        upper      = v2;
                        upperIndex = i2;
                        upperRange = midRange;
                    }
                    else
                    {
                        lower      = v2;
                        lowerIndex = i2;
                        lowerRange = midRange;
                    }
                    rest |= flipBit;
                }
                else
                {
                    //cannot search any deeper.
                    return(new BoundPair()
                    {
                        lower = lower,
                        lowerIndex = lowerIndex,
                        lowerRange = lowerRange,
                        upper = upper,
                        upperIndex = upperIndex,
                        upperRange = upperRange,
                        itterations = i,
                        nextGenerationCalls = n
                    });
                }
            }
            return(new BoundPair()
            {
                upper = upper,
                upperIndex = upperIndex,
                upperRange = upperRange,
                lower = lower,
                lowerIndex = lowerIndex,
                lowerRange = lowerRange,
                itterations = SearchDepth,
                nextGenerationCalls = n
            });
        }