tail method
Returns a string representation of the last counts elements along each dimension.
If pretty is true, the output is formatted with newlines and indentation.
Implementation
Future<String> tail(List<int> counts, {bool pretty = false}) async {
if (counts.length != shape.length) {
throw Exception(
"Counts length (${counts.length}) does not match tensor rank (${shape.length}).");
}
if (shape.length == 2) {
int numRows = counts[0];
int numCols = counts[1];
int totalCols = shape[1];
int startRow = shape[0] - numRows;
int startCol = totalCols - numCols;
List<List<double>> rows = [];
for (int r = startRow; r < shape[0]; r++) {
final rowData = Float32List(numCols);
await buffer.read(rowData, numCols,
readOffset: r * totalCols + startCol);
rows.add(rowData.map((e) => e.toDouble()).toList());
}
return _format2D(rows, pretty: pretty);
} else {
List<int> startIndices = [];
for (int i = 0; i < shape.length; i++) {
if (counts[i] > shape[i]) {
throw Exception(
"Tail count (${counts[i]}) exceeds tensor dimension $i size (${shape[i]}).");
}
startIndices.add(shape[i] - counts[i]);
}
Tensor<T> subTensor = await slice(
startIndices: startIndices, endIndices: List<int>.from(shape));
// For formatted output, slice the head of the sub-tensor.
subTensor = await subTensor.slice(
startIndices: List.filled(counts.length, 0), endIndices: counts);
T subData = await subTensor.getData();
List<double> dataList = _toDoubleList(subData);
return _formatTensor(dataList, counts, pretty: pretty);
}
}