You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Table where, for each respective policy, the value is a function which applies the policy to an input data type...
52
-
varPOLICY_TABLE1={
53
-
'default': defaultPolicy,
54
-
'default_index': defaultIndexPolicy,
55
-
'same': samePolicy,
56
-
'promoted': promotedPolicy,
57
-
'accumulation': accumulationPolicy
58
-
};
59
-
60
-
// Table where, for each respective policy, the value is an array whose first element is an assertion and whose second element is a fallback data type...
61
-
varPOLICY_TABLE2={
62
-
// Floating-point policies...
63
-
'floating_point': [
64
-
isFloatingPointDataType,
65
-
defaults.get('dtypes.floating_point')
66
-
],
67
-
'floating_point_and_generic': [
68
-
wrap(isFloatingPointDataType),
69
-
defaults.get('dtypes.floating_point')
70
-
],
71
-
'real_floating_point': [
72
-
isRealFloatingPointDataType,
73
-
DEFAULT_REAL_FLOATING_POINT_DTYPE
74
-
],
75
-
'real_floating_point_and_generic': [
76
-
wrap(isRealFloatingPointDataType),
77
-
DEFAULT_REAL_FLOATING_POINT_DTYPE
78
-
],
79
-
'complex_floating_point': [
80
-
isComplexFloatingPointDataType,
81
-
defaults.get('dtypes.complex_floating_point')
82
-
],
83
-
'complex_floating_point_and_generic': [
84
-
wrap(isComplexFloatingPointDataType),
85
-
defaults.get('dtypes.complex_floating_point')
86
-
],
87
-
88
-
// Integer policies...
89
-
'integer': [
90
-
isIntegerDataType,
91
-
defaults.get('dtypes.integer')
92
-
],
93
-
'integer_and_generic': [
94
-
wrap(isIntegerDataType),
95
-
defaults.get('dtypes.integer')
96
-
],
97
-
'signed_integer': [
98
-
isSignedIntegerDataType,
99
-
DEFAULT_SIGNED_INTEGER_DTYPE
100
-
],
101
-
'signed_integer_and_generic': [
102
-
wrap(isSignedIntegerDataType),
103
-
DEFAULT_SIGNED_INTEGER_DTYPE
104
-
],
105
-
'unsigned_integer': [
106
-
isUnsignedIntegerDataType,
107
-
DEFAULT_UNSIGNED_INTEGER_DTYPE
108
-
],
109
-
'unsigned_integer_and_generic': [
110
-
wrap(isUnsignedIntegerDataType),
111
-
DEFAULT_UNSIGNED_INTEGER_DTYPE
112
-
],
113
-
114
-
// Real-valued number policies...
115
-
'real': [
116
-
isRealDataType,
117
-
defaults.get('dtypes.real')
118
-
],
119
-
'real_and_generic': [
120
-
wrap(isRealDataType),
121
-
defaults.get('dtypes.real')
122
-
],
123
-
124
-
// Real- and complex-valued number policies...
125
-
'numeric': [
126
-
isNumericDataType,
127
-
defaults.get('dtypes.numeric')
128
-
],
129
-
'numeric_and_generic': [
130
-
wrap(isNumericDataType),
131
-
defaults.get('dtypes.numeric')
132
-
],
133
-
134
-
// Boolean policies...
135
-
'boolean': [
136
-
isBooleanDataType,
137
-
defaults.get('dtypes.boolean')
138
-
],
139
-
'boolean_and_generic': [
140
-
wrap(isBooleanDataType),
141
-
defaults.get('dtypes.boolean')
142
-
],
143
-
144
-
// Index policies...
145
-
'integer_index': [
146
-
isIntegerIndexDataType,
147
-
defaults.get('dtypes.integer_index')
148
-
],
149
-
'integer_index_and_generic': [
150
-
wrap(isIntegerIndexDataType),
151
-
defaults.get('dtypes.integer_index')
152
-
],
153
-
'boolean_index': [
154
-
isBooleanIndexDataType,
155
-
defaults.get('dtypes.boolean_index')
156
-
],
157
-
'boolean_index_and_generic': [
158
-
wrap(isBooleanIndexDataType),
159
-
defaults.get('dtypes.boolean_index')
160
-
],
161
-
'mask_index': [
162
-
isMaskIndexDataType,
163
-
defaults.get('dtypes.mask_index')
164
-
],
165
-
'mask_index_and_generic': [
166
-
wrap(isMaskIndexDataType),
167
-
defaults.get('dtypes.mask_index')
168
-
]
169
-
};
170
-
171
-
172
-
// FUNCTIONS //
173
-
174
-
/**
175
-
* Wraps a data type validation function to also check for a "generic" data type.
176
-
*
177
-
* @private
178
-
* @param {Function} fcn - validation function
179
-
* @returns {Function} wrapped validation function
180
-
*/
181
-
functionwrap(fcn){
182
-
returnwrapper;
183
-
184
-
/**
185
-
* Tests whether a provided data type is either "generic" or satisfies a data type validation function.
186
-
*
187
-
* @private
188
-
* @param {*} value - input value
189
-
* @returns {boolean} boolean indicating whether a provided value passes a test
190
-
*/
191
-
functionwrapper(value){
192
-
return(value==='generic')||fcn(value);
193
-
}
194
-
}
195
-
196
-
/**
197
-
* Returns the default data type.
198
-
*
199
-
* @private
200
-
* @returns {string} output ndarray data type
201
-
*/
202
-
functiondefaultPolicy(){
203
-
// When the policy is "default", the output data type should always be the default data type without consideration for the input data type:
204
-
returnDEFAULT_DTYPE;
205
-
}
206
-
207
-
/**
208
-
* Returns the default index data type.
209
-
*
210
-
* @private
211
-
* @returns {string} output ndarray data type
212
-
*/
213
-
functiondefaultIndexPolicy(){
214
-
// When the policy is "default_index", the output data type should always be the default index data type without consideration for the input data type:
215
-
returnDEFAULT_INDEX_DTYPE;
216
-
}
217
-
218
-
/**
219
-
* Applies the "same" policy by returning the input data type.
220
-
*
221
-
* @private
222
-
* @param {string} dtype - input ndarray data type
223
-
* @returns {string} output ndarray data type
224
-
*/
225
-
functionsamePolicy(dtype){
226
-
returndtype;
227
-
}
228
-
229
-
/**
230
-
* Applies the "promoted" policy by returning the input data type, as applying type promotion to a single data type is a no-op.
231
-
*
232
-
* @private
233
-
* @param {string} dtype - input ndarray data type
234
-
* @returns {string} output ndarray data type
235
-
*/
236
-
functionpromotedPolicy(dtype){
237
-
returndtype;
238
-
}
239
-
240
-
/**
241
-
* Applies the "accumulation" policy to an input data type.
242
-
*
243
-
* @private
244
-
* @param {string} dtype - input ndarray data type
245
-
* @returns {string} output ndarray data type
246
-
*/
247
-
functionaccumulationPolicy(dtype){
248
-
// If the input data type is floating-point, allow accumulation in that data type as overflow/underflow is handled naturally as a built-in feature of that data type...
249
-
if(isFloatingPointDataType(dtype)||dtype==='generic'){// NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited
250
-
returndtype;
251
-
}
252
-
// Unless the input data type value range is larger than the default un/signed integer data type, accumulate in the default un/signed integer data type, as accumulating in small range integer data types (e.g., `int8`) are at high risk for overflow, especially for ndarrays containing many elements...
0 commit comments