// Copy a1 to a2 for index tuples that are valid for both arrays.
  static void copyArray(AbstractArray a1, AbstractArray a2) {

    Debug.assertTrue(a1.getRank() == a2.getRank());
    int[] subscripts = new int[a1.getRank()];
    int axis = 0;
    copySubArray(a1, a2, subscripts, axis);
  }
  private static void copySubArray(AbstractArray a1, AbstractArray a2, int[] subscripts, int axis) {

    if (axis < subscripts.length) {
      final int limit = Math.min(a1.getDimension(axis), a2.getDimension(axis));
      for (int i = 0; i < limit; i++) {
        subscripts[axis] = i;
        copySubArray(a1, a2, subscripts, axis + 1);
      }
    } else {
      int i1 = a1.getRowMajorIndex(subscripts);
      int i2 = a2.getRowMajorIndex(subscripts);
      a2.aset(i2, a1.AREF(i1));
    }
  }