@stdlib/math-tools-unary 0.0.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,288 +0,0 @@
1
- /*
2
- * @license Apache-2.0
3
- *
4
- * Copyright (c) 2021 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
- /// <reference types="@stdlib/types"/>
20
-
21
- import { ndarray } from '@stdlib/types/ndarray';
22
- import dispatch = require( './index' );
23
-
24
- /**
25
- * Identity function.
26
- *
27
- * @param x - input value
28
- * @returns return value
29
- */
30
- function identity( x: number ): number {
31
- return x;
32
- }
33
-
34
- /**
35
- * Function which operates on a strided array.
36
- *
37
- * @param N - number of indexed elements
38
- * @param x - input array
39
- * @param strideX - `x` stride length
40
- * @param y - destination array
41
- * @param strideY - `y` stride length
42
- * @returns `y`
43
- */
44
- function stridedArrayFunction( N: number, x: ArrayLike<number>, strideX: number, y: ArrayLike<number>, strideY: number ): ArrayLike<number> { // tslint:disable-line:max-line-length
45
- let ix;
46
- let iy;
47
- if ( N <= 0 ) {
48
- return y;
49
- }
50
- if ( strideX < 0 ) {
51
- ix = ( 1 - N ) * strideX;
52
- } else {
53
- ix = 0;
54
- }
55
- if ( strideY < 0 ) {
56
- iy = ( 1 - N ) * strideY;
57
- } else {
58
- iy = 0;
59
- }
60
- if ( x[ ix ] !== x[ ix ] ) {
61
- return y;
62
- }
63
- if ( y[ iy ] !== y[ iy ] ) {
64
- return y;
65
- }
66
- return y;
67
- }
68
-
69
- /**
70
- * Function which operates on a strided array using alternative indexing semantics.
71
- *
72
- * @param N - number of indexed elements
73
- * @param x - input array
74
- * @param strideX - `x` stride length
75
- * @param offsetX - starting index for `x`
76
- * @param y - destination array
77
- * @param strideY - `y` stride length
78
- * @param offsetY - starting index for `y`
79
- * @returns `y`
80
- */
81
- function stridedArrayFunctionWithOffsets( N: number, x: ArrayLike<number>, strideX: number, offsetX: number, y: ArrayLike<number>, strideY: number, offsetY: number ): ArrayLike<number> { // tslint:disable-line:max-line-length
82
- let ix;
83
- let iy;
84
- if ( N <= 0 ) {
85
- return y;
86
- }
87
- ix = offsetX;
88
- iy = offsetY;
89
- if ( x[ ix + strideX ] !== x[ ix + strideX ] ) {
90
- return y;
91
- }
92
- if ( y[ iy + strideY ] !== y[ iy + strideY ] ) {
93
- return y;
94
- }
95
- return y;
96
- }
97
-
98
-
99
- // TESTS //
100
-
101
- // The function returns a dispatch function...
102
- {
103
- const t1 = {};
104
- dispatch( t1 ); // $ExpectType DispatchFunction
105
-
106
- const t2 = {
107
- 'scalar': [ 'number', identity ]
108
- };
109
- dispatch( t2 ); // $ExpectType DispatchFunction
110
-
111
- const t3 = {
112
- 'array': [ 'generic', stridedArrayFunction ]
113
- };
114
- dispatch( t3 ); // $ExpectType DispatchFunction
115
-
116
- const t4 = {
117
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
118
- };
119
- dispatch( t4 ); // $ExpectType DispatchFunction
120
-
121
- const t5 = {
122
- 'scalar': [ 'number', identity ],
123
- 'array': [ 'generic', stridedArrayFunction ]
124
- };
125
- dispatch( t5 ); // $ExpectType DispatchFunction
126
-
127
- const t6 = {
128
- 'scalar': [ 'number', identity ],
129
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
130
- };
131
- dispatch( t6 ); // $ExpectType DispatchFunction
132
-
133
- const t7 = {
134
- 'array': [ 'generic', stridedArrayFunction ],
135
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
136
- };
137
- dispatch( t7 ); // $ExpectType DispatchFunction
138
-
139
- const t8 = {
140
- 'scalar': [ 'number', identity ],
141
- 'array': [ 'generic', stridedArrayFunction ],
142
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
143
- };
144
- dispatch( t8 ); // $ExpectType DispatchFunction
145
- }
146
-
147
- // The function does not compile if not provided a table resolution object...
148
- {
149
- dispatch( '5' ); // $ExpectError
150
- dispatch( 5 ); // $ExpectError
151
- dispatch( true ); // $ExpectError
152
- dispatch( false ); // $ExpectError
153
- dispatch( null ); // $ExpectError
154
- dispatch( undefined ); // $ExpectError
155
- dispatch( [ '5' ] ); // $ExpectError
156
- dispatch( ( x: number ): number => x ); // $ExpectError
157
- }
158
-
159
- // The function does not compile if provided a table resolution object having an invalid `scalar` field...
160
- {
161
- dispatch( { 'scalar': 5 } ); // $ExpectError
162
- dispatch( { 'scalar': true } ); // $ExpectError
163
- dispatch( { 'scalar': false } ); // $ExpectError
164
- dispatch( { 'scalar': null } ); // $ExpectError
165
- dispatch( { 'scalar': [ 5 ] } ); // $ExpectError
166
- dispatch( { 'scalar': [ '5', 5 ] } ); // $ExpectError
167
- dispatch( { 'scalar': ( x: number ): number => x } ); // $ExpectError
168
- dispatch( { 'scalar': [ '5', ( x: string ): string => x ] } ); // $ExpectError
169
- }
170
-
171
- // The function does not compile if provided a table resolution object having an invalid `array` field...
172
- {
173
- dispatch( { 'array': 5 } ); // $ExpectError
174
- dispatch( { 'array': true } ); // $ExpectError
175
- dispatch( { 'array': false } ); // $ExpectError
176
- dispatch( { 'array': null } ); // $ExpectError
177
- dispatch( { 'array': [ 5 ] } ); // $ExpectError
178
- dispatch( { 'array': [ '5', 5 ] } ); // $ExpectError
179
- dispatch( { 'array': ( x: number ): number => x } ); // $ExpectError
180
- dispatch( { 'array': [ '5', ( x: string ): string => x ] } ); // $ExpectError
181
- }
182
-
183
- // The function does not compile if provided a table resolution object having an invalid `ndarray` field...
184
- {
185
- dispatch( { 'ndarray': 5 } ); // $ExpectError
186
- dispatch( { 'ndarray': true } ); // $ExpectError
187
- dispatch( { 'ndarray': false } ); // $ExpectError
188
- dispatch( { 'ndarray': null } ); // $ExpectError
189
- dispatch( { 'ndarray': [ 5 ] } ); // $ExpectError
190
- dispatch( { 'ndarray': [ '5', 5 ] } ); // $ExpectError
191
- dispatch( { 'ndarray': ( x: number ): number => x } ); // $ExpectError
192
- dispatch( { 'ndarray': [ '5', ( x: string ): string => x ] } ); // $ExpectError
193
- }
194
-
195
- // The function returns a function which returns a number if provided a number...
196
- {
197
- const t = {
198
- 'scalar': [ 'number', identity ],
199
- 'array': [ 'generic', stridedArrayFunction ],
200
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
201
- };
202
- const abs = dispatch( t ); // $ExpectType DispatchFunction
203
-
204
- abs( 8 ); // $ExpectType number
205
- }
206
-
207
- // The function returns a function which returns an array-like object if provided an array-like object...
208
- {
209
- const t = {
210
- 'scalar': [ 'number', identity ],
211
- 'array': [ 'generic', stridedArrayFunction ],
212
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
213
- };
214
- const abs = dispatch( t ); // $ExpectType DispatchFunction
215
-
216
- const x = new Float64Array( 10 );
217
-
218
- abs( x ); // $ExpectType ArrayLike<number>
219
- }
220
-
221
- // The function returns a function which returns an ndarray if provided an ndarray...
222
- {
223
- const t = {
224
- 'scalar': [ 'number', identity ],
225
- 'array': [ 'generic', stridedArrayFunction ],
226
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
227
- };
228
- const abs = dispatch( t ); // $ExpectType DispatchFunction
229
-
230
- const buf = [ 1, 2, 3, 4 ];
231
-
232
- const x: ndarray = {
233
- 'byteLength': null,
234
- 'BYTES_PER_ELEMENT': null,
235
- 'data': buf,
236
- 'dtype': 'generic',
237
- 'flags': {
238
- 'ROW_MAJOR_CONTIGUOUS': true,
239
- 'COLUMN_MAJOR_CONTIGUOUS': false
240
- },
241
- 'length': 4,
242
- 'ndims': 1,
243
- 'offset': 0,
244
- 'order': 'row-major',
245
- 'shape': [ 4 ],
246
- 'strides': [ 1 ],
247
- 'get': ( i: number ): number => {
248
- return buf[ i ];
249
- },
250
- 'set': ( i: number, v: number ): ndarray => {
251
- buf[ i ] = v;
252
- return x;
253
- }
254
- };
255
-
256
- abs( x ); // $ExpectType ndarray
257
- }
258
-
259
- // The function returns a function which does not compile if provided a value other than an ndarray, array-like object, or number...
260
- {
261
- const t = {
262
- 'scalar': [ 'number', identity ],
263
- 'array': [ 'generic', stridedArrayFunction ],
264
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
265
- };
266
- const abs = dispatch( t ); // $ExpectType DispatchFunction
267
-
268
- abs( '5' ); // $ExpectError
269
- abs( true ); // $ExpectError
270
- abs( false ); // $ExpectError
271
- abs( null ); // $ExpectError
272
- abs( undefined ); // $ExpectError
273
- abs( {} ); // $ExpectError
274
- abs( [ '5' ] ); // $ExpectError
275
- abs( ( x: number ): number => x ); // $ExpectError
276
- }
277
-
278
- // The function returns a function which does not compile if provided insufficient arguments...
279
- {
280
- const t = {
281
- 'scalar': [ 'number', identity ],
282
- 'array': [ 'generic', stridedArrayFunction ],
283
- 'ndarray': [ 'generic', stridedArrayFunctionWithOffsets ]
284
- };
285
- const abs = dispatch( t ); // $ExpectType DispatchFunction
286
-
287
- abs(); // $ExpectError
288
- }