Skip to content

Commit 36f3f0c

Browse files
committed
feat: add ndarray/base/zip2views1d
--- 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: 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent c8567dc commit 36f3f0c

File tree

11 files changed

+1034
-0
lines changed

11 files changed

+1034
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# zip2views1d
22+
23+
> Zip one or more one-dimensional ndarrays to an array of composite views.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var zip2views1d = require( '@stdlib/ndarray/base/zip2views1d' );
31+
```
32+
33+
#### zip2views1d( arrays, labels )
34+
35+
Zips one or more one-dimensional ndarrays to an array of composite views.
36+
37+
```javascript
38+
var array2ndarray = require( '@stdlib/ndarray/base/from-array' );
39+
40+
var x = array2ndarray( [ 1, 2, 3 ], 'row-major' );
41+
var y = array2ndarray( [ 'a', 'b', 'c' ], 'row-major' );
42+
43+
var labels = [ 'x', 'y' ];
44+
45+
var z = zip2views1d( [ x, y ], labels );
46+
// returns [ <Object>, <Object>, <Object> ]
47+
48+
var v0 = z[ 0 ].toJSON();
49+
// returns { 'x': 1, 'y': 'a' }
50+
51+
var v1 = z[ 1 ].toJSON();
52+
// returns { 'x': 2, 'y': 'b' }
53+
54+
var v2 = z[ 2 ].toJSON();
55+
// returns { 'x': 3, 'y': 'c' }
56+
57+
// Mutate one of the input arrays:
58+
x.set( 0, 5 );
59+
60+
v0 = z[ 0 ].toJSON();
61+
// returns { 'x': 5, 'y': 'a' }
62+
63+
// Set a view property:
64+
z[ 1 ].y = 'beep';
65+
66+
v1 = z[ 1 ].toJSON();
67+
// returns { 'x': 2, 'y': 'beep' }
68+
69+
var v = y.get( 1 );
70+
// returns 'beep'
71+
```
72+
73+
The function supports the following parameters:
74+
75+
- **arrays**: list of ndarrays to zip.
76+
- **labels**: list of labels.
77+
78+
Each element in the returned array is a class instance having prototype properties corresponding to the list of labels. As demonstrated in the example above, to convert an element to a regular object, invoke an element's `toJSON` method. Note, however, that the object returned by an element's `toJSON` method no longer shares the same memory as the provided input ndarrays.
79+
80+
</section>
81+
82+
<!-- /.usage -->
83+
84+
<section class="notes">
85+
86+
- The function assumes that the list of ndarrays to be zipped all have the same length.
87+
- The list of provided labels should equal the number of ndarrays to be zipped.
88+
- Each view in the returned array shares the same memory as the corresponding elements in the input ndarrays. Accordingly, mutation of either an input ndarray or a view will mutate the other.
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<section class="examples">
95+
96+
## Examples
97+
98+
<!-- eslint no-undef: "error" -->
99+
100+
```javascript
101+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
102+
var zeroTo = require( '@stdlib/array/base/zero-to' );
103+
var array2ndarray = require( '@stdlib/ndarray/base/from-array' );
104+
var zip2views1d = require( '@stdlib/ndarray/base/zip2views1d' );
105+
106+
var x = array2ndarray( zeroTo( 10 ), 'row-major' );
107+
var y = array2ndarray( discreteUniform( x.length, -100, 100 ), 'row-major' );
108+
109+
var labels = [ 'x', 'y' ];
110+
111+
var out = zip2views1d( [ x, y ], labels );
112+
// returns [...]
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
</section>
124+
125+
<!-- /.related -->
126+
127+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="links">
130+
131+
</section>
132+
133+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var array2ndarray = require( '@stdlib/ndarray/base/from-array' );
27+
var pkg = require( './../package.json' ).name;
28+
var zip2views1d = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':len=10', function benchmark( b ) {
34+
var labels;
35+
var x;
36+
var i;
37+
var v;
38+
39+
labels = [ 'x', 'y' ];
40+
x = array2ndarray( zeroTo( 10 ), 'row-major' );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = zip2views1d( [ x, x ], labels );
45+
if ( typeof v !== 'object' ) {
46+
b.fail( 'should return an array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isArray( v ) ) {
51+
b.fail( 'should return an array' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var array2ndarray = require( '@stdlib/ndarray/base/from-array' );
28+
var pkg = require( './../package.json' ).name;
29+
var zip2views1d = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} len - array length
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( len ) {
42+
var labels = [ 'x', 'y' ];
43+
var x = array2ndarray( zeroTo( len ), 'row-major' );
44+
return benchmark;
45+
46+
/**
47+
* Benchmark function.
48+
*
49+
* @private
50+
* @param {Benchmark} b - benchmark instance
51+
*/
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = zip2views1d( [ x, x ], labels );
59+
if ( typeof v !== 'object' ) {
60+
b.fail( 'should return an array' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( v ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( pkg+':len='+len, f );
94+
}
95+
}
96+
97+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays, labels )
3+
Zips one or more one-dimensional ndarrays to an array of composite views.
4+
5+
The function assumes that the list of ndarrays to be zipped all have the
6+
same length.
7+
8+
The list of provided labels should equal the number of ndarrays to be
9+
zipped.
10+
11+
Each view in the returned array shares the same memory as the corresponding
12+
elements in the input ndarrays. Accordingly, mutation of either an input
13+
ndarray or a view will mutate the other.
14+
15+
Parameters
16+
----------
17+
arrays: ArrayLikeObject<ndarray>
18+
List of ndarrays to zip.
19+
20+
labels: ArrayLikeObject
21+
List of labels.
22+
23+
Returns
24+
-------
25+
out: Array<Object>
26+
Output array.
27+
28+
Examples
29+
--------
30+
> var x = {{alias:@stdlib/ndarray/array}}( [ 1, 2 ] );
31+
> var y = {{alias:@stdlib/ndarray/array}}( [ 3, 4 ] );
32+
> var labels = [ 'x', 'y' ];
33+
> var out = {{alias}}( [ x, y ], labels );
34+
> var v = out[ 0 ].toJSON()
35+
{ 'x': 1, 'y': 3 }
36+
> v = out[ 1 ].toJSON()
37+
{ 'x': 2, 'y': 4 }
38+
39+
See Also
40+
--------
41+

0 commit comments

Comments
 (0)