Skip to content

Commit f5fa7a4

Browse files
committed
Add REPL text and TypeScript declarations
1 parent 5d08329 commit f5fa7a4

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( x )
3+
Computes the absolute value.
4+
5+
If provided a number, the function returns a number.
6+
7+
If provided an ndarray or array-like object, the function performs element-
8+
wise iteration and returns an ndarray having the same shape as `x` and
9+
containing element-wise results.
10+
11+
Parameters
12+
----------
13+
x: ndarray|ArrayLikeObject|number
14+
Input value.
15+
16+
Returns
17+
-------
18+
y: ndarray|number
19+
Element-wise results.
20+
21+
Examples
22+
--------
23+
// Provide a number:
24+
> var y = {{alias}}( -1.0 )
25+
1.0
26+
27+
// Provide an array-like object:
28+
> var x = new {{alias:@stdlib/array/float64}}( [ -1.0, -2.0 ] );
29+
> y = {{alias}}( x )
30+
<ndarray>
31+
> y.data
32+
<Float64Array>[ 1.0, 2.0 ]
33+
34+
// Provide an ndarray:
35+
> x = {{alias:@stdlib/ndarray/array}}( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
36+
> y = {{alias}}( x )
37+
<ndarray>
38+
> y.get( 0, 1 )
39+
2.0
40+
41+
See Also
42+
--------
43+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
import { ndarray } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Computes the absolute value.
28+
*
29+
* @param x - input value
30+
* @returns absolute value
31+
*
32+
* @example
33+
* var y = abs( -1.0 );
34+
* // returns 1.0
35+
*/
36+
declare function abs( x: number ): number;
37+
38+
/**
39+
* Computes the absolute value.
40+
*
41+
* @param x - input value
42+
* @returns absolute value
43+
*
44+
* @example
45+
* var Float64Array = require( `@stdlib/array/float64` );
46+
*
47+
* var x = new Float64Array( [ -1.0, -2.0 ] );
48+
*
49+
* var y = abs( x );
50+
* // returns <ndarray>
51+
*
52+
* var v = y.data;
53+
* // returns <Float64Array>[ 1.0, 2.0 ]
54+
*
55+
* @example
56+
* var array = require( `@stdlib/ndarray/array` );
57+
*
58+
* var x = array( [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] );
59+
*
60+
* var y = abs( x );
61+
* // returns <ndarray>
62+
*
63+
* var v = y.get( 0, 1 );
64+
* // returns 2.0
65+
*/
66+
declare function abs( x: ndarray | ArrayLike<number> ): ndarray;
67+
68+
69+
// EXPORTS //
70+
71+
export = abs;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
import abs = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number if provided a number...
25+
{
26+
abs( 8 ); // $ExpectType number
27+
}
28+
29+
// The function returns an ndarray if provided an array-like object...
30+
{
31+
const x = new Float64Array( 10 );
32+
33+
abs( x ); // $ExpectType ndarray
34+
}
35+
36+
// The function returns an ndarray if provided an ndarray...
37+
{
38+
const buf = [ 1, 2, 3, 4 ];
39+
40+
const x = {
41+
'byteLength': null,
42+
'BYTES_PER_ELEMENT': null,
43+
'data': buf,
44+
'dtype': 'generic',
45+
'flags': {
46+
'ROW_MAJOR_CONTIGUOUS': true,
47+
'COLUMN_MAJOR_CONTIGUOUS': false
48+
},
49+
'length': 4,
50+
'ndims': 1,
51+
'offset': 0,
52+
'order': 'row-major',
53+
'shape': [ 4 ],
54+
'strides': [ 1 ],
55+
'get': ( i: number ): number => {
56+
return buf[ i ];
57+
},
58+
'set': ( i: number, v: number ): void => {
59+
buf[ i ] = v;
60+
}
61+
};
62+
63+
abs( x ); // $ExpectType ndarray
64+
}
65+
66+
// The function does not compile if provided a value other than an ndarray, array-like object, or number...
67+
{
68+
abs( true ); // $ExpectError
69+
abs( false ); // $ExpectError
70+
abs( null ); // $ExpectError
71+
abs( undefined ); // $ExpectError
72+
abs( {} ); // $ExpectError
73+
}
74+
75+
// The function does not compile if provided insufficient arguments...
76+
{
77+
abs(); // $ExpectError
78+
}

0 commit comments

Comments
 (0)