blockDiag function
Matrix
blockDiag(
- List arrays
)
Implementation
Matrix blockDiag(List arrays) {
if (arrays.isEmpty) {
return Matrix.empty();
} else if (arrays.length == 1) {
if (arrays.first is num) {
return Matrix.scalar(arrays.first, 1);
} else if (arrays.first is Matrix) {
return arrays.first;
}
}
List transformed = [];
for (var element in arrays) {
transformed.add(atLeast2D(element));
}
int rows = 0;
int columns = 0;
for (var element in transformed) {
rows = rows + element.rowsNum as int;
columns = columns + element.columnsNum as int;
}
List<double> output = List.filled(rows * columns, 0);
int cellNumber = 0;
for (var matrix in transformed) {
matrix.forEach((vector) {
vector.forEach((scalar) {
output[cellNumber] = scalar;
cellNumber = cellNumber + 1;
});
cellNumber = cellNumber + columns - vector.length as int;
});
cellNumber = cellNumber + matrix.columnsNum as int;
}
return Matrix.fromFlattenedList(output, rows, columns);
}