Skip to content

Commit dce87cd

Browse files
committed
Add assign implementation and update index.js/main.js
1 parent b7b6c0d commit dce87cd

File tree

3 files changed

+118
-83
lines changed

3 files changed

+118
-83
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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;

lib/node_modules/@stdlib/stats/mskmax/lib/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@
6060

6161
// MODULES //
6262

63+
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
6364
var main = require( './main.js' );
65+
var assign = require( './assign.js' );
66+
67+
68+
// MAIN //
69+
70+
setReadOnly( main, 'assign', assign );
6471

6572

6673
// EXPORTS //

lib/node_modules/@stdlib/stats/mskmax/lib/main.js

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -95,89 +95,6 @@ function mskmax( x, mask ) {
9595
return base( arrs[ 0 ], arrs[ 1 ] );
9696
}
9797

98-
/**
99-
* Computes the maximum value along one or more ndarray dimensions according to a mask and assigns the results to a provided output ndarray.
100-
*
101-
* @name assign
102-
* @memberof mskmax
103-
* @type {Function}
104-
* @param {ndarray} x - input ndarray
105-
* @param {ndarray} mask - mask ndarray
106-
* @param {ndarray} out - output ndarray
107-
* @param {Options} [options] - function options
108-
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction
109-
* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions
110-
* @throws {TypeError} first argument must be an ndarray-like object
111-
* @throws {TypeError} second argument must be an ndarray-like object
112-
* @throws {TypeError} third argument must be an ndarray-like object
113-
* @throws {TypeError} options argument must be an object
114-
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
115-
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
116-
* @throws {Error} must provide valid options
117-
* @returns {ndarray} output ndarray
118-
*
119-
* @example
120-
* var Float64Array = require( '@stdlib/array/float64' );
121-
* var Uint8Array = require( '@stdlib/array/uint8' );
122-
* var ndarray = require( '@stdlib/ndarray/ctor' );
123-
*
124-
* // Create a data buffer:
125-
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
126-
*
127-
* // Create a mask buffer:
128-
* var mbuf = new Uint8Array( [ 0, 0, 0, 1, 0, 1 ] );
129-
*
130-
* // Create an output buffer:
131-
* var obuf = new Float64Array( [ 0.0 ] );
132-
*
133-
* // Define the shape of the input array:
134-
* var sh = [ 3, 2 ];
135-
*
136-
* // Define the array strides:
137-
* var sx = [ 2, 1 ];
138-
* var sm = [ 2, 1 ];
139-
*
140-
* // Define the index offset:
141-
* var ox = 0;
142-
* var om = 0;
143-
*
144-
* // Create the input ndarray:
145-
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
146-
*
147-
* // Create the mask ndarray:
148-
* var mask = new ndarray( 'uint8', mbuf, sh, sm, om, 'row-major' ); // cspell:disable-line
149-
*
150-
* // Create the output ndarray:
151-
* var out = new ndarray( 'float64', obuf, [], [ 0 ], 0, 'row-major' );
152-
*
153-
* // Perform reduction:
154-
* var res = mskmax.assign( x, mask, out );
155-
* // returns <ndarray>
156-
*
157-
* var v = res.get();
158-
* // returns 5.0
159-
*/
160-
function assign( x, mask, out ) {
161-
var arrs;
162-
if ( !isNdarrayLike( x ) ) {
163-
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
164-
}
165-
if ( !isNdarrayLike( mask ) ) {
166-
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', mask ) );
167-
}
168-
if ( !isNdarrayLike( out ) ) {
169-
throw new TypeError( format( 'invalid argument. Third argument must be an ndarray-like object. Value: `%s`.', out ) );
170-
}
171-
arrs = maybeBroadcastArrays( [ x, mask ] );
172-
if ( arguments.length > 3 ) {
173-
return base.assign( arrs[ 0 ], arrs[ 1 ], out, arguments[ 3 ] );
174-
}
175-
return base.assign( arrs[ 0 ], arrs[ 1 ], out );
176-
}
177-
178-
mskmax.assign = assign;
179-
180-
18198
// EXPORTS //
18299

183100
module.exports = mskmax;

0 commit comments

Comments
 (0)