예제 #1
0
        private Polynomial MultiplyKaratsuba(double[] leftCoefficients, double[] rightCoefficients, int leftOrder, int rightOrder, int n, int offset)
        {
            if (n == 1)
            {
                return(new Polynomial(new double[] { leftCoefficients[offset] * rightCoefficients[offset] }));
            }
            if (n == 2)
            {
                return(new Polynomial(new double[] { leftCoefficients[offset] * rightCoefficients[offset], leftCoefficients[offset] * rightCoefficients[offset + 1] + leftCoefficients[offset + 1] * rightCoefficients[offset], leftCoefficients[offset + 1] * rightCoefficients[offset + 1] }));
            }
            n >>= 1;
            double[] FF = new double[n], GG = new double[n];
            for (int i = offset; i < n + offset; i++)
            {
                FF[i - offset] = leftCoefficients[i] + leftCoefficients[n + i];
                GG[i - offset] = rightCoefficients[i] + rightCoefficients[n + i];
            }
            Polynomial FG0  = MultiplyKaratsuba(leftCoefficients, rightCoefficients, n - 1, n - 1, n, offset);
            Polynomial FG1  = MultiplyKaratsuba(leftCoefficients, rightCoefficients, Math.Max(leftOrder - n, 0), Math.Max(rightOrder - n, 0), n, offset + n);
            Polynomial FFGG = MultiplyKaratsuba(FF, GG, n - 1, n - 1, n, 0);

            FFGG.SubtractInplace(FG0);
            FFGG.SubtractInplace(FG1);
            FFGG.MultiplyShiftInplace(n);
            FG1.MultiplyShiftInplace(n + n);
            FG1.AddInplace(FFGG);
            FG1.AddInplace(FG0);
            return(FG1);
        }