All files ndarray.js

100% Statements 235/235
100% Branches 40/40
100% Functions 3/3
100% Lines 235/235

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 224 225 226 227 228 229 230 231 232 233 234 235 2363x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 71x 71x 71x 71x 71x 71x 20x 38x 38x 38x 71x 51x 85x 85x 85x 51x 71x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 319x 319x 319x 319x 319x 319x 319x 319x 319x 319x 72x 64x 64x 64x 72x 52x 52x 78x 78x 78x 52x 319x 247x 151x 151x 151x 247x 196x 196x 196x 66x 66x 66x 66x 196x 247x 319x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 8x 8x 63x 4x 4x 51x 63x 5x 5x 5x 63x 23x 23x 23x 23x 6x 23x 17x 17x 23x 23x 23x 128x 128x 128x 128x 264x 264x 264x 264x 128x 128x 128x 23x 23x 23x 48x 48x 48x 48x 48x 48x 14x 14x 48x 34x 34x 34x 48x 48x 48x 48x 48x 48x 191x 191x 191x 191x 122x 191x 20x 69x 49x 49x 191x 191x 191x 191x 191x 48x 23x 46x 63x 3x 3x 3x 3x 3x  
/**
* @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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' );
var binomcoef = require( '@stdlib/math/base/special/binomcoef' );
var dcopy = require( '@stdlib/blas/base/dcopy' ).ndarray;
var dfill = require( '@stdlib/blas/ext/base/dfill' ).ndarray;
 
 
// FUNCTIONS //
 
/**
* Initializes a workspace with the indices of the first combination of a specified length.
*
* @private
* @param {NonNegativeInteger} k - combination length
* @param {boolean} replacement - boolean indicating whether to allow duplication in combination
* @param {Int32Array} workspace - workspace array
* @param {integer} strideW - stride length for `workspace`
* @param {NonNegativeInteger} offsetW - starting index for `workspace`
*/
function firstCombination( k, replacement, workspace, strideW, offsetW ) {
	var iw;
	var p;
 
	iw = offsetW;
	if ( replacement ) {
		for ( p = 0; p < k; p++ ) {
			workspace[ iw ] = 0;
			iw += strideW;
		}
	} else {
		for ( p = 0; p < k; p++ ) {
			workspace[ iw ] = p;
			iw += strideW;
		}
	}
}
 
/**
* Advances a workspace to the indices of the next combination of a specified length.
*
* ## Notes
*
* -   If a workspace contains the last combination, the workspace is left unchanged.
*
* @private
* @param {NonNegativeInteger} k - combination length
* @param {NonNegativeInteger} N - number of indexed elements
* @param {boolean} replacement - boolean indicating whether to allow duplication in combination
* @param {Int32Array} workspace - workspace array
* @param {integer} strideW - stride length for `workspace`
* @param {NonNegativeInteger} offsetW - starting index for `workspace`
*/
function nextCombination( k, N, replacement, workspace, strideW, offsetW ) {
	var val;
	var iw;
	var i;
	var p;
 
	// Starting from the rightmost index, find the rightmost index which can be incremented...
	iw = offsetW + ( ( k-1 ) * strideW );
	i = k - 1;
	if ( replacement ) {
		while ( i >= 0 && workspace[ iw ] === N - 1 ) {
			i -= 1;
			iw -= strideW;
		}
		if ( i >= 0 ) {
			val = workspace[ iw ] + 1;
			for ( p = i; p < k; p++ ) {
				workspace[ iw ] = val;
				iw += strideW;
			}
		}
	} else {
		while ( i >= 0 && workspace[ iw ] === N - k + i ) {
			i -= 1;
			iw -= strideW;
		}
		if ( i >= 0 ) {
			workspace[ iw ] += 1;
			val = workspace[ iw ];
			for ( p = i + 1; p < k; p++ ) {
				iw += strideW;
				val += 1;
				workspace[ iw ] = val;
			}
		}
	}
}
 
 
// MAIN //
 
/**
* Computes combinations of a specified length from double-precision floating-point strided array elements using alternative indexing semantics.
*
* @param {NonNegativeInteger} N - number of indexed elements
* @param {NonNegativeInteger} k - number of elements to combine
* @param {boolean} replacement - boolean indicating whether to allow duplication in combination
* @param {Float64Array} x - input array
* @param {integer} strideX - stride length for `x`
* @param {NonNegativeInteger} offsetX - starting index for `x`
* @param {Float64Array} out - output array
* @param {integer} strideOut1 - stride length for the first dimension of `out`
* @param {integer} strideOut2 - stride length for the second dimension of `out`
* @param {NonNegativeInteger} offsetOut - starting index for `out`
* @param {Int32Array} workspace - workspace array
* @param {integer} strideW - stride length for `workspace`
* @param {NonNegativeInteger} offsetW - starting index for `workspace`
* @returns {Float64Array} output array
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var Int32Array = require( '@stdlib/array/int32' );
*
* var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
* var out = new Float64Array( 12 );
* var workspace = new Int32Array( 2 );
*
* dcombinations( 4, 2, false, x, 1, 0, out, 2, 1, 0, workspace, 1, 0 );
* // out => <Float64Array>[ 1.0, 2.0, 1.0, 3.0, 1.0, 4.0, 2.0, 3.0, 2.0, 4.0, 3.0, 4.0 ]
*/
function dcombinations( N, k, replacement, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut, workspace, strideW, offsetW ) { // eslint-disable-line max-len, max-params
	var runLen;
	var rem;
	var io;
	var iw;
	var oj;
	var np;
	var C;
	var L;
	var c;
	var j;
	var v;
 
	if ( N <= 0 || k <= 0 ) {
		return out;
	}
	if ( !replacement && k > N ) {
		return out;
	}
	// For `k` equal to one, each combination consists of a single element, and, thus, we can directly copy the input array elements to the output array (i.e., no need for a workspace)...
	if ( k === 1 ) {
		dcopy( N, x, strideX, offsetX, out, strideOut1, offsetOut );
		return out;
	}
	if ( isRowMajor( [ strideOut1, strideOut2 ] ) ) {
		// For row-major order, combinations are stored as contiguous rows, and, thus, we resolve one combination at a time, writing each combination's elements consecutively...
 
		// Compute the total number of combinations:
		if ( replacement ) {
			C = binomcoef( N + k - 1, k );
		} else {
			C = binomcoef( N, k );
		}
		firstCombination( k, replacement, workspace, strideW, offsetW );
		io = offsetOut;
		for ( c = 0; c < C; c++ ) {
			// Resolve the current combination's indices to input array values and write them to the output array:
			iw = offsetW;
			oj = io;
			for ( j = 0; j < k; j++ ) {
				out[ oj ] = x[ offsetX + ( workspace[ iw ] * strideX ) ];
				oj += strideOut2;
				iw += strideW;
			}
			io += strideOut1;
			nextCombination( k, N, replacement, workspace, strideW, offsetW );
		}
	} else {
		// For column-major order, the elements at a given combination position are stored contiguously within a column. As consecutive combinations share long prefixes, each column consists of runs of repeated elements (with run lengths equal to binomial coefficients), and, thus, we can fill each column as a sequence of contiguous runs...
		for ( j = 0; j < k; j++ ) {
			// The `j`-th column is determined by the `(j+1)`-element prefixes of each combination, enumerated in lexicographic order:
			L = j + 1;
			rem = k - 1 - j;
 
			// Enumerate only those prefixes which can be completed to a full combination. Without replacement, a prefix is extendable only if its last index leaves room for the remaining `rem` elements, and, thus, we restrict enumeration to a reduced universe of `N-rem` indices in order to avoid generating zero-length runs:
			if ( replacement ) {
				np = N;
				C = binomcoef( N + L - 1, L );
			} else {
				np = N - rem;
				C = binomcoef( np, L );
			}
			firstCombination( L, replacement, workspace, strideW, offsetW );
			io = offsetOut + ( j * strideOut2 );
 
			// Resolve the workspace index of each prefix's last element (i.e., the column's index value), which is invariant across prefixes:
			iw = offsetW + ( ( L-1 ) * strideW );
			for ( c = 0; c < C; c++ ) {
				v = workspace[ iw ];
 
				// Resolve the run length (i.e., the number of ways to complete the remaining `rem` positions with indices not less than `v`):
				if ( rem === 0 ) {
					runLen = 1;
				} else if ( replacement ) {
					runLen = binomcoef( N - v + rem - 1, rem );
				} else {
					runLen = binomcoef( N - 1 - v, rem );
				}
				// Write a run of the resolved input array value down the column:
				dfill( runLen, x[ offsetX + ( v * strideX ) ], out, strideOut1, io ); // eslint-disable-line max-len
				io += runLen * strideOut1;
				nextCombination( L, np, replacement, workspace, strideW, offsetW ); // eslint-disable-line max-len
			}
		}
	}
	return out;
}
 
 
// EXPORTS //
 
module.exports = dcombinations;