diff --git a/lib/node_modules/@stdlib/ndarray/every/README.md b/lib/node_modules/@stdlib/ndarray/every/README.md new file mode 100644 index 000000000000..9ee71a5e3f31 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/README.md @@ -0,0 +1,179 @@ + + +# every + +> Return a boolean indicating whether every element in the [ndarray][@stdlib/ndarray/ctor] passes a test implemented by a predicate function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var every = require( '@stdlib/ndarray/every' ); +``` + +#### every( x, predicate\[, thisArg] ) + +Return a boolean indicating whether every element in the [ndarray][@stdlib/ndarray/ctor] passes a test implemented by a predicate function. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( z ) { + return z > 6.0; +} + +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ] + +var y = every( x, predicate ); +// returns false +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **predicate**: predicate function. +- **thisArg**: predicate function execution context _(optional)_. + +To set the `predicate` function execution context, provide a `thisArg`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function predicate( z ) { + this.count += 1; + return z > 6.0; +} + +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 2.0, 3.0, 4.0 ], [ 8.0, 9.0, 10.0 ] ] + +var ctx = { + 'count': 0 +}; +var y = every( x, predicate, ctx ); +// returns false + +var count = ctx.count; +// returns 1 +``` + +The `predicate` function is provided the following arguments: + +- **value**: current array element. +- **indices**: current array element indices. +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- The function **always** returns a boolean. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var array = require( '@stdlib/ndarray/array' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var every = require( '@stdlib/ndarray/every' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = every( x, naryFunction( isPositive, 1 ) ); +console.log( y ); +``` + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.1d.js b/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.1d.js new file mode 100644 index 000000000000..8c321e41ce28 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.1d.js @@ -0,0 +1,142 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var every = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {boolean} result +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order ) { + var strides; + var xbuf; + var x; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = every( x, predicate ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isBoolean( y ) ) { + b.fail( 'should return an boolean' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.2d.js b/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.2d.js new file mode 100644 index 000000000000..fe5657d89244 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/benchmark/benchmark.2d.js @@ -0,0 +1,153 @@ +/** +* @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 isBoolean = require( '@stdlib/assert/is-boolean' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var pkg = require( './../package.json' ).name; +var every = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Predicate function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {boolean} result +*/ +function predicate( value ) { + return value > 0.0; +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, order ) { + var strides; + var xbuf; + var x; + var y; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = every( x, predicate ); + if ( isnan( y ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isBoolean( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len/2, 2 ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + + sh = [ 2, len/2 ]; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + + len = floor( sqrt( len ) ); + sh = [ len, len ]; + len *= len; + f = createBenchmark( len, sh, t1, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1, f ); + } + } + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/every/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/every/docs/repl.txt new file mode 100644 index 000000000000..e0bf61f85b49 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( x, predicate[, thisArg] ) + Return a boolean indicating whether every element in the ndarray passes a + test implemented by a predicate function. + + The predicate function is provided the following arguments: + + - value: current array element. + - indices: current array element indices. + - arr: the input ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + predicate: Function + Predicate function. + + thisArg: any (optional) + Predicate function execution context. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > function f( v ) { return v > 0.0; }; + > var y = {{alias}}( x, f ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/every/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/every/docs/types/index.d.ts new file mode 100644 index 000000000000..1304eb594e4d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/docs/types/index.d.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. +*/ + +// TypeScript Version: 4.1 + +/// + +/* eslint-disable max-lines */ + +import { typedndarray, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @returns boolean indicating whether an element passes a test +*/ +type Nullary = ( this: V ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @returns boolean indicating whether an element passes a test +*/ +type Unary = ( this: V, value: T ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @returns boolean indicating whether an element passes a test +*/ +type Binary = ( this: V, value: T, indices: Array ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Ternary = ( this: V, value: T, indices: Array, arr: typedndarray ) => boolean; + +/** +* Returns a boolean indicating whether an element passes a test. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns boolean indicating whether an element passes a test +*/ +type Predicate = Nullary | Unary | Binary | Ternary; + +/** +* Return a boolean indicating whether every element in the ndarray passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output ndarray +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 2 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* +* var y = every( x, isEven ); +* // returns true +*/ +declare function every( x: float64ndarray | float32ndarray | complex64ndarray | complex128ndarray | int32ndarray | int16ndarray | int8ndarray | uint32ndarray | uint16ndarray | uint8ndarray | uint8cndarray | boolndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + +/** +* Return a boolean indicating whether every element in the ndarray passes a test implemented by a predicate function. +* +* @param x - input ndarray +* @param predicate - predicate function +* @param thisArg - predicate function execution context +* @returns output ndarray +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var shape = [ 2, 3 ]; +* var strides = [ 6, 2 ]; +* var offset = 1; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* +* var y = every( x, isEven ); +* // returns true +*/ +declare function every( x: genericndarray, predicate: Predicate, thisArg?: ThisParameterType> ): boolean; + + +// EXPORTS // + +export = every; diff --git a/lib/node_modules/@stdlib/ndarray/every/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/every/docs/types/test.ts new file mode 100644 index 000000000000..834088354b8b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/docs/types/test.ts @@ -0,0 +1,98 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/// + +import empty = require( '@stdlib/ndarray/base/empty' ); +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import every = require( './index' ); + +/** +* Predicate function. +* +* @param x - input value +* @returns result +*/ +function predicate( x: any ): boolean { + return Boolean( x ); +} + +// The function returns an boolean... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + every( zeros( 'float64', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'float64', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'float32', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'float32', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'complex64', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'complex64', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'complex128', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'complex128', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'int32', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'int32', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'int16', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'int16', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'int8', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'int8', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'uint32', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'uint32', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'uint16', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'uint16', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'uint8', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'uint8', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'uint8c', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'uint8c', sh, ord ), predicate, {} ); // $ExpectType boolean + every( empty( 'bool', sh, ord ), predicate ); // $ExpectType boolean + every( empty( 'bool', sh, ord ), predicate, {} ); // $ExpectType boolean + every( zeros( 'generic', sh, ord ), predicate ); // $ExpectType boolean + every( zeros( 'generic', sh, ord ), predicate, {} ); // $ExpectType boolean +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + every( 5, predicate ); // $ExpectError + every( true, predicate ); // $ExpectError + every( false, predicate ); // $ExpectError + every( null, predicate ); // $ExpectError + every( undefined, predicate ); // $ExpectError + every( {}, predicate ); // $ExpectError + every( [ 1 ], predicate ); // $ExpectError + every( ( x: number ): number => x, predicate ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a function... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + every( x, '5' ); // $ExpectError + every( x, true ); // $ExpectError + every( x, false ); // $ExpectError + every( x, null ); // $ExpectError + every( x, undefined ); // $ExpectError + every( x, {} ); // $ExpectError + every( x, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + every(); // $ExpectError + every( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + every( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/every/examples/index.js b/lib/node_modules/@stdlib/ndarray/every/examples/index.js new file mode 100644 index 000000000000..32d30cdef1a5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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 naryFunction = require( '@stdlib/utils/nary-function' ); +var array = require( '@stdlib/ndarray/array' ); +var isPositive = require( '@stdlib/assert/is-positive-number' ).isPrimitive; +var every = require( './../lib' ); + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = every( x, naryFunction( isPositive, 1 ) ); +console.log( y ); diff --git a/lib/node_modules/@stdlib/ndarray/every/lib/index.js b/lib/node_modules/@stdlib/ndarray/every/lib/index.js new file mode 100644 index 000000000000..29a7f192ba75 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/lib/index.js @@ -0,0 +1,55 @@ +/** +* @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 boolean indicating whether every element in the ndarray passes a test implemented by a predicate function. +* +* @module @stdlib/ndarray/every +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var every = require( '@stdlib/ndarray/every' ); +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 2 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* +* var y = every( x, isEven ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/every/lib/main.js b/lib/node_modules/@stdlib/ndarray/every/lib/main.js new file mode 100644 index 000000000000..cf71c4cfcef6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/lib/main.js @@ -0,0 +1,129 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var nextCartesianIndex = require( '@stdlib/ndarray/base/next-cartesian-index' ).assign; +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Return a boolean indicating whether every element in the ndarray passes a test implemented by a predicate function. +* +* @param {ndarray} x - input ndarray +* @param {Callback} predicate - predicate function +* @param {*} [thisArg] - predicate execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} callback argument must be a function +* @returns {boolean} result +* +* @example +* var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var every = require( '@stdlib/ndarray/every' ); +* +* var buffer = new Float64Array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]); +* var shape = [2, 3]; +* var strides = [6, 2]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 2.0, 4.0, 6.0 ], [ 8.0, 10.0, 12.0 ] ] +* +* var y = every( x, isEven ); +* // returns true +*/ +function every( x, predicate, thisArg ) { + var ndims; + var clbk; + var ctx; + var ord; + var dim; + var idx; + var sh; + var N; + var v; + var i; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( arguments.length < 3 ) { + clbk = predicate; + } + if ( arguments.length === 3 ) { + if ( isFunction( predicate ) ) { + clbk = predicate; + ctx = thisArg; + } + } + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) ); + } + // Resolve the iteration order: + ord = getOrder( x ); + + // Resolve the input array shape: + sh = getShape( x ); + + // Compute the number of array elements: + N = numel( sh ); + + // Retrieve the number of dimensions: + ndims = sh.length; + + // Resolve the dimension in which indices should iterate fastest: + if ( ord === 'row-major' ) { + dim = ndims - 1; + } else { // ord === 'column-major' + dim = 0; + } + // Initialize an index array workspace: + idx = zeros( ndims ); + + // Check elements according to a predicate function... + for ( i = 0; i < N; i++ ) { + if ( i > 0 ) { + idx = nextCartesianIndex( sh, ord, idx, dim, idx ); + } + v = x.get.apply( x, idx ); + if ( !clbk.call( ctx, v, idx.slice(), x ) ) { + return false; + } + } + return true; +} + + +// EXPORTS // + +module.exports = every; diff --git a/lib/node_modules/@stdlib/ndarray/every/package.json b/lib/node_modules/@stdlib/ndarray/every/package.json new file mode 100644 index 000000000000..b95be42165fe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/ndarray/every", + "version": "0.0.0", + "description": "Return a boolean indicating whether every element in the ndarray passes a test implemented by a predicate function.", + "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", + "strided", + "array", + "ndarray", + "every", + "reject", + "select", + "take" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/every/test/test.js b/lib/node_modules/@stdlib/ndarray/every/test/test.js new file mode 100644 index 000000000000..a7fa1d553f79 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/every/test/test.js @@ -0,0 +1,288 @@ +/** +* @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 ones = require( '@stdlib/array/ones' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); +var strides2offset = require( '@stdlib/ndarray/base/strides2offset' ); +var Float64Array = require( '@stdlib/array/float64' ); +var every = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof every, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'data': true + } + ]; + + 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() { + every( value, predicate ); + }; + } + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function throws an error if a callback argument which is not a function', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + 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() { + every( x, value ); + }; + } +}); + +tape( 'the function checks every element in array according to a predicate function (row-major)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = every( x, predicate ); + + expected = true; + t.strictEqual( y, expected, 'returns expected value' ); + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = every( x, predicate ); + + expected = false; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function checks every element in array according to a predicate function (column-major)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = every( x, predicate ); + + expected = true; + t.strictEqual( y, expected, 'returns expected value' ); + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + y = every( x, predicate ); + + expected = false; + t.strictEqual( y, expected, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + return ( z > 0.0 ); + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = every( x, predicate, ctx ); + + expected = true; + t.strictEqual( y, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = every( x, predicate, ctx ); + + expected = false; + t.strictEqual( y, expected, 'returns expected value' ); + t.strictEqual( ctx.count, 2, 'returns expected value' ); + + t.end(); + + function predicate( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + return ( z > 0.0 ); + } +}); + +tape( 'the function invokes a provided callback with three arguments', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + y = every( x, predicate ); + + expected = true; + t.strictEqual( y, expected, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function predicate( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + return ( z > 0.0 ); + } +});