"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tf = require("../index");
var jasmine_util_1 = require("../jasmine_util");
var test_util_1 = require("../test_util");
var reduce_util = require("./reduce_util");
jasmine_util_1.describeWithFlags('Reduction: min', test_util_1.ALL_ENVS, function () {
    it('Tensor1D', function () {
        var a = tf.tensor1d([3, -1, 0, 100, -7, 2]);
        test_util_1.expectNumbersClose(tf.min(a).get(), -7);
    });
    it('ignores NaNs', function () {
        var a = tf.tensor1d([3, NaN, 2]);
        expect(tf.min(a).get()).toEqual(2);
    });
    it('2D', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectNumbersClose(tf.min(a).get(), -7);
    });
    it('2D axis=[0,1]', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectNumbersClose(tf.min(a, [0, 1]).get(), -7);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.min(a, 0);
        expect(r.shape).toEqual([3]);
        test_util_1.expectArraysClose(r, [3, -7, 0]);
    });
    it('2D, axis=0, keepDims', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.min(a, 0, true);
        expect(r.shape).toEqual([1, 3]);
        test_util_1.expectArraysClose(r, [3, -7, 0]);
    });
    it('2D, axis=1 provided as a number', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.min(a, 1);
        test_util_1.expectArraysClose(r, [2, -7]);
    });
    it('2D, axis = -1 provided as a number', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.min(a, -1);
        test_util_1.expectArraysClose(r, [2, -7]);
    });
    it('2D, axis=[1]', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.min(a, [1]);
        test_util_1.expectArraysClose(r, [2, -7]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.min({}); })
            .toThrowError(/Argument 'x' passed to 'min' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        test_util_1.expectNumbersClose(tf.min([3, -1, 0, 100, -7, 2]).get(), -7);
    });
});
jasmine_util_1.describeWithFlags('Reduction: max', test_util_1.ALL_ENVS, function () {
    it('with one element dominating', function () {
        var a = tf.tensor1d([3, -1, 0, 100, -7, 2]);
        var r = tf.max(a);
        test_util_1.expectNumbersClose(r.get(), 100);
    });
    it('with all elements being the same', function () {
        var a = tf.tensor1d([3, 3, 3]);
        var r = tf.max(a);
        test_util_1.expectNumbersClose(r.get(), 3);
    });
    it('ignores NaNs', function () {
        test_util_1.expectNumbersClose(tf.max(tf.tensor1d([3, NaN, 2])).get(), 3);
    });
    it('2D', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectNumbersClose(tf.max(a).get(), 100);
    });
    it('2D axis=[0,1]', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectNumbersClose(tf.max(a, [0, 1]).get(), 100);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.max(a, [0]);
        expect(r.shape).toEqual([3]);
        test_util_1.expectArraysClose(r, [100, -1, 2]);
    });
    it('2D, axis=0, keepDims', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.max(a, [0], true);
        expect(r.shape).toEqual([1, 3]);
        test_util_1.expectArraysClose(r, [100, -1, 2]);
    });
    it('2D, axis=1 provided as a number', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.max(a, 1);
        test_util_1.expectArraysClose(r, [5, 100]);
    });
    it('2D, axis = -1 provided as a number', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.max(a, -1);
        test_util_1.expectArraysClose(r, [5, 100]);
    });
    it('2D, axis=[1]', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.max(a, [1]);
        test_util_1.expectArraysClose(r, [5, 100]);
    });
    it('6D, axis=[5]', function () {
        var a = tf.range(0, 64).reshape([2, 2, 2, 2, 2, 2]);
        var r = tf.max(a, [5]);
        var expectedResult = [
            1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31,
            33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63
        ];
        test_util_1.expectArraysClose(r, expectedResult);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.max({}); })
            .toThrowError(/Argument 'x' passed to 'max' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var r = tf.max([3, -1, 0, 100, -7, 2]);
        test_util_1.expectNumbersClose(r.get(), 100);
    });
});
jasmine_util_1.describeWithFlags('Reduction: argmax', test_util_1.ALL_ENVS, function () {
    it('Tensor1D', function () {
        var a = tf.tensor1d([1, 0, 3, 2]);
        var result = tf.argMax(a);
        expect(result.dtype).toBe('int32');
        expect(result.get()).toBe(2);
    });
    it('one value', function () {
        var a = tf.tensor1d([10]);
        var result = tf.argMax(a);
        expect(result.dtype).toBe('int32');
        expect(result.get()).toBe(0);
    });
    it('N > than parallelization threshold', function () {
        var n = reduce_util.PARALLELIZE_THRESHOLD * 2;
        var values = new Float32Array(n);
        for (var i = 0; i < n; i++) {
            values[i] = i;
        }
        var a = tf.tensor1d(values);
        var result = tf.argMax(a);
        expect(result.dtype).toBe('int32');
        expect(result.get()).toBe(n - 1);
    });
    it('ignores NaNs', function () {
        var a = tf.tensor1d([0, 3, 5, NaN, 3]);
        var res = tf.argMax(a);
        expect(res.dtype).toBe('int32');
        expect(res.get()).toBe(2);
    });
    it('2D, no axis specified', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectArraysEqual(tf.argMax(a), [1, 0, 1]);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.argMax(a, 0);
        expect(r.shape).toEqual([3]);
        expect(r.dtype).toBe('int32');
        test_util_1.expectArraysEqual(r, [1, 0, 1]);
    });
    it('2D, axis=1', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.argMax(a, 1);
        expect(r.dtype).toBe('int32');
        test_util_1.expectArraysEqual(r, [2, 0]);
    });
    it('2D, axis = -1', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, 2], [2, 3]);
        var r = tf.argMax(a, -1);
        expect(r.dtype).toBe('int32');
        test_util_1.expectArraysEqual(r, [2, 0]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.argMax({}); })
            .toThrowError(/Argument 'x' passed to 'argMax' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var result = tf.argMax([1, 0, 3, 2]);
        expect(result.dtype).toBe('int32');
        expect(result.get()).toBe(2);
    });
});
jasmine_util_1.describeWithFlags('Reduction: argmin', test_util_1.ALL_ENVS, function () {
    it('Tensor1D', function () {
        var a = tf.tensor1d([1, 0, 3, 2]);
        var result = tf.argMin(a);
        expect(result.get()).toBe(1);
    });
    it('one value', function () {
        var a = tf.tensor1d([10]);
        var result = tf.argMin(a);
        expect(result.get()).toBe(0);
    });
    it('N > than parallelization threshold', function () {
        var n = reduce_util.PARALLELIZE_THRESHOLD * 2;
        var values = new Float32Array(n);
        for (var i = 0; i < n; i++) {
            values[i] = n - i;
        }
        var a = tf.tensor1d(values);
        var result = tf.argMin(a);
        expect(result.dtype).toBe('int32');
        expect(result.get()).toBe(n - 1);
    });
    it('ignores NaNs', function () {
        var a = tf.tensor1d([5, 0, NaN, -1, 3]);
        var res = tf.argMin(a);
        expect(res.get()).toBe(3);
    });
    it('2D, no axis specified', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        test_util_1.expectArraysEqual(tf.argMin(a), [0, 1, 0]);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([3, -1, 0, 100, -7, 2], [2, 3]);
        var r = tf.argMin(a, 0);
        expect(r.shape).toEqual([3]);
        expect(r.dtype).toBe('int32');
        test_util_1.expectArraysEqual(r, [0, 1, 0]);
    });
    it('2D, axis=1', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, -8], [2, 3]);
        var r = tf.argMin(a, 1);
        test_util_1.expectArraysEqual(r, [1, 2]);
    });
    it('2D, axis = -1', function () {
        var a = tf.tensor2d([3, 2, 5, 100, -7, -8], [2, 3]);
        var r = tf.argMin(a, -1);
        test_util_1.expectArraysEqual(r, [1, 2]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.argMin({}); })
            .toThrowError(/Argument 'x' passed to 'argMin' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var result = tf.argMin([1, 0, 3, 2]);
        expect(result.get()).toBe(1);
    });
});
jasmine_util_1.describeWithFlags('Reduction: logSumExp', test_util_1.ALL_ENVS, function () {
    it('0', function () {
        var a = tf.scalar(0);
        var result = tf.logSumExp(a);
        test_util_1.expectNumbersClose(result.get(), 0);
    });
    it('basic', function () {
        var a = tf.tensor1d([1, 2, -3]);
        var result = tf.logSumExp(a);
        test_util_1.expectNumbersClose(result.get(), Math.log(Math.exp(1) + Math.exp(2) + Math.exp(-3)));
    });
    it('propagates NaNs', function () {
        var a = tf.tensor1d([1, 2, NaN]);
        var result = tf.logSumExp(a);
        expect(result.get()).toEqual(NaN);
    });
    it('axes=0 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var r = tf.logSumExp(a, [0]);
        expect(r.shape).toEqual([2]);
        var expected = [
            Math.log(Math.exp(1) + Math.exp(3) + Math.exp(0)),
            Math.log(Math.exp(2) + Math.exp(0) + Math.exp(1))
        ];
        test_util_1.expectArraysClose(r, expected);
    });
    it('axes=0 in 2D array, keepDims', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var r = tf.logSumExp(a, [0], true);
        expect(r.shape).toEqual([1, 2]);
        var expected = [
            Math.log(Math.exp(1) + Math.exp(3) + Math.exp(0)),
            Math.log(Math.exp(2) + Math.exp(0) + Math.exp(1))
        ];
        test_util_1.expectArraysClose(r, expected);
    });
    it('axes=1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.logSumExp(a, [1]);
        expect(res.shape).toEqual([3]);
        var expected = [
            Math.log(Math.exp(1) + Math.exp(2)),
            Math.log(Math.exp(3) + Math.exp(0)),
            Math.log(Math.exp(0) + Math.exp(1)),
        ];
        test_util_1.expectArraysClose(res, expected);
    });
    it('axes = -1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.logSumExp(a, -1);
        expect(res.shape).toEqual([3]);
        var expected = [
            Math.log(Math.exp(1) + Math.exp(2)),
            Math.log(Math.exp(3) + Math.exp(0)),
            Math.log(Math.exp(0) + Math.exp(1)),
        ];
        test_util_1.expectArraysClose(res, expected);
    });
    it('2D, axes=1 provided as a single digit', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var res = tf.logSumExp(a, 1);
        expect(res.shape).toEqual([2]);
        var expected = [
            Math.log(Math.exp(1) + Math.exp(2) + Math.exp(3)),
            Math.log(Math.exp(0) + Math.exp(0) + Math.exp(1))
        ];
        test_util_1.expectArraysClose(res, expected);
    });
    it('axes=0,1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.logSumExp(a, [0, 1]);
        expect(res.shape).toEqual([]);
        var expected = [Math.log(Math.exp(1) + Math.exp(2) + Math.exp(3) + Math.exp(0) + Math.exp(0) +
                Math.exp(1))];
        test_util_1.expectArraysClose(res, expected);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.logSumExp({}); })
            .toThrowError(/Argument 'x' passed to 'logSumExp' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var result = tf.logSumExp([1, 2, -3]);
        test_util_1.expectNumbersClose(result.get(), Math.log(Math.exp(1) + Math.exp(2) + Math.exp(-3)));
    });
});
jasmine_util_1.describeWithFlags('Reduction: sum', test_util_1.ALL_ENVS, function () {
    it('basic', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var result = tf.sum(a);
        test_util_1.expectNumbersClose(result.get(), 7);
    });
    it('propagates NaNs', function () {
        var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]);
        expect(tf.sum(a).get()).toEqual(NaN);
    });
    it('sum over dtype int32', function () {
        var a = tf.tensor1d([1, 5, 7, 3], 'int32');
        var sum = tf.sum(a);
        expect(sum.get()).toBe(16);
    });
    it('sum over dtype bool', function () {
        var a = tf.tensor1d([true, false, false, true, true], 'bool');
        var sum = tf.sum(a);
        expect(sum.get()).toBe(3);
    });
    it('sums all values in 2D array with keep dim', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, null, true);
        expect(res.shape).toEqual([1, 1]);
        test_util_1.expectArraysClose(res, [7]);
    });
    it('sums across axis=0 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, [0]);
        expect(res.shape).toEqual([2]);
        test_util_1.expectArraysClose(res, [4, 3]);
    });
    it('sums across axis=0 in 2D array, keepDims', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, [0], true);
        expect(res.shape).toEqual([1, 2]);
        test_util_1.expectArraysClose(res, [4, 3]);
    });
    it('sums across axis=1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, [1]);
        expect(res.shape).toEqual([3]);
        test_util_1.expectArraysClose(res, [3, 3, 1]);
    });
    it('2D, axis=1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var res = tf.sum(a, 1);
        expect(res.shape).toEqual([2]);
        test_util_1.expectArraysClose(res, [6, 1]);
    });
    it('2D, axis = -1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var res = tf.sum(a, -1);
        expect(res.shape).toEqual([2]);
        test_util_1.expectArraysClose(res, [6, 1]);
    });
    it('sums across axis=0,1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, [0, 1]);
        expect(res.shape).toEqual([]);
        test_util_1.expectArraysClose(res, [7]);
    });
    it('2D, axis=[-1,-2] in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.sum(a, [-1, -2]);
        expect(res.shape).toEqual([]);
        test_util_1.expectArraysClose(res, [7]);
    });
    it('gradients: sum(2d)', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var dy = tf.scalar(10);
        var gradients = tf.grad(function (a) { return a.sum(); })(a, dy);
        expect(gradients.shape).toEqual(a.shape);
        expect(gradients.dtype).toEqual('float32');
        test_util_1.expectArraysClose(gradients, [10, 10, 10, 10, 10, 10]);
    });
    it('gradients: sum(2d, axis=0)', function () {
        var a = tf.tensor2d([[1, 2], [3, 0], [0, 1]], [3, 2]);
        var dy = tf.tensor1d([10, 20]);
        var axis = 0;
        var gradients = tf.grad(function (a) { return a.sum(axis); })(a, dy);
        expect(gradients.shape).toEqual(a.shape);
        expect(gradients.dtype).toEqual('float32');
        test_util_1.expectArraysClose(gradients, [10, 20, 10, 20, 10, 20]);
    });
    it('gradients: sum(2d, axis=1)', function () {
        var a = tf.tensor2d([[1, 2], [3, 0], [0, 1]], [3, 2]);
        var dy = tf.tensor1d([10, 20, 30]);
        var axis = 1;
        var gradients = tf.grad(function (a) { return a.sum(axis); })(a, dy);
        expect(gradients.shape).toEqual(a.shape);
        expect(gradients.dtype).toEqual('float32');
        test_util_1.expectArraysClose(gradients, [10, 10, 20, 20, 30, 30]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.sum({}); })
            .toThrowError(/Argument 'x' passed to 'sum' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var result = tf.sum([[1, 2], [3, 0], [0, 1]]);
        test_util_1.expectNumbersClose(result.get(), 7);
    });
});
jasmine_util_1.describeWithFlags('Reduction: mean', test_util_1.ALL_ENVS, function () {
    it('basic', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var r = tf.mean(a);
        expect(r.dtype).toBe('float32');
        test_util_1.expectNumbersClose(r.get(), 7 / 6);
    });
    it('propagates NaNs', function () {
        var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]);
        var r = tf.mean(a);
        expect(r.dtype).toBe('float32');
        expect(r.get()).toEqual(NaN);
    });
    it('mean(int32) => float32', function () {
        var a = tf.tensor1d([1, 5, 7, 3], 'int32');
        var r = tf.mean(a);
        expect(r.dtype).toBe('float32');
        test_util_1.expectNumbersClose(r.get(), 4);
    });
    it('mean(bool) => float32', function () {
        var a = tf.tensor1d([true, false, false, true, true], 'bool');
        var r = tf.mean(a);
        expect(r.dtype).toBe('float32');
        test_util_1.expectNumbersClose(r.get(), 3 / 5);
    });
    it('2D array with keep dim', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, null, true);
        expect(res.shape).toEqual([1, 1]);
        expect(res.dtype).toBe('float32');
        test_util_1.expectArraysClose(res, [7 / 6]);
    });
    it('axis=0 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, [0]);
        expect(res.shape).toEqual([2]);
        expect(res.dtype).toBe('float32');
        test_util_1.expectArraysClose(res, [4 / 3, 1]);
    });
    it('axis=0 in 2D array, keepDims', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, [0], true);
        expect(res.shape).toEqual([1, 2]);
        expect(res.dtype).toBe('float32');
        test_util_1.expectArraysClose(res, [4 / 3, 1]);
    });
    it('axis=1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, [1]);
        expect(res.dtype).toBe('float32');
        expect(res.shape).toEqual([3]);
        test_util_1.expectArraysClose(res, [1.5, 1.5, 0.5]);
    });
    it('axis = -1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, [-1]);
        expect(res.dtype).toBe('float32');
        expect(res.shape).toEqual([3]);
        test_util_1.expectArraysClose(res, [1.5, 1.5, 0.5]);
    });
    it('2D, axis=1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var res = tf.mean(a, 1);
        expect(res.shape).toEqual([2]);
        expect(res.dtype).toBe('float32');
        test_util_1.expectArraysClose(res, [2, 1 / 3]);
    });
    it('axis=0,1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var res = tf.mean(a, [0, 1]);
        expect(res.shape).toEqual([]);
        expect(res.dtype).toBe('float32');
        test_util_1.expectArraysClose(res, [7 / 6]);
    });
    it('gradients', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var dy = tf.scalar(1.5);
        var da = tf.grad(function (a) { return a.mean(); })(a, dy);
        expect(da.shape).toEqual(a.shape);
        test_util_1.expectArraysClose(da, [
            dy.get() / a.size, dy.get() / a.size, dy.get() / a.size,
            dy.get() / a.size, dy.get() / a.size, dy.get() / a.size
        ]);
    });
    it('gradients throws for defined axis', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var dy = tf.scalar(1.5);
        expect(function () { return tf.grad(function (a) { return a.mean(1); })(a, dy); }).toThrowError();
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.mean({}); })
            .toThrowError(/Argument 'x' passed to 'mean' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var r = tf.mean([[1, 2, 3], [0, 0, 1]]);
        expect(r.dtype).toBe('float32');
        test_util_1.expectNumbersClose(r.get(), 7 / 6);
    });
});
jasmine_util_1.describeWithFlags('Reduction: moments', test_util_1.ALL_ENVS, function () {
    it('basic', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var _a = tf.moments(a), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(variance.dtype).toBe('float32');
        test_util_1.expectNumbersClose(mean.get(), 7 / 6);
        test_util_1.expectNumbersClose(variance.get(), 1.1389);
    });
    it('propagates NaNs', function () {
        var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]);
        var _a = tf.moments(a), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(variance.dtype).toBe('float32');
        expect(mean.get()).toEqual(NaN);
        expect(variance.get()).toEqual(NaN);
    });
    it('moments(int32) => float32', function () {
        var a = tf.tensor1d([1, 5, 7, 3], 'int32');
        var _a = tf.moments(a), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(variance.dtype).toBe('float32');
        test_util_1.expectNumbersClose(mean.get(), 4);
        test_util_1.expectNumbersClose(variance.get(), 5);
    });
    it('moments(bool) => float32', function () {
        var a = tf.tensor1d([true, false, false, true, true], 'bool');
        var _a = tf.moments(a), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(variance.dtype).toBe('float32');
        test_util_1.expectNumbersClose(mean.get(), 3 / 5);
        test_util_1.expectNumbersClose(variance.get(), 0.23999998);
    });
    it('2D array with keep dim', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var _a = tf.moments(a, null, true), mean = _a.mean, variance = _a.variance;
        expect(mean.shape).toEqual([1, 1]);
        expect(mean.dtype).toBe('float32');
        expect(variance.shape).toEqual([1, 1]);
        expect(variance.dtype).toBe('float32');
        test_util_1.expectArraysClose(mean, [7 / 6]);
        test_util_1.expectArraysClose(variance, [1.138889]);
    });
    it('axis=0 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var _a = tf.moments(a, [0]), mean = _a.mean, variance = _a.variance;
        expect(mean.shape).toEqual([2]);
        expect(mean.dtype).toBe('float32');
        expect(variance.shape).toEqual([2]);
        expect(variance.dtype).toBe('float32');
        test_util_1.expectArraysClose(mean, [4 / 3, 1]);
        test_util_1.expectArraysClose(variance, [1.556, 2 / 3]);
    });
    it('axis=1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var _a = tf.moments(a, [1]), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(mean.shape).toEqual([3]);
        expect(variance.dtype).toBe('float32');
        expect(variance.shape).toEqual([3]);
        test_util_1.expectArraysClose(mean, [1.5, 1.5, 0.5]);
        test_util_1.expectArraysClose(variance, [0.25, 2.25, 0.25]);
    });
    it('2D, axis=1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var _a = tf.moments(a, 1), mean = _a.mean, variance = _a.variance;
        expect(mean.shape).toEqual([2]);
        expect(mean.dtype).toBe('float32');
        expect(variance.shape).toEqual([2]);
        expect(variance.dtype).toBe('float32');
        test_util_1.expectArraysClose(mean, [2, 1 / 3]);
        test_util_1.expectArraysClose(variance, [2 / 3, 0.222]);
    });
    it('2D, axis=-1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var _a = tf.moments(a, -1), mean = _a.mean, variance = _a.variance;
        expect(mean.shape).toEqual([2]);
        expect(mean.dtype).toBe('float32');
        expect(variance.shape).toEqual([2]);
        expect(variance.dtype).toBe('float32');
        test_util_1.expectArraysClose(mean, [2, 1 / 3]);
        test_util_1.expectArraysClose(variance, [2 / 3, 0.222]);
    });
    it('axis=0,1 in 2D array', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var _a = tf.moments(a, [0, 1]), mean = _a.mean, variance = _a.variance;
        expect(mean.shape).toEqual([]);
        expect(mean.dtype).toBe('float32');
        expect(variance.shape).toEqual([]);
        expect(variance.dtype).toBe('float32');
        test_util_1.expectArraysClose(mean, [7 / 6]);
        test_util_1.expectArraysClose(variance, [1.1389]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.moments({}); })
            .toThrowError(/Argument 'x' passed to 'moments' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var _a = tf.moments([1, 2, 3, 0, 0, 1]), mean = _a.mean, variance = _a.variance;
        expect(mean.dtype).toBe('float32');
        expect(variance.dtype).toBe('float32');
        test_util_1.expectNumbersClose(mean.get(), 7 / 6);
        test_util_1.expectNumbersClose(variance.get(), 1.1389);
    });
});
jasmine_util_1.describeWithFlags('Reduction: norm', test_util_1.ALL_ENVS, function () {
    it('scalar norm', function () {
        var a = tf.scalar(-22.0);
        var norm = tf.norm(a);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 22);
    });
    it('vector inf norm', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        var norm = tf.norm(a, Infinity);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 4);
    });
    it('vector -inf norm', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        var norm = tf.norm(a, -Infinity);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 1);
    });
    it('vector 1 norm', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        var norm = tf.norm(a, 1);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 10);
    });
    it('vector euclidean norm', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        var norm = tf.norm(a, 'euclidean');
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 5.4772);
    });
    it('vector 2-norm', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        var norm = tf.norm(a, 2);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 5.4772);
    });
    it('vector >2-norm to throw error', function () {
        var a = tf.tensor1d([1, -2, 3, -4]);
        expect(function () { return tf.norm(a, 3); }).toThrowError();
    });
    it('matrix inf norm', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [0, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 4);
    });
    it('matrix -inf norm', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 0, 1], [3, 2]);
        var norm = tf.norm(a, -Infinity, [0, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 1);
    });
    it('matrix 1 norm', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]);
        var norm = tf.norm(a, 1, [0, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 5);
    });
    it('matrix euclidean norm', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]);
        var norm = tf.norm(a, 'euclidean', [0, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 4.123);
    });
    it('matrix fro norm', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]);
        var norm = tf.norm(a, 'fro', [0, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 4.123);
    });
    it('matrix other norm to throw error', function () {
        var a = tf.tensor2d([1, 2, -3, 1, 1, 1], [3, 2]);
        expect(function () { return tf.norm(a, 2, [0, 1]); }).toThrowError();
    });
    it('propagates NaNs for norm', function () {
        var a = tf.tensor2d([1, 2, 3, NaN, 0, 1], [3, 2]);
        var norm = tf.norm(a);
        expect(norm.dtype).toBe('float32');
        expect(norm.get()).toEqual(NaN);
    });
    it('axis=null in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity);
        expect(norm.shape).toEqual([]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('2D array norm with keep dim', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, null, true);
        expect(norm.shape).toEqual([1, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('axis=0 in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [0]);
        expect(norm.shape).toEqual([2]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3, 2]);
    });
    it('axis=1 in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [1]);
        expect(norm.dtype).toBe('float32');
        expect(norm.shape).toEqual([3]);
        test_util_1.expectArraysClose(norm, [2, 3, 1]);
    });
    it('axis=1 keepDims in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [1], true);
        expect(norm.dtype).toBe('float32');
        expect(norm.shape).toEqual([3, 1]);
        test_util_1.expectArraysClose(norm, [2, 3, 1]);
    });
    it('2D norm with axis=1 provided as number', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [2, 3]);
        var norm = tf.norm(a, Infinity, 1);
        expect(norm.shape).toEqual([2]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3, 1]);
    });
    it('axis=0,1 in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [0, 1]);
        expect(norm.shape).toEqual([]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('axis=0,1 keepDims in 2D array norm', function () {
        var a = tf.tensor2d([1, 2, 3, 0, 0, 1], [3, 2]);
        var norm = tf.norm(a, Infinity, [0, 1], true);
        expect(norm.shape).toEqual([1, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('3D norm axis=0,1, matrix inf norm', function () {
        var a = tf.tensor3d([1, 2, -3, 1, 0, 1], [3, 2, 1]);
        var norm = tf.norm(a, Infinity, [0, 1]);
        expect(norm.shape).toEqual([1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [4]);
    });
    it('axis=0,1 keepDims in 3D array norm', function () {
        var a = tf.tensor3d([1, 2, 3, 0, 0, 1], [3, 2, 1]);
        var norm = tf.norm(a, Infinity, [0, 1], true);
        expect(norm.shape).toEqual([1, 1, 1]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('axis=0,1 keepDims in 3D array norm', function () {
        var a = tf.tensor3d([1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1], [3, 2, 2]);
        var norm = tf.norm(a, Infinity, [0, 1], true);
        expect(norm.shape).toEqual([1, 1, 2]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [4, 3]);
    });
    it('axis=null in 3D array norm', function () {
        var a = tf.tensor3d([1, 2, 3, 0, 0, 1], [3, 2, 1]);
        var norm = tf.norm(a, Infinity);
        expect(norm.shape).toEqual([]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('axis=null in 4D array norm', function () {
        var a = tf.tensor4d([1, 2, 3, 0, 0, 1], [3, 2, 1, 1]);
        var norm = tf.norm(a, Infinity);
        expect(norm.shape).toEqual([]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [3]);
    });
    it('axis=0,1 in 4D array norm', function () {
        var a = tf.tensor4d([
            1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1,
            1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1
        ], [3, 2, 2, 2]);
        var norm = tf.norm(a, Infinity, [0, 1]);
        expect(norm.shape).toEqual([2, 2]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [4, 3, 4, 3]);
    });
    it('axis=0,1 in 4D array norm', function () {
        var a = tf.tensor4d([
            1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1,
            1, 2, 3, 0, 0, 1, 1, 2, 3, 0, 0, 1
        ], [3, 2, 2, 2]);
        var norm = tf.norm(a, Infinity, [0, 1], true);
        expect(norm.shape).toEqual([1, 1, 2, 2]);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectArraysClose(norm, [4, 3, 4, 3]);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.norm({}); })
            .toThrowError(/Argument 'x' passed to 'norm' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var norm = tf.norm([1, -2, 3, -4], 1);
        expect(norm.dtype).toBe('float32');
        test_util_1.expectNumbersClose(norm.get(), 10);
    });
});
jasmine_util_1.describeWithFlags('Reduction: all', test_util_1.ALL_ENVS, function () {
    it('Tensor1D', function () {
        var a = tf.tensor1d([0, 0, 0], 'bool');
        test_util_1.expectNumbersClose(tf.all(a).get(), 0);
        a = tf.tensor1d([1, 0, 1], 'bool');
        test_util_1.expectNumbersClose(tf.all(a).get(), 0);
        a = tf.tensor1d([1, 1, 1], 'bool');
        test_util_1.expectNumbersClose(tf.all(a).get(), 1);
    });
    it('ignores NaNs', function () {
        var a = tf.tensor1d([1, NaN, 1], 'bool');
        expect(tf.all(a).get()).toEqual(1);
    });
    it('2D', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
        test_util_1.expectNumbersClose(tf.all(a).get(), 0);
    });
    it('2D axis=[0,1]', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        test_util_1.expectNumbersClose(tf.all(a, [0, 1]).get(), 0);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
        var r = tf.all(a, 0);
        expect(r.shape).toEqual([2]);
        test_util_1.expectArraysClose(r, [0, 0]);
        r = tf.all(a, 1);
        expect(r.shape).toEqual([2]);
        test_util_1.expectArraysClose(r, [1, 0]);
    });
    it('2D, axis=0, keepDims', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = a.all(0, true);
        expect(r.shape).toEqual([1, 3]);
        test_util_1.expectArraysClose(r, [0, 1, 0]);
    });
    it('2D, axis=1 provided as a number', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.all(a, 1);
        test_util_1.expectArraysClose(r, [0, 0]);
    });
    it('2D, axis = -1 provided as a number', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.all(a, -1);
        test_util_1.expectArraysClose(r, [0, 0]);
    });
    it('2D, axis=[1]', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.all(a, [1]);
        test_util_1.expectArraysClose(r, [0, 0]);
    });
    it('throws when dtype is not boolean', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2]);
        expect(function () { return tf.all(a); }).toThrowError(/Error Tensor must be of type bool/);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.all({}); })
            .toThrowError(/Argument 'x' passed to 'all' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var a = [0, 0, 0];
        test_util_1.expectNumbersClose(tf.all(a).get(), 0);
    });
});
jasmine_util_1.describeWithFlags('Reduction: any', test_util_1.ALL_ENVS, function () {
    it('Tensor1D', function () {
        var a = tf.tensor1d([0, 0, 0], 'bool');
        test_util_1.expectNumbersClose(tf.any(a).get(), 0);
        a = tf.tensor1d([1, 0, 1], 'bool');
        test_util_1.expectNumbersClose(tf.any(a).get(), 1);
        a = tf.tensor1d([1, 1, 1], 'bool');
        test_util_1.expectNumbersClose(tf.any(a).get(), 1);
    });
    it('ignores NaNs', function () {
        var a = tf.tensor1d([1, NaN, 0], 'bool');
        expect(tf.any(a).get()).toEqual(1);
    });
    it('2D', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
        test_util_1.expectNumbersClose(tf.any(a).get(), 1);
    });
    it('2D axis=[0,1]', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        test_util_1.expectNumbersClose(tf.any(a, [0, 1]).get(), 1);
    });
    it('2D, axis=0', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2], 'bool');
        var r = tf.any(a, 0);
        expect(r.shape).toEqual([2]);
        test_util_1.expectArraysClose(r, [1, 1]);
        r = tf.any(a, 1);
        expect(r.shape).toEqual([2]);
        test_util_1.expectArraysClose(r, [1, 0]);
    });
    it('2D, axis=0, keepDims', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = a.any(0, true);
        expect(r.shape).toEqual([1, 3]);
        test_util_1.expectArraysClose(r, [1, 1, 0]);
    });
    it('2D, axis=1 provided as a number', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.any(a, 1);
        test_util_1.expectArraysClose(r, [1, 1]);
    });
    it('2D, axis = -1 provided as a number', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.any(a, -1);
        test_util_1.expectArraysClose(r, [1, 1]);
    });
    it('2D, axis=[1]', function () {
        var a = tf.tensor2d([1, 1, 0, 0, 1, 0], [2, 3], 'bool');
        var r = tf.any(a, [1]);
        test_util_1.expectArraysClose(r, [1, 1]);
    });
    it('throws when dtype is not boolean', function () {
        var a = tf.tensor2d([1, 1, 0, 0], [2, 2]);
        expect(function () { return tf.any(a); }).toThrowError(/Error Tensor must be of type bool/);
    });
    it('throws when passed a non-tensor', function () {
        expect(function () { return tf.any({}); })
            .toThrowError(/Argument 'x' passed to 'any' must be a Tensor/);
    });
    it('accepts a tensor-like object', function () {
        var a = [0, 0, 0];
        test_util_1.expectNumbersClose(tf.any(a).get(), 0);
    });
});
//# sourceMappingURL=reduction_ops_test.js.map