main.js (2654B)
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 // MODULES // 22 23 var isFloat32VectorLike = require( '@stdlib/assert/is-float32vector-like' ); 24 var swap = require( './../../base/sswap' ).ndarray; 25 26 27 // MAIN // 28 29 /** 30 * Interchanges two single-precision floating-point vectors. 31 * 32 * @param {VectorLike} x - first input array 33 * @param {VectorLike} y - second input array 34 * @throws {TypeError} first argument must be a 1-dimensional ndarray containing single-precision floating-point numbers 35 * @throws {TypeError} second argument must be a 1-dimensional ndarray containing single-precision floating-point numbers 36 * @throws {RangeError} input arrays must be the same length 37 * @returns {VectorLike} `y` 38 * 39 * @example 40 * var Float32Array = require( '@stdlib/array/float32' ); 41 * var array = require( '@stdlib/ndarray/array' ); 42 * 43 * var x = array( new Float32Array( [ 4.0, 2.0, -3.0, 5.0, -1.0 ] ) ); 44 * var y = array( new Float32Array( [ 2.0, 6.0, -1.0, -4.0, 8.0 ] ) ); 45 * 46 * sswap( x, y ); 47 * 48 * var xbuf = x.data; 49 * // returns <Float32Array>[ 2.0, 6.0, -1.0, -4.0, 8.0 ] 50 * 51 * var ybuf = y.data; 52 * // returns <Float32Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ] 53 */ 54 function sswap( x, y ) { 55 if ( !isFloat32VectorLike( x ) ) { 56 throw new TypeError( 'invalid argument. First argument must be a 1-dimensional ndarray containing single-precision floating-point numbers (i.e., an ndarray whose underlying data buffer is a Float32Array). Value: `' + x + '`.' ); 57 } 58 if ( !isFloat32VectorLike( y ) ) { 59 throw new TypeError( 'invalid argument. Second argument must be a 1-dimensional ndarray containing single-precision floating-point numbers (i.e., an ndarray whose underlying data buffer is a Float32Array). Value: `' + y + '`.' ); 60 } 61 if ( x.length !== y.length ) { 62 throw new RangeError( 'invalid argument. Arrays must be the same length. First argument length: ' + x.length + '. Second argument length: ' + y.length + '.' ); 63 } 64 swap( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 65 return y; 66 } 67 68 69 // EXPORTS // 70 71 module.exports = sswap;