@@ -11,7 +11,15 @@ import CoreML
1111import Foundation
1212
1313extension MLMultiArray {
14- /// All values will be stored in the last dimension of the MLMultiArray (default is dims=1)
14+ /// Creates an MLMultiArray from an array of integers.
15+ ///
16+ /// All values are stored in the last dimension of the MLMultiArray, with leading
17+ /// dimensions set to 1. For example, with dims=2, the shape becomes [1, arr.count].
18+ ///
19+ /// - Parameters:
20+ /// - arr: Array of integers to convert
21+ /// - dims: Number of dimensions for the resulting MLMultiArray
22+ /// - Returns: MLMultiArray containing the integer values
1523 static func from( _ arr: [ Int ] , dims: Int = 1 ) -> MLMultiArray {
1624 var shape = Array ( repeating: 1 , count: dims)
1725 shape [ shape. count - 1 ] = arr. count
@@ -27,7 +35,15 @@ extension MLMultiArray {
2735 return o
2836 }
2937
30- /// All values will be stored in the last dimension of the MLMultiArray (default is dims=1)
38+ /// Creates an MLMultiArray from an array of doubles.
39+ ///
40+ /// All values are stored in the last dimension of the MLMultiArray, with leading
41+ /// dimensions set to 1. For example, with dims=2, the shape becomes [1, arr.count].
42+ ///
43+ /// - Parameters:
44+ /// - arr: Array of doubles to convert
45+ /// - dims: Number of dimensions for the resulting MLMultiArray
46+ /// - Returns: MLMultiArray containing the double values
3147 static func from( _ arr: [ Double ] , dims: Int = 1 ) -> MLMultiArray {
3248 var shape = Array ( repeating: 1 , count: dims)
3349 shape [ shape. count - 1 ] = arr. count
@@ -43,7 +59,13 @@ extension MLMultiArray {
4359 return o
4460 }
4561
46- /// This will concatenate all dimensions into one one-dim array.
62+ /// Converts an MLMultiArray to a flat array of integers.
63+ ///
64+ /// Concatenates all dimensions into a single one-dimensional array by reading
65+ /// the MLMultiArray data in memory order.
66+ ///
67+ /// - Parameter o: MLMultiArray to convert
68+ /// - Returns: Flat array of integer values
4769 static func toIntArray( _ o: MLMultiArray ) -> [ Int ] {
4870 var arr = Array ( repeating: 0 , count: o. count)
4971 let ptr = UnsafeMutablePointer < Int32 > ( OpaquePointer ( o. dataPointer) )
@@ -53,9 +75,18 @@ extension MLMultiArray {
5375 return arr
5476 }
5577
78+ /// Converts this MLMultiArray to a flat array of integers.
79+ ///
80+ /// - Returns: Flat array of integer values
5681 func toIntArray( ) -> [ Int ] { Self . toIntArray ( self ) }
5782
58- /// This will concatenate all dimensions into one one-dim array.
83+ /// Converts an MLMultiArray to a flat array of doubles.
84+ ///
85+ /// Concatenates all dimensions into a single one-dimensional array by reading
86+ /// the MLMultiArray data in memory order.
87+ ///
88+ /// - Parameter o: MLMultiArray to convert
89+ /// - Returns: Flat array of double values
5990 static func toDoubleArray( _ o: MLMultiArray ) -> [ Double ] {
6091 var arr : [ Double ] = Array ( repeating: 0 , count: o. count)
6192 let ptr = UnsafeMutablePointer < Double > ( OpaquePointer ( o. dataPointer) )
@@ -65,11 +96,17 @@ extension MLMultiArray {
6596 return arr
6697 }
6798
99+ /// Converts this MLMultiArray to a flat array of doubles.
100+ ///
101+ /// - Returns: Flat array of double values
68102 func toDoubleArray( ) -> [ Double ] { Self . toDoubleArray ( self ) }
69103
70- /// Helper to construct a sequentially-indexed multi array,
71- /// useful for debugging and unit tests
72- /// Example in 3 dimensions:
104+ /// Creates a test MLMultiArray with sequentially indexed values.
105+ ///
106+ /// Useful for debugging and unit tests. Values are assigned sequentially
107+ /// starting from 0, following the memory layout of the specified shape.
108+ ///
109+ /// Example output for shape [2, 3, 4]:
73110 /// ```
74111 /// [[[ 0, 1, 2, 3 ],
75112 /// [ 4, 5, 6, 7 ],
@@ -78,6 +115,9 @@ extension MLMultiArray {
78115 /// [ 16, 17, 18, 19 ],
79116 /// [ 20, 21, 22, 23 ]]]
80117 /// ```
118+ ///
119+ /// - Parameter shape: Desired shape of the test tensor
120+ /// - Returns: MLMultiArray with sequential values for testing
81121 static func testTensor( shape: [ Int ] ) -> MLMultiArray {
82122 let arr = try ! MLMultiArray ( shape: shape as [ NSNumber ] , dataType: . double)
83123 let ptr = UnsafeMutablePointer < Double > ( OpaquePointer ( arr. dataPointer) )
@@ -199,6 +239,12 @@ extension MLMultiArray {
199239}
200240
201241extension MLShapedArray < Float > {
242+ /// Efficiently extracts float values from the shaped array.
243+ ///
244+ /// Uses optimized memory copying when possible (stride=1), falling back to
245+ /// slower scalar access for non-contiguous arrays.
246+ ///
247+ /// - Returns: Array of Float values from the shaped array
202248 var floats : [ Float ] {
203249 guard strides. first == 1 , strides. count == 1 else {
204250 // For some reason this path is slow.
@@ -213,6 +259,12 @@ extension MLShapedArray<Float> {
213259}
214260
215261extension MLShapedArraySlice < Float > {
262+ /// Efficiently extracts float values from the shaped array slice.
263+ ///
264+ /// Uses optimized memory copying when possible (stride=1), falling back to
265+ /// slower scalar access for non-contiguous slices.
266+ ///
267+ /// - Returns: Array of Float values from the shaped array slice
216268 var floats : [ Float ] {
217269 guard strides. first == 1 , strides. count == 1 else {
218270 // For some reason this path is slow.
@@ -227,6 +279,12 @@ extension MLShapedArraySlice<Float> {
227279}
228280
229281extension MLMultiArray {
282+ /// Efficiently extracts float values from the MLMultiArray if it contains float32 data.
283+ ///
284+ /// Uses fast memory copying to extract all float values as a contiguous array.
285+ /// Returns nil if the array doesn't contain float32 data.
286+ ///
287+ /// - Returns: Array of Float values, or nil if not float32 type
230288 var floats : [ Float ] ? {
231289 guard dataType == . float32 else { return nil }
232290
0 commit comments