All files / assign-scalar/lib main.js

60.13% Statements 270/449
100% Branches 1/1
0% Functions 0/3
60.13% Lines 270/449

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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 4501x 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 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  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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 isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' );
var isRealDataType = require( '@stdlib/ndarray/base/assert/is-real-data-type' );
var isComplexArray = require( '@stdlib/array/base/assert/is-complex-typed-array' );
var isBooleanArray = require( '@stdlib/array/base/assert/is-booleanarray' );
var iterationOrder = require( '@stdlib/ndarray/base/iteration-order' );
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var minmaxViewBufferIndex = require( '@stdlib/ndarray/base/minmax-view-buffer-index' );
var ndarray2object = require( '@stdlib/ndarray/base/ndarraylike2object' );
var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var gscal = require( '@stdlib/blas/base/gscal' );
var blockedaccessorassign2d = require( './2d_blocked_accessors.js' );
var blockedaccessorassign3d = require( './3d_blocked_accessors.js' );
var blockedaccessorassign4d = require( './4d_blocked_accessors.js' );
var blockedaccessorassign5d = require( './5d_blocked_accessors.js' );
var blockedaccessorassign6d = require( './6d_blocked_accessors.js' );
var blockedaccessorassign7d = require( './7d_blocked_accessors.js' );
var blockedaccessorassign8d = require( './8d_blocked_accessors.js' );
var blockedaccessorassign9d = require( './9d_blocked_accessors.js' );
var blockedaccessorassign10d = require( './10d_blocked_accessors.js' );
var blockedcomplexassign2d = require( './2d_blocked_complex.js' );
var blockedcomplexassign3d = require( './3d_blocked_complex.js' );
var blockedcomplexassign4d = require( './4d_blocked_complex.js' );
var blockedcomplexassign5d = require( './5d_blocked_complex.js' );
var blockedcomplexassign6d = require( './6d_blocked_complex.js' );
var blockedcomplexassign7d = require( './7d_blocked_complex.js' );
var blockedcomplexassign8d = require( './8d_blocked_complex.js' );
var blockedcomplexassign9d = require( './9d_blocked_complex.js' );
var blockedcomplexassign10d = require( './10d_blocked_complex.js' );
var blockedassign2d = require( './2d_blocked.js' );
var blockedassign3d = require( './3d_blocked.js' );
var blockedassign4d = require( './4d_blocked.js' );
var blockedassign5d = require( './5d_blocked.js' );
var blockedassign6d = require( './6d_blocked.js' );
var blockedassign7d = require( './7d_blocked.js' );
var blockedassign8d = require( './8d_blocked.js' );
var blockedassign9d = require( './9d_blocked.js' );
var blockedassign10d = require( './10d_blocked.js' );
var accessorassign0d = require( './0d_accessors.js' );
var accessorassign1d = require( './1d_accessors.js' );
var accessorassign2d = require( './2d_accessors.js' );
var accessorassign3d = require( './3d_accessors.js' );
var accessorassign4d = require( './4d_accessors.js' );
var accessorassign5d = require( './5d_accessors.js' );
var accessorassign6d = require( './6d_accessors.js' );
var accessorassign7d = require( './7d_accessors.js' );
var accessorassign8d = require( './8d_accessors.js' );
var accessorassign9d = require( './9d_accessors.js' );
var accessorassign10d = require( './10d_accessors.js' );
var accessorassignnd = require( './nd_accessors.js' );
var complexassign0d = require( './0d_complex.js' );
var complexassign1d = require( './1d_complex.js' );
var complexassign2d = require( './2d_complex.js' );
var complexassign3d = require( './3d_complex.js' );
var complexassign4d = require( './4d_complex.js' );
var complexassign5d = require( './5d_complex.js' );
var complexassign6d = require( './6d_complex.js' );
var complexassign7d = require( './7d_complex.js' );
var complexassign8d = require( './8d_complex.js' );
var complexassign9d = require( './9d_complex.js' );
var complexassign10d = require( './10d_complex.js' );
var complexassignnd = require( './nd_complex.js' );
var assign0d = require( './0d.js' );
var assign1d = require( './1d.js' );
var assign2d = require( './2d.js' );
var assign3d = require( './3d.js' );
var assign4d = require( './4d.js' );
var assign5d = require( './5d.js' );
var assign6d = require( './6d.js' );
var assign7d = require( './7d.js' );
var assign8d = require( './8d.js' );
var assign9d = require( './9d.js' );
var assign10d = require( './10d.js' );
var assignnd = require( './nd.js' );
 
 
// VARIABLES //
 
var ASSIGN = [
	assign0d,
	assign1d,
	assign2d,
	assign3d,
	assign4d,
	assign5d,
	assign6d,
	assign7d,
	assign8d,
	assign9d,
	assign10d
];
var ACCESSOR_ASSIGN = [
	accessorassign0d,
	accessorassign1d,
	accessorassign2d,
	accessorassign3d,
	accessorassign4d,
	accessorassign5d,
	accessorassign6d,
	accessorassign7d,
	accessorassign8d,
	accessorassign9d,
	accessorassign10d
];
var COMPLEX_ASSIGN = [
	complexassign0d,
	complexassign1d,
	complexassign2d,
	complexassign3d,
	complexassign4d,
	complexassign5d,
	complexassign6d,
	complexassign7d,
	complexassign8d,
	complexassign9d,
	complexassign10d
];
var BLOCKED_ASSIGN = [
	blockedassign2d, // 0
	blockedassign3d,
	blockedassign4d,
	blockedassign5d,
	blockedassign6d,
	blockedassign7d,
	blockedassign8d,
	blockedassign9d,
	blockedassign10d // 8
];
var BLOCKED_ACCESSOR_ASSIGN = [
	blockedaccessorassign2d, // 0
	blockedaccessorassign3d,
	blockedaccessorassign4d,
	blockedaccessorassign5d,
	blockedaccessorassign6d,
	blockedaccessorassign7d,
	blockedaccessorassign8d,
	blockedaccessorassign9d,
	blockedaccessorassign10d // 8
];
var BLOCKED_COMPLEX_ASSIGN = [
	blockedcomplexassign2d, // 0
	blockedcomplexassign3d,
	blockedcomplexassign4d,
	blockedcomplexassign5d,
	blockedcomplexassign6d,
	blockedcomplexassign7d,
	blockedcomplexassign8d,
	blockedcomplexassign9d,
	blockedcomplexassign10d // 8
];
var MAX_DIMS = ASSIGN.length - 1;
 
// TODO: consider adding a package utility for mapping a complex dtype to its complementary real-valued counterpart
var COMPLEX_TO_REAL = { // WARNING: this table needs to be manually updated if we add support for additional complex number dtypes
	'complex128': 'float64',
	'complex64': 'float32',
	'complex32': 'float16'
};
 
 
// FUNCTIONS //
 
/**
* Converts a boolean ndarray to an 8-bit unsigned integer ndarray.
*
* ## Notes
*
* -   The function mutates the input ndarray object.
*
* @private
* @param {Object} x - input ndarray object
* @returns {Object} output ndarray object
*/
function boolean2uint8( x ) {
	x.data = reinterpretBoolean( x.data, 0 );
	x.accessorProtocol = false;
	return x;
}
 
/**
* Converts a complex-valued floating-point ndarray to a real-valued floating-point ndarray.
*
* ## Notes
*
* -   The function mutates the input ndarray object.
*
* @private
* @param {Object} x - input ndarray object
* @returns {Object} output ndarray object
*/
function complex2real( x ) {
	var ndims = x.shape.length;
	x.data = reinterpretComplex( x.data, 0 );
	x.accessorProtocol = false;
	x.dtype = COMPLEX_TO_REAL[ String( x.dtype ) ];
	x.strides = gscal( ndims, 2, x.strides, 1 );
	x.offset *= 2;
	return x;
}
 
 
// MAIN //
 
/**
* Assigns a scalar value to every element of an output ndarray.
*
* ## Notes
*
* -   Each provided ndarray should be an object with the following properties:
*
*     -   **dtype**: data type.
*     -   **data**: data buffer.
*     -   **shape**: dimensions.
*     -   **strides**: stride lengths.
*     -   **offset**: index offset.
*     -   **order**: specifies whether an ndarray is row-major (C-style) or column-major (Fortran-style).
*
* @param {ArrayLikeObject<Object>} arrays - array-like object containing a zero-dimensional ndarray containing the scalar value and one output ndarray
* @returns {void}
*
* @example
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var Float64Array = require( '@stdlib/array/float64' );
*
* // Create a zero-dimensional ndarray containing the scalar value:
* var x = scalar2ndarray( 5.0, {
*     'dtype': 'float64'
* });
*
* // Create a data buffer:
* var ybuf = new Float64Array( 12 );
*
* // Define the shape of the output array:
* var shape = [ 3, 1, 2 ];
*
* // Define the array strides:
* var sy = [ 4, 4, 1 ];
*
* // Define the index offset:
* var oy = 1;
*
* // Create the output ndarray-like object:
* var y = {
*     'dtype': 'float64',
*     'data': ybuf,
*     'shape': shape,
*     'strides': sy,
*     'offset': oy,
*     'order': 'row-major'
* };
*
* // Assign the scalar value:
* assignScalar( [ x, y ] );
*
* console.log( y.data );
* // => <Float64Array>[ 0.0, 5.0, 5.0, 0.0, 0.0, 5.0, 5.0, 0.0, 0.0, 5.0, 5.0, 0.0 ]
*/
function assignScalar( arrays ) {
	var isCmplx;
	var ndims;
	var ymmv;
	var shy;
	var ioy;
	var len;
	var ord;
	var sy;
	var oy;
	var ns;
	var re;
	var im;
	var x;
	var y;
	var d;
	var i;

	// Unpack the ndarrays and standardize ndarray meta data:
	x = ndarray2object( arrays[ 0 ] );
	y = ndarray2object( arrays[ 1 ] );

	// Check for known array types which can be reinterpreted for better iteration performance...
	if ( isBooleanArray( x.data ) && isBooleanArray( y.data ) ) {
		x = boolean2uint8( x );
		y = boolean2uint8( y );
	} else if ( isComplexArray( x.data ) && isComplexArray( y.data ) ) {
		x = complex2real( x );
		y = complex2real( y );
		re = x.data[ x.offset ];
		im = x.data[ x.offset+1 ];
		isCmplx = true;
	}
	// Determine whether we are casting a real data type to a complex data type and we need to use a specialized accessor (note: we don't support the other way, complex-to-real, as this is not an allowed (mostly) safe cast; note: we cannot create a specialized view for assigning only real components, as the imaginary component for each element in `y` also needs to be set to zero and while we could perform two passes, it's not clear it's worth the effort)...
	else if ( isRealDataType( x.dtype ) && isComplexDataType( y.dtype ) ) {
		y = complex2real( y );
		re = x.data[ x.offset ];
		im = 0.0;
		isCmplx = true;
	}
	shy = y.shape;
	ndims = shy.length;

	// Determine whether we can avoid iteration altogether...
	if ( ndims === 0 ) {
		if ( x.accessorProtocol || y.accessorProtocol ) {
			return ACCESSOR_ASSIGN[ ndims ]( x, y );
		}
		if ( isCmplx ) {
			return COMPLEX_ASSIGN[ ndims ]( re, im, y );
		}
		return ASSIGN[ ndims ]( x, y );
	}
	// Compute the number of elements and the number of singleton dimensions...
	len = 1; // number of elements
	ns = 0;  // number of singleton dimensions
	for ( i = 0; i < ndims; i++ ) {
		d = shy[ i ];

		// Note that, if one of the dimensions is `0`, the length will be `0`...
		len *= d;

		// Check whether the current dimension is a singleton dimension...
		if ( d === 1 ) {
			ns += 1;
		}
	}
	// Check whether we were provided an empty ndarray...
	if ( len === 0 ) {
		return;
	}
	// Determine whether the ndarray is one-dimensional and thus readily translates to a one-dimensional strided array...
	if ( ndims === 1 ) {
		if ( x.accessorProtocol || y.accessorProtocol ) {
			return ACCESSOR_ASSIGN[ ndims ]( x, y );
		}
		if ( isCmplx ) {
			return COMPLEX_ASSIGN[ ndims ]( re, im, y );
		}
		return ASSIGN[ ndims ]( x, y );
	}
	sy = y.strides;

	// Determine whether the ndarray has only **one** non-singleton dimension (e.g., ndims=4, shape=[10,1,1,1]) so that we can treat the ndarray as being equivalent to a one-dimensional strided array...
	if ( ns === ndims-1 ) {
		// Get the index of the non-singleton dimension...
		for ( i = 0; i < ndims; i++ ) {
			if ( shy[ i ] !== 1 ) {
				break;
			}
		}
		y.shape = [ shy[i] ];
		y.strides = [ sy[i] ];
		if ( x.accessorProtocol || y.accessorProtocol ) {
			return ACCESSOR_ASSIGN[ 1 ]( x, y );
		}
		if ( isCmplx ) {
			return COMPLEX_ASSIGN[ 1 ]( re, im, y );
		}
		return ASSIGN[ 1 ]( x, y );
	}
	ioy = iterationOrder( sy ); // +/-1

	// Determine whether we can avoid blocked iteration...
	if ( ioy !== 0 ) {
		// Determine the minimum and maximum linear indices which are accessible by the array view:
		ymmv = minmaxViewBufferIndex( shy, sy, y.offset );

		// Determine whether we can ignore shape (and strides) and treat the ndarray as a linear one-dimensional strided array...
		if ( len === ( ymmv[1]-ymmv[0]+1 ) ) {
			// Note: the above is equivalent to @stdlib/ndarray/base/assert/is-contiguous, but in-lined so we can retain computed values...
			if ( ioy === 1 ) {
				oy = ymmv[ 0 ];
			} else {
				oy = ymmv[ 1 ];
			}
			y.shape = [ len ];
			y.strides = [ ioy ];
			y.offset = oy;
			if ( x.accessorProtocol || y.accessorProtocol ) {
				return ACCESSOR_ASSIGN[ 1 ]( x, y );
			}
			if ( isCmplx ) {
				return COMPLEX_ASSIGN[ 1 ]( re, im, y );
			}
			return ASSIGN[ 1 ]( x, y );
		}
		// The ndarray is non-contiguous, so we cannot directly use one-dimensional array functionality...

		// Determine whether we can use simple nested loops...
		if ( ndims <= MAX_DIMS ) {
			ord = strides2order( sy );

			// So long as iteration always moves in the same direction (i.e., no mixed sign strides), we can leverage cache-optimal (i.e., normal) nested loops without resorting to blocked iteration...
			if ( x.accessorProtocol || y.accessorProtocol ) {
				return ACCESSOR_ASSIGN[ ndims ]( x, y, ord === 1 );
			}
			if ( isCmplx ) {
				return COMPLEX_ASSIGN[ ndims ]( re, im, y, ord === 1 );
			}
			return ASSIGN[ ndims ]( x, y, ord === 1 );
		}
		// Fall-through to blocked iteration...
	}
	// At this point, we're either dealing with a non-contiguous n-dimensional array or a high dimensional n-dimensional array, so our only hope is that we can still perform blocked iteration...

	// Determine whether we can perform blocked iteration...
	if ( ndims <= MAX_DIMS ) {
		if ( x.accessorProtocol || y.accessorProtocol ) {
			return BLOCKED_ACCESSOR_ASSIGN[ ndims-2 ]( x, y );
		}
		if ( isCmplx ) {
			return BLOCKED_COMPLEX_ASSIGN[ ndims-2 ]( re, im, y );
		}
		return BLOCKED_ASSIGN[ ndims-2 ]( x, y );
	}
	// Fall-through to linear view iteration without regard for how data is stored in memory (i.e., take the slow path)...
	if ( x.accessorProtocol || y.accessorProtocol ) {
		return accessorassignnd( x, y );
	}
	if ( isCmplx ) {
		return complexassignnd( re, im, y );
	}
	assignnd( x, y );
}
 
 
// EXPORTS //
 
module.exports = assignScalar;