@stdlib/ndarray-base-broadcast-array-except-dimensions 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +253 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +85 -0
- package/lib/index.js +69 -0
- package/lib/main.js +169 -0
- package/package.json +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
<details>
|
|
23
|
+
<summary>
|
|
24
|
+
About stdlib...
|
|
25
|
+
</summary>
|
|
26
|
+
<p>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.</p>
|
|
27
|
+
<p>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.</p>
|
|
28
|
+
<p>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.</p>
|
|
29
|
+
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
|
|
30
|
+
</details>
|
|
31
|
+
|
|
32
|
+
# broadcastArrayExceptDimensions
|
|
33
|
+
|
|
34
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
|
+
|
|
36
|
+
> Broadcast an input [ndarray][@stdlib/ndarray/base/ctor] to a target shape while keeping a list of specified dimensions unchanged.
|
|
37
|
+
|
|
38
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
39
|
+
|
|
40
|
+
<section class="intro">
|
|
41
|
+
|
|
42
|
+
</section>
|
|
43
|
+
|
|
44
|
+
<!-- /.intro -->
|
|
45
|
+
|
|
46
|
+
<!-- Package usage documentation. -->
|
|
47
|
+
|
|
48
|
+
<section class="installation">
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @stdlib/ndarray-base-broadcast-array-except-dimensions
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
</section>
|
|
57
|
+
|
|
58
|
+
<section class="usage">
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
<!-- eslint-disable id-length -->
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
var broadcastArrayExceptDimensions = require( '@stdlib/ndarray-base-broadcast-array-except-dimensions' );
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
#### broadcastArrayExceptDimensions( arr, shape, dims )
|
|
69
|
+
|
|
70
|
+
Broadcasts an input [ndarray][@stdlib/ndarray/base/ctor] to a target shape while keeping a list of specified dimensions unchanged.
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
var array = require( '@stdlib/ndarray-array' );
|
|
74
|
+
var getShape = require( '@stdlib/ndarray-shape' );
|
|
75
|
+
|
|
76
|
+
// Create a 2x2 ndarray:
|
|
77
|
+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
|
|
78
|
+
// returns <ndarray>
|
|
79
|
+
|
|
80
|
+
// Perform broadcasting:
|
|
81
|
+
var y = broadcastArrayExceptDimensions( x, [ 3, 2, 2 ], [ -1 ] );
|
|
82
|
+
// returns <ndarray>
|
|
83
|
+
|
|
84
|
+
var ysh = getShape( y );
|
|
85
|
+
// returns [ 3, 2, 2 ]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The function accepts the following arguments:
|
|
89
|
+
|
|
90
|
+
- **arr**: input ndarray.
|
|
91
|
+
- **shape**: target shape.
|
|
92
|
+
- **dims**: list of dimensions to exclude from broadcasting. Should be a list of negative integers.
|
|
93
|
+
|
|
94
|
+
</section>
|
|
95
|
+
|
|
96
|
+
<!-- /.usage -->
|
|
97
|
+
|
|
98
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
99
|
+
|
|
100
|
+
<section class="notes">
|
|
101
|
+
|
|
102
|
+
## Notes
|
|
103
|
+
|
|
104
|
+
- The function throws an error if a provided [ndarray][@stdlib/ndarray/base/ctor] is [incompatible][@stdlib/ndarray/base/broadcast-shapes] with a provided shape.
|
|
105
|
+
- The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead.
|
|
106
|
+
- The function always returns a new [ndarray][@stdlib/ndarray/base/ctor] instance even if the input [ndarray][@stdlib/ndarray/base/ctor] shape and the desired shape are the same.
|
|
107
|
+
|
|
108
|
+
</section>
|
|
109
|
+
|
|
110
|
+
<!-- /.notes -->
|
|
111
|
+
|
|
112
|
+
<!-- Package usage examples. -->
|
|
113
|
+
|
|
114
|
+
<section class="examples">
|
|
115
|
+
|
|
116
|
+
## Examples
|
|
117
|
+
|
|
118
|
+
<!-- eslint no-undef: "error" -->
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
var array = require( '@stdlib/ndarray-array' );
|
|
122
|
+
var numel = require( '@stdlib/ndarray-base-numel' );
|
|
123
|
+
var ind2sub = require( '@stdlib/ndarray-ind2sub' );
|
|
124
|
+
var getShape = require( '@stdlib/ndarray-shape' );
|
|
125
|
+
var broadcastArrayExceptDimensions = require( '@stdlib/ndarray-base-broadcast-array-except-dimensions' );
|
|
126
|
+
|
|
127
|
+
// Create a 1x3 array:
|
|
128
|
+
var x = array( [ [ 1, 2, 3 ] ] );
|
|
129
|
+
// returns <ndarray>
|
|
130
|
+
|
|
131
|
+
// Broadcast the array to 2x1x3:
|
|
132
|
+
var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
|
|
133
|
+
// returns <ndarray>
|
|
134
|
+
|
|
135
|
+
// Retrieve the shape:
|
|
136
|
+
var sh = getShape( y );
|
|
137
|
+
// returns [ 2, 1, 3 ]
|
|
138
|
+
|
|
139
|
+
// Retrieve the number of elements:
|
|
140
|
+
var N = numel( sh );
|
|
141
|
+
|
|
142
|
+
// Loop through the array elements...
|
|
143
|
+
var sub;
|
|
144
|
+
var v;
|
|
145
|
+
var i;
|
|
146
|
+
for ( i = 0; i < N; i++ ) {
|
|
147
|
+
v = y.iget( i );
|
|
148
|
+
sub = ind2sub( sh, i );
|
|
149
|
+
console.log( 'Y[%s] = %d', sub.join( ', ' ), v );
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
</section>
|
|
154
|
+
|
|
155
|
+
<!-- /.examples -->
|
|
156
|
+
|
|
157
|
+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
158
|
+
|
|
159
|
+
<section class="references">
|
|
160
|
+
|
|
161
|
+
</section>
|
|
162
|
+
|
|
163
|
+
<!-- /.references -->
|
|
164
|
+
|
|
165
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
166
|
+
|
|
167
|
+
<section class="related">
|
|
168
|
+
|
|
169
|
+
</section>
|
|
170
|
+
|
|
171
|
+
<!-- /.related -->
|
|
172
|
+
|
|
173
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
<section class="main-repo" >
|
|
177
|
+
|
|
178
|
+
* * *
|
|
179
|
+
|
|
180
|
+
## Notice
|
|
181
|
+
|
|
182
|
+
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.
|
|
183
|
+
|
|
184
|
+
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].
|
|
185
|
+
|
|
186
|
+
#### Community
|
|
187
|
+
|
|
188
|
+
[![Chat][chat-image]][chat-url]
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
See [LICENSE][stdlib-license].
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
## Copyright
|
|
198
|
+
|
|
199
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
200
|
+
|
|
201
|
+
</section>
|
|
202
|
+
|
|
203
|
+
<!-- /.stdlib -->
|
|
204
|
+
|
|
205
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
206
|
+
|
|
207
|
+
<section class="links">
|
|
208
|
+
|
|
209
|
+
[npm-image]: http://img.shields.io/npm/v/@stdlib/ndarray-base-broadcast-array-except-dimensions.svg
|
|
210
|
+
[npm-url]: https://npmjs.org/package/@stdlib/ndarray-base-broadcast-array-except-dimensions
|
|
211
|
+
|
|
212
|
+
[test-image]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/actions/workflows/test.yml/badge.svg?branch=v0.1.0
|
|
213
|
+
[test-url]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/actions/workflows/test.yml?query=branch:v0.1.0
|
|
214
|
+
|
|
215
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/ndarray-base-broadcast-array-except-dimensions/main.svg
|
|
216
|
+
[coverage-url]: https://codecov.io/github/stdlib-js/ndarray-base-broadcast-array-except-dimensions?branch=main
|
|
217
|
+
|
|
218
|
+
<!--
|
|
219
|
+
|
|
220
|
+
[dependencies-image]: https://img.shields.io/david/stdlib-js/ndarray-base-broadcast-array-except-dimensions.svg
|
|
221
|
+
[dependencies-url]: https://david-dm.org/stdlib-js/ndarray-base-broadcast-array-except-dimensions/main
|
|
222
|
+
|
|
223
|
+
-->
|
|
224
|
+
|
|
225
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
226
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
227
|
+
|
|
228
|
+
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
229
|
+
|
|
230
|
+
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
231
|
+
|
|
232
|
+
[umd]: https://github.com/umdjs/umd
|
|
233
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
234
|
+
|
|
235
|
+
[deno-url]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/tree/deno
|
|
236
|
+
[deno-readme]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/blob/deno/README.md
|
|
237
|
+
[umd-url]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/tree/umd
|
|
238
|
+
[umd-readme]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/blob/umd/README.md
|
|
239
|
+
[esm-url]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/tree/esm
|
|
240
|
+
[esm-readme]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/blob/esm/README.md
|
|
241
|
+
[branches-url]: https://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/blob/main/branches.md
|
|
242
|
+
|
|
243
|
+
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions/main/LICENSE
|
|
244
|
+
|
|
245
|
+
[@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray-ctor
|
|
246
|
+
|
|
247
|
+
[@stdlib/ndarray/base/ctor]: https://www.npmjs.com/package/@stdlib/ndarray-base-ctor
|
|
248
|
+
|
|
249
|
+
[@stdlib/ndarray/base/broadcast-shapes]: https://www.npmjs.com/package/@stdlib/ndarray-base-broadcast-shapes
|
|
250
|
+
|
|
251
|
+
</section>
|
|
252
|
+
|
|
253
|
+
<!-- /.links -->
|
package/SECURITY.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";var y=function(a,e){return function(){return e||a((e={exports:{}}).exports,e),e.exports}};var w=y(function(k,q){"use strict";var s=require("@stdlib/array-base-copy"),b=require("@stdlib/ndarray-base-ctor"),D=require("@stdlib/ndarray-base-shape"),j=require("@stdlib/ndarray-base-strides"),E=require("@stdlib/ndarray-base-offset"),x=require("@stdlib/ndarray-base-order"),z=require("@stdlib/ndarray-base-dtype"),A=require("@stdlib/ndarray-base-data-buffer"),I=require("@stdlib/ndarray-base-normalize-indices"),p=require("@stdlib/array-base-join"),T=require("@stdlib/array-base-zeros"),c=require("@stdlib/string-format");function O(a,e,f){var o,i,d,t,g,m,u,v,h,r,n,l;if(e=s(e),i=s(f),t=D(a,!1),u=e.length,v=t.length,u<v)throw new Error("invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.");if(o=T(u),m=i.length,i=I(i,v-1),i===null)throw new RangeError(c("invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].",p(f,",")));if(i.sort(),i.length!==m)throw new Error(c("invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].",p(f,",")));for(l=i.length-1,g=j(a,!1),r=u-1;r>=0;r--)if(n=v-u+r,!(n<0)){if(l>=0&&i[l]===n){e[r]=t[n],o[r]=g[n],l-=1;continue}if(h=t[n],d=e[r],d!==0&&d<h)throw new Error(c("invalid argument. Input array cannot be broadcast to the specified shape, as the specified shape has a dimension whose size is less than the size of the corresponding dimension in the input array. Array shape: (%s). Desired shape: (%s). Dimension: %u.",s(t).join(", "),s(e).join(", "),r));if(h===d)o[r]=g[n];else if(h===1)o[r]=0;else throw new Error(c("invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: (%s). Desired shape: (%s). Dimension: %u.",s(t).join(", "),s(e).join(", "),r))}return b(z(a),A(a),e,o,E(a),x(a))}q.exports=O});var S=w();module.exports=S;
|
|
2
|
+
/**
|
|
3
|
+
* @license Apache-2.0
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar copy = require( '@stdlib/array-base-copy' );\nvar ndarray = require( '@stdlib/ndarray-base-ctor' );\nvar getShape = require( '@stdlib/ndarray-base-shape' );\nvar getStrides = require( '@stdlib/ndarray-base-strides' );\nvar getOffset = require( '@stdlib/ndarray-base-offset' );\nvar getOrder = require( '@stdlib/ndarray-base-order' );\nvar getDType = require( '@stdlib/ndarray-base-dtype' );\nvar getData = require( '@stdlib/ndarray-base-data-buffer' );\nvar normalizeIndices = require( '@stdlib/ndarray-base-normalize-indices' );\nvar join = require( '@stdlib/array-base-join' );\nvar zeros = require( '@stdlib/array-base-zeros' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Broadcasts an input ndarray to a target shape while keeping a list of specified dimensions unchanged.\n*\n* ## Notes\n*\n* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension.\n*\n* @param {ndarray} arr - input array\n* @param {NonNegativeIntegerArray} shape - desired shape\n* @param {NegativeIntegerArray} dims - list of dimensions to exclude from broadcasting\n* @throws {Error} input array cannot have more dimensions than the desired shape\n* @throws {Error} broadcasted dimensions in the input array and desired shape must be broadcast compatible\n* @throws {RangeError} dimension indices must not exceed desired shape bounds\n* @throws {Error} must provide unique dimension indices\n* @returns {ndarray} broadcasted array\n*\n* @example\n* var array = require( '@stdlib/ndarray-array' );\n* var getShape = require( '@stdlib/ndarray-shape' );\n*\n* var x = array( [ [ 1, 2, 3 ] ] );\n* // returns <ndarray>\n*\n* var shx = getShape( x );\n* // returns [ 1, 3 ]\n*\n* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );\n* // returns <ndarray>\n*\n* var shy = getShape( y );\n* // returns [ 2, 1, 3 ]\n*\n* var v = y.get( 0, 0, 0 );\n* // returns 1\n*\n* v = y.get( 0, 0, 1 );\n* // returns 2\n*\n* v = y.get( 0, 0, 2 );\n* // returns 3\n*\n* v = y.get( 1, 0, 0 );\n* // returns 1\n*\n* v = y.get( 1, 0, 1 );\n* // returns 2\n*\n* v = y.get( 1, 0, 2 );\n* // returns 3\n*/\nfunction broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-line id-length\n\tvar strides;\n\tvar idx;\n\tvar dim;\n\tvar sh;\n\tvar st;\n\tvar dl;\n\tvar N;\n\tvar M;\n\tvar d;\n\tvar i;\n\tvar j;\n\tvar k;\n\n\t// Copy input arguments to avoid unintended mutation:\n\tshape = copy( shape );\n\tidx = copy( dims );\n\n\t// Resolve the shape of the input array:\n\tsh = getShape( arr, false );\n\n\tN = shape.length;\n\tM = sh.length;\n\tif ( N < M ) {\n\t\tthrow new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' );\n\t}\n\t// Initialize a strides array:\n\tstrides = zeros( N );\n\n\t// Verify that we've been provided a list of unique dimension indices...\n\tdl = idx.length;\n\tidx = normalizeIndices( idx, M-1 );\n\tif ( idx === null ) {\n\t\tthrow new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );\n\t}\n\tidx.sort(); // sort in ascending order\n\tif ( idx.length !== dl ) {\n\t\tthrow new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );\n\t}\n\tk = idx.length - 1;\n\n\t// Determine the output array strides...\n\tst = getStrides( arr, false );\n\tfor ( i = N-1; i >= 0; i-- ) {\n\t\t// Moving from right-to-left, resolve an index into the input array shape:\n\t\tj = M - N + i; // M-1, M-2, M-3, ..., M-M, ..., M-N with N >= M\n\n\t\t// For prepended singleton dimensions, the stride is zero...\n\t\tif ( j < 0 ) {\n\t\t\tcontinue;\n\t\t}\n\t\t// Check for a dimension being held constant...\n\t\tif ( k >= 0 && idx[ k ] === j ) {\n\t\t\tshape[ i ] = sh[ j ];\n\t\t\tstrides[ i ] = st[ j ];\n\t\t\tk -= 1;\n\t\t\tcontinue;\n\t\t}\n\t\t// Broadcast the current dimension...\n\t\td = sh[ j ];\n\t\tdim = shape[ i ];\n\t\tif ( dim !== 0 && dim < d ) {\n\t\t\tthrow new Error( format( 'invalid argument. Input array cannot be broadcast to the specified shape, as the specified shape has a dimension whose size is less than the size of the corresponding dimension in the input array. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );\n\t\t}\n\t\tif ( d === dim ) {\n\t\t\tstrides[ i ] = st[ j ];\n\t\t} else if ( d === 1 ) {\n\t\t\t// In order to broadcast dimensions, we set the stride for that dimension to zero...\n\t\t\tstrides[ i ] = 0;\n\t\t} else {\n\t\t\t// At this point, we know that `dim > d` and that `d` does not equal `1` (e.g., `dim=3` and `d=2`); in which case, the shapes are considered incompatible (even for desired shapes which are multiples of array dimensions, as might be desired when \"tiling\" an array; e.g., `dim=4` and `d=2`)...\n\t\t\tthrow new Error( format( 'invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );\n\t\t}\n\t}\n\treturn ndarray( getDType( arr ), getData( arr ), shape, strides, getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len\n}\n\n\n// EXPORTS //\n\nmodule.exports = broadcastArrayExceptDimensions;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Broadcast an input ndarray to a target shape while keeping a list of specified dimensions unchanged.\n*\n* @module @stdlib/ndarray-base-broadcast-array-except-dimensions\n*\n* @example\n* var array = require( '@stdlib/ndarray-array' );\n* var getShape = require( '@stdlib/ndarray-shape' );\n* var broadcastArrayExceptDimensions = require( '@stdlib/ndarray-base-broadcast-array-except-dimensions' );\n*\n* var x = array( [ [ 1, 2, 3 ] ] );\n* // returns <ndarray>\n*\n* var shx = getShape( x );\n* // returns [ 1, 3 ]\n*\n* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );\n* // returns <ndarray>\n*\n* var shy = getShape( y );\n* // returns [ 2, 1, 3 ]\n*\n* var v = y.get( 0, 0, 0 );\n* // returns 1\n*\n* v = y.get( 0, 0, 1 );\n* // returns 2\n*\n* v = y.get( 0, 0, 2 );\n* // returns 3\n*\n* v = y.get( 1, 0, 0 );\n* // returns 1\n*\n* v = y.get( 1, 0, 1 );\n* // returns 2\n*\n* v = y.get( 1, 0, 2 );\n* // returns 3\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAO,QAAS,yBAA0B,EAC1CC,EAAU,QAAS,2BAA4B,EAC/CC,EAAW,QAAS,4BAA6B,EACjDC,EAAa,QAAS,8BAA+B,EACrDC,EAAY,QAAS,6BAA8B,EACnDC,EAAW,QAAS,4BAA6B,EACjDC,EAAW,QAAS,4BAA6B,EACjDC,EAAU,QAAS,kCAAmC,EACtDC,EAAmB,QAAS,wCAAyC,EACrEC,EAAO,QAAS,yBAA0B,EAC1CC,EAAQ,QAAS,0BAA2B,EAC5CC,EAAS,QAAS,uBAAwB,EAuD9C,SAASC,EAAgCC,EAAKC,EAAOC,EAAO,CAC3D,IAAIC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EACAC,EAWJ,GARAb,EAAQd,EAAMc,CAAM,EACpBG,EAAMjB,EAAMe,CAAK,EAGjBI,EAAKjB,EAAUW,EAAK,EAAM,EAE1BS,EAAIR,EAAM,OACVS,EAAIJ,EAAG,OACFG,EAAIC,EACR,MAAM,IAAI,MAAO,8JAA+J,EAQjL,GALAP,EAAUN,EAAOY,CAAE,EAGnBD,EAAKJ,EAAI,OACTA,EAAMT,EAAkBS,EAAKM,EAAE,CAAE,EAC5BN,IAAQ,KACZ,MAAM,IAAI,WAAYN,EAAQ,2FAA4FF,EAAMM,EAAM,GAAI,CAAE,CAAE,EAG/I,GADAE,EAAI,KAAK,EACJA,EAAI,SAAWI,EACnB,MAAM,IAAI,MAAOV,EAAQ,iGAAkGF,EAAMM,EAAM,GAAI,CAAE,CAAE,EAMhJ,IAJAY,EAAIV,EAAI,OAAS,EAGjBG,EAAKjB,EAAYU,EAAK,EAAM,EACtBY,EAAIH,EAAE,EAAGG,GAAK,EAAGA,IAKtB,GAHAC,EAAIH,EAAID,EAAIG,EAGP,EAAAC,EAAI,GAIT,IAAKC,GAAK,GAAKV,EAAKU,CAAE,IAAMD,EAAI,CAC/BZ,EAAOW,CAAE,EAAIN,EAAIO,CAAE,EACnBV,EAASS,CAAE,EAAIL,EAAIM,CAAE,EACrBC,GAAK,EACL,QACD,CAIA,GAFAH,EAAIL,EAAIO,CAAE,EACVR,EAAMJ,EAAOW,CAAE,EACVP,IAAQ,GAAKA,EAAMM,EACvB,MAAM,IAAI,MAAOb,EAAQ,8PAA+PX,EAAMmB,CAAG,EAAE,KAAM,IAAK,EAAGnB,EAAMc,CAAM,EAAE,KAAM,IAAK,EAAGW,CAAE,CAAE,EAElV,GAAKD,IAAMN,EACVF,EAASS,CAAE,EAAIL,EAAIM,CAAE,UACVF,IAAM,EAEjBR,EAASS,CAAE,EAAI,MAGf,OAAM,IAAI,MAAOd,EAAQ,2IAA4IX,EAAMmB,CAAG,EAAE,KAAM,IAAK,EAAGnB,EAAMc,CAAM,EAAE,KAAM,IAAK,EAAGW,CAAE,CAAE,EAGhO,OAAOxB,EAASK,EAAUO,CAAI,EAAGN,EAASM,CAAI,EAAGC,EAAOE,EAASZ,EAAWS,CAAI,EAAGR,EAAUQ,CAAI,CAAE,CACpG,CAKAd,EAAO,QAAUa,ICzGjB,IAAIgB,EAAO,IAKX,OAAO,QAAUA",
|
|
6
|
+
"names": ["require_main", "__commonJSMin", "exports", "module", "copy", "ndarray", "getShape", "getStrides", "getOffset", "getOrder", "getDType", "getData", "normalizeIndices", "join", "zeros", "format", "broadcastArrayExceptDimensions", "arr", "shape", "dims", "strides", "idx", "dim", "sh", "st", "dl", "N", "M", "d", "i", "j", "k", "main"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="@stdlib/types"/>
|
|
22
|
+
|
|
23
|
+
import { Collection } from '@stdlib/types/array';
|
|
24
|
+
import { typedndarray } from '@stdlib/types/ndarray';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Broadcasts an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
|
|
28
|
+
*
|
|
29
|
+
* ## Notes
|
|
30
|
+
*
|
|
31
|
+
* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension.
|
|
32
|
+
* - The function throws an error if a provided ndarray is incompatible with a provided shape.
|
|
33
|
+
* - The returned array is a view on the input array data buffer. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned array, copy the array before performing operations which may mutate elements.
|
|
34
|
+
* - The returned array is a "base" ndarray, and, thus, the returned array does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead.
|
|
35
|
+
* - The function always returns a new ndarray instance even if the input ndarray shape and the desired shape are the same.
|
|
36
|
+
*
|
|
37
|
+
* @param arr - input array
|
|
38
|
+
* @param shape - desired shape
|
|
39
|
+
* @param dims - list of dimensions to exclude from broadcasting
|
|
40
|
+
* @throws input array cannot have more dimensions than the desired shape
|
|
41
|
+
* @throws broadcasted dimensions in the input array and desired shape must be broadcast compatible
|
|
42
|
+
* @throws dimension indices must not exceed desired shape bounds
|
|
43
|
+
* @throws must provide unique dimension indices
|
|
44
|
+
* @returns broadcasted array
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
48
|
+
* var getShape = require( '@stdlib/ndarray-shape' );
|
|
49
|
+
*
|
|
50
|
+
* var x = array( [ [ 1, 2, 3 ] ] );
|
|
51
|
+
* // returns <ndarray>
|
|
52
|
+
*
|
|
53
|
+
* var shx = getShape( x );
|
|
54
|
+
* // returns [ 1, 3 ]
|
|
55
|
+
*
|
|
56
|
+
* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
|
|
57
|
+
* // returns <ndarray>
|
|
58
|
+
*
|
|
59
|
+
* var shy = getShape( y );
|
|
60
|
+
* // returns [ 2, 1, 3 ]
|
|
61
|
+
*
|
|
62
|
+
* var v = y.get( 0, 0, 0 );
|
|
63
|
+
* // returns 1
|
|
64
|
+
*
|
|
65
|
+
* v = y.get( 0, 0, 1 );
|
|
66
|
+
* // returns 2
|
|
67
|
+
*
|
|
68
|
+
* v = y.get( 0, 0, 2 );
|
|
69
|
+
* // returns 3
|
|
70
|
+
*
|
|
71
|
+
* v = y.get( 1, 0, 0 );
|
|
72
|
+
* // returns 1
|
|
73
|
+
*
|
|
74
|
+
* v = y.get( 1, 0, 1 );
|
|
75
|
+
* // returns 2
|
|
76
|
+
*
|
|
77
|
+
* v = y.get( 1, 0, 2 );
|
|
78
|
+
* // returns 3
|
|
79
|
+
*/
|
|
80
|
+
declare function broadcastArrayExceptDimensions<T = unknown, U extends typedndarray<T> = typedndarray<T>>( arr: U, shape: Collection<number>, dims: Collection<number> ): U;
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
// EXPORTS //
|
|
84
|
+
|
|
85
|
+
export = broadcastArrayExceptDimensions;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Broadcast an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/ndarray-base-broadcast-array-except-dimensions
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
28
|
+
* var getShape = require( '@stdlib/ndarray-shape' );
|
|
29
|
+
* var broadcastArrayExceptDimensions = require( '@stdlib/ndarray-base-broadcast-array-except-dimensions' );
|
|
30
|
+
*
|
|
31
|
+
* var x = array( [ [ 1, 2, 3 ] ] );
|
|
32
|
+
* // returns <ndarray>
|
|
33
|
+
*
|
|
34
|
+
* var shx = getShape( x );
|
|
35
|
+
* // returns [ 1, 3 ]
|
|
36
|
+
*
|
|
37
|
+
* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
|
|
38
|
+
* // returns <ndarray>
|
|
39
|
+
*
|
|
40
|
+
* var shy = getShape( y );
|
|
41
|
+
* // returns [ 2, 1, 3 ]
|
|
42
|
+
*
|
|
43
|
+
* var v = y.get( 0, 0, 0 );
|
|
44
|
+
* // returns 1
|
|
45
|
+
*
|
|
46
|
+
* v = y.get( 0, 0, 1 );
|
|
47
|
+
* // returns 2
|
|
48
|
+
*
|
|
49
|
+
* v = y.get( 0, 0, 2 );
|
|
50
|
+
* // returns 3
|
|
51
|
+
*
|
|
52
|
+
* v = y.get( 1, 0, 0 );
|
|
53
|
+
* // returns 1
|
|
54
|
+
*
|
|
55
|
+
* v = y.get( 1, 0, 1 );
|
|
56
|
+
* // returns 2
|
|
57
|
+
*
|
|
58
|
+
* v = y.get( 1, 0, 2 );
|
|
59
|
+
* // returns 3
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
// MODULES //
|
|
63
|
+
|
|
64
|
+
var main = require( './main.js' );
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
// EXPORTS //
|
|
68
|
+
|
|
69
|
+
module.exports = main;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var copy = require( '@stdlib/array-base-copy' );
|
|
24
|
+
var ndarray = require( '@stdlib/ndarray-base-ctor' );
|
|
25
|
+
var getShape = require( '@stdlib/ndarray-base-shape' );
|
|
26
|
+
var getStrides = require( '@stdlib/ndarray-base-strides' );
|
|
27
|
+
var getOffset = require( '@stdlib/ndarray-base-offset' );
|
|
28
|
+
var getOrder = require( '@stdlib/ndarray-base-order' );
|
|
29
|
+
var getDType = require( '@stdlib/ndarray-base-dtype' );
|
|
30
|
+
var getData = require( '@stdlib/ndarray-base-data-buffer' );
|
|
31
|
+
var normalizeIndices = require( '@stdlib/ndarray-base-normalize-indices' );
|
|
32
|
+
var join = require( '@stdlib/array-base-join' );
|
|
33
|
+
var zeros = require( '@stdlib/array-base-zeros' );
|
|
34
|
+
var format = require( '@stdlib/string-format' );
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// MAIN //
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Broadcasts an input ndarray to a target shape while keeping a list of specified dimensions unchanged.
|
|
41
|
+
*
|
|
42
|
+
* ## Notes
|
|
43
|
+
*
|
|
44
|
+
* - The function expects that each index in the list of dimensions is negative in order to ensure that indices correspond to the same relative position in the output ndarray shape. For example, given an input ndarray shape `[2,X1,X2]` and a desired shape `[6,7,2,Y1,Y2]`, a list of negative dimensions `[-2,-1]` correctly maps the unchanged dimensions `X` in the input ndarray to ignored dimensions `Y` in the provided target shape. Nonnegative indices, however, afford no such mapping. For example, the list of dimensions `[1,2]` corresponds to `[X1,X2]` in the input ndarray shape, but to `[7,2]` in the target shape, which is not desired. By expecting negative indices, we avoid confusion and ensure that users always refer to dimensions relative to the last broadcasted dimension.
|
|
45
|
+
*
|
|
46
|
+
* @param {ndarray} arr - input array
|
|
47
|
+
* @param {NonNegativeIntegerArray} shape - desired shape
|
|
48
|
+
* @param {NegativeIntegerArray} dims - list of dimensions to exclude from broadcasting
|
|
49
|
+
* @throws {Error} input array cannot have more dimensions than the desired shape
|
|
50
|
+
* @throws {Error} broadcasted dimensions in the input array and desired shape must be broadcast compatible
|
|
51
|
+
* @throws {RangeError} dimension indices must not exceed desired shape bounds
|
|
52
|
+
* @throws {Error} must provide unique dimension indices
|
|
53
|
+
* @returns {ndarray} broadcasted array
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* var array = require( '@stdlib/ndarray-array' );
|
|
57
|
+
* var getShape = require( '@stdlib/ndarray-shape' );
|
|
58
|
+
*
|
|
59
|
+
* var x = array( [ [ 1, 2, 3 ] ] );
|
|
60
|
+
* // returns <ndarray>
|
|
61
|
+
*
|
|
62
|
+
* var shx = getShape( x );
|
|
63
|
+
* // returns [ 1, 3 ]
|
|
64
|
+
*
|
|
65
|
+
* var y = broadcastArrayExceptDimensions( x, [ 2, 2, 3 ], [ -2 ] );
|
|
66
|
+
* // returns <ndarray>
|
|
67
|
+
*
|
|
68
|
+
* var shy = getShape( y );
|
|
69
|
+
* // returns [ 2, 1, 3 ]
|
|
70
|
+
*
|
|
71
|
+
* var v = y.get( 0, 0, 0 );
|
|
72
|
+
* // returns 1
|
|
73
|
+
*
|
|
74
|
+
* v = y.get( 0, 0, 1 );
|
|
75
|
+
* // returns 2
|
|
76
|
+
*
|
|
77
|
+
* v = y.get( 0, 0, 2 );
|
|
78
|
+
* // returns 3
|
|
79
|
+
*
|
|
80
|
+
* v = y.get( 1, 0, 0 );
|
|
81
|
+
* // returns 1
|
|
82
|
+
*
|
|
83
|
+
* v = y.get( 1, 0, 1 );
|
|
84
|
+
* // returns 2
|
|
85
|
+
*
|
|
86
|
+
* v = y.get( 1, 0, 2 );
|
|
87
|
+
* // returns 3
|
|
88
|
+
*/
|
|
89
|
+
function broadcastArrayExceptDimensions( arr, shape, dims ) { // eslint-disable-line id-length
|
|
90
|
+
var strides;
|
|
91
|
+
var idx;
|
|
92
|
+
var dim;
|
|
93
|
+
var sh;
|
|
94
|
+
var st;
|
|
95
|
+
var dl;
|
|
96
|
+
var N;
|
|
97
|
+
var M;
|
|
98
|
+
var d;
|
|
99
|
+
var i;
|
|
100
|
+
var j;
|
|
101
|
+
var k;
|
|
102
|
+
|
|
103
|
+
// Copy input arguments to avoid unintended mutation:
|
|
104
|
+
shape = copy( shape );
|
|
105
|
+
idx = copy( dims );
|
|
106
|
+
|
|
107
|
+
// Resolve the shape of the input array:
|
|
108
|
+
sh = getShape( arr, false );
|
|
109
|
+
|
|
110
|
+
N = shape.length;
|
|
111
|
+
M = sh.length;
|
|
112
|
+
if ( N < M ) {
|
|
113
|
+
throw new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' );
|
|
114
|
+
}
|
|
115
|
+
// Initialize a strides array:
|
|
116
|
+
strides = zeros( N );
|
|
117
|
+
|
|
118
|
+
// Verify that we've been provided a list of unique dimension indices...
|
|
119
|
+
dl = idx.length;
|
|
120
|
+
idx = normalizeIndices( idx, M-1 );
|
|
121
|
+
if ( idx === null ) {
|
|
122
|
+
throw new RangeError( format( 'invalid argument. Third argument contains an out-of-bounds dimension index. Value: [%s].', join( dims, ',' ) ) );
|
|
123
|
+
}
|
|
124
|
+
idx.sort(); // sort in ascending order
|
|
125
|
+
if ( idx.length !== dl ) {
|
|
126
|
+
throw new Error( format( 'invalid argument. Third argument must contain a list of unique dimension indices. Value: [%s].', join( dims, ',' ) ) );
|
|
127
|
+
}
|
|
128
|
+
k = idx.length - 1;
|
|
129
|
+
|
|
130
|
+
// Determine the output array strides...
|
|
131
|
+
st = getStrides( arr, false );
|
|
132
|
+
for ( i = N-1; i >= 0; i-- ) {
|
|
133
|
+
// Moving from right-to-left, resolve an index into the input array shape:
|
|
134
|
+
j = M - N + i; // M-1, M-2, M-3, ..., M-M, ..., M-N with N >= M
|
|
135
|
+
|
|
136
|
+
// For prepended singleton dimensions, the stride is zero...
|
|
137
|
+
if ( j < 0 ) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
// Check for a dimension being held constant...
|
|
141
|
+
if ( k >= 0 && idx[ k ] === j ) {
|
|
142
|
+
shape[ i ] = sh[ j ];
|
|
143
|
+
strides[ i ] = st[ j ];
|
|
144
|
+
k -= 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
// Broadcast the current dimension...
|
|
148
|
+
d = sh[ j ];
|
|
149
|
+
dim = shape[ i ];
|
|
150
|
+
if ( dim !== 0 && dim < d ) {
|
|
151
|
+
throw new Error( format( 'invalid argument. Input array cannot be broadcast to the specified shape, as the specified shape has a dimension whose size is less than the size of the corresponding dimension in the input array. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );
|
|
152
|
+
}
|
|
153
|
+
if ( d === dim ) {
|
|
154
|
+
strides[ i ] = st[ j ];
|
|
155
|
+
} else if ( d === 1 ) {
|
|
156
|
+
// In order to broadcast dimensions, we set the stride for that dimension to zero...
|
|
157
|
+
strides[ i ] = 0;
|
|
158
|
+
} else {
|
|
159
|
+
// At this point, we know that `dim > d` and that `d` does not equal `1` (e.g., `dim=3` and `d=2`); in which case, the shapes are considered incompatible (even for desired shapes which are multiples of array dimensions, as might be desired when "tiling" an array; e.g., `dim=4` and `d=2`)...
|
|
160
|
+
throw new Error( format( 'invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: (%s). Desired shape: (%s). Dimension: %u.', copy( sh ).join( ', ' ), copy( shape ).join( ', ' ), i ) );
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return ndarray( getDType( arr ), getData( arr ), shape, strides, getOffset( arr ), getOrder( arr ) ); // eslint-disable-line max-len
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
// EXPORTS //
|
|
168
|
+
|
|
169
|
+
module.exports = broadcastArrayExceptDimensions;
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/ndarray-base-broadcast-array-except-dimensions",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Broadcast an input ndarray to a target shape while keeping a list of specified dimensions unchanged.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"doc": "./docs",
|
|
19
|
+
"lib": "./lib",
|
|
20
|
+
"dist": "./dist"
|
|
21
|
+
},
|
|
22
|
+
"types": "./docs/types",
|
|
23
|
+
"scripts": {},
|
|
24
|
+
"homepage": "https://stdlib.io",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git://github.com/stdlib-js/ndarray-base-broadcast-array-except-dimensions.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@stdlib/array-base-copy": "^0.2.2",
|
|
34
|
+
"@stdlib/array-base-join": "^0.1.1",
|
|
35
|
+
"@stdlib/array-base-zeros": "^0.2.2",
|
|
36
|
+
"@stdlib/ndarray-base-ctor": "^0.2.2",
|
|
37
|
+
"@stdlib/ndarray-base-data-buffer": "^0.2.2",
|
|
38
|
+
"@stdlib/ndarray-base-dtype": "^0.2.2",
|
|
39
|
+
"@stdlib/ndarray-base-normalize-indices": "^0.1.0",
|
|
40
|
+
"@stdlib/ndarray-base-offset": "^0.2.2",
|
|
41
|
+
"@stdlib/ndarray-base-order": "^0.2.2",
|
|
42
|
+
"@stdlib/ndarray-base-shape": "^0.2.2",
|
|
43
|
+
"@stdlib/ndarray-base-strides": "^0.2.2",
|
|
44
|
+
"@stdlib/string-format": "^0.2.2",
|
|
45
|
+
"@stdlib/error-tools-fmtprodmsg": "^0.2.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=0.10.0",
|
|
50
|
+
"npm": ">2.7.0"
|
|
51
|
+
},
|
|
52
|
+
"os": [
|
|
53
|
+
"aix",
|
|
54
|
+
"darwin",
|
|
55
|
+
"freebsd",
|
|
56
|
+
"linux",
|
|
57
|
+
"macos",
|
|
58
|
+
"openbsd",
|
|
59
|
+
"sunos",
|
|
60
|
+
"win32",
|
|
61
|
+
"windows"
|
|
62
|
+
],
|
|
63
|
+
"keywords": [
|
|
64
|
+
"stdlib",
|
|
65
|
+
"stdtypes",
|
|
66
|
+
"types",
|
|
67
|
+
"base",
|
|
68
|
+
"ndarray",
|
|
69
|
+
"broadcast",
|
|
70
|
+
"broadcasting",
|
|
71
|
+
"reshape",
|
|
72
|
+
"multidimensional",
|
|
73
|
+
"array",
|
|
74
|
+
"utilities",
|
|
75
|
+
"utility",
|
|
76
|
+
"utils",
|
|
77
|
+
"util"
|
|
78
|
+
],
|
|
79
|
+
"funding": {
|
|
80
|
+
"type": "opencollective",
|
|
81
|
+
"url": "https://opencollective.com/stdlib"
|
|
82
|
+
}
|
|
83
|
+
}
|