moveaxis function

VARP moveaxis(
  1. VARP a,
  2. List<int> source,
  3. List<int> destination
)

Implementation

VARP moveaxis(VARP a, List<int> source, List<int> destination) {
  final ndim = a.ndim;
  MnnAssert(ndim != null, "a must have a shape");
  final source_ = _normalAxes(source, ndim!);
  final destination_ = _normalAxes(destination, ndim);
  MnnAssert(source_.length == destination_.length, "source and destination must have the same length");
  final axes = List.generate(ndim, (index) => index).where((e) => !source_.contains(e)).toList();
  final sorted = List.generate(source_.length, (i) => i).map((i) => (destination_[i], source_[i])).toList();
  sorted.sort((a, b) => a.$1.compareTo(b.$1));
  for (final (dst, src) in sorted) {
    axes.insert(dst, src);
  }
  return F.transpose(a, axes);
}