Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 141x 40x 40x 141x 101x 141x 101x 101x 101x 101x 101x 101x 101x 101x 101x 101x 141x 16x 16x 85x 85x 69x 69x 2x 2x 2x 67x 67x 67x 67x 67x 69x 16x 16x 16x 16x 16x 16x 141x 14x 14x 141x 69x 14x 14x 69x 41x 8x 8x 8x 4x 4x 4x 4x 4x 4x 41x 8x 8x 33x 16x 16x 25x 9x 9x 41x 69x 12x 11x 11x 1x 1x 69x 53x 53x 53x 53x 53x 53x 53x 53x 141x 141x 141x 141x 141x 141x 141x 141x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isFunction = require( '@stdlib/assert/is-function' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isInteger = require( '@stdlib/assert/is-integer' );
var isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' );
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
var getShape = require( '@stdlib/ndarray/shape' );
var getOrder = require( '@stdlib/ndarray/order' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' );
var map = require( '@stdlib/ndarray/base/map' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var emptyLike = require( '@stdlib/ndarray/empty-like' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var ROW_MAJOR = 'row-major';
var COL_MAJOR = 'column-major';
// MAIN //
/**
* Flattens an ndarray from a specified dimension while applying a callback function to each element.
*
* @param {ndarray} x - input ndarray
* @param {integer} dim - dimension to start flattening from
* @param {Options} [options] - function options
* @param {string} [options.order='row-major'] - order in which input ndarray elements should be flattened
* @param {*} [options.dtype] - output ndarray data type
* @param {Function} fcn - callback function
* @param {*} [thisArg] - callback execution context
* @throws {TypeError} first argument must be an ndarray having one or more dimensions
* @throws {TypeError} second argument must be an integer
* @throws {TypeError} options argument must be an object
* @throws {TypeError} callback argument must be a function
* @throws {TypeError} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* return value * 2.0;
* }
*
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
* // returns <ndarray>
*
* var y = flattenFromBy( x, 1, scale );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ]
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* return value * 2.0;
* }
*
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
* 'shape': [ 2, 3 ],
* 'order': 'column-major'
* });
* // returns <ndarray>
*
* var y = flattenFromBy( x, 0, scale );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ]
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* return value * 2.0;
* }
*
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
* 'shape': [ 2, 3 ],
* 'order': 'row-major'
* });
* // returns <ndarray>
*
* var opts = {
* 'order': 'column-major'
* };
*
* var y = flattenFromBy( x, 0, opts, scale );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ 2.0, 8.0, 4.0, 10.0, 6.0, 12.0 ]
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* return value * 2.0;
* }
*
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
* 'shape': [ 2, 3 ],
* 'order': 'column-major'
* });
* // returns <ndarray>
*
* var opts = {
* 'order': 'row-major'
* };
*
* var y = flattenFromBy( x, 0, opts, scale );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ 2.0, 6.0, 10.0, 4.0, 8.0, 12.0 ]
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* return value * 2.0;
* }
*
* var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], {
* 'shape': [ 2, 3 ],
* 'order': 'row-major'
* });
* // returns <ndarray>
*
* var opts = {
* 'order': 'same'
* };
*
* var y = flattenFromBy( x, 0, opts, scale );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ 2.0, 4.0, 6.0, 8.0, 10.0, 12.0 ]
*
* @example
* var array = require( '@stdlib/ndarray/array' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* function scale( value ) {
* this.count += 1;
* return value * 2.0;
* }
*
* var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 5.0, 6.0 ] ] ] );
* // returns <ndarray>
*
* var ctx = {
* 'count': 0
* };
*
* var y = flattenFromBy( x, 1, scale, ctx );
* // returns <ndarray>
*
* var arr = ndarray2array( y );
* // returns [ [ 2.0, 4.0 ], [ 6.0, 8.0 ], [ 10.0, 12.0 ] ]
*
* var count = ctx.count;
* // returns 6
*/
function flattenFromBy( x, dim, options, fcn, thisArg ) {
var hasOpts;
var nargs;
var view;
var opts;
var ctx;
var xsh;
var cb;
var st;
var y;
var o;
if ( !isndarrayLike( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
}
if ( !isInteger( dim ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) );
}
xsh = getShape( x );
if ( xsh.length < 1 ) {
throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', xsh.length ) );
}
nargs = arguments.length;
hasOpts = false;
// Define default options:
opts = {
'order': ROW_MAJOR, // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row)
'dtype': getDType( x )
};
// Case: flattenFromBy( x, dim, fcn )
if ( nargs <= 3 ) {
cb = options;
}
// Case: flattenFromBy( x, dim, ???, ??? )
else if ( nargs === 4 ) {
// Case: flattenFromBy( x, dim, fcn, thisArg )
if ( isFunction( options ) ) {
cb = options;
ctx = fcn;
}
// Case: flattenFromBy( x, dim, options, fcn )
else {
hasOpts = true;
cb = fcn;
}
}
// Case: flattenFromBy( x, dim, options, fcn, thisArg )
else {
hasOpts = true;
cb = fcn;
ctx = thisArg;
}
if ( !isFunction( cb ) ) {
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
}
if ( hasOpts ) {
if ( !isPlainObject( options ) ) {
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
}
if ( hasOwnProp( options, 'order' ) ) {
if ( options.order === 'any' ) {
// When 'any', we want to flatten according to the physical layout of the data in memory...
o = strides2order( getStrides( x ) );
if ( o === 1 ) {
// Data is currently arranged in row-major order:
opts.order = ROW_MAJOR;
} else if ( o === 2 ) {
// Data is currently arranged in column-major order:
opts.order = COL_MAJOR;
} else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major
// When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray:
opts.order = getOrder( x );
}
} else if ( options.order === 'same' ) {
// When 'same', we want to flatten according to the stated order of the input ndarray:
opts.order = getOrder( x );
} else if ( isOrder( options.order ) ) {
// When provided a specific order, flatten according to that order regardless of the order of the input ndarray:
opts.order = options.order;
} else {
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) );
}
}
if ( hasOwnProp( options, 'dtype' ) ) {
if ( !isMostlySafeCast( opts.dtype, options.dtype ) ) {
throw new TypeError( format( 'invalid option. First argument cannot be safely cast to the specified data type. Input data type: %s. Option: `%s`.', String( opts.dtype ), String( options.dtype ) ) );
}
opts.dtype = options.dtype;
}
}
// Create an output ndarray having contiguous memory:
y = emptyLike( x, {
'shape': flattenShapeFrom( xsh, dim ), // note: delegate to `flattenShapeFrom` to handle `dim` normalization
'order': opts.order,
'dtype': opts.dtype
});
// Create a view on top of the output ndarray having the same shape as the input ndarray:
st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ];
view = new ndarray( opts.dtype, getData( y ), xsh, st, 0, opts.order );
// Transform and assign elements to the output ndarray:
map( [ x, view ], cb, ctx );
return y;
}
// EXPORTS //
module.exports = flattenFromBy;
|