Skip to content

Latest commit

 

History

History

README.md

zsumkbn

Calculate the sum of double-precision complex floating-point strided array elements using an improved Kahan–Babuška algorithm.

Usage

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

zsumkbn( N, x, strideX )

Computes the sum of double-precision complex floating-point strided array elements using an improved Kahan–Babuška algorithm.

var Complex128Array = require( '@stdlib/array/complex128' );

var x = new Complex128Array( [ 1.0, -2.0, 2.0, 3.0 ] );

var v = zsumkbn( x.length, x, 1 );
// returns <Complex128>[ 3.0, 1.0 ]

The function has the following parameters:

  • N: number of indexed elements.
  • x: input Complex128Array.
  • strideX: stride length for x.

The N and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element:

var Complex128Array = require( '@stdlib/array/complex128' );

var x = new Complex128Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );

var v = zsumkbn( 2, x, 2 );
// returns <Complex128>[ -1.0, 5.0 ]

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

var Complex128Array = require( '@stdlib/array/complex128' );

var x0 = new Complex128Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
var x1 = new Complex128Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var v = zsumkbn( 2, x1, 2 );
// returns <Complex128>[ 5.0, 2.0 ]

zsumkbn.ndarray( N, x, strideX, offsetX )

Computes the sum of double-precision complex floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics.

var Complex128Array = require( '@stdlib/array/complex128' );

var x = new Complex128Array( [ 1.0, -2.0, 2.0, 3.0 ] );

var v = zsumkbn.ndarray( 2, x, 1, 0 );
// returns <Complex128>[ 3.0, 1.0 ]

The function has the following additional parameters:

  • offsetX: starting index for x.

While typed array views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other element starting from the second element:

var Complex128Array = require( '@stdlib/array/complex128' );

var x = new Complex128Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );

var v = zsumkbn.ndarray( 2, x, 2, 1 );
// returns <Complex128>[ 5.0, 2.0 ]

Notes

  • If N <= 0, both functions return 0.0 + 0.0i.

Examples

var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var Complex128Array = require( '@stdlib/array/complex128' );
var zsumkbn = require( '@stdlib/blas/ext/base/zsumkbn' );

var xbuf = discreteUniform( 10, -100, 100, {
    'dtype': 'float64'
});
console.log( xbuf );

var x = new Complex128Array( xbuf );
var v = zsumkbn( x.length, x, 1 );
console.log( v );

C APIs

Usage

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

stdlib_strided_zsumkbn( N, *X, strideX )

Computes the sum of double-precision complex floating-point strided array elements using an improved Kahan–Babuška algorithm.

#include "stdlib/complex/float64/ctor.h"

const stdlib_complex128_t x[] = {
    stdlib_complex128( 1.0, 2.0 ),
    stdlib_complex128( 3.0, 4.0 )
};

stdlib_complex128_t v = stdlib_strided_zsumkbn( 2, x, 1 );

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • X: [in] stdlib_complex128_t* input array.
  • strideX: [in] CBLAS_INT stride length for X.
stdlib_complex128_t stdlib_strided_zsumkbn( const CBLAS_INT N, const stdlib_complex128_t *X, const CBLAS_INT strideX );

stdlib_strided_zsumkbn_ndarray( N, *X, strideX, offsetX )

Computes the sum of double-precision complex floating-point strided array elements using an improved Kahan–Babuška algorithm and alternative indexing semantics.

#include "stdlib/complex/float64/ctor.h"

const stdlib_complex128_t x[] = {
    stdlib_complex128( 1.0, 2.0 ),
    stdlib_complex128( 3.0, 4.0 )
};

stdlib_complex128_t v = stdlib_strided_zsumkbn_ndarray( 2, x, 1, 0 );

The function accepts the following arguments:

  • N: [in] CBLAS_INT number of indexed elements.
  • X: [in] stdlib_complex128_t* input array.
  • strideX: [in] CBLAS_INT stride length for X.
  • offsetX: [in] CBLAS_INT starting index for X.
stdlib_complex128_t stdlib_strided_zsumkbn_ndarray( const CBLAS_INT N, const stdlib_complex128_t *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );

Examples

#include "stdlib/blas/ext/base/zsumkbn.h"
#include "stdlib/complex/float64/ctor.h"
#include "stdlib/complex/float64/real.h"
#include "stdlib/complex/float64/imag.h"
#include <stdio.h>

int main( void ) {
    // Create a strided array:
    const stdlib_complex128_t x[] = {
        stdlib_complex128( 1.0, 2.0 ),
        stdlib_complex128( 3.0, 4.0 ),
        stdlib_complex128( 5.0, 6.0 ),
        stdlib_complex128( 7.0, 8.0 )
    };

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

    // Specify the stride length:
    const int strideX = 1;

    // Compute the sum:
    stdlib_complex128_t v = stdlib_strided_zsumkbn( N, x, strideX );

    // Print the result:
    printf( "sum: %lf + %lfi\n", stdlib_complex128_real( v ), stdlib_complex128_imag( v ) );
}

References

  • Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." Zeitschrift Für Angewandte Mathematik Und Mechanik 54 (1): 39–51. doi:10.1002/zamm.19740540106.