UNPKG

1.67 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2020 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { ENGINE } from '../engine';
18import { Diag } from '../kernel_names';
19import { convertToTensor } from '../tensor_util_env';
20import { op } from './operation';
21/**
22 * Returns a diagonal tensor with a given diagonal values.
23 *
24 * Given a diagonal, this operation returns a tensor with the diagonal and
25 * everything else padded with zeros.
26 *
27 * Assume the input has dimensions `[D1,..., Dk]`, then the output is a tensor
28 * of rank 2k with dimensions `[D1,..., Dk, D1,..., Dk]`
29 *
30 * ```js
31 * const x = tf.tensor1d([1, 2, 3, 4]);
32 *
33 * tf.diag(x).print()
34 * ```
35 * ```js
36 * const x = tf.tensor1d([1, 2, 3, 4, 5, 6, 6, 8], [4, 2])
37 *
38 * tf.diag(x).print()
39 * ```
40 * @param x The input tensor.
41 *
42 * @doc {heading: 'Tensors', subheading: 'Creation'}
43 */
44function diag_(x) {
45 const $x = convertToTensor(x, 'x', 'diag');
46 const inputs = { x: $x };
47 return ENGINE.runKernel(Diag, inputs);
48}
49export const diag = op({ diag_ });
50//# sourceMappingURL=diag.js.map
\No newline at end of file