|
| 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 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isNdarrayLike = require( '@stdlib/assert/is-ndarray-like' ); |
| 24 | +var format = require( '@stdlib/string/format' ); |
| 25 | +var maybeBroadcastArrays = require( '@stdlib/ndarray/base/maybe-broadcast-arrays' ); |
| 26 | +var base = require( './base.js' ); |
| 27 | + |
| 28 | + |
| 29 | +// MAIN // |
| 30 | + |
| 31 | +/** |
| 32 | +* Computes the maximum value along one or more ndarray dimensions and assigns the results to a provided output ndarray according to a mask. |
| 33 | +* |
| 34 | +* @param {ndarray} x - input ndarray |
| 35 | +* @param {ndarray} mask - mask ndarray |
| 36 | +* @param {ndarray} out - output ndarray |
| 37 | +* @param {Options} [options] - function options |
| 38 | +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction |
| 39 | +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions |
| 40 | +* @throws {TypeError} first argument must be an ndarray-like object |
| 41 | +* @throws {TypeError} second argument must be an ndarray-like object |
| 42 | +* @throws {TypeError} third argument must be an ndarray-like object |
| 43 | +* @throws {TypeError} options argument must be an object |
| 44 | +* @throws {RangeError} dimension indices must not exceed input ndarray bounds |
| 45 | +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions |
| 46 | +* @throws {Error} must provide valid options |
| 47 | +* @returns {ndarray} output ndarray |
| 48 | +* |
| 49 | +* @example |
| 50 | +* var Float64Array = require( '@stdlib/array/float64' ); |
| 51 | +* var Uint8Array = require( '@stdlib/array/uint8' ); |
| 52 | +* var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 53 | +* |
| 54 | +* // Create a data buffer: |
| 55 | +* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 56 | +* |
| 57 | +* // Create a mask buffer: |
| 58 | +* var mbuf = new Uint8Array( [ 0, 0, 0, 1, 0, 1 ] ); |
| 59 | +* |
| 60 | +* // Create an output buffer: |
| 61 | +* var obuf = new Float64Array( [ 0.0 ] ); |
| 62 | +* |
| 63 | +* // Define the shape of the input array: |
| 64 | +* var sh = [ 3, 2 ]; |
| 65 | +* |
| 66 | +* // Define the array strides: |
| 67 | +* var sx = [ 2, 1 ]; |
| 68 | +* var sm = [ 2, 1 ]; |
| 69 | +* |
| 70 | +* // Define the index offset: |
| 71 | +* var ox = 0; |
| 72 | +* var om = 0; |
| 73 | +* |
| 74 | +* // Create the input ndarray: |
| 75 | +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); |
| 76 | +* |
| 77 | +* // Create the mask ndarray: |
| 78 | +* var mask = new ndarray( 'uint8', mbuf, sh, sm, om, 'row-major' ); // cspell:disable-line |
| 79 | +* |
| 80 | +* // Create the output ndarray: |
| 81 | +* var out = new ndarray( 'float64', obuf, [], [ 0 ], 0, 'row-major' ); |
| 82 | +* |
| 83 | +* // Perform reduction: |
| 84 | +* var res = assign( x, mask, out ); |
| 85 | +* // returns <ndarray> |
| 86 | +* |
| 87 | +* var v = res.get(); |
| 88 | +* // returns 5.0 |
| 89 | +*/ |
| 90 | +function assign( x, mask, out ) { |
| 91 | + var arrs; |
| 92 | + if ( !isNdarrayLike( x ) ) { |
| 93 | + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); |
| 94 | + } |
| 95 | + if ( !isNdarrayLike( mask ) ) { |
| 96 | + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', mask ) ); |
| 97 | + } |
| 98 | + if ( !isNdarrayLike( out ) ) { |
| 99 | + throw new TypeError( format( 'invalid argument. Third argument must be an ndarray-like object. Value: `%s`.', out ) ); |
| 100 | + } |
| 101 | + arrs = maybeBroadcastArrays( [ x, mask ] ); |
| 102 | + if ( arguments.length > 3 ) { |
| 103 | + return base.assign( arrs[ 0 ], arrs[ 1 ], out, arguments[ 3 ] ); |
| 104 | + } |
| 105 | + return base.assign( arrs[ 0 ], arrs[ 1 ], out ); |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +// EXPORTS // |
| 110 | + |
| 111 | +module.exports = assign; |
0 commit comments