diagToMatrix function

Array diagToMatrix(
  1. Tuple2<int, int> lAndU,
  2. Array diag
)

Implementation

Array diagToMatrix(Tuple2<int, int> lAndU, Array diag) {
  var l = lAndU.item1;
  var u = lAndU.item2;
  var dim = 3;
  var matrix = Array.zeros(shape: Tuple2(dim, dim));
  for (var i in range(end: l + u + 1)) {
    var partialMatrixValues = matrix
        .getValues()
        .skip(max(0, i - u))
        .map((line) => line.skip(max(u - i, 0)).toList())
        .toList();
    var partialMatrix = Array(values: partialMatrixValues);
    var partialDiagValues = diag.getRow(i).skip(max(0, u - i)).toList();
    var partialDiagMatrix =
        fillDiagonalWithValues(partialMatrix, partialDiagValues);
    matrix.values = [
      for (var k in range(end: matrix.shape.item1))
        [
          for (var j in range(end: matrix.shape.item2))
            if (k >= max(0, i - u))
              if (j >= max(0, u - i))
                partialDiagMatrix
                    .getValue(Tuple2(k - max(0, i - u), j - max(0, u - i)))
              else
                matrix.getValue(Tuple2(k, j))
            else
              matrix.getValue(Tuple2(k, j))
        ]
    ];
  }

  return matrix;
}