From 99a5f385fb37790c49cc4ab719d22a1d806d0ca3 Mon Sep 17 00:00:00 2001 From: aman-095 Date: Mon, 11 Nov 2024 18:17:52 +0530 Subject: [PATCH] feat: add support for stack for sasum --- lib/node_modules/@stdlib/blas/sasum/README.md | 167 ++++++++ .../@stdlib/blas/sasum/benchmark/benchmark.js | 103 +++++ .../blas/sasum/benchmark/benchmark.stack.js | 120 ++++++ .../blas/sasum/docs/img/equation_l1norm.svg | 44 ++ .../@stdlib/blas/sasum/docs/repl.txt | 39 ++ .../@stdlib/blas/sasum/docs/types/index.d.ts | 60 +++ .../@stdlib/blas/sasum/docs/types/test.ts | 102 +++++ .../@stdlib/blas/sasum/examples/index.js | 36 ++ .../@stdlib/blas/sasum/lib/index.js | 47 +++ .../@stdlib/blas/sasum/lib/main.js | 133 ++++++ .../@stdlib/blas/sasum/package.json | 73 ++++ .../@stdlib/blas/sasum/test/test.js | 390 ++++++++++++++++++ 12 files changed, 1314 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/sasum/README.md create mode 100644 lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.stack.js create mode 100644 lib/node_modules/@stdlib/blas/sasum/docs/img/equation_l1norm.svg create mode 100644 lib/node_modules/@stdlib/blas/sasum/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/sasum/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/sasum/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/sasum/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/sasum/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/sasum/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/sasum/package.json create mode 100644 lib/node_modules/@stdlib/blas/sasum/test/test.js diff --git a/lib/node_modules/@stdlib/blas/sasum/README.md b/lib/node_modules/@stdlib/blas/sasum/README.md new file mode 100644 index 000000000000..daae636fabe1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/README.md @@ -0,0 +1,167 @@ + + +# sasum + +> Compute the sum of [absolute values][@stdlib/math/base/special/abs] ([_L1_ norm][l1norm]). + +
+ +The [_L1_ norm][l1norm] is defined as + + + +```math +\|\mathbf{x}\|_1 = \sum_{i=0}^{n-1} \vert x_i \vert +``` + + + + + +
+ + + +
+ +## Usage + +```javascript +var sasum = require( '@stdlib/blas/sasum' ); +``` + +#### sasum( x\[, dim] ) + +Computes the sum of [absolute values][@stdlib/math/base/special/abs]. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); + +var z = sasum( x ); +// returns + +var v = z.get(); +// returns 15.0 +``` + +The function has the following parameters: + +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] whose underlying data type is `float32`. +- **dim**: dimension for which to compute the sum. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. + +For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function computes the sum for each pair of vectors in `x` according to the specified dimension index. + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); +var array = require( '@stdlib/ndarray/array' ); + +var opts = { + 'shape': [ 2, 3 ] +}; +var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts ); + +var z = sasum( x ); +// returns + +var v1 = z.get( 0 ); +// returns 9.0 + +var v2 = z.get( 1 ); +// returns 9.0 +``` + +
+ + + +
+ +## Notes + +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. +- The output [`ndarray`][@stdlib/ndarray/ctor] has the same data type as the input [`ndarray`][@stdlib/ndarray/ctor] and has a shape which is determined by broadcasting and excludes the contracted dimension. +- If provided an empty vector, the sum is `0`. +- `sasum()` provides a higher-level interface to the [BLAS][blas] level 1 function [`sasum`][@stdlib/blas/base/sasum]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var sasum = require( '@stdlib/blas/sasum' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var z = sasum( x, -1 ); +console.log( ndarray2array( z ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.js new file mode 100644 index 000000000000..44e4224e9fd9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.js @@ -0,0 +1,103 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var pkg = require( './../package.json' ).name; +var sasum = require( './../lib/main.js' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = array( uniform( len, -100.0, 100.0, opts ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = sasum( x ); + if ( isnan( d.get() ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.get() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+'::vectors:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.stack.js b/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.stack.js new file mode 100644 index 000000000000..939ccc3867ec --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/benchmark/benchmark.stack.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var array = require( '@stdlib/ndarray/array' ); +var pkg = require( './../package.json' ).name; +var sasum = require( './../lib/main.js' ); + + +// VARIABLES // + +var OPTS = { + 'dtype': 'float32' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveIntegerArray} shape - array shape +* @returns {Function} benchmark function +*/ +function createBenchmark( shape ) { + var x; + var N; + var o; + + N = numel( shape ); + o = { + 'shape': shape + }; + x = array( uniform( N, -100.0, 100.0, OPTS ), o ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = sasum( x ); + if ( isnan( d.iget( 0 ) ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.iget( 0 ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var shape; + var min; + var max; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = pow( 10, i ); + + shape = [ 2, N/2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + + shape = [ N/2, 2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/sasum/docs/img/equation_l1norm.svg b/lib/node_modules/@stdlib/blas/sasum/docs/img/equation_l1norm.svg new file mode 100644 index 000000000000..4dbb928da546 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/docs/img/equation_l1norm.svg @@ -0,0 +1,44 @@ + +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 + + + \ No newline at end of file diff --git a/lib/node_modules/@stdlib/blas/sasum/docs/repl.txt b/lib/node_modules/@stdlib/blas/sasum/docs/repl.txt new file mode 100644 index 000000000000..244ce215e181 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/docs/repl.txt @@ -0,0 +1,39 @@ + +{{alias}}( x[, dim] ) + Computes the sum of absolute values. + + For multi-dimensional input array, the function performs batched + computation, such that the function computes the sum for each pair + of vectors in `x` according to the specified dimension index. + + If provided an empty vector, the sum is `0`. + + Parameters + ---------- + x: ndarray + First input array. Must have a 'float32' data type. Must have at least + one dimension. + + dim: integer (optional) + Dimension index for which to compute the dot product. Must be a negative + integer. Negative indices are resolved relative to the last array + dimension, with the last dimension corresponding to `-1`. Default: -1. + + Returns + ------- + out: ndarray + The dot product. The output array has the same data type and shape as + the input array. + + Examples + -------- + > var xbuf = new {{alias:@stdlib/array/float32}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ); + > var x = {{alias:@stdlib/ndarray/array}}( xbuf ); + > var z = {{alias}}( x ) + + > z.get() + 15.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/sasum/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/sasum/docs/types/index.d.ts new file mode 100644 index 000000000000..8605a0380f4e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/docs/types/index.d.ts @@ -0,0 +1,60 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float32ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the sum of absolute values. +* +* ## Notes +* +* - If provided at least one input array having more than one dimension, the input arrays are broadcasted to a common shape. +* - For multi-dimensional input arrays, the function performs batched computation, such that the function computes the dot product for each pair of vectors in `x` and `y` according to the specified dimension index. +* - The size of the contracted dimension must be the same for both input arrays. +* - The function resolves the dimension index for which to compute the dot product **before** broadcasting. +* - If provided empty vectors, the dot product is `0`. +* - Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. +* - The output array has the same data type as the input arrays and has a shape which is determined by broadcasting and excludes the contracted dimension. +* +* @param x - input array +* @param dim - dimension for which to compute the sum (default: -1) +* @throws first argument must be a non-zero-dimensional ndarray containing single-precision floating-point numbers +* @returns sum +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* +* var z = sasum( x ); +* returns +* +* var v = z.get(); +* // returns 15.0 +*/ +declare function sasum( x: float32ndarray, dim?: number ): float32ndarray; + + +// EXPORTS // + +export = sasum; diff --git a/lib/node_modules/@stdlib/blas/sasum/docs/types/test.ts b/lib/node_modules/@stdlib/blas/sasum/docs/types/test.ts new file mode 100644 index 000000000000..79868d446475 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/docs/types/test.ts @@ -0,0 +1,102 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import sasum = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + sasum( zeros( [ 10 ] ), zeros( [ 10 ] ) ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + const y = zeros( [ 10 ] ); + + sasum( 10, y ); // $ExpectError + sasum( '10', y ); // $ExpectError + sasum( true, y ); // $ExpectError + sasum( false, y ); // $ExpectError + sasum( null, y ); // $ExpectError + sasum( undefined, y ); // $ExpectError + sasum( {}, y ); // $ExpectError + sasum( [], y ); // $ExpectError + sasum( ( x: number ): number => x, y ); // $ExpectError + + sasum( 10, y, -1 ); // $ExpectError + sasum( '10', y, -1 ); // $ExpectError + sasum( true, y, -1 ); // $ExpectError + sasum( false, y, -1 ); // $ExpectError + sasum( null, y, -1 ); // $ExpectError + sasum( undefined, y, -1 ); // $ExpectError + sasum( {}, y, -1 ); // $ExpectError + sasum( [], y, -1 ); // $ExpectError + sasum( ( x: number ): number => x, y, -1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an ndarray... +{ + const x = zeros( [ 10 ] ); + + sasum( x, 10 ); // $ExpectError + sasum( x, '10' ); // $ExpectError + sasum( x, true ); // $ExpectError + sasum( x, false ); // $ExpectError + sasum( x, null ); // $ExpectError + sasum( x, undefined ); // $ExpectError + sasum( x, {} ); // $ExpectError + sasum( x, [] ); // $ExpectError + sasum( x, ( x: number ): number => x ); // $ExpectError + + sasum( x, 10, -1 ); // $ExpectError + sasum( x, '10', -1 ); // $ExpectError + sasum( x, true, -1 ); // $ExpectError + sasum( x, false, -1 ); // $ExpectError + sasum( x, null, -1 ); // $ExpectError + sasum( x, undefined, -1 ); // $ExpectError + sasum( x, {}, -1 ); // $ExpectError + sasum( x, [], -1 ); // $ExpectError + sasum( x, ( x: number ): number => x, -1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = zeros( [ 10 ] ); + const y = zeros( [ 10 ] ); + + sasum( x, y, '10' ); // $ExpectError + sasum( x, y, true ); // $ExpectError + sasum( x, y, false ); // $ExpectError + sasum( x, y, null ); // $ExpectError + sasum( x, y, {} ); // $ExpectError + sasum( x, y, [] ); // $ExpectError + sasum( x, y, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ] ); + const y = zeros( [ 10 ] ); + + sasum(); // $ExpectError + sasum( x ); // $ExpectError + sasum( x, y, -1, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/sasum/examples/index.js b/lib/node_modules/@stdlib/blas/sasum/examples/index.js new file mode 100644 index 000000000000..8edafc778c63 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var sasum = require( './../lib' ); + +var opts = { + 'dtype': 'float32' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var z = sasum( x, -1 ); +console.log( ndarray2array( z ) ); diff --git a/lib/node_modules/@stdlib/blas/sasum/lib/index.js b/lib/node_modules/@stdlib/blas/sasum/lib/index.js new file mode 100644 index 000000000000..d95875fb6921 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/lib/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* BLAS level 1 routine to compute the sum of absolute values. +* +* @module @stdlib/blas/sasum +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var array = require( '@stdlib/ndarray/array' ); +* var sasum = require( '@stdlib/blas/sasum' ); +* +* var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* +* var z = sasum( x ); +* // returns +* +* var v = z.get(); +* // returns 15.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/sasum/lib/main.js b/lib/node_modules/@stdlib/blas/sasum/lib/main.js new file mode 100644 index 000000000000..e7c3e976a5b8 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/lib/main.js @@ -0,0 +1,133 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFloat32ndarrayLike = require( '@stdlib/assert/is-float32ndarray-like' ); +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var without = require( '@stdlib/array/base/without' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); +var empty = require( '@stdlib/ndarray/empty' ); +var base = require( '@stdlib/blas/base/sasum' ).ndarray; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Computes the sum of absolute values. +* +* @param {ndarrayLike} x - first input array +* @param {NegativeInteger} [dim=-1] - dimension for which to compute the sum +* @throws {TypeError} first argument must be a ndarray containing single-precision floating-point numbers +* @throws {TypeError} first argument must have at least one dimension +* @throws {TypeError} second argument must be a negative integer +* @throws {RangeError} second argument is out-of-bounds +* @returns {ndarray} ndarray containing the sum values +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* +* var z = sasum( x ); +* // returns +* +* var v = z.get(); +* // returns 15.0 +*/ +function sasum( x ) { + var dim; + var xsh; + var osh; + var xit; + var out; + var dm; + var xc; + var vx; + var S; + var v; + var i; + + if ( !isFloat32ndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing single-precision floating-point numbers. Value: `%s`.', x ) ); + } + // Convert the input array to "base" ndarrays: + xc = ndarraylike2ndarray( x ); + + // Resolve the input array shape: + xsh = xc.shape; + + // Validate that we've been provided non-zero-dimensional array... + if ( xsh.length < 1 ) { + throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); + } + // Validate that the dimension argument is a negative integer... + if ( arguments.length > 1 ) { + dim = arguments[ 1 ]; + if ( !isNegativeInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) ); + } + } else { + dim = -1; + } + // Validate that a provided dimension index is within bounds... + dm = xsh.length - 1; + dim = normalizeIndex( dim, dm ); + if ( dim === -1 ) { + throw new RangeError( format( 'invalid argument. Second argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); + } + S = xsh[ dim ]; + + // Resolve the output array shape by excluding the contracted dimension: + osh = without( xc.shape, dim ); + + // Allocate an empty output array: + out = empty( osh, { + 'dtype': xc.dtype, + 'order': xc.order + }); + + // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... + if ( osh.length === 0 ) { + v = base( S, xc.data, xc.strides[0], xc.offset ); + out.iset( v ); + return out; + } + // Create iterators for iterating over stacks of vectors: + xit = nditerStacks( xc, [ dim ] ); + + // Compute the sum for each vector: + for ( i = 0; i < numel( osh ); i++ ) { + vx = xit.next().value; + v = base( S, vx.data, vx.strides[0], vx.offset ); + out.iset( i, v ); + } + return out; +} + + +// EXPORTS // + +module.exports = sasum; diff --git a/lib/node_modules/@stdlib/blas/sasum/package.json b/lib/node_modules/@stdlib/blas/sasum/package.json new file mode 100644 index 000000000000..34b0744e77f5 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/package.json @@ -0,0 +1,73 @@ +{ + "name": "@stdlib/blas/sasum", + "version": "0.0.0", + "description": "Compte the sum of absolute values.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "level 1", + "sasum", + "sum", + "absolute", + "abs", + "linear", + "algebra", + "subroutines", + "vector", + "array", + "ndarray", + "float32", + "single", + "float", + "float32array" + ] +} diff --git a/lib/node_modules/@stdlib/blas/sasum/test/test.js b/lib/node_modules/@stdlib/blas/sasum/test/test.js new file mode 100644 index 000000000000..c8e0df9bc6fc --- /dev/null +++ b/lib/node_modules/@stdlib/blas/sasum/test/test.js @@ -0,0 +1,390 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var shape = require( '@stdlib/ndarray/shape' ); +var sasum = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sasum, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( sasum.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a non-zero-dimensional ndarray containing single-precision floating-point numbers', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + zeros( [], { + 'dtype': 'float32' + }), + array( new Float64Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sasum( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not a non-zero-dimensional ndarray containing single-precision floating-point numbers (dimension)', function test( t ) { + var values; + var i; + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + zeros( [], { + 'dtype': 'float32' + }), + array( new Float64Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + sasum( value, -1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a negative integer (vectors)', function test( t ) { + var values; + var i; + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float32Array( 10 ) ); + + return function badValue() { + sasum( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a negative integer (multi-dimensional arrays)', function test( t ) { + var values; + var i; + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float32Array( 10 ), opts ); + + return function badValue() { + sasum( x, value ); + }; + } +}); + +tape( 'the function calculates the sum of absolute values', function test( t ) { + var sum; + var x; + + x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + + x = array( x ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 28.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (default)', function test( t ) { + var opts; + var sum; + var x; + + x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [ 4 ], 'returns expected value' ); + t.strictEqual( sum.get( 0 ), 6.0, 'returns expected value' ); + t.strictEqual( sum.get( 1 ), 8.0, 'returns expected value' ); + t.strictEqual( sum.get( 2 ), 3.0, 'returns expected value' ); + t.strictEqual( sum.get( 3 ), 11.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-1)', function test( t ) { + var opts; + var sum; + var x; + + x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + sum = sasum( x, -1 ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [ 4 ], 'returns expected value' ); + t.strictEqual( sum.get( 0 ), 6.0, 'returns expected value' ); + t.strictEqual( sum.get( 1 ), 8.0, 'returns expected value' ); + t.strictEqual( sum.get( 2 ), 3.0, 'returns expected value' ); + t.strictEqual( sum.get( 3 ), 11.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-2)', function test( t ) { + var opts; + var sum; + var x; + + x = new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + + opts = { + 'shape': [ 2, 4 ] + }; + x = array( x, opts ); + + sum = sasum( x, -2 ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [ 4 ], 'returns expected value' ); + t.strictEqual( sum.get( 0 ), 5.0, 'returns expected value' ); + t.strictEqual( sum.get( 1 ), 4.0, 'returns expected value' ); + t.strictEqual( sum.get( 2 ), 8.0, 'returns expected value' ); + t.strictEqual( sum.get( 3 ), 11.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the sum is `0`', function test( t ) { + var sum; + var x; + + x = new Float32Array(); + + x = array( x ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a strided vector for `x`', function test( t ) { + var sum; + var x; + + x = new Float32Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + + x = ndarray( 'float32', x, [ 3 ], [ 2 ], 0, 'row-major' ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 13.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a negative stride', function test( t ) { + var sum; + var x; + + x = new Float32Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + + x = ndarray( 'float32', x, [ 3 ], [ -2 ], x.length-1, 'row-major' ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 9.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an `x` offset', function test( t ) { + var sum; + var x; + + x = new Float32Array([ + 0.0, + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + + x = ndarray( 'float32', x, [ 3 ], [ 2 ], 1, 'row-major' ); + + sum = sasum( x ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 13.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports underlying data buffers with view offsets', function test( t ) { + var sum; + var x0; + var x1; + + x0 = new Float32Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + + x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + + x1 = ndarray( 'float32', x1, [ 3 ], [ 2 ], 0, 'row-major' ); + + sum = sasum( x1 ); + + t.strictEqual( sum instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( sum ), ndims( x1 )-1, 'returns expected value' ); + t.deepEqual( shape( sum ), [], 'returns expected value' ); + t.strictEqual( sum.get(), 12.0, 'returns expected value' ); + + t.end(); +});