|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 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 | + */ |
| 17 | +import * as tf from '../index'; |
| 18 | +import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; |
| 19 | +import {expectArraysClose, expectArraysEqual} from '../test_util'; |
| 20 | + |
| 21 | +describeWithFlags('diag', ALL_ENVS, () => { |
| 22 | + it('1d', async () => { |
| 23 | + const m = tf.tensor1d([5]); |
| 24 | + const diag = tf.diag(m); |
| 25 | + expect(diag.shape).toEqual([1, 1]); |
| 26 | + expectArraysClose(await diag.data(), [5]); |
| 27 | + }); |
| 28 | + it('2d', async () => { |
| 29 | + const m = tf.tensor2d([8, 2, 3, 4, 5, 1], [3, 2]); |
| 30 | + const diag = tf.diag(m); |
| 31 | + expect(diag.shape).toEqual([3, 2, 3, 2]); |
| 32 | + expectArraysClose(await diag.data(), [ |
| 33 | + 8, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, |
| 34 | + 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 1 |
| 35 | + ]); |
| 36 | + }); |
| 37 | + it('3d', async () => { |
| 38 | + const m = tf.tensor3d([8, 5, 5, 7, 9, 10, 15, 1, 2, 14, 12, 3], [2, 2, 3]); |
| 39 | + const diag = tf.diag(m); |
| 40 | + expect(diag.shape).toEqual([2, 2, 3, 2, 2, 3]); |
| 41 | + expectArraysClose(await diag.data(), [ |
| 42 | + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, |
| 43 | + 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, |
| 44 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 45 | + 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, |
| 46 | + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, |
| 47 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, |
| 48 | + 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + it('4d', async () => { |
| 52 | + const m = tf.tensor4d( |
| 53 | + [ |
| 54 | + 8, 5, 5, 7, 9, 10, 15, 1, 2, 14, 12, 3, |
| 55 | + 9, 6, 6, 8, 10, 11, 16, 2, 3, 15, 13, 4 |
| 56 | + ], |
| 57 | + [2, 2, 3, 2]); |
| 58 | + const diag = tf.diag(m); |
| 59 | + expect(diag.shape).toEqual([2, 2, 3, 2, 2, 2, 3, 2]); |
| 60 | + expectArraysClose(await diag.data(), [ |
| 61 | + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 62 | + 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 63 | + 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 0, 0, 0, 0, |
| 78 | + 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 79 | + 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 80 | + 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 81 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 82 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, |
| 83 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, |
| 84 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, |
| 85 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 86 | + 13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 87 | + 0, 0, 0, 4 |
| 88 | + ]); |
| 89 | + }); |
| 90 | + it('int32', async () => { |
| 91 | + const m = tf.tensor1d([5, 3], 'int32'); |
| 92 | + const diag = tf.diag(m); |
| 93 | + expect(diag.shape).toEqual([2, 2]); |
| 94 | + expect(diag.dtype).toBe('int32'); |
| 95 | + expectArraysEqual(await diag.data(), [5, 0, 0, 3]); |
| 96 | + }); |
| 97 | + it('bool', async () => { |
| 98 | + const m = tf.tensor1d([5, 3], 'bool'); |
| 99 | + const diag = tf.diag(m); |
| 100 | + expect(diag.shape).toEqual([2, 2]); |
| 101 | + expect(diag.dtype).toBe('bool'); |
| 102 | + expectArraysEqual(await diag.data(), [1, 0, 0, 1]); |
| 103 | + }); |
| 104 | + it('complex', () => { |
| 105 | + const real = tf.tensor1d([2.25]); |
| 106 | + const imag = tf.tensor1d([4.75]); |
| 107 | + const m = tf.complex(real, imag); |
| 108 | + expect(() => tf.diag(m)).toThrowError(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments