public _permuteIndexTo()

in rfcs/20210731-tfjs-named-tensors/gtensor.ts [361:380]


  public _permuteIndexTo(i:number, new_i:number): GTensor<G> {
    // hack/trick to start with the identity permutation [0,1,...n].
    console.log('this.dimNames', this.dimNames);
    const permutation = this.dimNames.map((s, i) => i);
    console.log('permutation', permutation);
    // Now swap the last and the ith index.
    //
    // TODO(ldixon): I heard that some permutations are cheeper than others,
    // so is there some smart way to do an optimal permutation?
    const lastIndex = new_i;
    permutation[i] = lastIndex;
    permutation[lastIndex] = i;
    const oldLastName = this.dimNames[lastIndex];
    const newLastName = this.dimNames[i];
    const newDimNames = this.dimNames.slice();
    newDimNames[lastIndex] = newLastName;
    newDimNames[i] = oldLastName;
    console.log('newDimNames', newDimNames);
    return new GTensor<G>(tf.transpose(this.tensor, permutation), newDimNames);
  }