Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Sep 24, 2023
1 parent 2cab243 commit b12edc3
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 221 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ In addition, the namespace contains the following multidimensional array utility
- <span class="signature">[`ndarrayCastingModes()`][@stdlib/ndarray/casting-modes]</span><span class="delimiter">: </span><span class="description">list of ndarray casting modes.</span>
- <span class="signature">[`defaults()`][@stdlib/ndarray/defaults]</span><span class="delimiter">: </span><span class="description">default ndarray settings.</span>
- <span class="signature">[`dispatch( fcns, types, data, nargs, nin, nout )`][@stdlib/ndarray/dispatch]</span><span class="delimiter">: </span><span class="description">create an ndarray function interface which performs multiple dispatch.</span>
- <span class="signature">[`dispatch( fcns, types, data, nargs, nin, nout )`][@stdlib/ndarray/dispatch]</span><span class="delimiter">: </span><span class="description">create an ndarray function interface which performs multiple dispatch.</span>
- <span class="signature">[`ndarrayDataTypes( [kind] )`][@stdlib/ndarray/dtypes]</span><span class="delimiter">: </span><span class="description">list of ndarray data types.</span>
- <span class="signature">[`ndemptyLike( x[, options] )`][@stdlib/ndarray/empty-like]</span><span class="delimiter">: </span><span class="description">create an uninitialized ndarray having the same shape and data type as a provided ndarray.</span>
- <span class="signature">[`ndempty( shape[, options] )`][@stdlib/ndarray/empty]</span><span class="delimiter">: </span><span class="description">create an uninitialized ndarray having a specified shape and data type.</span>
Expand Down
101 changes: 9 additions & 92 deletions dispatch-by/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
-->

# Dispatch
# dispatchBy

> Create an [ndarray][@stdlib/ndarray/ctor] function interface which performs multiple dispatch.
Expand All @@ -33,78 +33,17 @@ limitations under the License.
## Usage

```javascript
var dispatch = require( '@stdlib/ndarray/dispatch' );
var dispatchBy = require( '@stdlib/ndarray/dispatch-by' );
```

#### dispatch( fcns, types, data, nargs, nin, nout )
#### dispatchBy( fcns, types, data, nargs, nin, nout )

Returns an [ndarray][@stdlib/ndarray/ctor] function interface which performs multiple dispatch.

<!-- eslint-disable array-element-newline -->

```javascript
var unary = require( '@stdlib/ndarray/base/unary' );
var Float64Array = require( '@stdlib/array/float64' );
var Float32Array = require( '@stdlib/array/float32' );
var ndarray = require( '@stdlib/ndarray/ctor' );

function foo( x ) {
return x * 10.0;
}

function bar( x ) {
return x * 5.0;
}

// Define a list of ndarray functions for applying a unary callback:
var fcns = [
unary,
unary
];

// Define a one-dimensional list of input and output array types:
var types = [
'float64', 'float64', // input, output
'float32', 'float32' // input, output
];

// Define a list of callbacks which should be applied based on the provided array types:
var data = [
foo,
bar
];

// Define the total number of input arguments:
var nargs = 2; // input_array + output_array

// Define the number of input ndarrays:
var nin = 1;

// Define the number of output ndarrays:
var nout = 1;

// Create an ndarray function interface:
var fcn = dispatch( fcns, types, data, nargs, nin, nout );

// ...

var xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
var ybuf = new Float64Array( xbuf.length );

var x = ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
var y = ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );

fcn( x, y );
// ybuf => <Float64Array>[ 10.0, 20.0, 30.0 ]

xbuf = new Float32Array( [ 1.0, 2.0, 3.0 ] );
ybuf = new Float32Array( xbuf.length );

x = ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
y = ndarray( 'float32', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );

fcn( x, y );
// ybuf => <Float32Array>[ 5.0, 10.0, 15.0 ]
console.log( 'TODO' );
```

The function accepts the following arguments:
Expand Down Expand Up @@ -154,7 +93,7 @@ The function accepts the following arguments:
<!-- eslint-disable array-element-newline -->
```javascript
var unary = require( '@stdlib/ndarray/base/unary' );
var unary = require( '@stdlib/ndarray/base/unary-by' );
function foo( x ) {
return x * 10.0;
Expand All @@ -177,15 +116,15 @@ The function accepts the following arguments:
bar
];
var fcn = dispatch( fcns, types, data, 2, 1, 1 );
var fcn = dispatchBy( fcns, types, data, 2, 1, 1 );
```
is equivalent to
<!-- eslint-disable array-element-newline -->
```javascript
var unary = require( '@stdlib/ndarray/base/unary' );
var unary = require( '@stdlib/ndarray/base/unary-by' );
function foo( x ) {
return x * 10.0;
Expand All @@ -204,7 +143,7 @@ The function accepts the following arguments:
bar
];
var fcn = dispatch( unary, types, data, 2, 1, 1 );
var fcn = dispatchBy( unary, types, data, 2, 1, 1 );
```
</section>
Expand All @@ -218,29 +157,7 @@ The function accepts the following arguments:
<!-- eslint no-undef: "error" -->
```javascript
var unary = require( '@stdlib/ndarray/base/unary' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var abs = require( '@stdlib/math/base/special/abs' );
var Float64Array = require( '@stdlib/array/float64' );
var dispatch = require( '@stdlib/ndarray/dispatch' );
var types = [ 'float64', 'float64' ];
var data = [
abs
];
var absolute = dispatch( unary, types, data, 2, 1, 1 );
var xbuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
var x = ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
var y = ndarray( 'float64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
absolute( x, y );
console.log( ybuf );
// => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
console.log( 'TODO' );
```

</section>
Expand Down
10 changes: 5 additions & 5 deletions fancy/examples/3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ var s = E( 1, S(0,_,2), _ );
// Use the slice to create a view on the original ndarray:
var y1 = x[ s ];
console.log( toArray( y1 ) );
// => [ [ [ 9, 10 ], [ 13, 100 ] ] ]
// => [ [ 9, 10 ], [ 13, 100 ] ]

// Use alternative syntax:
var y2 = x[ [ 1, S(0,_,2), _ ] ];
console.log( toArray( y2 ) );
// => [ [ [ 9, 10 ], [ 13, 100 ] ] ]
// => [ [ 9, 10 ], [ 13, 100 ] ]

// Use alternative syntax:
var y3 = x[ '1,0::2,:' ];
console.log( toArray( y3 ) );
// => [ [ [ 9, 10 ], [ 13, 100 ] ] ]
// => [ [ 9, 10 ], [ 13, 100 ] ]

// Flip dimensions:
var y4 = x[ [ 1, S(_,_,-2), S(_,_,-1) ] ];
console.log( toArray( y4 ) );
// => [ [ [ 100, 13 ], [ 10, 9 ] ] ]
// => [ [ 100, 13 ], [ 10, 9 ] ]

// Only the second rows:
var y5 = x[ [ _, 1, _ ] ];
console.log( toArray( y5 ) );
// => [ [ [ 5, 6 ] ], [ [ 11, 12 ] ] ]
// throws [ [ 5, 6 ], [ 11, 12 ] ]
6 changes: 3 additions & 3 deletions fancy/lib/get.nd.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ var isFunction = require( '@stdlib/assert/is-function' );
var trim = require( '@stdlib/string/base/trim' );
var str2multislice = require( '@stdlib/slice/base/str2multislice' );
var seq2multislice = require( '@stdlib/slice/base/seq2multislice' );
var sargs2multislice = require( '@stdlib/slice/base/sargs2multislice' );
var format = require( '@stdlib/string/format' );
var hasProperty = require( './has_property.js' );
var RE_SUBSEQ = require( './re_subseq.js' );
var multisliceWrap = require( './wrap_multislice_arguments.js' );
var sliceView = require( './view.nd.js' );
var empty = require( './empty.js' );

Expand Down Expand Up @@ -104,9 +104,9 @@ function get( target, property, receiver ) {
}
// Case: array syntax (e.g., [ Slice(0,10,1), null, Slice(4,null,-1) ])

// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, str2multislice( multisliceWrap( prop ) ).data ); // TODO: handle `null`
// TODO: => @stdlib/ndarray/base/slice: return slice( receiver, sargs2multislice( prop ).data ); // TODO: handle `null`

return sliceView( target, '['+property+']', receiver, str2multislice( multisliceWrap( prop ) ) );
return sliceView( target, '['+property+']', receiver, sargs2multislice( prop ) );

/**
* Method wrapper.
Expand Down
48 changes: 0 additions & 48 deletions fancy/lib/nonreduced_dimensions.js

This file was deleted.

4 changes: 2 additions & 2 deletions fancy/lib/slice_start.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ var sub2ind = require( './../../base/sub2ind' );
// MAIN //

/**
* Resolves the linear index of the first element indexed by a multi-slice.
* Resolves the linear index of the first element indexed by a normalized multi-slice.
*
* ## Notes
*
* - If `strides` contains negative strides, if an `offset` is greater than `0`, the function returns a linear index with respect to the underlying data buffer. If an `offset` is equal to `0`, the function returns a linear index with respect to the array view. For more information, see `@stdlib/ndarray/base/sub2ind`.
*
* @private
* @param {MultiSlice} slice - multi-slice object
* @param {MultiSlice} slice - normalized multi-slice object
* @param {NonNegativeIntegerArray} shape - array shape
* @param {IntegerArray} strides - array strides
* @param {NonNegativeInteger} offset - index offset
Expand Down
4 changes: 2 additions & 2 deletions fancy/lib/slice_strides.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
// MAIN //

/**
* Resolves slice strides.
* Resolves slice strides for a provided normalized multi-slice object.
*
* @private
* @param {MultiSlice} slice - multi-slice object
* @param {MultiSlice} slice - normalized multi-slice object
* @param {IntegerArray} strides - array strides
* @param {NonNegativeIntegerArray} sdims - indices of non-reduced dimensions
* @returns {IntegerArray} slice strides
Expand Down
13 changes: 7 additions & 6 deletions fancy/lib/view.nd.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
// MODULES //

var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' );
var nonreducedDimensions = require( '@stdlib/slice/base/nonreduced-dimensions' );
var sliceShape = require( '@stdlib/slice/base/shape' );
var take = require( '@stdlib/array/base/take' );
var vind2bind = require( './../../base/vind2bind' );
var numel = require( './../../base/numel' );
var format = require( '@stdlib/string/format' );
var nonreducedDimensions = require( './nonreduced_dimensions.js' );
var sliceStart = require( './slice_start.js' );
var sliceStrides = require( './slice_strides.js' );
var options = require( './array_options.js' );
Expand Down Expand Up @@ -58,6 +58,7 @@ function sliceView( target, property, receiver, slice ) {
var sdims;
var ctor;
var sh;
var ns;

// Verify that we were successfully able to create a multi-slice:
if ( slice === null ) {
Expand All @@ -76,17 +77,17 @@ function sliceView( target, property, receiver, slice ) {
throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', shape.join( ',' ), slice.ndims ) );
}
// Normalize the slice object based on the array shape:
slice = normalizeMultiSlice( slice, shape, true );
ns = normalizeMultiSlice( slice, shape, true );

// In strict mode, if the slice exceeds array bounds, raise an exception...
if ( slice.code && strict ) {
if ( ns.code && strict ) {
throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) );
}
// Resolve the output array constructor:
ctor = receiver.constructor;

// Compute the slice shape:
sh = sliceShape( slice );
sh = sliceShape( ns );

// Resolve the indices of the non-reduced dimensions:
sdims = nonreducedDimensions( slice );
Expand All @@ -96,7 +97,7 @@ function sliceView( target, property, receiver, slice ) {
return empty( ctor, dtype, take( sh, sdims ), order );
}
// Resolve the index offset of the first element:
offset = vind2bind( shape, strides, offset, order, sliceStart( slice, shape, strides, 0 ), 'throw' );
offset = vind2bind( shape, strides, offset, order, sliceStart( ns, shape, strides, 0 ), 'throw' ); // TODO: @stdlib/ndarray/base/sind2bind

// Remove reduced dimensions from the slice shape:
sh = take( sh, sdims );
Expand All @@ -106,7 +107,7 @@ function sliceView( target, property, receiver, slice ) {
return new ctor( dtype, target.data, [], [ 0 ], offset, order, options() ); // eslint-disable-line max-len
}
// Update strides according to slice steps:
strides = sliceStrides( slice, strides, sdims );
strides = sliceStrides( ns, strides, sdims );

// Return a slice view:
return new ctor( dtype, target.data, sh, strides, offset, order, options() ); // eslint-disable-line max-len
Expand Down
Loading

0 comments on commit b12edc3

Please sign in to comment.