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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 888x 888x 888x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 422x 3x 3x 279x 279x 279x 422x 43x 3x 3x 43x 6x 6x 34x 43x 20x 20x 20x 43x 14x 14x 43x 236x 236x 236x 256x 256x 422x 97x 97x 9x 9x 88x 97x 3x 3x 85x 97x 6x 6x 79x 97x 5x 5x 74x 97x 66x 66x 66x 66x 97x 8x 8x 97x 225x 422x 139x 139x 225x 225x 422x 141x 422x 84x 84x 211x 211x 422x 83x 83x 83x 128x 128x 57x 57x 34x 34x 57x 57x 71x 71x 30x 30x 30x 30x 17x 17x 30x 41x 41x 27x 27x 19x 19x 19x 41x 14x 7x 7x 7x 7x 197x 422x 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.
*/
/* eslint-disable max-len */
'use strict';
// MODULES //
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isObject = require( '@stdlib/assert/is-plain-object' );
var isArray = require( '@stdlib/assert/is-array' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getShape = require( '@stdlib/ndarray/shape' );
var getOrder = require( '@stdlib/ndarray/order' );
var format = require( '@stdlib/string/format' );
var base = require( './base.js' );
// FUNCTIONS //
/**
* Tests if a value is a numeric scalar (number or complex-like).
*
* @private
* @param {*} value - value to test
* @returns {boolean} boolean indicating whether value is a numeric scalar
*/
function isScalar( value ) {
return isNumber( value ) || isComplexLike( value );
}
// MAIN //
/**
* Multiplies each element in an input ndarray by a scalar constant and adds a scalar constant to each result along one or more ndarray dimensions.
*
* @param {ndarrayLike} x - input ndarray
* @param {(ndarrayLike|number|ComplexLike)} [alpha=1.0] - scalar constant to multiply
* @param {(ndarrayLike|number|ComplexLike)} [beta=0.0] - scalar constant to add
* @param {Options} [options] - function options
* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} alpha argument must be either an ndarray-like object, a number, or a complex number
* @throws {TypeError} beta argument must be either an ndarray-like object, a number, or a complex number
* @throws {TypeError} options argument must be an object
* @throws {RangeError} dimension indices must not exceed input ndarray bounds
* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions
* @throws {Error} must provide valid options
* @returns {ndarray} output ndarray
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* // Create a data buffer:
* var xbuf = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
*
* // Define the shape of the input array:
* var sh = [ 2, 2, 2 ];
*
* // Define the array strides:
* var sx = [ 4, 2, 1 ];
*
* // Define the index offset:
* var ox = 0;
*
* // Create an input ndarray:
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
*
* // Perform operation:
* var out = axpb( x, 5.0, 3.0 );
* // returns <ndarray>[ [ [ -7.0, 8.0 ], [ 18.0, -22.0 ] ], [ [ 23.0, 3.0 ], [ -2.0, -12.0 ] ] ]
*/
function axpb( x ) {
var nargs;
var alpha;
var beta;
var opts;
var ord;
var dt;
var sh;
nargs = arguments.length;
// Resolve input ndarray meta data:
dt = getDType( x );
ord = getOrder( x );
// Case: axpb( x )
if ( nargs < 2 ) {
return base( x, broadcastScalar( 1.0, dt, [], ord ), broadcastScalar( 0.0, dt, [], ord ) );
}
alpha = arguments[ 1 ];
// Case: axpb( x, alpha )
if ( nargs === 2 ) {
if ( isScalar( alpha ) ) {
return base( x, broadcastScalar( alpha, dt, [], ord ), broadcastScalar( 0.0, dt, [], ord ) );
}
if ( isndarrayLike( alpha ) ) {
return base( x, alpha, broadcastScalar( 0.0, dt, [], ord ) );
}
// Case: axpb( x, opts )
if ( isObject( alpha ) && !isArray( alpha ) ) {
opts = alpha;
alpha = 1.0;
beta = 0.0;
} else {
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a number, a complex number, or an options object. Value: `%s`.', alpha ) );
}
}
else {
beta = arguments[ 2 ];
}
// Case: axpb( x, alpha, beta )
if ( nargs === 3 && !opts ) {
// Case: axpb( x, alpha_scalar, beta_scalar )
if ( isScalar( alpha ) && isScalar( beta ) ) {
return base( x, broadcastScalar( alpha, dt, [], ord ), broadcastScalar( beta, dt, [], ord ) );
}
// Case: axpb( x, alpha_ndarray, beta_scalar )
if ( isndarrayLike( alpha ) && isScalar( beta ) ) {
return base( x, alpha, broadcastScalar( beta, dt, [], ord ) );
}
// Case: axpb( x, alpha_scalar, beta_ndarray )
if ( isScalar( alpha ) && isndarrayLike( beta ) ) {
return base( x, broadcastScalar( alpha, dt, [], ord ), beta );
}
// Case: axpb( x, alpha_ndarray, beta_ndarray )
if ( isndarrayLike( alpha ) && isndarrayLike( beta ) ) {
return base( x, alpha, beta );
}
// Case: axpb( x, alpha, opts )
if ( isScalar( alpha ) || isndarrayLike( alpha ) ) {
opts = beta;
beta = 0.0;
// Intentionally fall through...
} else {
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a number, or a complex number. Value: `%s`.', alpha ) );
}
}
// Case: axpb( x, alpha, beta, opts )
if ( nargs > 3 ) {
opts = arguments[ 3 ];
}
// Resolve the shape for broadcasting:
if ( hasOwnProp( opts, 'dims' ) ) {
sh = nonCoreShape( getShape( x ), opts.dims );
} else {
sh = [];
}
// Case: axpb( x, alpha_scalar, beta_scalar, opts )
if ( isScalar( alpha ) && isScalar( beta ) ) {
alpha = broadcastScalar( alpha, dt, sh, ord );
beta = broadcastScalar( beta, dt, sh, ord );
}
// Case: axpb( x, alpha_ndarray, beta_scalar, opts )
else if ( isndarrayLike( alpha ) && isScalar( beta ) ) {
// When not provided `dims`, the operation is performed across all dimensions and `alpha` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `alpha` to match the shape of the non-core dimensions...
if ( hasOwnProp( opts, 'dims' ) ) {
alpha = maybeBroadcastArray( alpha, sh );
}
beta = broadcastScalar( beta, dt, sh, ord );
}
// Case: axpb( x, alpha_scalar, beta_ndarray, opts )
else if ( isScalar( alpha ) && isndarrayLike( beta ) ) {
alpha = broadcastScalar( alpha, dt, sh, ord );
// When not provided `dims`, the operation is performed across all dimensions and `beta` is assumed to be a zero-dimensional ndarray; when `dims` is provided, we need to broadcast `beta` to match the shape of the non-core dimensions...
if ( hasOwnProp( opts, 'dims' ) ) {
beta = maybeBroadcastArray( beta, sh );
}
}
// Case: axpb( x, alpha_ndarray, beta_ndarray, opts )
else if ( isndarrayLike( alpha ) && isndarrayLike( beta ) ) {
// When not provided `dims`, the operation is performed across all dimensions and both `alpha` and `beta` are assumed to be zero-dimensional ndarrays; when `dims` is provided, we need to broadcast them to match the shape of the non-core dimensions...
if ( hasOwnProp( opts, 'dims' ) ) {
alpha = maybeBroadcastArray( alpha, sh );
beta = maybeBroadcastArray( beta, sh );
}
} else {
if ( !isScalar( alpha ) && !isndarrayLike( alpha ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a number, or a complex number. Value: `%s`.', alpha ) );
}
throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray, a number, or a complex number. Value: `%s`.', beta ) );
}
return base( x, alpha, beta, opts );
}
// EXPORTS //
module.exports = axpb;
|