Skip to content

Commit 4914f67

Browse files
committed
docs: update examples and documentation
1 parent 141c3f7 commit 4914f67

File tree

3 files changed

+95
-31
lines changed

3 files changed

+95
-31
lines changed

lib/node_modules/@stdlib/ndarray/map/README.md

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# map
2222

23-
> Apply a callback function to elements in an input ndarray and assign results to elements in an output ndarray.
23+
> Apply a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assign results to elements in a new output [ndarray][@stdlib/ndarray/ctor].
2424
2525
<section class="intro">
2626

@@ -36,56 +36,87 @@ limitations under the License.
3636
var map = require( '@stdlib/ndarray/map' );
3737
```
3838

39-
#### map( x, \[options, ]fcn\[, thisArg] )
39+
#### map( x\[, options], fcn\[, thisArg] )
4040

41-
Applies a callback function to elements in an input ndarray and assigns results to elements in an output ndarray.
41+
Applies a callback function to elements in an input [ndarray][@stdlib/ndarray/ctor] and assigns results to elements in a new output [ndarray][@stdlib/ndarray/ctor].
42+
43+
<!-- eslint-disable max-len -->
4244

4345
```javascript
4446
var Float64Array = require( '@stdlib/array/float64' );
4547
var ndarray = require( '@stdlib/ndarray/ctor' );
48+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
4649

47-
function scale( x ) {
48-
return x * 10.0;
50+
function scale( z ) {
51+
return z * 10.0;
4952
}
5053

51-
// Create the input data buffer:
52-
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
53-
54-
// Define the shape of the input ndarray:
55-
var shape = [ 3, 2 ];
56-
57-
// Define the array strides:
58-
var strides = [ 2, 1 ];
59-
60-
// Define the index offset:
61-
var offset = 0;
54+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
55+
var shape = [ 2, 3 ];
56+
var strides = [ 6, 1 ];
57+
var offset = 1;
6258

63-
// Create the input ndarray:
6459
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
60+
// returns <ndarray>
6561

66-
// Apply the map function:
6762
var y = map( x, scale );
63+
// returns <ndarray>
6864

69-
console.log( y.data );
70-
// => <Float64Array>[ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0 ]
65+
var arr = ndarray2array( y );
66+
// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ]
7167
```
7268

7369
The function accepts the following arguments:
7470

75-
- **x**: input ndarray.
71+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
7672
- **options**: function options.
7773
- **fcn**: callback to apply.
7874
- **thisArg**: callback execution context.
7975

8076
The function accepts the following options:
8177

82-
- **dtype**: output ndarray data type. Defaults to match the input ndarray if not specified.
78+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor].
79+
80+
By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option.
81+
82+
<!-- eslint-disable max-len -->
83+
84+
```javascript
85+
var Float64Array = require( '@stdlib/array/float64' );
86+
var ndarray = require( '@stdlib/ndarray/ctor' );
87+
var dtype = require( '@stdlib/ndarray/dtype' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
90+
function scale( z ) {
91+
return z * 10.0;
92+
}
93+
94+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
95+
var shape = [ 2, 3 ];
96+
var strides = [ 6, 1 ];
97+
var offset = 1;
98+
99+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
100+
// returns <ndarray>
101+
102+
var opts = {
103+
'dtype': 'float32'
104+
};
105+
var y = map( x, opts, scale );
106+
// returns <ndarray>
107+
108+
var dt = dtype( y );
109+
// returns 'float32'
110+
111+
var arr = ndarray2array( y );
112+
// returns [ [ 20.0, 30.0, 40.0 ], [ 80.0, 90.0, 100.0 ] ]
113+
```
83114

84115
The callback function is provided the following arguments:
85116

86117
- **values**: current array element.
87118
- **indices**: current array element indices.
88-
- **arr**: the input ndarray.
119+
- **arr**: the input [ndarray][@stdlib/ndarray/ctor].
89120

90121
</section>
91122

@@ -95,6 +126,35 @@ The callback function is provided the following arguments:
95126

96127
## Notes
97128

129+
- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed by a provided callback function.
130+
131+
<!-- eslint-disable max-len -->
132+
133+
```javascript
134+
var Float64Array = require( '@stdlib/array/float64' );
135+
var ndarray = require( '@stdlib/ndarray/ctor' );
136+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
137+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
138+
139+
function toComplex( z ) {
140+
return new Complex128( z, 0.0 );
141+
}
142+
143+
var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
144+
var shape = [ 2, 3 ];
145+
var strides = [ 6, 1 ];
146+
var offset = 1;
147+
148+
var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
149+
// returns <ndarray>
150+
151+
var opts = {
152+
'dtype': 'complex128'
153+
};
154+
var y = map( x, opts, toComplex );
155+
// returns <ndarray>
156+
```
157+
98158
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance.
99159

100160
</section>
@@ -110,7 +170,7 @@ The callback function is provided the following arguments:
110170
```javascript
111171
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
112172
var abs = require( '@stdlib/math/base/special/abs' );
113-
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
173+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
114174
var naryFunction = require( '@stdlib/utils/nary-function' );
115175
var ndarray = require( '@stdlib/ndarray/ctor' );
116176
var map = require( '@stdlib/ndarray/map' );
@@ -122,10 +182,10 @@ var shape = [ 5, 2 ];
122182
var strides = [ 2, 1 ];
123183
var offset = 0;
124184
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
185+
console.log( ndarray2array( x ) );
125186

126187
var y = map( x, naryFunction( abs, 1 ) );
127-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
128-
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
188+
console.log( ndarray2array( y ) );
129189
```
130190

131191
</section>
@@ -142,6 +202,10 @@ console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
142202

143203
<section class="links">
144204

205+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
206+
207+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
208+
145209
<!-- <related-links> -->
146210

147211
<!-- </related-links> -->

lib/node_modules/@stdlib/ndarray/map/docs/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ declare function map<T = unknown, V = unknown>( x: typedndarray<T>, options: Flo
777777
* var Complex128 = require( '@stdlib/complex/float64/ctor' );
778778
*
779779
* function toComplex( z ) {
780-
* return new Complex128( z, 0 );
780+
* return new Complex128( z, 0.0 );
781781
* }
782782
*
783783
* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
@@ -812,7 +812,7 @@ declare function map<T = unknown, V = unknown>( x: typedndarray<T>, options: Com
812812
* var Complex64 = require( '@stdlib/complex/float32/ctor' );
813813
*
814814
* function toComplex( z ) {
815-
* return new Complex64( z, 0 );
815+
* return new Complex64( z, 0.0 );
816816
* }
817817
*
818818
* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );

lib/node_modules/@stdlib/ndarray/map/examples/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
2222
var abs = require( '@stdlib/math/base/special/abs' );
23-
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
23+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
2424
var naryFunction = require( '@stdlib/utils/nary-function' );
2525
var ndarray = require( '@stdlib/ndarray/ctor' );
2626
var map = require( './../lib' );
@@ -32,7 +32,7 @@ var shape = [ 5, 2 ];
3232
var strides = [ 2, 1 ];
3333
var offset = 0;
3434
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
35-
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
35+
console.log( ndarray2array( x ) );
3636

3737
var y = map( x, naryFunction( abs, 1 ) );
38-
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
38+
console.log( ndarray2array( y ) );

0 commit comments

Comments
 (0)