From 9625abef2117be840bd8759712b1f386d19ae24e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Fri, 9 Oct 2026 01:14:18 +0500 Subject: [PATCH] feat: add blas/ext/base/ndarray/sediff --- .../blas/ext/base/ndarray/sediff/README.md | 132 ++++++++ .../ndarray/sediff/benchmark/benchmark.js | 108 ++++++ .../ext/base/ndarray/sediff/docs/repl.txt | 35 ++ .../base/ndarray/sediff/docs/types/index.d.ts | 56 ++++ .../base/ndarray/sediff/docs/types/test.ts | 75 +++++ .../ext/base/ndarray/sediff/examples/index.js | 43 +++ .../blas/ext/base/ndarray/sediff/lib/index.js | 46 +++ .../blas/ext/base/ndarray/sediff/lib/main.js | 114 +++++++ .../blas/ext/base/ndarray/sediff/package.json | 71 ++++ .../blas/ext/base/ndarray/sediff/test/test.js | 313 ++++++++++++++++++ 10 files changed, 993 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/README.md create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/package.json create mode 100644 lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/test/test.js diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/README.md b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/README.md new file mode 100644 index 000000000000..94ca2b4f6e60 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/README.md @@ -0,0 +1,132 @@ + + +# sediff + +> Calculate the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray. + +
+ +
+ + + +
+ +## Usage + +```javascript +var sediff = require( '@stdlib/blas/ext/base/ndarray/sediff' ); +``` + +#### sediff( arrays ) + +Calculates the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray. + +```javascript +var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); + +var x = new Float32Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); +var prepend = new Float32Vector( [ 1.0 ] ); +var append = new Float32Vector( [ 11.0 ] ); +var out = new Float32Vector( 6 ); + +var y = sediff( [ x, prepend, append, out ] ); +// returns [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional ndarray containing values to prepend prior to computing differences. + - a one-dimensional ndarray containing values to append prior to computing differences. + - a one-dimensional output ndarray. Must have `N + N1 + N2 - 1` elements, where `N` is the number of elements in the input ndarray, `N1` is the number of elements to prepend, and `N2` is the number of elements to append. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/discrete-uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var sediff = require( '@stdlib/blas/ext/base/ndarray/sediff' ); + +var N = 10; +var N1 = 2; +var N2 = 2; +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ N ], -100, 100, opts ); +var p = discreteUniform( [ N1 ], -100, 100, opts ); +var a = discreteUniform( [ N2 ], -100, 100, opts ); +var out = zeros( [ N + N1 + N2 - 1 ], opts ); + +console.log( 'x: ', ndarray2array( x ) ); +console.log( 'prepend: ', ndarray2array( p ) ); +console.log( 'append: ', ndarray2array( a ) ); + +sediff( [ x, p, a, out ] ); +console.log( 'out: ', ndarray2array( out ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/benchmark/benchmark.js new file mode 100644 index 000000000000..fe647011a948 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/benchmark/benchmark.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 uniform = require( '@stdlib/random/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var sediff = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var prepend; + var append; + var opts; + var out; + var x; + + opts = { + 'dtype': 'float32' + }; + x = uniform( [ len ], -100.0, 100.0, opts ); + prepend = uniform( [ 1 ], -100.0, 100.0, opts ); + append = uniform( [ 1 ], -100.0, 100.0, opts ); + out = zeros( [ len + 1 ], opts ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = sediff( [ x, prepend, append, out ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + 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( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/repl.txt new file mode 100644 index 000000000000..d1783280faa2 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( arrays ) + Calculates the differences between consecutive elements of a one-dimensional + single-precision floating-point ndarray. + + The output ndarray must have `N + N1 + N2 - 1` elements, where `N` is the + number of elements in the input ndarray, `N1` is the number of elements to + prepend, and `N2` is the number of elements to append. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a one-dimensional ndarray containing values to prepend. + - a one-dimensional ndarray containing values to append. + - a one-dimensional output ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + > var p = new {{alias:@stdlib/ndarray/vector/float32}}( [ 1.0 ] ); + > var a = new {{alias:@stdlib/ndarray/vector/float32}}( [ 11.0 ] ); + > var out = new {{alias:@stdlib/ndarray/vector/float32}}( 6 ); + > {{alias}}( [ x, p, a, out ] ) + [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/index.d.ts new file mode 100644 index 000000000000..a6d053c0461e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/index.d.ts @@ -0,0 +1,56 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Calculates the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional ndarray containing values to prepend. +* - a one-dimensional ndarray containing values to append. +* - a one-dimensional output ndarray. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* +* var x = new Float32Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); +* var prepend = new Float32Vector( [ 1.0 ] ); +* var append = new Float32Vector( [ 11.0 ] ); +* var out = new Float32Vector( 6 ); +* +* var y = sediff( [ x, prepend, append, out ] ); +* // returns [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] +*/ +declare function sediff( arrays: [ float32ndarray, float32ndarray, float32ndarray, float32ndarray ] ): float32ndarray; + + +// EXPORTS // + +export = sediff; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/test.ts new file mode 100644 index 000000000000..efe1e74fd375 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/docs/types/test.ts @@ -0,0 +1,75 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import sediff = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const p = zeros( [ 1 ], { + 'dtype': 'float32' + }); + const a = zeros( [ 1 ], { + 'dtype': 'float32' + }); + const out = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + sediff( [ x, p, a, out ] ); // $ExpectType float32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + sediff( '10' ); // $ExpectError + sediff( 10 ); // $ExpectError + sediff( true ); // $ExpectError + sediff( false ); // $ExpectError + sediff( null ); // $ExpectError + sediff( undefined ); // $ExpectError + sediff( [] ); // $ExpectError + sediff( {} ); // $ExpectError + sediff( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float32' + }); + const p = zeros( [ 1 ], { + 'dtype': 'float32' + }); + const a = zeros( [ 1 ], { + 'dtype': 'float32' + }); + const out = zeros( [ 10 ], { + 'dtype': 'float32' + }); + + sediff(); // $ExpectError + sediff( [ x, p, a, out ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/examples/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/examples/index.js new file mode 100644 index 000000000000..7d5a62f1e1dd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/examples/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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/discrete-uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var sediff = require( './../lib' ); + +var N = 10; +var N1 = 2; +var N2 = 2; +var opts = { + 'dtype': 'float32' +}; + +var x = discreteUniform( [ N ], -100, 100, opts ); +var p = discreteUniform( [ N1 ], -100, 100, opts ); +var a = discreteUniform( [ N2 ], -100, 100, opts ); +var out = zeros( [ N + N1 + N2 - 1 ], opts ); + +console.log( 'x: ', ndarray2array( x ) ); +console.log( 'prepend: ', ndarray2array( p ) ); +console.log( 'append: ', ndarray2array( a ) ); + +sediff( [ x, p, a, out ] ); +console.log( 'out: ', ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/index.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/index.js new file mode 100644 index 000000000000..b8f304f30c28 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Calculate the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray. +* +* @module @stdlib/blas/ext/base/ndarray/sediff +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* var sediff = require( '@stdlib/blas/ext/base/ndarray/sediff' ); +* +* var x = new Float32Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); +* var prepend = new Float32Vector( [ 1.0 ] ); +* var append = new Float32Vector( [ 11.0 ] ); +* var out = new Float32Vector( 6 ); +* +* var y = sediff( [ x, prepend, append, out ] ); +* // returns [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/main.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/main.js new file mode 100644 index 000000000000..f28350dcfa81 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/lib/main.js @@ -0,0 +1,114 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/ext/base/sediff' ).ndarray; + + +// MAIN // + +/** +* Calculates the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a one-dimensional ndarray containing values to prepend. +* - a one-dimensional ndarray containing values to append. +* - a one-dimensional output ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {ndarray} output ndarray +* +* @example +* var Float32Vector = require( '@stdlib/ndarray/vector/float32' ); +* +* var x = new Float32Vector( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); +* var prepend = new Float32Vector( [ 1.0 ] ); +* var append = new Float32Vector( [ 11.0 ] ); +* var out = new Float32Vector( 6 ); +* +* var y = sediff( [ x, prepend, append, out ] ); +* // returns [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] +*/ +function sediff( arrays ) { + var prepend; + var append; + var out; + var po; + var ps; + var pd; + var oo; + var os; + var od; + var ao; + var as; + var ad; + var N2; + var N1; + var xo; + var xs; + var xd; + var N; + var x; + + x = arrays[ 0 ]; + prepend = arrays[ 1 ]; + append = arrays[ 2 ]; + out = arrays[ 3 ]; + + // Resolve number of indexed elements... + N = numelDimension( x, 0 ); + N1 = numelDimension( prepend, 0 ); + N2 = numelDimension( append, 0 ); + + // Resolve the array data... + xd = getData( x ); + pd = getData( prepend ); + ad = getData( append ); + od = getData( out ); + + // Resolve the array strides... + xs = getStride( x, 0 ); + ps = getStride( prepend, 0 ); + as = getStride( append, 0 ); + os = getStride( out, 0 ); + + // Resolve the array offsets... + xo = getOffset( x ); + po = getOffset( prepend ); + ao = getOffset( append ); + oo = getOffset( out ); + + strided( N, xd, xs, xo, N1, pd, ps, po, N2, ad, as, ao, od, os, oo ); + return out; +} + + +// EXPORTS // + +module.exports = sediff; diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/package.json b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/package.json new file mode 100644 index 000000000000..5abe59549f3c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/blas/ext/base/ndarray/sediff", + "version": "0.0.0", + "description": "Calculate the differences between consecutive elements of a one-dimensional single-precision floating-point ndarray.", + "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", + "extended", + "difference", + "diff", + "sediff", + "gradient", + "strided", + "array", + "ndarray", + "float32", + "float", + "single", + "single-precision" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/test/test.js b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/test/test.js new file mode 100644 index 000000000000..6dd53c02328c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/base/ndarray/sediff/test/test.js @@ -0,0 +1,313 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var Float32Array = require( '@stdlib/array/float32' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var sediff = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float32Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float32', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof sediff, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( sediff.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the differences between consecutive elements of a one-dimensional ndarray', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 6 ); + + x = vector( xbuf, 5, 1, 0 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 6, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports no prepend and no append', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + xbuf = new Float32Array( [ 1.0, 3.0, 6.0, 10.0 ] ); + pbuf = new Float32Array( 0 ); + abuf = new Float32Array( 0 ); + obuf = new Float32Array( 3 ); + + x = vector( xbuf, 4, 1, 0 ); + p = vector( pbuf, 0, 1, 0 ); + a = vector( abuf, 0, 1, 0 ); + out = vector( obuf, 3, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 2.0, 3.0, 4.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // stride=2 accesses x[0]=2, x[2]=6, x[4]=10 (N=3): + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 4 ); + + x = vector( xbuf, 3, 2, 0 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 4, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 4.0, 4.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a negative stride', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // stride=-1, offset=4 accesses x[4]=10, x[3]=8, ..., x[0]=2: + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 6 ); + + x = vector( xbuf, 5, -1, 4 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 6, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, -2.0, -2.0, -2.0, -2.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // offset=2, N=3 accesses x[2]=6, x[3]=8, x[4]=10: + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 4 ); + + x = vector( xbuf, 3, 1, 2 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 4, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 2.0, 2.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-unit stride', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // stride=2 for out: results written at indices 0, 2, 4, 6, 8, 10: + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 12 ); + + x = vector( xbuf, 5, 1, 0 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 6, 2, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 0.0, 2.0, 0.0, 2.0, 0.0, 2.0, 0.0, 2.0, 0.0, 11.0, 0.0 ] ); // eslint-disable-line max-len + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an output ndarray having a non-zero offset', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // offset=2 for out: results written starting at index 2: + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 8 ); + + x = vector( xbuf, 5, 1, 0 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 6, 1, 2 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 2.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports an input ndarray having a zero stride', function test( t ) { + var expected; + var actual; + var obuf; + var pbuf; + var abuf; + var xbuf; + var out; + var x; + var p; + var a; + + // stride=0, offset=2: all 5 elements read xbuf[2]=6.0: + xbuf = new Float32Array( [ 2.0, 4.0, 6.0, 8.0, 10.0 ] ); + pbuf = new Float32Array( [ 1.0 ] ); + abuf = new Float32Array( [ 11.0 ] ); + obuf = new Float32Array( 6 ); + + x = vector( xbuf, 5, 0, 2 ); + p = vector( pbuf, 1, 1, 0 ); + a = vector( abuf, 1, 1, 0 ); + out = vector( obuf, 6, 1, 0 ); + + actual = sediff( [ x, p, a, out ] ); + t.strictEqual( actual, out, 'returns expected value' ); + + expected = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 0.0, 11.0 ] ); + t.strictEqual( isSameFloat32Array( getData( actual ), expected ), true, 'returns expected value' ); + + t.end(); +});