Пример #1
0
        /**
         * Computes the inverse mod 3.
         * Returns <code>null</code> if the polynomial is not invertible.
         *
         * @return a new polynomial
         */
        public IntegerPolynomial InvertF3()
        {
            int N = coeffs.Length;
            int k = 0;
            IntegerPolynomial b = new IntegerPolynomial(N + 1);

            b.coeffs[0] = 1;
            IntegerPolynomial c = new IntegerPolynomial(N + 1);
            IntegerPolynomial f = new IntegerPolynomial(N + 1);

            Array.Copy(coeffs, f.coeffs, N);
            f.ModPositive(3);
            // set g(x) = x^N − 1
            IntegerPolynomial g = new IntegerPolynomial(N + 1);

            g.coeffs[0] = -1;
            g.coeffs[N] = 1;
            while (true)
            {
                while (f.coeffs[0] == 0)
                {
                    for (int i = 1; i <= N; i++)
                    {
                        f.coeffs[i - 1]     = f.coeffs[i];                       // f(x) = f(x) / x
                        c.coeffs[N + 1 - i] = c.coeffs[N - i];                   // c(x) = c(x) * x
                    }
                    f.coeffs[N] = 0;
                    c.coeffs[0] = 0;
                    k++;
                    if (f.EqualsZero())
                    {
                        return(null);                          // not invertible
                    }
                }
                if (f.EqualsAbsOne())
                {
                    break;
                }
                if (f.Degree() < g.Degree())
                {
                    // exchange f and g
                    IntegerPolynomial temp = f;
                    f = g;
                    g = temp;
                    // exchange b and c
                    temp = b;
                    b    = c;
                    c    = temp;
                }
                if (f.coeffs[0] == g.coeffs[0])
                {
                    f.Sub(g, 3);
                    b.Sub(c, 3);
                }
                else
                {
                    f.Add(g, 3);
                    b.Add(c, 3);
                }
            }

            if (b.coeffs[N] != 0)
            {
                return(null);
            }
            // Fp(x) = [+-] x^(N-k) * b(x)
            IntegerPolynomial Fp = new IntegerPolynomial(N);
            int j = 0;

            k %= N;
            for (int i = N - 1; i >= 0; i--)
            {
                j = i - k;
                if (j < 0)
                {
                    j += N;
                }
                Fp.coeffs[j] = f.coeffs[0] * b.coeffs[i];
            }

            Fp.EnsurePositive(3);
            return(Fp);
        }
Пример #2
0
        /**
         * Multithreaded version of {@link #resultant()}.
         *
         * @return <code>(rho, res)</code> satisfying <code>res = rho*this + t*(x^n-1)</code> for some integer <code>t</code>.
         */
        /* Taking out Async for now, harder problem than just translating.
         * public async Task<Resultant> ResultantMultiThread()
         * {
         *      int N = coeffs.Length;
         *
         *      // upper bound for resultant(f, g) = ||f, 2||^deg(g) * ||g, 2||^deg(f) = squaresum(f)^(N/2) * 2^(deg(f)/2) because g(x)=x^N-1
         *      // see http://jondalon.mathematik.uni-osnabrueck.de/staff/phpages/brunsw/CompAlg.pdf chapter 3
         *      BigInteger max = squareSum().pow((N + 1) / 2);
         *      max = max.multiply(BigInteger.ValueOf(2).pow((degree() + 1) / 2));
         *      BigInteger max2 = max.multiply(BigInteger.ValueOf(2));
         *
         *      // compute resultants modulo prime numbers
         *      BigInteger prime = BigInteger.ValueOf(10000);
         *      BigInteger pProd = Constants.BIGINT_ONE;
         *      LinkedBlockingQueue<Task<ModularResultant>> resultantTasks = new LinkedBlockingQueue<Task<ModularResultant>>();
         *      Iterator<BigInteger> primes = BIGINT_PRIMES.Iterator();
         *      ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
         *      while (pProd.CompareTo(max2) < 0)
         *      {
         *              if (primes.hasNext())
         *              {
         *                      prime = primes.next();
         *              }
         *              else
         *              {
         *                      prime = prime.NextProbablePrime();
         *              }
         *              Task<ModularResultant> task = executor.submit(new ModResultantTask(prime.IntValue));
         *              resultantTasks.add(task);
         *              pProd = pProd.Multiply(prime);
         *      }
         *
         *      // Combine modular resultants to obtain the resultant.
         *      // For efficiency, first combine all pairs of small resultants to bigger resultants,
         *      // then combine pairs of those, etc. until only one is left.
         *      ModularResultant overallResultant = null;
         *      while (!resultantTasks.isEmpty())
         *      {
         *              try
         *              {
         *                      Task<ModularResultant> modRes1 = resultantTasks.take();
         *                      Task<ModularResultant> modRes2 = resultantTasks.poll();
         *                      if (modRes2 == null)
         *                      {
         *                              // modRes1 is the only one left
         *                              overallResultant = await modRes1; //modRes1..get();
         *                              break;
         *                      }
         *                      Task<ModularResultant> newTask = executor.submit(new CombineTask(modRes1.get(), modRes2.get()));
         *                      resultantTasks.add(newTask);
         *              }
         *              catch (Exception e)
         *              {
         *                      throw new InvalidOperationException(e.ToString());
         *              }
         *      }
         *      executor.shutdown();
         *      BigInteger res = overallResultant.res;
         *      BigIntPolynomial rhoP = overallResultant.rho;
         *
         *      BigInteger pProd2 = pProd.divide(BigInteger.ValueOf(2));
         *      BigInteger pProd2n = pProd2.negate();
         *
         *      if (res.compareTo(pProd2) > 0)
         *      {
         *              res = res.subtract(pProd);
         *      }
         *      if (res.compareTo(pProd2n) < 0)
         *      {
         *              res = res.add(pProd);
         *      }
         *
         *      for (int i = 0; i < N; i++)
         *      {
         *              BigInteger c = rhoP.coeffs[i];
         *              if (c.compareTo(pProd2) > 0)
         *              {
         *                      rhoP.coeffs[i] = c.subtract(pProd);
         *              }
         *              if (c.compareTo(pProd2n) < 0)
         *              {
         *                      rhoP.coeffs[i] = c.add(pProd);
         *              }
         *      }
         *
         *      return new Resultant(rhoP, res);
         * }
         */
        /**
         * Resultant of this polynomial with <code>x^n-1 mod p</code>.
         *
         * @return <code>(rho, res)</code> satisfying <code>res = rho*this + t*(x^n-1) mod p</code> for some integer <code>t</code>.
         */
        public ModularResultant Resultant(int p)
        {
            // Add a coefficient as the following operations involve polynomials of degree deg(f)+1
            int[] fcoeffs = new int[coeffs.Length + 1];
            Array.Copy(coeffs, fcoeffs, coeffs.Length);
            IntegerPolynomial f = new IntegerPolynomial(fcoeffs);
            int N = fcoeffs.Length;

            IntegerPolynomial a = new IntegerPolynomial(N);

            a.coeffs[0]     = -1;
            a.coeffs[N - 1] = 1;
            IntegerPolynomial b  = new IntegerPolynomial(f.coeffs);
            IntegerPolynomial v1 = new IntegerPolynomial(N);
            IntegerPolynomial v2 = new IntegerPolynomial(N);

            v2.coeffs[0] = 1;
            int da = N - 1;
            int db = b.Degree();
            int ta = da;
            int c  = 0;
            int r  = 1;

            while (db > 0)
            {
                c = Util.Util.Invert(b.coeffs[db], p);
                c = (c * a.coeffs[da]) % p;
                a.MultShiftSub(b, c, da - db, p);
                v1.MultShiftSub(v2, c, da - db, p);

                da = a.Degree();
                if (da < db)
                {
                    r *= Util.Util.Pow(b.coeffs[db], ta - da, p);
                    r %= p;
                    if (ta % 2 == 1 && db % 2 == 1)
                    {
                        r = (-r) % p;
                    }
                    IntegerPolynomial temp = a;
                    a = b;
                    b = temp;
                    int tempdeg = da;
                    da   = db;
                    temp = v1;
                    v1   = v2;
                    v2   = temp;
                    ta   = db;
                    db   = tempdeg;
                }
            }
            r *= Util.Util.Pow(b.coeffs[0], da, p);
            r %= p;
            c  = Util.Util.Invert(b.coeffs[0], p);
            v2.Multiply(c);
            v2.Mod(p);
            v2.Multiply(r);
            v2.Mod(p);

            // drop the highest coefficient so #coeffs matches the original input
            int[] newCoeffs = new int[v2.coeffs.Length - 1];
            Array.Copy(v2.coeffs, newCoeffs, v2.coeffs.Length - 1);
            v2.coeffs = newCoeffs;
            return(new ModularResultant(new BigIntPolynomial(v2), BigInteger.ValueOf(r), BigInteger.ValueOf(p)));
        }
Пример #3
0
        /**
         * Computes the inverse mod <code>q; q</code> must be a power of 2.<br>
         * Returns <code>null</code> if the polynomial is not invertible.
         *
         * @param q the modulus
         * @return a new polynomial
         */
        public IntegerPolynomial InvertFq(int q)
        {
            int N = coeffs.Length;
            int k = 0;
            IntegerPolynomial b = new IntegerPolynomial(N + 1);

            b.coeffs[0] = 1;
            IntegerPolynomial c = new IntegerPolynomial(N + 1);
            IntegerPolynomial f = new IntegerPolynomial(N + 1);

            Array.Copy(coeffs, f.coeffs, N);
            f.ModPositive(2);
            // set g(x) = x^N − 1
            IntegerPolynomial g = new IntegerPolynomial(N + 1);

            g.coeffs[0] = 1;
            g.coeffs[N] = 1;
            while (true)
            {
                while (f.coeffs[0] == 0)
                {
                    for (int i = 1; i <= N; i++)
                    {
                        f.coeffs[i - 1]     = f.coeffs[i];                       // f(x) = f(x) / x
                        c.coeffs[N + 1 - i] = c.coeffs[N - i];                   // c(x) = c(x) * x
                    }
                    f.coeffs[N] = 0;
                    c.coeffs[0] = 0;
                    k++;
                    if (f.EqualsZero())
                    {
                        return(null);                          // not invertible
                    }
                }
                if (f.EqualsOne())
                {
                    break;
                }
                if (f.Degree() < g.Degree())
                {
                    // exchange f and g
                    IntegerPolynomial temp = f;
                    f = g;
                    g = temp;
                    // exchange b and c
                    temp = b;
                    b    = c;
                    c    = temp;
                }
                f.Add(g, 2);
                b.Add(c, 2);
            }

            if (b.coeffs[N] != 0)
            {
                return(null);
            }
            // Fq(x) = x^(N-k) * b(x)
            IntegerPolynomial Fq = new IntegerPolynomial(N);
            int j = 0;

            k %= N;
            for (int i = N - 1; i >= 0; i--)
            {
                j = i - k;
                if (j < 0)
                {
                    j += N;
                }
                Fq.coeffs[j] = b.coeffs[i];
            }

            return(Mod2ToModq(Fq, q));
        }