@stdlib/ndarray-base-broadcast-shapes

Broadcast array shapes to a single shape.

https://github.com/stdlib-js/ndarray-base-broadcast-shapes

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.4%) to scientific vocabulary

Keywords

array base broadcast broadcasting javascript multidimensional ndarray node node-js nodejs stdlib types util utilities utility utils
Last synced: 6 months ago · JSON representation ·

Repository

Broadcast array shapes to a single shape.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
array base broadcast broadcasting javascript multidimensional ndarray node node-js nodejs stdlib types util utilities utility utils
Created over 4 years ago · Last pushed 6 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

broadcastShapes

NPM version Build Status Coverage Status <!-- dependencies -->

Broadcast array shapes to a single shape.

## Installation ```bash npm install @stdlib/ndarray-base-broadcast-shapes ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships. To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage ```javascript var broadcastShapes = require( '@stdlib/ndarray-base-broadcast-shapes' ); ``` #### broadcastShapes( shapes ) Broadcasts array shapes to a single shape. ```javascript var sh1 = [ 8, 1, 6, 1 ]; var sh2 = [ 7, 1, 5 ]; var sh = broadcastShapes( [ sh1, sh2 ] ); // returns [ 8, 7, 6, 5 ] ```
## Notes - When operating on two arrays, the function compares their shapes element-wise, beginning with the trailing (i.e., rightmost) dimension. The following are examples of compatible shapes and their corresponding broadcasted shape: ```text A (4d array): 8 x 1 x 6 x 1 B (3d array): 7 x 1 x 5 --------------------------------- Result (4d array): 8 x 7 x 6 x 5 A (2d array): 5 x 4 B (1d array): 1 ------------------------- Result (2d array): 5 x 4 A (2d array): 5 x 4 B (1d array): 4 ------------------------- Result (2d array): 5 x 4 A (3d array): 15 x 3 x 5 B (3d array): 15 x 1 x 5 ------------------------------ Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 5 ------------------------------ Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5 B (2d array): 3 x 1 ------------------------------ Result (3d array): 15 x 3 x 5 A (5d array): 8 x 1 x 1 x 6 x 1 B (4d array): 1 x 7 x 1 x 5 C (5d array): 8 x 4 x 1 x 6 x 5 ------------------------------------- Result (5d array): 8 x 4 x 7 x 6 x 5 A (5d array): 8 x 1 x 1 x 6 x 1 B (1d array): 0 ------------------------------------- Result (5d array): 8 x 1 x 1 x 6 x 0 A (5d array): 8 x 0 x 1 x 6 x 1 B (2d array): 6 x 5 ------------------------------------- Result (5d array): 8 x 0 x 1 x 6 x 5 A (5d array): 8 x 1 x 1 x 6 x 1 B (5d array): 8 x 0 x 1 x 6 x 1 ------------------------------------- Result (5d array): 8 x 0 x 1 x 6 x 1 A (3d array): 3 x 2 x 1 B (0d array): ----------------------------- Result (3d array): 3 x 2 x 1 A (0d array): B (3d array): 3 x 2 x 1 ----------------------------- Result (3d array): 3 x 2 x 1 ``` As demonstrated above, arrays are not required to have the same number of dimensions in order to be broadcast compatible. Array shapes with fewer dimensions are implicitly prepended with singleton dimensions (i.e., dimensions equal to `1`). Accordingly, the following example ```text A (2d array): 5 x 4 B (1d array): 4 ------------------------- Result (2d array): 5 x 4 ``` is equivalent to ```text A (2d array): 5 x 4 B (2d array): 1 x 4 ------------------------- Result (2d array): 5 x 4 ``` Similarly, a zero-dimensional array is expanded (by prepending singleton dimensions) from ```text A (3d array): 3 x 2 x 1 B (0d array): ----------------------------- Result (3d array): 3 x 2 x 1 ``` to ```text A (3d array): 3 x 2 x 1 B (3d array): 1 x 1 x 1 ----------------------------- Result (3d array): 3 x 2 x 1 ``` Stated otherwise, every array has implicit leading dimensions of size `1`. During broadcasting, a `3 x 4` matrix is the same as a `3 x 4 x 1 x 1 x 1` multidimensional array. - Two respective dimensions in two shape arrays are compatible if 1. the dimensions are equal. 2. one dimension is `1`. The two aforementioned rules apply to empty arrays or arrays that have a dimension of size `0`. For unequal dimensions, the size of the dimension which is not `1` determines the size of the output shape dimension. Accordingly, dimensions of size `0` must be paired with a dimension of size `0` or `1`. In such cases, by the rules above, the size of the corresponding output shape dimension is `0`. - The function returns `null` if provided incompatible shapes (i.e., shapes which cannot be broadcast with one another). ```javascript var sh1 = [ 3, 2 ]; var sh2 = [ 2, 3 ]; var sh = broadcastShapes( [ sh1, sh2 ] ); // returns null ``` The following are examples of array shapes which are **not** compatible and do **not** broadcast: ```text A (1d array): 3 B (1d array): 4 # dimension does not match A (2d array): 2 x 1 B (3d array): 8 x 4 x 3 # second dimension does not match A (3d array): 15 x 3 x 5 B (2d array): 15 x 3 # singleton dimensions can only be prepended, not appended A (5d array): 8 x 8 x 1 x 6 x 1 B (5d array): 8 x 0 x 1 x 6 x 1 # second dimension does not match ```
## Examples ```javascript var lpad = require( '@stdlib/string-left-pad' ); var broadcastShapes = require( '@stdlib/ndarray-base-broadcast-shapes' ); var shapes; var out; var sh; var i; var j; function shape2string( shape ) { return lpad( shape.join( ' x ' ), 20, ' ' ); } shapes = [ [ [ 1, 2 ], [ 2 ] ], [ [ 1, 1 ], [ 3, 4 ] ], [ [ 6, 7 ], [ 5, 6, 1 ], [ 7 ], [ 5, 1, 7 ] ], [ [ 1, 3 ], [ 3, 1 ] ], [ [ 1 ], [ 3 ] ], [ [ 2 ], [ 3, 2 ] ], [ [ 2, 3 ], [ 2, 3 ], [ 2, 3 ], [ 2, 3 ] ], [ [ 1, 2 ], [ 1, 2 ] ] ]; for ( i = 0; i < shapes.length; i++ ) { sh = shapes[ i ]; for ( j = 0; j < sh.length; j++ ) { console.log( shape2string( sh[ j ] ) ); } console.log( lpad( '', 20, '-' ) ); out = broadcastShapes( sh ); console.log( shape2string( out )+'\n' ); } ```

## C APIs
### Usage ```c #include "stdlib/ndarray/base/broadcast_shapes.h" ``` #### stdlib_ndarray_broadcast_shapes( M, \*\*shapes, \*ndims, \*out ) Broadcasts array shapes to a single shape. ```c #include "stdlib/ndarray/base/broadcast_shapes.h" #include int64_t N1 = 4; int64_t sh1[] = { 8, 1, 6, 1 }; int64_t N2 = 3; int64_t sh2[] = { 7, 1, 5 }; int64_t ndims[] = { N1, N2 }; int64_t *shapes[] = { sh1, sh2 }; int64_t out[] = { 0, 0, 0, 0 }; int8_t status = stdlib_ndarray_broadcast_shapes( 2, shapes, ndims, out ); if ( status != 0 ) { // Handle error... } ``` The function accepts the following arguments: - **M**: `[in] int64_t` number of shape arrays. - **shapes**: `[in] int64_t**` array of shape arrays (dimensions). - **ndims**: `[in] int64_t*` number of dimensions for each respective shape array. - **out**: `[out] int64_t*` output shape array. ```c int8_t stdlib_ndarray_broadcast_shapes( const int64_t M, const int64_t *shapes[], const int64_t ndims[], int64_t *out ); ``` If successful, the function returns `0`; otherwise, the function returns `-1` (e.g., due to incompatible shapes).
### Notes - Even if the function is unsuccessful, the function may still overwrite elements in the output array before returning. In other words, do not assume that providing incompatible shapes is a no-op with regard to the output array.
### Examples ```c #include "stdlib/ndarray/base/broadcast_shapes.h" #include #include #include int main( void ) { int64_t N1 = 4; int64_t sh1[] = { 8, 1, 6, 1 }; int64_t N2 = 3; int64_t sh2[] = { 7, 1, 5 }; int64_t ndims[] = { N1, N2 }; int64_t *shapes[] = { sh1, sh2 }; int64_t out[] = { 0, 0, 0, 0 }; int8_t status = stdlib_ndarray_broadcast_shapes( 2, shapes, ndims, out ); if ( status != 0 ) { printf( "incompatible shapes\n" ); return 1; } int64_t i; printf( "shape = ( " ); for ( i = 0; i < N1; i++ ) { printf( "%"PRId64"", out[ i ] ); if ( i < N1-1 ) { printf( ", " ); } } printf( " )\n" ); return 0; } ```

* * * ## Notice This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

Citation (CITATION.cff)

cff-version: 1.2.0
title: stdlib
message: >-
  If you use this software, please cite it using the
  metadata from this file.

type: software

authors:
  - name: The Stdlib Authors
    url: https://github.com/stdlib-js/stdlib/graphs/contributors

repository-code: https://github.com/stdlib-js/stdlib
url: https://stdlib.io

abstract: |
  Standard library for JavaScript and Node.js.

keywords:
  - JavaScript
  - Node.js
  - TypeScript
  - standard library
  - scientific computing
  - numerical computing
  - statistical computing

license: Apache-2.0 AND BSL-1.0

date-released: 2016

GitHub Events

Total
  • Push event: 40
Last Year
  • Push event: 40

Committers

Last synced: 11 months ago

All Time
  • Total Commits: 68
  • Total Committers: 1
  • Avg Commits per committer: 68.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 18
  • Committers: 1
  • Avg Commits per committer: 18.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 68
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 33 last-month
  • Total dependent packages: 5
  • Total dependent repositories: 3
  • Total versions: 13
  • Total maintainers: 4
npmjs.org: @stdlib/ndarray-base-broadcast-shapes

Broadcast array shapes to a single shape.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.2
    published over 1 year ago
  • Versions: 13
  • Dependent Packages: 5
  • Dependent Repositories: 3
  • Downloads: 33 Last month
Rankings
Dependent packages count: 3.7%
Dependent repos count: 6.4%
Average: 10.6%
Downloads: 10.9%
Forks count: 15.4%
Stargazers count: 16.7%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 6 months ago

Dependencies

package.json npm
  • @stdlib/bench ^0.0.x development
  • @stdlib/string-left-pad ^0.0.x development
  • istanbul ^0.4.1 development
  • tap-spec 5.x.x development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/types ^0.0.x
  • @stdlib/utils-library-manifest ^0.0.x
.github/workflows/benchmark.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request v3 composite
.github/workflows/examples.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • actions/upload-artifact v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/productionize.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • JS-DevTools/npm-publish v1 composite
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/test.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/test_bundles.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • denoland/setup-deno v1 composite
.github/workflows/test_coverage.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • codecov/codecov-action v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/test_install.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite