Contents

slice(at:)

Returns a sub-view with the same rank as this view by slicing the dimensions at the provided ranges.

Declaration

@export(implementation) func slice<let indexRank : Int>(at ranges: [indexRank of any NDArray.RangeExpression]) -> NDArray.View<Element>

Parameters

  • ranges:

    The range expressions describing where to slice along each dimension. indexRank must be ≤ rank. Unspecified trailing dimensions are assumed to be .all.

Discussion

For example if you have a 2D NDArray and want to compute the sum of a specific row, you can slice that row and then access a span over it (or use withUnsafePointer if not contiguous).

/// Returns the sum of the given row.
func sumOfRow(
  of view: borrowing NDArray.View<Float>,
  row: Int
) -> Float {
  let rowSlice = view.slice(at: [row])
  let elements = rowSlice.contiguousElements! // contiguous row expected in this case

  var sum: Float = 0
  for i in elements.indices {
    sum += elements[i]
  }
  return sum
}