main.js (1777B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 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 // VARIABLES // 22 23 var M = 5; 24 25 26 // MAIN // 27 28 /** 29 * Multiplies `x` by a scalar `alpha`. 30 * 31 * @param {PositiveInteger} N - number of indexed elements 32 * @param {number} alpha - scalar 33 * @param {NumericArray} x - input array 34 * @param {PositiveInteger} stride - index increment 35 * @returns {NumericArray} input array 36 * 37 * @example 38 * var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ]; 39 * 40 * gscal( x.length, 5.0, x, 1 ); 41 * // x => [ -10.0, 5.0, 15.0, -25.0, 20.0, 0.0, -5.0, -15.0 ] 42 */ 43 function gscal( N, alpha, x, stride ) { 44 var m; 45 var i; 46 47 if ( N <= 0 || stride <= 0|| alpha === 1.0 ) { 48 return x; 49 } 50 // Use loop unrolling if the stride is equal to `1`... 51 if ( stride === 1 ) { 52 m = N % M; 53 54 // If we have a remainder, run a clean-up loop... 55 if ( m > 0 ) { 56 for ( i = 0; i < m; i += 1 ) { 57 x[ i ] *= alpha; 58 } 59 } 60 if ( N < M ) { 61 return x; 62 } 63 for ( i = m; i < N; i += M ) { 64 x[ i ] *= alpha; 65 x[ i+1 ] *= alpha; 66 x[ i+2 ] *= alpha; 67 x[ i+3 ] *= alpha; 68 x[ i+4 ] *= alpha; 69 } 70 return x; 71 } 72 N *= stride; 73 for ( i = 0; i < N; i += stride ) { 74 x[ i ] *= alpha; 75 } 76 return x; 77 } 78 79 80 // EXPORTS // 81 82 module.exports = gscal;