Skip to content

Commit 2e2f268

Browse files
committed
refactor: use blas/tools/swap-factory
1 parent c070a88 commit 2e2f268

File tree

1 file changed

+4
-91
lines changed
  • lib/node_modules/@stdlib/blas/dswap/lib

1 file changed

+4
-91
lines changed

lib/node_modules/@stdlib/blas/dswap/lib/main.js

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,17 @@
2020

2121
// MODULES //
2222

23-
var isFloat64ndarrayLike = require( '@stdlib/assert/is-float64ndarray-like' );
24-
var isNegativeInteger = require( '@stdlib/assert/is-negative-integer' ).isPrimitive;
25-
var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' );
26-
var hasEqualValues = require( '@stdlib/array/base/assert/has-equal-values-indexed' );
27-
var min = require( '@stdlib/math/base/special/fast/min' );
28-
var without = require( '@stdlib/array/base/without' );
29-
var ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' );
30-
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
31-
var nditerStacks = require( '@stdlib/ndarray/iter/stacks' );
32-
var numel = require( '@stdlib/ndarray/base/numel' );
3323
var base = require( '@stdlib/blas/base/dswap' ).ndarray;
34-
var format = require( '@stdlib/string/format' );
24+
var factory = require( '@stdlib/blas/tools/swap-factory' );
3525

3626

3727
// MAIN //
3828

3929
/**
4030
* Interchanges two double-precision floating-point vectors.
4131
*
32+
* @name dswap
33+
* @type {Function}
4234
* @param {ndarrayLike} x - first input array
4335
* @param {ndarrayLike} y - second input array
4436
* @param {NegativeInteger} [dim] - dimension along which to interchange elements
@@ -66,86 +58,7 @@ var format = require( '@stdlib/string/format' );
6658
* var ybuf = y.data;
6759
* // returns <Float64Array>[ 4.0, 2.0, -3.0, 5.0, -1.0 ]
6860
*/
69-
function dswap( x, y ) {
70-
var dim;
71-
var xsh;
72-
var ysh;
73-
var xit;
74-
var yit;
75-
var xc;
76-
var yc;
77-
var vx;
78-
var vy;
79-
var dm;
80-
var S;
81-
var N;
82-
var i;
83-
if ( !isFloat64ndarrayLike( x ) ) {
84-
throw new TypeError( format( 'invalid argument. First argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', x ) );
85-
}
86-
if ( !isFloat64ndarrayLike( y ) ) {
87-
throw new TypeError( format( 'invalid argument. Second argument must be an ndarray containing double-precision floating-point numbers. Value: `%s`.', y ) );
88-
}
89-
if ( isReadOnly( x ) || isReadOnly( y ) ) {
90-
throw new Error( 'invalid argument. Cannot write to read-only array.' );
91-
}
92-
// Convert the input arrays to "base" ndarrays:
93-
xc = ndarraylike2ndarray( x );
94-
yc = ndarraylike2ndarray( y );
95-
96-
// Resolve the input array shapes:
97-
xsh = xc.shape;
98-
ysh = yc.shape;
99-
100-
// Validate that we've been provided non-zero-dimensional arrays...
101-
if ( xsh.length < 1 ) {
102-
throw new TypeError( format( 'invalid argument. First argument must have at least one dimension.' ) );
103-
}
104-
if ( ysh.length < 1 ) {
105-
throw new TypeError( format( 'invalid argument. Second argument must have at least one dimension.' ) );
106-
}
107-
// Validate that the arrays have the same shape...
108-
if ( !hasEqualValues( xsh, ysh ) ) {
109-
throw new Error( 'invalid arguments. The first and second arguments must have the same shape.' );
110-
}
111-
// Validate that the dimension argument is a negative integer...
112-
if ( arguments.length > 2 ) {
113-
dim = arguments[ 2 ];
114-
if ( !isNegativeInteger( dim ) ) {
115-
throw new TypeError( format( 'invalid argument. Third argument must be a negative integer. Value: `%s`.', dim ) );
116-
}
117-
} else {
118-
dim = -1;
119-
}
120-
// Validate that a provided dimension index is within bounds...
121-
dm = min( xsh.length, ysh.length ) - 1;
122-
dim = normalizeIndex( dim, dm );
123-
if ( dim === -1 ) {
124-
throw new RangeError( format( 'invalid argument. Third argument must be a value on the interval: [%d,%d]. Value: `%d`.', -dm, -1, arguments[ 2 ] ) );
125-
}
126-
// Resolve the size of the interchange dimension:
127-
S = xsh[ dim ];
128-
129-
// If we are only provided one-dimensional input arrays, we can skip iterating over stacks...
130-
if ( xsh.length === 1 ) {
131-
base( S, xc.data, xc.strides[0], xc.offset, yc.data, yc.strides[0], yc.offset ); // eslint-disable-line max-len
132-
return y;
133-
}
134-
// Resolve the number of stacks:
135-
N = numel( without( xsh, dim ) );
136-
137-
// Create iterators for iterating over stacks of vectors:
138-
xit = nditerStacks( xc, [ dim ] );
139-
yit = nditerStacks( yc, [ dim ] );
140-
141-
// Interchange each pair of vectors...
142-
for ( i = 0; i < N; i++ ) {
143-
vx = xit.next().value;
144-
vy = yit.next().value;
145-
base( S, vx.data, vx.strides[0], vx.offset, vy.data, vy.strides[0], vy.offset ); // eslint-disable-line max-len
146-
}
147-
return y;
148-
}
61+
var dswap = factory( base, 'float64' );
14962

15063

15164
// EXPORTS //

0 commit comments

Comments
 (0)