From 606b45c2b02f93374634f7b1708c2f953dc45e0e Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Thu, 26 Dec 2024 15:15:36 +0000 Subject: [PATCH 1/4] feat: add C ndarray interface and refactor implementation --- 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/smaxabs/include.gypi | 2 +- .../include/stdlib/stats/base/smaxabs.h | 9 +- .../@stdlib/stats/base/smaxabs/lib/index.js | 7 +- .../@stdlib/stats/base/smaxabs/lib/ndarray.js | 18 ++- .../stats/base/smaxabs/lib/ndarray.native.js | 18 +-- .../@stdlib/stats/base/smaxabs/lib/smaxabs.js | 40 +----- .../stats/base/smaxabs/lib/smaxabs.native.js | 9 +- .../@stdlib/stats/base/smaxabs/manifest.json | 75 ++++++++++- .../@stdlib/stats/base/smaxabs/src/addon.c | 60 +++++++++ .../@stdlib/stats/base/smaxabs/src/addon.cpp | 117 ------------------ .../@stdlib/stats/base/smaxabs/src/main.c | 72 +++++++++++ .../@stdlib/stats/base/smaxabs/src/smaxabs.c | 61 --------- 12 files changed, 235 insertions(+), 253 deletions(-) create mode 100644 lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.c delete mode 100644 lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.cpp create mode 100644 lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c delete mode 100644 lib/node_modules/@stdlib/stats/base/smaxabs/src/smaxabs.c diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/include.gypi b/lib/node_modules/@stdlib/stats/base/smaxabs/include.gypi index 868c5c12e852..26476a8c2655 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/include.gypi +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/include.gypi @@ -36,7 +36,7 @@ # Source files: 'src_files': [ - '<(src_dir)/addon.cpp', + '<(src_dir)/addon.c', ' +#include "stdlib/blas/base/shared.h" /* * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. @@ -31,7 +31,12 @@ extern "C" { /** * Computes the maximum absolute value of a single-precision floating-point strided array. */ -float stdlib_strided_smaxabs( const int64_t N, const float *X, const int64_t stride ); +float API_SUFFIX(stdlib_strided_smaxabs)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ); + +/** +* Computes the maximum absolute value of a single-precision floating-point strided array using alternative indexing semantics. +*/ +float API_SUFFIX(stdlib_strided_smaxabs_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/index.js b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/index.js index 1b109b1f7542..7916858f5b39 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/index.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/index.js @@ -28,20 +28,17 @@ * var smaxabs = require( '@stdlib/stats/base/smaxabs' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = smaxabs( N, x, 1 ); +* var v = smaxabs( x.length, x, 1 ); * // returns 2.0 * * @example * var Float32Array = require( '@stdlib/array/float32' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * var smaxabs = require( '@stdlib/stats/base/smaxabs' ); * * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = smaxabs.ndarray( N, x, 2, 1 ); +* var v = smaxabs.ndarray( 8, x, 2, 1 ); * // returns 4.0 */ diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.js b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.js index 758f4e196614..8dc88df4ef0d 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.js @@ -31,21 +31,19 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float32Array} x - input array -* @param {integer} stride - stride length -* @param {NonNegativeInteger} offset - starting index +* @param {integer} strideX - stride length +* @param {NonNegativeInteger} offsetX - starting index * @returns {number} maximum absolute value * * @example * var Float32Array = require( '@stdlib/array/float32' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = smaxabs( N, x, 2, 1 ); +* var v = smaxabs( 4, x, 2, 1 ); * // returns 4.0 */ -function smaxabs( N, x, stride, offset ) { +function smaxabs( N, x, strideX, offsetX ) { var max; var ix; var v; @@ -54,13 +52,13 @@ function smaxabs( N, x, stride, offset ) { if ( N <= 0 ) { return NaN; } - if ( N === 1 || stride === 0 ) { - return abs( x[ offset ] ); + if ( N === 1 || strideX === 0 ) { + return abs( x[ offsetX ] ); } - ix = offset; + ix = offsetX; max = abs( x[ ix ] ); for ( i = 1; i < N; i++ ) { - ix += stride; + ix += strideX; v = abs( x[ ix ] ); if ( isnanf( v ) ) { return v; diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.native.js index 078b01e4f8fd..ea37fa96e8d3 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/ndarray.native.js @@ -20,8 +20,7 @@ // MODULES // -var Float32Array = require( '@stdlib/array/float32' ); -var addon = require( './smaxabs.native.js' ); +var addon = require( './../src/addon.node' ); // MAIN // @@ -31,27 +30,20 @@ var addon = require( './smaxabs.native.js' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float32Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @param {NonNegativeInteger} offset - starting index * @returns {number} maximum absolute value * * @example * var Float32Array = require( '@stdlib/array/float32' ); -* var floor = require( '@stdlib/math/base/special/floor' ); * * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -* var N = floor( x.length / 2 ); * -* var v = smaxabs( N, x, 2, 1 ); +* var v = smaxabs( 4, x, 2, 1 ); * // returns 4.0 */ -function smaxabs( N, x, stride, offset ) { - var view; - if ( stride < 0 ) { - offset += (N-1) * stride; - } - view = new Float32Array( x.buffer, x.byteOffset+(x.BYTES_PER_ELEMENT*offset), x.length-offset ); // eslint-disable-line max-len - return addon( N, view, stride ); +function smaxabs( N, x, strideX, offset ) { + return addon.ndarray( N, x, strideX, offset ); } diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.js b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.js index e5144d977b84..ea5bd0fb36fe 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.js @@ -20,8 +20,8 @@ // MODULES // -var isnanf = require( '@stdlib/math/base/assert/is-nanf' ); -var abs = require( '@stdlib/math/base/special/abs' ); +var stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); // MAIN // @@ -31,47 +31,19 @@ var abs = require( '@stdlib/math/base/special/abs' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float32Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} maximum absolute value * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = smaxabs( N, x, 1 ); +* var v = smaxabs( x.length, x, 1 ); * // returns 2.0 */ -function smaxabs( N, x, stride ) { - var max; - var ix; - var v; - var i; - - if ( N <= 0 ) { - return NaN; - } - if ( N === 1 || stride === 0 ) { - return abs( x[ 0 ] ); - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - max = abs( x[ ix ] ); - for ( i = 1; i < N; i++ ) { - ix += stride; - v = abs( x[ ix ] ); - if ( isnanf( v ) ) { - return v; - } - if ( v > max ) { - max = v; - } - } - return max; +function smaxabs( N, x, strideX ) { + return ndarray( N, x, strideX, stride2offset( N, strideX ) ); } diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.native.js index ff3adebad00d..3b5a4a23ca6e 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/lib/smaxabs.native.js @@ -30,20 +30,19 @@ var addon = require( './../src/addon.node' ); * * @param {PositiveInteger} N - number of indexed elements * @param {Float32Array} x - input array -* @param {integer} stride - stride length +* @param {integer} strideX - stride length * @returns {number} maximum absolute value * * @example * var Float32Array = require( '@stdlib/array/float32' ); * * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -* var N = x.length; * -* var v = smaxabs( N, x, 1 ); +* var v = smaxabs( x.length, x, 1 ); * // returns 2.0 */ -function smaxabs( N, x, stride ) { - return addon( N, x, stride ); +function smaxabs( N, x, strideX ) { + return addon( N, x, strideX ); } diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/manifest.json b/lib/node_modules/@stdlib/stats/base/smaxabs/manifest.json index 7f3aae50937e..4b310a204081 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/manifest.json +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/manifest.json @@ -1,5 +1,8 @@ { - "options": {}, + "options": { + "task": "build", + "wasm": false + }, "fields": [ { "field": "src", @@ -24,18 +27,80 @@ ], "confs": [ { + "task": "build", + "wasm": false, "src": [ - "./src/smaxabs.c" + "./src/main.c" ], "include": [ "./include" ], - "libraries": [ - "-lm" + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/special/absf", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/napi/export", + "@stdlib/napi/argv", + "@stdlib/napi/argv-int64", + "@stdlib/napi/argv-strided-float32array", + "@stdlib/napi/create-double" + ] + }, + { + "task": "benchmark", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" + ] + }, + { + "task": "examples", + "wasm": false, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" + ] + }, + { + "task": "", + "wasm": true, + "src": [ + "./src/main.c" + ], + "include": [ + "./include" ], + "libraries": [], "libpath": [], "dependencies": [ - "@stdlib/math/base/assert/is-nanf" + "@stdlib/blas/base/shared", + "@stdlib/strided/base/stride2offset", + "@stdlib/math/base/assert/is-nanf", + "@stdlib/math/base/special/absf" ] } ] diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.c b/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.c new file mode 100644 index 000000000000..fa15e9dd1b47 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.c @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/base/smaxabs.h" +#include "stdlib/napi/export.h" +#include "stdlib/napi/argv.h" +#include "stdlib/napi/argv_int64.h" +#include "stdlib/napi/argv_strided_float32array.h" +#include "stdlib/napi/create_double.h" +#include + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_smaxabs( N, X, strideX ), v ); + return v; +} + +/** +* Receives JavaScript callback invocation data. +* +* @param env environment under which the function is invoked +* @param info callback data +* @return Node-API value +*/ +static napi_value addon_method( napi_env env, napi_callback_info info ) { + STDLIB_NAPI_ARGV( env, info, argv, argc, 4 ); + STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 ); + STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 2 ); + STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 3 ); + STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 1 ); + STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_smaxabs_ndarray( N, X, strideX, offsetX ), v ); + return v; +} + +STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method ) diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.cpp b/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.cpp deleted file mode 100644 index 3002410ce423..000000000000 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/src/addon.cpp +++ /dev/null @@ -1,117 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/stats/base/smaxabs.h" -#include -#include -#include -#include -#include - -/** -* Add-on namespace. -*/ -namespace stdlib_stats_base_smaxabs { - - /** - * Computes the maximum absolute value of a single-precision floating-point strided array. - * - * ## Notes - * - * - When called from JavaScript, the function expects three arguments: - * - * - `N`: number of indexed elements - * - `X`: input array - * - `stride`: stride length - */ - napi_value node_smaxabs( napi_env env, napi_callback_info info ) { - napi_status status; - - size_t argc = 3; - napi_value argv[ 3 ]; - status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr ); - assert( status == napi_ok ); - - if ( argc < 3 ) { - napi_throw_error( env, nullptr, "invalid invocation. Must provide 3 arguments." ); - return nullptr; - } - - napi_valuetype vtype0; - status = napi_typeof( env, argv[ 0 ], &vtype0 ); - assert( status == napi_ok ); - if ( vtype0 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. First argument must be a number." ); - return nullptr; - } - - bool res; - status = napi_is_typedarray( env, argv[ 1 ], &res ); - assert( status == napi_ok ); - if ( res == false ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float32Array." ); - return nullptr; - } - - napi_valuetype vtype2; - status = napi_typeof( env, argv[ 2 ], &vtype2 ); - assert( status == napi_ok ); - if ( vtype2 != napi_number ) { - napi_throw_type_error( env, nullptr, "invalid argument. Third argument must be a number." ); - return nullptr; - } - - int64_t N; - status = napi_get_value_int64( env, argv[ 0 ], &N ); - assert( status == napi_ok ); - - int64_t stride; - status = napi_get_value_int64( env, argv[ 2 ], &stride ); - assert( status == napi_ok ); - - napi_typedarray_type vtype1; - size_t xlen; - void *X; - status = napi_get_typedarray_info( env, argv[ 1 ], &vtype1, &xlen, &X, nullptr, nullptr ); - assert( status == napi_ok ); - if ( vtype1 != napi_float32_array ) { - napi_throw_type_error( env, nullptr, "invalid argument. Second argument must be a Float32Array." ); - return nullptr; - } - if ( (N-1)*llabs(stride) >= (int64_t)xlen ) { - napi_throw_range_error( env, nullptr, "invalid argument. Second argument has insufficient elements based on the associated stride and the number of indexed elements." ); - return nullptr; - } - - napi_value v; - status = napi_create_double( env, (double)stdlib_strided_smaxabs( N, (float *)X, stride ), &v ); - assert( status == napi_ok ); - - return v; - } - - napi_value Init( napi_env env, napi_value exports ) { - napi_status status; - napi_value fcn; - status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, node_smaxabs, NULL, &fcn ); - assert( status == napi_ok ); - return fcn; - } - - NAPI_MODULE( NODE_GYP_MODULE_NAME, Init ) -} // end namespace stdlib_stats_base_smaxabs diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c b/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c new file mode 100644 index 000000000000..7d25850f8af1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c @@ -0,0 +1,72 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2020 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/stats/base/smaxabs.h" +#include "stdlib/math/base/assert/is_nanf.h" +#include "stdlib/blas/base/shared.h" +#include "stdlib/strided/base/stride2offset.h" +#include "stdlib/math/base/special/absf.h" + +/** +* Computes the maximum absolute value of a single-precision floating-point strided array. +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @return output value +*/ +float API_SUFFIX(stdlib_strided_smaxabs)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ) { + const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); + return API_SUFFIX(stdlib_strided_smaxabs_ndarray)( N, X, strideX, ox ); +} + +/** +* Computes the maximum absolute value of a single-precision floating-point strided array. +* +* @param N number of indexed elements +* @param X input array +* @param strideX stride length +* @param offsetX starting index for X +* @return output value +*/ +float API_SUFFIX(stdlib_strided_smaxabs_ndarray)( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { + CBLAS_INT ix; + CBLAS_INT i; + float max; + float v; + + if ( N <= 0 ) { + return 0.0 / 0.0; // NaN + } + if ( N == 1 || strideX == 0 ) { + return stdlib_base_absf( X[ offsetX ] ); + } + ix = offsetX; + max = stdlib_base_absf( X[ ix ] ); + for ( i = 1; i < N; i++ ) { + ix += strideX; + v = stdlib_base_absf( X[ ix ] ); + if ( stdlib_base_is_nanf( v ) ) { + return v; + } + if ( v > max ) { + max = v; + } + } + return max; +} diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/src/smaxabs.c b/lib/node_modules/@stdlib/stats/base/smaxabs/src/smaxabs.c deleted file mode 100644 index d80017877582..000000000000 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/src/smaxabs.c +++ /dev/null @@ -1,61 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2020 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "stdlib/stats/base/smaxabs.h" -#include "stdlib/math/base/assert/is_nanf.h" -#include -#include - -/** -* Computes the maximum absolute value of a single-precision floating-point strided array. -* -* @param N number of indexed elements -* @param X input array -* @param stride stride length -* @return output value -*/ -float stdlib_strided_smaxabs( const int64_t N, const float *X, const int64_t stride ) { - int64_t ix; - int64_t i; - float max; - float v; - - if ( N <= 0 ) { - return 0.0 / 0.0; // NaN - } - if ( N == 1 || stride == 0 ) { - return fabsf( X[ 0 ] ); - } - if ( stride < 0 ) { - ix = (1-N) * stride; - } else { - ix = 0; - } - max = fabsf( X[ ix ] ); - for ( i = 1; i < N; i++ ) { - ix += stride; - v = fabsf( X[ ix ] ); - if ( stdlib_base_is_nanf( v ) ) { - return v; - } - if ( v > max ) { - max = v; - } - } - return max; -} From 3734452f9db3d208029e129ae666e1efe78c11fb Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Thu, 26 Dec 2024 16:22:19 +0000 Subject: [PATCH 2/4] refactor: update tests --- 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/stats/base/smaxabs/src/main.c | 2 +- .../@stdlib/stats/base/smaxabs/test/test.ndarray.js | 13 +++---------- .../stats/base/smaxabs/test/test.ndarray.native.js | 13 +++---------- .../@stdlib/stats/base/smaxabs/test/test.smaxabs.js | 13 +++---------- .../stats/base/smaxabs/test/test.smaxabs.native.js | 13 +++---------- 5 files changed, 13 insertions(+), 41 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c b/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c index 7d25850f8af1..45727923ee01 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/src/main.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2020 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.js index c52a2d48b61c..8f322c65b5ca 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float32Array = require( '@stdlib/array/float32' ); @@ -96,7 +95,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -111,15 +109,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2, 0 ); + v = smaxabs( 4, x, 2, 0 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -134,8 +130,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, -2, 6 ); + v = smaxabs( 4, x, -2, 6 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -154,7 +149,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f }); tape( 'the function supports an `offset` parameter', function test( t ) { - var N; var x; var v; @@ -168,9 +162,8 @@ tape( 'the function supports an `offset` parameter', function test( t ) { 3.0, 4.0 // 3 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2, 1 ); + v = smaxabs( 4, x, 2, 1 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.native.js index 744cdcfdc765..9e0b6da9c8f0 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.ndarray.native.js @@ -22,7 +22,6 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float32Array = require( '@stdlib/array/float32' ); @@ -105,7 +104,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -120,15 +118,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2, 0 ); + v = smaxabs( 4, x, 2, 0 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -143,8 +139,7 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, -2, 6 ); + v = smaxabs( 4, x, -2, 6 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -163,7 +158,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f }); tape( 'the function supports an `offset` parameter', opts, function test( t ) { - var N; var x; var v; @@ -177,9 +171,8 @@ tape( 'the function supports an `offset` parameter', opts, function test( t ) { 3.0, 4.0 // 3 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2, 1 ); + v = smaxabs( 4, x, 2, 1 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.js b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.js index e6faa3fa18e3..a34f72b2ebb2 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.js @@ -21,7 +21,6 @@ // MODULES // var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float32Array = require( '@stdlib/array/float32' ); @@ -96,7 +95,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', function test( t ) { - var N; var x; var v; @@ -111,15 +109,13 @@ tape( 'the function supports a `stride` parameter', function test( t ) { 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2 ); + v = smaxabs( 4, x, 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', function test( t ) { - var N; var x; var v; @@ -134,8 +130,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t ) 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, -2 ); + v = smaxabs( 4, x, -2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -156,7 +151,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f tape( 'the function supports view offsets', function test( t ) { var x0; var x1; - var N; var v; x0 = new Float32Array([ @@ -172,9 +166,8 @@ tape( 'the function supports view offsets', function test( t ) { ]); x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = smaxabs( N, x1, 2 ); + v = smaxabs( 4, x1, 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.native.js index a4b6339d1140..8772776acd51 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/test/test.smaxabs.native.js @@ -22,7 +22,6 @@ var resolve = require( 'path' ).resolve; var tape = require( 'tape' ); -var floor = require( '@stdlib/math/base/special/floor' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); var Float32Array = require( '@stdlib/array/float32' ); @@ -187,7 +186,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first }); tape( 'the function supports a `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -202,15 +200,13 @@ tape( 'the function supports a `stride` parameter', opts, function test( t ) { 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, 2 ); + v = smaxabs( 4, x, 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); }); tape( 'the function supports a negative `stride` parameter', opts, function test( t ) { - var N; var x; var v; @@ -225,8 +221,7 @@ tape( 'the function supports a negative `stride` parameter', opts, function test 2.0 ]); - N = floor( x.length / 2 ); - v = smaxabs( N, x, -2 ); + v = smaxabs( 4, x, -2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); @@ -247,7 +242,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f tape( 'the function supports view offsets', opts, function test( t ) { var x0; var x1; - var N; var v; x0 = new Float32Array([ @@ -263,9 +257,8 @@ tape( 'the function supports view offsets', opts, function test( t ) { ]); x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element - N = floor(x1.length / 2); - v = smaxabs( N, x1, 2 ); + v = smaxabs( 4, x1, 2 ); t.strictEqual( v, 4.0, 'returns expected value' ); t.end(); From 403c04b08b49cc73c702ef4bb3cc8c2f27d33a79 Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Thu, 26 Dec 2024 17:17:52 +0000 Subject: [PATCH 3/4] refactor: update docs, examples and benchmarks --- 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 status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - 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: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../stats/base/smaxabs/benchmark/benchmark.js | 18 +++---- .../smaxabs/benchmark/benchmark.native.js | 14 ++---- .../smaxabs/benchmark/benchmark.ndarray.js | 18 +++---- .../benchmark/benchmark.ndarray.native.js | 14 ++---- .../smaxabs/benchmark/c/benchmark.length.c | 50 ++++++++++++++++++- .../@stdlib/stats/base/smaxabs/docs/repl.txt | 32 ++++++------ .../stats/base/smaxabs/docs/types/index.d.ts | 12 ++--- .../stats/base/smaxabs/examples/c/example.c | 9 ++-- .../stats/base/smaxabs/examples/index.js | 14 ++---- 9 files changed, 104 insertions(+), 77 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.js index fff4b4b554b0..31d02efe78ce 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.js @@ -21,14 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +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 Float32Array = require( '@stdlib/array/float32' ); var pkg = require( './../package.json' ).name; var smaxabs = require( './../lib/smaxabs.js' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -39,13 +45,7 @@ var smaxabs = require( './../lib/smaxabs.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float32Array( len ); - for ( i = 0; i < x.length; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.native.js index 014bdf17028f..83d8a82710da 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.native.js @@ -22,10 +22,9 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +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 Float32Array = require( '@stdlib/array/float32' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -36,6 +35,9 @@ var smaxabs = tryRequire( resolve( __dirname, './../lib/smaxabs.native.js' ) ); var opts = { 'skip': ( smaxabs instanceof Error ) }; +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -48,13 +50,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float32Array( len ); - for ( i = 0; i < x.length; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.js index 45a7f855fcbf..a8832c304903 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.js @@ -21,14 +21,20 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +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 Float32Array = require( '@stdlib/array/float32' ); var pkg = require( './../package.json' ).name; var smaxabs = require( './../lib/ndarray.js' ); +// VARIABLES // + +var options = { + 'dtype': 'float32' +}; + + // FUNCTIONS // /** @@ -39,13 +45,7 @@ var smaxabs = require( './../lib/ndarray.js' ); * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float32Array( len ); - for ( i = 0; i < x.length; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.native.js b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.native.js index d769cfead44e..d81a42559922 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.native.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/benchmark.ndarray.native.js @@ -22,10 +22,9 @@ var resolve = require( 'path' ).resolve; var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); +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 Float32Array = require( '@stdlib/array/float32' ); var tryRequire = require( '@stdlib/utils/try-require' ); var pkg = require( './../package.json' ).name; @@ -36,6 +35,9 @@ var smaxabs = tryRequire( resolve( __dirname, './../lib/ndarray.native.js' ) ); var opts = { 'skip': ( smaxabs instanceof Error ) }; +var options = { + 'dtype': 'float32' +}; // FUNCTIONS // @@ -48,13 +50,7 @@ var opts = { * @returns {Function} benchmark function */ function createBenchmark( len ) { - var x; - var i; - - x = new Float32Array( len ); - for ( i = 0; i < x.length; i++ ) { - x[ i ] = ( randu()*20.0 ) - 10.0; - } + var x = uniform( len, -10.0, 10.0, options ); return benchmark; function benchmark( b ) { diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/c/benchmark.length.c b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/c/benchmark.length.c index 1757b4edf654..be645b41d92f 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/c/benchmark.length.c +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/benchmark/c/benchmark.length.c @@ -94,7 +94,7 @@ static float rand_float( void ) { * @param len array length * @return elapsed time in seconds */ -static double benchmark( int iterations, int len ) { +static double benchmark1( int iterations, int len ) { double elapsed; float x[ len ]; float v; @@ -107,6 +107,7 @@ static double benchmark( int iterations, int len ) { v = 0.0f; t = tic(); for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar v = stdlib_strided_smaxabs( len, x, 1 ); if ( v != v ) { printf( "should not return NaN\n" ); @@ -120,6 +121,40 @@ static double benchmark( int iterations, int len ) { return elapsed; } +/** +* Runs a benchmark. +* +* @param iterations number of iterations +* @param len array length +* @return elapsed time in seconds +*/ +static double benchmark2( int iterations, int len ) { + double elapsed; + float x[ len ]; + float v; + double t; + int i; + + for ( i = 0; i < len; i++ ) { + x[ i ] = ( rand_float()*20000.0f ) - 10000.0f; + } + v = 0.0f; + t = tic(); + for ( i = 0; i < iterations; i++ ) { + // cppcheck-suppress uninitvar + v = stdlib_strided_smaxabs_ndarray( len, x, 1, 0 ); + if ( v != v ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( v != v ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + /** * Main execution sequence. */ @@ -142,7 +177,18 @@ int main( void ) { for ( j = 0; j < REPEATS; j++ ) { count += 1; printf( "# c::%s:len=%d\n", NAME, len ); - elapsed = benchmark( iter, len ); + elapsed = benchmark1( iter, len ); + print_results( iter, elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + } + for ( i = MIN; i <= MAX; i++ ) { + len = pow( 10, i ); + iter = ITERATIONS / pow( 10, i-1 ); + for ( j = 0; j < REPEATS; j++ ) { + count += 1; + printf( "# c::%s:ndarray:len=%d\n", NAME, len ); + elapsed = benchmark2( iter, len ); print_results( iter, elapsed ); printf( "ok %d benchmark finished\n", count ); } diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/smaxabs/docs/repl.txt index 4b4f5378acdf..9a100e107961 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/docs/repl.txt @@ -1,10 +1,10 @@ -{{alias}}( N, x, stride ) +{{alias}}( N, x, strideX ) Computes the maximum absolute value of a single-precision floating-point strided array. - The `N` and `stride` parameters determine which elements in `x` are accessed - at runtime. + The `N` and stride parameters determine which elements in the strided array + are accessed at runtime. Indexing is relative to the first index. To introduce an offset, use a typed array view. @@ -19,8 +19,8 @@ x: Float32Array Input array. - stride: integer - Index increment. + strideX: integer + Stride Length. Returns ------- @@ -34,22 +34,19 @@ > {{alias}}( x.length, x, 1 ) 2.0 - // Using `N` and `stride` parameters: + // Using `N` and stride parameters: > x = new {{alias:@stdlib/array/float32}}( [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ] ); - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > var stride = 2; - > {{alias}}( N, x, stride ) + > {{alias}}( 3, x, 2 ) 2.0 // Using view offsets: > var x0 = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); > var x1 = new {{alias:@stdlib/array/float32}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); - > N = {{alias:@stdlib/math/base/special/floor}}( x0.length / 2 ); - > stride = 2; - > {{alias}}( N, x1, stride ) + > {{alias}}( 3, x1, 2 ) 2.0 -{{alias}}.ndarray( N, x, stride, offset ) + +{{alias}}.ndarray( N, x, strideX, offsetX ) Computes the maximum absolute value of a single-precision floating-point strided array using alternative indexing semantics. @@ -65,10 +62,10 @@ x: Float32Array Input array. - stride: integer - Index increment. + strideX: integer + Stride Length. - offset: integer + offsetX: integer Starting index. Returns @@ -85,8 +82,7 @@ // Using offset parameter: > var x = new {{alias:@stdlib/array/float32}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, -1.0 ] ); - > var N = {{alias:@stdlib/math/base/special/floor}}( x.length / 2 ); - > {{alias}}.ndarray( N, x, 2, 1 ) + > {{alias}}.ndarray( 3, x, 2, 1 ) 2.0 See Also diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/smaxabs/docs/types/index.d.ts index 5d789412ef1e..b15dbb449cab 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/docs/types/index.d.ts @@ -27,7 +27,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length + * @param strideX - stride length * @returns maximum absolute value * * @example @@ -38,15 +38,15 @@ interface Routine { * var v = smaxabs( x.length, x, 1 ); * // returns 2.0 */ - ( N: number, x: Float32Array, stride: number ): number; + ( N: number, x: Float32Array, strideX: number ): number; /** * Computes the maximum absolute value of a single-precision floating-point strided array using alternative indexing semantics. * * @param N - number of indexed elements * @param x - input array - * @param stride - stride length - * @param offset - starting index + * @param strideX - stride length + * @param offsetX - starting index * @returns maximum absolute value * * @example @@ -57,7 +57,7 @@ interface Routine { * var v = smaxabs.ndarray( x.length, x, 1, 0 ); * // returns 2.0 */ - ndarray( N: number, x: Float32Array, stride: number, offset: number ): number; + ndarray( N: number, x: Float32Array, strideX: number, offsetX: number ): number; } /** @@ -65,7 +65,7 @@ interface Routine { * * @param N - number of indexed elements * @param x - input array -* @param stride - stride length +* @param strideX - stride length * @returns maximum absolute value * * @example diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/examples/c/example.c b/lib/node_modules/@stdlib/stats/base/smaxabs/examples/c/example.c index 1b7c5ed9c0a2..aa18def70924 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/examples/c/example.c +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/examples/c/example.c @@ -17,21 +17,20 @@ */ #include "stdlib/stats/base/smaxabs.h" -#include #include int main( void ) { // Create a strided array: - float x[] = { 1.0, -2.0, -3.0, 4.0, -5.0, -6.0, 7.0, 8.0 }; + const float x[] = { 1.0f, -2.0f, -3.0f, 4.0f, -5.0f, -6.0f, 7.0f, 8.0f }; // Specify the number of elements: - int64_t N = 4; + const int N = 4; // Specify the stride length: - int64_t stride = 2; + const int strideX = 2; // Compute the maximum absolute value: - float v = stdlib_strided_smaxabs( N, x, stride ); + float v = stdlib_strided_smaxabs( N, x, strideX ); // Print the result: printf( "maxabs: %f\n", v ); diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/examples/index.js b/lib/node_modules/@stdlib/stats/base/smaxabs/examples/index.js index d02feaa168e5..53e9a6f50654 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/examples/index.js +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/examples/index.js @@ -18,18 +18,12 @@ 'use strict'; -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float32Array = require( '@stdlib/array/float32' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); var smaxabs = require( './../lib' ); -var x; -var i; - -x = new Float32Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); console.log( x ); var v = smaxabs( x.length, x, 1 ); From 770aed0682447e2d13fdf4affc5986bc4db489ed Mon Sep 17 00:00:00 2001 From: aayush0325 Date: Thu, 26 Dec 2024 18:44:54 +0000 Subject: [PATCH 4/4] docs: update README.md --- 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 status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na --- --- .../@stdlib/stats/base/smaxabs/README.md | 163 ++++++++++++++---- 1 file changed, 133 insertions(+), 30 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/base/smaxabs/README.md b/lib/node_modules/@stdlib/stats/base/smaxabs/README.md index 903d45390af7..2fe593a759bd 100644 --- a/lib/node_modules/@stdlib/stats/base/smaxabs/README.md +++ b/lib/node_modules/@stdlib/stats/base/smaxabs/README.md @@ -36,7 +36,7 @@ limitations under the License. var smaxabs = require( '@stdlib/stats/base/smaxabs' ); ``` -#### smaxabs( N, x, stride ) +#### smaxabs( N, x, strideX ) Computes the maximum absolute value of a single-precision floating-point strided array `x`. @@ -44,9 +44,8 @@ Computes the maximum absolute value of a single-precision floating-point strided var Float32Array = require( '@stdlib/array/float32' ); var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -var N = x.length; -var v = smaxabs( N, x, 1 ); +var v = smaxabs( x.length, x, 1 ); // returns 2.0 ``` @@ -54,18 +53,16 @@ The function has the following parameters: - **N**: number of indexed elements. - **x**: input [`Float32Array`][@stdlib/array/float32]. -- **stride**: index increment for `x`. +- **strideX**: stride length. -The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`, +The `N` and stride parameters determine which elements in `x` are accessed at runtime. For example, to compute the maximum absolute value of every other element in `x`, ```javascript var Float32Array = require( '@stdlib/array/float32' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); -var N = floor( x.length / 2 ); -var v = smaxabs( N, x, 2 ); +var v = smaxabs( 4, x, 2 ); // returns 4.0 ``` @@ -75,18 +72,15 @@ Note that indexing is relative to the first index. To introduce an offset, use [ ```javascript var Float32Array = require( '@stdlib/array/float32' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element -var N = floor( x0.length / 2 ); - -var v = smaxabs( N, x1, 2 ); +var v = smaxabs( 4, x1, 2 ); // returns 4.0 ``` -#### smaxabs.ndarray( N, x, stride, offset ) +#### smaxabs.ndarray( N, x, strideX, offsetX ) Computes the maximum absolute value of a single-precision floating-point strided array using alternative indexing semantics. @@ -94,26 +88,23 @@ Computes the maximum absolute value of a single-precision floating-point strided var Float32Array = require( '@stdlib/array/float32' ); var x = new Float32Array( [ 1.0, -2.0, 2.0 ] ); -var N = x.length; -var v = smaxabs.ndarray( N, x, 1, 0 ); +var v = smaxabs.ndarray( x.length, x, 1, 0 ); // returns 2.0 ``` The function has the following additional parameters: -- **offset**: starting index for `x`. +- **offsetX**: starting index for `x`. -While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the maximum absolute value for every other value in `x` starting from the second value +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the maximum absolute value for every other element in `x` starting from the second element ```javascript var Float32Array = require( '@stdlib/array/float32' ); -var floor = require( '@stdlib/math/base/special/floor' ); var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); -var N = floor( x.length / 2 ); -var v = smaxabs.ndarray( N, x, 2, 1 ); +var v = smaxabs.ndarray( 4, x, 2, 1 ); // returns 4.0 ``` @@ -138,18 +129,13 @@ var v = smaxabs.ndarray( N, x, 2, 1 ); ```javascript -var randu = require( '@stdlib/random/base/randu' ); -var round = require( '@stdlib/math/base/special/round' ); -var Float32Array = require( '@stdlib/array/float32' ); -var smaxabs = require( '@stdlib/stats/base/smaxabs' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); -var x; -var i; +var smaxabs = require( '@stdlib/stats/base/smaxabs' ); -x = new Float32Array( 10 ); -for ( i = 0; i < x.length; i++ ) { - x[ i ] = round( (randu()*100.0) - 50.0 ); -} +var x = discreteUniform( 10, -50, 50, { + 'dtype': 'float32' +}); console.log( x ); var v = smaxabs( x.length, x, 1 ); @@ -160,6 +146,123 @@ console.log( v ); + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/stats/base/smaxabs.h" +``` + +#### stdlib_strided_smaxabs( N, \*X, strideX ) + +Computes the maximum absolute value of a single-precision floating-point strided array. + +```c +const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f }; + +float v = stdlib_strided_smaxabs( 4, x, 1 ); +// returns 4.0f +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. + +```c +float stdlib_strided_smaxabs( const CBLAS_INT N, const float *X, const CBLAS_INT strideX ); +``` + +#### stdlib_strided_smaxabs_ndarray( N, \*X, strideX, offsetX ) + +Computes the maximum absolute value of a single-precision floating-point strided array using alternative indexing semantics. + +```c +const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f }; + +float v = stdlib_strided_smaxabs_ndarray( 4, x, 1, 0 ); +// returns 4.0f +``` + +The function accepts the following arguments: + +- **N**: `[in] CBLAS_INT` number of indexed elements. +- **X**: `[in] float*` input array. +- **strideX**: `[in] CBLAS_INT` stride length for `X`. +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. + +```c +float stdlib_strided_smaxabs_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/stats/base/smaxabs.h" +#include + +int main( void ) { + // Create a strided array: + const float x[] = { 1.0f, -2.0f, -3.0f, 4.0f, -5.0f, -6.0f, 7.0f, 8.0f }; + + // Specify the number of elements: + const int N = 4; + + // Specify the stride length: + const int strideX = 2; + + // Compute the maximum absolute value: + float v = stdlib_strided_smaxabs( N, x, strideX ); + + // Print the result: + printf( "maxabs: %f\n", v ); +} +``` + +
+ + + +
+ + +