예제 #1
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();
 }
예제 #2
0
 public static String toString(long[] xs, String var) {
   return Euclid.toString(xs, 0, xs.length, var);
 }
예제 #3
0
 public static boolean toString(
     Appendable writer, long[] xs0, long[] xs1, int begin, int end, String var)
     throws IOException {
   end = highest(xs0, begin, end);
   if (end < begin + 1) {
     return false;
   }
   boolean first = true;
   while (begin < end--) {
     long x0 = xs0[end];
     long x1 = xs1[end];
     if (x0 == 0) {
       continue;
     } else if (x0 == x1) {
       x0 = 1;
       x1 = 1;
     } else if (x0 == -x1) {
       x0 = -1;
       x1 = 1;
     } else if (x1 < 0) {
       x0 = -x0;
       x1 = -x1;
     }
     boolean minus = x0 < 0;
     if (minus) {
       x0 = -x0;
     }
     if (first) {
       first = false;
       if (minus) {
         writer.append("- ");
       }
       if (begin == end) {
         toString(writer, x0, x1);
       } else {
         if (x0 != x1) {
           toString(writer, x0, x1);
         }
         writer.append(var);
         if (begin + 1 < end) {
           writer.append('^');
           writer.append(Integer.toString(end));
         }
       }
     } else {
       if (minus) {
         writer.append(" - ");
       } else {
         writer.append(" + ");
       }
       if (end == 0 || x0 != x1) {
         toString(writer, x0, x1);
       }
       switch (end - begin) {
         case 0:
           break;
         case 1:
           writer.append(var);
           break;
         default:
           writer.append(var);
           writer.append('^');
           writer.append(Integer.toString(end - begin));
           break;
       }
     }
   }
   return true;
 }