From 90d6b3a444c7c169eb5f5f5c70ac369325e04389 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 17 Jun 2026 21:45:04 +0500 Subject: [PATCH] feat: add differentiate/strided/ggradient --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../differentiate/strided/ggradient/README.md | 194 +++++++ .../benchmark.non_uniform_spacing.js | 109 ++++ .../benchmark/benchmark.uniform_spacing.js | 109 ++++ .../strided/ggradient/docs/repl.txt | 143 +++++ .../strided/ggradient/docs/types/index.d.ts | 131 +++++ .../strided/ggradient/docs/types/test.ts | 386 ++++++++++++++ .../strided/ggradient/examples/index.js | 39 ++ .../strided/ggradient/lib/accessors.js | 152 ++++++ .../strided/ggradient/lib/index.js | 61 +++ .../strided/ggradient/lib/main.js | 61 +++ .../strided/ggradient/lib/ndarray.js | 146 ++++++ .../strided/ggradient/package.json | 68 +++ .../strided/ggradient/test/test.js | 38 ++ .../strided/ggradient/test/test.main.js | 496 ++++++++++++++++++ .../strided/ggradient/test/test.ndarray.js | 496 ++++++++++++++++++ 15 files changed, 2629 insertions(+) create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/README.md create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.non_uniform_spacing.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.uniform_spacing.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/examples/index.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/accessors.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/index.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/main.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/ndarray.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/package.json create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.main.js create mode 100644 lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.ndarray.js diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/README.md b/lib/node_modules/@stdlib/differentiate/strided/ggradient/README.md new file mode 100644 index 000000000000..9b555037b43d --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/README.md @@ -0,0 +1,194 @@ + + +# ggradient + +> Compute the gradient of a strided array. + +
+ +## Usage + +```javascript +var ggradient = require( '@stdlib/differentiate/strided/ggradient' ); +``` + +#### ggradient( N, edgeOrder, x, strideX, h, strideH, out, strideOut ) + +Computes the gradient of a strided array. + +```javascript +var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +var h = [ 1.0, 2.0, 3.0, 4.0 ]; +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient( x.length, 1, x, 1, h, 1, out, 1 ); +// out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **edgeOrder**: approximation order at the boundaries. +- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length for `x`. +- **h**: spacing [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideH**: stride length for `h`. +- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideOut**: stride length for `out`. + +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the gradient of every other element: + +```javascript +var x = [ 0.0, -1.0, 1.0, -1.0, 4.0, -1.0, 9.0, -1.0, 16.0 ]; +var h = [ 1.0 ]; +var out = [ 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0 ]; + +ggradient( 5, 2, x, 2, h, 0, out, 2 ); +// out => [ 0.0, -1.0, 2.0, -1.0, 4.0, -1.0, 6.0, -1.0, 8.0 ] +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Initial arrays: +var xbuf = new Float64Array( [ -1.0, -1.0, 0.0, 1.0, 9.0, 36.0, 100.0 ] ); +var hbuf = new Float64Array( [ 0.0, 1.0, 2.0, 3.0, 4.0 ] ); +var obuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + +// Create offset views: +var x1 = new Float64Array( xbuf.buffer, xbuf.BYTES_PER_ELEMENT*2 ); // start at 3rd element +var h1 = new Float64Array( hbuf.buffer, hbuf.BYTES_PER_ELEMENT*1 ); // start at 2nd element +var out1 = new Float64Array( obuf.buffer, obuf.BYTES_PER_ELEMENT*2 ); + +// Compute the gradient of the last 5 elements: +ggradient( 5, 1, x1, 1, h1, 1, out1, 1 ); +// obuf => [ 0.0, 0.0, 1.0, 2.0, 6.0, 12.0, 16.0 ] +``` + + + +#### ggradient.ndarray( N, edgeOrder, x, strideX, offsetX, h, strideH, offsetH, out, strideOut, offsetOut ) + + + +Computes the gradient of a strided array using alternative indexing semantics. + +```javascript +var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +var h = [ 1.0, 2.0, 3.0, 4.0 ]; +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); +// out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. +- **offsetH**: starting index for `h`. +- **offsetOut**: starting index for `out`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to compute the gradient of the last five elements: + +```javascript +var x = [ -1.0, -1.0, 0.0, 1.0, 9.0, 36.0, 100.0 ]; +var h = [ 1.0, 2.0, 3.0, 4.0 ]; +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient.ndarray( 5, 1, x, 1, 2, h, 1, 0, out, 1, 2 ); +// out => [ 0.0, 0.0, 1.0, 2.0, 6.0, 12.0, 16.0 ] +``` + +
+ + + +
+ +## Notes + +- If `N <= 0` or `N === 1`, both functions return `out` unchanged. +- Both functions support first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). + +
+ + + +
+ +## Examples + + + +```javascript +var ggradient = require( '@stdlib/differentiate/strided/ggradient' ); + +// Approximate the gradient of f(t) = 3t^2 at t = 0, 1, 2, 3, 4 with uniform spacing... +var x = [ 0.0, 3.0, 12.0, 27.0, 48.0 ]; +var h = [ 1.0 ]; +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient( x.length, 2, x, 1, h, 0, out, 1 ); +console.log( out ); +// => [ 0.0, 6.0, 12.0, 18.0, 24.0 ] + +// Approximate the gradient of f(t) = 3t^2 at t = 0, 1, 3, 4, 7 with non-uniform spacing... +x = [ 0.0, 3.0, 27.0, 48.0, 147.0 ]; +h = [ 1.0, 2.0, 1.0, 3.0 ]; +out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient( x.length, 2, x, 1, h, 1, out, 1 ); +console.log( out ); +// => [ 0.0, 6.0, 18.0, 24.0, 42.0 ] +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.non_uniform_spacing.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.non_uniform_spacing.js new file mode 100644 index 000000000000..9eebc3ebf788 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.non_uniform_spacing.js @@ -0,0 +1,109 @@ +/** +* @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/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var ggradient = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var h; + var x; + + x = uniform( len, -100.0, 100.0, options ); + h = uniform( len-1, 0.1, 2.0, options ); + out = uniform( len, -100.0, 100.0, options ); + 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 = ggradient( x.length, 2, x, 1, h, 1, out, 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( isnan( out[ i%len ] ) ) { + 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( format( '%s::non_uniform_spacing:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.uniform_spacing.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.uniform_spacing.js new file mode 100644 index 000000000000..a343d2c175b5 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/benchmark/benchmark.uniform_spacing.js @@ -0,0 +1,109 @@ +/** +* @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/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var ggradient = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Create a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var out; + var h; + var x; + + x = uniform( len, -100.0, 100.0, options ); + h = uniform( 1, 0.1, 2.0, options ); + out = uniform( len, -100.0, 100.0, options ); + 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 = ggradient( x.length, 2, x, 1, h, 0, out, 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( isnan( out[ i%len ] ) ) { + 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( format( '%s::uniform_spacing:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/repl.txt b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/repl.txt new file mode 100644 index 000000000000..0e3fca8c038e --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/repl.txt @@ -0,0 +1,143 @@ + +{{alias}}( N, edgeOrder, x, strideX, h, strideH, out, strideOut ) + Computes the gradient of a strided array. + + The `N` and stride parameters determine which elements in the strided arrays + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use typed + array views. + + If `N <= 0` or `N == 1`, the function returns `out` unchanged. + + The function supports first-order (i.e., `edgeOrder = 1`) and + second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the + boundaries. + + Parameters + ---------- + N: integer + Number of indexed elements. + + edgeOrder: integer + Approximation order at the boundaries. + + x: Array|TypedArray + Input array. + + strideX: integer + Stride length for `x`. + + h: Array|TypedArray + Spacing array. + + strideH: integer + Stride length for `h`. + + out: Array|TypedArray + Output array. + + strideOut: integer + Stride length for `out`. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + // Standard Usage: + > var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; + > var h = [ 1.0, 2.0, 3.0, 4.0 ]; + > var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + > {{alias}}( x.length, 1, x, 1, h, 1, out, 1 ) + [ 1.0, 2.0, 6.0, 12.0, 16.0 ] + + // Using `N` and stride parameters: + > x = [ 0.0, -1.0, 1.0, -1.0, 4.0, -1.0, 9.0 ]; + > h = [ 1.0 ]; + > out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + > {{alias}}( 4, 1, x, 2, h, 0, out, 2 ) + [ 1.0, 0.0, 2.0, 0.0, 4.0, 0.0, 5.0 ] + + // Using view offsets: + > var xbuf = [ -1.0, -1.0, 0.0, 1.0, 9.0, 36.0, 100.0 ]; + > var hbuf = [ 0.0, 1.0, 2.0, 3.0, 4.0 ]; + > var obuf = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + > var x0 = new {{alias:@stdlib/array/float64}}( xbuf ); + > var h0 = new {{alias:@stdlib/array/float64}}( hbuf ); + > var out0 = new {{alias:@stdlib/array/float64}}( obuf ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*2 ); + > var h1 = new {{alias:@stdlib/array/float64}}( h0.buffer, h0.BYTES_PER_ELEMENT*1 ); + > var out1 = new {{alias:@stdlib/array/float64}}( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); + > {{alias}}( 5, 1, x1, 1, h1, 1, out1, 1 ) + [ 1.0, 2.0, 6.0, 12.0, 16.0 ] + + +{{alias}}.ndarray( N, edgeOrder, x, sx, ox, h, sh, oh, o, so, oo ) + Computes the gradient of a strided array using alternative indexing + semantics. + + While typed array views mandate a view offset based on the underlying + buffer, the offset parameters support indexing semantics based on starting + indices. + + Parameters + ---------- + N: integer + Number of indexed elements. + + edgeOrder: integer + Approximation order at the boundaries. + + x: Array|TypedArray + Input array. + + sx: integer + Stride length for `x`. + + ox: integer + Starting index for `x`. + + h: Array|TypedArray + Spacing array. + + sh: integer + Stride length for `h`. + + oh: integer + Starting index for `h`. + + o: Array|TypedArray + Output array. + + so: integer + Stride length for `out`. + + oo: integer + Starting index for `out`. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + // Standard Usage: + > var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; + > var h = [ 1.0, 2.0, 3.0, 4.0 ]; + > var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + > {{alias}}.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ) + [ 1.0, 2.0, 6.0, 12.0, 16.0 ] + + // Using index offsets: + > x = [ -1.0, -1.0, 0.0, 1.0, 9.0, 36.0, 100.0 ]; + > h = [ 1.0, 2.0, 3.0, 4.0 ]; + > out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + > {{alias}}.ndarray( 5, 1, x, 1, 2, h, 1, 0, out, 1, 2 ) + [ 0.0, 0.0, 1.0, 2.0, 6.0, 12.0, 16.0 ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/index.d.ts b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/index.d.ts new file mode 100644 index 000000000000..bfffbe3e76de --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/index.d.ts @@ -0,0 +1,131 @@ +/* +* @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 { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Output array. +*/ +type OutputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `ggradient`. +*/ +interface Routine { + /** + * Computes the gradient of a strided array. + * + * ## Notes + * + * - The function supports first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. + * + * @param N - number of indexed elements + * @param edgeOrder - approximation order at the boundaries + * @param x - input array + * @param strideX - stride length for `x` + * @param h - spacing array + * @param strideH - stride length for `h` + * @param out - output array + * @param strideOut - stride length for `out` + * @returns output array + * + * @example + * var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; + * var h = [ 1.0, 2.0, 3.0, 4.0 ]; + * var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + * + * ggradient( x.length, 1, x, 1, h, 1, out, 1 ); + * // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] + */ + ( N: number, edgeOrder: number, x: InputArray, strideX: number, h: InputArray, strideH: number, out: T, strideOut: number ): T; + + /** + * Computes the gradient of a strided array using alternative indexing semantics. + * + * ## Notes + * + * - The function supports first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. + * + * @param N - number of indexed elements + * @param edgeOrder - approximation order at the boundaries + * @param x - input array + * @param strideX - stride length for `x` + * @param offsetX - starting index for `x` + * @param h - spacing array + * @param strideH - stride length for `h` + * @param offsetH - starting index for `h` + * @param out - output array + * @param strideOut - stride length for `out` + * @param offsetOut - starting index for `out` + * @returns output array + * + * @example + * var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; + * var h = [ 1.0, 2.0, 3.0, 4.0 ]; + * var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + * + * ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); + * // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] + */ + ndarray( N: number, edgeOrder: number, x: InputArray, strideX: number, offsetX: number, h: InputArray, strideH: number, offsetH: number, out: T, strideOut: number, offsetOut: number ): T; +} + +/** +* Computes the gradient of a strided array. +* +* @param N - number of indexed elements +* @param edgeOrder - approximation order at the boundaries +* @param x - input array +* @param strideX - stride length for `x` +* @param h - spacing array +* @param strideH - stride length for `h` +* @param out - output array +* @param strideOut - stride length for `out` +* @returns output array +* +* @example +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient( x.length, 1, x, 1, h, 1, out, 1 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +* +* @example +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +*/ +declare var ggradient: Routine; + + +// EXPORTS // + +export = ggradient; diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/test.ts b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/test.ts new file mode 100644 index 000000000000..4cbfa9c510fd --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/docs/types/test.ts @@ -0,0 +1,386 @@ +/* +* @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 AccessorArray = require( '@stdlib/array/base/accessor' ); +import ggradient = require( './index' ); + + +// TESTS // + +// The function returns a numeric array... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( x.length, 1, x, 1, h, 1, out, 1 ); // $ExpectType Float64Array + ggradient( x.length, 1, new AccessorArray( x ), 1, new AccessorArray( h ), 1, new AccessorArray( out ), 1 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( '10', 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( true, 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( false, 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( null, 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( undefined, 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( [], 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( {}, 1, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( ( x: number ): number => x, 1, x, 1, h, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( x.length, '10', x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, true, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, false, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, null, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, undefined, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, [], x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, {}, x, 1, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, ( x: number ): number => x, x, 1, h, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a numeric array... +{ + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( 10, 1, 10, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, '10', 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, true, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, false, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, null, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, undefined, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, [ '1' ], 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, {}, 1, h, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, ( x: number ): number => x, 1, h, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( x.length, 1, x, '10', h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, true, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, false, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, null, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, undefined, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, [], h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, {}, h, 1, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, ( x: number ): number => x, h, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + const out = new Float64Array( 10 ); + + ggradient( 10, 1, x, 1, 10, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, '10', 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, true, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, false, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, null, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, undefined, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, [ '1' ], 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, {}, 1, out, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, ( x: number ): number => x, 1, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( x.length, 1, x, 1, h, '10', out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, true, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, false, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, null, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, undefined, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, [], out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, {}, out, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, ( x: number ): number => x, out, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + + ggradient( 10, 1, x, 1, h, 1, 10, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, '10', 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, true, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, false, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, null, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, undefined, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, [ '1' ], 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, {}, 1 ); // $ExpectError + ggradient( 10, 1, x, 1, h, 1, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an eighth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient( x.length, 1, x, 1, h, 1, out, '10' ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, true ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, false ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, null ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, undefined ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, [] ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, {} ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient(); // $ExpectError + ggradient( x.length ); // $ExpectError + ggradient( x.length, 1 ); // $ExpectError + ggradient( x.length, 1, x ); // $ExpectError + ggradient( x.length, 1, x, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1 ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out ); // $ExpectError + ggradient( x.length, 1, x, 1, h, 1, out, 1, {} ); // $ExpectError +} + +// Attached to the main export is an `ndarray` method which returns a numeric array... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectType Float64Array + ggradient.ndarray( x.length, 1, new AccessorArray( x ), 1, 0, new AccessorArray( h ), 1, 0, new AccessorArray( out ), 1, 0 ); // $ExpectType AccessorArray +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( '10', 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( true, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( false, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( null, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( undefined, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( [], 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( {}, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( ( x: number ): number => x, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, '10', x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, true, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, false, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, null, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, undefined, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, [], x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, {}, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, ( x: number ): number => x, x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a numeric array... +{ + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( 10, 1, 10, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, '10', 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, true, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, false, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, null, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, undefined, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, [ '1' ], 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, {}, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, ( x: number ): number => x, 1, 0, h, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, '10', 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, true, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, false, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, null, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, undefined, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, [], 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, {}, 0, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, ( x: number ): number => x, 0, h, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, '10', h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, true, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, false, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, null, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, undefined, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, [], h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, {}, h, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, ( x: number ): number => x, h, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( 10, 1, x, 1, 0, 10, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, '10', 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, true, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, false, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, null, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, undefined, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, [ '1' ], 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, {}, 1, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, ( x: number ): number => x, 1, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, 0, h, '10', 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, true, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, false, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, null, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, undefined, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, [], 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, {}, 0, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, ( x: number ): number => x, 0, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eighth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, '10', out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, true, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, false, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, null, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, undefined, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, [], out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, {}, out, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, ( x: number ): number => x, out, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a ninth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, 10, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, '10', 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, true, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, false, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, null, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, undefined, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, [ '1' ], 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, {}, 1, 0 ); // $ExpectError + ggradient.ndarray( 10, 1, x, 1, 0, h, 1, 0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a tenth argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, '10', 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, true, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, false, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, null, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, undefined, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, [], 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, {}, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eleventh argument which is not a number... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, '10' ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, true ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, false ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, null ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, undefined ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, [] ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, {} ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + const h = new Float64Array( 9 ); + const out = new Float64Array( 10 ); + + ggradient.ndarray(); // $ExpectError + ggradient.ndarray( x.length ); // $ExpectError + ggradient.ndarray( x.length, 1 ); // $ExpectError + ggradient.ndarray( x.length, 1, x ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1 ); // $ExpectError + ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/examples/index.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/examples/index.js new file mode 100644 index 000000000000..7ecc3f039e91 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/examples/index.js @@ -0,0 +1,39 @@ +/** +* @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 ggradient = require( './../lib' ); + +// Approximate the gradient of f(t) = 3t^2 at t = 0, 1, 2, 3, 4 with uniform spacing... +var x = [ 0.0, 3.0, 12.0, 27.0, 48.0 ]; +var h = [ 1.0 ]; +var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient( x.length, 2, x, 1, h, 0, out, 1 ); +console.log( out ); +// => [ 0.0, 6.0, 12.0, 18.0, 24.0 ] + +// Approximate the gradient of f(t) = 3t^2 at t = 0, 1, 3, 4, 7 with non-uniform spacing... +x = [ 0.0, 3.0, 27.0, 48.0, 147.0 ]; +h = [ 1.0, 2.0, 1.0, 3.0 ]; +out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + +ggradient( x.length, 2, x, 1, h, 1, out, 1 ); +console.log( out ); +// => [ 0.0, 6.0, 18.0, 24.0, 42.0 ] diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/accessors.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/accessors.js new file mode 100644 index 000000000000..a46f098a1d48 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/accessors.js @@ -0,0 +1,152 @@ +/** +* @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 max-params, max-len */ + +'use strict'; + +// MAIN // + +/** +* Computes the gradient of a strided array using alternative indexing semantics and accessor arrays. +* +* ## Notes +* +* - The function supports first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} edgeOrder - approximation order at the boundaries +* @param {Object} x - input array object +* @param {Collection} x.data - input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {Object} h - spacing array object +* @param {Collection} h.data - spacing array data +* @param {Array} h.accessors - array element accessors +* @param {integer} strideH - stride length for `h` +* @param {NonNegativeInteger} offsetH - starting index for `h` +* @param {Object} out - output array object +* @param {Collection} out.data - output array data +* @param {Array} out.accessors - array element accessors +* @param {integer} strideOut - stride length for `out` +* @param {NonNegativeInteger} offsetOut - starting index for `out` +* @returns {Object} output array object +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient( x.length, 1, arraylike2object( toAccessorArray( x ) ), 1, 0, arraylike2object( toAccessorArray( h ) ), 1, 0, arraylike2object( toAccessorArray( out ) ), 1, 0 ); +* +* console.log( out ); +* // => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +*/ +function ggradient( N, edgeOrder, x, strideX, offsetX, h, strideH, offsetH, out, strideOut, offsetOut ) { + var xbuf; + var hbuf; + var obuf; + var xget; + var hget; + var oset; + var prev; + var curr; + var hL; + var hR; + var d1; + var d2; + var ix; + var ih; + var io; + var a; + var b; + var c; + var s; + var i; + + // Cache references to array data and accessors: + xbuf = x.data; + hbuf = h.data; + obuf = out.data; + xget = x.accessors[ 0 ]; + hget = h.accessors[ 0 ]; + oset = out.accessors[ 1 ]; + + // Left boundary... + d1 = hget( hbuf, offsetH ); + if ( edgeOrder >= 2 && N >= 3 ) { + d2 = hget( hbuf, offsetH + strideH ); + s = d1 + d2; + a = -(d1 + s) / ( d1 * s ); + b = s / ( d1 * d2 ); + c = -d1 / ( d2 * s ); + oset( obuf, offsetOut, ( a*xget( xbuf, offsetX ) ) + ( b*xget( xbuf, offsetX + strideX ) ) + ( c*xget( xbuf, offsetX + (2*strideX) ) ) ); + } else { + oset( obuf, offsetOut, ( xget( xbuf, offsetX + strideX ) - xget( xbuf, offsetX ) ) / d1 ); + } + + // N=2: right boundary equals left boundary formula (first-order only)... + if ( N === 2 ) { + oset( obuf, offsetOut + strideOut, ( xget( xbuf, offsetX + strideX ) - xget( xbuf, offsetX ) ) / d1 ); + return out; + } + + // Interior points... + ix = offsetX + strideX; + ih = offsetH; + io = offsetOut + strideOut; + prev = xget( xbuf, offsetX ); + for ( i = 1; i < N-1; i++ ) { + curr = xget( xbuf, ix ); + hL = hget( hbuf, ih ); + hR = hget( hbuf, ih + strideH ); + s = hL + hR; + a = -hR / ( hL * s ); + b = (hR - hL) / ( hL * hR ); + c = hL / ( hR * s ); + oset( obuf, io, ( a*prev ) + ( b*curr ) + ( c*xget( xbuf, ix + strideX ) ) ); + prev = curr; + ix += strideX; + ih += strideH; + io += strideOut; + } + + // Right boundary... + d2 = hget( hbuf, ih ); + if ( edgeOrder >= 2 ) { + d1 = hget( hbuf, ih - strideH ); + s = d1 + d2; + a = d2 / ( d1 * s ); + b = -s / ( d1 * d2 ); + c = (d2 + s) / ( d2 * s ); + oset( obuf, io, ( a*xget( xbuf, ix - (2*strideX) ) ) + ( b*prev ) + ( c*xget( xbuf, ix ) ) ); + } else { + oset( obuf, io, ( xget( xbuf, ix ) - prev ) / d2 ); + } + return out; +} + + +// EXPORTS // + +module.exports = ggradient; diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/index.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/index.js new file mode 100644 index 000000000000..f03a5702b862 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/index.js @@ -0,0 +1,61 @@ +/** +* @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'; + +/** +* Compute the gradient of a strided array. +* +* @module @stdlib/differentiate/strided/ggradient +* +* @example +* var ggradient = require( '@stdlib/differentiate/strided/ggradient' ); +* +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient( x.length, 1, x, 1, h, 1, out, 1 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +* +* @example +* var ggradient = require( '@stdlib/differentiate/strided/ggradient' ); +* +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient.ndarray( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/main.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/main.js new file mode 100644 index 000000000000..7601cad24275 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/main.js @@ -0,0 +1,61 @@ +/** +* @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 stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Computes the gradient of a strided array. +* +* ## Notes +* +* - The function supports first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} edgeOrder - approximation order at the boundaries +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length for `x` +* @param {NumericArray} h - spacing array +* @param {integer} strideH - stride length for `h` +* @param {NumericArray} out - output array +* @param {integer} strideOut - stride length for `out` +* @returns {NumericArray} output array +* +* @example +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient( x.length, 1, x, 1, h, 1, out, 1 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +*/ +function ggradient( N, edgeOrder, x, strideX, h, strideH, out, strideOut ) { + return ndarray( N, edgeOrder, x, strideX, stride2offset( N, strideX ), h, strideH, stride2offset( N-1, strideH ), out, strideOut, stride2offset( N, strideOut ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = ggradient; diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/ndarray.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/ndarray.js new file mode 100644 index 000000000000..4cbf5d43f142 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/lib/ndarray.js @@ -0,0 +1,146 @@ +/** +* @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 max-params, max-len */ + +'use strict'; + +// MODULES // + +var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Computes the gradient of a strided array using alternative indexing semantics. +* +* ## Notes +* +* - The function supports first-order (i.e., `edgeOrder = 1`) and second-order (i.e., `edgeOrder = 2`) finite-difference approximations at the boundaries. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {PositiveInteger} edgeOrder - approximation order at the boundaries +* @param {NumericArray} x - input array +* @param {integer} strideX - stride length for `x` +* @param {NonNegativeInteger} offsetX - starting index for `x` +* @param {NumericArray} h - spacing array +* @param {integer} strideH - stride length for `h` +* @param {NonNegativeInteger} offsetH - starting index for `h` +* @param {NumericArray} out - output array +* @param {integer} strideOut - stride length for `out` +* @param {NonNegativeInteger} offsetOut - starting index for `out` +* @returns {NumericArray} output array +* +* @example +* var x = [ 0.0, 1.0, 9.0, 36.0, 100.0 ]; +* var h = [ 1.0, 2.0, 3.0, 4.0 ]; +* var out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; +* +* ggradient( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); +* // out => [ 1.0, 2.0, 6.0, 12.0, 16.0 ] +*/ +function ggradient( N, edgeOrder, x, strideX, offsetX, h, strideH, offsetH, out, strideOut, offsetOut ) { + var prev; + var curr; + var xo; + var ho; + var oo; + var hL; + var hR; + var d1; + var d2; + var ix; + var ih; + var io; + var a; + var b; + var c; + var s; + var i; + + if ( N <= 0 || N === 1 ) { + return out; + } + xo = arraylike2object( x ); + ho = arraylike2object( h ); + oo = arraylike2object( out ); + if ( xo.accessorProtocol || ho.accessorProtocol || oo.accessorProtocol ) { + accessors( N, edgeOrder, xo, strideX, offsetX, ho, strideH, offsetH, oo, strideOut, offsetOut ); + return out; + } + + // Left boundary... + d1 = h[ offsetH ]; + if ( edgeOrder >= 2 && N >= 3 ) { + d2 = h[ offsetH + strideH ]; + s = d1 + d2; + a = -(d1 + s) / ( d1 * s ); + b = s / ( d1 * d2 ); + c = -d1 / ( d2 * s ); + out[ offsetOut ] = ( a*x[ offsetX ] ) + ( b*x[ offsetX + strideX ] ) + ( c*x[ offsetX + (2*strideX) ] ); + } else { + out[ offsetOut ] = ( x[ offsetX + strideX ] - x[ offsetX ] ) / d1; + } + + // N=2: right boundary equals left boundary formula (first-order only)... + if ( N === 2 ) { + out[ offsetOut + strideOut ] = ( x[ offsetX + strideX ] - x[ offsetX ] ) / d1; + return out; + } + + // Interior points... + ix = offsetX + strideX; + ih = offsetH; + io = offsetOut + strideOut; + prev = x[ offsetX ]; + for ( i = 1; i < N-1; i++ ) { + curr = x[ ix ]; + hL = h[ ih ]; + hR = h[ ih + strideH ]; + s = hL + hR; + a = -hR / ( hL * s ); + b = (hR - hL) / ( hL * hR ); + c = hL / ( hR * s ); + out[ io ] = ( a*prev ) + ( b*curr ) + ( c*x[ ix + strideX ] ); + prev = curr; + ix += strideX; + ih += strideH; + io += strideOut; + } + + // Right boundary... + d2 = h[ ih ]; + if ( edgeOrder >= 2 ) { + d1 = h[ ih - strideH ]; + s = d1 + d2; + a = d2 / ( d1 * s ); + b = -s / ( d1 * d2 ); + c = (d2 + s) / ( d2 * s ); + out[ io ] = ( a*x[ ix - (2*strideX) ] ) + ( b*prev ) + ( c*x[ ix ] ); + } else { + out[ io ] = ( x[ ix ] - prev ) / d2; + } + return out; +} + + +// EXPORTS // + +module.exports = ggradient; diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/package.json b/lib/node_modules/@stdlib/differentiate/strided/ggradient/package.json new file mode 100644 index 000000000000..39de9037c088 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/differentiate/strided/ggradient", + "version": "0.0.0", + "description": "Compute the gradient of a strided array.", + "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", + "differentiate", + "gradient", + "finite", + "difference", + "numerical", + "strided", + "array", + "vector", + "generic", + "ggradient" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.js new file mode 100644 index 000000000000..afe2f5b75e01 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.js @@ -0,0 +1,38 @@ +/** +* @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 ggradient = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ggradient, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof ggradient.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.main.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.main.js new file mode 100644 index 000000000000..6a97b8d0c5ff --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.main.js @@ -0,0 +1,496 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var ggradient = require( './../lib/main.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ggradient, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 8', function test( t ) { + t.strictEqual( ggradient.length, 8, 'has expected arity' ); + t.end(); +}); + +tape( 'the function returns a reference to the output array', function test( t ) { + var out; + var x; + var y; + var h; + + x = [ 1.0, 2.0, 3.0 ]; + h = [ 1.0 ]; + y = [ 0.0, 0.0, 0.0 ]; + + out = ggradient( x.length, 1, x, 1, h, 1, y, 1 ); + + t.strictEqual( out, y, 'same reference' ); + t.end(); +}); + +tape( 'the function returns a reference to the output array (accessors)', function test( t ) { + var out; + var x; + var y; + var h; + + x = toAccessorArray( [ 1.0, 2.0, 3.0 ] ); + h = toAccessorArray( [ 1.0 ] ); + y = toAccessorArray( [ 0.0, 0.0, 0.0 ] ); + + out = ggradient( x.length, 1, x, 1, h, 1, y, 1 ); + + t.strictEqual( out, y, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 1.0, 2.0, 3.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 0.0, 0.0, 0.0 ]; + + ggradient( 0, 1, x, 1, h, 0, out, 1 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the output array unchanged', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 99.0 ]; + + ggradient( 1, 1, x, 1, h, 0, out, 1 ); + + expected = [ 99.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the output array unchanged (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 99.0 ]; + + ggradient( 1, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 99.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2`, the function computes the gradient using the first-order boundary approximation', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 1, x, 1, h, 0, out, 1 ); + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2`, the function computes the gradient using the first-order boundary approximation (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2` and `edgeOrder` equal to `2`, the function computes the gradient using the first-order boundary approximation', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 2, x, 1, h, 0, out, 1 ); + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2` and `edgeOrder` equal to `2`, the function computes the gradient using the first-order boundary approximation (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 2, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=1)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, h, 0, out, 1 ); + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=1, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=2)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, x, 1, h, 0, out, 1 ); + + expected = [ 0.0, 2.0, 4.0, 6.0, 8.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=2, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 0.0, 2.0, 4.0, 6.0, 8.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=1)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, h, 1, out, 1 ); + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=1, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 1, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=2)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, x, 1, h, 1, out, 1 ); + + expected = [ 0.0, 24.0, 72.0, 96.0, 168.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=2, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, toAccessorArray( x ), 1, toAccessorArray( h ), 1, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 0.0, 24.0, 72.0, 96.0, 168.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `x` stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 16.0, 9.0, 4.0, 1.0, 0.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, -1, h, 0, out, 1 ); + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative `x` stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 16.0, 9.0, 4.0, 1.0, 0.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), -1, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative output stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, h, 0, out, -1 ); + + expected = [ 7.0, 6.0, 4.0, 2.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a negative output stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 0, toAccessorArray( out ), -1 ); // eslint-disable-line max-len + + expected = [ 7.0, 6.0, 4.0, 2.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports non-unit strides', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, -1.0, 1.0, -1.0, 4.0, -1.0, 9.0, -1.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0 ]; + + ggradient( 5, 1, x, 2, h, 0, out, 2 ); + + expected = [ 1.0, -1.0, 2.0, -1.0, 4.0, -1.0, 6.0, -1.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports non-unit strides (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, -1.0, 1.0, -1.0, 4.0, -1.0, 9.0, -1.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0, -1.0, 0.0 ]; + + ggradient( 5, 1, toAccessorArray( x ), 2, toAccessorArray( h ), 0, toAccessorArray( out ), 2 ); // eslint-disable-line max-len + + expected = [ 1.0, -1.0, 2.0, -1.0, 4.0, -1.0, 6.0, -1.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a zero `x` stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, x, 0, h, 0, out, 1 ); + + expected = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a zero `x` stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, toAccessorArray( x ), 0, toAccessorArray( h ), 0, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a non-unit `h` stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, -1.0, 2.0, -1.0, 1.0, -1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, h, 2, out, 1 ); + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a non-unit `h` stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, -1.0, 2.0, -1.0, 1.0, -1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, toAccessorArray( h ), 2, toAccessorArray( out ), 1 ); // eslint-disable-line max-len + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.ndarray.js b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.ndarray.js new file mode 100644 index 000000000000..e48f455ab758 --- /dev/null +++ b/lib/node_modules/@stdlib/differentiate/strided/ggradient/test/test.ndarray.js @@ -0,0 +1,496 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var ggradient = require( './../lib/ndarray.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof ggradient, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 11', function test( t ) { + t.strictEqual( ggradient.length, 11, 'has expected arity' ); + t.end(); +}); + +tape( 'the function returns a reference to the output array', function test( t ) { + var out; + var x; + var y; + var h; + + x = [ 1.0, 2.0, 3.0 ]; + h = [ 1.0 ]; + y = [ 0.0, 0.0, 0.0 ]; + + out = ggradient( x.length, 1, x, 1, 0, h, 0, 0, y, 1, 0 ); + + t.strictEqual( out, y, 'same reference' ); + t.end(); +}); + +tape( 'the function returns a reference to the output array (accessors)', function test( t ) { + var out; + var x; + var y; + var h; + + x = toAccessorArray( [ 1.0, 2.0, 3.0 ] ); + h = toAccessorArray( [ 1.0 ] ); + y = toAccessorArray( [ 0.0, 0.0, 0.0 ] ); + + out = ggradient( x.length, 1, x, 1, 0, h, 0, 0, y, 1, 0 ); + + t.strictEqual( out, y, 'same reference' ); + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns the output array unchanged', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 1.0, 2.0, 3.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0 ]; + expected = [ 0.0, 0.0, 0.0 ]; + + ggradient( 0, 1, x, 1, 0, h, 0, 0, out, 1, 0 ); + + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the output array unchanged', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 99.0 ]; + + ggradient( 1, 1, x, 1, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 99.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `1`, the function returns the output array unchanged (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 99.0 ]; + + ggradient( 1, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 99.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2`, the function computes the gradient using the first-order boundary approximation', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 1, x, 1, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2`, the function computes the gradient using the first-order boundary approximation (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2` and `edgeOrder` equal to `2`, the function computes the gradient using the first-order boundary approximation', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 2, x, 1, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an `N` parameter equal to `2` and `edgeOrder` equal to `2`, the function computes the gradient using the first-order boundary approximation (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 2.0, 5.0 ]; + h = [ 3.0 ]; + out = [ 0.0, 0.0 ]; + + ggradient( 2, 2, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 1.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=1)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=1, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=2)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, x, 1, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 0.0, 2.0, 4.0, 6.0, 8.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (uniform spacing, edgeOrder=2, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 0.0, 2.0, 4.0, 6.0, 8.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=1)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, 0, h, 1, 0, out, 1, 0 ); + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=1, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 1, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=2)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, x, 1, 0, h, 1, 0, out, 1, 0 ); + + expected = [ 0.0, 24.0, 72.0, 96.0, 168.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function computes the gradient (non-uniform spacing, edgeOrder=2, accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 2, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 1, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 0.0, 24.0, 72.0, 96.0, 168.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports index offsets', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ -1.0, -1.0, 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, x, 1, 2, h, 0, 0, out, 1, 2 ); + + expected = [ -1.0, -1.0, 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports index offsets (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ -1.0, -1.0, 0.0, 1.0, 4.0, 9.0, 16.0 ]; + h = [ 1.0 ]; + out = [ -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, toAccessorArray( x ), 1, 2, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 2 ); // eslint-disable-line max-len + + expected = [ -1.0, -1.0, 1.0, 2.0, 4.0, 6.0, 7.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 16.0, 9.0, 4.0, 1.0, 0.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, x, -1, 4, h, 0, 0, out, -1, 4 ); + + expected = [ 7.0, 6.0, 4.0, 2.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative strides (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 16.0, 9.0, 4.0, 1.0, 0.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, toAccessorArray( x ), -1, 4, toAccessorArray( h ), 0, 0, toAccessorArray( out ), -1, 4 ); // eslint-disable-line max-len + + expected = [ 7.0, 6.0, 4.0, 2.0, 1.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a zero `x` stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, x, 0, 0, h, 0, 0, out, 1, 0 ); + + expected = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a zero `x` stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 5.0 ]; + h = [ 1.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( 5, 1, toAccessorArray( x ), 0, 0, toAccessorArray( h ), 0, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a non-unit `h` stride', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, -1.0, 2.0, -1.0, 1.0, -1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, 0, h, 2, 0, out, 1, 0 ); + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports a non-unit `h` stride (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ 1.0, -1.0, 2.0, -1.0, 1.0, -1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 2, 0, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `h` offset', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ -1.0, 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, x, 1, 0, h, 1, 1, out, 1, 0 ); + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports an `h` offset (accessors)', function test( t ) { + var expected; + var out; + var x; + var h; + + x = [ 0.0, 12.0, 108.0, 192.0, 588.0 ]; + h = [ -1.0, 1.0, 2.0, 1.0, 3.0 ]; + out = [ 0.0, 0.0, 0.0, 0.0, 0.0 ]; + + ggradient( x.length, 1, toAccessorArray( x ), 1, 0, toAccessorArray( h ), 1, 1, toAccessorArray( out ), 1, 0 ); // eslint-disable-line max-len + + expected = [ 12.0, 24.0, 72.0, 96.0, 132.0 ]; + t.deepEqual( out, expected, 'returns expected value' ); + t.end(); +});