Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jul 15, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 18
Kotlin friendly names#1
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1063566
Rename Shape.size to Shape.get, add Shape.toListOrNull, ignore .idea …
rnett 230b1d4
Revert formatting changes
rnett d046486
Formating fix
rnett e64b197
Properly format Shape.java
rnett 647a995
Fix missed size
rnett b32d928
Re-add deprecated size
rnett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| *.iml | ||
| .idea | ||
| target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,7 +17,9 @@ | ||
| package org.tensorflow.ndarray; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| /** | ||
| * The shape of a Tensor or {@link NdArray}. | ||
| @@ -74,8 +76,8 @@ public static Shape scalar() { | ||
| * Shape scalar = Shape.of() | ||
| * }</pre> | ||
| * | ||
| * @param dimensionSizes number of elements in each dimension of this shape, if any, or | ||
| * {@link Shape#UNKNOWN_SIZE} if unknown. | ||
| * @param dimensionSizes number of elements in each dimension of this shape, if any, or {@link | ||
| * Shape#UNKNOWN_SIZE} if unknown. | ||
| * @return a new shape | ||
| */ | ||
| public static Shape of(long... dimensionSizes) { | ||
| @@ -108,13 +110,34 @@ public long size() { | ||
| * an unknown size, {@link Shape#UNKNOWN_SIZE} is returned. | ||
| * | ||
| * @param i the index of the dimension to get the size for. If this Shape has a known number of | ||
| * dimensions, it must be < {@link Shape#numDimensions()}. The index may be negative, in which | ||
| * case the position is counted from the end of the shape. E.g.: {@code size(-1)} returns the | ||
| * size of the last dimension, {@code size(-2)} the size of the second to last dimension etc. | ||
| * dimensions, it must be < {@link Shape#numDimensions()}. The index may be negative, in | ||
| * which case the position is counted from the end of the shape. E.g.: {@code size(-1)} | ||
| * returns the size of the last dimension, {@code size(-2)} the size of the second to last | ||
| * dimension etc. | ||
| * @return The size of the dimension with the given index if known, {@link Shape#UNKNOWN_SIZE} | ||
| * otherwise. | ||
| * @deprecated Renamed to {@link #get(int)}. | ||
| */ | ||
| public long size(int i) { | ||
| @Deprecated | ||
| public long size(int i){ | ||
| return get(i); | ||
| } | ||
| /** | ||
| * The size of the dimension with the given index. | ||
| * | ||
| * <p>If {@link Shape#isUnknown()} is true or the size of the dimension with the given index has | ||
| * an unknown size, {@link Shape#UNKNOWN_SIZE} is returned. | ||
| * | ||
| * @param i the index of the dimension to get the size for. If this Shape has a known number of | ||
| * dimensions, it must be < {@link Shape#numDimensions()}. The index may be negative, in | ||
| * which case the position is counted from the end of the shape. E.g.: {@code size(-1)} | ||
| * returns the size of the last dimension, {@code size(-2)} the size of the second to last | ||
| * dimension etc. | ||
| * @return The size of the dimension with the given index if known, {@link Shape#UNKNOWN_SIZE} | ||
| * otherwise. | ||
| */ | ||
| public long get(int i) { | ||
| if (dimensionSizes == null) { | ||
| return UNKNOWN_SIZE; | ||
| } else if (i >= 0) { | ||
| @@ -177,6 +200,24 @@ public long[] asArray() { | ||
| } | ||
| } | ||
| /** | ||
| * Returns a defensive copy of the this Shape's axes. Changes to the returned list do not change | ||
| * this Shape's state. Returns null if {@link Shape#isUnknown()} is true. | ||
| */ | ||
| public List<Long> toListOrNull() { | ||
| long[] array = asArray(); | ||
| if (array == null) { | ||
| return null; | ||
| } | ||
| List<Long> list = new ArrayList<>(array.length); | ||
| for (long l : array) { | ||
| list.add(l); | ||
| } | ||
| return list; | ||
Craigacp marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| @Override | ||
| public int hashCode() { | ||
| return dimensionSizes != null ? Arrays.hashCode(dimensionSizes) : super.hashCode(); | ||
| @@ -186,6 +227,7 @@ public int hashCode() { | ||
| * Equals implementation for Shapes. Two Shapes are considered equal iff: | ||
| * | ||
| * <p> | ||
| * | ||
| * <ul> | ||
| * <li>the number of dimensions is defined and equal for both | ||
| * <li>the size of each dimension is defined and equal for both | ||
| @@ -236,7 +278,8 @@ public Shape head() { | ||
| * Returns an n-dimensional Shape with the dimensions matching the first n dimensions of this | ||
| * shape | ||
| * | ||
| * @param n the number of leading dimensions to get, must be <= than {@link Shape#numDimensions()} | ||
| * @param n the number of leading dimensions to get, must be <= than {@link | ||
| * Shape#numDimensions()} | ||
| * @return an n-dimensional Shape with the first n dimensions matching the first n dimensions of | ||
| * this Shape | ||
| */ | ||
| @@ -252,7 +295,9 @@ public Shape take(int n) { | ||
| /** Returns a new Shape, with this Shape's first dimension removed. */ | ||
| public Shape tail() { | ||
| if (dimensionSizes.length < 2) return Shape.of(); | ||
| if (dimensionSizes.length < 2) { | ||
| return Shape.of(); | ||
| } | ||
| return Shape.of(Arrays.copyOfRange(dimensionSizes, 1, dimensionSizes.length)); | ||
| } | ||
| @@ -276,15 +321,21 @@ public Shape takeLast(int n) { | ||
| } | ||
| /** | ||
| * Return a {@code end - begin} dimensional shape with dimensions matching this Shape from {@code begin} to {@code end}. | ||
| * Return a {@code end - begin} dimensional shape with dimensions matching this Shape from {@code | ||
| * begin} to {@code end}. | ||
| * | ||
| * @param begin Where to start the sub-shape. | ||
| * @param end Where to end the sub-shape, exclusive. | ||
| * @return the sub-shape bounded by begin and end. | ||
| */ | ||
| public Shape subShape(int begin, int end){ | ||
| public Shape subShape(int begin, int end){ | ||
| if (end > numDimensions()) { | ||
| throw new ArrayIndexOutOfBoundsException( | ||
| "End index " + end + " out of bounds: shape only has " + numDimensions() + " dimensions."); | ||
| "End index " | ||
| + end | ||
| + " out of bounds: shape only has " | ||
| + numDimensions() | ||
| + " dimensions."); | ||
| } | ||
| if (begin < 0) { | ||
| throw new ArrayIndexOutOfBoundsException( | ||
| @@ -423,7 +474,7 @@ public boolean isCompatibleWith(Shape shape) { | ||
| return false; | ||
| } | ||
| for (int i = 0; i < numDimensions(); i++) { | ||
| if (!isCompatible(size(i), shape.size(i))) { | ||
| if (!isCompatible(get(i), shape.get(i))) { | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3798,9 +3798,9 @@ private static int[] computeArrayDims(NdArray<?> ndArray, int expectedRank) { | ||
| } | ||
| int[] arrayShape = new int[expectedRank]; | ||
| for (int i = 0; i < expectedRank; ++i) { | ||
| long dimSize = shape.size(i); | ||
| long dimSize = shape.get(i); | ||
rnett marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (dimSize > Integer.MAX_VALUE) { | ||
| throw new IllegalArgumentException("Dimension " + i + " is too large to fit in a standard array (" + shape.size(i) + ")"); | ||
| throw new IllegalArgumentException("Dimension " + i + " is too large to fit in a standard array (" + shape.get(i) + ")"); | ||
| } | ||
| arrayShape[i] = (int)dimSize; | ||
| } | ||
6 changes: 4 additions & 2 deletions
6 ndarray/src/main/java/org/tensorflow/ndarray/impl/dimension/DimensionalSpace.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18 ndarray/src/test/java/org/tensorflow/ndarray/NdArrayTestBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 18 additions & 12 deletions
30 ndarray/src/test/java/org/tensorflow/ndarray/ShapeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.