main.js (1325B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Returns the number of singleton dimensions. 25 * 26 * ## Notes 27 * 28 * - A singleton dimension is a dimension whose size is equal to `1`. 29 * 30 * @param {NonNegativeIntegerArray} shape - array shape 31 * @returns {NonNegativeInteger} number of singleton dimensions 32 * 33 * @example 34 * var shape = [ 2, 1, 1 ]; 35 * 36 * var n = singletonDimensions( shape ); 37 * // returns 2 38 * 39 * @example 40 * var shape = [ 2, 2, 2 ]; 41 * 42 * var n = singletonDimensions( shape ); 43 * // returns 0 44 */ 45 function singletonDimensions( shape ) { 46 var cnt; 47 var i; 48 49 cnt = 0; 50 for ( i = 0; i < shape.length; i++ ) { 51 if ( shape[ i ] === 1 ) { 52 cnt += 1; 53 } 54 } 55 return cnt; 56 } 57 58 59 // EXPORTS // 60 61 module.exports = singletonDimensions;