Skip to content

Commit 751b29f

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/nans
PR-URL: #11144 Closes: stdlib-js/metr-issue-tracker#210 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 7d2950a commit 751b29f

File tree

15 files changed

+1461
-0
lines changed

15 files changed

+1461
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
# nans
22+
23+
> Create a NaN-filled [ndarray][@stdlib/ndarray/base/ctor] having a specified shape and [data type][@stdlib/ndarray/dtypes].
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 nans = require( '@stdlib/ndarray/base/nans' );
41+
```
42+
43+
#### nans( dtype, shape, order )
44+
45+
Creates a NaN-filled [ndarray][@stdlib/ndarray/base/ctor] having a specified shape and [data type][@stdlib/ndarray/dtypes].
46+
47+
```javascript
48+
var getDType = require( '@stdlib/ndarray/dtype' );
49+
50+
var arr = nans( 'float64', [ 2, 2 ], 'row-major' );
51+
// returns <ndarray>[ [ NaN, NaN ], [ NaN, NaN ] ]
52+
53+
var dt = String( getDType( arr ) );
54+
// returns 'float64'
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. Must be a floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
60+
- **shape**: array shape.
61+
- **order**: specifies whether an [ndarray][@stdlib/ndarray/base/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style).
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<!-- Package usage examples. -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
var nans = require( '@stdlib/ndarray/base/nans' );
86+
87+
var dt = [
88+
'float64',
89+
'float32',
90+
'complex128',
91+
'complex64',
92+
'generic'
93+
];
94+
95+
// Generate NaN-filled arrays...
96+
var arr;
97+
var i;
98+
for ( i = 0; i < dt.length; i++ ) {
99+
arr = nans( dt[ i ], [ 2, 2 ], 'row-major' );
100+
console.log( ndarray2array( arr ) );
101+
}
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- 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. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
129+
130+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
131+
132+
</section>
133+
134+
<!-- /.links -->
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var format = require( '@stdlib/string/format' );
26+
var pkg = require( './../package.json' ).name;
27+
var nans = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) {
33+
var arr;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
arr = nans( 'float64', [ 0 ], 'row-major' );
38+
if ( arr.length !== 0 ) {
39+
b.fail( 'should have length 0' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isndarrayLike( arr ) ) {
44+
b.fail( 'should return an ndarray' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) {
51+
var arr;
52+
var i;
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = nans( 'float32', [ 0 ], 'row-major' );
56+
if ( arr.length !== 0 ) {
57+
b.fail( 'should have length 0' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) {
69+
var arr;
70+
var i;
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
arr = nans( 'complex128', [ 0 ], 'row-major' );
74+
if ( arr.length !== 0 ) {
75+
b.fail( 'should have length 0' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isndarrayLike( arr ) ) {
80+
b.fail( 'should return an ndarray' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
85+
86+
bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) {
87+
var arr;
88+
var i;
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
arr = nans( 'complex64', [ 0 ], 'row-major' );
92+
if ( arr.length !== 0 ) {
93+
b.fail( 'should have length 0' );
94+
}
95+
}
96+
b.toc();
97+
if ( !isndarrayLike( arr ) ) {
98+
b.fail( 'should return an ndarray' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
});
103+
104+
bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) {
105+
var arr;
106+
var i;
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
arr = nans( 'generic', [ 0 ], 'row-major' );
110+
if ( arr.length !== 0 ) {
111+
b.fail( 'should have length 0' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isndarrayLike( arr ) ) {
116+
b.fail( 'should return an ndarray' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 pow = require( '@stdlib/math/base/special/pow' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var nans = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var arr;
51+
var i;
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = nans( 'complex128', [ len ], 'row-major' );
56+
if ( arr.length !== len ) {
57+
b.fail( 'unexpected length' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
}
67+
}
68+
69+
70+
// MAIN //
71+
72+
/**
73+
* Main execution sequence.
74+
*
75+
* @private
76+
*/
77+
function main() {
78+
var len;
79+
var min;
80+
var max;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
len = pow( 10, i );
89+
f = createBenchmark( len );
90+
bench( format( '%s:dtype=complex128,size=%d', pkg, len ), f );
91+
}
92+
}
93+
94+
main();

0 commit comments

Comments
 (0)