Skip to content

Latest commit

 

History

History
380 lines (258 loc) · 11.7 KB

File metadata and controls

380 lines (258 loc) · 11.7 KB

swhere

Take elements from one of two single-precision floating-point strided arrays depending on a condition.

Usage

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

swhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut )

Takes elements from one of two single-precision floating-point strided arrays depending on a condition.

var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var y = new Float32Array( [ 4.0, 5.0, 6.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere( 3, condition, 1, x, 1, y, 1, out, 1 );
// out => <Float32Array>[ 1.0, 5.0, 3.0 ]

The function has the following parameters:

  • N: number of indexed elements.
  • condition: condition BooleanArray.
  • strideC: stride length for condition.
  • x: first input Float32Array.
  • strideX: stride length for x.
  • y: second input Float32Array.
  • strideY: stride length for y.
  • out: output Float32Array.
  • strideOut: stride length for out.

The N and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to select from every other element:

var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, false, true, true, false ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere( 3, condition, 2, x, 2, y, 2, out, 1 );
// out => <Float32Array>[ 1.0, 9.0, 5.0 ]

Note that indexing is relative to the first index. To introduce an offset, use typed array views.

var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

// Initial arrays...
var condition0 = new BooleanArray( [ false, true, false, true, false, true ] );
var x0 = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y0 = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out0 = new Float32Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );

// Create offset views...
var condition1 = new BooleanArray( condition0.buffer, condition0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
var out1 = new Float32Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

swhere( 3, condition1, 2, x1, 2, y1, 2, out1, 1 );
// out0 => <Float32Array>[ 0.0, 2.0, 4.0, 6.0, 0.0, 0.0 ]

swhere.ndarray( N, condition, strideC, offsetC, x, strideX, offsetX, y, strideY, offsetY, out, strideOut, offsetOut )

Takes elements from one of two single-precision floating-point strided arrays depending on a condition using alternative indexing semantics.

var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ true, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0 ] );
var y = new Float32Array( [ 4.0, 5.0, 6.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere.ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );
// out => <Float32Array>[ 1.0, 5.0, 3.0 ]

The function has the following additional parameters:

  • offsetC: starting index for condition.
  • offsetX: starting index for x.
  • offsetY: starting index for y.
  • offsetOut: starting index for out.

While typed array views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to select from every other element starting from the second element:

var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );

var condition = new BooleanArray( [ false, true, false, false, false, true ] );
var x = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
var y = new Float32Array( [ 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
var out = new Float32Array( [ 0.0, 0.0, 0.0 ] );

swhere.ndarray( 3, condition, 2, 1, x, 2, 1, y, 2, 1, out, 1, 0 );
// out => <Float32Array>[ 2.0, 10.0, 6.0 ]

Notes

  • If N <= 0, both functions return out unchanged.
  • The condition argument must be a BooleanArray.

Examples

var bernoulli = require( '@stdlib/random/array/bernoulli' );
var uniform = require( '@stdlib/random/array/uniform' );
var BooleanArray = require( '@stdlib/array/bool' );
var Float32Array = require( '@stdlib/array/float32' );
var swhere = require( '@stdlib/blas/ext/base/swhere' );

var cbuf = bernoulli( 20, 0.5, {
    'dtype': 'uint8'
});
var condition = new BooleanArray( cbuf.buffer );
console.log( condition );
var x = uniform( 20, 0.0, 100.0, {
    'dtype': 'float32'
});
console.log( x );
var y = uniform( 20, -100.0, 0.0, {
    'dtype': 'float32'
});
console.log( y );

var out = new Float32Array( condition.length );
console.log( out );

swhere( condition.length, condition, 1, x, 1, y, 1, out, 1 );
console.log( out );

C APIs

Usage

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

stdlib_strided_swhere( N, *Condition, strideC, *X, strideX, *Y, strideY, *Out, strideOut )

Takes elements from one of two single-precision floating-point strided arrays depending on a condition.

#include <stdbool.h>

const bool condition[] = { true, false, true };
const float x[] = { 1.0f, 2.0f, 3.0f };
const float y[] = { 4.0f, 5.0f, 6.0f };
float out[] = { 0.0f, 0.0f, 0.0f };

stdlib_strided_swhere( 3, condition, 1, x, 1, y, 1, out, 1 );

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • Condition: [in] bool* condition array.
  • strideC: [in] CBLAS_INT stride length for Condition.
  • X: [in] float* first input array.
  • strideX: [in] CBLAS_INT stride length for X.
  • Y: [in] float* second input array.
  • strideY: [in] CBLAS_INT stride length for Y.
  • Out: [out] float* output array.
  • strideOut: [in] CBLAS_INT stride length for Out.
void stdlib_strided_swhere( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const float *X, const CBLAS_INT strideX, const float *Y, const CBLAS_INT strideY, float *Out, const CBLAS_INT strideOut );

stdlib_strided_swhere_ndarray( N, *Condition, strideC, offsetC, *X, strideX, offsetX, *Y, strideY, offsetY, *Out, strideOut, offsetOut )

Takes elements from one of two single-precision floating-point strided arrays depending on a condition using alternative indexing semantics.

#include <stdbool.h>

const bool condition[] = { true, false, true };
const float x[] = { 1.0f, 2.0f, 3.0f };
const float y[] = { 4.0f, 5.0f, 6.0f };
float out[] = { 0.0f, 0.0f, 0.0f };

stdlib_strided_swhere_ndarray( 3, condition, 1, 0, x, 1, 0, y, 1, 0, out, 1, 0 );

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • Condition: [in] bool* condition array.
  • strideC: [in] CBLAS_INT stride length for Condition.
  • offsetC: [in] CBLAS_INT starting index for Condition.
  • X: [in] float* first input array.
  • strideX: [in] CBLAS_INT stride length for X.
  • offsetX: [in] CBLAS_INT starting index for X.
  • Y: [in] float* second input array.
  • strideY: [in] CBLAS_INT stride length for Y.
  • offsetY: [in] CBLAS_INT starting index for Y.
  • Out: [out] float* output array.
  • strideOut: [in] CBLAS_INT stride length for Out.
  • offsetOut: [in] CBLAS_INT starting index for Out.
void stdlib_strided_swhere_ndarray( const CBLAS_INT N, const bool *Condition, const CBLAS_INT strideC, const CBLAS_INT offsetC, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const float *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY, float *Out, const CBLAS_INT strideOut, const CBLAS_INT offsetOut );

Examples

#include "stdlib/blas/ext/base/swhere.h"
#include <stdio.h>
#include <stdbool.h>

int main( void ) {
    // Create strided arrays:
    const bool condition[] = { true, false, true, false, true };
    const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
    const float y[] = { 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
    float out[] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };

    // Specify the number of indexed elements:
    const int N = 5;

    // Specify stride lengths:
    const int strideC = 1;
    const int strideX = 1;
    const int strideY = 1;
    const int strideOut = 1;

    // Select from `x` or `y` based on the condition array:
    stdlib_strided_swhere( N, condition, strideC, x, strideX, y, strideY, out, strideOut );

    // Print the result:
    for ( int i = 0; i < N; i++ ) {
        printf( "out[ %i ] = %f\n", i, out[ i ] );
    }
}