Repository navigation
Expand file tree
/
Copy pathNdArray.java
More file actions
302 lines (286 loc) · 12.8 KB
/
Copy pathNdArray.java
File metadata and controls
302 lines (286 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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.
=======================================================================
*/
package org.tensorflow.ndarray;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.tensorflow.ndarray.buffer.DataBuffer;
import org.tensorflow.ndarray.index.Index;
/**
* A data structure of N-dimensions.
*
* <p>The `NdArray` interface creates an abstraction between the physical storage of a data record,
* which can be linear or segmented, and its logical representation. In general, they achieve
* better performances than standard multi-dimensional arrays in Java by mapping directly linear
* data segments in memory.
*
* <p>Like {@link DataBuffer}, {@code NdArray} instances support 64-bits indexing so they can be
* used to map very large data records. They also support special coordinates that allow traversing
* their values in any direction or to select only a subset of them.
*
* <p>Example of usage:
* <pre>{@code
* // Creates a 2x3x2 matrix (of rank 3)
* FloatNdArray matrix3d = NdArrays.ofFloats(shape(2, 3, 2));
*
* // Initialize sub-matrices data with vectors
* matrix.set(NdArrays.vectorOf(1.0f, 2.0f), 0, 0)
* .set(NdArrays.vectorOf(3.0f, 4.0f), 0, 1)
* .set(NdArrays.vectorOf(5.0f, 6.0f), 0, 2)
* .set(NdArrays.vectorOf(7.0f, 8.0f), 1, 0)
* .set(NdArrays.vectorOf(9.0f, 10.0f), 1, 1)
* .set(NdArrays.vectorOf(11.0f, 12.0f), 1, 2);
*
* // Access the second 3x2 matrix (of rank 2)
* FloatNdArray matrix = matrix3d.get(1);
*
* // Access directly the float value at (1, 0) from the second matrix
* assertEquals(9.0f, matrix.getFloat(1, 0));
* }</pre>
*
* @param <T> the type of values to be mapped
*/
public interface NdArray<T> extends Shaped {
/**
* Returns a sequence of all elements at a given dimension.
*
* <p>Logically, the N-dimensional array can be flatten in a single vector, where the scalars of
* the {@code (n - 1)}th element precedes those of the {@code (n)}th element, for a total of
* {@link #size()} values.
*
* <p>For example, given a {@code n x m} matrix on the {@code [x, y]} axes, elements are iterated in
* the following order:
* <p>x<sub>0</sub>y<sub>0</sub>, x<sub>0</sub>y<sub>1</sub>, ..., x<sub>0</sub>y<sub>m-1</sub>, x<sub>1</sub>y<sub>0</sub>, x<sub>1</sub>y<sub>1</sub>, ..., x<sub>n-1</sub>y<sub>m-1</sub>
*
* <p>The returned sequence can then be iterated to visit each elements, either by calling
* {@link NdArraySequence#forEach(Consumer)} or {@link NdArraySequence#forEachIndexed(BiConsumer)}.
* <pre>{@code
* // Iterate matrix for initializing each of its vectors
* matrixOfFloats.elements(0).forEach(v -> {
* v.set(vector(1.0f, 2.0f, 3.0f));
* });
*
* // Iterate a vector for reading each of its scalar
* vectorOfFloats.scalars().forEachIdx((coords, s) -> {
* System.out.println("Value " + s.getFloat() + " found at " + coords);
* });
* }</pre>
*
* @param dimensionIdx index of the dimension
* @return an {@code NdArray} sequence
* @throws IllegalArgumentException if {@code dimensionIdx} is greater or equal to the total
* number of dimensions of this array
*/
NdArraySequence<? extends NdArray<T>> elements(int dimensionIdx);
/**
* Returns a sequence of all scalars in this array.
*
* <p>This is equivalent to call {@code elements(shape().numDimensions() - 1)}
*
* @return an {@code NdArray} sequence
*/
NdArraySequence<? extends NdArray<T>> scalars();
/**
* Creates a multi-dimensional view (or slice) of this array by mapping one or more dimensions
* to the given index selectors.
*
* <p>Slices allow to traverse an N-dimensional array in any of its axis and/or to filter only
* elements of interest. For example, for a given matrix on the {@code [x, y]} axes, it is
* possible to iterate elements at {@code y=0} for all {@code x}.
*
* <p>Any changes applied to the returned slice affect the data of this array as well, as there
* is no copy involved.
*
* <p>Example of usage:
* <pre>{@code
* FloatNdArray matrix3d = NdArrays.ofFloats(shape(3, 2, 4)); // with [x, y, z] axes
*
* // Iterates elements on the x axis by preserving only the 3rd value on the z axis,
* // (i.e. [x, y, 2])
* matrix3d.slice(all(), all(), at(2)).elements(0).forEach(m -> {
* assertEquals(shape(2), m); // y=2, z=0 (scalar)
* });
*
* // Creates a slice that contains only the last element of the y axis and elements with an
* // odd `z` coordinate.
* FloatNdArray slice = matrix3d.slice(all(), at(1), odd());
* assertEquals(shape(3, 2), slice.shape()); // x=3, y=0 (scalar), z=2 (odd coordinates)
*
* // Iterates backward the elements on the x axis
* matrix3d.slice(flip()).elements(0).forEach(m -> {
* assertEquals(shape(2, 4), m); // y=2, z=4
* });
* }</pre>
*
* @param indices index selectors per dimensions, starting from dimension 0 of this array.
* @return the element resulting of the index selection
* @throws IndexOutOfBoundsException if some coordinates are outside the limits of their
* respective dimension
*/
NdArray<T> slice(Index... indices);
/**
* Returns the N-dimensional element of this array at the given coordinates.
*
* <p>Elements of any of the dimensions of this array can be retrieved. For example, if the number
* of coordinates is equal to the number of dimensions of this array, then a rank-0 (scalar) array
* is returned, which value can then be obtained by calling `array.getObject()`.
*
* <p>Any changes applied to the returned elements affect the data of this array as well, as there
* is no copy involved.
*
* <p>Note that invoking this method is an equivalent and more efficient way to slice this array
* on single scalar, i.e. {@code array.get(x, y, z)} is equal to
* {@code array.slice(at(x), at(y), at(z))}
*
* @param coordinates coordinates of the element to access, none will return this array
* @return the element at this index
* @throws IndexOutOfBoundsException if some coordinates are outside the limits of their
* respective dimension
*/
NdArray<T> get(long... coordinates);
/**
* Assigns the value of the N-dimensional element found at the given coordinates.
*
* <p>The number of coordinates provided can be anywhere between 0 and rank - 1. For example:
* <pre>{@code
* FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2
* matrix.set(vector(10.0f, 20.0f), 0); // success
* matrix.set(scalar(10.0f), 1, 0); // success
* }</pre>
*
* @param src an array of the values to assign
* @param coordinates coordinates of the element to assign
* @return this array
* @throws IndexOutOfBoundsException if some coordinates are outside the limits of their
* respective dimension
*/
NdArray<T> set(NdArray<T> src, long... coordinates);
/**
* Returns the value of the scalar found at the given coordinates.
*
* <p>To access the scalar element, the number of coordinates provided must be equal to the number
* of dimensions of this array (i.e. its rank). For example:
* <pre>{@code
* FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2
* matrix.getObject(0, 1); // succeeds, returns 0.0f
* matrix.getObject(0); // throws IllegalRankException
*
* FloatNdArray scalar = matrix.get(0, 1); // scalar rank = 0
* scalar.getObject(); // succeeds, returns 0.0f
* }</pre>
*
* Note: if this array stores values of a primitive type, prefer the usage of the specialized
* method in the subclass for that type. For example, {@code floatArray.getFloat(0); }.
*
* @param coordinates coordinates of the scalar to resolve
* @return value of that scalar
* @throws IndexOutOfBoundsException if some coordinates are outside the limits of their
* respective dimension
* @throws IllegalRankException if number of coordinates is not sufficient to access a scalar
* element
*/
T getObject(long... coordinates);
/**
* Assigns the value of the scalar found at the given coordinates.
*
* <p>To access the scalar element, the number of coordinates provided must be equal to the number
* of dimensions of this array (i.e. its rank). For example:
* <pre>{@code
* FloatNdArray matrix = NdArrays.ofFloats(shape(2, 2)); // matrix rank = 2
* matrix.setObject(10.0f, 0, 1); // succeeds
* matrix.setObject(10.0f, 0); // throws IllegalRankException
*
* FloatNdArray scalar = matrix.get(0, 1); // scalar rank = 0
* scalar.setObject(10.0f); // succeeds
* }</pre>
*
* Note: if this array stores values of a primitive type, prefer the usage of the specialized
* method in the subclass for that type. For example, {@code floatArray.setFloat(10.0f, 0); }
*
* @param value the value to assign
* @param coordinates coordinates of the scalar to assign
* @return this array
* @throws IndexOutOfBoundsException if some coordinates are outside the limits of their
* respective dimension
* @throws IllegalRankException if number of coordinates is not sufficient to access a scalar
* element
*/
NdArray<T> setObject(T value, long... coordinates);
/**
* Copy the content of this array to the destination array.
*
* <p>The {@link #shape()} of the destination array must be equal to the shape of this array, or
* an exception is thrown. After the copy, the content of both arrays can be altered
* independently, without affecting each other.
*
* @param dst array to receive a copy of the content of this array
* @return this array
* @throws IllegalArgumentException if the shape of {@code dst} is not equal to the shape of this
* array
*/
NdArray<T> copyTo(NdArray<T> dst);
/**
* Read the content of this N-dimensional array into the destination buffer.
*
* <p>The size of the buffer must be equal or greater to the {@link #size()} of this
* array, or an exception is thrown. After the copy, content of the buffer and of the array can be
* altered independently, without affecting each other.
*
* @param dst the destination buffer
* @return this array
* @throws java.nio.BufferOverflowException if the buffer cannot hold the content of this array
* @see DataBuffer#size()
*/
NdArray<T> read(DataBuffer<T> dst);
/**
* Write the content of this N-dimensional array from the source buffer.
*
* <p>The size of the buffer must be equal or greater to the {@link #size()} of this
* array, or an exception is thrown. After the copy, content of the buffer and of the array can be
* altered independently, without affecting each other.
*
* @param src the source buffer
* @return this array
* @throws java.nio.BufferUnderflowException if the buffer has not enough remaining data to write
* into this array
* @see DataBuffer#size()
*/
NdArray<T> write(DataBuffer<T> src);
/**
* Checks equality between n-dimensional arrays.
*
* <p>An array is equal to another object if this object is another {@link NdArray} of the
* same shape, type and the elements are equal and in the same order. For example:
*
* <pre>{@code
* IntNdArray array = NdArrays.ofInts(Shape.of(2, 2))
* .set(NdArrays.vectorOf(1, 2), 0)
* .set(NdArrays.vectorOf(3, 4), 1);
*
* assertEquals(array, StdArrays.ndCopyOf(new int[][] {{1, 2}, {3, 4}})); // true
* assertEquals(array, StdArrays.ndCopyOf(new Integer[][] {{1, 2}, {3, 4}})); // true, as Integers are equal to ints
* assertNotEquals(array, NdArrays.vectorOf(1, 2, 3, 4)); // false, different shapes
* assertNotEquals(array, StdArrays.ndCopyOf(new int[][] {{3, 4}, {1, 2}})); // false, different order
* assertNotEquals(array, StdArrays.ndCopyOf(new long[][] {{1L, 2L}, {3L, 4L}})); // false, different types
* }</pre>
*
* <p>Note that the computation required to verify equality between two arrays can be expensive
* in some cases and therefore, it is recommended to not use this method in a critical path
* where performances matter.
*
* @param obj object to compare this array with
* @return true if this array is equal to the provided object
*/
@Override
boolean equals(Object obj);
}