Skip to content

Latest commit

 

History

History
320 lines (205 loc) · 9.41 KB

File metadata and controls

320 lines (205 loc) · 9.41 KB

dcombinations

Compute combinations of a specified length from double-precision floating-point strided array elements.

Usage

var dcombinations = require( '@stdlib/blas/ext/base/dcombinations' );

dcombinations( N, k, replacement, x, strideX, out, LDO )

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 ]

Notes

  • If N <= 0 or k <= 0, both functions return out unchanged.
  • If replacement is false and k > N, both functions return out unchanged.
  • For an input array x of length N, the output array out must contain at least C(N, k) combinations if replacement is false, or C(N+k-1, k) combinations if replacement is true, where each combination has k elements.

Examples

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 );

C APIs

Usage

#include "stdlib/blas/ext/base/dcombinations.h"

stdlib_strided_dcombinations( N, k, replacement, *X, strideX, *Out, LDO )

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_INT number of indexed elements.
  • k: [in] CBLAS_INT number of elements to combine.
  • replacement: [in] bool boolean indicating whether to allow duplication in combination.
  • X: [in] double* input array.
  • strideX: [in] CBLAS_INT stride length.
  • Out: [out] double* output array.
  • LDO: [in] CBLAS_INT stride length for the leading dimension of Out.
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_INT number of indexed elements.
  • k: [in] CBLAS_INT number of elements to combine.
  • replacement: [in] bool boolean indicating whether to allow duplication in combination.
  • X: [in] double* input array.
  • strideX: [in] CBLAS_INT stride length.
  • offsetX: [in] CBLAS_INT starting index for X.
  • Out: [out] double* output array.
  • strideOut1: [in] CBLAS_INT stride length of the first dimension of Out.
  • strideOut2: [in] CBLAS_INT stride length of the second dimension of Out.
  • offsetOut: [in] CBLAS_INT starting index for Out.
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 );

Examples

#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 ] );
    }
}