/
ShapeUtils.java
648 lines (584 loc) · 14.6 KB
/
ShapeUtils.java
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*******************************************************************************
* Copyright (c) 2016 Diamond Light Source Ltd. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Diamond Light Source Ltd - initial API and implementation
*******************************************************************************/
package org.eclipse.january.dataset;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
public class ShapeUtils {
private ShapeUtils() {
}
/**
* Calculate total number of items in given shape
* @param shape
* @return size
*/
public static long calcLongSize(final int[] shape) {
if (shape == null) { // special case of null-shaped
return 0;
}
final int rank = shape.length;
if (rank == 0) { // special case of zero-rank shape
return 1;
}
double dsize = 1.0;
for (int i = 0; i < rank; i++) {
// make sure the indexes isn't zero or negative
if (shape[i] == 0) {
return 0;
} else if (shape[i] < 0) {
throw new IllegalArgumentException(String.format(
"The %d-th is %d which is not allowed as it is negative", i, shape[i]));
}
dsize *= shape[i];
}
// check to see if the size is larger than an integer, i.e. we can't allocate it
if (dsize > Long.MAX_VALUE) {
throw new IllegalArgumentException("Size of the dataset is too large to allocate");
}
return (long) dsize;
}
/**
* Calculate total number of items in given shape
* @param shape
* @return size
*/
public static int calcSize(final int[] shape) {
long lsize = calcLongSize(shape);
// check to see if the size is larger than an integer, i.e. we can't allocate it
if (lsize > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Size of the dataset is too large to allocate");
}
return (int) lsize;
}
/**
* Check if shapes are broadcast compatible
*
* @param ashape
* @param bshape
* @return true if they are compatible
*/
public static boolean areShapesBroadcastCompatible(final int[] ashape, final int[] bshape) {
if (ashape == null || bshape == null) {
return ashape == bshape;
}
if (ashape.length < bshape.length) {
return areShapesBroadcastCompatible(bshape, ashape);
}
for (int a = ashape.length - bshape.length, b = 0; a < ashape.length && b < bshape.length; a++, b++) {
if (ashape[a] != bshape[b] && ashape[a] != 1 && bshape[b] != 1) {
return false;
}
}
return true;
}
/**
* Check if shapes are compatible, ignoring extra axes of length 1
*
* @param ashape
* @param bshape
* @return true if they are compatible
*/
public static boolean areShapesCompatible(final int[] ashape, final int[] bshape) {
if (ashape == null || bshape == null) {
return ashape == bshape;
}
List<Integer> alist = new ArrayList<Integer>();
for (int a : ashape) {
if (a > 1) alist.add(a);
}
final int imax = alist.size();
int i = 0;
for (int b : bshape) {
if (b == 1)
continue;
if (i >= imax || b != alist.get(i++))
return false;
}
return i == imax;
}
/**
* Check if shapes are compatible but skip axis
*
* @param ashape
* @param bshape
* @param axis
* @return true if they are compatible
*/
public static boolean areShapesCompatible(final int[] ashape, final int[] bshape, final int axis) {
if (ashape == null || bshape == null) {
return ashape == bshape;
}
if (ashape.length != bshape.length) {
return false;
}
final int rank = ashape.length;
for (int i = 0; i < rank; i++) {
if (i != axis && ashape[i] != bshape[i]) {
return false;
}
}
return true;
}
/**
* Remove dimensions of 1 in given shape - from both ends only, if true
*
* @param oshape
* @param onlyFromEnds
* @return newly squeezed shape (or original if unsqueezed)
*/
public static int[] squeezeShape(final int[] oshape, boolean onlyFromEnds) {
int unitDims = 0;
int rank = oshape.length;
int start = 0;
if (onlyFromEnds) {
int i = rank - 1;
for (; i >= 0; i--) {
if (oshape[i] == 1) {
unitDims++;
} else {
break;
}
}
for (int j = 0; j <= i; j++) {
if (oshape[j] == 1) {
unitDims++;
} else {
start = j;
break;
}
}
} else {
for (int i = 0; i < rank; i++) {
if (oshape[i] == 1) {
unitDims++;
}
}
}
if (unitDims == 0) {
return oshape;
}
int[] newDims = new int[rank - unitDims];
if (unitDims == rank)
return newDims; // zero-rank dataset
if (onlyFromEnds) {
rank = newDims.length;
for (int i = 0; i < rank; i++) {
newDims[i] = oshape[i+start];
}
} else {
int j = 0;
for (int i = 0; i < rank; i++) {
if (oshape[i] > 1) {
newDims[j++] = oshape[i];
if (j >= newDims.length)
break;
}
}
}
return newDims;
}
/**
* Remove dimension of 1 in given shape
*
* @param oshape
* @param axis
* @return newly squeezed shape
*/
public static int[] squeezeShape(final int[] oshape, int axis) {
if (oshape == null) {
return null;
}
final int rank = oshape.length;
if (rank == 0) {
return new int[0];
}
if (axis < 0) {
axis += rank;
}
if (axis < 0 || axis >= rank) {
throw new IllegalArgumentException("Axis argument is outside allowed range");
}
int[] nshape = new int[rank-1];
for (int i = 0; i < axis; i++) {
nshape[i] = oshape[i];
}
for (int i = axis+1; i < rank; i++) {
nshape[i-1] = oshape[i];
}
return nshape;
}
/**
* Get shape from object (array or list supported)
* @param obj
* @return shape can be null if obj is null
*/
public static int[] getShapeFromObject(final Object obj) {
if (obj == null) {
return null;
}
ArrayList<Integer> lshape = new ArrayList<Integer>();
getShapeFromObj(lshape, obj, 0);
final int rank = lshape.size();
final int[] shape = new int[rank];
for (int i = 0; i < rank; i++) {
shape[i] = lshape.get(i);
}
return shape;
}
/**
* Get shape from object
* @param ldims
* @param obj
* @param depth
* @return true if there is a possibility of differing lengths
*/
private static boolean getShapeFromObj(final ArrayList<Integer> ldims, Object obj, int depth) {
if (obj == null)
return true;
if (obj instanceof List<?>) {
List<?> jl = (List<?>) obj;
int l = jl.size();
updateShape(ldims, depth, l);
for (int i = 0; i < l; i++) {
Object lo = jl.get(i);
if (!getShapeFromObj(ldims, lo, depth + 1)) {
break;
}
}
return true;
}
Class<? extends Object> ca = obj.getClass().getComponentType();
if (ca != null) {
final int l = Array.getLength(obj);
updateShape(ldims, depth, l);
if (DTypeUtils.isClassSupportedAsElement(ca)) {
return true;
}
for (int i = 0; i < l; i++) {
Object lo = Array.get(obj, i);
if (!getShapeFromObj(ldims, lo, depth + 1)) {
break;
}
}
return true;
} else if (obj instanceof IDataset) {
int[] s = ((IDataset) obj).getShape();
for (int i = 0; i < s.length; i++) {
updateShape(ldims, depth++, s[i]);
}
return true;
} else {
return false; // not an array of any type
}
}
private static void updateShape(final ArrayList<Integer> ldims, final int depth, final int l) {
if (depth >= ldims.size()) {
ldims.add(l);
} else if (l > ldims.get(depth)) {
ldims.set(depth, l);
}
}
/**
* Get n-D position from given index
* @param n index
* @param shape
* @return n-D position
*/
public static int[] getNDPositionFromShape(int n, int[] shape) {
if (shape == null) {
return null;
}
int rank = shape.length;
if (rank == 0) {
return new int[0];
}
if (rank == 1) {
return new int[] { n };
}
int[] output = new int[rank];
for (rank--; rank > 0; rank--) {
output[rank] = n % shape[rank];
n /= shape[rank];
}
output[0] = n;
return output;
}
/**
* Get flattened view index of given position
* @param shape
* @param pos
* the integer array specifying the n-D position
* @return the index on the flattened dataset
*/
public static int getFlat1DIndex(final int[] shape, final int[] pos) {
final int imax = pos.length;
if (imax == 0) {
return 0;
}
return AbstractDataset.get1DIndexFromShape(shape, pos);
}
/**
* This function takes a dataset and checks its shape against another dataset. If they are both of the same size,
* then this returns with no error, if there is a problem, then an error is thrown.
*
* @param g
* The first dataset to be compared
* @param h
* The second dataset to be compared
* @throws IllegalArgumentException
* This will be thrown if there is a problem with the compatibility
*/
public static void checkCompatibility(final ILazyDataset g, final ILazyDataset h) throws IllegalArgumentException {
if (!areShapesCompatible(g.getShape(), h.getShape())) {
throw new IllegalArgumentException("Shapes do not match");
}
}
/**
* Check that axis is in range [-rank,rank)
*
* @param rank
* @param axis
* @return sanitized axis in range [0, rank)
* @since 2.1
*/
public static int checkAxis(int rank, int axis) {
if (axis < 0) {
axis += rank;
}
if (axis < 0 || axis >= rank) {
throw new IllegalArgumentException("Axis " + axis + " given is out of range [0, " + rank + ")");
}
return axis;
}
private static int[] convert(Collection<Integer> list) {
int[] array = new int[list.size()];
int i = 0;
for (Integer l : list) {
array[i++] = l;
}
return array;
}
/**
* Check that all axes are in range [-rank,rank)
* @param rank
* @param axes
* @return sanitized axes in range [0, rank) and sorted in increasing order
* @since 2.2
*/
public static int[] checkAxes(int rank, int... axes) {
return convert(sanitizeAxes(rank, axes));
}
/**
* Check that all axes are in range [-rank,rank)
* @param rank
* @param axes
* @return sanitized axes in range [0, rank) and sorted in increasing order
* @since 2.2
*/
private static SortedSet<Integer> sanitizeAxes(int rank, int... axes) {
SortedSet<Integer> nAxes = new TreeSet<>();
for (int i = 0; i < axes.length; i++) {
nAxes.add(checkAxis(rank, axes[i]));
}
return nAxes;
}
/**
* @param rank
* @param axes
* @return remaining axes not given by input
* @since 2.2
*/
public static int[] getRemainingAxes(int rank, int... axes) {
SortedSet<Integer> nAxes = sanitizeAxes(rank, axes);
int[] remains = new int[rank - axes.length];
int j = 0;
for (int i = 0; i < rank; i++) {
if (!nAxes.contains(i)) {
remains[j++] = i;
}
}
return remains;
}
/**
* Remove axes from shape
* @param shape
* @param axes
* @return reduced shape
* @since 2.2
*/
public static int[] reduceShape(int[] shape, int... axes) {
int[] remain = getRemainingAxes(shape.length, axes);
for (int i = 0; i < remain.length; i++) {
int a = remain[i];
remain[i] = shape[a];
}
return remain;
}
/**
* Set reduced axes to 1
* @param shape
* @param axes
* @return shape with same rank
* @since 2.2
*/
public static int[] getReducedShapeKeepRank(int[] shape, int... axes) {
int[] keep = shape.clone();
axes = checkAxes(shape.length, axes);
for (int i : axes) {
keep[i] = 1;
}
return keep;
}
/**
* @param a
* @param b
* @return true if arrays only differs by unit entries
* @since 2.2
*/
public static boolean differsByOnes(int[] a, int[] b) {
int aRank = a.length;
int bRank = b.length;
int ai = 0;
int bi = 0;
int al = 1;
int bl = 1;
do {
while (ai < aRank && (al = a[ai++]) == 1) { // next non-unit dimension
}
while (bi < bRank && (bl = b[bi++]) == 1) {
}
if (al != bl) {
return false;
}
} while (ai < aRank && bi < bRank);
if (ai == aRank) {
while (bi < bRank) {
if (b[bi++] != 1) {
return false;
}
}
}
if (bi == bRank) {
while (ai < aRank) {
if (a[ai++] != 1) {
return false;
}
}
}
return true;
}
/**
* Calculate the padding difference between two shapes. Padding can be positive (negative)
* for added (removed) dimensions. NB positive or negative padding is given after matched
* dimensions
* @param aShape
* @param bShape
* @return padding can be null if shapes are equal
* @throws IllegalArgumentException if one shape is null but not the other, or if shapes do
* not possess common non-unit lengths
* @since 2.2
*/
public static int[] calcShapePadding(int[] aShape, int[] bShape) {
if (Arrays.equals(aShape, bShape)) {
return null;
}
if (aShape == null || bShape == null) {
throw new IllegalArgumentException("If one shape is null then the other must be null too");
}
if (!differsByOnes(aShape, bShape)) {
throw new IllegalArgumentException("Non-unit lengths in shapes must be equal");
}
int aRank = aShape.length;
int bRank = bShape.length;
int[] padding;
if (aRank == 0 || bRank == 0) {
padding = new int[1];
padding[0] = aRank == 0 ? bRank : -aRank;
return padding;
}
padding = new int[Math.max(aRank, bRank) + 2];
int ai = 0;
int bi = 0;
int al = 0;
int bl = 0;
int pi = 0;
int p;
boolean aLeft = ai < aRank;
boolean bLeft = bi < bRank;
while (aLeft && bLeft) {
if (aLeft) {
al = aShape[ai++];
aLeft = ai < aRank;
}
if (bLeft) {
bl = bShape[bi++];
bLeft = bi < bRank;
}
if (al != bl) {
p = 0;
while (al == 1 && aLeft) {
al = aShape[ai++];
aLeft = ai < aRank;
p--;
}
while (bl == 1 && bLeft) {
bl = bShape[bi++];
bLeft = bi < bRank;
p++;
}
padding[pi++] = p;
}
if (al == bl) {
pi++;
}
}
if (aLeft || bLeft) {
p = 0;
while (ai < aRank && aShape[ai++] == 1) {
p--;
}
while (bi < bRank && bShape[bi++] == 1) {
p++;
}
padding[pi++] = p;
}
return Arrays.copyOf(padding, pi);
}
static int[] padShape(int[] padding, int nr, int[] oldShape) {
if (padding == null) {
return oldShape.clone();
}
int or = oldShape.length;
int[] newShape = new int[nr];
int di = 0;
for (int i = 0, si = 0; i < (or+1) && si <= or && di < nr; i++) {
int c = padding[i];
if (c == 0) {
newShape[di++] = oldShape[si++];
} else if (c > 0) {
int dim = di + c;
while (di < dim) {
newShape[di++] = 1;
}
} else if (c < 0) {
si -= c; // remove dimensions by skipping forward in source array (should check that they are unit entries)
}
}
while (di < nr) {
newShape[di++] = 1;
}
return newShape;
}
}