Compute combinations of a specified length from double-precision floating-point strided array elements.
var dcombinations = require( '@stdlib/blas/ext/base/dcombinations' );Computes combinations of a specified length from double-precision floating-point strided array elements.
var Float64Array = require( '@stdlib/array/float64' );
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var out = new Float64Array( 12 );
dcombinations( 4, 2, false, x, 1, out, 2 );
// 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 ]The function has the following parameters:
- N: number of indexed elements.
- k: number of elements to combine.
- replacement: boolean indicating whether to allow duplication in combination.
- x: input
Float64Array. - strideX: stride length.
- out: output
Float64Array. - LDO: stride length for the leading dimension of
out.
The N, k, and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to operate on every other element:
var Float64Array = require( '@stdlib/array/float64' );
var x = new Float64Array( [ 1.0, 0.0, 2.0, 0.0, 3.0 ] );
var out = new Float64Array( 6 );
dcombinations( 3, 2, false, x, 2, out, 2 );
// out => <Float64Array>[ 1.0, 2.0, 1.0, 3.0, 2.0, 3.0 ]Note that indexing is relative to the first index. To introduce an offset, use typed array views.
var Float64Array = require( '@stdlib/array/float64' );
// Initial array:
var x0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
// Create an offset view:
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var out = new Float64Array( 6 );
dcombinations( 3, 2, false, x1, 1, out, 2 );
// out => <Float64Array>[ 1.0, 2.0, 1.0, 3.0, 2.0, 3.0 ]dcombinations.ndarray( N, k, replacement, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut )
Computes combinations of a specified length from double-precision floating-point strided array elements using alternative indexing semantics.
var Float64Array = require( '@stdlib/array/float64' );
var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
var out = new Float64Array( 12 );
dcombinations.ndarray( 4, 2, false, x, 1, 0, out, 2, 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 ]The function has the following additional parameters:
- offsetX: starting index for
x. - strideOut1: stride length of the first dimension of
out. - strideOut2: stride length of the second dimension of
out. - offsetOut: starting index for
out.
While typed array views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on a starting index. For example, to access only the last three elements:
var Float64Array = require( '@stdlib/array/float64' );
var x = new Float64Array( [ 0.0, 0.0, 1.0, 2.0, 3.0 ] );
var out = new Float64Array( 6 );
dcombinations.ndarray( 3, 2, false, x, 1, 2, out, 2, 1, 0 );
// out => <Float64Array>[ 1.0, 2.0, 1.0, 3.0, 2.0, 3.0 ]- If
N <= 0ork <= 0, both functions returnoutunchanged. - If
replacementisfalseandk > N, both functions returnoutunchanged. - For an input array
xof lengthN, the output arrayoutmust contain at leastC(N, k)combinations ifreplacementisfalse, orC(N+k-1, k)combinations ifreplacementistrue, where each combination haskelements.
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var Float64Array = require( '@stdlib/array/float64' );
var dcombinations = require( '@stdlib/blas/ext/base/dcombinations' );
var N = 5;
var k = 2;
var x = discreteUniform( N, 1, 10, {
'dtype': 'float64'
});
console.log( x );
var out = new Float64Array( 20 );
dcombinations( N, k, false, x, 1, out, 2 );
console.log( out );#include "stdlib/blas/ext/base/dcombinations.h"Computes combinations of a specified length from double-precision floating-point strided array elements.
double x[] = { 1.0, 2.0, 3.0, 4.0 };
double out[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
stdlib_strided_dcombinations( 4, 2, false, x, 1, out, 2 );The function accepts the following arguments:
- N:
[in] CBLAS_INTnumber of indexed elements. - k:
[in] CBLAS_INTnumber of elements to combine. - replacement:
[in] boolboolean indicating whether to allow duplication in combination. - X:
[in] double*input array. - strideX:
[in] CBLAS_INTstride length. - Out:
[out] double*output array. - LDO:
[in] CBLAS_INTstride length for the leading dimension ofOut.
void stdlib_strided_dcombinations( const CBLAS_INT N, const CBLAS_INT k, const bool replacement, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT LDO );stdlib_strided_dcombinations_ndarray( N, k, replacement, *X, strideX, offsetX, *Out, strideOut1, strideOut2, offsetOut )
Computes combinations of a specified length from double-precision floating-point strided array elements using alternative indexing semantics.
double x[] = { 1.0, 2.0, 3.0, 4.0 };
double out[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
stdlib_strided_dcombinations_ndarray( 4, 2, false, x, 1, 0, out, 2, 1, 0 );The function accepts the following arguments:
- N:
[in] CBLAS_INTnumber of indexed elements. - k:
[in] CBLAS_INTnumber of elements to combine. - replacement:
[in] boolboolean indicating whether to allow duplication in combination. - X:
[in] double*input array. - strideX:
[in] CBLAS_INTstride length. - offsetX:
[in] CBLAS_INTstarting index forX. - Out:
[out] double*output array. - strideOut1:
[in] CBLAS_INTstride length of the first dimension ofOut. - strideOut2:
[in] CBLAS_INTstride length of the second dimension ofOut. - offsetOut:
[in] CBLAS_INTstarting index forOut.
void stdlib_strided_dcombinations_ndarray( const CBLAS_INT N, const CBLAS_INT k, const bool replacement, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut1, const CBLAS_INT strideOut2, const CBLAS_INT offsetOut );#include "stdlib/blas/ext/base/dcombinations.h"
#include <stdbool.h>
#include <stdio.h>
int main( void ) {
// Create a strided array:
double x[] = { 1.0, 2.0, 3.0, 4.0 };
// Create an output array:
double out[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
// Compute combinations:
stdlib_strided_dcombinations( 4, 2, false, x, 1, out, 2 );
// Print each combination:
for ( int i = 0; i < 6; i++ ) {
printf( "[ %lf, %lf ]\n", out[ i*2 ], out[ (i*2)+1 ] );
}
}