Skip to content

Commit 1cd4c55

Browse files
committed
refactor: apply suggestions & add test cases
1 parent 5ae1975 commit 1cd4c55

File tree

3 files changed

+197
-43
lines changed

3 files changed

+197
-43
lines changed

lib/node_modules/@stdlib/ndarray/map/docs/repl.txt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,5 @@
4747
> y.data
4848
<Float64Array>[ 10.0, 20.0, 30.0, 40.0 ]
4949

50-
// Using options...
51-
> var buffer = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
52-
> var dtype = 'float64';
53-
> var shape = [ 2, 2 ];
54-
> var strides = [ 2, 1 ];
55-
> var offset = 0;
56-
> var order = 'row-major';
57-
58-
// Define a callback function:
59-
> function f( v ) { return v*10.0; };
60-
61-
// Define the options:
62-
> var opts = { 'dtype': 'float32' };
63-
64-
// Using ndarray...
65-
> var x = {{alias:@stdlib/ndarray/ctor}}( dtype, buffer, shape, strides, offset, order );
66-
> var y = {{alias}}( x, opts, f );
67-
> y.data
68-
<Float32Array>[ 10.0, 20.0, 30.0, 40.0 ]
69-
7050
See Also
7151
--------

lib/node_modules/@stdlib/ndarray/map/lib/main.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ var format = require( '@stdlib/string/format' );
3939
* @throws {TypeError} first argument must have a recognized data type
4040
* @throws {TypeError} callback argument must be a function
4141
* @throws {TypeError} options argument must be an object
42-
* @throws {TypeError} `dtype` option must be a supported ndarray data type
4342
* @returns {ndarray} output ndarray
4443
*
4544
* @example

lib/node_modules/@stdlib/ndarray/map/test/test.js

Lines changed: 197 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
2727
var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
2828
var Float64Array = require( '@stdlib/array/float64' );
2929
var Float32Array = require( '@stdlib/array/float32' );
30-
var Complex128Array = require( '@stdlib/array/complex128' );
31-
var Complex128 = require( '@stdlib/complex/float64/ctor' );
3230
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
3331
var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' );
34-
var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' );
3532
var numel = require( '@stdlib/ndarray/base/numel' );
33+
var dfill = require( '@stdlib/blas/ext/base/dfill' );
34+
var sfill = require( '@stdlib/blas/ext/base/sfill' );
35+
var blockSize = require( '@stdlib/ndarray/base/unary-tiling-block-size' );
3636
var map = require( './../lib' );
3737

3838

@@ -79,7 +79,7 @@ tape( 'the function throws an error if provided a first argument having an unrec
7979
}
8080
});
8181

82-
tape( 'the function throws an error if the second argument is not a function when options are not provided', function test( t ) {
82+
tape( 'the function throws an error if the second argument is not a function', function test( t ) {
8383
var values;
8484
var x;
8585
var i;
@@ -108,7 +108,7 @@ tape( 'the function throws an error if the second argument is not a function whe
108108
}
109109
});
110110

111-
tape( 'the function throws an error if third argument is not a function when options are provided', function test( t ) {
111+
tape( 'the function throws an error if third argument is not a function (options)', function test( t ) {
112112
var values;
113113
var opts;
114114
var x;
@@ -174,7 +174,7 @@ tape( 'the function throws an error if provided an options argument which is not
174174
}
175175
});
176176

177-
tape( 'the function throws an error if provided an options argument which does not have a `dtype` property', function test( t ) {
177+
tape( 'the function throws an error if the provided options argument does not have a `dtype` property', function test( t ) {
178178
var values;
179179
var x;
180180
var i;
@@ -208,7 +208,7 @@ tape( 'the function throws an error if provided an options argument which does n
208208
}
209209
});
210210

211-
tape( 'the function applies a callback to each indexed element in an input 3-dimensional ndarray and returns an output 3-dimensional ndarray with mapped values (row-major, contiguous)', function test( t ) {
211+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, contiguous)', function test( t ) {
212212
var expected;
213213
var ord;
214214
var sh;
@@ -243,7 +243,42 @@ tape( 'the function applies a callback to each indexed element in an input 3-dim
243243
}
244244
});
245245

246-
tape( 'the function applies a callback to each indexed element in an input 3-dimensional ndarray and returns an output 3-dimensional ndarray with mapped values, using the provided options (row-major, contiguous)', function test( t ) {
246+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, contiguous)', function test( t ) {
247+
var expected;
248+
var ord;
249+
var sh;
250+
var st;
251+
var dt;
252+
var o;
253+
var x;
254+
var y;
255+
256+
dt = 'float64';
257+
ord = 'column-major';
258+
sh = [ 2, 1, 2 ];
259+
st = shape2strides( sh, ord );
260+
o = strides2offset( sh, st );
261+
262+
x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord );
263+
264+
y = map( x, scale );
265+
266+
expected = new Float64Array([
267+
10.0,
268+
10.0,
269+
10.0,
270+
10.0
271+
]);
272+
t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' );
273+
274+
t.end();
275+
276+
function scale( z ) {
277+
return z * 10.0;
278+
}
279+
});
280+
281+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, contiguous, options)', function test( t ) {
247282
var expected;
248283
var opts;
249284
var ord;
@@ -281,7 +316,7 @@ tape( 'the function applies a callback to each indexed element in an input 3-dim
281316
}
282317
});
283318

284-
tape( 'the function applies a callback to each indexed element in an input 3-dimensional ndarray and returns an output 3-dimensional ndarray with mapped values, using the provided options (row-major, singleton dimensions, accessors)', function test( t ) {
319+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, contiguous, options)', function test( t ) {
285320
var expected;
286321
var opts;
287322
var ord;
@@ -292,35 +327,175 @@ tape( 'the function applies a callback to each indexed element in an input 3-dim
292327
var x;
293328
var y;
294329

295-
dt = 'generic';
296-
ord = 'row-major';
297-
sh = [ 4, 1, 1 ];
330+
dt = 'float64';
331+
ord = 'column-major';
332+
sh = [ 2, 1, 2 ];
298333
st = shape2strides( sh, ord );
299334
o = strides2offset( sh, st );
300-
301335
x = ndarray( dt, ones( numel( sh ), dt ), sh, st, o, ord );
302336

303337
opts = {
304-
'dtype': 'complex128'
338+
'dtype': 'float32'
305339
};
306-
307340
y = map( x, opts, scale );
308341

309-
expected = new Complex128Array([
310-
10.0,
311-
10.0,
312-
10.0,
313-
10.0,
342+
expected = new Float32Array([
314343
10.0,
315344
10.0,
316345
10.0,
317346
10.0
318347
]);
319-
t.strictEqual( isSameComplex128Array( y.data, expected ), true, 'returns expected value' );
348+
t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' );
349+
350+
t.end();
351+
352+
function scale( z ) {
353+
return z * 10.0;
354+
}
355+
});
356+
357+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, non-contiguous, large arrays)', function test( t ) {
358+
var expected;
359+
var bsize;
360+
var ord;
361+
var sh;
362+
var st;
363+
var dt;
364+
var o;
365+
var x;
366+
var y;
367+
368+
dt = 'float64';
369+
ord = 'row-major';
370+
371+
bsize = blockSize( dt );
372+
sh = [ bsize*2, 1, 2 ];
373+
st = [ -4, -4, 2 ];
374+
o = strides2offset( sh, st );
375+
376+
x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord );
377+
378+
y = map( x, scale );
379+
expected = new Float64Array( y.length );
380+
dfill( y.length, 10.0, expected, 1 );
381+
382+
t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' );
383+
384+
t.end();
385+
386+
function scale( z ) {
387+
return z * 10.0;
388+
}
389+
});
390+
391+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (row-major, non-contiguous, large arrays, options)', function test( t ) {
392+
var expected;
393+
var bsize;
394+
var opts;
395+
var ord;
396+
var sh;
397+
var st;
398+
var dt;
399+
var o;
400+
var x;
401+
var y;
402+
403+
dt = 'float64';
404+
ord = 'row-major';
405+
406+
bsize = blockSize( dt );
407+
sh = [ bsize*2, 1, 2 ];
408+
st = [ -4, -4, 2 ];
409+
o = strides2offset( sh, st );
410+
411+
x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord );
412+
413+
opts = {
414+
'dtype': 'float32'
415+
};
416+
y = map( x, opts, scale );
417+
expected = new Float32Array( y.length );
418+
sfill( y.length, 10.0, expected, 1 );
419+
420+
t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' );
320421

321422
t.end();
322423

323424
function scale( z ) {
324-
return new Complex128( z*10.0, z*10.0 );
425+
return z * 10.0;
426+
}
427+
});
428+
429+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, non-contiguous, large arrays)', function test( t ) {
430+
var expected;
431+
var bsize;
432+
var ord;
433+
var sh;
434+
var st;
435+
var dt;
436+
var o;
437+
var x;
438+
var y;
439+
440+
dt = 'float64';
441+
ord = 'column-major';
442+
443+
bsize = blockSize( dt );
444+
sh = [ 2, 1, bsize*2 ];
445+
st = [ 2, -4, -4 ];
446+
o = strides2offset( sh, st );
447+
448+
x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord );
449+
450+
y = map( x, scale );
451+
452+
expected = new Float64Array( y.length );
453+
dfill( y.length, 10.0, expected, 1 );
454+
455+
t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' );
456+
457+
t.end();
458+
459+
function scale( z ) {
460+
return z * 10.0;
461+
}
462+
});
463+
464+
tape( 'the function applies a callback to each indexed element in an input ndarray and returns an output ndarray with mapped values (column-major, non-contiguous, large arrays, options)', function test( t ) {
465+
var expected;
466+
var bsize;
467+
var opts;
468+
var ord;
469+
var sh;
470+
var st;
471+
var dt;
472+
var o;
473+
var x;
474+
var y;
475+
476+
dt = 'float64';
477+
ord = 'column-major';
478+
479+
bsize = blockSize( dt );
480+
sh = [ 2, 1, bsize*2 ];
481+
st = [ 2, -4, -4 ];
482+
o = strides2offset( sh, st );
483+
484+
x = ndarray( dt, ones( numel( sh )*2, dt ), sh, st, o, ord );
485+
486+
opts = {
487+
'dtype': 'float32'
488+
};
489+
y = map( x, opts, scale );
490+
491+
expected = new Float32Array( y.length );
492+
sfill( y.length, 10.0, expected, 1 );
493+
494+
t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' );
495+
496+
t.end();
497+
498+
function scale( z ) {
499+
return z * 10.0;
325500
}
326501
});

0 commit comments

Comments
 (0)