main.js (4028B)
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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); 24 var isCollection = require( '@stdlib/assert/is-collection' ); 25 var isFloat64Array = require( '@stdlib/assert/is-float64array' ); 26 var isFloat32Array = require( '@stdlib/assert/is-float32array' ); 27 var dswap = require( './../../base/dswap' ).ndarray; 28 var sswap = require( './../../base/sswap' ).ndarray; 29 var swap = require( './../../base/gswap' ).ndarray; 30 31 32 // MAIN // 33 34 /** 35 * Interchanges two vectors. 36 * 37 * ## Notes 38 * 39 * - In general, for best performance, especially for large vectors, provide 1-dimensional `ndarrays` whose underlying data type is either `float64` or `float32`. 40 * 41 * @param {(Collection|VectorLike)} x - first input array 42 * @param {(Collection|VectorLike)} y - second input array 43 * @throws {TypeError} first argument must be either an array-like object or a 1-dimensional ndarray 44 * @throws {TypeError} second argument must be either an array-like object or a 1-dimensional ndarray 45 * @throws {RangeError} input arrays must be the same length 46 * @returns {(Collection|VectorLike)} `y` 47 * 48 * @example 49 * var x = [ 4.0, 2.0, -3.0, 5.0, -1.0 ]; 50 * var y = [ 2.0, 6.0, -1.0, -4.0, 8.0 ]; 51 * 52 * gswap( x, y ); 53 * // x => [ 2.0, 6.0, -1.0, -4.0, 8.0 ] 54 * // y => [ 4.0, 2.0, -3.0, 5.0, -1.0 ] 55 */ 56 function gswap( x, y ) { 57 var isxf64; 58 var isxf32; 59 var isyf64; 60 var isyf32; 61 var isxa; 62 var isxv; 63 var isya; 64 var isyv; 65 66 isxa = isndarrayLike( x ); 67 isya = isndarrayLike( y ); 68 isxv = isxa && x.ndims === 1 && x.strides.length === 1; // is ndarray-like vector? 69 isyv = isya && y.ndims === 1 && y.strides.length === 1; // is ndarray-like vector? 70 if ( isxv ) { 71 isxf64 = isFloat64Array( x.data ); 72 isxf32 = ( isxf64 ) ? false : isFloat32Array( x.data ); 73 } else if ( isxa === false && isCollection( x ) ) { 74 isxf64 = isFloat64Array( x ); 75 isxf32 = ( isxf64 ) ? false : isFloat32Array( x ); 76 } else { 77 throw new TypeError( 'invalid argument. First argument must be either an array-like object or a 1-dimensional ndarray. Value: `' + x + '`.' ); 78 } 79 if ( isyv ) { 80 isyf64 = isFloat64Array( y.data ); 81 isyf32 = ( isyf64 ) ? false : isFloat32Array( y.data ); 82 } else if ( isya === false && isCollection( y ) ) { 83 isyf64 = isFloat64Array( y ); 84 isyf32 = ( isyf64 ) ? false : isFloat32Array( y ); 85 } else { 86 throw new TypeError( 'invalid argument. Second argument must be either an array-like object or a 1-dimensional ndarray. Value: `' + y + '`.' ); 87 } 88 if ( x.length !== y.length ) { 89 throw new RangeError( 'invalid argument. Arrays must be the same length. First argument length: ' + x.length + '. Second argument length: ' + y.length + '.' ); 90 } 91 if ( isxv && isyv ) { 92 if ( isxf64 && isyf64 ) { 93 dswap( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 94 return y; 95 } 96 if ( isxf32 && isyf32 ) { 97 sswap( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 98 return y; 99 } 100 swap( x.length, x.data, x.strides[ 0 ], x.offset, y.data, y.strides[ 0 ], y.offset ); // eslint-disable-line max-len 101 return y; 102 } 103 if ( isxv ) { 104 swap( x.length, x.data, x.strides[ 0 ], x.offset, y, 1, 0 ); 105 return y; 106 } 107 if ( isyv ) { 108 swap( x.length, x, 1, 0, y.data, y.strides[ 0 ], y.offset ); 109 return y; 110 } 111 swap( x.length, x, 1, 0, y, 1, 0 ); 112 return y; 113 } 114 115 116 // EXPORTS // 117 118 module.exports = gswap;