Skip to content

Commit e5c9e29

Browse files
committed
Add utility to create an array filled with NaNs
1 parent adf6342 commit e5c9e29

File tree

15 files changed

+1444
-0
lines changed

15 files changed

+1444
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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 an array filled with NaNs and having a specified length.
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/array/nans' );
41+
```
42+
43+
#### nans( length\[, dtype] )
44+
45+
Creates an array filled with NaNs and having a specified length.
46+
47+
```javascript
48+
var arr = nans( 2 );
49+
// returns <Float64Array>[ NaN, NaN ]
50+
```
51+
52+
The function recognizes the following data types:
53+
54+
- `float64`: double-precision floating-point numbers (IEEE 754)
55+
- `float32`: single-precision floating-point numbers (IEEE 754)
56+
- `complex128`: double-precision complex floating-point numbers
57+
- `complex64`: single-precision complex floating-point numbers
58+
- `generic`: generic JavaScript values
59+
60+
By default, the output array data type is `float64` (i.e., a [typed array][mdn-typed-array]). To specify an alternative data type, provide a `dtype` argument.
61+
62+
```javascript
63+
var arr = nans( 2, 'float32' );
64+
// returns <Float32Array>[ NaN, NaN ]
65+
```
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- For complex number arrays, each element of the returned array has a real component equal to `NaN` and an imaginary component equal to `NaN`.
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var dtypes = require( '@stdlib/array/typed-float-dtypes' );
93+
var nans = require( '@stdlib/array/nans' );
94+
95+
// Get a list of array data types:
96+
var dt = dtypes();
97+
98+
// Generate filled arrays...
99+
var arr;
100+
var i;
101+
for ( i = 0; i < dt.length; i++ ) {
102+
arr = nans( 4, dt[ i ] );
103+
console.log( arr );
104+
}
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- 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. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
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+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
132+
133+
</section>
134+
135+
<!-- /.links -->
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var nans = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var arr;
34+
var i;
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
arr = nans( 0 );
38+
if ( arr.length !== 0 ) {
39+
b.fail( 'should have length 0' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isTypedArrayLike( arr ) ) {
44+
b.fail( 'should return a typed array' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
49+
50+
bench( pkg+':dtype=float64', function benchmark( b ) {
51+
var arr;
52+
var i;
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = nans( 0, 'float64' );
56+
if ( arr.length !== 0 ) {
57+
b.fail( 'should have length 0' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isTypedArrayLike( arr ) ) {
62+
b.fail( 'should return a typed array' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+':dtype=float32', function benchmark( b ) {
69+
var arr;
70+
var i;
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
arr = nans( 0, 'float32' );
74+
if ( arr.length !== 0 ) {
75+
b.fail( 'should have length 0' );
76+
}
77+
}
78+
b.toc();
79+
if ( !isTypedArrayLike( arr ) ) {
80+
b.fail( 'should return a typed array' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
85+
86+
bench( pkg+':dtype=complex128', function benchmark( b ) {
87+
var arr;
88+
var i;
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
arr = nans( 0, 'complex128' );
92+
if ( arr.length !== 0 ) {
93+
b.fail( 'should have length 0' );
94+
}
95+
}
96+
b.toc();
97+
if ( !isTypedArrayLike( arr ) ) {
98+
b.fail( 'should return a typed array' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
});
103+
104+
bench( pkg+':dtype=complex64', function benchmark( b ) {
105+
var arr;
106+
var i;
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
arr = nans( 0, 'complex64' );
110+
if ( arr.length !== 0 ) {
111+
b.fail( 'should have length 0' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isTypedArrayLike( arr ) ) {
116+
b.fail( 'should return a typed array' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
121+
122+
bench( pkg+':dtype=generic', function benchmark( b ) {
123+
var arr;
124+
var i;
125+
b.tic();
126+
for ( i = 0; i < b.iterations; i++ ) {
127+
arr = nans( 0, 'generic' );
128+
if ( arr.length !== 0 ) {
129+
b.fail( 'should have length 0' );
130+
}
131+
}
132+
b.toc();
133+
if ( !isArray( arr ) ) {
134+
b.fail( 'should return an array' );
135+
}
136+
b.pass( 'benchmark finished' );
137+
b.end();
138+
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var nans = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var arr;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
arr = nans( len, 'complex128' );
55+
if ( arr.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isTypedArrayLike( arr ) ) {
61+
b.fail( 'should return a typed array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':dtype=complex128,len='+len, f );
90+
}
91+
}
92+
93+
main();

0 commit comments

Comments
 (0)