Skip to content

Commit e7b56b1

Browse files
committed
feat: add ndarray/ndarraylike2ndarray
1 parent 453c7f9 commit e7b56b1

File tree

10 files changed

+1049
-0
lines changed

10 files changed

+1049
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# ndarraylike2ndarray
22+
23+
> Convert an ndarray-like object to an [`ndarray`][@stdlib/ndarray/ctor].
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 ndarraylike2ndarray = require( '@stdlib/ndarray/ndarraylike2ndarray' );
41+
```
42+
43+
#### ndarraylike2ndarray( x\[, options] )
44+
45+
Converts an ndarray-like object to an [`ndarray`][@stdlib/ndarray/ctor].
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var arr = array( [ [ 1, 2 ], [ 3, 4 ] ] );
51+
var out = ndarraylike2ndarray( arr );
52+
// returns <ndarray>
53+
```
54+
55+
The function supports the same `options` as [`ndarray`][@stdlib/ndarray/ctor].
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
## Notes
66+
67+
- If provided a **read-only** [`ndarray`][@stdlib/ndarray/ctor], the function returns a **read-only** [`ndarray`][@stdlib/ndarray/ctor].
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<!-- Package usage examples. -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var array = require( '@stdlib/ndarray/array' );
83+
var ndarraylike2ndarray = require( '@stdlib/ndarray/ndarraylike2ndarray' );
84+
85+
// Create an ndarray:
86+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
87+
88+
// Create another ndarray sharing the same data:
89+
var out = ndarraylike2ndarray( x );
90+
// returns <ndarray>
91+
92+
// Print various properties:
93+
console.log( 'dtype: %s', out.dtype );
94+
console.log( 'ndims: %d', out.shape.length );
95+
console.log( 'length: %d', out.length );
96+
console.log( 'shape: [ %s ]', out.shape.join( ', ' ) );
97+
console.log( 'strides: [ %s ]', out.strides.join( ', ' ) );
98+
console.log( 'offset: %d', out.offset );
99+
console.log( 'order: %s', out.order );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 ndarraylike2ndarray = 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 = ndarraylike2ndarray( 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 = ndarraylike2ndarray( 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 = ndarraylike2ndarray( 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+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
{{alias}}( x[, options] )
3+
Converts an ndarray-like object to an ndarray.
4+
5+
If provided a read-only ndarray, the function returns a read-only ndarray.
6+
7+
Parameters
8+
----------
9+
x: ndarrayLike
10+
Input ndarray-like object.
11+
12+
options: Object (optional)
13+
Options.
14+
15+
options.mode: string (optional)
16+
Specifies how to handle indices which exceed array dimensions. If equal
17+
to 'throw', an ndarray instance throws an error when an index exceeds
18+
array dimensions. If equal to 'normalize', an ndarray instance
19+
normalizes negative indices and throws an error when an index exceeds
20+
array dimensions. If equal to 'wrap', an ndarray instance wraps around
21+
indices exceeding array dimensions using modulo arithmetic. If equal to
22+
'clamp', an ndarray instance sets an index exceeding array dimensions
23+
to either `0` (minimum index) or the maximum index. Default: 'throw'.
24+
25+
options.submode: Array<string> (optional)
26+
Specifies how to handle subscripts which exceed array dimensions. If a
27+
mode for a corresponding dimension is equal to 'throw', an ndarray
28+
instance throws an error when a subscript exceeds array dimensions. If
29+
equal to 'normalize', an ndarray instance normalizes negative
30+
subscripts and throws an error when a subscript exceeds array
31+
dimensions. If equal to 'wrap', an ndarray instance wraps around
32+
subscripts exceeding array dimensions using modulo arithmetic. If equal
33+
to 'clamp', an ndarray instance sets a subscript exceeding array
34+
dimensions to either `0` (minimum index) or the maximum index. If the
35+
number of modes is fewer than the number of dimensions, the function
36+
recycles modes using modulo arithmetic. Default: [ options.mode ].
37+
38+
options.readonly: boolean (optional)
39+
Boolean indicating whether an array should be read-only.
40+
41+
Returns
42+
-------
43+
out: ndarray
44+
Output array.
45+
46+
Examples
47+
--------
48+
> var arr = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] );
49+
> var out = {{alias}}( arr )
50+
<ndarray>
51+
52+
See Also
53+
--------
54+

0 commit comments

Comments
 (0)