@stdlib/ndarray-base 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CITATION.cff +30 -0
- package/LICENSE +0 -304
- package/NOTICE +1 -1
- package/README.md +101 -11
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +769 -6
- package/lib/index.js +157 -0
- package/package.json +222 -164
- package/docs/types/test.ts +0 -29
package/docs/types/index.d.ts
CHANGED
|
@@ -16,14 +16,17 @@
|
|
|
16
16
|
* limitations under the License.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
// TypeScript Version:
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
20
|
|
|
21
21
|
/* tslint:disable:max-line-length */
|
|
22
22
|
/* tslint:disable:max-file-line-count */
|
|
23
23
|
|
|
24
24
|
import assert = require( '@stdlib/ndarray-base-assert' );
|
|
25
|
+
import binaryLoopOrder = require( '@stdlib/ndarray-base-binary-loop-interchange-order' );
|
|
26
|
+
import binaryBlockSize = require( '@stdlib/ndarray-base-binary-tiling-block-size' );
|
|
25
27
|
import bind2vind = require( '@stdlib/ndarray-base-bind2vind' );
|
|
26
28
|
import broadcastArray = require( '@stdlib/ndarray-base-broadcast-array' );
|
|
29
|
+
import broadcastScalar = require( '@stdlib/ndarray-base-broadcast-scalar' );
|
|
27
30
|
import broadcastShapes = require( '@stdlib/ndarray-base-broadcast-shapes' );
|
|
28
31
|
import buffer = require( '@stdlib/ndarray-base-buffer' );
|
|
29
32
|
import bufferCtors = require( '@stdlib/ndarray-base-buffer-ctors' );
|
|
@@ -41,15 +44,30 @@ import dtypeResolveStr = require( '@stdlib/ndarray-base-dtype-resolve-str' );
|
|
|
41
44
|
import dtypeStr2Enum = require( '@stdlib/ndarray-base-dtype-str2enum' );
|
|
42
45
|
import dtype2c = require( '@stdlib/ndarray-base-dtype2c' );
|
|
43
46
|
import dtypes2signatures = require( '@stdlib/ndarray-base-dtypes2signatures' );
|
|
47
|
+
import empty = require( '@stdlib/ndarray-base-empty' );
|
|
48
|
+
import emptyLike = require( '@stdlib/ndarray-base-empty-like' );
|
|
49
|
+
import expandDimensions = require( '@stdlib/ndarray-base-expand-dimensions' );
|
|
50
|
+
import scalar2ndarray = require( '@stdlib/ndarray-base-from-scalar' );
|
|
44
51
|
import ind = require( '@stdlib/ndarray-base-ind' );
|
|
45
52
|
import ind2sub = require( '@stdlib/ndarray-base-ind2sub' );
|
|
46
53
|
import iterationOrder = require( '@stdlib/ndarray-base-iteration-order' );
|
|
47
54
|
import maxViewBufferIndex = require( '@stdlib/ndarray-base-max-view-buffer-index' );
|
|
55
|
+
import maybeBroadcastArray = require( '@stdlib/ndarray-base-maybe-broadcast-array' );
|
|
48
56
|
import metaDataProps = require( '@stdlib/ndarray-base-meta-data-props' );
|
|
49
57
|
import minViewBufferIndex = require( '@stdlib/ndarray-base-min-view-buffer-index' );
|
|
50
58
|
import minmaxViewBufferIndex = require( '@stdlib/ndarray-base-minmax-view-buffer-index' );
|
|
59
|
+
import ndarraylike2object = require( '@stdlib/ndarray-base-ndarraylike2object' );
|
|
51
60
|
import nonsingletonDimensions = require( '@stdlib/ndarray-base-nonsingleton-dimensions' );
|
|
61
|
+
import nullary = require( '@stdlib/ndarray-base-nullary' );
|
|
62
|
+
import nullaryLoopOrder = require( '@stdlib/ndarray-base-nullary-loop-interchange-order' );
|
|
63
|
+
import nullaryBlockSize = require( '@stdlib/ndarray-base-nullary-tiling-block-size' );
|
|
52
64
|
import numel = require( '@stdlib/ndarray-base-numel' );
|
|
65
|
+
import outputPolicyEnum2Str = require( '@stdlib/ndarray-base-output-policy-enum2str' );
|
|
66
|
+
import outputPolicyResolveEnum = require( '@stdlib/ndarray-base-output-policy-resolve-enum' );
|
|
67
|
+
import outputPolicyResolveStr = require( '@stdlib/ndarray-base-output-policy-resolve-str' );
|
|
68
|
+
import outputPolicyStr2Enum = require( '@stdlib/ndarray-base-output-policy-str2enum' );
|
|
69
|
+
import prependSingletonDimensions = require( '@stdlib/ndarray-base-prepend-singleton-dimensions' );
|
|
70
|
+
import removeSingletonDimensions = require( '@stdlib/ndarray-base-remove-singleton-dimensions' );
|
|
53
71
|
import serializeMetaData = require( '@stdlib/ndarray-base-serialize-meta-data' );
|
|
54
72
|
import shape2strides = require( '@stdlib/ndarray-base-shape2strides' );
|
|
55
73
|
import singletonDimensions = require( '@stdlib/ndarray-base-singleton-dimensions' );
|
|
@@ -57,8 +75,16 @@ import strides2offset = require( '@stdlib/ndarray-base-strides2offset' );
|
|
|
57
75
|
import strides2order = require( '@stdlib/ndarray-base-strides2order' );
|
|
58
76
|
import sub2ind = require( '@stdlib/ndarray-base-sub2ind' );
|
|
59
77
|
import ndarray2array = require( '@stdlib/ndarray-base-to-array' );
|
|
78
|
+
import transpose = require( '@stdlib/ndarray-base-transpose' );
|
|
79
|
+
import unary = require( '@stdlib/ndarray-base-unary' );
|
|
80
|
+
import unaryBy = require( '@stdlib/ndarray-base-unary-by' );
|
|
81
|
+
import unaryLoopOrder = require( '@stdlib/ndarray-base-unary-loop-interchange-order' );
|
|
82
|
+
import unaryOutputDataType = require( '@stdlib/ndarray-base-unary-output-dtype' );
|
|
83
|
+
import unaryBlockSize = require( '@stdlib/ndarray-base-unary-tiling-block-size' );
|
|
60
84
|
import vind2bind = require( '@stdlib/ndarray-base-vind2bind' );
|
|
61
85
|
import wrapIndex = require( '@stdlib/ndarray-base-wrap-index' );
|
|
86
|
+
import zeros = require( '@stdlib/ndarray-base-zeros' );
|
|
87
|
+
import zerosLike = require( '@stdlib/ndarray-base-zeros-like' );
|
|
62
88
|
|
|
63
89
|
/**
|
|
64
90
|
* Interface describing the `base` namespace.
|
|
@@ -69,6 +95,70 @@ interface Namespace {
|
|
|
69
95
|
*/
|
|
70
96
|
assert: typeof assert;
|
|
71
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Reorders ndarray dimensions and associated strides for loop interchange.
|
|
100
|
+
*
|
|
101
|
+
* ## Notes
|
|
102
|
+
*
|
|
103
|
+
* - The returned object has the following properties:
|
|
104
|
+
*
|
|
105
|
+
* - **sh**: dimensions sorted in loop order.
|
|
106
|
+
* - **sx**: first input ndarray strides sorted in loop order.
|
|
107
|
+
* - **sy**: second input ndarray strides sorted in loop order.
|
|
108
|
+
* - **sz**: output ndarray strides sorted in loop order.
|
|
109
|
+
*
|
|
110
|
+
* - When iterating over the elements of a multi-dimensional array, accessing elements which are closer in memory can improve performance. To this end, loop interchange is a technique used in loop nest optimization to improve locality of reference and take advantage of CPU cache.
|
|
111
|
+
*
|
|
112
|
+
* The purpose of this function is to order ndarray dimensions according to the magnitude of array strides. By using the ordered dimensions and associated strides, one can construct nested loops (one for each dimension) such that the innermost loop iterates over the dimension in which array elements are closest in memory and the outermost loop iterates over the dimension in which array elements are farthest apart in memory. As a consequence, element iteration is optimized to minimize cache misses and ensure locality of reference.
|
|
113
|
+
*
|
|
114
|
+
* - Cache performance may be degraded if the layout order (i.e., row-major or column-major) differs for the input and output ndarrays. This function is intended to optimize cache performance for the most common layout order. Accordingly, if the output ndarray has a different layout order (e.g., if the input ndarrays are row-major and the output ndarray is column-major), cache misses are likely for the output ndarray. In general, to ensure best performance, input and output ndarrays should have the same layout order.
|
|
115
|
+
*
|
|
116
|
+
* - The function assumes that the input and output ndarrays have the same shape. Hence, loop interchange order should only be determined **after** broadcasting.
|
|
117
|
+
*
|
|
118
|
+
* @param sh - array dimensions
|
|
119
|
+
* @param sx - first input array stride lengths
|
|
120
|
+
* @param sy - second input array stride lengths
|
|
121
|
+
* @param sz - output array stride lengths
|
|
122
|
+
* @returns loop interchange data
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* var sh = [ 2, 3, 4 ];
|
|
126
|
+
*
|
|
127
|
+
* var sx = [ 12, 4, 1 ]; // row-major
|
|
128
|
+
* var sy = [ 24, 8, 1 ]; // row-major
|
|
129
|
+
* var sz = [ 1, -2, 6 ]; // column-major
|
|
130
|
+
*
|
|
131
|
+
* var o = loopOrder( sh, sx, sy, sz );
|
|
132
|
+
* // returns {...}
|
|
133
|
+
*
|
|
134
|
+
* var ssh = o.sh;
|
|
135
|
+
* // returns [ 4, 3, 2 ]
|
|
136
|
+
*
|
|
137
|
+
* var ssx = o.sx;
|
|
138
|
+
* // returns [ 1, 4, 12 ]
|
|
139
|
+
*
|
|
140
|
+
* var ssy = o.sy;
|
|
141
|
+
* // returns [ 1, 8, 24 ]
|
|
142
|
+
*
|
|
143
|
+
* var ssz = o.sz;
|
|
144
|
+
* // returns [ 6, -2, 1 ]
|
|
145
|
+
*/
|
|
146
|
+
binaryLoopOrder: typeof binaryLoopOrder;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Returns a loop block size for multi-dimensional array tiled loops.
|
|
150
|
+
*
|
|
151
|
+
* @param dtypeX - first input array data type
|
|
152
|
+
* @param dtypeY - second input array data type
|
|
153
|
+
* @param dtypeZ - output array data type
|
|
154
|
+
* @returns block size (in units of elements)
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* var bsize = ns.binaryBlockSize( 'float64', 'float64', 'float64' );
|
|
158
|
+
* // returns <number>
|
|
159
|
+
*/
|
|
160
|
+
binaryBlockSize: typeof binaryBlockSize;
|
|
161
|
+
|
|
72
162
|
/**
|
|
73
163
|
* Converts a linear index in an underlying data buffer to a linear index in an array view.
|
|
74
164
|
*
|
|
@@ -144,6 +234,30 @@ interface Namespace {
|
|
|
144
234
|
*/
|
|
145
235
|
broadcastArray: typeof broadcastArray;
|
|
146
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Broadcasts a scalar value to an ndarray having a specified shape.
|
|
239
|
+
*
|
|
240
|
+
* @param value - scalar value
|
|
241
|
+
* @param dtype - array data type
|
|
242
|
+
* @param shape - array shape
|
|
243
|
+
* @param order - array order
|
|
244
|
+
* @returns ndarray
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* var x = ns.broadcastScalar( 1.0, 'generic', [ 2, 2 ], 'row-major' );
|
|
248
|
+
* // returns <ndarray>
|
|
249
|
+
*
|
|
250
|
+
* var sh = x.shape;
|
|
251
|
+
* // returns [ 2, 2 ]
|
|
252
|
+
*
|
|
253
|
+
* var dt = x.dtype;
|
|
254
|
+
* // returns 'generic'
|
|
255
|
+
*
|
|
256
|
+
* var v = x.get( 0, 1 );
|
|
257
|
+
* // returns 1.0
|
|
258
|
+
*/
|
|
259
|
+
broadcastScalar: typeof broadcastScalar;
|
|
260
|
+
|
|
147
261
|
/**
|
|
148
262
|
* Broadcasts array shapes to a single shape.
|
|
149
263
|
*
|
|
@@ -518,9 +632,9 @@ interface Namespace {
|
|
|
518
632
|
dtypeStr2Enum: typeof dtypeStr2Enum;
|
|
519
633
|
|
|
520
634
|
/**
|
|
521
|
-
* Returns the C data type associated with a provided data type
|
|
635
|
+
* Returns the C data type associated with a provided data type value.
|
|
522
636
|
*
|
|
523
|
-
* @param dtype - data type
|
|
637
|
+
* @param dtype - data type value
|
|
524
638
|
* @returns C data type (or null)
|
|
525
639
|
*
|
|
526
640
|
* @example
|
|
@@ -556,6 +670,118 @@ interface Namespace {
|
|
|
556
670
|
*/
|
|
557
671
|
dtypes2signatures: typeof dtypes2signatures;
|
|
558
672
|
|
|
673
|
+
/**
|
|
674
|
+
* Creates an uninitialized array having a specified shape and data type.
|
|
675
|
+
*
|
|
676
|
+
* @param dtype - underlying data type
|
|
677
|
+
* @param shape - array shape
|
|
678
|
+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
|
|
679
|
+
* @returns output array
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* var arr = ns.empty( 'float32', [ 2, 2 ], 'row-major' );
|
|
683
|
+
* // returns <ndarray>
|
|
684
|
+
*
|
|
685
|
+
* var sh = arr.shape;
|
|
686
|
+
* // returns [ 2, 2 ]
|
|
687
|
+
*
|
|
688
|
+
* var dt = arr.dtype;
|
|
689
|
+
* // returns 'float32'
|
|
690
|
+
*/
|
|
691
|
+
empty: typeof empty;
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
|
|
695
|
+
*
|
|
696
|
+
* @param x - input array
|
|
697
|
+
* @returns output array
|
|
698
|
+
*
|
|
699
|
+
* @example
|
|
700
|
+
* var zeros = require( `@stdlib/ndarray/base/zeros` );
|
|
701
|
+
*
|
|
702
|
+
* var x = zeros( 'generic', [ 2, 2 ], 'row-major' );
|
|
703
|
+
* // returns <ndarray>
|
|
704
|
+
*
|
|
705
|
+
* var sh = x.shape;
|
|
706
|
+
* // returns [ 2, 2 ]
|
|
707
|
+
*
|
|
708
|
+
* var dt = x.dtype;
|
|
709
|
+
* // returns 'generic'
|
|
710
|
+
*
|
|
711
|
+
* var y = ns.emptyLike( x );
|
|
712
|
+
* // returns <ndarray>
|
|
713
|
+
*
|
|
714
|
+
* sh = y.shape;
|
|
715
|
+
* // returns [ 2, 2 ]
|
|
716
|
+
*
|
|
717
|
+
* dt = y.dtype;
|
|
718
|
+
* // returns 'generic'
|
|
719
|
+
*/
|
|
720
|
+
emptyLike: typeof emptyLike;
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* Expands the shape of an array by inserting a new dimension of size one at a specified axis.
|
|
724
|
+
*
|
|
725
|
+
* ## Notes
|
|
726
|
+
*
|
|
727
|
+
* - A provided axis must reside on the interval `[-N-1, N]`, where `N` is the rank (i.e., number of dimensions) of the provided input array. If provided a negative `axis`, the axis position at which to insert a singleton dimension is computed as `N + axis + 1`. Hence, if provided `-1`, the resolved axis position is `N` (i.e., a singleton dimension is appended to the input array).
|
|
728
|
+
*
|
|
729
|
+
* @param x - input array
|
|
730
|
+
* @param axis - axis at which to insert a singleton dimension
|
|
731
|
+
* @returns output array
|
|
732
|
+
*
|
|
733
|
+
* @example
|
|
734
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
735
|
+
*
|
|
736
|
+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
|
|
737
|
+
* // returns <ndarray>
|
|
738
|
+
*
|
|
739
|
+
* var shx = x.shape;
|
|
740
|
+
* // returns [ 2, 2 ]
|
|
741
|
+
*
|
|
742
|
+
* var y = ns.expandDimensions( x, 1 );
|
|
743
|
+
* // returns <ndarray>
|
|
744
|
+
*
|
|
745
|
+
* var shy = y.shape;
|
|
746
|
+
* // returns [ 2, 1, 2 ]
|
|
747
|
+
*
|
|
748
|
+
* var v = y.get( 0, 0, 0 );
|
|
749
|
+
* // returns 1
|
|
750
|
+
*
|
|
751
|
+
* v = y.get( 0, 0, 1 );
|
|
752
|
+
* // returns 2
|
|
753
|
+
*
|
|
754
|
+
* v = y.get( 1, 0, 0 );
|
|
755
|
+
* // returns 3
|
|
756
|
+
*
|
|
757
|
+
* v = y.get( 1, 0, 1 );
|
|
758
|
+
* // returns 4
|
|
759
|
+
*/
|
|
760
|
+
expandDimensions: typeof expandDimensions;
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Returns a zero-dimensional ndarray containing a provided scalar value.
|
|
764
|
+
*
|
|
765
|
+
* @param value - scalar value
|
|
766
|
+
* @param dtype - array data type
|
|
767
|
+
* @param order - memory layout (row-major or column-major)
|
|
768
|
+
* @returns zero-dimensional ndarray
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* var x = ns.scalar2ndarray( 1.0, 'generic', 'row-major' );
|
|
772
|
+
* // returns <ndarray>
|
|
773
|
+
*
|
|
774
|
+
* var sh = x.shape;
|
|
775
|
+
* // returns []
|
|
776
|
+
*
|
|
777
|
+
* var dt = x.dtype;
|
|
778
|
+
* // returns 'generic'
|
|
779
|
+
*
|
|
780
|
+
* var v = x.get();
|
|
781
|
+
* // returns 1.0
|
|
782
|
+
*/
|
|
783
|
+
scalar2ndarray: typeof scalar2ndarray;
|
|
784
|
+
|
|
559
785
|
/**
|
|
560
786
|
* Returns an index given an index mode.
|
|
561
787
|
*
|
|
@@ -767,6 +993,58 @@ interface Namespace {
|
|
|
767
993
|
*/
|
|
768
994
|
maxViewBufferIndex: typeof maxViewBufferIndex;
|
|
769
995
|
|
|
996
|
+
/**
|
|
997
|
+
* Broadcasts an ndarray to a specified shape if and only if the specified shape differs from the provided ndarray's shape.
|
|
998
|
+
*
|
|
999
|
+
* ## Notes
|
|
1000
|
+
*
|
|
1001
|
+
* - The function throws an error if a provided ndarray is incompatible with a provided shape.
|
|
1002
|
+
* - If a provided ndarray has the same shape as the specified shape, the function returns the provided ndarray.
|
|
1003
|
+
* - If a provided ndarray has a different (broadcast compatible) shape than the specified shape, the function returns a new (base) ndarray view of the provided ndarray's data. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned array, copy the array before performing operations which may mutate elements.
|
|
1004
|
+
* - A returned array view is a "base" ndarray, and, thus, a returned array view does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to broadcast an ndarray-like object within internal implementations and to do so with minimal overhead.
|
|
1005
|
+
*
|
|
1006
|
+
* @param arr - input array
|
|
1007
|
+
* @param shape - desired shape
|
|
1008
|
+
* @throws input array cannot have more dimensions than the desired shape
|
|
1009
|
+
* @throws input array dimension sizes must be `1` or equal to the corresponding dimension in the provided shape
|
|
1010
|
+
* @throws input array and desired shape must be broadcast compatible
|
|
1011
|
+
* @returns broadcasted array
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
1015
|
+
*
|
|
1016
|
+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
|
|
1017
|
+
* // returns <ndarray>
|
|
1018
|
+
*
|
|
1019
|
+
* var shx = x.shape;
|
|
1020
|
+
* // returns [ 2, 2 ]
|
|
1021
|
+
*
|
|
1022
|
+
* var y = ns.maybeBroadcastArray( x, [ 3, 2, 2 ] );
|
|
1023
|
+
* // returns <ndarray>
|
|
1024
|
+
*
|
|
1025
|
+
* var shy = y.shape;
|
|
1026
|
+
* // returns [ 3, 2, 2 ]
|
|
1027
|
+
*
|
|
1028
|
+
* var v = y.get( 0, 0, 0 );
|
|
1029
|
+
* // returns 1
|
|
1030
|
+
*
|
|
1031
|
+
* v = y.get( 0, 0, 1 );
|
|
1032
|
+
* // returns 2
|
|
1033
|
+
*
|
|
1034
|
+
* v = y.get( 1, 0, 0 );
|
|
1035
|
+
* // returns 1
|
|
1036
|
+
*
|
|
1037
|
+
* v = y.get( 1, 1, 0 );
|
|
1038
|
+
* // returns 3
|
|
1039
|
+
*
|
|
1040
|
+
* v = y.get( 2, 0, 0 );
|
|
1041
|
+
* // returns 1
|
|
1042
|
+
*
|
|
1043
|
+
* v = y.get( 2, 1, 1 );
|
|
1044
|
+
* // returns 4
|
|
1045
|
+
*/
|
|
1046
|
+
maybeBroadcastArray: typeof maybeBroadcastArray;
|
|
1047
|
+
|
|
770
1048
|
/**
|
|
771
1049
|
* Defines non-enumerable read-only properties which expose ndarray function meta data.
|
|
772
1050
|
*
|
|
@@ -885,6 +1163,26 @@ interface Namespace {
|
|
|
885
1163
|
*/
|
|
886
1164
|
minmaxViewBufferIndex: typeof minmaxViewBufferIndex;
|
|
887
1165
|
|
|
1166
|
+
/**
|
|
1167
|
+
* Converts an ndarray-like to an object likely to have the same "shape".
|
|
1168
|
+
*
|
|
1169
|
+
* ## Notes
|
|
1170
|
+
*
|
|
1171
|
+
* - This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to standardize the "shape" of the object holding ndarray meta data to ensure that internal functions operating on ndarrays are provided consistent argument "shapes".
|
|
1172
|
+
*
|
|
1173
|
+
* @param x - input ndarray
|
|
1174
|
+
* @returns object containing ndarray data
|
|
1175
|
+
*
|
|
1176
|
+
* @example
|
|
1177
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
1178
|
+
*
|
|
1179
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
|
|
1180
|
+
*
|
|
1181
|
+
* var obj = ns.ndarraylike2object( x );
|
|
1182
|
+
* // returns {...}
|
|
1183
|
+
*/
|
|
1184
|
+
ndarraylike2object: typeof ndarraylike2object;
|
|
1185
|
+
|
|
888
1186
|
/**
|
|
889
1187
|
* Returns the number of non-singleton dimensions.
|
|
890
1188
|
*
|
|
@@ -905,6 +1203,89 @@ interface Namespace {
|
|
|
905
1203
|
*/
|
|
906
1204
|
nonsingletonDimensions: typeof nonsingletonDimensions;
|
|
907
1205
|
|
|
1206
|
+
/**
|
|
1207
|
+
* Applies a nullary callback and assigns results to elements in an output ndarray.
|
|
1208
|
+
*
|
|
1209
|
+
* @param arrays - array-like object containing an output ndarray
|
|
1210
|
+
* @param fcn - nullary callback
|
|
1211
|
+
*
|
|
1212
|
+
* @example
|
|
1213
|
+
* var Float64Array = require( `@stdlib/array/float64` );
|
|
1214
|
+
* var ndarray = require( `@stdlib/ndarray/ctor` );
|
|
1215
|
+
*
|
|
1216
|
+
* function fcn() {
|
|
1217
|
+
* return 10.0;
|
|
1218
|
+
* }
|
|
1219
|
+
*
|
|
1220
|
+
* // Create data buffers:
|
|
1221
|
+
* var xbuf = new Float64Array( 12 );
|
|
1222
|
+
*
|
|
1223
|
+
* // Define the shape of the output array:
|
|
1224
|
+
* var shape = [ 3, 1, 2 ];
|
|
1225
|
+
*
|
|
1226
|
+
* // Define the array strides:
|
|
1227
|
+
* var sx = [ 4, 4, 1 ];
|
|
1228
|
+
*
|
|
1229
|
+
* // Define the index offset:
|
|
1230
|
+
* var ox = 1;
|
|
1231
|
+
*
|
|
1232
|
+
* // Create the output ndarray:
|
|
1233
|
+
* var x = ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' );
|
|
1234
|
+
*
|
|
1235
|
+
* // Apply the ns.nullary function:
|
|
1236
|
+
* ns.nullary( [ x ], fcn );
|
|
1237
|
+
*
|
|
1238
|
+
* console.log( x.data );
|
|
1239
|
+
* // => <Float64Array>[ 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 10.0, 0.0 ]
|
|
1240
|
+
*/
|
|
1241
|
+
nullary: typeof nullary;
|
|
1242
|
+
|
|
1243
|
+
/**
|
|
1244
|
+
* Reorders ndarray dimensions and associated strides for loop interchange.
|
|
1245
|
+
*
|
|
1246
|
+
* ## Notes
|
|
1247
|
+
*
|
|
1248
|
+
* - The returned object has the following properties:
|
|
1249
|
+
*
|
|
1250
|
+
* - **sh**: dimensions sorted in loop order.
|
|
1251
|
+
* - **sx**: ndarray strides sorted in loop order.
|
|
1252
|
+
*
|
|
1253
|
+
* - When iterating over the elements of a multi-dimensional array, accessing elements which are closer in memory can improve performance. To this end, loop interchange is a technique used in loop nest optimization to improve locality of reference and take advantage of CPU cache.
|
|
1254
|
+
*
|
|
1255
|
+
* The purpose of this function is to order ndarray dimensions according to the magnitude of array strides. By using the ordered dimensions and associated strides, one can construct nested loops (one for each dimension) such that the innermost loop iterates over the dimension in which array elements are closest in memory and the outermost loop iterates over the dimension in which array elements are farthest apart in memory. As a consequence, element iteration is optimized to minimize cache misses and ensure locality of reference.
|
|
1256
|
+
*
|
|
1257
|
+
* @param sh - array dimensions
|
|
1258
|
+
* @param sx - array stride lengths
|
|
1259
|
+
* @returns loop interchange data
|
|
1260
|
+
*
|
|
1261
|
+
* @example
|
|
1262
|
+
* var sh = [ 2, 3, 4 ];
|
|
1263
|
+
*
|
|
1264
|
+
* var sx = [ 12, 4, 1 ]; // row-major
|
|
1265
|
+
*
|
|
1266
|
+
* var o = ns.nullaryLoopOrder( sh, sx );
|
|
1267
|
+
* // returns {...}
|
|
1268
|
+
*
|
|
1269
|
+
* var ssh = o.sh;
|
|
1270
|
+
* // returns [ 4, 3, 2 ]
|
|
1271
|
+
*
|
|
1272
|
+
* var ssx = o.sx;
|
|
1273
|
+
* // returns [ 1, 4, 12 ]
|
|
1274
|
+
*/
|
|
1275
|
+
nullaryLoopOrder: typeof nullaryLoopOrder;
|
|
1276
|
+
|
|
1277
|
+
/**
|
|
1278
|
+
* Returns a loop block size for multi-dimensional array tiled loops.
|
|
1279
|
+
*
|
|
1280
|
+
* @param dtypeX - array data type
|
|
1281
|
+
* @returns block size (in units of elements)
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* var bsize = ns.nullaryBlockSize( 'float64' );
|
|
1285
|
+
* // returns <number>
|
|
1286
|
+
*/
|
|
1287
|
+
nullaryBlockSize: typeof nullaryBlockSize;
|
|
1288
|
+
|
|
908
1289
|
/**
|
|
909
1290
|
* Returns the number of elements in an array.
|
|
910
1291
|
*
|
|
@@ -917,6 +1298,147 @@ interface Namespace {
|
|
|
917
1298
|
*/
|
|
918
1299
|
numel: typeof numel;
|
|
919
1300
|
|
|
1301
|
+
/**
|
|
1302
|
+
* Returns the policy string associated with an output ndarray data type policy enumeration constant.
|
|
1303
|
+
*
|
|
1304
|
+
* @param policy - policy enumeration constant
|
|
1305
|
+
* @returns policy string
|
|
1306
|
+
*
|
|
1307
|
+
* @example
|
|
1308
|
+
* var str2enum = require( `@stdlib/ndarray/base/output-policy-str2enum` );
|
|
1309
|
+
*
|
|
1310
|
+
* var v = str2enum( 'same' );
|
|
1311
|
+
* // returns <number>
|
|
1312
|
+
*
|
|
1313
|
+
* var policy = ns.outputPolicyEnum2Str( v );
|
|
1314
|
+
* // returns 'same'
|
|
1315
|
+
*/
|
|
1316
|
+
outputPolicyEnum2Str: typeof outputPolicyEnum2Str;
|
|
1317
|
+
|
|
1318
|
+
/**
|
|
1319
|
+
* Returns the enumeration constant associated with an ndarray data type policy value.
|
|
1320
|
+
*
|
|
1321
|
+
* ## Notes
|
|
1322
|
+
*
|
|
1323
|
+
* - Downstream consumers of this function should **not** rely on specific integer values (e.g., `SAME == 0`). Instead, the function should be used in an opaque manner.
|
|
1324
|
+
*
|
|
1325
|
+
* @param policy - policy value
|
|
1326
|
+
* @returns enumeration constant
|
|
1327
|
+
*
|
|
1328
|
+
* @example
|
|
1329
|
+
* var v = ns.outputPolicyResolveEnum( 'same' );
|
|
1330
|
+
* // returns <number>
|
|
1331
|
+
*/
|
|
1332
|
+
outputPolicyResolveEnum: typeof outputPolicyResolveEnum;
|
|
1333
|
+
|
|
1334
|
+
/**
|
|
1335
|
+
* Returns the policy string associated with an output ndarray data type policy value.
|
|
1336
|
+
*
|
|
1337
|
+
* @param policy - policy value
|
|
1338
|
+
* @returns policy string
|
|
1339
|
+
*
|
|
1340
|
+
* @example
|
|
1341
|
+
* var str2enum = require( `@stdlib/ndarray/base/output-policy-str2enum` );
|
|
1342
|
+
*
|
|
1343
|
+
* var v = ns.outputPolicyResolveStr( str2enum( 'same' ) );
|
|
1344
|
+
* // returns 'same'
|
|
1345
|
+
*/
|
|
1346
|
+
outputPolicyResolveStr: typeof outputPolicyResolveStr;
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* Returns the enumeration constant associated with an output ndarray data type policy string.
|
|
1350
|
+
*
|
|
1351
|
+
* ## Notes
|
|
1352
|
+
*
|
|
1353
|
+
* - Downstream consumers of this function should **not** rely on specific integer values (e.g., `SAME == 0`). Instead, the function should be used in an opaque manner.
|
|
1354
|
+
*
|
|
1355
|
+
* @param policy - policy string
|
|
1356
|
+
* @returns enumeration constant
|
|
1357
|
+
*
|
|
1358
|
+
* @example
|
|
1359
|
+
* var v = ns.outputPolicyStr2Enum( 'same' );
|
|
1360
|
+
* // returns <number>
|
|
1361
|
+
*/
|
|
1362
|
+
outputPolicyStr2Enum: typeof outputPolicyStr2Enum;
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* Returns an array with a specified number of prepended singleton dimensions.
|
|
1366
|
+
*
|
|
1367
|
+
* @param x - input array
|
|
1368
|
+
* @param n - number of singleton dimensions to prepend
|
|
1369
|
+
* @returns output array
|
|
1370
|
+
*
|
|
1371
|
+
* @example
|
|
1372
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
1373
|
+
*
|
|
1374
|
+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
|
|
1375
|
+
* // returns <ndarray>
|
|
1376
|
+
*
|
|
1377
|
+
* var shx = x.shape;
|
|
1378
|
+
* // returns [ 2, 2 ]
|
|
1379
|
+
*
|
|
1380
|
+
* var y = ns.prependSingletonDimensions( x, 3 );
|
|
1381
|
+
* // returns <ndarray>
|
|
1382
|
+
*
|
|
1383
|
+
* var shy = y.shape;
|
|
1384
|
+
* // returns [ 1, 1, 1, 2, 2 ]
|
|
1385
|
+
*
|
|
1386
|
+
* var v = y.get( 0, 0, 0, 0, 0 );
|
|
1387
|
+
* // returns 1
|
|
1388
|
+
*
|
|
1389
|
+
* v = y.get( 0, 0, 0, 0, 1 );
|
|
1390
|
+
* // returns 2
|
|
1391
|
+
*
|
|
1392
|
+
* v = y.get( 0, 0, 0, 1, 0 );
|
|
1393
|
+
* // returns 3
|
|
1394
|
+
*
|
|
1395
|
+
* v = y.get( 0, 0, 0, 1, 1 );
|
|
1396
|
+
* // returns 4
|
|
1397
|
+
*/
|
|
1398
|
+
prependSingletonDimensions: typeof prependSingletonDimensions;
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Returns an array without singleton dimensions.
|
|
1402
|
+
*
|
|
1403
|
+
* ## Notes
|
|
1404
|
+
*
|
|
1405
|
+
* - If a provided ndarray does not have any singleton dimensions, the function returns the provided ndarray unchanged.
|
|
1406
|
+
* - If a provided ndarray does have singleton dimensions, the function returns a new ndarray view.
|
|
1407
|
+
*
|
|
1408
|
+
* @param x - input array
|
|
1409
|
+
* @returns squeezed array
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
1413
|
+
*
|
|
1414
|
+
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], {
|
|
1415
|
+
* 'ndmin': 5
|
|
1416
|
+
* });
|
|
1417
|
+
* // returns <ndarray>
|
|
1418
|
+
*
|
|
1419
|
+
* var shx = x.shape;
|
|
1420
|
+
* // returns [ 1, 1, 1, 2, 2 ]
|
|
1421
|
+
*
|
|
1422
|
+
* var y = ns.removeSingletonDimensions( x );
|
|
1423
|
+
* // returns <ndarray>
|
|
1424
|
+
*
|
|
1425
|
+
* var shy = y.shape;
|
|
1426
|
+
* // returns [ 2, 2 ]
|
|
1427
|
+
*
|
|
1428
|
+
* var v = y.get( 0, 0 );
|
|
1429
|
+
* // returns 1
|
|
1430
|
+
*
|
|
1431
|
+
* v = y.get( 0, 1 );
|
|
1432
|
+
* // returns 2
|
|
1433
|
+
*
|
|
1434
|
+
* v = y.get( 1, 0 );
|
|
1435
|
+
* // returns 3
|
|
1436
|
+
*
|
|
1437
|
+
* v = y.get( 1, 1 );
|
|
1438
|
+
* // returns 4
|
|
1439
|
+
*/
|
|
1440
|
+
removeSingletonDimensions: typeof removeSingletonDimensions;
|
|
1441
|
+
|
|
920
1442
|
/**
|
|
921
1443
|
* Serializes ndarray meta data.
|
|
922
1444
|
*
|
|
@@ -927,7 +1449,7 @@ interface Namespace {
|
|
|
927
1449
|
* - Meta data format:
|
|
928
1450
|
*
|
|
929
1451
|
* ```text
|
|
930
|
-
* | <endianness> (1 byte) | <dtype> (2 bytes) | <ndims> (8 bytes) | <shape> (ndims*8 bytes) | <strides> (ndims*8 bytes) | <offset> (8 bytes) | <order> (1 byte) | <mode> (1 byte) | <nsubmodes> (8 bytes) | <submodes> (nsubmodes*1 bytes) |
|
|
1452
|
+
* | <endianness> (1 byte) | <dtype> (2 bytes) | <ndims> (8 bytes) | <shape> (ndims*8 bytes) | <strides> (ndims*8 bytes) | <offset> (8 bytes) | <order> (1 byte) | <mode> (1 byte) | <nsubmodes> (8 bytes) | <submodes> (nsubmodes*1 bytes) | <flags> (4 bytes) |
|
|
931
1453
|
* ```
|
|
932
1454
|
*
|
|
933
1455
|
* which translates to the following `ArrayBuffer` layout:
|
|
@@ -944,6 +1466,7 @@ interface Namespace {
|
|
|
944
1466
|
* <mode>[int8],
|
|
945
1467
|
* <nsubmodes>[int64],
|
|
946
1468
|
* <submodes>[nsubmodes*int8]
|
|
1469
|
+
* <flags>[int32]
|
|
947
1470
|
* ]
|
|
948
1471
|
* ```
|
|
949
1472
|
*
|
|
@@ -954,13 +1477,13 @@ interface Namespace {
|
|
|
954
1477
|
* - Buffer length:
|
|
955
1478
|
*
|
|
956
1479
|
* ```text
|
|
957
|
-
* 1 + 2 + 8 + (ndims*8) + (ndims*8) + 8 + 1 + 1 + 8 + (nsubmodes*1) =
|
|
1480
|
+
* 1 + 2 + 8 + (ndims*8) + (ndims*8) + 8 + 1 + 1 + 8 + (nsubmodes*1) + 4 = 33 + (ndims*16) + nsubmodes
|
|
958
1481
|
* ```
|
|
959
1482
|
*
|
|
960
1483
|
* For example, consider a three-dimensional ndarray with one subscript index mode (submode):
|
|
961
1484
|
*
|
|
962
1485
|
* ```text
|
|
963
|
-
*
|
|
1486
|
+
* 33 + (3*16) + 1 = 82 bytes
|
|
964
1487
|
* ```
|
|
965
1488
|
*
|
|
966
1489
|
* - Views:
|
|
@@ -975,6 +1498,7 @@ interface Namespace {
|
|
|
975
1498
|
* - mode: `Int8Array( buf, 20+(ndims*16), 1 )`
|
|
976
1499
|
* - nsubmodes: `Int64Array( buf, 21+(ndims*16), 1 )`
|
|
977
1500
|
* - submodes: `Int8Array( buf, 29+(ndims*16), nsubmodes )`
|
|
1501
|
+
* - flags: `Int32Array( buf, 29+(ndims*16)+nsubmodes, 1)`
|
|
978
1502
|
*
|
|
979
1503
|
* @param x - input ndarray
|
|
980
1504
|
* @returns serialized meta data
|
|
@@ -1189,6 +1713,196 @@ interface Namespace {
|
|
|
1189
1713
|
*/
|
|
1190
1714
|
ndarray2array: typeof ndarray2array;
|
|
1191
1715
|
|
|
1716
|
+
/**
|
|
1717
|
+
* Transposes a matrix (or a stack of matrices).
|
|
1718
|
+
*
|
|
1719
|
+
* @param x - input array
|
|
1720
|
+
* @returns ndarray view
|
|
1721
|
+
*
|
|
1722
|
+
* @example
|
|
1723
|
+
* var array = require( `@stdlib/ndarray/array` );
|
|
1724
|
+
*
|
|
1725
|
+
* var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], {
|
|
1726
|
+
* 'dtype': 'generic'
|
|
1727
|
+
* });
|
|
1728
|
+
* // returns <ndarray>
|
|
1729
|
+
*
|
|
1730
|
+
* var sh = x.shape;
|
|
1731
|
+
* // returns [ 2, 3 ]
|
|
1732
|
+
*
|
|
1733
|
+
* var y = ns.transpose( x );
|
|
1734
|
+
* // returns <ndarray>
|
|
1735
|
+
*
|
|
1736
|
+
* sh = y.shape;
|
|
1737
|
+
* // returns [ 3, 2 ]
|
|
1738
|
+
*
|
|
1739
|
+
* var bool = ( x.get( 0, 1 ) === y.get( 1, 0 ) );
|
|
1740
|
+
* // returns true
|
|
1741
|
+
*
|
|
1742
|
+
* bool = ( x.data === y.data );
|
|
1743
|
+
* // returns true
|
|
1744
|
+
*/
|
|
1745
|
+
transpose: typeof transpose;
|
|
1746
|
+
|
|
1747
|
+
/**
|
|
1748
|
+
* Applies a unary callback to elements in an ndarray and assigns results to elements in an ndarray.
|
|
1749
|
+
*
|
|
1750
|
+
* @param arrays - array-like object containing one input ndarray and one output ndarray
|
|
1751
|
+
* @param fcn - unary callback
|
|
1752
|
+
* @throws arrays must have the same number of dimensions
|
|
1753
|
+
* @throws arrays must have the same shape
|
|
1754
|
+
*
|
|
1755
|
+
* @example
|
|
1756
|
+
* var Float64Array = require( `@stdlib/array/float64` );
|
|
1757
|
+
* var ndarray = require( `@stdlib/ndarray/ctor` );
|
|
1758
|
+
*
|
|
1759
|
+
* function scale( x ) {
|
|
1760
|
+
* return x * 10.0;
|
|
1761
|
+
* }
|
|
1762
|
+
*
|
|
1763
|
+
* // Create data buffers:
|
|
1764
|
+
* var xbuf = 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 ] );
|
|
1765
|
+
* var ybuf = new Float64Array( 6 );
|
|
1766
|
+
*
|
|
1767
|
+
* // Define the shape of the input and output arrays:
|
|
1768
|
+
* var shape = [ 3, 1, 2 ];
|
|
1769
|
+
*
|
|
1770
|
+
* // Define the array strides:
|
|
1771
|
+
* var sx = [ 4, 4, 1 ];
|
|
1772
|
+
* var sy = [ 2, 2, 1 ];
|
|
1773
|
+
*
|
|
1774
|
+
* // Define the index offsets:
|
|
1775
|
+
* var ox = 1;
|
|
1776
|
+
* var oy = 0;
|
|
1777
|
+
*
|
|
1778
|
+
* // Create the input and output ndarrays:
|
|
1779
|
+
* var x = ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' );
|
|
1780
|
+
* var y = ndarray( 'float64', ybuf, shape, sy, oy, 'row-major' );
|
|
1781
|
+
*
|
|
1782
|
+
* // Apply the ns.unary function:
|
|
1783
|
+
* ns.unary( [ x, y ], scale );
|
|
1784
|
+
*
|
|
1785
|
+
* console.log( y.data );
|
|
1786
|
+
* // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0, 100.0, 110.0 ]
|
|
1787
|
+
*/
|
|
1788
|
+
unary: typeof unary;
|
|
1789
|
+
|
|
1790
|
+
/**
|
|
1791
|
+
* Applies a unary function to elements in an ndarray and assigns results to elements in an ndarray.
|
|
1792
|
+
*
|
|
1793
|
+
* @param arrays - array-like object containing one input ndarray and one output ndarray
|
|
1794
|
+
* @param fcn - unary function to apply to callback return values
|
|
1795
|
+
* @param clbk - callback function
|
|
1796
|
+
* @param thisArg - callback execution context
|
|
1797
|
+
* @throws arrays must have the same number of dimensions
|
|
1798
|
+
* @throws arrays must have the same shape
|
|
1799
|
+
*
|
|
1800
|
+
* @example
|
|
1801
|
+
* var identity = require( `@stdlib/math/base/special/identity` );
|
|
1802
|
+
* var Float64Array = require( `@stdlib/array/float64` );
|
|
1803
|
+
* var ndarray = require( `@stdlib/ndarray/ctor` );
|
|
1804
|
+
*
|
|
1805
|
+
* function scale( x ) {
|
|
1806
|
+
* return x * 10.0;
|
|
1807
|
+
* }
|
|
1808
|
+
*
|
|
1809
|
+
* // Create data buffers:
|
|
1810
|
+
* var xbuf = 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 ] );
|
|
1811
|
+
* var ybuf = new Float64Array( 6 );
|
|
1812
|
+
*
|
|
1813
|
+
* // Define the shape of the input and output arrays:
|
|
1814
|
+
* var shape = [ 3, 1, 2 ];
|
|
1815
|
+
*
|
|
1816
|
+
* // Define the array strides:
|
|
1817
|
+
* var sx = [ 4, 4, 1 ];
|
|
1818
|
+
* var sy = [ 2, 2, 1 ];
|
|
1819
|
+
*
|
|
1820
|
+
* // Define the index offsets:
|
|
1821
|
+
* var ox = 1;
|
|
1822
|
+
* var oy = 0;
|
|
1823
|
+
*
|
|
1824
|
+
* // Create the input and output ndarrays:
|
|
1825
|
+
* var x = ndarray( 'float64', xbuf, shape, sx, ox, 'row-major' );
|
|
1826
|
+
* var y = ndarray( 'float64', ybuf, shape, sy, oy, 'row-major' );
|
|
1827
|
+
*
|
|
1828
|
+
* // Apply the unary function:
|
|
1829
|
+
* ns.unaryBy( [ x, y ], scale, identity );
|
|
1830
|
+
*
|
|
1831
|
+
* console.log( y.data );
|
|
1832
|
+
* // => <Float64Array>[ 20.0, 30.0, 60.0, 70.0, 100.0, 110.0 ]
|
|
1833
|
+
*/
|
|
1834
|
+
unaryBy: typeof unaryBy;
|
|
1835
|
+
|
|
1836
|
+
/**
|
|
1837
|
+
* Reorders ndarray dimensions and associated strides for loop interchange.
|
|
1838
|
+
*
|
|
1839
|
+
* ## Notes
|
|
1840
|
+
*
|
|
1841
|
+
* - The returned object has the following properties:
|
|
1842
|
+
*
|
|
1843
|
+
* - **sh**: dimensions sorted in loop order.
|
|
1844
|
+
* - **sx**: input ndarray strides sorted in loop order.
|
|
1845
|
+
* - **sy**: output ndarray strides sorted in loop order.
|
|
1846
|
+
*
|
|
1847
|
+
* - When iterating over the elements of a multi-dimensional array, accessing elements which are closer in memory can improve performance. To this end, loop interchange is a technique used in loop nest optimization to improve locality of reference and take advantage of CPU cache.
|
|
1848
|
+
*
|
|
1849
|
+
* The purpose of this function is to order ndarray dimensions according to the magnitude of **input array** strides. By using the ordered dimensions and associated strides, one can construct nested loops (one for each dimension) such that the innermost loop iterates over the dimension in which array elements are closest in memory and the outermost loop iterates over the dimension in which array elements are farthest apart in memory. As a consequence, element iteration is optimized to minimize cache misses and ensure locality of reference.
|
|
1850
|
+
*
|
|
1851
|
+
* - Cache performance may be degraded if the layout order (i.e., row-major or column-major) differs for the input and output ndarrays. This function is intended to optimize cache performance for the input ndarray. If the output ndarray has a different layout order (e.g., if the input ndarray is row-major and the output ndarray is column-major), cache misses are likely for the output ndarray. In general, to ensure best performance, input and output ndarrays should have the same layout order.
|
|
1852
|
+
*
|
|
1853
|
+
* - The function assumes that the input and output ndarrays have the same shape. Hence, loop interchange order should only be determined **after** broadcasting.
|
|
1854
|
+
*
|
|
1855
|
+
* @param sh - array dimensions
|
|
1856
|
+
* @param sx - input array stride lengths
|
|
1857
|
+
* @param sy - output array stride lengths
|
|
1858
|
+
* @returns loop interchange data
|
|
1859
|
+
*
|
|
1860
|
+
* @example
|
|
1861
|
+
* var sh = [ 2, 3, 4 ];
|
|
1862
|
+
*
|
|
1863
|
+
* var sx = [ 12, 4, 1 ]; // row-major
|
|
1864
|
+
* var sy = [ 1, -2, 6 ]; // column-major
|
|
1865
|
+
*
|
|
1866
|
+
* var o = ns.unaryLoopOrder( sh, sx, sy );
|
|
1867
|
+
* // returns {...}
|
|
1868
|
+
*
|
|
1869
|
+
* var ssh = o.sh;
|
|
1870
|
+
* // returns [ 4, 3, 2 ]
|
|
1871
|
+
*
|
|
1872
|
+
* var ssx = o.sx;
|
|
1873
|
+
* // returns [ 1, 4, 12 ]
|
|
1874
|
+
*
|
|
1875
|
+
* var ssy = o.sy;
|
|
1876
|
+
* // returns [ 6, -2, 1 ]
|
|
1877
|
+
*/
|
|
1878
|
+
unaryLoopOrder: typeof unaryLoopOrder;
|
|
1879
|
+
|
|
1880
|
+
/**
|
|
1881
|
+
* Resolves the output ndarray data type for a unary function.
|
|
1882
|
+
*
|
|
1883
|
+
* @param dtype - input ndarray data type
|
|
1884
|
+
* @param policy - output ndarray data type policy
|
|
1885
|
+
* @returns output ndarray data type
|
|
1886
|
+
*
|
|
1887
|
+
* @example
|
|
1888
|
+
* var dt = ns.unaryOutputDataType( 'float64', 'complex_floating_point' );
|
|
1889
|
+
* // returns <string>
|
|
1890
|
+
*/
|
|
1891
|
+
unaryOutputDataType: typeof unaryOutputDataType;
|
|
1892
|
+
|
|
1893
|
+
/**
|
|
1894
|
+
* Returns a loop block size for multi-dimensional array tiled loops.
|
|
1895
|
+
*
|
|
1896
|
+
* @param dtypeX - input array data type
|
|
1897
|
+
* @param dtypeY - output array data type
|
|
1898
|
+
* @returns block size (in units of elements)
|
|
1899
|
+
*
|
|
1900
|
+
* @example
|
|
1901
|
+
* var bsize = ns.unaryBlockSize( 'float64', 'float64' );
|
|
1902
|
+
* // returns <number>
|
|
1903
|
+
*/
|
|
1904
|
+
unaryBlockSize: typeof unaryBlockSize;
|
|
1905
|
+
|
|
1192
1906
|
/**
|
|
1193
1907
|
* Converts a linear index in an array view to a linear index in an underlying data buffer.
|
|
1194
1908
|
*
|
|
@@ -1231,6 +1945,55 @@ interface Namespace {
|
|
|
1231
1945
|
* // returns 6
|
|
1232
1946
|
*/
|
|
1233
1947
|
wrapIndex: typeof wrapIndex;
|
|
1948
|
+
|
|
1949
|
+
/**
|
|
1950
|
+
* Creates a zero-filled array having a specified shape and data type.
|
|
1951
|
+
*
|
|
1952
|
+
* @param dtype - underlying data type
|
|
1953
|
+
* @param shape - array shape
|
|
1954
|
+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
|
|
1955
|
+
* @returns zero-filled array
|
|
1956
|
+
*
|
|
1957
|
+
* @example
|
|
1958
|
+
* var arr = ns.zeros( 'float32', [ 2, 2 ], 'row-major' );
|
|
1959
|
+
* // returns <ndarray>
|
|
1960
|
+
*
|
|
1961
|
+
* var sh = arr.shape;
|
|
1962
|
+
* // returns [ 2, 2 ]
|
|
1963
|
+
*
|
|
1964
|
+
* var dt = arr.dtype;
|
|
1965
|
+
* // returns 'float32'
|
|
1966
|
+
*/
|
|
1967
|
+
zeros: typeof zeros;
|
|
1968
|
+
|
|
1969
|
+
/**
|
|
1970
|
+
* Creates a zero-filled array having the same shape and data type as a provided input ndarray.
|
|
1971
|
+
*
|
|
1972
|
+
* @param x - input array
|
|
1973
|
+
* @returns zero-filled array
|
|
1974
|
+
*
|
|
1975
|
+
* @example
|
|
1976
|
+
* var zeros = require( `@stdlib/ndarray/base/zeros` );
|
|
1977
|
+
*
|
|
1978
|
+
* var x = zeros( 'generic', [ 2, 2 ], 'row-major' );
|
|
1979
|
+
* // returns <ndarray>
|
|
1980
|
+
*
|
|
1981
|
+
* var sh = x.shape;
|
|
1982
|
+
* // returns [ 2, 2 ]
|
|
1983
|
+
*
|
|
1984
|
+
* var dt = x.dtype;
|
|
1985
|
+
* // returns 'generic'
|
|
1986
|
+
*
|
|
1987
|
+
* var y = ns.zerosLike( x );
|
|
1988
|
+
* // returns <ndarray>
|
|
1989
|
+
*
|
|
1990
|
+
* sh = y.shape;
|
|
1991
|
+
* // returns [ 2, 2 ]
|
|
1992
|
+
*
|
|
1993
|
+
* dt = y.dtype;
|
|
1994
|
+
* // returns 'generic'
|
|
1995
|
+
*/
|
|
1996
|
+
zerosLike: typeof zerosLike;
|
|
1234
1997
|
}
|
|
1235
1998
|
|
|
1236
1999
|
/**
|