Skip to content

Commit cf9c1fd

Browse files
authored
feat: add blas/ext/base/ndarray/gone-to
PR-URL: #11308 Closes: stdlib-js/metr-issue-tracker#245 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent f7bab5c commit cf9c1fd

File tree

10 files changed

+695
-0
lines changed

10 files changed

+695
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# goneTo
22+
23+
> Fill a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from one.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var goneTo = require( '@stdlib/blas/ext/base/ndarray/gone-to' );
37+
```
38+
39+
#### goneTo( arrays )
40+
41+
Fills a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from one.
42+
43+
```javascript
44+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
46+
var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
47+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
48+
// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
49+
50+
var out = goneTo( [ x ] );
51+
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **arrays**: array-like object containing a one-dimensional input ndarray.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="notes">
63+
64+
## Notes
65+
66+
- The input ndarray is modified **in-place** (i.e., the input ndarray is **mutated**).
67+
68+
</section>
69+
70+
<!-- /.notes -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var zeros = require( '@stdlib/array/zeros' );
80+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
81+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
82+
var goneTo = require( '@stdlib/blas/ext/base/ndarray/gone-to' );
83+
84+
var xbuf = zeros( 10, 'generic' );
85+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
86+
console.log( ndarray2array( x ) );
87+
88+
goneTo( [ x ] );
89+
console.log( ndarray2array( x ) );
90+
```
91+
92+
</section>
93+
94+
<!-- /.examples -->
95+
96+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
97+
98+
<section class="related">
99+
100+
</section>
101+
102+
<!-- /.related -->
103+
104+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
105+
106+
<section class="links">
107+
108+
</section>
109+
110+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 zeros = require( '@stdlib/array/zeros' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var goneTo = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var xbuf;
50+
var x;
51+
52+
xbuf = zeros( len, options.dtype );
53+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
54+
55+
return benchmark;
56+
57+
/**
58+
* Benchmark function.
59+
*
60+
* @private
61+
* @param {Benchmark} b - benchmark instance
62+
*/
63+
function benchmark( b ) {
64+
var out;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = goneTo( [ x ] );
70+
if ( typeof out !== 'object' ) {
71+
b.fail( 'should return an ndarray' );
72+
}
73+
}
74+
b.toc();
75+
if ( typeof out !== 'object' ) {
76+
b.fail( 'should return an ndarray' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( format( '%s:len=%d', pkg, len ), f );
105+
}
106+
}
107+
108+
main();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arrays )
3+
Fills a one-dimensional ndarray with linearly spaced numeric elements which
4+
increment by `1` starting from one.
5+
6+
The input ndarray is modified *in-place* (i.e., the input ndarray is
7+
*mutated*).
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing a one-dimensional input ndarray.
13+
14+
Returns
15+
-------
16+
out: ndarray
17+
Input ndarray.
18+
19+
Examples
20+
--------
21+
> var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
22+
> var dt = 'generic';
23+
> var sh = [ xbuf.length ];
24+
> var sx = [ 1 ];
25+
> var ox = 0;
26+
> var ord = 'row-major';
27+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
28+
> {{alias}}( [ x ] )
29+
<ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
30+
31+
See Also
32+
--------
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Fills a one-dimensional ndarray with linearly spaced numeric elements which increment by `1` starting from one.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray
29+
* @returns input ndarray
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
*
34+
* var xbuf = [ 0.0, 0.0, 0.0, 0.0 ];
35+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
36+
* // returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
37+
*
38+
* var out = goneTo( [ x ] );
39+
* // returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
40+
*/
41+
declare function goneTo<T extends typedndarray<unknown> = typedndarray<unknown>>( arrays: [ T ] ): T;
42+
43+
44+
// EXPORTS //
45+
46+
export = goneTo;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import goneTo = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns an ndarray...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
33+
goneTo( [ x ] ); // $ExpectType genericndarray<number>
34+
}
35+
36+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
37+
{
38+
goneTo( '10' ); // $ExpectError
39+
goneTo( 10 ); // $ExpectError
40+
goneTo( true ); // $ExpectError
41+
goneTo( false ); // $ExpectError
42+
goneTo( null ); // $ExpectError
43+
goneTo( undefined ); // $ExpectError
44+
goneTo( [] ); // $ExpectError
45+
goneTo( {} ); // $ExpectError
46+
goneTo( ( x: number ): number => x ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the function is provided an unsupported number of arguments...
50+
{
51+
const x = zeros( [ 10 ], {
52+
'dtype': 'generic'
53+
});
54+
55+
goneTo(); // $ExpectError
56+
goneTo( [ x ], {} ); // $ExpectError
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
var zeros = require( '@stdlib/array/zeros' );
22+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
23+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
24+
var goneTo = require( './../lib' );
25+
26+
var xbuf = zeros( 10, 'generic' );
27+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
28+
console.log( ndarray2array( x ) );
29+
30+
goneTo( [ x ] );
31+
console.log( ndarray2array( x ) );

0 commit comments

Comments
 (0)