sub method Null safety

List sub(
  1. List list,
  2. {dynamic element = 0,
  3. dynamic tensor}
)

Implementation

List sub(List list, {dynamic element = 0, var tensor}) {
  if (tensor == null && element == 0) {
    throw new Exception(
        "DartTensorException : Enter either an element or a tensor to subtract.");
  } else {
    if (tensor != null && element != 0) {
      throw new Exception(
          "DartTensorException : Provide one either an element or a tensor to subtract, not both");
    } else {
      List shape = getDim(list);
      List flat = flatten(list);
      List temp = [];
      if (element != 0) {
        flat = flat.map((e) => e - element).toList();
        temp = generate(flat, shape);
        return temp;
      } else {
        bool flag = true;
        List shape1 = getDim(tensor);
        List flat1 = flatten(tensor);
        if (shape.length == shape1.length) {
          for (int i = 0; i < shape.length; i++) {
            if (shape[i] != shape1[i]) {
              flag = false;
              break;
            }
          }
          if (flag) {
            for (int i = 0; i < flat.length; i++) {
              temp.add(flat[i] - flat1[i]);
            }
          } else {
            throw new Exception(
                "DartTensorException : Both Tensors should be of equal shape. Got tensor of shape $shape and $shape1.");
          }
        } else {
          throw new Exception(
              "DartTensorException : Both Tensors should be of equal shape. Got tensor of shape $shape and $shape1.");
        }
        temp = generate(temp, shape);
        return temp;
      }
    }
  }
}