All files main.js

98.78% Statements 162/164
96.55% Branches 28/29
100% Functions 2/2
98.78% Lines 162/164

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 1651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 370x 370x 370x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 265x 265x 265x 265x 265x 265x 265x 265x 265x 265x 265x 80x 80x 265x     185x 185x 185x 185x 185x 265x 15x 15x 265x 14x 14x 156x 156x 156x 156x 156x 265x 130x 130x 28x 28x 102x 130x 84x 130x 18x 18x 265x 26x 26x 120x 120x 265x 64x 265x 56x 56x 114x 265x 58x 265x 56x 56x 108x 265x 20x 20x 88x 88x 265x 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 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 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 - scalar constant to multiply
* @param {(ndarrayLike|number|ComplexLike)} beta - 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} second argument must be either an ndarray-like object, a number, or a complex number
* @throws {TypeError} third 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, alpha, beta ) {
	var isScalarAlpha;
	var isScalarBeta;
	var options;
	var nargs;
	var ord;
	var dt;
	var sh;
 
	nargs = arguments.length;
	if ( !isndarrayLike( x ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) );
	}
	if ( nargs < 3 ) {
		throw new TypeError( 'invalid invocation. Insufficient arguments. Must provide at least three arguments.' );
	}
	// Cache scalar checks:
	isScalarAlpha = isScalar( alpha );
	isScalarBeta = isScalar( beta );
 
	// Validate alpha and beta arguments:
	if ( !isScalarAlpha && !isndarrayLike( alpha ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be either an ndarray, a number, or a complex number. Value: `%s`.', alpha ) );
	}
	if ( !isScalarBeta && !isndarrayLike( beta ) ) {
		throw new TypeError( format( 'invalid argument. Third argument must be either an ndarray, a number, or a complex number. Value: `%s`.', beta ) );
	}
	// Resolve input ndarray meta data:
	dt = getDType( x );
	ord = getOrder( x );
 
	// Case: axpb( x, alpha, beta, options )
	if ( nargs > 3 ) {
		options = arguments[ 3 ];
		if ( !isObject( options ) ) {
			throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
		}
		// Resolve the shape for broadcasting:
		if ( hasOwnProp( options, 'dims' ) ) {
			sh = nonCoreShape( getShape( x ), options.dims );
		} else {
			sh = [];
		}
	} else {
		sh = [];
	}
 
	// Broadcast alpha to match the appropriate shape:
	if ( isScalarAlpha ) {
		alpha = broadcastScalar( alpha, dt, sh, ord );
	} else {
		alpha = maybeBroadcastArray( alpha, sh );
	}
	// Broadcast beta to match the appropriate shape:
	if ( isScalarBeta ) {
		beta = broadcastScalar( beta, dt, sh, ord );
	} else {
		beta = maybeBroadcastArray( beta, sh );
	}
	// Case: axpb( x, alpha, beta )
	if ( nargs === 3 ) {
		return base( x, alpha, beta );
	}
	// Case: axpb( x, alpha, beta, options )
	return base( x, alpha, beta, options );
}
 
 
// EXPORTS //
 
module.exports = axpb;