diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/README.md b/lib/node_modules/@stdlib/blas/tools/dot-factory/README.md new file mode 100644 index 000000000000..c36615867f5b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/README.md @@ -0,0 +1,181 @@ + + +# factory + +> Return a function which computes the dot product. + +
+ +
+ + + +
+ +## Usage + +```javascript +var factory = require( '@stdlib/blas/tools/dot-factory' ); +``` + +#### factory( base, dtype ) + +Returns a function which computes the dot product. + +```javascript +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; + +var dot = factory( ddot, 'float64' ); +``` + +The function has the following parameters: + +- **base**: "base" function which computes the dot product. Must have an `ndarray` function signature (i.e., must support index offsets). +- **dtype**: array data type. The function assumes that the data type of all provided arrays is the same. + +#### dot( x, y\[, dim] ) + +Computes the dot product of two floating-point vectors. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; + +var dot = factory( ddot, 'float64' ); + +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); + +var z = dot( x, y ); +// returns + +var v = z.get(); +// returns -5.0 +``` + +The returned function has the following parameters: + +- **x**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with `y`. +- **y**: a non-zero-dimensional [`ndarray`][@stdlib/ndarray/ctor]. Must be [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with `x`. +- **dim**: dimension for which to compute the dot product. Must be a negative integer. Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. Default: `-1`. + +If provided at least one input [`ndarray`][@stdlib/ndarray/ctor] having more than one dimension, the input [`ndarrays`][@stdlib/ndarray/ctor] are [broadcasted][@stdlib/ndarray/base/broadcast-shapes] to a common shape. For multi-dimensional input [`ndarrays`][@stdlib/ndarray/ctor], the function performs batched computation, such that the function computes the dot product for each pair of vectors in `x` and `y` according to the specified dimension index. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; + +var dot = factory( ddot, 'float64' ); + +var opts = { + 'shape': [ 2, 3 ] +}; +var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 3.0 ] ), opts ); +var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 2.0 ] ), opts ); + +var z = dot( x, y ); +// returns + +var v1 = z.get( 0 ); +// returns 23.0 + +var v2 = z.get( 1 ); +// returns -22.0 +``` + +
+ + + +
+ +## Notes + +For the returned function, + +- The size of the contracted dimension must be the same for both input [`ndarrays`][@stdlib/ndarray/ctor]. +- The function resolves the dimension index for which to compute the dot product **before** broadcasting. +- Negative indices are resolved relative to the last [`ndarray`][@stdlib/ndarray/ctor] dimension, with the last dimension corresponding to `-1`. +- The output [`ndarray`][@stdlib/ndarray/ctor] has the same data type as the input [`ndarrays`][@stdlib/ndarray/ctor] and has a shape which is determined by broadcasting and excludes the contracted dimension. +- If provided empty vectors, the dot product is `0`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +var factory = require( '@stdlib/blas/tools/dot-factory' ); + +var dot = factory( ddot, 'float64' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var y = array( discreteUniform( 10, 0, 10, opts ), { + 'shape': x.shape +}); +console.log( ndarray2array( y ) ); + +var z = dot( x, y, -1 ); +console.log( ndarray2array( z ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.js new file mode 100644 index 000000000000..bb186df4bc73 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float64' +}; +var dot = factory( ddot, opts.dtype ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = array( uniform( len, -100.0, 100.0, opts ) ); + var y = array( uniform( len, -100.0, 100.0, opts ) ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dot( x, y ); + if ( isnan( d.get() ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.get() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( pkg+'::vectors:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.stack.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.stack.js new file mode 100644 index 000000000000..ac73ccf122d4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/benchmark/benchmark.stack.js @@ -0,0 +1,124 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'dtype': 'float64' +}; +var dot = factory( ddot, opts.dtype ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveIntegerArray} shape - array shape +* @returns {Function} benchmark function +*/ +function createBenchmark( shape ) { + var x; + var y; + var N; + var o; + + N = numel( shape ); + o = { + 'shape': shape + }; + x = array( uniform( N, -100.0, 100.0, opts ), o ); + y = array( uniform( N, -100.0, 100.0, opts ), o ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var d; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + d = dot( x, y ); + if ( isnan( d.iget( 0 ) ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( d.iget( 0 ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var shape; + var min; + var max; + var N; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + N = pow( 10, i ); + + shape = [ 2, N/2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + + shape = [ N/2, 2 ]; + f = createBenchmark( shape ); + bench( pkg+'::stacks:size='+N+',ndims='+shape.length+',shape=('+shape.join( ',' )+')', f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/repl.txt b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/repl.txt new file mode 100644 index 000000000000..306d5e94f9bf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/repl.txt @@ -0,0 +1,76 @@ + +{{alias}}( base, dtype ) + Returns a function which computes the dot product. + + Parameters + ---------- + base: Function + Base function which interchanges two vectors. Must have an ndarray + signature (i.e., must support index offsets). + + dtype: any + Array data type. The function assumes that the data type of all provided + arrays is the same. + + Returns + ------- + fcn: Function + Function which interchanges two vectors. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/blas/base/ddot}}, 'float64' ) + + + +fcn( x, y[, dim] ) + Computes the dot product of two floating-point vectors. + + If provided at least one input array having more than one dimension, the + input arrays are broadcasted to a common shape. + + For multi-dimensional input arrays, the function performs batched + computation, such that the function computes the dot product for each pair + of vectors in `x` and `y` according to the specified dimension index. + + The size of the contracted dimension must be the same for both input arrays. + + The function resolves the dimension index for which to compute the dot + product *before* broadcasting. + + If provided empty vectors, the dot product is `0`. + + Parameters + ---------- + x: ndarray + First input array. Must have a 'float32' data type. Must have at least + one dimension and be broadcast-compatible with the second input array. + + y: ndarray + Second input array. Must have a 'float32' data type. Must have at least + one dimension and be broadcast-compatible with the first input array. + + dim: integer (optional) + Dimension index for which to compute the dot product. Must be a negative + integer. Negative indices are resolved relative to the last array + dimension, with the last dimension corresponding to `-1`. Default: -1. + + Returns + ------- + out: ndarray + The dot product. The output array has the same data type as the input + arrays and has a shape which is determined by broadcasting and excludes + the contracted dimension. + + Examples + -------- + > var fcn = {{alias}}( {{alias:@stdlib/blas/base/ddot}}.ndarray, 'float64' ); + > var x = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float64}}( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); + > var y = {{alias:@stdlib/ndarray/array}}( new {{alias:@stdlib/array/float64}}( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); + > var z = fcn( x, y ); + > z.get() + -5.0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/index.d.ts new file mode 100644 index 000000000000..0933a455c4df --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/index.d.ts @@ -0,0 +1,91 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* "Base" function which computes the dot product. +* +* @param N - number of indexed elements +* @param x - first input array +* @param strideX - `x` stride length +* @param offsetX - starting index for `x` +* @param y - second input array +* @param strideY - `y` stride length +* @param offsetY - starting index for `y` +* @returns dot product +*/ +type BaseFunction = ( N: number, x: T, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ) => T; + +/** +* Computes the dot product of two floating-point vectors. +* +* ## Notes +* +* - If provided at least one input array having more than one dimension, the input arrays are broadcasted to a common shape. +* - For multi-dimensional input arrays, the function performs batched computation, such that the function computes the dot product for each pair of vectors in `x` and `y` according to the specified dimension index. +* - The size of the contracted dimension must be the same for both input arrays. +* - The function resolves the dimension index for which to compute the dot product **before** broadcasting. +* - If provided empty vectors, the dot product is `0`. +* - Negative indices are resolved relative to the last array dimension, with the last dimension corresponding to `-1`. +* - The output array has the same data type as the input arrays and has a shape which is determined by broadcasting and excludes the contracted dimension. +* +* @param x - first input array +* @param y - second input array +* @param dim - dimension for which to compute the dot product (default: -1) +* @throws first argument must be a non-zero-dimensional ndarray containing floating-point numbers +* @throws second argument must be a non-zero-dimensional ndarray containing floating-point numbers +* @throws input arrays must be broadcast-compatible +* @throws the size of the contracted dimension must be the same for both input arrays +* @returns dot product +*/ +type DotFunction = ( x: ndarray, y: ndarray, dim?: number ) => ndarray; + +/** +* Returns a function which computes the dot product. +* +* @param base - "base" function which computes the dot product +* @param dtype - array data type +* @returns function wrapper +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +* +* var dot = factory( ddot, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* var z = dot( x, y ); +* +* var v = z.get(); +* // returns -5.0 +*/ +declare function factory>( base: BaseFunction, dtype: any ): DotFunction; + + +// EXPORTS // + +export = factory; diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/test.ts b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/test.ts new file mode 100644 index 000000000000..d60b7add8762 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/docs/types/test.ts @@ -0,0 +1,133 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable @typescript-eslint/unbound-method */ + +import ddot = require( '@stdlib/blas/base/ddot' ); +import zeros = require( '@stdlib/ndarray/zeros' ); +import factory = require( './index' ); + + +// TESTS // + +// The function returns a function... +{ + factory( ddot.ndarray, 'float64' ); // $ExpectType DotFunction +} + +// The compiler throws an error if the function is provided a first argument which is not a valid function... +{ + factory( 10, 'float64' ); // $ExpectError + factory( '10', 'float64' ); // $ExpectError + factory( true, 'float64' ); // $ExpectError + factory( false, 'float64' ); // $ExpectError + factory( null, 'float64' ); // $ExpectError + factory( undefined, 'float64' ); // $ExpectError + factory( {}, 'float64' ); // $ExpectError + factory( [], 'float64' ); // $ExpectError + factory( ( x: number ): number => x, 'float64' ); // $ExpectError +} + +// The function returns a function which returns a number... +{ + const dot = factory( ddot.ndarray, 'float64' ); + + dot( zeros( [ 10 ], { 'dtype': 'float64' } ), zeros( [ 10 ], { 'dtype': 'float64' } ) ); // $ExpectType ndarray +} + +// The compiler throws an error if the returned function is provided a first argument which is not an ndarray... +{ + const dot = factory( ddot.ndarray, 'float64' ); + + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dot( 10, y ); // $ExpectError + dot( '10', y ); // $ExpectError + dot( true, y ); // $ExpectError + dot( false, y ); // $ExpectError + dot( null, y ); // $ExpectError + dot( undefined, y ); // $ExpectError + dot( {}, y ); // $ExpectError + dot( [], y ); // $ExpectError + dot( ( x: number ): number => x, y ); // $ExpectError + + dot( 10, y, -1 ); // $ExpectError + dot( '10', y, -1 ); // $ExpectError + dot( true, y, -1 ); // $ExpectError + dot( false, y, -1 ); // $ExpectError + dot( null, y, -1 ); // $ExpectError + dot( undefined, y, -1 ); // $ExpectError + dot( {}, y, -1 ); // $ExpectError + dot( [], y, -1 ); // $ExpectError + dot( ( x: number ): number => x, y, -1 ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided a second argument which is not an ndarray... +{ + const dot = factory( ddot.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dot( x, 10 ); // $ExpectError + dot( x, '10' ); // $ExpectError + dot( x, true ); // $ExpectError + dot( x, false ); // $ExpectError + dot( x, null ); // $ExpectError + dot( x, undefined ); // $ExpectError + dot( x, {} ); // $ExpectError + dot( x, [] ); // $ExpectError + dot( x, ( x: number ): number => x ); // $ExpectError + + dot( x, 10, -1 ); // $ExpectError + dot( x, '10', -1 ); // $ExpectError + dot( x, true, -1 ); // $ExpectError + dot( x, false, -1 ); // $ExpectError + dot( x, null, -1 ); // $ExpectError + dot( x, undefined, -1 ); // $ExpectError + dot( x, {}, -1 ); // $ExpectError + dot( x, [], -1 ); // $ExpectError + dot( x, ( x: number ): number => x, -1 ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided a third argument which is not a number... +{ + const dot = factory( ddot.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dot( x, y, '10' ); // $ExpectError + dot( x, y, true ); // $ExpectError + dot( x, y, false ); // $ExpectError + dot( x, y, null ); // $ExpectError + dot( x, y, {} ); // $ExpectError + dot( x, y, [] ); // $ExpectError + dot( x, y, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the returned function is provided an unsupported number of arguments... +{ + const dot = factory( ddot.ndarray, 'float64' ); + + const x = zeros( [ 10 ], { 'dtype': 'float64' } ); + const y = zeros( [ 10 ], { 'dtype': 'float64' } ); + + dot(); // $ExpectError + dot( x ); // $ExpectError + dot( x, y, -1, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/examples/index.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/examples/index.js new file mode 100644 index 000000000000..fbdc073231ed --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +var factory = require( './../lib' ); + +var dot = factory( ddot, 'float64' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = array( discreteUniform( 10, 0, 100, opts ), { + 'shape': [ 5, 2 ] +}); +console.log( ndarray2array( x ) ); + +var y = array( discreteUniform( 10, 0, 10, opts ), { + 'shape': x.shape +}); +console.log( ndarray2array( y ) ); + +var z = dot( x, y, -1 ); +console.log( ndarray2array( z ) ); diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/index.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/index.js new file mode 100644 index 000000000000..6ee91df9da82 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/index.js @@ -0,0 +1,51 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a function which calculates the dot product of two floating-point vectors. +* +* @module @stdlib/blas/tools/dot-factory +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +* var factory = require( '@stdlib/blas/tools/dot-factory' ); +* +* var dot = factory( ddot, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* var z = dot( x, y ); +* // returns +* +* var v = z.get(); +* // returns -5.0 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/main.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/main.js new file mode 100644 index 000000000000..90a7238c5280 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/lib/main.js @@ -0,0 +1,206 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLikeWithDataType = require( '@stdlib/assert/is-ndarray-like-with-data-type' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive; +var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); +var min = require( '@stdlib/math/base/special/fast/min' ); +var without = require( '@stdlib/array/base/without' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var maybeBroadcastArrays = require( '@stdlib/ndarray/base/maybe-broadcast-arrays' ); +var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var nditerStacks = require( '@stdlib/ndarray/iter/stacks' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a function which computes the dot product of two floating-point vectors. +* +* @param {Function} base - "base" function which computes the dot product +* @param {(String|null)} dtype - array data type +* @throws {TypeError} first argument must be a function +* @throws {TypeError} second argument must be a data type +* @returns {Function} function wrapper +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +* +* var dot = factory( ddot, 'float64' ); +* +* var x = array( new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); +* var y = array( new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); +* +* var z = dot( x, y ); +* // returns +* +* var v = z.get(); +* // returns -5.0 +*/ +function factory( base, dtype ) { + var isValid; + if ( !isFunction( base ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', base ) ); + } + if ( !isDataType( dtype ) && dtype !== null ) { + throw new TypeError( format( 'invalid argument. Second argument must be a data type. Value: `%s`.', dtype ) ); + } + isValid = ( dtype ) ? isValidWrapper : isndarrayLike; + return dot; + + /** + * Tests if an input value is an ndarray-like object having a specified data type. + * + * @private + * @param {*} value - value to test + * @returns {boolean} boolean indicating if an input value is an ndarray-like object having a specified data type + */ + function isValidWrapper( value ) { + return isndarrayLikeWithDataType( value, dtype ); + } + + /** + * Computes the dot product. + * + * @private + * @param {ndarrayLike} x - first input array + * @param {ndarrayLike} y - second input array + * @param {NegativeInteger} [dim=-1] - dimension for which to compute the dot product + * @throws {TypeError} first argument must be an ndarray + * @throws {TypeError} first argument must have at least one dimension + * @throws {TypeError} second argument must be an ndarray + * @throws {TypeError} second argument must have at least one dimension + * @throws {TypeError} third argument must be a negative integer + * @throws {Error} input arrays must be broadcast compatible + * @throws {RangeError} the size of the contracted dimension must be the same for both input arrays + * @throws {RangeError} third argument is out-of-bounds + * @returns {ndarray} ndarray containing the dot product + */ + function dot( x, y ) { + var dim; + var xsh; + var ysh; + var osh; + var xit; + var yit; + var out; + var tmp; + var xc; + var yc; + var vx; + var vy; + var dm; + var S; + var v; + var i; + + if ( !isValid( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object having a supported data type. Value: `%s`.', x ) ); + } + if ( !isValid( y ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object having a supported data type. Value: `%s`.', y ) ); + } + // Convert the input arrays to "base" ndarrays: + xc = ndarraylike2ndarray( x ); + yc = ndarraylike2ndarray( y ); + + // Resolve the input array shapes: + xsh = xc.shape; + ysh = yc.shape; + + // Validate that we've been provided non-zero-dimensional arrays... + if ( xsh.length < 1 ) { + throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) ); + } + if ( ysh.length < 1 ) { + throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) ); + } + // Validate that the dimension argument is a negative integer... + if ( arguments.length > 2 ) { + dim = arguments[ 2 ]; + if ( !isNegativeInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) ); + } + } else { + dim = -1; + } + // Validate that a provided dimension index is within bounds **before** broadcasting... + dm = min( xsh.length, ysh.length ) - 1; + dim = normalizeIndex( dim, dm ); + if ( dim === -1 ) { + throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) ); + } + // Validate that the contracted dimension size is the same for both input arrays... + S = xsh[ dim ]; + if ( ysh[ dim ] !== S ) { + throw new RangeError( format( 'invalid argument. The size of the contracted dimension must be the same for both input ndarrays. Dim(%s,%d) = %d. Dim(%s,%d) = %d.', 'x', dim, S, 'y', dim, ysh[ dim ] ) ); + } + // Broadcast the input arrays to a common shape.... + try { + tmp = maybeBroadcastArrays( [ xc, yc ] ); + } catch ( err ) { // eslint-disable-line no-unused-vars + throw new Error( format( 'invalid arguments. Input ndarrays must be broadcast compatible. Shape(%s) = (%s). Shape(%s) = (%s).', 'x', xsh.join( ',' ), 'y', ysh.join( ',' ) ) ); + } + xc = tmp[ 0 ]; + yc = tmp[ 1 ]; + + // Resolve the output array shape by excluding the contracted dimension: + osh = without( xc.shape, dim ); + + // Allocate an empty output array: + out = empty( osh, { + 'dtype': xc.dtype, + 'order': xc.order + }); + + // If we are only provided one-dimensional input arrays, we can skip iterating over stacks... + if ( osh.length === 0 ) { + v = base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len + out.iset( v ); + return out; + } + // Create iterators for iterating over stacks of vectors: + xit = nditerStacks( xc, [ dim ] ); + yit = nditerStacks( yc, [ dim ] ); + + // Compute the dot product for each pair of vectors... + for ( i = 0; i < numel( osh ); i++ ) { + vx = xit.next().value; + vy = yit.next().value; + v = base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len + out.iset( i, v ); + } + return out; + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/package.json b/lib/node_modules/@stdlib/blas/tools/dot-factory/package.json new file mode 100644 index 000000000000..1dd6854d0ba7 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/package.json @@ -0,0 +1,78 @@ +{ + "name": "@stdlib/blas/tools/dot-factory", + "version": "0.0.0", + "description": "Return a function which calculates the dot product of two floating-point vectors.", + "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", + "browser": "./lib/main.js", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "level 1", + "linear", + "algebra", + "subroutines", + "dot", + "product", + "dot product", + "scalar", + "scalar product", + "inner", + "inner product", + "vector", + "array", + "ndarray", + "factory", + "tool", + "tools", + "utilities", + "utils" + ] +} diff --git a/lib/node_modules/@stdlib/blas/tools/dot-factory/test/test.js b/lib/node_modules/@stdlib/blas/tools/dot-factory/test/test.js new file mode 100644 index 000000000000..ef07acc43139 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/tools/dot-factory/test/test.js @@ -0,0 +1,819 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Int16Array = require( '@stdlib/array/int16' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var shape = require( '@stdlib/ndarray/shape' ); +var ddot = require( '@stdlib/blas/base/ddot' ).ndarray; +var factory = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a function', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( value, 'float64' ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid second argument', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( ddot, value ); + }; + } +}); + +tape( 'the function returns a function', function test( t ) { + t.strictEqual( typeof factory( ddot, 'float64' ), 'function', 'returns expected value' ); + t.end(); +}); + +tape( 'the returned function has an arity of 2', function test( t ) { + var dot = factory( ddot, 'float64' ); + t.strictEqual( dot.length, 2, 'has expected arity' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a non-zero-dimensional ndarray having a supported data type', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + array( new Int16Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( value, y ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not a non-zero-dimensional ndarray having a supported data type (dimension)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + array( new Int16Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( value, y, -1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a non-zero-dimensional ndarray having a supported data type', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + array( new Int16Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a non-zero-dimensional ndarray having a supported data type (dimension)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + 5, + '5', + true, + false, + null, + void 0, + {}, + [], + function noop() {}, + array( new Int16Array( 10 ) ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( x, value, -1 ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a negative integer (vectors)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not a negative integer (multi-dimensional arrays)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + '5', + 0, + 5, + NaN, + -3.14, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + var y = array( new Float64Array( 10 ), opts ); + + return function badValue() { + dot( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is out-of-bounds (vectors)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + -2, + -3, + -4, + -5 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 10 ) ); + + return function badValue() { + dot( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is out-of-bounds (multi-dimensional arrays)', function test( t ) { + var values; + var dot; + var i; + + dot = factory( ddot, 'float64' ); + + values = [ + -3, + -4, + -5, + -10 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + var opts = { + 'shape': [ 2, 5 ] + }; + var x = array( new Float64Array( 10 ), opts ); + var y = array( new Float64Array( 10 ), opts ); + + return function badValue() { + dot( x, y, value ); + }; + } +}); + +tape( 'the function throws an error if provided arrays having an unequal contracted dimension (vectors)', function test( t ) { + var dot = factory( ddot, 'float64' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + var x = array( new Float64Array( 10 ) ); + var y = array( new Float64Array( 100 ) ); + dot( x, y ); + } +}); + +tape( 'the function throws an error if provided arrays having an unequal contracted dimension (multi-dimensional)', function test( t ) { + var dot = factory( ddot, 'float64' ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + var x = array( new Float64Array( 100 ), { + 'shape': [ 25, 4 ] + }); + var y = array( new Float64Array( 100 ), { + 'shape': [ 50, 2 ] + }); + dot( x, y, -1 ); + } +}); + +tape( 'the function throws an error if provided broadcast-incompatible input arrays', function test( t ) { + var dot = factory( ddot, 'float64' ); + + t.throws( badValue, Error, 'throws an error' ); + t.end(); + + function badValue() { + var x = array( new Float64Array( 100 ), { + 'shape': [ 5, 10, 2 ] + }); + var y = array( new Float64Array( 100 ), { + 'shape': [ 50, 1, 2 ] + }); + dot( x, y, -1 ); + } +}); + +tape( 'the function calculates the dot product of vectors `x` and `y`', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + x = array( x ); + y = array( y ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), -17.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (default)', function test( t ) { + var opts; + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [ 4 ], 'returns expected value' ); + t.strictEqual( d.get( 0 ), 20.0, 'returns expected value' ); + t.strictEqual( d.get( 1 ), -17.0, 'returns expected value' ); + t.strictEqual( d.get( 2 ), 8.0, 'returns expected value' ); + t.strictEqual( d.get( 3 ), -28.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-1)', function test( t ) { + var opts; + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + d = dot( x, y, -1 ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [ 4 ], 'returns expected value' ); + t.strictEqual( d.get( 0 ), 20.0, 'returns expected value' ); + t.strictEqual( d.get( 1 ), -17.0, 'returns expected value' ); + t.strictEqual( d.get( 2 ), 8.0, 'returns expected value' ); + t.strictEqual( d.get( 3 ), -28.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports operating on stacks of vectors (dim=-2)', function test( t ) { + var opts; + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0, -1.0, -4.0, 8.0, 8.0, 2.0, -3.0 ] ); + + opts = { + 'shape': [ 2, 4 ] + }; + x = array( x, opts ); + y = array( y, opts ); + + d = dot( x, y, -2 ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [ 4 ], 'returns expected value' ); + t.strictEqual( d.get( 0 ), 0.0, 'returns expected value' ); + t.strictEqual( d.get( 1 ), 28.0, 'returns expected value' ); + t.strictEqual( d.get( 2 ), -7.0, 'returns expected value' ); + t.strictEqual( d.get( 3 ), -38.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports broadcasting', function test( t ) { + var opts; + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array( [ 4.0, 2.0, -3.0, 5.0, -1.0, 2.0, -5.0, 6.0 ] ); + y = new Float64Array( [ 2.0, 6.0 ] ); + + opts = { + 'shape': [ 4, 2 ] + }; + x = array( x, opts ); + + opts = { + 'shape': [ 1, 2 ] + }; + y = array( y, opts ); + + d = dot( x, y, -1 ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [ 4 ], 'returns expected value' ); + t.strictEqual( d.get( 0 ), 20.0, 'returns expected value' ); + t.strictEqual( d.get( 1 ), 24.0, 'returns expected value' ); + t.strictEqual( d.get( 2 ), 10.0, 'returns expected value' ); + t.strictEqual( d.get( 3 ), 26.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided empty vectors, the dot product is `0`', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array(); + y = new Float64Array(); + + x = array( x ); + y = array( y ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), 0.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a strided vector for the first argument', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array([ + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, // 1 + -3.0 // 2 + ]); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 1 ], 0, 'row-major' ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), -12.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a strided vector for the second argument', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array([ + 2.0, // 0 + -3.0, // 1 + -5.0 // 2 + ]); + y = new Float64Array([ + 8.0, // 0 + 2.0, + -3.0, // 1 + 3.0, + -4.0, // 2 + 1.0 + ]); + + x = ndarray( 'float64', x, [ 3 ], [ 1 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 2 ], 0, 'row-major' ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), 45.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array([ + 1.0, // 2 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 0 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0 // 0 + ]); + + x = ndarray( 'float64', x, [ 3 ], [ -2 ], x.length-1, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ -1 ], y.length-1, 'row-major' ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), 67.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 3.0, // 1 + 4.0, + 5.0 // 2 + ]); + y = new Float64Array([ + 6.0, // 2 + 7.0, // 1 + 8.0 // 0 + ]); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 0, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ -1 ], y.length-1, 'row-major' ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), 59.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports strided vectors having offsets', function test( t ) { + var dot; + var d; + var x; + var y; + + dot = factory( ddot, 'float64' ); + + x = new Float64Array([ + 0.0, + 2.0, // 0 + -3.0, + -5.0, // 1 + 7.0, + 6.0 // 2 + ]); + y = new Float64Array([ + 0.0, + 0.0, + 8.0, // 0 + 2.0, // 1 + -3.0 // 2 + ]); + + x = ndarray( 'float64', x, [ 3 ], [ 2 ], 1, 'row-major' ); + y = ndarray( 'float64', y, [ 3 ], [ 1 ], 2, 'row-major' ); + + d = dot( x, y ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), -12.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports underlying data buffers with view offsets', function test( t ) { + var dot; + var x0; + var y0; + var x1; + var y1; + var d; + + dot = factory( ddot, 'float64' ); + + x0 = new Float64Array([ + 1.0, + 2.0, // 0 + 3.0, + 4.0, // 1 + 5.0, + 6.0 // 2 + ]); + y0 = new Float64Array([ + 6.0, + 7.0, + 8.0, + 9.0, // 0 + 10.0, // 1 + 11.0 // 2 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); + + x1 = ndarray( 'float64', x1, [ 3 ], [ 2 ], 0, 'row-major' ); + y1 = ndarray( 'float64', y1, [ 3 ], [ 1 ], 0, 'row-major' ); + + d = dot( x1, y1 ); + + t.strictEqual( d instanceof ndarray, true, 'returns expected value' ); + t.strictEqual( ndims( d ), ndims( x1 )-1, 'returns expected value' ); + t.deepEqual( shape( d ), [], 'returns expected value' ); + t.strictEqual( d.get(), 124.0, 'returns expected value' ); + + t.end(); +});