Skip to content

Commit aadc88d

Browse files
committed
Add support for complex number dtypes and data buffers exposing get and set methods
1 parent 9fddc8d commit aadc88d

File tree

9 files changed

+857
-26
lines changed

9 files changed

+857
-26
lines changed

lib/node_modules/@stdlib/ndarray/base/ctor/README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
6161

6262
The constructor has the following parameters:
6363

64-
- **dtype**: underlying data type.
64+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes].
6565
- **buffer**: data buffer.
6666
- **shape**: array shape (dimensions).
6767
- **strides**: array strides which are index offsets specifying how to access along corresponding dimensions.
@@ -212,7 +212,7 @@ var bool = ( d === buffer );
212212

213213
#### ndarray.prototype.dtype
214214

215-
Underlying data type.
215+
Underlying [data type][@stdlib/ndarray/dtypes].
216216

217217
```javascript
218218
var Uint8Array = require( '@stdlib/array/uint8' );
@@ -563,8 +563,14 @@ The method does **not** serialize data outside of the buffer region defined by t
563563

564564
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
565565

566+
* * *
567+
566568
<section class="notes">
567569

570+
## Notes
571+
572+
- A data buffer must be an array-like object (i.e., have a `length` property). For data buffers which are not indexed collections (i.e., collections which cannot support direct index access, such as `buffer[ index ]`; e.g., [`Complex64Array`][@stdlib/array/complex64], [`Complex128Array`][@stdlib/array/complex128], etc), a data buffer should provide `#.get( idx )` and `#.set( v, idx )` methods. Note that, for `set` methods, the value to set should be the first argument, followed by the linear index, similar to the native typed array `set` method.
573+
568574
</section>
569575

570576
<!-- /.notes -->
@@ -639,6 +645,12 @@ str = JSON.stringify( arr.toJSON() );
639645

640646
[json]: http://www.json.org/
641647

648+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
649+
650+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib
651+
652+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib
653+
642654
</section>
643655

644656
<!-- /.links -->

lib/node_modules/@stdlib/ndarray/base/ctor/lib/get.js

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ function get() {
4040
for ( i = 0; i < arguments.length; i++ ) {
4141
idx += this._strides[ i ] * arguments[ i ];
4242
}
43+
if ( this._getter ) {
44+
return this._getter.call( this._buffer, idx );
45+
}
4346
return this._buffer[ idx ];
4447
}
4548

lib/node_modules/@stdlib/ndarray/base/ctor/lib/iget.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ function iget( idx ) {
4747
if ( this._flags.ROW_MAJOR_CONTIGUOUS || this._flags.COLUMN_MAJOR_CONTIGUOUS ) { // eslint-disable-line max-len
4848
// Trivial case where we have all positive strides...
4949
if ( this._iterationOrder === 1 ) {
50-
return this._buffer[ this._offset + idx ];
50+
if ( this._getter ) {
51+
return this._getter.call( this._buffer, this._offset+idx );
52+
}
53+
return this._buffer[ this._offset+idx ];
5154
}
5255
// Trivial case where we have all negative strides...
5356
if ( this._iterationOrder === -1 ) {
54-
return this._buffer[ this._offset - idx ];
57+
if ( this._getter ) {
58+
return this._getter.call( this._buffer, this.offset-idx );
59+
}
60+
return this._buffer[ this._offset-idx ];
5561
}
5662
}
5763
// The approach which follows is to resolve a view index to its subscripts and then plug the subscripts into the standard formula for computing the linear index in the underlying data buffer...
@@ -65,6 +71,9 @@ function iget( idx ) {
6571
idx /= shape[ i ];
6672
ind += s * strides[ i ];
6773
}
74+
if ( this._getter ) {
75+
return this._getter.call( this._buffer, ind );
76+
}
6877
return this._buffer[ ind ];
6978
}
7079
// Case: row-major
@@ -74,6 +83,9 @@ function iget( idx ) {
7483
idx /= shape[ i ];
7584
ind += s * strides[ i ];
7685
}
86+
if ( this._getter ) {
87+
return this._getter.call( this._buffer, ind );
88+
}
7789
return this._buffer[ ind ];
7890
}
7991

lib/node_modules/@stdlib/ndarray/base/ctor/lib/iset.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,30 @@ function iset( idx, v ) {
4343

4444
ndims = this._ndims;
4545
if ( ndims === 0 ) {
46-
this._buffer[ this._offset ] = idx;
46+
if ( this._setter ) {
47+
this._setter.call( this._buffer, idx, this._offset );
48+
} else {
49+
this._buffer[ this._offset ] = idx;
50+
}
4751
return this;
4852
}
4953
if ( this._flags.ROW_MAJOR_CONTIGUOUS || this._flags.COLUMN_MAJOR_CONTIGUOUS ) { // eslint-disable-line max-len
5054
// Trivial case where we have all positive strides...
5155
if ( this._iterationOrder === 1 ) {
52-
this._buffer[ this._offset + idx ] = v;
56+
if ( this._setter ) {
57+
this._setter.call( this._buffer, v, this._offset+idx );
58+
} else {
59+
this._buffer[ this._offset+idx ] = v;
60+
}
5361
return this;
5462
}
5563
// Trivial case where we have all negative strides...
5664
if ( this._iterationOrder === -1 ) {
57-
this._buffer[ this._offset - idx ] = v;
65+
if ( this._setter ) {
66+
this._setter.call( this._buffer, v, this._offset-idx );
67+
} else {
68+
this._buffer[ this._offset-idx ] = v;
69+
}
5870
return this;
5971
}
6072
}
@@ -69,7 +81,11 @@ function iset( idx, v ) {
6981
idx /= shape[ i ];
7082
ind += s * strides[ i ];
7183
}
72-
this._buffer[ ind ] = v;
84+
if ( this._setter ) {
85+
this._setter.call( this._buffer, v, ind );
86+
} else {
87+
this._buffer[ ind ] = v;
88+
}
7389
return this;
7490
}
7591
// Case: row-major
@@ -79,7 +95,11 @@ function iset( idx, v ) {
7995
idx /= shape[ i ];
8096
ind += s * strides[ i ];
8197
}
82-
this._buffer[ ind ] = v;
98+
if ( this._setter ) {
99+
this._setter.call( this._buffer, v, ind );
100+
} else {
101+
this._buffer[ ind ] = v;
102+
}
83103
return this;
84104
}
85105

lib/node_modules/@stdlib/ndarray/base/ctor/lib/main.js

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ function ndarray( dtype, buffer, shape, strides, offset, order ) {
106106
this._shape = shape;
107107
this._strides = strides;
108108

109+
if ( buffer.get && buffer.set ) {
110+
this._getter = buffer.get;
111+
this._setter = buffer.set;
112+
} else {
113+
this._getter = null;
114+
this._setter = null;
115+
}
116+
109117
this._iterationOrder = iterationOrder( strides );
110118

111119
// Determine if the array can be stored contiguously:

lib/node_modules/@stdlib/ndarray/base/ctor/lib/set.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ function set() {
4141
for ( i = 0; i < arguments.length-1; i++ ) {
4242
idx += this._strides[ i ] * arguments[ i ];
4343
}
44-
this._buffer[ idx ] = arguments[ i ];
44+
if ( this._setter ) {
45+
this._setter.call( this._buffer, arguments[ i ], idx );
46+
} else {
47+
this._buffer[ idx ] = arguments[ i ];
48+
}
4549
return this;
4650
}
4751

lib/node_modules/@stdlib/ndarray/base/ctor/lib/tojson.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var real = require( '@stdlib/complex/real' );
24+
var imag = require( '@stdlib/complex/imag' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -34,6 +40,7 @@ function toJSON() {
3440
/* eslint-disable no-invalid-this */
3541
var out;
3642
var len;
43+
var v;
3744
var i;
3845

3946
len = this._length;
@@ -55,8 +62,15 @@ function toJSON() {
5562
}
5663
// Cast data to generic array...
5764
out.data = [];
58-
for ( i = 0; i < len; i++ ) {
59-
out.data.push( this.iget( i ) );
65+
if ( out.dtype === 'complex64' || out.dtype === 'complex128' ) {
66+
for ( i = 0; i < len; i++ ) {
67+
v = this.iget( i );
68+
out.data.push( real( v ), imag( v ) );
69+
}
70+
} else {
71+
for ( i = 0; i < len; i++ ) {
72+
out.data.push( this.iget( i ) );
73+
}
6074
}
6175
return out;
6276
}

lib/node_modules/@stdlib/ndarray/base/ctor/lib/tostring.js

+51-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// MODULES //
2222

2323
var replace = require( '@stdlib/string/replace' );
24+
var real = require( '@stdlib/complex/real' );
25+
var imag = require( '@stdlib/complex/imag' );
2426

2527

2628
// VARIABLES //
@@ -36,7 +38,9 @@ var CTORS = {
3638
'float32': 'new Float32Array( [ {{data}} ] )',
3739
'float64': 'new Float64Array( [ {{data}} ] )',
3840
'generic': '[ {{data}} ]',
39-
'binary': 'new Buffer( [ {{data}} ] )'
41+
'binary': 'new Buffer( [ {{data}} ] )',
42+
'complex64': 'new Complex64Array( [ {{data}} ] )',
43+
'complex128': 'new Complex128Array( [ {{data}} ] )'
4044
};
4145

4246

@@ -58,37 +62,70 @@ function toString() { // eslint-disable-line stdlib/no-redeclare
5862
var ndims;
5963
var ctor;
6064
var str;
65+
var dt;
66+
var v;
6167
var i;
6268

6369
ndims = this._shape.length;
70+
dt = this._dtype;
6471

6572
// Function to invoke to create an ndarray:
66-
str = 'ndarray( \''+this._dtype+'\', ';
73+
str = 'ndarray( \''+dt+'\', ';
6774

6875
// Data buffer parameter...
6976
buffer = '';
7077
if ( this._length <= 100 ) {
71-
for ( i = 0; i < this._length; i++ ) {
72-
buffer += this.iget( i );
73-
if ( i < this._length-1 ) {
74-
buffer += ', ';
78+
if ( dt === 'complex64' || dt === 'complex128' ) {
79+
for ( i = 0; i < this._length; i++ ) {
80+
v = this.iget( i );
81+
buffer += real( v ) + ', ' + imag( v );
82+
if ( i < this._length-1 ) {
83+
buffer += ', ';
84+
}
85+
}
86+
} else {
87+
for ( i = 0; i < this._length; i++ ) {
88+
buffer += this.iget( i );
89+
if ( i < this._length-1 ) {
90+
buffer += ', ';
91+
}
7592
}
7693
}
7794
} else {
7895
// First three values...
79-
for ( i = 0; i < 3; i++ ) {
80-
buffer += this.iget( i );
81-
if ( i < 2 ) {
82-
buffer += ', ';
96+
if ( dt === 'complex64' || dt === 'complex128' ) {
97+
for ( i = 0; i < 3; i++ ) {
98+
v = this.iget( i );
99+
buffer += real( v ) + ', ' + imag( v );
100+
if ( i < 2 ) {
101+
buffer += ', ';
102+
}
103+
}
104+
} else {
105+
for ( i = 0; i < 3; i++ ) {
106+
buffer += this.iget( i );
107+
if ( i < 2 ) {
108+
buffer += ', ';
109+
}
83110
}
84111
}
85112
buffer += ', ..., ';
86113

87114
// Last three values...
88-
for ( i = 2; i >= 0; i-- ) {
89-
buffer += this.iget( this._length-1-i );
90-
if ( i > 0 ) {
91-
buffer += ', ';
115+
if ( dt === 'complex64' || dt === 'complex128' ) {
116+
for ( i = 2; i >= 0; i-- ) {
117+
v = this.iget( this._length-1-i );
118+
buffer += real( v ) + ', ' + imag( v );
119+
if ( i > 0 ) {
120+
buffer += ', ';
121+
}
122+
}
123+
} else {
124+
for ( i = 2; i >= 0; i-- ) {
125+
buffer += this.iget( this._length-1-i );
126+
if ( i > 0 ) {
127+
buffer += ', ';
128+
}
92129
}
93130
}
94131
}

0 commit comments

Comments
 (0)