All files main.js

99.1% Statements 221/223
98% Branches 49/50
100% Functions 4/4
99.1% Lines 221/223

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 2241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 362x 32x 32x 362x 13x 45x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 6x 6x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 9x 9x 29x 9x 9x 11x 29x 1x 1x 29x     10x 29x 28x 28x 1x 3x 3x 1x 1x 28x 22x 22x 28x 29x 1x 1x 3x 2x 2x 3x 1x 1x 3x 3x 1x 1x 29x 1x 1x 3x 3x 1x 1x 7x 29x 20x 20x 20x 20x 29x 20x 20x 7x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 9x 9x 31x 40x 1x 1x 40x 23x 9x 9x 14x 14x 14x 12x 12x 14x 23x 40x 7x 7x 9x 9x 40x 1x 1x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var isCollection = require( '@stdlib/assert/is-collection' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var getDType = require( '@stdlib/array/dtype' );
var dtypes = require( '@stdlib/array/dtypes' );
var empty = require( '@stdlib/array/empty' );
var exp = require( '@stdlib/math/base/special/exp' );
var format = require( '@stdlib/string/format' );
 
 
// VARIABLES //
 
var IDTYPES = dtypes( 'real_and_generic' );
var ODTYPES = dtypes( 'floating_point_and_generic' );
 
 
// FUNCTIONS //
 
/**
* Returns a boolean indicating if a provided data type is allowed.
*
* @private
* @param {string} dt - data type
* @param {StringArray} list - list of allowed data types
* @returns {boolean} boolean indicating if a data type is allowed
*/
function isAllowedDataType( dt, list ) {
	var i;
	for ( i = 0; i < list.length; i++ ) {
		if ( list[ i ] === dt ) {
			return true;
		}
	}
	return false;
}
 
/**
* Returns the default output data type.
*
* @private
* @param {string} dt - input data type
* @returns {string} output data type
*/
function defaultOutputDType( dt ) {
	if ( dt === 'float32' || dt === 'generic' ) {
		return dt;
	}
	return 'float64';
}
 
/**
* Computes the softmax function for each element in an input array and assigns results to a provided output array.
*
* @private
* @param {Collection} x - input array
* @param {Collection} out - output array
* @throws {TypeError} first argument must be a collection
* @throws {TypeError} second argument must be a collection
* @throws {RangeError} output array must have the same length as the input array
* @returns {Collection} output array
*/
function assign( x, out ) {
	var count;
	var max;
	var sum;
	var len;
	var p;
	var v;
	var i;
 
	if ( !isCollection( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) );
	}
	if ( !isCollection( out ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a collection. Value: `%s`.', out ) );
	}
	len = x.length;
	if ( out.length !== len ) {
		throw new RangeError( format( 'invalid argument. Output array must have the same length as the input array. Input length: `%u`. Output length: `%u`.', len, out.length ) );
	}
	if ( len === 0 ) {
		return out;
	}
	max = -Infinity;
	for ( i = 0; i < len; i++ ) {
		v = x[ i ];
		if ( isNaN( v ) ) {
			for ( i = 0; i < len; i++ ) {
				out[ i ] = NaN;
			}
			return out;
		}
		if ( v > max ) {
			max = v;
		}
	}
	if ( max === Infinity ) {
		count = 0;
		for ( i = 0; i < len; i++ ) {
			if ( x[ i ] === Infinity ) {
				count += 1;
			}
		}
		p = 1.0 / count;
		for ( i = 0; i < len; i++ ) {
			out[ i ] = ( x[ i ] === Infinity ) ? p : 0.0;
		}
		return out;
	}
	if ( max === -Infinity ) {
		p = 1.0 / len;
		for ( i = 0; i < len; i++ ) {
			out[ i ] = p;
		}
		return out;
	}
	sum = 0.0;
	for ( i = 0; i < len; i++ ) {
		v = exp( x[ i ] - max );
		out[ i ] = v;
		sum += v;
	}
	for ( i = 0; i < len; i++ ) {
		out[ i ] /= sum;
	}
	return out;
}
 
 
// MAIN //
 
/**
* Computes the softmax function for each element in an input array.
*
* @name softmax
* @type {Function}
* @param {Collection} x - input array
* @param {Object} [options] - function options
* @param {string} [options.dtype] - output array data type
* @throws {TypeError} first argument must be a collection
* @throws {TypeError} first argument must be a collection having a supported data type
* @throws {TypeError} options argument must be a plain object
* @throws {TypeError} must provide a valid `dtype` option
* @returns {(Array|TypedArray)} output array
*
* @example
* var out = softmax( [ 1.0, 2.0, 3.0 ] );
* // returns [ ~0.090, ~0.245, ~0.665 ]
*
* @example
* var out = softmax( [ 1.0, 2.0, 3.0 ], {
*     'dtype': 'float64'
* });
* // returns <Float64Array>[ ~0.090, ~0.245, ~0.665 ]
*
* @example
* var y = [ 0.0, 0.0, 0.0 ];
*
* var out = softmax.assign( [ 1.0, 2.0, 3.0 ], y );
* // returns [ ~0.090, ~0.245, ~0.665 ]
*
* var bool = ( out === y );
* // returns true
*/
function softmax( x, options ) {
	var outputDType;
	var inputDType;
	var out;
 
	if ( !isCollection( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a collection. Value: `%s`.', x ) );
	}
	inputDType = getDType( x );
	if ( !isAllowedDataType( inputDType, IDTYPES ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a collection having a supported data type. Value: `%s`.', x ) );
	}
	if ( arguments.length > 1 ) {
		if ( !isPlainObject( options ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a plain object. Value: `%s`.', options ) );
		}
		if ( hasOwnProp( options, 'dtype' ) ) {
			outputDType = options.dtype;
			if ( !isAllowedDataType( outputDType, ODTYPES ) ) {
				throw new TypeError( format( 'invalid option. `%s` option must be one of the following: `%s`. Option: `%s`.', 'dtype', ODTYPES.join( ', ' ), outputDType ) );
			}
		}
	}
	if ( outputDType === void 0 ) {
		outputDType = defaultOutputDType( inputDType );
	}
	out = empty( x.length, outputDType );
	return assign( x, out );
}
 
setReadOnly( softmax, 'assign', assign );
 
 
// EXPORTS //
 
module.exports = softmax;