示例#1
0
        public static Object Solve()
        {
            var diameter = FibonacciSequence.ItemsX.Take(1000).Sum();

            var x = IntX.Multiply(diameter, 314159265359, MultiplyMode.Classic);
            var y = IntX.Divide(x, 100000000000, DivideMode.Classic);

            return(y);
        }
    public static DecimalX Calculate(int numberOfDigitsRequired)
    {
        int  max = numberOfDigitsRequired + 8;                                             //  To be safe, compute 8 extra digits, to be dropped at end. The 8 is arbitrary
        var  a   = IntX.Multiply(InverseTan(5, max), new IntX(16), MultiplyMode.AutoFht);  //16 x arctan(1/5)
        var  b   = IntX.Multiply(InverseTan(239, max), new IntX(4), MultiplyMode.AutoFht); //4 x arctan(1/239)
        IntX pi  = a - b;

        return(new DecimalX(
                   IntX.Divide(pi, IntX.Pow(10, (uint)8), DivideMode.AutoNewton)
                   , IntX.Pow(10, (uint)numberOfDigitsRequired, MultiplyMode.AutoFht)));
    }
    public static IntX InverseTan(int denominator, int numberOfDigitsRequired)
    {
        int  demonimatorSquared = denominator * denominator;
        int  degreeNeeded       = GetDegreeOfPrecisionNeeded(demonimatorSquared, numberOfDigitsRequired);
        IntX tenToNumberPowerOfDigitsRequired = GetTenToPowerOfNumberOfDigitsRequired(numberOfDigitsRequired);
        int  c = 2 * degreeNeeded + 1;
        IntX s = IntX.Divide(tenToNumberPowerOfDigitsRequired, new IntX(c), DivideMode.AutoNewton);                 // s = (10^N)/c

        for (int i = 0; i < degreeNeeded; i++)
        {
            c = c - 2;
            var temp1 = IntX.Divide(tenToNumberPowerOfDigitsRequired, new IntX(c), DivideMode.AutoNewton);
            var temp2 = IntX.Divide(s, new IntX(demonimatorSquared), DivideMode.AutoNewton);
            s = temp1 - temp2;
        }
        Console.WriteLine("Number of iterations=" + degreeNeeded);
        // return s/denominator, which is integer part of 10^numberOfDigitsRequired times arctan(1/k)
        return(IntX.Divide(s, new IntX(denominator), DivideMode.AutoNewton));
    }
示例#4
0
        public IntX Determinant(bool preserve = true)
        {
            if (preserve)
            {
                return(Copy().Determinant(false));
            }
            if (Width != Height)
            {
                throw new ArgumentOutOfRangeException("Is not a square matrix, determinant not defined");
            }

            /*
             * if (Width == 2 && Height == 2)
             *  return Elements[0, 0] * Elements[1, 1] - Elements[0, 1] * Elements[1, 0];
             * if (Width == 1 && Height == 1)
             *  return Elements[0, 0];
             *
             * IntX solution = 0;
             * MatrixX minorMatrixX = new MatrixX(Width - 1, Height - 1);
             * for (int i = 0; i < Width; i++)
             * {
             *  GetMinor(i, 0, ref minorMatrixX);
             *  if (i % 2 == 0)
             *      solution += minorMatrixX.Determinant() * Elements[i, 0];
             *  else
             *      solution -= minorMatrixX.Determinant() * Elements[i, 0];
             * }
             */
            IntX determinantDivisor = 1, deterimant = 1;

            for (int i = 0; i < Width - 1; i++)//column & row
            {
                //find canidate row
                int m = i;
                for (; m < Height; m++)
                {
                    if (Elements[i, m] != 0)
                    {
                        break;
                    }
                }
                if (m == Height)
                {
                    return(0);
                }
                //swap canidate with row i
                if (m != i)
                {
                    for (int j = 0; j < Width; j++)
                    {
                        IntX a = Elements[j, i], b = Elements[j, m];
                        Elements[j, i] = b;
                        Elements[m, i] = a;
                    }
                }
                if (Elements[i, i] == 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                deterimant *= Elements[i, i];
                //multiply and add to all other rows after i
                for (int j = i + 1; j < Height; j++)//row
                {
                    // Finding scalar multiple
                    IntX canidateMult = Elements[i, j];
                    for (int k = i; k < Width; k++)//column
                    {
                        // Column is k
                        // Canidate row is i
                        // Current row is j
                        // Scalar multiplier for current row is Elements[i,i]
                        // Scalar multiplier for subtraction canidate row is canidateMult (initially Elements[i,j])

                        Elements[k, j] = Elements[k, j] * Elements[i, i]
                                         - Elements[k, i] * canidateMult;
                    }
                }
                // Find total divisor.
                determinantDivisor *= IntX.Pow(Elements[i, i], (uint)(Height - i - 1));
            }
            return(IntX.Divide(deterimant * Elements[Width - 1, Height - 1], determinantDivisor, DivideMode.Classic));
        }