Skip to content

Commit 93bcefd

Browse files
committed
Add utility to convert an ndarray-like object to an object having a consistent shape
1 parent c215af9 commit 93bcefd

File tree

14 files changed

+1123
-0
lines changed

14 files changed

+1123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# ndarraylike2object
22+
23+
> Convert an [`ndarray`][@stdlib/ndarray/ctor]-like object to an object likely to have the same "shape".
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var ndarraylike2object = require( '@stdlib/ndarray/base/ndarraylike2object' );
41+
```
42+
43+
#### ndarraylike2object( x )
44+
45+
Converts an [`ndarray`][@stdlib/ndarray/ctor]-like object to an object likely to have the same "shape".
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var arr = array( [ [ 1, 2 ], [ 3, 4 ] ] );
51+
var obj = ndarraylike2object( arr );
52+
// returns {...}
53+
```
54+
55+
</section>
56+
57+
<!-- /.usage -->
58+
59+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
60+
61+
<section class="notes">
62+
63+
## Notes
64+
65+
- The returned object has the following properties:
66+
67+
- **ref**: reference to the original [`ndarray`][@stdlib/ndarray/ctor]-like object.
68+
- **dtype**: underlying data type.
69+
- **data**: data buffer.
70+
- **length**: number of elements.
71+
- **shape**: array dimensions.
72+
- **strides**: array strides.
73+
- **offset**: index offset.
74+
- **order**: order.
75+
- **accessors**: `boolean` indicating whether the data buffer uses accessors for getting and setting elements.
76+
- **getter**: accessor for retrieving a data buffer element.
77+
- **setter**: accessor for setting a data buffer element.
78+
79+
- This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to standardize the "shape" of the object holding [`ndarray`][@stdlib/ndarray/ctor] meta data to ensure that internal functions operating on ndarrays are provided consistent argument "shapes".
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var array = require( '@stdlib/ndarray/array' );
95+
var ndarraylike2object = require( '@stdlib/ndarray/base/ndarraylike2object' );
96+
97+
// Create an ndarray:
98+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
99+
100+
// Convert to a standardized object:
101+
var obj = ndarraylike2object( x );
102+
// returns {...}
103+
104+
// Print various properties:
105+
console.log( 'dtype: %s', obj.dtype );
106+
console.log( 'ndims: %d', obj.shape.length );
107+
console.log( 'numel: %d', obj.length );
108+
console.log( 'shape: [ %s ]', obj.shape.join( ', ' ) );
109+
console.log( 'strides: [ %s ]', obj.strides.join( ', ' ) );
110+
console.log( 'offset: %d', obj.offset );
111+
console.log( 'order: %s', obj.order );
112+
console.log( 'accessors: %s', obj.accessors );
113+
```
114+
115+
</section>
116+
117+
<!-- /.examples -->
118+
119+
<!-- 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. -->
120+
121+
<section class="references">
122+
123+
</section>
124+
125+
<!-- /.references -->
126+
127+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128+
129+
<section class="related">
130+
131+
</section>
132+
133+
<!-- /.related -->
134+
135+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="links">
138+
139+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
140+
141+
</section>
142+
143+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 Float64Array = require( '@stdlib/array/float64' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isCollection = require( '@stdlib/assert/is-collection' );
28+
var pkg = require( './../package.json' ).name;
29+
var ndarraylike2object = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+'::base_ndarray', function benchmark( b ) {
35+
var strides;
36+
var values;
37+
var buffer;
38+
var offset;
39+
var dtype;
40+
var shape;
41+
var order;
42+
var out;
43+
var i;
44+
45+
dtype = 'float64';
46+
buffer = new Float64Array( 4 );
47+
shape = [ 2, 2 ];
48+
strides = [ 2, 1 ];
49+
offset = 0;
50+
order = 'row-major';
51+
52+
values = [
53+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
54+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
55+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
56+
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
57+
ndarrayBase( dtype, buffer, shape, strides, offset, order )
58+
];
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = ndarraylike2object( values[ i%values.length ] );
63+
if ( typeof out !== 'object' ) {
64+
b.fail( 'should return an object' );
65+
}
66+
}
67+
b.toc();
68+
if ( !isCollection( out.data ) ) {
69+
b.fail( 'should return a collection' );
70+
}
71+
b.pass( 'benchmark finished' );
72+
b.end();
73+
});
74+
75+
bench( pkg+'::ndarray', function benchmark( b ) {
76+
var strides;
77+
var values;
78+
var buffer;
79+
var offset;
80+
var dtype;
81+
var shape;
82+
var order;
83+
var out;
84+
var i;
85+
86+
dtype = 'float64';
87+
buffer = new Float64Array( 4 );
88+
shape = [ 2, 2 ];
89+
strides = [ 2, 1 ];
90+
offset = 0;
91+
order = 'row-major';
92+
93+
values = [
94+
ndarray( dtype, buffer, shape, strides, offset, order ),
95+
ndarray( dtype, buffer, shape, strides, offset, order ),
96+
ndarray( dtype, buffer, shape, strides, offset, order ),
97+
ndarray( dtype, buffer, shape, strides, offset, order ),
98+
ndarray( dtype, buffer, shape, strides, offset, order )
99+
];
100+
101+
b.tic();
102+
for ( i = 0; i < b.iterations; i++ ) {
103+
out = ndarraylike2object( values[ i%values.length ] );
104+
if ( typeof out !== 'object' ) {
105+
b.fail( 'should return an object' );
106+
}
107+
}
108+
b.toc();
109+
if ( !isCollection( out.data ) ) {
110+
b.fail( 'should return a collection' );
111+
}
112+
b.pass( 'benchmark finished' );
113+
b.end();
114+
});
115+
116+
bench( pkg+'::ndarray_like', function benchmark( b ) {
117+
var strides;
118+
var values;
119+
var buffer;
120+
var offset;
121+
var dtype;
122+
var shape;
123+
var order;
124+
var out;
125+
var obj;
126+
var i;
127+
128+
dtype = 'float64';
129+
buffer = new Float64Array( 4 );
130+
shape = [ 2, 2 ];
131+
strides = [ 2, 1 ];
132+
offset = 0;
133+
order = 'row-major';
134+
135+
values = [];
136+
for ( i = 0; i < 5; i++ ) {
137+
obj = {
138+
'dtype': dtype,
139+
'data': buffer,
140+
'shape': shape,
141+
'strides': strides,
142+
'offset': offset,
143+
'order': order
144+
};
145+
values.push( obj );
146+
}
147+
148+
b.tic();
149+
for ( i = 0; i < b.iterations; i++ ) {
150+
out = ndarraylike2object( values[ i%values.length ] );
151+
if ( typeof out !== 'object' ) {
152+
b.fail( 'should return an object' );
153+
}
154+
}
155+
b.toc();
156+
if ( !isCollection( out.data ) ) {
157+
b.fail( 'should return a collection' );
158+
}
159+
b.pass( 'benchmark finished' );
160+
b.end();
161+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( x )
3+
Converts an ndarray-like to an object likely to have the same "shape".
4+
5+
The returned object has the following properties:
6+
7+
- ref: reference to the original ndarray-like object.
8+
- dtype: underlying data type.
9+
- data: data buffer.
10+
- length: number of elements.
11+
- shape: array dimensions.
12+
- strides: array strides.
13+
- offset: index offset.
14+
- order: order.
15+
- accessors: boolean indicating whether the data buffer uses accessors for
16+
getting and setting elements.
17+
- getter: accessor for retrieving a data buffer element.
18+
- setter: accessor for setting a data buffer element.
19+
20+
Parameters
21+
----------
22+
x: ndarray
23+
Input ndarray.
24+
25+
Returns
26+
-------
27+
out: Object
28+
Object containing ndarray meta data.
29+
30+
Examples
31+
--------
32+
> var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
33+
> var out = {{alias}}( arr )
34+
{...}
35+
36+
See Also
37+
--------
38+

0 commit comments

Comments
 (0)