Skip to content

Commit 590a7d9

Browse files
committed
docs: add REPL help and TypeScript declarations
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 903ffa2 commit 590a7d9

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
{{alias}}( arrays, fcn )
3+
Applies a binary callback to elements in input ndarrays and assigns results
4+
to elements in an output ndarray.
5+
6+
Each provided "ndarray" should be an object with the following properties:
7+
8+
- dtype: data type.
9+
- data: data buffer.
10+
- shape: dimensions.
11+
- strides: stride lengths.
12+
- offset: index offset.
13+
- order: specifies whether an ndarray is row-major (C-style) or column-major
14+
(Fortran-style).
15+
16+
Parameters
17+
----------
18+
arrays: ArrayLikeObject<ndarray>
19+
Array-like object containing two input ndarrays and one output ndarray.
20+
21+
fcn: Function
22+
Binary callback.
23+
24+
Examples
25+
--------
26+
// Define ndarray data and meta data...
27+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
28+
> var ybuf = new {{alias:@stdlib/array/float64}}( [ 5.0, 6.0, 7.0, 8.0 ] );
29+
> var zbuf = new {{alias:@stdlib/array/float64}}( [ 0.0, 0.0, 0.0, 0.0 ] );
30+
> var dtype = 'float64';
31+
> var shape = [ 2, 2 ];
32+
> var sx = [ 2, 1 ];
33+
> var sy = [ 2, 1 ];
34+
> var sz = [ 2, 1 ];
35+
> var ox = 0;
36+
> var oy = 0;
37+
> var oz = 0;
38+
> var order = 'row-major';
39+
40+
// Using ndarrays...
41+
> var x = {{alias:@stdlib/ndarray/ctor}}( dtype, xbuf, shape, sx, ox, order );
42+
> var y = {{alias:@stdlib/ndarray/ctor}}( dtype, ybuf, shape, sy, oy, order );
43+
> var z = {{alias:@stdlib/ndarray/ctor}}( dtype, zbuf, shape, sz, oz, order );
44+
> {{alias}}( [ x, y, z ], {{alias:@stdlib/number/float64/base/add}} );
45+
> {{alias:@stdlib/ndarray/data-buffer}}( z )
46+
<Float64Array>[ 6.0, 8.0, 10.0, 12.0 ]
47+
48+
// Using minimal ndarray-like objects...
49+
> x = {
50+
... 'dtype': dtype,
51+
... 'data': xbuf,
52+
... 'shape': shape,
53+
... 'strides': sx,
54+
... 'offset': ox,
55+
... 'order': order
56+
... };
57+
> y = {
58+
... 'dtype': dtype,
59+
... 'data': ybuf,
60+
... 'shape': shape,
61+
... 'strides': sy,
62+
... 'offset': oy,
63+
... 'order': order
64+
... };
65+
> z = {
66+
... 'dtype': dtype,
67+
... 'data': zbuf,
68+
... 'shape': shape,
69+
... 'strides': sz,
70+
... 'offset': oz,
71+
... 'order': order
72+
... };
73+
> {{alias}}( [ x, y, z ], {{alias:@stdlib/number/float64/base/add}} );
74+
> {{alias:@stdlib/ndarray/data-buffer}}( z )
75+
<Float64Array>[ 6.0, 8.0, 10.0, 12.0 ]
76+
77+
See Also
78+
--------
79+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
import { ndarray } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Callback invoked for ndarray elements.
28+
*
29+
* @param x - first ndarray element
30+
* @param y - second ndarray element
31+
* @returns result
32+
*/
33+
type Binary = ( x: any, y: any ) => any;
34+
35+
/**
36+
* Applies a binary callback to elements in input ndarrays and assigns results to elements in an output ndarray.
37+
*
38+
* @param arrays - array-like object containing two input ndarrays and one output ndarray
39+
* @param fcn - binary callback
40+
* @throws arrays must have the same number of dimensions
41+
* @throws arrays must have the same shape
42+
*
43+
* @example
44+
* var Float64Array = require( '@stdlib/array/float64' );
45+
* var ndarray = require( '@stdlib/ndarray/ctor' );
46+
* var getData = require( '@stdlib/ndarray/data-buffer' );
47+
*
48+
* function add( a, b ) {
49+
* return a + b;
50+
* }
51+
*
52+
* // Create data buffers:
53+
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
54+
* var ybuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
55+
* var zbuf = new Float64Array( 6 );
56+
*
57+
* // Define the shape of the input and output arrays:
58+
* var shape = [ 3, 1, 2 ];
59+
*
60+
* // Define the array strides:
61+
* var sx = [ 2, 2, 1 ];
62+
* var sy = [ 2, 2, 1 ];
63+
* var sz = [ 2, 2, 1 ];
64+
*
65+
* // Define the index offsets:
66+
* var ox = 0;
67+
* var oy = 0;
68+
* var oz = 0;
69+
*
70+
* // Create the input and output ndarrays:
71+
* var x = new ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' );
72+
* var y = new ndarray( 'float64', ybuf, shape, sy, oy, 'row-major' );
73+
* var z = new ndarray( 'float64', zbuf, shape, sz, oz, 'row-major' );
74+
*
75+
* // Apply the binary function:
76+
* binary( [ x, y, z ], add );
77+
*
78+
* console.log( getData( z ) );
79+
* // => <Float64Array>[ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
80+
*/
81+
declare function binary( arrays: ArrayLike<ndarray>, fcn: Binary ): void;
82+
83+
84+
// EXPORTS //
85+
86+
export = binary;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 zeros = require( '@stdlib/ndarray/zeros' );
20+
import binary = require( './index' );
21+
22+
/**
23+
* Callback function.
24+
*
25+
* @param x - first input value
26+
* @param y - second input value
27+
* @returns result
28+
*/
29+
function fcn( x: number, y: number ): number {
30+
return x + y;
31+
}
32+
33+
34+
// TESTS //
35+
36+
// The function returns `undefined`...
37+
{
38+
const x = zeros( [ 2, 2 ] );
39+
const y = zeros( [ 2, 2 ] );
40+
const z = zeros( [ 2, 2 ] );
41+
const arrays = [ x, y, z ];
42+
43+
binary( arrays, fcn ); // $ExpectType void
44+
}
45+
46+
// The compiler throws an error if the function is provided a first argument which is not an array-like object containing ndarray-like objects...
47+
{
48+
binary( 5, fcn ); // $ExpectError
49+
binary( true, fcn ); // $ExpectError
50+
binary( false, fcn ); // $ExpectError
51+
binary( null, fcn ); // $ExpectError
52+
binary( undefined, fcn ); // $ExpectError
53+
binary( {}, fcn ); // $ExpectError
54+
binary( [ 1 ], fcn ); // $ExpectError
55+
binary( ( x: number ): number => x, fcn ); // $ExpectError
56+
}
57+
58+
// The compiler throws an error if the function is provided a second argument which is not a binary function...
59+
{
60+
const x = zeros( [ 2, 2 ] );
61+
const y = zeros( [ 2, 2 ] );
62+
const z = zeros( [ 2, 2 ] );
63+
const arrays = [ x, y, z ];
64+
65+
binary( arrays, '10' ); // $ExpectError
66+
binary( arrays, 5 ); // $ExpectError
67+
binary( arrays, true ); // $ExpectError
68+
binary( arrays, false ); // $ExpectError
69+
binary( arrays, null ); // $ExpectError
70+
binary( arrays, undefined ); // $ExpectError
71+
binary( arrays, [] ); // $ExpectError
72+
binary( arrays, {} ); // $ExpectError
73+
}
74+
75+
// The compiler throws an error if the function is provided an unsupported number of arguments...
76+
{
77+
const x = zeros( [ 2, 2 ] );
78+
const y = zeros( [ 2, 2 ] );
79+
const z = zeros( [ 2, 2 ] );
80+
const arrays = [ x, y, z ];
81+
82+
binary(); // $ExpectError
83+
binary( arrays ); // $ExpectError
84+
binary( arrays, fcn, 10 ); // $ExpectError
85+
}

0 commit comments

Comments
 (0)