concatenate method

List concatenate(
  1. List list,
  2. List tensor, {
  3. int axis = 0,
})

Implementation

List concatenate(List list, List tensor, {int axis = 0}) {
  List shape = getDim(list);
  List shape1 = getDim(tensor);
  bool flag = true;
  List temp = [];
  if (axis >= shape.length || axis >= shape1.length) {
    throw new Exception(
        "DartTensorException : Axis $axis is not allowed with provided tensors.");
  }
  if (shape.length == shape1.length) {
    for (int i = 0; i < shape.length; i++) {
      if (shape[i] != shape1[i]) {
        flag = false;
        break;
      }
    }
    List flat = flatten(list);
    List flat1 = flatten(tensor);
    if (flag) {
      if (axis == 0) {
        temp = list + tensor;
        return temp;
      } else if (axis > 0) {
        int mult = 1;
        for (int i = axis; i < shape.length; i++) {
          mult = mult * shape[i] as int;
        }
        int remain = 1;
        for (int i = axis - 1; i >= 0; i--) {
          remain = remain * shape[i] as int;
        }
        for (int i = 0; i < remain; i++) {
          for (int j = 0; j < mult; j++) {
            temp.add(flat[i * mult + j]);
          }
          for (int j = 0; j < mult; j++) {
            temp.add(flat1[i * mult + j]);
          }
        }
        shape[axis] = shape[axis] * 2;
      }
      temp = generate(temp, shape);
      return temp;
    } else {
      throw new Exception(
          "DartTensorException : Both Tensors should be of equal shape. Got tensor of shape $shape and $shape1.");
    }
  }
  return temp;
}