unaryOperation<R> method
Performs an unary element-wise operation function
on this tensor and
stores the result into target
or (if missing) into a newly created one.
Implementation
Tensor<R> unaryOperation<R>(Map1<T, R> function,
{DataType<R>? type, Tensor<R>? target}) {
if (target == null) {
// Create a new tensor.
return Tensor<R>.fromIterable(values.map(function),
shape: layout.shape, type: type ?? DataType.fromType<R>());
} else if (target == this) {
// Perform the operation in-place.
final targetData = target.data;
for (final i in layout.indices) {
targetData[i] = function(data[i]);
}
return target;
} else {
// Perform the operation into another one.
final targetLayout = target.layout;
LayoutError.checkEqualShape(layout, targetLayout, 'target');
final sourceData = data, sourceIterator = layout.indices.iterator;
final targetData = target.data,
targetIterator = targetLayout.indices.iterator;
while (targetIterator.moveNext() && sourceIterator.moveNext()) {
targetData[targetIterator.current] =
function(sourceData[sourceIterator.current]);
}
return target;
}
}