* Computes the difference between two lists of numbers. * * Given a Tensor `x` and a Tensor `y`, this operation returns a Tensor `out` * that represents all values that are in `x` but not in `y`. The returned * Tensor `out` is sorted in the same order that the numbers appear in `x` * (duplicate
(
x: Tensor|TensorLike, y: Tensor|TensorLike)
| 49 | * @doc {heading: 'Tensors', subheading: 'Transformations'} |
| 50 | */ |
| 51 | async function setdiff1dAsync_( |
| 52 | x: Tensor|TensorLike, y: Tensor|TensorLike): Promise<[Tensor, Tensor]> { |
| 53 | const $x = convertToTensor(x, 'x', 'setdiff1d'); |
| 54 | const $y = convertToTensor(y, 'y', 'setdiff1d'); |
| 55 | |
| 56 | util.assert( |
| 57 | $x.dtype === $y.dtype, |
| 58 | () => `x and y should have the same dtype, but got x (${ |
| 59 | $x.dtype}) and y (${$y.dtype}).`); |
| 60 | |
| 61 | util.assert( |
| 62 | $x.rank === 1, () => `x should be 1D tensor, but got x (${$x.shape}).`); |
| 63 | |
| 64 | util.assert( |
| 65 | $y.rank === 1, () => `y should be 1D tensor, but got y (${$y.shape}).`); |
| 66 | |
| 67 | const xVals = await $x.data(); |
| 68 | const yVals = await $y.data(); |
| 69 | const ySet = new Set(yVals); |
| 70 | |
| 71 | let outputSize = 0; |
| 72 | for (let i = 0; i < xVals.length; i++) { |
| 73 | if (!ySet.has(xVals[i])) { |
| 74 | outputSize++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | const buffer = new TensorBuffer([outputSize], $x.dtype); |
| 79 | const indices = new TensorBuffer([outputSize], 'int32'); |
| 80 | for (let i = 0, p = 0; i < xVals.length; i++) { |
| 81 | if (!ySet.has(xVals[i])) { |
| 82 | buffer.values[p] = xVals[i]; |
| 83 | indices.values[p] = i; |
| 84 | p++; |
| 85 | } |
| 86 | } |
| 87 | return [buffer.toTensor(), indices.toTensor()]; |
| 88 | } |
| 89 | export const setdiff1dAsync = setdiff1dAsync_; |
nothing calls this directly
no test coverage detected
searching dependent graphs…