Skip to content

Commit 502342c

Browse files
committed
Shape: better doc strings for GetOffset, GetCoordinates and GetCoordinatesFromAbsoluteIndex
1 parent a009aad commit 502342c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

src/NumSharp.Core/View/Shape.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ public int TransformOffset(int offset)
389389

390390
/// <summary>
391391
/// Get offset index out of coordinate indices.
392+
///
393+
/// The offset is the absolute offset in memory for the given coordinates.
394+
/// Even for shapes that were sliced and reshaped after slicing and sliced again (and so forth)
395+
/// this returns the absolute memory offset.
396+
///
397+
/// Note: the inverse operation to this is GetCoordinatesFromAbsoluteIndex
392398
/// </summary>
393399
/// <param name="indices">The coordinates to turn into linear offset</param>
394400
/// <returns>The index in the memory block that refers to a specific value.</returns>
@@ -878,9 +884,14 @@ private int GetOffset_broadcasted_1D(int index)
878884
}
879885

880886
/// <summary>
881-
/// Transforms offset index in this shape into coordinates that matches this shape.
887+
/// Gets coordinates in this shape from index in this shape (slicing is ignored).
888+
/// Example: Shape (2,3)
889+
/// 0 => [0, 0]
890+
/// 1 => [0, 1]
891+
/// ...
892+
/// 6 => [1, 2]
882893
/// </summary>
883-
/// <param name="offset"></param>
894+
/// <param name="offset">the index if you would iterate from 0 to shape.size in row major order</param>
884895
/// <returns></returns>
885896
[MethodImpl((MethodImplOptions)768)]
886897
public int[] GetCoordinates(int offset)
@@ -930,8 +941,22 @@ public int[] GetCoordinates(int offset)
930941
return coords;
931942
}
932943

933-
/// <summary>
934-
/// Retrievs the coordinates in current shape (potentially sliced and reshaped) from index in original array
944+
/// <summary>
945+
/// Retrievs the coordinates in current shape (potentially sliced and reshaped) from index in original array.
946+
/// Note: this is the inverse operation of GetOffset
947+
/// Example: Shape a (2,3) => sliced to b (2,2) by a[:, 1:]
948+
/// The absolute indices in a are:
949+
/// [0, 1, 2,
950+
/// 3, 4, 5]
951+
/// The absolute indices in b are:
952+
/// [1, 2,
953+
/// 4, 5]
954+
/// Note: due to slicing the absolute indices (offset in memory) are different from what GetCoordinates would return, which are relative indices in the shape.
955+
///
956+
/// Examples:
957+
/// a.GetCoordinatesFromAbsoluteIndex(1) returns [0, 1]
958+
/// b.GetCoordinatesFromAbsoluteIndex(1) returns [0, 0]
959+
/// b.GetCoordinatesFromAbsoluteIndex(0) returns [] because it is out of shape
935960
/// </summary>
936961
/// <param name="offset">Is the index in the original array before it was sliced and/or reshaped</param>
937962
/// <returns></returns>

0 commit comments

Comments
 (0)