Skip to content

Instantly share code, notes, and snippets.

@Rayhawk11
Created October 15, 2020 01:44
Show Gist options
  • Save Rayhawk11/cbaaeee4d1446599cf5b8f28cfe34d49 to your computer and use it in GitHub Desktop.
Save Rayhawk11/cbaaeee4d1446599cf5b8f28cfe34d49 to your computer and use it in GitHub Desktop.
import 'package:ml_linalg/linalg.dart';
List<Matrix> fastSplitMatrix(Matrix m) {
if (m.rowsNum % 2 != 0 || m.columnsNum % 2 != 0) {
throw Exception('Can only split even matrices!');
}
var rowsPer = m.rowsNum ~/ 2;
var colsPer = m.columnsNum ~/ 2;
var results = List<Matrix>(4);
var t1 = List<Vector>(rowsPer);
var t2 = List<Vector>(rowsPer);
for (var rowIndex = 0; rowIndex < rowsPer; rowIndex++) {
var row = m[rowIndex];
t1[rowIndex] = row.subvector(0, colsPer);
t2[rowIndex] = row.subvector(colsPer);
}
results[0] = Matrix.fromRows(t1);
results[1] = Matrix.fromRows(t2);
for (var rowIndex = rowsPer; rowIndex < m.rowsNum; rowIndex++) {
var row = m[rowIndex];
t1[rowIndex - rowsPer] = row.subvector(0, colsPer);
t2[rowIndex - rowsPer] = row.subvector(colsPer);
}
results[2] = Matrix.fromRows(t1);
results[3] = Matrix.fromRows(t2);
return results;
}
void main() {
var testMatrix = Matrix.fromList([[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]);
var split = fastSplitMatrix(testMatrix);
print(split[0]);
print(split[0] + split[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment