diff --git a/.editorconfig b/.editorconfig index 0fd4d6c8..13e9c391 100644 --- a/.editorconfig +++ b/.editorconfig @@ -179,3 +179,8 @@ indent_size = 2 [*.gypi] indent_style = space indent_size = 2 + +# Set properties for citation files: +[*.{cff,cff.txt}] +indent_style = space +indent_size = 2 diff --git a/CITATION.cff b/CITATION.cff new file mode 100644 index 00000000..2ec6fffa --- /dev/null +++ b/CITATION.cff @@ -0,0 +1,30 @@ +cff-version: 1.2.0 +title: stdlib +message: >- + If you use this software, please cite it using the + metadata from this file. + +type: software + +authors: + - name: The Stdlib Authors + url: https://github.com/stdlib-js/stdlib/graphs/contributors + +repository-code: https://github.com/stdlib-js/stdlib +url: https://stdlib.io + +abstract: | + Standard library for JavaScript and Node.js. + +keywords: + - JavaScript + - Node.js + - TypeScript + - standard library + - scientific computing + - numerical computing + - statistical computing + +license: Apache-2.0 AND BSL-1.0 + +date-released: 2016 diff --git a/README.md b/README.md index 9bba6c59..d1ca0e50 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,17 @@ limitations under the License. --> + +
+ + About stdlib... + +

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

+

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

+

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

+

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

+
+ # ndarray [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] diff --git a/array/docs/types/index.d.ts b/array/docs/types/index.d.ts index 06671563..a20c23a1 100644 --- a/array/docs/types/index.d.ts +++ b/array/docs/types/index.d.ts @@ -21,7 +21,7 @@ /// import { ArrayLike } from '@stdlib/types/array'; -import { DataType, ndarray, Mode, Order, Shape } from '@stdlib/types/ndarray'; +import { DataType, typedndarray, Mode, Order, Shape } from '@stdlib/types/ndarray'; /** * Interface defining function options. @@ -164,7 +164,7 @@ interface ExtendedOptions extends Options { * var v = arr.get( 0 ); * // returns [ 1, 2 ] */ -declare function array( options: OptionsWithShape | OptionsWithBuffer ): ndarray; // tslint:disable-line:max-line-length +declare function array( options: OptionsWithShape | OptionsWithBuffer ): typedndarray; // tslint:disable-line:no-unnecessary-generics /** * Returns a multidimensional array. @@ -220,7 +220,7 @@ declare function array( options: OptionsWithShape | OptionsWithBuffer ): ndarray * var v = arr.get( 0, 0 ); * // returns 1.0 */ -declare function array( buffer: ArrayLike, options?: ExtendedOptions ): ndarray; // tslint:disable-line:max-line-length +declare function array( buffer: ArrayLike, options?: ExtendedOptions ): typedndarray; // tslint:disable-line:no-unnecessary-generics // EXPORTS // diff --git a/array/docs/types/test.ts b/array/docs/types/test.ts index 042a95cb..b58b3d7c 100644 --- a/array/docs/types/test.ts +++ b/array/docs/types/test.ts @@ -23,10 +23,10 @@ import array = require( './index' ); // The function returns an ndarray... { - array( [ [ 1, 2 ], [ 3, 4 ] ] ); // $ExpectType ndarray - array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), { 'shape': [ 2, 2 ] } ); // $ExpectType ndarray - array( { 'shape': [ 2, 2 ] } ); // $ExpectType ndarray - array( { 'buffer': [ [ 1, 2 ], [ 3, 4 ] ] } ); // $ExpectType ndarray + array( [ [ 1, 2 ], [ 3, 4 ] ] ); // $ExpectType typedndarray + array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), { 'shape': [ 2, 2 ] } ); // $ExpectType typedndarray + array( { 'shape': [ 2, 2 ] } ); // $ExpectType typedndarray + array( { 'buffer': [ [ 1, 2 ], [ 3, 4 ] ] } ); // $ExpectType typedndarray } // The compiler throws an error if the function is provided a first argument which is not an array, buffer, or options object... @@ -41,6 +41,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a second argument which is not an options object... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, 'abc' ); // $ExpectError array( buffer, true ); // $ExpectError array( buffer, false ); // $ExpectError @@ -52,6 +53,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `dtype` option which is not a recognized data type... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'dtype': 'abc' } ); // $ExpectError array( buffer, { 'dtype': 123 } ); // $ExpectError array( buffer, { 'dtype': true } ); // $ExpectError @@ -74,6 +76,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided an `order` option which is not a recognized order... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'order': 'abc' } ); // $ExpectError array( buffer, { 'order': 123 } ); // $ExpectError array( buffer, { 'order': true } ); // $ExpectError @@ -96,6 +99,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `shape` option which is not an array-like object containing numbers... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'shape': 'abc' } ); // $ExpectError array( buffer, { 'shape': 123 } ); // $ExpectError array( buffer, { 'shape': true } ); // $ExpectError @@ -116,6 +120,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `mode` option which is not a recognized mode... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'mode': 'abc' } ); // $ExpectError array( buffer, { 'mode': 123 } ); // $ExpectError array( buffer, { 'mode': true } ); // $ExpectError @@ -138,6 +143,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided an `submode` option which is not an array of strings... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'submode': 'abc' } ); // $ExpectError array( buffer, { 'submode': 123 } ); // $ExpectError array( buffer, { 'submode': true } ); // $ExpectError @@ -158,6 +164,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `copy` option which is not a boolean... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'copy': 'abc' } ); // $ExpectError array( buffer, { 'copy': 123 } ); // $ExpectError array( buffer, { 'copy': null } ); // $ExpectError @@ -176,6 +183,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `flatten` option which is not a boolean... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'flatten': 'abc' } ); // $ExpectError array( buffer, { 'flatten': 123 } ); // $ExpectError array( buffer, { 'flatten': null } ); // $ExpectError @@ -194,6 +202,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `ndmin` option which is not a number... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'ndmin': 'abc' } ); // $ExpectError array( buffer, { 'ndmin': false } ); // $ExpectError array( buffer, { 'ndmin': true } ); // $ExpectError @@ -214,6 +223,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `casting` option which is not a string... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'casting': 123 } ); // $ExpectError array( buffer, { 'casting': false } ); // $ExpectError array( buffer, { 'casting': true } ); // $ExpectError @@ -234,6 +244,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided a `readonly` option which is not a boolean... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array( buffer, { 'readonly': 'abc' } ); // $ExpectError array( buffer, { 'readonly': 123 } ); // $ExpectError array( buffer, { 'readonly': null } ); // $ExpectError @@ -252,6 +263,7 @@ import array = require( './index' ); // The compiler throws an error if the function is provided an invalid number of arguments... { const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + array(); // $ExpectError array( buffer, {}, {} ); // $ExpectError } diff --git a/ctor/benchmark/benchmark.attributes.js b/ctor/benchmark/benchmark.attributes.js new file mode 100644 index 00000000..0b57f0b2 --- /dev/null +++ b/ctor/benchmark/benchmark.attributes.js @@ -0,0 +1,655 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::get:byteLength', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.byteLength; + if ( v !== null ) { + b.fail( 'should return null' ); + } + } + b.toc(); + if ( v !== null ) { + b.fail( 'should return null' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_byteLength', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._byteLength; // eslint-disable-line no-underscore-dangle + if ( v !== null ) { + b.fail( 'should return null' ); + } + } + b.toc(); + if ( v !== null ) { + b.fail( 'should return null' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.BYTES_PER_ELEMENT; + if ( v !== null ) { + b.fail( 'should return null' ); + } + } + b.toc(); + if ( v !== null ) { + b.fail( 'should return null' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:data', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.data; + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_buffer', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._buffer; // eslint-disable-line no-underscore-dangle + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:dtype', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.dtype; + if ( v !== 'generic' ) { + b.fail( 'should return expected data type' ); + } + } + b.toc(); + if ( v !== 'generic' ) { + b.fail( 'should return expected data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:flags', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.flags; + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_flags', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._flags; // eslint-disable-line no-underscore-dangle + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:length', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.length; + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_length', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._length; // eslint-disable-line no-underscore-dangle + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:ndims', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.ndims; + if ( v !== shape.length ) { + b.fail( 'should return expected number of dimensions' ); + } + } + b.toc(); + if ( v !== shape.length ) { + b.fail( 'should return expected number of dimensions' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:offset', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.offset; + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + } + b.toc(); + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_offset', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._offset; // eslint-disable-line no-underscore-dangle + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + } + b.toc(); + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:order', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.order; + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + } + b.toc(); + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_order', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._order; // eslint-disable-line no-underscore-dangle + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + } + b.toc(); + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:shape', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.shape; + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_shape', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._shape; // eslint-disable-line no-underscore-dangle + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:strides', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.strides; + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:_strides', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out._strides; // eslint-disable-line no-underscore-dangle + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.get.js b/ctor/benchmark/benchmark.get.js new file mode 100644 index 00000000..8ccb9c33 --- /dev/null +++ b/ctor/benchmark/benchmark.get.js @@ -0,0 +1,588 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%20)-10 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%20)-10 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.iget.js b/ctor/benchmark/benchmark.iget.js new file mode 100644 index 00000000..1cbc37e2 --- /dev/null +++ b/ctor/benchmark/benchmark.iget.js @@ -0,0 +1,344 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:iget', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iget:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iget:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.instantiation.js b/ctor/benchmark/benchmark.instantiation.js new file mode 100644 index 00000000..ce1dc31a --- /dev/null +++ b/ctor/benchmark/benchmark.instantiation.js @@ -0,0 +1,338 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.iset.js b/ctor/benchmark/benchmark.iset.js new file mode 100644 index 00000000..5f781d09 --- /dev/null +++ b/ctor/benchmark/benchmark.iset.js @@ -0,0 +1,364 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:iset', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iset:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.iset( j, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.iget( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iset:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.iset( j, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.iget( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ 5-j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ 5-j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ 5-j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ 5-j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ 0 ] !== buffer[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( out.iget( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.iset( j, v ); + if ( buffer[ 0 ] !== buffer[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( out.iget( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.js b/ctor/benchmark/benchmark.js deleted file mode 100644 index dacc68d2..00000000 --- a/ctor/benchmark/benchmark.js +++ /dev/null @@ -1,2785 +0,0 @@ -/** -* @license Apache-2.0 -* -* Copyright (c) 2018 The Stdlib Authors. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -'use strict'; - -// MODULES // - -var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var floor = require( '@stdlib/math/base/special/floor' ); -var pkg = require( './../package.json' ).name; -var ndarray = require( './../lib' ); - - -// MAIN // - -bench( pkg+'::2d,instantiation', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,instantiation,new', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = new ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d,instantiation', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d,instantiation,new', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = new ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d,instantiation', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d,instantiation,new', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = new ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d,instantiation', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d,instantiation,new', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 0 ] = randu(); - out = new ndarray( 'generic', buffer, shape, strides, offset, order ); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - } - b.toc(); - if ( out.length === 0 ) { - b.fail( 'should have a length greater than 0' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:byteLength', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.byteLength; - if ( v !== null ) { - b.fail( 'should return null' ); - } - } - b.toc(); - if ( v !== null ) { - b.fail( 'should return null' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_byteLength', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._byteLength; // eslint-disable-line no-underscore-dangle - if ( v !== null ) { - b.fail( 'should return null' ); - } - } - b.toc(); - if ( v !== null ) { - b.fail( 'should return null' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.BYTES_PER_ELEMENT; - if ( v !== null ) { - b.fail( 'should return null' ); - } - } - b.toc(); - if ( v !== null ) { - b.fail( 'should return null' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:data', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.data; - if ( v.length !== 6 ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== 6 ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_buffer', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._buffer; // eslint-disable-line no-underscore-dangle - if ( v.length !== 6 ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== 6 ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:dtype', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.dtype; - if ( v !== 'generic' ) { - b.fail( 'should return expected data type' ); - } - } - b.toc(); - if ( v !== 'generic' ) { - b.fail( 'should return expected data type' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:flags', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.flags; - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_flags', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._flags; // eslint-disable-line no-underscore-dangle - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:length', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.length; - if ( v !== buffer.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v !== buffer.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_length', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._length; // eslint-disable-line no-underscore-dangle - if ( v !== buffer.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v !== buffer.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:ndims', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.ndims; - if ( v !== shape.length ) { - b.fail( 'should return expected number of dimensions' ); - } - } - b.toc(); - if ( v !== shape.length ) { - b.fail( 'should return expected number of dimensions' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:offset', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.offset; - if ( v !== offset ) { - b.fail( 'should return expected offset' ); - } - } - b.toc(); - if ( v !== offset ) { - b.fail( 'should return expected offset' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_offset', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._offset; // eslint-disable-line no-underscore-dangle - if ( v !== offset ) { - b.fail( 'should return expected offset' ); - } - } - b.toc(); - if ( v !== offset ) { - b.fail( 'should return expected offset' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:order', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.order; - if ( v !== order ) { - b.fail( 'should return expected order' ); - } - } - b.toc(); - if ( v !== order ) { - b.fail( 'should return expected order' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_order', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._order; // eslint-disable-line no-underscore-dangle - if ( v !== order ) { - b.fail( 'should return expected order' ); - } - } - b.toc(); - if ( v !== order ) { - b.fail( 'should return expected order' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:shape', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.shape; - if ( v.length !== shape.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== shape.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_shape', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._shape; // eslint-disable-line no-underscore-dangle - if ( v.length !== shape.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== shape.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:strides', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out.strides; - if ( v.length !== strides.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== strides.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::get:_strides', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = out._strides; // eslint-disable-line no-underscore-dangle - if ( v.length !== strides.length ) { - b.fail( 'should return expected length' ); - } - } - b.toc(); - if ( v.length !== strides.length ) { - b.fail( 'should return expected length' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:get', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:get:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*20.0 ) - 10.0 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:get:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*20.0 ) - 10.0 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:get', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*3.0 ), 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:get:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:get:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:get', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, floor( randu()*3.0 ), 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:get:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:get:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:get', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, floor( randu()*3.0 ), 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:get:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:get:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:get', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, 0, floor( randu()*3.0 ), 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:get:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:get:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.get( 0, 0, 0, floor( randu()*30.0 ) - 15.0, 1 ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iget', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iget:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iget:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var v; - var i; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_positive_strides:iget:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_positive_strides:iget:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_negative_strides:iget:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, -1 ]; - offset = 5; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_negative_strides:iget:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, -1 ]; - offset = 5; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,mixed_sign_strides:iget:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, 1 ]; - offset = 4; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,mixed_sign_strides:iget:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, 1 ]; - offset = 4; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.iget( floor( randu()*6.0 ) ); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - } - b.toc(); - if ( v !== v ) { - b.fail( 'should not return NaN' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:set', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.set( j, v ); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:set:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*20.0 ) - 10.0; - tmp = out.set( j, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:set:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*20.0 ) - 10.0; - tmp = out.set( j, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:set', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*3.0 ); - out.set( j, 1, v ); - if ( buffer[ (2*j) + (1*1) ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ (2*j) + 1 ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:set:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d:set:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:set', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*3.0 ); - out.set( 0, j, 1, v ); - if ( buffer[ (2*j) + (1*1) ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ (2*j) + 1 ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:set:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::3d:set:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 3, 2 ]; - strides = [ 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:set', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*3.0 ); - out.set( 0, 0, j, 1, v ); - if ( buffer[ (2*j) + (1*1) ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ (2*j) + 1 ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:set:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::4d:set:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 3, 2 ]; - strides = [ 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:set', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*3.0 ); - out.set( 0, 0, 0, j, 1, v ); - if ( buffer[ (2*j) + (1*1) ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ (2*j) + 1 ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:set:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, 0, 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, 0, 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::5d:set:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 1, 1, 1, 3, 2 ]; - strides = [ 6, 6, 6, 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.set( 0, 0, 0, j, 1, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.get( 0, 0, 0, j, 1 ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iset', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iset:mode=wrap', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'wrap' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.iset( j, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.iget( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::1d:iset:mode=clamp', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var opts; - var out; - var tmp; - var v; - var i; - var j; - - opts = { - 'mode': 'clamp' - }; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 6 ]; - strides = [ 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*30.0 ) - 15.0; - tmp = out.iset( j, v ); - if ( typeof tmp !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( out.iget( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_positive_strides:iset:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_positive_strides:iset:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_negative_strides:iset:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, -1 ]; - offset = 5; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ 5-j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ 5-j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,all_negative_strides:iset:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, -1 ]; - offset = 5; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ 5-j ] !== v ) { - b.fail( 'should set value' ); - } - } - b.toc(); - if ( buffer[ 5-j ] !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,mixed_sign_strides:iset:order=row-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, 1 ]; - offset = 4; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ 0 ] !== buffer[ 0 ] ) { - b.fail( 'should not be NaN' ); - } - } - b.toc(); - if ( out.iget( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+'::2d,mixed_sign_strides:iset:order=column-major', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - var j; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ -2, 1 ]; - offset = 4; - order = 'column-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - v = randu(); - j = floor( randu()*6.0 ); - out.iset( j, v ); - if ( buffer[ 0 ] !== buffer[ 0 ] ) { - b.fail( 'should not be NaN' ); - } - } - b.toc(); - if ( out.iget( j ) !== v ) { - b.fail( 'should set value' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':toJSON', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.toJSON(); - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - } - b.toc(); - if ( typeof v !== 'object' ) { - b.fail( 'should return an object' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); - -bench( pkg+':toString', function benchmark( b ) { - var strides; - var buffer; - var offset; - var shape; - var order; - var out; - var v; - var i; - - buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; - shape = [ 3, 2 ]; - strides = [ 2, 1 ]; - offset = 0; - order = 'row-major'; - - out = ndarray( 'generic', buffer, shape, strides, offset, order ); - - b.tic(); - for ( i = 0; i < b.iterations; i++ ) { - buffer[ 1 ] = randu(); - v = out.toString(); - if ( typeof v !== 'string' ) { - b.fail( 'should return a string' ); - } - } - b.toc(); - if ( typeof v !== 'string' ) { - b.fail( 'should return a string' ); - } - b.pass( 'benchmark finished' ); - b.end(); -}); diff --git a/ctor/benchmark/benchmark.set.js b/ctor/benchmark/benchmark.set.js new file mode 100644 index 00000000..c7cb30f0 --- /dev/null +++ b/ctor/benchmark/benchmark.set.js @@ -0,0 +1,628 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 6; + out.set( j, v ); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%20) - 10; + tmp = out.set( j, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%20) - 10; + tmp = out.set( j, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 3; + out.set( j, 1, v ); + if ( buffer[ (2*j) + (1*1) ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 3; + out.set( 0, j, 1, v ); + if ( buffer[ (2*j) + (1*1) ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 3; + out.set( 0, 0, j, 1, v ); + if ( buffer[ (2*j) + (1*1) ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = i % 3; + out.set( 0, 0, 0, j, 1, v ); + if ( buffer[ (2*j) + (1*1) ] !== v ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, 0, 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var v; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = i; + j = (i%30) - 15; + tmp = out.set( 0, 0, 0, j, 1, v ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, 0, j, 1 ) !== v ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.to_json.js b/ctor/benchmark/benchmark.to_json.js new file mode 100644 index 00000000..0a7a508b --- /dev/null +++ b/ctor/benchmark/benchmark.to_json.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toJSON', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.toJSON(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/ctor/benchmark/benchmark.to_string.js b/ctor/benchmark/benchmark.to_string.js new file mode 100644 index 00000000..05582568 --- /dev/null +++ b/ctor/benchmark/benchmark.to_string.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toString', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.toString(); + if ( typeof v !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof v !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/README.md b/fancy/README.md new file mode 100644 index 00000000..6a93e088 --- /dev/null +++ b/fancy/README.md @@ -0,0 +1,788 @@ + + + + +# FancyArray + +> Fancy multidimensional array constructor. + + + +
+ +A **FancyArray** is an [`ndarray`][@stdlib/ndarray/ctor] which supports slicing via indexing expressions. + +```javascript +var FancyArray = require( '@stdlib/ndarray/fancy' ); + +var buffer = [ 1, 2, 3, 4, 5, 6 ]; +var x = new FancyArray( 'generic', buffer, [ 6 ], [ 1 ], 0, 'row-major' ); +// returns + +// Select the first 3 elements: +var y = x[ ':3' ]; +// returns + +var arr = toArray( y ); +// returns [ 1, 2, 3 ] + +// Select every other element, starting with the second element: +y = x[ '1::2' ]; +// returns + +arr = toArray( y ); +// returns [ 2, 4, 6 ] + +// Reverse the array, starting with last element and skipping every other element: +y = x[ '::-2' ]; +// returns + +arr = toArray( y ); +// returns [ 6, 4, 2 ] +``` + +
+ + + + + +
+ +## Usage + +```javascript +var FancyArray = require( '@stdlib/ndarray/fancy' ); +``` + + + +#### new FancyArray( dtype, buffer, shape, strides, offset, order\[, options] ) + +Returns a `FancyArray` instance. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); +// returns +``` + +The constructor expects the following arguments: + +- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. +- **buffer**: data buffer. +- **shape**: array shape (dimensions). +- **strides**: array strides which are index offsets specifying how to access along corresponding dimensions. +- **offset**: index offset specifying the location of the first indexed element in the data buffer. +- **order**: array order, which is either `row-major` (C-style) or `column-major` (Fortran-style). + +The constructor accepts the following `options`: + +- **mode**: specifies how to handle indices which exceed array dimensions. Default: `'throw'`. +- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the constructor recycles modes using modulo arithmetic. Default: `[ options.mode ]`. +- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`. + +The constructor supports the following `modes`: + +- **throw**: specifies that a `FancyArray` instance should throw an error when an index exceeds array dimensions. +- **wrap**: specifies that a `FancyArray` instance should wrap around an index exceeding array dimensions using modulo arithmetic. +- **clamp**: specifies that a `FancyArray` instance should set an index exceeding array dimensions to either `0` (minimum index) or the maximum index. + +By default, a `FancyArray` instance **throws** when provided an index which exceeds array dimensions. To support alternative indexing behavior, set the `mode` option, which will affect all public **methods** (but **not** slicing semantics) for getting and setting array elements. + +```javascript +var opts = { + 'mode': 'clamp' +}; + +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order, opts ); +// returns + +// Attempt to access an out-of-bounds linear index (clamped): +var v = arr.iget( 10 ); +// returns 4.0 +``` + +By default, the `mode` option is applied to subscripts which exceed array dimensions. To specify behavior for each dimension, set the `submode` option. + +```javascript +var opts = { + 'submode': [ 'wrap', 'clamp' ] +}; + +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; +var shape = [ 2, 2, 2 ]; +var order = 'row-major'; +var strides = [ 4, 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order, opts ); +// returns + +// Attempt to access out-of-bounds subscripts: +var v = arr.get( -2, 10, -1 ); // linear index: 3 +// returns 4.0 +``` + +* * * + +### Properties + + + +#### FancyArray.name + +String value of the constructor name. + +```javascript +var str = FancyArray.name; +// returns 'ndarray' +``` + + + +#### FancyArray.prototype.byteLength + +Size (in bytes) of the array (if known). + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +// Specify the array configuration: +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'float64', buffer, shape, strides, offset, order ); + +// Get the byte length: +var nbytes = arr.byteLength; +// returns 32 +``` + +If unable to determine the size of the array, the property value is `null`. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the byte length: +var nbytes = arr.byteLength; +// returns null +``` + + + +#### FancyArray.prototype.BYTES_PER_ELEMENT + +Size (in bytes) of each array element (if known). + +```javascript +var Float32Array = require( '@stdlib/array/float32' ); + +// Specify the array configuration: +var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'float32', buffer, shape, strides, offset, order ); + +// Get the number of bytes per element: +var nbytes = arr.BYTES_PER_ELEMENT; +// returns 4 +``` + +If size of each array element is unknown, the property value is `null`. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the number of bytes per element: +var nbytes = arr.BYTES_PER_ELEMENT; +// returns null +``` + + + +#### FancyArray.prototype.data + +A reference to the underlying data buffer. + +```javascript +var Int8Array = require( '@stdlib/array/int8' ); + +// Specify the array configuration: +var buffer = new Int8Array( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'int8', buffer, shape, strides, offset, order ); + +// Get the buffer reference: +var d = arr.data; +// returns [ 1, 2, 3, 4 ] + +var bool = ( d === buffer ); +// returns true +``` + + + +#### FancyArray.prototype.dtype + +Underlying [data type][@stdlib/ndarray/dtypes]. + +```javascript +var Uint8Array = require( '@stdlib/array/uint8' ); + +// Specify the array configuration: +var buffer = new Uint8Array( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ -2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'uint8', buffer, shape, strides, offset, order ); + +// Get the underlying data type: +var dtype = arr.dtype; +// returns 'uint8' +``` + + + +#### FancyArray.prototype.flags + +Meta information, such as information regarding the memory layout of the array. The returned object has the following properties: + +- **ROW_MAJOR_CONTIGUOUS**: `boolean` indicating if an array is row-major contiguous. +- **COLUMN_MAJOR_CONTIGUOUS**: `boolean` indicating if an array is column-major contiguous. +- **READONLY**: `boolean` indicating whether an array is **read-only**. + +An array is contiguous if (1) an array is compatible with being stored in a single memory segment and (2) each array element is adjacent to the next array element. Note that an array can be both row-major contiguous and column-major contiguous at the same time (e.g., if an array is a 1-dimensional array with `strides = [1]`). + +```javascript +var Int32Array = require( '@stdlib/array/int32' ); + +// Specify the array configuration: +var buffer = new Int32Array( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'column-major'; +var strides = [ 1, 2 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'int32', buffer, shape, strides, offset, order ); + +// Get the array flags: +var flg = arr.flags; +// returns {...} +``` + + + +#### FancyArray.prototype.length + +Number of array elements. + +```javascript +var Uint16Array = require( '@stdlib/array/uint16' ); + +// Specify the array configuration: +var buffer = new Uint16Array( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'column-major'; +var strides = [ -1, -2 ]; +var offset = 3; + +// Create a new array: +var arr = new FancyArray( 'uint16', buffer, shape, strides, offset, order ); + +// Get the array length: +var len = arr.length; +// returns 4 +``` + + + +#### FancyArray.prototype.ndims + +Number of dimensions. + +```javascript +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); + +// Specify the array configuration: +var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ -2, -1 ]; +var offset = 3; + +// Create a new array: +var arr = new FancyArray( 'uint8c', buffer, shape, strides, offset, order ); + +// Get the number of dimensions: +var ndims = arr.ndims; +// returns 2 +``` + + + +#### FancyArray.prototype.offset + +Index offset which specifies the `buffer` index at which to start iterating over array elements. + +```javascript +var Int16Array = require( '@stdlib/array/int16' ); + +// Specify the array configuration: +var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ -2, -1 ]; +var offset = 10; + +// Create a new array: +var arr = new FancyArray( 'int16', buffer, shape, strides, offset, order ); + +// Get the index offset: +var o = arr.offset; +// returns 10 +``` + + + +#### FancyArray.prototype.order + +Array order. The array order is either row-major (C-style) or column-major (Fortran-style). + +```javascript +var Uint32Array = require( '@stdlib/array/uint32' ); + +// Specify the array configuration: +var buffer = new Uint32Array( [ 1, 2, 3, 4 ] ); +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'uint32', buffer, shape, strides, offset, order ); + +// Get the array order: +var ord = arr.order; +// returns 'row-major' +``` + + + +#### FancyArray.prototype.shape + +Returns a copy of the array shape. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the array shape: +var dims = arr.shape; +// returns [ 2, 2 ] +``` + + + +#### FancyArray.prototype.strides + +Returns a copy of the array strides which specify how to access data along corresponding array dimensions. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'column-major'; +var strides = [ -1, 2 ]; +var offset = 1; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the array strides: +var s = arr.strides; +// returns [ -1, 2 ] +``` + +* * * + +### Methods + + + +#### FancyArray.prototype.get( i, j, k, ... ) + +Returns an array element specified according to provided subscripts. The number of provided subscripts must **equal** the number of dimensions. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the element located at (1,1): +var v = arr.get( 1, 1 ); +// returns 6.0 +``` + + + +#### FancyArray.prototype.iget( idx ) + +Returns an array element located at a specified linear index. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Get the element located at index 3: +var v = arr.iget( 3 ); +// returns 6.0 +``` + +For zero-dimensional arrays, the input argument is ignored and, for clarity, should **not** be provided. + + + +#### FancyArray.prototype.set( i, j, k, ..., v ) + +Sets an array element specified according to provided subscripts. The number of provided subscripts must **equal** the number of dimensions. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Set the element located at (1,1): +arr.set( 1, 1, 40.0 ); +var v = arr.get( 1, 1 ); +// returns 40.0 + +// Get the underlying buffer: +var d = arr.data; +// returns [ 1.0, 2.0, 3.0, 40.0 ] +``` + +The method returns the `FancyArray` instance. If an array is **read-only**, the method raises an exception. + + + +#### FancyArray.prototype.iset( idx, v ) + +Sets an array element located at a specified linear index. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +var shape = [ 2, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 0; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Set the element located at index 3: +arr.iset( 3, 40.0 ); +var v = arr.iget( 3 ); +// returns 40.0 + +// Get the underlying buffer: +var d = arr.data; +// returns [ 1.0, 2.0, 3.0, 40.0 ] +``` + +For zero-dimensional arrays, the first, and **only**, argument should be the value `v` to set. + +The method returns the `FancyArray` instance. If an array is **read-only**, the method raises an exception. + + + +#### FancyArray.prototype.toString() + +Serializes a `FancyArray` as a string. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; +var shape = [ 3, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Serialize to a string: +var str = arr.toString(); +// returns "ndarray( 'generic', [ 3, 4, 5, 6, 7, 8 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' )" +``` + +The method does **not** serialize data outside of the buffer region defined by the array configuration. + + + +#### FancyArray.prototype.toJSON() + +Serializes a `FancyArray` as a [JSON][json] object. `JSON.stringify()` implicitly calls this method when stringifying a `FancyArray` instance. + +```javascript +// Specify the array configuration: +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; +var shape = [ 3, 2 ]; +var order = 'row-major'; +var strides = [ 2, 1 ]; +var offset = 2; + +// Create a new array: +var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + +// Serialize to JSON: +var o = arr.toJSON(); +// returns { 'type': 'ndarray', 'dtype': 'generic', 'flags': {...}, 'offset': 0, 'order': 'row-major', 'shape': [ 3, 2 ], 'strides': [ 2, 1 ], 'data': [ 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] } +``` + +The method does **not** serialize data outside of the buffer region defined by the array configuration. + +
+ + + + + +
+ +## Notes + +- To create a zero-dimensional array, provide an empty `shape` and a single `strides` element equal to `0`. The `order` can be either `row-major` or `column-major` and has no effect on data storage or access. + + ```javascript + var buffer = [ 1 ]; + var shape = []; + var order = 'row-major'; + var strides = [ 0 ]; + var offset = 0; + + // Create a new zero-dimensional array: + var arr = new FancyArray( 'generic', buffer, shape, strides, offset, order ); + // returns + ``` + +- A `FancyArray` is an [`ndarray`][@stdlib/ndarray/ctor] instance and supports all [`ndarray`][@stdlib/ndarray/ctor] options, attributes, and methods. A `FancyArray` can be consumed by any API which supports [`ndarray`][@stdlib/ndarray/ctor] instances. + +- Indexing expressions provide a convenient and powerful means for creating and operating on [`ndarray`][@stdlib/ndarray/ctor] views; however, their use does entail a performance cost. Indexing expressions are best suited for interactive use (e.g., in the [REPL][@stdlib/repl]) and scripting. For performance critical applications, prefer equivalent functional APIs supporting [`ndarray`][@stdlib/ndarray/ctor] instances. + +- In older JavaScript environments which do **not** support [`Proxy`][@stdlib/proxy/ctor] objects, the use of indexing expressions is **not** supported. + +
+ + + + + +
+ +* * * + +## Examples + + + + + +```javascript +var S = require( '@stdlib/slice/ctor' ); +var E = require( '@stdlib/slice/multi' ); +var toArray = require( '@stdlib/ndarray/to-array' ); +var FancyArray = require( '@stdlib/ndarray/fancy' ); + +var buffer = [ + 1, 2, + 3, 4, // 0 + 5, 6, // 1 + 7, 8, // 2 + 9, 10 +]; +var shape = [ 3, 2 ]; +var strides = [ 2, 1 ]; +var offset = 2; + +var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +// Access an ndarray property: +var ndims = x.ndims; +// returns 2 + +// Retrieve an ndarray element: +var v = x.get( 2, 1 ); +// returns 8 + +// Set an ndarray element: +x.set( 2, 1, 20 ); +v = x.get( 2, 1 ); +// returns 20 + +// Create an alias for `undefined` for more concise slicing expressions: +var _ = void 0; + +// Create a multi-dimensional slice: +var s = E( S(0,_,2), _ ); +// returns + +// Use the slice to create a view on the original ndarray: +var y1 = x[ s ]; +console.log( toArray( y1 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Use alternative syntax: +var y2 = x[ [ S(0,_,2), _ ] ]; +console.log( toArray( y2 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Use alternative syntax: +var y3 = x[ '0::2,:' ]; +console.log( toArray( y3 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Flip dimensions: +var y4 = x[ [ S(_,_,-2), S(_,_,-1) ] ]; +console.log( toArray( y4 ) ); +// => [ [ 20, 7 ], [ 4, 3 ] ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + + diff --git a/fancy/benchmark/benchmark.attributes.js b/fancy/benchmark/benchmark.attributes.js new file mode 100644 index 00000000..5711c56f --- /dev/null +++ b/fancy/benchmark/benchmark.attributes.js @@ -0,0 +1,391 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::get:byteLength', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.byteLength; + if ( v !== null ) { + b.fail( 'should return null' ); + } + } + b.toc(); + if ( v !== null ) { + b.fail( 'should return null' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:BYTES_PER_ELEMENT', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.BYTES_PER_ELEMENT; + if ( v !== null ) { + b.fail( 'should return null' ); + } + } + b.toc(); + if ( v !== null ) { + b.fail( 'should return null' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:data', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.data; + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== 6 ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:dtype', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.dtype; + if ( v !== 'generic' ) { + b.fail( 'should return expected data type' ); + } + } + b.toc(); + if ( v !== 'generic' ) { + b.fail( 'should return expected data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:flags', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.flags; + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:length', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.length; + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v !== buffer.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:ndims', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.ndims; + if ( v !== shape.length ) { + b.fail( 'should return expected number of dimensions' ); + } + } + b.toc(); + if ( v !== shape.length ) { + b.fail( 'should return expected number of dimensions' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:offset', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.offset; + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + } + b.toc(); + if ( v !== offset ) { + b.fail( 'should return expected offset' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:order', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.order; + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + } + b.toc(); + if ( v !== order ) { + b.fail( 'should return expected order' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:shape', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.shape; + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== shape.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::get:strides', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = out.strides; + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + } + b.toc(); + if ( v.length !== strides.length ) { + b.fail( 'should return expected length' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.get.js b/fancy/benchmark/benchmark.get.js new file mode 100644 index 00000000..dcdd0cba --- /dev/null +++ b/fancy/benchmark/benchmark.get.js @@ -0,0 +1,588 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%20)-10 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%20)-10 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, i%3, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:get:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.get( 0, 0, 0, (i%30)-15, 1 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.get_slice.js b/fancy/benchmark/benchmark.get_slice.js new file mode 100644 index 00000000..c2a0d31c --- /dev/null +++ b/fancy/benchmark/benchmark.get_slice.js @@ -0,0 +1,902 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable new-cap, max-len */ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); +var S = require( '@stdlib/slice/ctor' ); +var MultiSlice = require( '@stdlib/slice/multi' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// VARIABLES // + +var opts = { + 'skip': !hasProxySupport() +}; +var _ = null; + + +// MAIN // + +bench( pkg+'::1d,get_slice,slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( 2, -1 ), + S( _, -3, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,get_slice,array_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( 2, -1 ), + S( _, -3, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ [ slices[ i%slices.length ] ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,get_slice,multi_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + new MultiSlice( S() ), + new MultiSlice( S( _, _, 2 ) ), + new MultiSlice( S( 2, -1 ) ), + new MultiSlice( S( _, -3, -1 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,get_slice,integer', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + 0, + 1, + 2, + 3, + 4, + 5 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,get_slice,subsequence', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + ':', + '::2', + '2:-1', + ':-3:-1' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,get_slice,array_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var s; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( _, -1 ), + S( _, -3, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + s = slices[ i%slices.length ]; + v = out[ [ s, s ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,get_slice,multi_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + new MultiSlice( S(), S() ), + new MultiSlice( S( _, _, 2 ), S( _, _, 2 ) ), + new MultiSlice( S( _, -1 ), S( _, -1 ) ), + new MultiSlice( S( _, -3, -1 ), S( _, -3, -1 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,get_slice,integer', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ [ i%shape[0], i%shape[1] ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,get_slice,subsequence', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + ':, :', + '::2, ::2', + ':-1, :-1', + ':-3:-1, :-3:-1' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,get_slice,array_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var s; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 2, 2 ]; + strides = [ 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( _, -1 ), + S( _, -2, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + s = slices[ i%slices.length ]; + v = out[ [ s, s, s ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,get_slice,multi_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 2, 2 ]; + strides = [ 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + new MultiSlice( S(), S(), S() ), + new MultiSlice( S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ) ), + new MultiSlice( S( _, -1 ), S( _, -1 ), S( _, -1 ) ), + new MultiSlice( S( _, -2, -1 ), S( _, -2, -1 ), S( _, -2, -1 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,get_slice,integer', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 2, 2 ]; + strides = [ 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ [ i%shape[0], i%shape[1], i%shape[2] ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,get_slice,subsequence', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 2, 2 ]; + strides = [ 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + ':, :, :', + '::2, ::2, ::2', + ':-1, :-1, :-1', + ':-2:-1, :-2:-1, :-2:-1' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,get_slice,array_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var s; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 2, 2 ]; + strides = [ 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( _, -1 ), + S( _, _, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + s = slices[ i%slices.length ]; + v = out[ [ s, s, s, s ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,get_slice,multi_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 2, 2 ]; + strides = [ 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + new MultiSlice( S(), S(), S(), S() ), + new MultiSlice( S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ) ), + new MultiSlice( S( _, -1 ), S( _, -1 ), S( _, -1 ), S( _, -1 ) ), + new MultiSlice( S( _, _, -1 ), S( _, _, -1 ), S( _, _, -1 ), S( _, _, -1 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,get_slice,integer', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 2, 2 ]; + strides = [ 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ [ i%shape[0], i%shape[1], i%shape[2], i%shape[3] ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,get_slice,subsequence', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 2, 2 ]; + strides = [ 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + ':, :, :, :', + '::2, ::2, ::2, ::2', + ':-1, :-1, :-1, :-1', + '::-1, ::-1, ::-1, ::-1' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,get_slice,array_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var s; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 1, 2, 2 ]; + strides = [ 4, 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + S(), + S( _, _, 2 ), + S( _, -1 ), + S( _, _, -1 ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + s = slices[ i%slices.length ]; + v = out[ [ s, s, s, s, s ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,get_slice,multi_slice_object', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 1, 2, 2 ]; + strides = [ 4, 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + new MultiSlice( S(), S(), S(), S(), S() ), + new MultiSlice( S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ), S( _, _, 2 ) ), + new MultiSlice( S( _, -1 ), S( _, -1 ), S( _, -1 ), S( _, -1 ), S( _, -1 ) ), + new MultiSlice( S( _, _, -1 ), S( _, _, -1 ), S( _, _, -1 ), S( _, _, -1 ), S( _, _, -1 ) ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,get_slice,integer', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 1, 2, 2 ]; + strides = [ 4, 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ [ i%shape[0], i%shape[1], i%shape[2], i%shape[3], i%shape[4] ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,get_slice,subsequence', opts, function benchmark( b ) { + var strides; + var buffer; + var offset; + var slices; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ]; + shape = [ 2, 1, 1, 2, 2 ]; + strides = [ 4, 4, 4, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + slices = [ + ':, :, :, :, :', + '::2, ::2, ::2, ::2, ::2', + ':-1, :-1, :-1, :-1, :-1', + '::-1, ::-1, ::-1, ::-1, ::-1' + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out[ slices[ i%slices.length ] ]; + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.iget.js b/fancy/benchmark/benchmark.iget.js new file mode 100644 index 00000000..f612f318 --- /dev/null +++ b/fancy/benchmark/benchmark.iget.js @@ -0,0 +1,344 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:iget', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iget:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iget:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var v; + var i; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iget:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iget:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.iget( i%6 ); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( v !== v ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.instantiation.js b/fancy/benchmark/benchmark.instantiation.js new file mode 100644 index 00000000..3161fa4c --- /dev/null +++ b/fancy/benchmark/benchmark.instantiation.js @@ -0,0 +1,338 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,instantiation', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d,instantiation,new', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 0 ] = i; + out = new ndarray( 'generic', buffer, shape, strides, offset, order ); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + } + b.toc(); + if ( out.length === 0 ) { + b.fail( 'should have a length greater than 0' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.iset.js b/fancy/benchmark/benchmark.iset.js new file mode 100644 index 00000000..804c0a39 --- /dev/null +++ b/fancy/benchmark/benchmark.iset.js @@ -0,0 +1,346 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:iset', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iset:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.iset( j, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.iget( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:iset:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.iset( j, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.iget( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_positive_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ 5-j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ 5-j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,all_negative_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, -1 ]; + offset = 5; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ 5-j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ 5-j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iset:order=row-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ 0 ] !== buffer[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( out.iget( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d,mixed_sign_strides:iset:order=column-major', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ -2, 1 ]; + offset = 4; + order = 'column-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.iset( j, i ); + if ( buffer[ 0 ] !== buffer[ 0 ] ) { + b.fail( 'should not be NaN' ); + } + } + b.toc(); + if ( out.iget( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.set.js b/fancy/benchmark/benchmark.set.js new file mode 100644 index 00000000..a92e99fa --- /dev/null +++ b/fancy/benchmark/benchmark.set.js @@ -0,0 +1,598 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 6; + out.set( j, i ); + if ( buffer[ j ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ j ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%20) - 10; + tmp = out.set( j, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::1d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 6 ]; + strides = [ 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%20) - 10; + tmp = out.set( j, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 3; + out.set( j, 1, i ); + if ( buffer[ (2*j) + (1*1) ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::2d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 3; + out.set( 0, j, 1, i ); + if ( buffer[ (2*j) + (1*1) ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::3d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 3, 2 ]; + strides = [ 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 3; + out.set( 0, 0, j, 1, i ); + if ( buffer[ (2*j) + (1*1) ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::4d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 3, 2 ]; + strides = [ 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var i; + var j; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = i % 3; + out.set( 0, 0, 0, j, 1, i ); + if ( buffer[ (2*j) + (1*1) ] !== i ) { + b.fail( 'should set value' ); + } + } + b.toc(); + if ( buffer[ (2*j) + 1 ] !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set:mode=wrap', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'wrap' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, 0, 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::5d:set:mode=clamp', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var opts; + var out; + var tmp; + var i; + var j; + + opts = { + 'mode': 'clamp' + }; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 1, 1, 1, 3, 2 ]; + strides = [ 6, 6, 6, 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order, opts ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + j = (i%30) - 15; + tmp = out.set( 0, 0, 0, j, 1, i ); + if ( typeof tmp !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( out.get( 0, 0, 0, j, 1 ) !== (i-1) ) { + b.fail( 'should set value' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.to_json.js b/fancy/benchmark/benchmark.to_json.js new file mode 100644 index 00000000..1039609a --- /dev/null +++ b/fancy/benchmark/benchmark.to_json.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toJSON', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.toJSON(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/benchmark/benchmark.to_string.js b/fancy/benchmark/benchmark.to_string.js new file mode 100644 index 00000000..4b670165 --- /dev/null +++ b/fancy/benchmark/benchmark.to_string.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var pkg = require( './../package.json' ).name; +var ndarray = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toString', function benchmark( b ) { + var strides; + var buffer; + var offset; + var shape; + var order; + var out; + var v; + var i; + + buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + shape = [ 3, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + out = ndarray( 'generic', buffer, shape, strides, offset, order ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + buffer[ 1 ] = i; + v = out.toString(); + if ( typeof v !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( typeof v !== 'string' ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/fancy/examples/0d.js b/fancy/examples/0d.js new file mode 100644 index 00000000..02e4dee0 --- /dev/null +++ b/fancy/examples/0d.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable new-cap */ + +'use strict'; + +var E = require( '@stdlib/slice/multi' ); +var FancyArray = require( './../lib' ); + +var buffer = [ 6 ]; +var shape = []; +var strides = [ 0 ]; +var offset = 0; + +var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +// Access an ndarray property: +var ndims = x.ndims; +console.log( 'ndims: %d', ndims ); +// => 'ndims: 0' + +// Retrieve an ndarray element: +var v = x.get(); +console.log( 'x[] = %d', v ); +// => 'x[] = 6' + +// Set an ndarray element: +x.set( 10 ); +console.log( 'x[] = %d', x.get() ); +// => 'x[] = 10' + +// Use an empty multi-slice to create a separate array view: +var y1 = x[ E() ]; +console.log( y1.get() ); +// => 10 + +// Use alternative syntax: +var y2 = x[ [] ]; +console.log( y2.get() ); +// => 10 + +// Use alternative syntax: +var y3 = x[ '' ]; +console.log( y3.get() ); +// => 10 diff --git a/fancy/examples/1d.js b/fancy/examples/1d.js new file mode 100644 index 00000000..24d4c46e --- /dev/null +++ b/fancy/examples/1d.js @@ -0,0 +1,76 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable new-cap, comma-spacing */ + +'use strict'; + +var S = require( '@stdlib/slice/ctor' ); +var E = require( '@stdlib/slice/multi' ); +var toArray = require( './../../to-array' ); +var FancyArray = require( './../lib' ); + +var buffer = [ 1, 2, 3, 4, 5, 6 ]; +var shape = [ 6 ]; +var strides = [ 1 ]; +var offset = 0; + +var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +// Access an ndarray property: +var ndims = x.ndims; +console.log( 'ndims: %d', ndims ); +// => 'ndims: 1' + +// Retrieve an ndarray element: +var v = x.get( 2 ); +console.log( 'x[2] = %d', v ); +// => 'x[2] = 3' + +// Set an ndarray element: +x.set( 2, 10 ); +console.log( 'x[2] = %d', x.get( 2 ) ); +// => 'x[2] = 10' + +// Create an alias for `undefined` for more concise slicing expressions: +var _ = void 0; + +// Create a multi-dimensional slice: +var s = E( S(0,_,2) ); +// returns + +// Use a multi-slice to create a view on the original ndarray: +var y1 = x[ s ]; +console.log( toArray( y1 ) ); +// => [ 1, 10, 5 ] + +// Use a slice to create a view on the original ndarray: +var y2 = x[ s.data[0] ]; +console.log( toArray( y2 ) ); +// => [ 1, 10, 5 ] + +// Use alternative syntax: +var y3 = x[ [ S(0,_,2) ] ]; +console.log( toArray( y3 ) ); +// => [ 1, 10, 5 ] + +// Use alternative syntax: +var y4 = x[ '::-2' ]; +console.log( toArray( y4 ) ); +// => [ 6, 4, 2 ] diff --git a/fancy/examples/3d.js b/fancy/examples/3d.js new file mode 100644 index 00000000..ecd9c407 --- /dev/null +++ b/fancy/examples/3d.js @@ -0,0 +1,95 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable new-cap, array-element-newline, comma-spacing */ + +'use strict'; + +var S = require( '@stdlib/slice/ctor' ); +var E = require( '@stdlib/slice/multi' ); +var toArray = require( './../../to-array' ); +var FancyArray = require( './../lib' ); + +var buffer = [ + 1, 2, + + 3, 4, // 0,0,{0,1} + 5, 6, // 0,1,{0,1} + 7, 8, // 0,2,{0,1} + + 9, 10, // 1,0,{0,1} + 11, 12, // 1,1,{0,1} + 13, 14, // 1,2,{0,1} + + 15, 16, + 17, 18, + 19, 20 +]; +var shape = [ 2, 3, 2 ]; +var strides = [ 6, 2, 1 ]; +var offset = 2; + +var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +// Access an ndarray property: +var ndims = x.ndims; +console.log( 'ndims: %d', ndims ); +// => 'ndims: 3' + +// Retrieve an ndarray element: +var v = x.get( 1, 2, 1 ); +console.log( 'x[1,2,1] = %d', v ); +// => 'x[1,2,1] = 14' + +// Set an ndarray element: +x.set( 1, 2, 1, 100 ); +console.log( 'x[1,2,1] = %d', x.get( 1, 2, 1 ) ); +// => 'x[1,2,1] = 100' + +// Create an alias for `undefined` for more concise slicing expressions: +var _ = void 0; + +// Create a multi-dimensional slice: +var s = E( 1, S(0,_,2), _ ); +// returns + +// Use the slice to create a view on the original ndarray: +var y1 = x[ s ]; +console.log( toArray( y1 ) ); +// => [ [ [ 9, 10 ], [ 13, 100 ] ] ] + +// Use alternative syntax: +var y2 = x[ [ 1, S(0,_,2), _ ] ]; +console.log( toArray( y2 ) ); +// => [ [ [ 9, 10 ], [ 13, 100 ] ] ] + +// Use alternative syntax: +var y3 = x[ '1,0::2,:' ]; +console.log( toArray( y3 ) ); +// => [ [ [ 9, 10 ], [ 13, 100 ] ] ] + +// Flip dimensions: +var y4 = x[ [ 1, S(_,_,-2), S(_,_,-1) ] ]; +console.log( toArray( y4 ) ); +// => [ [ [ 100, 13 ], [ 10, 9 ] ] ] + +// Only the second rows: +var y5 = x[ [ _, 1, _ ] ]; +console.log( toArray( y5 ) ); +// => [ [ [ 5, 6 ] ], [ [ 11, 12 ] ] ] diff --git a/fancy/examples/index.js b/fancy/examples/index.js new file mode 100644 index 00000000..73c5978b --- /dev/null +++ b/fancy/examples/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable new-cap, array-element-newline, comma-spacing */ + +'use strict'; + +var S = require( '@stdlib/slice/ctor' ); +var E = require( '@stdlib/slice/multi' ); +var toArray = require( './../../to-array' ); +var FancyArray = require( './../lib' ); + +var buffer = [ + 1, 2, + 3, 4, // 0 + 5, 6, // 1 + 7, 8, // 2 + 9, 10 +]; +var shape = [ 3, 2 ]; +var strides = [ 2, 1 ]; +var offset = 2; + +var x = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +// returns + +// Access an ndarray property: +var ndims = x.ndims; +console.log( 'ndims: %d', ndims ); +// => 'ndims: 2' + +// Retrieve an ndarray element: +var v = x.get( 2, 1 ); +console.log( 'x[2,1] = %d', v ); +// => 'x[2,1] = 8' + +// Set an ndarray element: +x.set( 2, 1, 20 ); +console.log( 'x[2,1] = %d', x.get( 2, 1 ) ); +// => 'x[2,1] = 20' + +// Create an alias for `undefined` for more concise slicing expressions: +var _ = void 0; + +// Create a multi-dimensional slice: +var s = E( S(0,_,2), _ ); +// returns + +// Use the slice to create a view on the original ndarray: +var y1 = x[ s ]; +console.log( toArray( y1 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Use alternative syntax: +var y2 = x[ [ S(0,_,2), _ ] ]; +console.log( toArray( y2 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Use alternative syntax: +var y3 = x[ '0::2,:' ]; +console.log( toArray( y3 ) ); +// => [ [ 3, 4 ], [ 7, 20 ] ] + +// Flip dimensions: +var y4 = x[ [ S(_,_,-2), S(_,_,-1) ] ]; +console.log( toArray( y4 ) ); +// => [ [ 20, 7 ], [ 4, 3 ] ] diff --git a/fancy/lib/array_options.js b/fancy/lib/array_options.js new file mode 100644 index 00000000..64564512 --- /dev/null +++ b/fancy/lib/array_options.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns array creation options. +* +* @private +* @returns {Object} options +*/ +function options() { + return { + // Default to always returning read-only arrays: + 'readonly': true + }; +} + + +// EXPORTS // + +module.exports = options; diff --git a/fancy/lib/empty.js b/fancy/lib/empty.js new file mode 100644 index 00000000..e57c8afa --- /dev/null +++ b/fancy/lib/empty.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var buffer = require( './../../base/buffer' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var options = require( './array_options.js' ); + + +// MAIN // + +/** +* Returns an empty n-dimensional ndarray. +* +* @private +* @param {Function} ctor - ndarray constructor +* @param {string} dtype - array data type +* @param {NonNegativeIntegerArray} shape - array shape +* @param {string} order - layout order +* @returns {ndarray} empty ndarray +*/ +function empty( ctor, dtype, shape, order ) { + var strides; + var ndims; + + ndims = shape.length; + if ( ndims === 0 ) { + strides = [ 0 ]; + } else { + strides = zeros( ndims ); + } + return new ctor( dtype, buffer( dtype, 0 ), shape, strides, 0, order, options() ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = empty; diff --git a/fancy/lib/get.0d.js b/fancy/lib/get.0d.js new file mode 100644 index 00000000..1c82af50 --- /dev/null +++ b/fancy/lib/get.0d.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var trim = require( '@stdlib/string/base/trim' ); +var str2multislice = require( '@stdlib/slice/base/str2multislice' ); +var format = require( '@stdlib/string/format' ); +var hasProperty = require( './has_property.js' ); +var options = require( './array_options.js' ); +var RE_INTEGER = require( './re_integer.js' ); + + +// MAIN // + +/** +* Trap for retrieving property values. +* +* @private +* @param {Object} target - target object +* @param {(string|symbol)} property - property name +* @param {Object} receiver - the proxy object or an object inheriting from the proxy +* @throws {Error} invalid slice operation +* @throws {RangeError} number of slice dimensions must match the number of array dimensions +* @returns {*} result +*/ +function get( target, property, receiver ) { + var dtype; + var value; + var prop; + var ch; + var sh; + var s; + if ( hasProperty( property ) ) { + value = target[ property ]; + if ( isFunction( value ) ) { + return wrapper; + } + return value; + } + prop = trim( property ); + + // Resolve target meta data: + dtype = target.dtype; + sh = target.shape; + + // Retrieve the first character in order to to detect how a slice operation was specified: + ch = prop[ 0 ]; + + // Case: multi-slice + if ( ch === 'M' ) { + // Convert the string to a slice object: + s = str2multislice( prop ); + if ( s === null ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, s.data ); + + // Ensure that we were provided an empty multi-slice: + if ( s.ndims !== sh.length ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), s.ndims ) ); + } + } + // Case: non-empty string + else if ( prop.length !== 0 ) { + // TODO: the following can be generalized by going ahead and parsing the slice string or integer and passing to a functional API which then verifies that s.ndims !== sh.length. We need only retain the error raised for an invalid operation. + + // Case: slice or an integer + if ( ch === 'S' || RE_INTEGER.test( prop ) ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', sh.join( ',' ), 1 ) ); + } + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [] ); + + // Return an zero-dimensional array view: + return new receiver.constructor( dtype, target.data, sh, target.strides, target.offset, target.order, options() ); // eslint-disable-line max-len + + /** + * Method wrapper. + * + * @private + * @returns {*} results + */ + function wrapper() { + var args; + var i; + + args = []; + for ( i = 0; i < arguments.length; i++ ) { + args.push( arguments[ i ] ); + } + return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this + } +} + + +// EXPORTS // + +module.exports = get; diff --git a/fancy/lib/get.1d.js b/fancy/lib/get.1d.js new file mode 100644 index 00000000..9b79685d --- /dev/null +++ b/fancy/lib/get.1d.js @@ -0,0 +1,220 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var trim = require( '@stdlib/string/base/trim' ); +var seq2slice = require( '@stdlib/slice/base/seq2slice' ); +var str2slice = require( '@stdlib/slice/base/str2slice' ); +var str2multislice = require( '@stdlib/slice/base/str2multislice' ); +var normalizeSlice = require( '@stdlib/slice/base/normalize-slice' ); +var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' ); +var vind2bind = require( './../../base/vind2bind' ); +var format = require( '@stdlib/string/format' ); +var hasProperty = require( './has_property.js' ); +var options = require( './array_options.js' ); +var RE_INTEGER = require( './re_integer.js' ); +var sliceView = require( './view.1d.js' ); +var empty = require( './empty.js' ); + + +// MAIN // + +/** +* Trap for retrieving property values. +* +* @private +* @param {Object} target - target object +* @param {(string|symbol)} property - property name +* @param {Object} receiver - the proxy object or an object inheriting from the proxy +* @throws {Error} invalid slice operation +* @throws {RangeError} number of slice dimensions must match the number of array dimensions +* @throws {RangeError} slice exceeds array bounds +* @throws {Error} slice increment must be a non-zero integer +* @returns {*} result +*/ +function get( target, property, receiver ) { + var strides; + var offset; + var strict; + var dtype; + var shape; + var order; + var value; + var parts; + var prop; + var ch; + var s; + if ( hasProperty( property ) ) { + value = target[ property ]; + if ( isFunction( value ) ) { + return wrapper; + } + return value; + } + prop = trim( property ); + + // Resolve target meta data: + dtype = target.dtype; + shape = target.shape; + strides = target.strides; + offset = target.offset; + order = target.order; + strict = false; // TODO: support strict mode + + // Retrieve the first character in order to to detect how a slice operation was specified: + ch = prop[ 0 ]; + + // Case: slice + if ( ch === 'S' ) { + // Convert the string to a slice object: + s = str2slice( property ); + if ( s === null ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [ s ] ); + + // Normalize the slice object based on the array length: + s = normalizeSlice( s, shape[ 0 ], true ); + + // If the slice exceeds array bounds, return an empty one-dimensional array... + if ( s.code ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + return empty( receiver.constructor, dtype, [ 0 ], order ); + } + // Return a view of the provided array: + return sliceView( receiver.constructor, dtype, target.data, shape, strides, offset, order, s ); + } + // Case: multi-slice + if ( ch === 'M' ) { + // Convert the string to a slice object: + s = str2multislice( prop ); + if ( s === null ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, s.data ); + + // Ensure that we were provided a one-dimensional multi-slice: + if ( s.ndims !== shape.length ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', shape.join( ',' ), s.ndims ) ); + } + // Normalize the slice object based on the array shape: + s = normalizeMultiSlice( s, shape, true ); + + // If the slice exceeds array bounds, return an empty one-dimensional array... + if ( s.code ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + return empty( receiver.constructor, dtype, [ 0 ], order ); + } + // Return a view of the provided array: + return sliceView( receiver.constructor, dtype, target.data, shape, strides, offset, order, s.data[ 0 ] ); + } + // Case: integer + if ( RE_INTEGER.test( prop ) ) { + // Convert the string to a numeric value: + s = parseInt( prop, 10 ); + + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [ s ] ); + + // Check whether we need to resolve an index relative to the last array element... + if ( s < 0 ) { + s = shape[ 0 ] + s; + + // If the index exceeds array bounds, return an empty zero-dimensional array... + if ( s < 0 ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + return empty( receiver.constructor, dtype, [], order ); + } + } + // If the index exceeds array bounds, return an empty zero-dimensional array... + else if ( s >= shape[ 0 ] ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + return empty( receiver.constructor, dtype, [], order ); + } + // Compute the index offset for the indexed element: + offset = vind2bind( shape, strides, offset, order, s, 'throw' ); + + // Return a zero-dimensional array: + return new receiver.constructor( dtype, target.data, [], [ 0 ], offset, order, options() ); + } + // Check whether we were provided a multi-dimensional subsequence string... + parts = prop.split( /\s*,\s*/ ); + if ( parts.length > 1 ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', shape.join( ',' ), parts.length ) ); + } + prop = parts[ 0 ]; + if ( prop.length === 0 ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', shape.join( ',' ), 0 ) ); + } + // Attempt to convert the subsequence string to a slice object: + s = seq2slice( prop, shape[ 0 ], true ); + if ( s.code ) { + if ( s.code === 'ERR_SLICE_OUT_OF_BOUNDS' ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + // Return an empty zero-dimensional array: + return empty( receiver.constructor, dtype, [], order ); + } + if ( s.code === 'ERR_SLICE_INVALID_INCREMENT' ) { + throw new Error( format( 'invalid operation. A subsequence increment must be a non-zero integer. Value: `%s`.', property ) ); + } + if ( s.code === 'ERR_SLICE_INVALID_SUBSEQUENCE' ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, [ s ] ); + + // Return a view of the provided array: + return sliceView( receiver.constructor, dtype, target.data, shape, strides, offset, order, s ); + + /** + * Method wrapper. + * + * @private + * @returns {*} results + */ + function wrapper() { + var args; + var i; + + args = []; + for ( i = 0; i < arguments.length; i++ ) { + args.push( arguments[ i ] ); + } + return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this + } +} + + +// EXPORTS // + +module.exports = get; diff --git a/fancy/lib/get.nd.js b/fancy/lib/get.nd.js new file mode 100644 index 00000000..c14fb23f --- /dev/null +++ b/fancy/lib/get.nd.js @@ -0,0 +1,132 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isFunction = require( '@stdlib/assert/is-function' ); +var trim = require( '@stdlib/string/base/trim' ); +var str2multislice = require( '@stdlib/slice/base/str2multislice' ); +var seq2multislice = require( '@stdlib/slice/base/seq2multislice' ); +var format = require( '@stdlib/string/format' ); +var hasProperty = require( './has_property.js' ); +var RE_SUBSEQ = require( './re_subseq.js' ); +var multisliceWrap = require( './wrap_multislice_arguments.js' ); +var sliceView = require( './view.nd.js' ); +var empty = require( './empty.js' ); + + +// FUNCTIONS // + +/** +* Trap for retrieving property values. +* +* @private +* @param {Object} target - target object +* @param {(string|symbol)} property - property name +* @param {Object} receiver - the proxy object or an object inheriting from the proxy +* @throws {Error} invalid slice operation +* @throws {RangeError} number of slice dimensions must match the number of array dimensions +* @throws {RangeError} slice exceeds array bounds +* @throws {Error} slice increment must be a non-zero integer +* @returns {*} result +*/ +function get( target, property, receiver ) { + var strict; + var shape; + var value; + var prop; + var ch; + var s; + + if ( hasProperty( property ) ) { + value = target[ property ]; + if ( isFunction( value ) ) { + return wrapper; + } + return value; + } + prop = trim( property ); + + // Resolve target meta data: + strict = false; // TODO: support strict mode + + // Retrieve the first character in order to to detect how a slice operation was specified: + ch = prop[ 0 ]; + + // Case: multi-slice (e.g., 'MultiSlice(Slice(0,10,2),null,2,Slice(10,5,-1))') + if ( ch === 'M' ) { + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, str2multislice( prop ).data ); // TODO: handle `null` + + return sliceView( target, property, receiver, str2multislice( prop ) ); + } + // Case: subsequence string (e.g., ':10,1,::-1,:,-5,2::3') + if ( RE_SUBSEQ.test( prop ) ) { + shape = target.shape; + s = seq2multislice( prop, shape, true ); + if ( s.code ) { + if ( s.code === 'ERR_SLICE_OUT_OF_BOUNDS' ) { + if ( strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + // Return an empty zero-dimensional array: + return empty( receiver.constructor, target.dtype, [], target.order ); + } + if ( s.code === 'ERR_SLICE_INVALID_INCREMENT' ) { + throw new Error( format( 'invalid operation. A subsequence increment must be a non-zero integer. Value: `%s`.', property ) ); + } + if ( s.code === 'ERR_SLICE_INVALID_ELLIPSIS' ) { + throw new Error( format( 'invalid operation. A subsequence may only include a single ellipsis. Value: `%s`.', property ) ); + } + if ( s.code === 'ERR_SLICE_INVALID_SUBSEQUENCE' ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + } + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, s.data ); + + return sliceView( target, property, receiver, s ); + } + // Case: array syntax (e.g., [ Slice(0,10,1), null, Slice(4,null,-1) ]) + + // TODO: => @stdlib/ndarray/base/slice: return slice( receiver, str2multislice( multisliceWrap( prop ) ).data ); // TODO: handle `null` + + return sliceView( target, '['+property+']', receiver, str2multislice( multisliceWrap( prop ) ) ); + + /** + * Method wrapper. + * + * @private + * @returns {*} results + */ + function wrapper() { + var args; + var i; + + args = []; + for ( i = 0; i < arguments.length; i++ ) { + args.push( arguments[ i ] ); + } + return value.apply( ( this === receiver ) ? target : this, args ); // eslint-disable-line no-invalid-this + } +} + + +// EXPORTS // + +module.exports = get; diff --git a/fancy/lib/has_property.js b/fancy/lib/has_property.js new file mode 100644 index 00000000..10f3368a --- /dev/null +++ b/fancy/lib/has_property.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var propertiesIn = require( '@stdlib/utils/properties-in' ); +var contains = require( '@stdlib/array/base/assert/contains' ).factory; +var ndarray = require( './../../ctor' ); +var defaults = require( './../../defaults' ); + + +// MAIN // + +/** +* Tests whether a property name exists on the parent prototype. +* +* @private +* @name hasProperty +* @type {Function} +* @param {(string|symbol)} name - property name +* @returns {boolean} boolean indicating whether the property exists on the parent prototype +* +* @example +* var bool = hasProperty( 'get' ); +* // returns true +* +* bool = hasProperty( 'foo' ); +* // returns false +*/ +var hasProperty = contains( propertiesIn( new ndarray( 'generic', [ 0 ], [], [ 0 ], 0, defaults.get( 'order' ) ) ) ); + + +// EXPORTS // + +module.exports = hasProperty; diff --git a/fancy/lib/index.js b/fancy/lib/index.js new file mode 100644 index 00000000..fa9bd370 --- /dev/null +++ b/fancy/lib/index.js @@ -0,0 +1,45 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Fancy array constructor. +* +* @module @stdlib/ndarray/fancy +* +* @example +* var FancyArray = require( '@stdlib/ndarray/fancy' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var out = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/fancy/lib/main.js b/fancy/lib/main.js new file mode 100644 index 00000000..538c3d11 --- /dev/null +++ b/fancy/lib/main.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var parent = require( './../../ctor' ); // eslint-disable-line stdlib/no-redeclare +var inherit = require( '@stdlib/utils/inherit' ); +var Proxy = require( '@stdlib/proxy/ctor' ); +var get0d = require( './get.0d.js' ); +var get1d = require( './get.1d.js' ); +var getnd = require( './get.nd.js' ); + + +// MAIN // + +/** +* Fancy ndarray constructor. +* +* @constructor +* @param {string} dtype - data type +* @param {Collection} buffer - data buffer +* @param {NonNegativeIntegerArray} shape - array shape +* @param {IntegerArray} strides - array strides +* @param {NonNegativeInteger} offset - index offset +* @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @param {Options} [options] - function options +* @param {string} [options.mode="throw"] - specifies how to handle indices which exceed array dimensions +* @param {StringArray} [options.submode=["throw"]] - specifies how to handle subscripts which exceed array dimensions on a per dimension basis +* @param {boolean} [options.readonly=false] - boolean indicating whether an array should be read-only +* @throws {TypeError} `dtype` argument must be a supported ndarray data type +* @throws {TypeError} `buffer` argument must be an array-like object, typed-array-like, or a Buffer +* @throws {TypeError} `buffer` argument `get` and `set` properties must be functions +* @throws {TypeError} `shape` argument must be an array-like object containing nonnegative integers +* @throws {Error} `shape` argument length must equal the number of dimensions +* @throws {TypeError} `strides` argument must be an array-like object containing integers +* @throws {Error} `strides` argument length must equal the number of dimensions (except for zero-dimensional arrays; in which case, the `strides` argument length must be equal to `1`) +* @throws {Error} for zero-dimensional ndarrays, the `strides` argument must contain a single element equal to `0` +* @throws {TypeError} `offset` argument must be a nonnegative integer +* @throws {TypeError} `order` argument must be a supported ndarray order +* @throws {Error} `buffer` argument must be compatible with specified meta data +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @throws {RangeError} too many dimensions +* @returns {FancyArray} FancyArray instance +* +* @example +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var out = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +*/ +function FancyArray( dtype, buffer, shape, strides, offset, order, options ) { + var handlers; + var nargs; + var ndims; + + nargs = arguments.length; + if ( !( this instanceof FancyArray ) ) { + if ( nargs < 7 ) { + return new FancyArray( dtype, buffer, shape, strides, offset, order ); + } + return new FancyArray( dtype, buffer, shape, strides, offset, order, options ); + } + // Call the parent constructor: + parent.call( this, dtype, buffer, shape, strides, offset, order, ( nargs < 7 ) ? {} : options ); + + if ( Proxy ) { // NOTE: cannot use `@stdlib/assert/has-proxy-support` here, as that API uses code evaluation and might violate CSPs + ndims = shape.length; + handlers = {}; + if ( ndims === 0 ) { + handlers.get = get0d; + } else if ( ndims === 1 ) { + handlers.get = get1d; + } else { + handlers.get = getnd; + } + return new Proxy( this, handlers ); + } + // TODO: replace with `@stdlib/console/warn` (or equivalent once available) + console.warn( 'WARNING: Proxy objects are not supported in the current environment. Some `FancyArray` functionality may not be available.' ); // eslint-disable-line no-console + return this; +} + +// Inherit from the parent constructor: +inherit( FancyArray, parent ); + +/** +* Constructor name. +* +* @name name +* @memberof FancyArray +* @type {string} +* @default 'ndarray' +* +* @example +* var str = FancyArray.name; +* // returns 'ndarray' +*/ +setReadOnly( FancyArray, 'name', 'ndarray' ); + + +// EXPORTS // + +module.exports = FancyArray; diff --git a/fancy/lib/nonreduced_dimensions.js b/fancy/lib/nonreduced_dimensions.js new file mode 100644 index 00000000..076ca78d --- /dev/null +++ b/fancy/lib/nonreduced_dimensions.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns a list of non-reduced dimensions in an un-normalized multi-slice. +* +* @private +* @param {MultiSlice} slice - input slice +* @returns {NonNegativeIntegerArray} list of non-reduced dimensions +*/ +function nonreducedDimensions( slice ) { + var data; + var out; + var i; + + data = slice.data; + out = []; + for ( i = 0; i < data.length; i++ ) { + if ( typeof data[ i ] !== 'number' ) { + out.push( i ); + } + } + return out; +} + + +// EXPORTS // + +module.exports = nonreducedDimensions; diff --git a/fancy/lib/re_integer.js b/fancy/lib/re_integer.js new file mode 100644 index 00000000..51055d52 --- /dev/null +++ b/fancy/lib/re_integer.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Regular expression for testing whether a string is an integer string. +* +* @private +* @name RE_INTEGER +* @type {RegExp} +* +* @example +* var bool = RE_INTEGER.test( '10' ); +* // returns true +* +* @example +* var bool = RE_INTEGER.test( '-1' ); +* // returns true +* +* @example +* var bool = RE_INTEGER.test( '0:10:2' ); +* // returns false +*/ +var RE_INTEGER = /^-?[0-9]+$/; + + +// EXPORTS // + +module.exports = RE_INTEGER; diff --git a/fancy/lib/re_subseq.js b/fancy/lib/re_subseq.js new file mode 100644 index 00000000..56f32030 --- /dev/null +++ b/fancy/lib/re_subseq.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Regular expression for testing whether a string is a subsequence string. +* +* @private +* @name RE_SUBSEQ +* @type {RegExp} +* +* @example +* var bool = RE_SUBSEQ.test( '0:10:2' ); +* // returns true +* +* @example +* var bool = RE_SUBSEQ.test( '0' ); +* // returns false +* +* @example +* var bool = RE_SUBSEQ.test( 'Slice(0,10,2)' ); +* // returns false +*/ +var RE_SUBSEQ = /:/; + + +// EXPORTS // + +module.exports = RE_SUBSEQ; diff --git a/fancy/lib/slice_start.js b/fancy/lib/slice_start.js new file mode 100644 index 00000000..919c5691 --- /dev/null +++ b/fancy/lib/slice_start.js @@ -0,0 +1,59 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var sub2ind = require( './../../base/sub2ind' ); + + +// MAIN // + +/** +* Resolves the linear index of the first element indexed by a multi-slice. +* +* ## Notes +* +* - If `strides` contains negative strides, if an `offset` is greater than `0`, the function returns a linear index with respect to the underlying data buffer. If an `offset` is equal to `0`, the function returns a linear index with respect to the array view. For more information, see `@stdlib/ndarray/base/sub2ind`. +* +* @private +* @param {MultiSlice} slice - multi-slice object +* @param {NonNegativeIntegerArray} shape - array shape +* @param {IntegerArray} strides - array strides +* @param {NonNegativeInteger} offset - index offset +* @returns {NonNegativeInteger} linear index of the first element +*/ +function sliceStart( slice, shape, strides, offset ) { + var data; + var args; + var i; + + data = slice.data; + args = [ shape, strides, offset ]; + for ( i = 0; i < data.length; i++ ) { + args.push( data[ i ].start ); + } + args.push( 'throw' ); + return sub2ind.apply( null, args ); +} + + +// EXPORTS // + +module.exports = sliceStart; diff --git a/fancy/lib/slice_strides.js b/fancy/lib/slice_strides.js new file mode 100644 index 00000000..b8deb28c --- /dev/null +++ b/fancy/lib/slice_strides.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Resolves slice strides. +* +* @private +* @param {MultiSlice} slice - multi-slice object +* @param {IntegerArray} strides - array strides +* @param {NonNegativeIntegerArray} sdims - indices of non-reduced dimensions +* @returns {IntegerArray} slice strides +*/ +function sliceStrides( slice, strides, sdims ) { + var data; + var out; + var i; + var j; + + data = slice.data; + out = []; + for ( i = 0; i < sdims.length; i++ ) { + j = sdims[ i ]; + out.push( strides[j]*data[j].step ); + } + return out; +} + + +// EXPORTS // + +module.exports = sliceStrides; diff --git a/fancy/lib/view.1d.js b/fancy/lib/view.1d.js new file mode 100644 index 00000000..20555c99 --- /dev/null +++ b/fancy/lib/view.1d.js @@ -0,0 +1,68 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var vind2bind = require( './../../base/vind2bind' ); +var sliceLength = require( '@stdlib/slice/base/length' ); +var empty = require( './empty.js' ); +var options = require( './array_options.js' ); + + +// MAIN // + +/** +* Returns a view of a one-dimensional array. +* +* @private +* @param {Function} ctor - array constructor +* @param {string} dtype - array data type +* @param {Collection} data - underlying data buffer +* @param {NonNegativeIntegerArray} shape - array shape +* @param {IntegerArray} strides - array strides +* @param {NonNegativeInteger} offset - index offset +* @param {string} order - layout order +* @param {Slice} slice - slice object +* @returns {ndarray} ndarray view +*/ +function sliceView( ctor, dtype, data, shape, strides, offset, order, slice ) { + var start = slice.start; + + // If the slice does not contain any elements, return an empty one-dimensional array... + if ( start === slice.stop ) { // TODO: replace with assertion utility? + return empty( ctor, dtype, [ 0 ], order ); + } + // Compute a new offset (i.e., the position of the starting index in the underlying buffer): + offset = vind2bind( shape, strides, offset, order, start, 'throw' ); + + // Determine the number of elements in the slice: + shape = [ sliceLength( slice ) ]; + + // Compute new array strides: + strides = [ strides[0]*slice.step ]; + + // Return a one-dimensional view: + return new ctor( dtype, data, shape, strides, offset, order, options() ); +} + + +// EXPORTS // + +module.exports = sliceView; diff --git a/fancy/lib/view.nd.js b/fancy/lib/view.nd.js new file mode 100644 index 00000000..afb0456b --- /dev/null +++ b/fancy/lib/view.nd.js @@ -0,0 +1,118 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var normalizeMultiSlice = require( '@stdlib/slice/base/normalize-multi-slice' ); +var sliceShape = require( '@stdlib/slice/base/shape' ); +var take = require( '@stdlib/array/base/take' ); +var vind2bind = require( './../../base/vind2bind' ); +var numel = require( './../../base/numel' ); +var format = require( '@stdlib/string/format' ); +var nonreducedDimensions = require( './nonreduced_dimensions.js' ); +var sliceStart = require( './slice_start.js' ); +var sliceStrides = require( './slice_strides.js' ); +var options = require( './array_options.js' ); +var empty = require( './empty.js' ); + + +// MAIN // + +/** +* Returns a view of an n-dimensional array. +* +* @private +* @param {Object} target - target object +* @param {string} property - original property string +* @param {Object} receiver - the proxy object or an object inheriting from the proxy +* @param {(MultiSlice|null)} slice - multi-slice object +* @throws {Error} invalid slice operation +* @throws {RangeError} number of slice dimensions must match the number of array dimensions +* @throws {RangeError} slice exceeds array bounds +* @returns {ndarray} ndarray view +*/ +function sliceView( target, property, receiver, slice ) { + var strides; + var offset; + var strict; + var dtype; + var shape; + var order; + var sdims; + var ctor; + var sh; + + // Verify that we were successfully able to create a multi-slice: + if ( slice === null ) { + throw new Error( format( 'invalid operation. Unsupported slice operation. Value: `%s`.', property ) ); + } + // Retrieve target meta data: + dtype = target.dtype; + shape = target.shape; + strides = target.strides; + offset = target.offset; + order = target.order; + strict = false; // TODO: support strict mode + + // Ensure that the number of array dimensions matches the number of slices: + if ( slice.ndims !== shape.length ) { + throw new RangeError( format( 'invalid operation. Number of array dimensions does not match the number of slice dimensions. Array shape: (%s). Slice dimensions: %u.', shape.join( ',' ), slice.ndims ) ); + } + // Normalize the slice object based on the array shape: + slice = normalizeMultiSlice( slice, shape, true ); + + // In strict mode, if the slice exceeds array bounds, raise an exception... + if ( slice.code && strict ) { + throw new RangeError( format( 'invalid operation. Slice exceeds array bounds. Array shape: (%s).', shape.join( ',' ) ) ); + } + // Resolve the output array constructor: + ctor = receiver.constructor; + + // Compute the slice shape: + sh = sliceShape( slice ); + + // Resolve the indices of the non-reduced dimensions: + sdims = nonreducedDimensions( slice ); + + // If the slice does not contain any elements, return an empty array... + if ( numel( sh ) === 0 ) { + return empty( ctor, dtype, take( sh, sdims ), order ); + } + // Resolve the index offset of the first element: + offset = vind2bind( shape, strides, offset, order, sliceStart( slice, shape, strides, 0 ), 'throw' ); + + // Remove reduced dimensions from the slice shape: + sh = take( sh, sdims ); + + // If all dimensions were reduced, return a zero-dimensional array... + if ( sh.length === 0 ) { + return new ctor( dtype, target.data, [], [ 0 ], offset, order, options() ); // eslint-disable-line max-len + } + // Update strides according to slice steps: + strides = sliceStrides( slice, strides, sdims ); + + // Return a slice view: + return new ctor( dtype, target.data, sh, strides, offset, order, options() ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = sliceView; diff --git a/fancy/lib/wrap_multislice_arguments.js b/fancy/lib/wrap_multislice_arguments.js new file mode 100644 index 00000000..9f2f3703 --- /dev/null +++ b/fancy/lib/wrap_multislice_arguments.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var replace = require( '@stdlib/string/base/replace' ); + + +// MAIN // + +/** +* Returns string-serialized array elements as arguments to the MultiSlice constructor. +* +* @private +* @param {string} str - array elements serialized as a string +* @returns {string} wrapped arguments +* +* @example +* var args = wrap( ',Slice(0,10,1)' ); +* // returns 'MultiSlice(null,Slice(0,10,1))' +* +* @example +* var args = wrap( 'Slice(0,10,1),' ); +* // returns 'MultiSlice(Slice(0,10,1),null)' +* +* @example +* var args = wrap( 'Slice(0,10,1),,,Slice(0,10,1)' ); +* // returns 'MultiSlice(Slice(0,10,1),null,null,Slice(0,10,1))' +* +* @example +* var args = wrap( ',Slice(0,10,1),null,,Slice(2,9,2),null,' ); +* // returns 'MultiSlice(null,Slice(0,10,1),null,null,Slice(2,9,2),null,null)' +*/ +function wrap( str ) { + // In order to support `x[ [...] ]` syntax, we need to touch up the serialized string due to how `undefined` is serialized... + str = replace( str, /^,/, 'null,' ); // leading comma (e.g., [ void 0, Slice(0,10,1) ] => ',Slice(0,10,1)') + str = replace( str, /,$/, ',null' ); // trailing comma (e.g., [ Slice(0,10,1), void 0 ] => 'Slice(0,10,1),') + str = replace( str, /,(?=,)/g, ',null' ); // between commas (e.g., [ Slice(0,10,1), void 0, void 0, Slice(0,10,1) ] => 'Slice(0,10,1),,,Slice(0,10,1)') + return 'MultiSlice(' + str + ')'; +} + + +// EXPORTS // + +module.exports = wrap; diff --git a/fancy/package.json b/fancy/package.json new file mode 100644 index 00000000..0edc401f --- /dev/null +++ b/fancy/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/ndarray/fancy", + "version": "0.0.0", + "description": "Fancy multidimensional array constructor.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=6.0.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "data", + "structure", + "array", + "typed", + "typed array", + "typed-array", + "multidimensional", + "dimensions", + "dims", + "ndarray", + "constructors", + "constructor", + "ctors", + "ctor", + "numpy.ndarray", + "fancy", + "slicing" + ], + "__stdlib__": {} +} diff --git a/package.json b/package.json index b747a468..6788c3d4 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,9 @@ "@stdlib/blas": "github:stdlib-js/blas#main", "@stdlib/console": "github:stdlib-js/console#main", "@stdlib/fs": "github:stdlib-js/fs#main", + "@stdlib/proxy": "github:stdlib-js/proxy#main", "@stdlib/random": "github:stdlib-js/random#main", + "@stdlib/slice": "github:stdlib-js/slice#main", "@stdlib/strided": "github:stdlib-js/strided#main", "@stdlib/time": "github:stdlib-js/time#main", "proxyquire": "^2.0.0",