Example #1
0
 public static GcdFraction gcd(long[] xs0, long[] xs1, int nx, long[] ys0, long[] ys1, int ny) {
   nx = highest(xs0, 0, nx);
   ny = highest(ys0, 0, ny);
   if (nx == 0 || ny == 0) {
     throw new IllegalArgumentException(dividesByZero("x or y"));
   } else if (nx < ny) {
     long[] zs0 = xs0;
     long[] zs1 = xs1;
     int nz = nx;
     xs0 = ys0;
     xs1 = ys1;
     nx = ny;
     ys0 = zs0;
     ys1 = zs1;
     ny = nz;
   }
   while (0 < ny) {
     int nr = Euclid.divides(xs0, xs1, nx, ys0, ys1, ny);
     long[] rs0 = xs0;
     long[] rs1 = xs1;
     nr = highest(rs0, 0, nr);
     // Arrays.fill(rs0, nr, nx, 0);
     xs0 = ys0;
     xs1 = ys1;
     nx = ny;
     ys0 = rs0;
     ys1 = rs1;
     ny = nr;
   }
   return new GcdFraction(xs0, xs1, nx);
 }
Example #2
0
 public static GcdLong gcd(long[] xs, int nx, long[] ys, int ny) {
   nx = highest(xs, 0, nx);
   ny = highest(ys, 0, ny);
   if (nx == 0 || ny == 0) {
     throw new IllegalArgumentException(dividesByZero("x or y"));
   } else if (nx < ny) {
     long[] zs = xs;
     int nz = nx;
     xs = ys;
     nx = ny;
     ys = zs;
     ny = nz;
   }
   while (0 < ny) {
     int nr = Euclid.divides(xs, nx, ys, ny);
     long[] rs = xs;
     nr = highest(rs, 0, nr);
     // Arrays.fill(rs, nr, nx, 0);
     xs = ys;
     nx = ny;
     ys = rs;
     ny = nr;
   }
   return new GcdLong(xs, nx);
 }
Example #3
0
 protected static void reduce(long[] xs0, long[] xs1, int ind, boolean gcd) {
   if (xs0[ind] == 0) {
     xs1[ind] = 1;
   } else if (xs0[ind] == xs1[ind]) {
     xs0[ind] = 1;
     xs1[ind] = 1;
   } else if (xs0[ind] == -xs1[ind]) {
     xs0[ind] = -1;
     xs1[ind] = 1;
   } else if (xs0[ind] % xs1[ind] == 0) {
     xs0[ind] = xs0[ind] / xs1[ind];
     xs1[ind] = 1;
   } else {
     if (xs1[ind] < 0) {
       xs0[ind] = -xs0[ind];
       xs1[ind] = -xs1[ind];
     }
     if (gcd) {
       long g = Euclid.gcd(Math.abs(xs0[ind]), xs1[ind]);
       if (g != 1) {
         xs0[ind] /= g;
         xs1[ind] /= g;
       }
     }
   }
 }
Example #4
0
 public static String toString(long[] xs, int begin, int end, String var) {
   StringBuilder buffer = new StringBuilder();
   try {
     if (!Euclid.toString(buffer, xs, begin, end, var)) {
       buffer.append("0");
     }
   } catch (IOException ex) {
     ex.printStackTrace();
   }
   return buffer.toString();
 }
Example #5
0
 public static String toString(long[] xs, String var) {
   return Euclid.toString(xs, 0, xs.length, var);
 }