From 5b42d5f3123706e1ca75eb01ab22412808a44a6e Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 28 Sep 2025 12:35:21 +0500 Subject: [PATCH 1/4] feat: add ndarray/base/flatten-shape-from --- 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: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/base/flatten-shape-from/README.md | 243 ++++++++++++++++++ .../flatten-shape-from/benchmark/benchmark.js | 82 ++++++ .../flatten-shape-from/benchmark/c/Makefile | 146 +++++++++++ .../benchmark/c/benchmark.c | 140 ++++++++++ .../base/flatten-shape-from/docs/repl.txt | 55 ++++ .../flatten-shape-from/docs/types/index.d.ts | 50 ++++ .../flatten-shape-from/docs/types/test.ts | 58 +++++ .../flatten-shape-from/examples/c/Makefile | 146 +++++++++++ .../flatten-shape-from/examples/c/example.c | 40 +++ .../base/flatten-shape-from/examples/index.js | 37 +++ .../stdlib/ndarray/base/flatten_shape_from.h | 40 +++ .../base/flatten-shape-from/lib/assign.js | 67 +++++ .../base/flatten-shape-from/lib/index.js | 49 ++++ .../base/flatten-shape-from/lib/main.js | 65 +++++ .../base/flatten-shape-from/manifest.json | 38 +++ .../base/flatten-shape-from/package.json | 70 +++++ .../base/flatten-shape-from/src/main.c | 65 +++++ .../base/flatten-shape-from/test/test.js | 141 ++++++++++ 18 files changed, 1532 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/include/stdlib/ndarray/base/flatten_shape_from.h create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/manifest.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c create mode 100644 lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md new file mode 100644 index 000000000000..11018260b3e4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md @@ -0,0 +1,243 @@ + + +# flattenShapeFrom + +> Flatten a shape starting from a specified dimension. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); +``` + +#### flattenShapeFrom( shape, dim ) + +Flattens a shape starting from a specified dimension. + +```javascript +var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 ); +// returns [ 3, 6 ] +``` + +The function accepts the following parameters: + +- **shape**: array shape. +- **dim**: dimension to start flattening from. + +#### flattenShapeFrom.assign( shape, dim, out ) + +Flattens a shape starting from a specified dimension and assigns results to a provided output array. + +```javascript +var sh = [ 0, 0 ]; + +var out = flattenShapeFrom.assign( [ 3, 3, 2 ], 1, sh ); +// returns [ 3, 6 ] + +var bool = ( sh === out ); +// returns true +``` + +The function accepts the following parameters: + +- **shape**: array shape. +- **dim**: dimension to start flattening from. +- **out**: output array. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var zip = require( '@stdlib/array/base/zip' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); + +var opts = { + 'dtype': 'int32' +}; +var d1 = discreteUniform( 100, 1, 10, opts ); +var d2 = discreteUniform( d1.length, 1, 10, opts ); +var d3 = discreteUniform( d1.length, 1, 10, opts ); +var d4 = discreteUniform( d1.length, 1, 10, opts ); + +var dims = discreteUniform( d1.length, 0, 3, opts ); +var shapes = zip( [ d1, d2, d3, d4 ] ); + +logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/ndarray/base/flatten_shape_from.h" +``` + +#### stdlib_ndarray_flatten_shape_from( ndims, \*shape, dim, \*out ) + +Flattens a shape starting from a specified dimension. + +```c +const int64_t ndims = 3; +const int64_t shape[] = { 2, 3, 10 }; +int64_t out[ 2 ]; + +stdlib_ndarray_flatten_shape( ndims, shape, 1, out ); +``` + +The function accepts the following arguments: + +- **ndims**: `[in] int64_t` number of dimensions. +- **shape**: `[in] int64_t*` array shape (dimensions). +- **dim**: `[in] int64_t` dimension to start flattening from. +- **out**: `[out] int64_t*` output array. + +```c +int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *shape, const int64_t dim, int64_t *out ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/ndarray/base/flatten_shape_from.h" +#include +#include + +int main( void ) { + const int64_t shape[] = { 2, 3, 4, 10 }; + const int64_t ndims = 4; + const int64_t dim = 2; + int64_t out[ 3 ]; + + stdlib_ndarray_flatten_shape_from( ndims, shape, dim, out ); + + int i; + printf( "shape = ( " ); + for ( i = 0; i < ndims-dim+1; i++ ) { + printf( "%"PRId64"", out[ i ] ); + if ( i < ndims-dim ) { + printf( ", " ); + } + } + printf( " )\n" ); +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/benchmark.js new file mode 100644 index 000000000000..d68fac3eb5d4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/benchmark.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var isArray = require( '@stdlib/assert/is-array' ); +var pkg = require( './../package.json' ).name; +var flattenShapeFrom = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var shape; + var out; + var i; + + shape = discreteUniform( 5, 1, 10, { + 'dtype': 'generic' + }); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + shape[ 0 ] += 1; + out = flattenShapeFrom( shape, 0 ); + if ( out.length !== 1 ) { + b.fail( 'should have expected length' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':assign', function benchmark( b ) { + var shape; + var out; + var i; + + shape = discreteUniform( 5, 1, 10, { + 'dtype': 'generic' + }); + + out = [ 0 ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + shape[ 0 ] += 1; + out = flattenShapeFrom.assign( shape, 0, out ); + if ( out.length !== 1 ) { + b.fail( 'should have expected length' ); + } + } + b.toc(); + if ( !isArray( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/Makefile new file mode 100644 index 000000000000..5d7e79f50788 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles C source files. +# +# @param {string} SOURCE_FILES - list of C source files +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [C_COMPILER] - C compiler +# @param {string} [CFLAGS] - C compiler flags +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} SOURCE_FILES - list of C source files +# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) +# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c new file mode 100644 index 000000000000..87a0ec950fab --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c @@ -0,0 +1,140 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/ndarray/base/flatten_shape_from.h" +#include +#include +#include +#include +#include +#include + +#define NAME "flatten-shape" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + double t; + int i; + + int64_t shape[] = { 10, 10, 10 }; + int64_t ndims = 3; + int64_t out[ 1 ]; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + shape[ 0 ] += 1; + stdlib_ndarray_flatten_shape_from( ndims, shape, 0, out ); + if ( out[ 0 ] < 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( out[ 0 ] < 0 ) { + printf( "unexpected result\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int i; + + count = 0; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::native::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt new file mode 100644 index 000000000000..5bb631b46e61 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt @@ -0,0 +1,55 @@ + +{{alias}}( shape, dim ) + Flattens a shape starting from a specified dimension. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to start flattening from. + + Returns + ------- + out: Array + Flattened shape. + + Examples + -------- + > var sh = [ 3, 3, 2 ]; + > var out = {{alias}}( sh, 1 ) + [ 3, 6 ] + + +{{alias}}.assign( shape, dim, out ) + Flattens a shape starting from a specified dimension and assigns results to + a provided output array. + + Parameters + ---------- + shape: ArrayLike + Array shape. + + dim: integer + Dimension to start flattening from. + + out: Array|TypedArray|Object + Output array. + + Returns + ------- + out: Array|TypedArray|Object + Output array. + + Examples + -------- + > var sh = [ 2, 1, 10 ]; + > var out = [ 0 ]; + > var s = {{alias}}.assign( sh, 0, out ) + [ 20 ] + > var bool = ( s === out ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/index.d.ts new file mode 100644 index 000000000000..cff0390a5639 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; + +/** +* Flattens a shape starting from a specified dimension. +* +* @param shape - array shape +* @param dim - dimension to start flattening from +* @returns flattened shape +* +* @example +* var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 ); +* // returns [ 3, 6 ] +* +* sh = flattenShapeFrom( [ 3, 2, 1 ], 1 ); +* // returns [ 3, 2 ] +* +* sh = flattenShapeFrom( [ 3 ], 0 ); +* // returns [ 3 ] +* +* sh = flattenShapeFrom( [ 3, 2 ], 0 ); +* // returns [ 6 ] +*/ +declare function flattenShapeFrom( shape: Collection, dim: number ): Array; + + +// EXPORTS // + +export = flattenShapeFrom; diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/test.ts new file mode 100644 index 000000000000..8f0eec47c569 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 flattenShapeFrom = require( './index' ); + + +// TESTS // + +// The function returns an array of numbers... +{ + flattenShapeFrom( [ 3, 2, 1 ], 1 ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a first argument that is not an array-like object containing numbers... +{ + flattenShapeFrom( true, 1 ); // $ExpectError + flattenShapeFrom( false, 1 ); // $ExpectError + flattenShapeFrom( null, 1 ); // $ExpectError + flattenShapeFrom( undefined, 1 ); // $ExpectError + flattenShapeFrom( '5', 1 ); // $ExpectError + flattenShapeFrom( [ '1', '2' ], 1 ); // $ExpectError + flattenShapeFrom( {}, 1 ); // $ExpectError + flattenShapeFrom( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument that is not a number... +{ + flattenShapeFrom( [ 2, 3 ], true ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], false ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], null ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], undefined ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], '5' ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], [ '1', '2' ] ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], {} ); // $ExpectError + flattenShapeFrom( [ 2, 3 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + flattenShapeFrom(); // $ExpectError + flattenShapeFrom( [ 3, 2 ] ); // $ExpectError + flattenShapeFrom( [ 3, 2 ], 1, 0 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/example.c b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/example.c new file mode 100644 index 000000000000..146ac08c9cfb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/c/example.c @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/ndarray/base/flatten_shape_from.h" +#include +#include + +int main( void ) { + const int64_t shape[] = { 2, 3, 4, 10 }; + const int64_t ndims = 4; + const int64_t dim = 2; + int64_t out[ 3 ]; + + stdlib_ndarray_flatten_shape_from( ndims, shape, dim, out ); + + int i; + printf( "shape = ( " ); + for ( i = 0; i < ndims-dim+1; i++ ) { + printf( "%"PRId64"", out[ i ] ); + if ( i < ndims-dim ) { + printf( ", " ); + } + } + printf( " )\n" ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js new file mode 100644 index 000000000000..da3acabc3787 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js @@ -0,0 +1,37 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 zip = require( '@stdlib/array/base/zip' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var flattenShapeFrom = require( './../lib' ); + +var opts = { + 'dtype': 'int32' +}; +var d1 = discreteUniform( 100, 1, 10, opts ); +var d2 = discreteUniform( d1.length, 1, 10, opts ); +var d3 = discreteUniform( d1.length, 1, 10, opts ); +var d4 = discreteUniform( d1.length, 1, 10, opts ); + +var dims = discreteUniform( d1.length, 0, 3, opts ); +var shapes = zip( [ d1, d2, d3, d4 ] ); + +logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom ); diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/include/stdlib/ndarray/base/flatten_shape_from.h b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/include/stdlib/ndarray/base/flatten_shape_from.h new file mode 100644 index 000000000000..af6f75317453 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/include/stdlib/ndarray/base/flatten_shape_from.h @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +#ifndef STDLIB_NDARRAY_BASE_FLATTEN_SHAPE_FROM_H +#define STDLIB_NDARRAY_BASE_FLATTEN_SHAPE_FROM_H + +#include + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Flattens a shape starting from a specified dimension. +*/ +int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *shape, const int64_t dim, int64_t *out ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_NDARRAY_BASE_FLATTEN_SHAPE_FROM_H diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js new file mode 100644 index 000000000000..c738f95b2f77 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +// MAIN // + +/** +* Flattens a shape starting from a specified dimension and assigns results to a provided output array. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {NonNegativeInteger} dim - dimension to start flattening from +* @param {(Array|TypedArray|Object)} out - output object +* @returns {(Array|TypedArray|Object)} array shape +* +* @example +* var sh = [ 0, 0 ]; +* +* var out = flattenShapeFrom( [ 3, 3, 2 ], 1, sh ); +* // returns [ 3, 6 ] +* +* var bool = ( out === sh ); +* // returns true +*/ +function flattenShapeFrom( shape, dim, out ) { + var ndims; + var s; + var i; + var j; + + ndims = shape.length; + s = 1; + j = 0; + for ( i = 0; i < ndims; i++ ) { // e.g., shape=[2,3,4,5], dim=2 => shape=[2,3,20] + if ( i >= dim ) { + s *= shape[ i ]; + if ( i === ndims-1 ) { + out[ j ] = s; + j += 1; + } + } else { + out[ j ] = shape[ i ]; + j += 1; + } + } + return out; +} + + +// EXPORTS // + +module.exports = flattenShapeFrom; diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/index.js new file mode 100644 index 000000000000..b1794a47f2d4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Flatten a shape starting from a specified dimension. +* +* @module @stdlib/ndarray/base/flatten-shape-from +* +* @example +* var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); +* +* var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 ); +* // returns [ 3, 6 ] +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js new file mode 100644 index 000000000000..18790f5bbaa4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 min = require( '@stdlib/math/base/special/fast/min' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var assign = require( './assign.js' ); + + +// MAIN // + +/** +* Flattens a shape starting from a specified dimension. +* +* @param {NonNegativeIntegerArray} shape - array shape +* @param {NonNegativeInteger} dim - dimension to start flattening from +* @returns {NonNegativeIntegerArray} flattened shape +* +* @example +* var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 ); +* // returns [ 3, 6 ] +* +* sh = flattenShapeFrom( [ 3, 2, 1 ], 1 ); +* // returns [ 3, 2 ] +* +* sh = flattenShapeFrom( [ 3 ], 0 ); +* // returns [ 3 ] +* +* sh = flattenShapeFrom( [ 3, 2 ], 0 ); +* // returns [ 6 ] +*/ +function flattenShapeFrom( shape, dim ) { + var ndims; + var out; + var d; + + ndims = shape.length; + d = min( ndims-1, dim ); + out = zeros( d + 1 ); + assign( shape, d, out ); + return out; +} + + +// EXPORTS // + +module.exports = flattenShapeFrom; diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/manifest.json b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/manifest.json new file mode 100644 index 000000000000..04e61e361caa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/manifest.json @@ -0,0 +1,38 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/package.json b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/package.json new file mode 100644 index 000000000000..cc37e0ad3d65 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/ndarray/base/flatten-shape-from", + "version": "0.0.0", + "description": "Flatten a shape starting from a specified dimension.", + "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", + "include": "./include", + "lib": "./lib", + "src": "./src", + "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", + "stdtypes", + "types", + "base", + "ndarray", + "shape", + "flatten", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c new file mode 100644 index 000000000000..f42c9f83d412 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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/ndarray/base/flatten_shape_from.h" +#include + +/** +* Flattens a shape starting from a specified dimension. +* +* @param ndims number of dimensions +* @param shape array shape (dimensions) +* @param dim dimension to start flattening from +* @param out output shape +* @return status code +* +* @example +* #include "stdlib/ndarray/base/flatten_shape_from.h" +* +* const int64_t ndims = 3; +* const int64_t shape[] = { 2, 3, 10 }; +* int64_t out[ 2 ]; +* +* stdlib_ndarray_flatten_shape( ndims, shape, 1, out ); +*/ +int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *shape, const int64_t dim, int64_t *out ) { + int64_t d; + int64_t s; + int64_t i; + int64_t j; + + d = ndims - 1; + if ( dim < d ) { + d = dim; + } + s = 1; + j = 0; + for ( i = 0; i < ndims; i++ ) { // e.g., shape=[2,3,4,5], dim=2 => shape=[2,3,20] + if ( i >= d ) { + s *= shape[ i ]; + if ( i == ndims-1 ) { + out[ j ] = s; + j += 1; + } + } else { + out[ j ] = shape[ i ]; + j += 1; + } + } + return 0; +} diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js new file mode 100644 index 000000000000..c1d906bef74c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js @@ -0,0 +1,141 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isArray = require( '@stdlib/assert/is-array' ); +var flattenShapeFrom = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flattenShapeFrom, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function flattens a provided shape starting from a specified dimension', function test( t ) { + var expected; + var actual; + var shape; + + shape = [ 3, 2 ]; + expected = [ 6 ]; + actual = flattenShapeFrom( shape, 0 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 10 ]; + actual = flattenShapeFrom( shape, 1 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + actual = flattenShapeFrom( shape, 0 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + actual = flattenShapeFrom( shape, 2 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + actual = flattenShapeFrom( shape, 10 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'attached to the main function is an `assign` method which supports flattening a provided shape starting from a specified dimension and assigning results to an output array', function test( t ) { + var expected; + var actual; + var shape; + var out; + + shape = [ 3, 2 ]; + expected = [ 6 ]; + + out = [ 0 ]; + actual = flattenShapeFrom.assign( shape, 0, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 10 ]; + + out = [ 0, 0 ]; + actual = flattenShapeFrom.assign( shape, 1, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + + out = [ 0 ]; + actual = flattenShapeFrom.assign( shape, 0, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + + out = [ 0, 0, 0 ]; + actual = flattenShapeFrom.assign( shape, 2, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + + out = [ 0, 0, 0 ]; + actual = flattenShapeFrom.assign( shape, 10, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); From 2a05e07dd941e39df445af076f1c97a0ae32d53f Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 28 Sep 2025 13:17:14 +0100 Subject: [PATCH 2/4] bench: fix name --- 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: 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: passed - 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 --- --- .../ndarray/base/flatten-shape-from/benchmark/c/benchmark.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c index 87a0ec950fab..f73d6004b8f7 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/benchmark/c/benchmark.c @@ -24,7 +24,7 @@ #include #include -#define NAME "flatten-shape" +#define NAME "flatten-shape-from" #define ITERATIONS 1000000 #define REPEATS 3 From 3a4b6dd59859674a46a0bbfb6a92463b7975d660 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 28 Sep 2025 13:43:13 +0100 Subject: [PATCH 3/4] chore: clean-up and add support for negative dimension indices --- 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: passed - task: lint_javascript_src status: passed - 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 --- --- .../ndarray/base/flatten-shape-from/README.md | 2 +- .../base/flatten-shape-from/docs/repl.txt | 8 +- .../base/flatten-shape-from/lib/assign.js | 23 ++++- .../base/flatten-shape-from/lib/main.js | 31 ++++++- .../base/flatten-shape-from/src/main.c | 10 +- .../base/flatten-shape-from/test/test.js | 92 +++++++++++++++++++ 6 files changed, 155 insertions(+), 11 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md index 11018260b3e4..fda53bbf5998 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md @@ -71,7 +71,7 @@ var bool = ( sh === out ); The function accepts the following parameters: - **shape**: array shape. -- **dim**: dimension to start flattening from. +- **dim**: dimension to start flattening from. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. - **out**: output array. diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt index 5bb631b46e61..2cf12e3ca4fd 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/docs/repl.txt @@ -8,7 +8,9 @@ Array shape. dim: integer - Dimension to start flattening from. + Dimension to start flattening from. If provided an integer less than + zero, the dimension index is resolved relative to the last dimension, + with the last dimension corresponding to the value `-1`. Returns ------- @@ -32,7 +34,9 @@ Array shape. dim: integer - Dimension to start flattening from. + Dimension to start flattening from. If provided an integer less than + zero, the dimension index is resolved relative to the last dimension, + with the last dimension corresponding to the value `-1`. out: Array|TypedArray|Object Output array. diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js index c738f95b2f77..e4758028baf0 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/assign.js @@ -24,9 +24,9 @@ * Flattens a shape starting from a specified dimension and assigns results to a provided output array. * * @param {NonNegativeIntegerArray} shape - array shape -* @param {NonNegativeInteger} dim - dimension to start flattening from -* @param {(Array|TypedArray|Object)} out - output object -* @returns {(Array|TypedArray|Object)} array shape +* @param {integer} dim - dimension to start flattening from +* @param {Collection} out - output object +* @returns {Collection} array shape * * @example * var sh = [ 0, 0 ]; @@ -36,6 +36,15 @@ * * var bool = ( out === sh ); * // returns true +* +* @example +* var sh = [ 0, 0 ]; +* +* var out = flattenShapeFrom( [ 3, 3, 2 ], -2, sh ); +* // returns [ 3, 6 ] +* +* var bool = ( out === sh ); +* // returns true */ function flattenShapeFrom( shape, dim, out ) { var ndims; @@ -44,6 +53,14 @@ function flattenShapeFrom( shape, dim, out ) { var j; ndims = shape.length; + + // Normalize the dimension index... + if ( dim < 0 ) { + dim += ndims; + if ( dim < 0 ) { + dim = 0; + } + } s = 1; j = 0; for ( i = 0; i < ndims; i++ ) { // e.g., shape=[2,3,4,5], dim=2 => shape=[2,3,20] diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js index 18790f5bbaa4..12ae86921911 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/lib/main.js @@ -31,7 +31,7 @@ var assign = require( './assign.js' ); * Flattens a shape starting from a specified dimension. * * @param {NonNegativeIntegerArray} shape - array shape -* @param {NonNegativeInteger} dim - dimension to start flattening from +* @param {integer} dim - dimension to start flattening from * @returns {NonNegativeIntegerArray} flattened shape * * @example @@ -46,6 +46,25 @@ var assign = require( './assign.js' ); * * sh = flattenShapeFrom( [ 3, 2 ], 0 ); * // returns [ 6 ] +* +* sh = flattenShapeFrom( [], 0 ); +* // returns [] +* +* @example +* var sh = flattenShapeFrom( [ 3, 3, 2 ], -2 ); +* // returns [ 3, 6 ] +* +* sh = flattenShapeFrom( [ 3, 2, 1 ], -2 ); +* // returns [ 3, 2 ] +* +* sh = flattenShapeFrom( [ 3 ], -1 ); +* // returns [ 3 ] +* +* sh = flattenShapeFrom( [ 3, 2 ], -2 ); +* // returns [ 6 ] +* +* sh = flattenShapeFrom( [], -1 ); +* // returns [] */ function flattenShapeFrom( shape, dim ) { var ndims; @@ -53,8 +72,16 @@ function flattenShapeFrom( shape, dim ) { var d; ndims = shape.length; + + // Normalize the dimension index... + if ( dim < 0 ) { + dim += ndims; + if ( dim < 0 ) { + dim = 0; + } + } d = min( ndims-1, dim ); - out = zeros( d + 1 ); + out = zeros( d+1 ); assign( shape, d, out ); return out; } diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c index f42c9f83d412..a3b3127fe8ab 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/src/main.c @@ -43,9 +43,13 @@ int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *sh int64_t i; int64_t j; - d = ndims - 1; - if ( dim < d ) { - d = dim; + // Normalize the dimension index... + d = dim; + if ( d < 0 ) { + d += ndims; + if ( d < 0 ) { + d = 0; + } } s = 1; j = 0; diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js index c1d906bef74c..bb06c3f6c91a 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/test/test.js @@ -78,6 +78,47 @@ tape( 'the function flattens a provided shape starting from a specified dimensio t.strictEqual( actual.length, expected.length, 'returns expected length' ); t.deepEqual( actual, expected, 'returns expected value' ); + // Negative dimension indices... + shape = [ 3, 2 ]; + expected = [ 6 ]; + actual = flattenShapeFrom( shape, -2 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 10 ]; + actual = flattenShapeFrom( shape, -2 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + actual = flattenShapeFrom( shape, -3 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + actual = flattenShapeFrom( shape, -10 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + actual = flattenShapeFrom( shape, -1 ); + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected length' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); }); @@ -137,5 +178,56 @@ tape( 'attached to the main function is an `assign` method which supports flatte t.strictEqual( actual.length, expected.length, 'returns expected value' ); t.deepEqual( actual, expected, 'returns expected value' ); + // Negative indices... + shape = [ 3, 2 ]; + expected = [ 6 ]; + + out = [ 0 ]; + actual = flattenShapeFrom.assign( shape, -2, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 10 ]; + + out = [ 0, 0 ]; + actual = flattenShapeFrom.assign( shape, -2, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + + out = [ 0 ]; + actual = flattenShapeFrom.assign( shape, -3, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 20 ]; + + out = [ 0 ]; + actual = flattenShapeFrom.assign( shape, -10, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + shape = [ 2, 1, 10 ]; + expected = [ 2, 1, 10 ]; + + out = [ 0, 0, 0 ]; + actual = flattenShapeFrom.assign( shape, -1, out ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( actual.length, expected.length, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); }); From 095c21d1e694c388302f09f74a0a5bbd24b550d6 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 28 Sep 2025 13:44:12 +0100 Subject: [PATCH 4/4] docs: update examples --- 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: passed - 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 --- --- .../@stdlib/ndarray/base/flatten-shape-from/README.md | 2 +- .../@stdlib/ndarray/base/flatten-shape-from/examples/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md index fda53bbf5998..9a91d06d458b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/README.md @@ -108,7 +108,7 @@ var d2 = discreteUniform( d1.length, 1, 10, opts ); var d3 = discreteUniform( d1.length, 1, 10, opts ); var d4 = discreteUniform( d1.length, 1, 10, opts ); -var dims = discreteUniform( d1.length, 0, 3, opts ); +var dims = discreteUniform( d1.length, -4, 3, opts ); var shapes = zip( [ d1, d2, d3, d4 ] ); logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom ); diff --git a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js index da3acabc3787..f36308479607 100644 --- a/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/flatten-shape-from/examples/index.js @@ -31,7 +31,7 @@ var d2 = discreteUniform( d1.length, 1, 10, opts ); var d3 = discreteUniform( d1.length, 1, 10, opts ); var d4 = discreteUniform( d1.length, 1, 10, opts ); -var dims = discreteUniform( d1.length, 0, 3, opts ); +var dims = discreteUniform( d1.length, -4, 3, opts ); var shapes = zip( [ d1, d2, d3, d4 ] ); logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom );