|
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 dtype = require( './../../dtype' ); |
|
24 |
+ |
var full = require( './../../full' ); |
|
25 |
+ |
var Complex128 = require( '@stdlib/complex/float64' ); |
|
26 |
+ |
var Complex64 = require( '@stdlib/complex/float32' ); |
|
27 |
+ |
var format = require( '@stdlib/string/format' ); |
|
28 |
+ |
|
|
29 |
+ |
|
|
30 |
+ |
// VARIABLES // |
|
31 |
+ |
|
|
32 |
+ |
var Z128 = new Complex128( NaN, NaN ); |
|
33 |
+ |
var Z64 = new Complex64( NaN, NaN ); |
|
34 |
+ |
var DTYPES = [ 'float64', 'float32', 'complex128', 'complex64', 'generic' ]; |
|
35 |
+ |
|
|
36 |
+ |
|
|
37 |
+ |
// MAIN // |
|
38 |
+ |
|
|
39 |
+ |
/** |
|
40 |
+ |
* Creates an array filled with NaNs and having the same length and data type as a provided input array. |
|
41 |
+ |
* |
|
42 |
+ |
* @param {(Array|TypedArray|ComplexArray)} x - input array |
|
43 |
+ |
* @param {string} [dtype] - data type |
|
44 |
+ |
* @throws {TypeError} first argument must be an array or typed array |
|
45 |
+ |
* @throws {TypeError} second argument must be a supported data type |
|
46 |
+ |
* @returns {(TypedArray|Array|ComplexArray)} array or typed array |
|
47 |
+ |
* |
|
48 |
+ |
* @example |
|
49 |
+ |
* var arr = nansLike( [ 0.0, 0.0 ] ); |
|
50 |
+ |
* // returns [ NaN, NaN ] |
|
51 |
+ |
* |
|
52 |
+ |
* @example |
|
53 |
+ |
* var arr = nansLike( [ 0.0, 0.0 ], 'float32' ); |
|
54 |
+ |
* // returns <Float32Array>[ NaN, NaN ] |
|
55 |
+ |
*/ |
|
56 |
+ |
function nansLike( x ) { |
|
57 |
+ |
var dt; |
|
58 |
+ |
var v; |
|
59 |
+ |
|
|
60 |
+ |
dt = dtype( x ); // delegate input argument validation to dtype resolution |
|
61 |
+ |
if ( dt === null ) { |
|
62 |
+ |
throw new TypeError( format( 'invalid argument. First argument must be either an array, typed array, or complex typed array. Value: `%s`.', x ) ); |
|
63 |
+ |
} |
|
64 |
+ |
if ( arguments.length > 1 ) { |
|
65 |
+ |
dt = arguments[ 1 ]; |
|
66 |
+ |
if ( DTYPES.indexOf( dt ) === -1 ) { |
|
67 |
+ |
throw new TypeError( format( 'invalid argument. Second argument must be one of the following: "%s". Value: `%s`.', DTYPES.join( '", "' ), dt ) ); |
|
68 |
+ |
} |
|
69 |
+ |
} else if ( DTYPES.indexOf( dt ) === -1 ) { |
|
70 |
+ |
throw new TypeError( format( 'invalid argument. First argument must be one of the following data types: "%s". Value: `%s`.', DTYPES.join( '", "' ), dt ) ); |
|
71 |
+ |
} |
|
72 |
+ |
if ( dt === 'complex128' ) { |
|
73 |
+ |
v = Z128; |
|
74 |
+ |
} else if ( dt === 'complex64' ) { |
|
75 |
+ |
v = Z64; |
|
76 |
+ |
} else { |
|
77 |
+ |
v = NaN; |
|
78 |
+ |
} |
|
79 |
+ |
return full( x.length, v, dt ); |
|
80 |
+ |
} |
|
81 |
+ |
|
|
82 |
+ |
|
|
83 |
+ |
// EXPORTS // |
|
84 |
+ |
|
|
85 |
+ |
module.exports = nansLike; |