dasum

Compute the sum of absolute values (L1 norm).

The L1 norm is defined as

double-vertical-bar bold x double-vertical-bar Subscript 1 Baseline equals sigma-summation Underscript i equals 0 Overscript n minus 1 Endscripts StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue

Usage

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

dasum( N, x, stride )

Computes the sum of absolute values.

var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var sum = dasum( x.length, x, 1 );
// returns 19.0

The function accepts the following parameters:

  • N: number of elements to sum.
  • x: input Float64Array.
  • stride: index increment.

The N and stride parameters determine which elements in x are used to compute the sum. For example, to sum every other value,

var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var N = floor( x.length / 2 );
var stride = 2;

var sum = dasum( N, x, stride );
// returns 10.0

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

var Float64Array = require( '@stdlib/array/float64' );
var floor = require( '@stdlib/math/base/special/floor' );

// Initial array...
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );

// Create an offset view...
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element

var N = floor( x0.length / 2 );

// Sum every other value...
var sum = dasum( N, x1, 2 );
// returns 12.0

If either N or stride is less than or equal to 0, the function returns 0.

dasum.ndarray( N, x, stride, offset )

Computes the sum of absolute values, with alternative indexing semantics.

var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );

var sum = dasum.ndarray( x.length, x, 1, 0 );
// returns 19.0

The function accepts the following additional parameters:

  • offset: starting index.

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 sum the last three elements,

var Float64Array = require( '@stdlib/array/float64' );

var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );

var sum = dasum.ndarray( 3, x, 1, x.length-3 );
// returns 15.0

// Using a negative stride to sum from the last element:
sum = dasum.ndarray( 3, x, -1, x.length-1 );
// returns 15.0

dasum.wasm( [options] )

Returns a memory managed function to compute the sum of absolute values.

var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

// Number of data elements:
var N = 5;

// Allocate space on the heap:
var bytes = wasm.malloc( N * 8 ); // 8 bytes per double

// Create a Float64Array view:
var view = new Float64Array( bytes.buffer, bytes.byteOffset, N );

// Copy data to the heap:
view.set( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );

// Compute the sum:
var s = wasm( N, bytes, 1 );
// returns 15.0

// Free the memory:
wasm.free( bytes );

For externally defined typed arrays, data must be copied to the heap.

var Uint8Array = require( '@stdlib/array/uint8' );
var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

// Externally defined data array:
var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );

// Allocate space on the heap:
var bytes = wasm.malloc( x.length * x.BYTES_PER_ELEMENT );

// Copy data to the heap:
bytes.set( new Uint8Array( x.buffer ) );

// Compute the sum:
var s = wasm( x.length, bytes, 1 );
// returns 15.0

// Free the memory:
wasm.free( bytes );

The method accepts the following options:

  • memory: total memory. If not provided a buffer, setting the memory option instructs the returned function to allocate an internal memory store of the specified size.
  • stack: total stack size. Must be less than the memory option and large enough for a program's needs. Default: 1024 bytes.
  • buffer: ArrayBuffer serving as the underlying memory store. If not provided, each returned function will allocate and manage its own memory. If provided a memory option, the buffer byteLength must equal the specified total memory.

To create a function using an externally defined memory buffer, set the buffer option.

var ArrayBuffer = require( '@stdlib/array/buffer' );
var buffer = new ArrayBuffer( 16777216 ); // ~16MB

var wasm = dasum.wasm({
    'buffer': buffer
});

Providing external memory can be advantageous when wanting to a) centrally manage memory allocation, b) share memory between multiple memory managed functions, and/or c) limit the total amount of allocated memory within an application or library.

wasm.malloc( nbytes )

Allocates space on the heap and returns a bytes-wise typed array view (Uint8Array).

var wasm = dasum.wasm();

// Allocate 64 bytes:
var bytes = wasm.malloc( 64 );
bytes.getValue( ptr[, type] )

Returns a value at a specific memory address (represented by a byte index). By default, the function returns a double. Possible types include: 'i8', 'i16', 'i32', 'i64', 'float', and 'double'.

var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

var N = 3;

var bytes = wasm.malloc( N * 8 );
var view = new Float64Array( bytes.buffer, bytes.byteOffset, N );

view.set( [ 1.0, -2.0, 3.0 ] );

var ptr = 1 * 8; // 8 bytes per double
var y = bytes.getValue( ptr );
// returns -2.0

wasm.free( bytes );

While this method may be convenient when interacting with the bytes view directly, using a typed array view is likely to be more performant.

var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

var N = 3;

var bytes = wasm.malloc( N * 8 );
var view = new Float64Array( bytes.buffer, bytes.byteOffset, N );

view.set( [ 1.0, -2.0, 3.0 ] );

var y = view[ 1 ];
// returns -2.0

wasm.free( bytes );
bytes.setValue( ptr, value[, type] )

Sets a value at a specific memory address (represented by a byte index). By default, the function sets a double. Possible types include: 'i8', 'i16', 'i32', 'i64', 'float', and 'double'.

var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

var N = 3;

var bytes = wasm.malloc( N * 8 );
var view = new Float64Array( bytes.buffer, bytes.byteOffset, N );

view.set( [ 1.0, -2.0, 3.0 ] );

var ptr = 1 * 8; // 8 bytes per double
var y = bytes.getValue( ptr );
// returns -2.0

bytes.setValue( ptr, -10.0 );

y = bytes.getValue( ptr );
// returns -10.0

wasm.free( bytes );

While this method may be convenient when interacting with the bytes view directly, using a typed array view is likely to be more performant.

var Float64Array = require( '@stdlib/array/float64' );

var wasm = dasum.wasm();

var N = 3;

var bytes = wasm.malloc( N * 8 );
var view = new Float64Array( bytes.buffer, bytes.byteOffset, N );

view.set( [ 1.0, -2.0, 3.0 ] );

var y = view[ 1 ];
// returns -2.0

view[ 1 ] = -10.0;

y = view[ 1 ];
// returns -10.0

wasm.free( bytes );
wasm.free( heap )

Frees allocated space.

var wasm = dasum.wasm();

var bytes = wasm.malloc( 64 );

// ...

// Free the space and allow reallocation:
wasm.free( bytes );

Notes

  • If N <= 0, the sum is 0.
  • dasum() corresponds to the BLAS level 1 function dasum.

Examples

var round = require( '@stdlib/math/base/special/round' );
var randu = require( '@stdlib/random/base/randu' );
var Float64Array = require( '@stdlib/array/float64' );
var dasum = require( '@stdlib/blas/base/dasum' );

var rand;
var sign;
var x;
var i;

x = new Float64Array( 100 );
for ( i = 0; i < x.length; i++ ) {
    rand = round( randu()*100.0 );
    sign = randu();
    if ( sign < 0.5 ) {
        sign = -1.0;
    } else {
        sign = 1.0;
    }
    x[ i ] = sign * rand;
}
console.log( dasum( x.length, x, 1 ) );