diff --git a/.gitignore b/.gitignore index 894318a..3018c18 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ # Swift Package Manager .build/ +.swiftpm DerivedData/ diff --git a/README.md b/README.md index f7d2de2..57dfb5f 100644 --- a/README.md +++ b/README.md @@ -13,29 +13,30 @@ features to enable fast and simple handling of multidimensional numeric data. + ## Table of Contents - [Installation](#installation) - - [Swift Package Manager](#swift-package-manager) + - [Swift Package Manager](#swift-package-manager) - [Multiple Views on Underlying Data](#multiple-views-on-underlying-data) - [Sliced and Strided Access](#sliced-and-strided-access) - - [Single Slice](#single-slice) - - [`UnboundedRange` Slices](#unboundedrange-slices) - - [`Range` and `ClosedRange` Slices](#range-and-closedrange-slices) - - [`PartialRangeFrom`, `PartialRangeUpTo` and `PartialRangeThrough` Slices](#partialrangefrom-partialrangeupto-and-partialrangethrough-slices) + - [Single Slice](#single-slice) + - [`UnboundedRange` Slices](#unboundedrange-slices) + - [`Range` and `ClosedRange` Slices](#range-and-closedrange-slices) + - [`PartialRangeFrom`, `PartialRangeUpTo` and `PartialRangeThrough` Slices](#partialrangefrom-partialrangeupto-and-partialrangethrough-slices) - [Element Manipulation](#element-manipulation) - [Reshaping](#reshaping) - [Elementwise Operations](#elementwise-operations) - - [Scalars](#scalars) - - [Basic Functions](#basic-functions) + - [Scalars](#scalars) + - [Basic Functions](#basic-functions) - [Linear Algebra Operations for `Double` and `Float` `NdArray`s.](#linear-algebra-operations-for-double-and-float-ndarrays) - - [Matrix Vector Multiplication](#matrix-vector-multiplication) - - [Matrix Matrix Multiplication](#matrix-matrix-multiplication) - - [Matrix Inversion](#matrix-inversion) - - [Solve a Linear System of Equations](#solve-a-linear-system-of-equations) + - [Matrix Vector Multiplication](#matrix-vector-multiplication) + - [Matrix Matrix Multiplication](#matrix-matrix-multiplication) + - [Matrix Inversion](#matrix-inversion) + - [Solve a Linear System of Equations](#solve-a-linear-system-of-equations) - [Pretty Printing](#pretty-printing) - [Type Concept](#type-concept) - - [Subtypes](#subtypes) + - [Subtypes](#subtypes) - [Numerical Backend](#numerical-backend) - [Not Implemented](#not-implemented) - [Out of Scope](#out-of-scope) @@ -45,12 +46,16 @@ features to enable fast and simple handling of multidimensional numeric data. ## Installation +The API is stable from versions up to `0.3.0`. Version `0.4.0` deprecates the old slicing API and introduces a more type +safe API. Version `0.5.0` will remove the old slicing API and thus contain breaking changes. It is recommended to fix +all compiler warnings on `0.4.0` before upgrading to `0.5.0`, see also [API Changes](#api-changes). + ### Swift Package Manager ```swift let package = Package( dependencies: [ - .package(url: "https://github.com/dastrobu/NdArray.git", from: "0.3.0"), + .package(url: "https://github.com/dastrobu/NdArray.git", from: "0.4.0"), ] ) ``` @@ -61,7 +66,8 @@ Two arrays can easily point to the same data and data can be modified through bo different from the Swift internal array object, which has copy on write semantics, meaning you cannot pass around pointers to the same data. Whereas this behaviour is very nice for small amounts of data, since it reduces side effects. For numerical computation with huge arrays, it is preferable to let the programmer manage copies. The behaviour of the -NdArray is very similar to NumPy's ndarray object. Here is an example: +NdArray is very similar to +[NumPy's ndarray object](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html). Here is an example: ```swift let a = NdArray([9, 9, 0, 9]) @@ -78,31 +84,70 @@ Like NumPy's ndarray, slices and strides can be created. ```swift let a = NdArray.range(to: 10) -let b = NdArray(a[..., 2]) +let b = NdArray(a[0... ~ 2]) // every second element print(a) // [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] print(b) // [0.0, 2.0, 4.0, 6.0, 8.0] print(b.strides) // [2] -b[...].set(0) +b[0...].set(0) print(a) // [0.0, 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 9.0] print(b) // [0.0, 0.0, 0.0, 0.0, 0.0] ``` This creates an array first, then a strided view on the data, making it easy to set every second element to 0. +### Slices and the Stride Operator `~` + +As shown in the previous example, strides can be defined via the stride operator `~`. The unbounded range slice `0...` +takes all elements along an axis. The stride `~ 2` selects only every second element. Here is a short comparison with +NumPy's syntax. + +``` +NdArray NumPy +a[0...] a[::] +a[0... ~ 2] a[::2] +a[..<42 ~ 2] a[:42:2] +a[3..<42 ~ 2] a[3:42:2] +a[3...42 ~ 2] a[3:41:2] +``` + +Alternatively, slice objects can be created programmatically. The following notations are equivalent: + +``` + a[0...] ≡ a[Slice()] + a[1...] ≡ a[Slice(lowerBound: 1)] + a[..<42] ≡ a[Slice(upperBound: 42)] + a[...42] ≡ a[Slice(upperBound: 43)] + a[1..<42] ≡ a[Slice(lowerBound: 1, upperBound: 42)] + a[1... ~ 2] ≡ a[Slice(lowerBound: 1, upperBound, stride: 2)] + a[..<42 ~ 3] ≡ a[Slice(upperBound: 42, stride: 3)] + a[1..<42 ~ 3] ≡ a[Slice(lowerBound: 1, upperBound: 42, stride: 3)] +``` + +Note, to avoid confusion with pure indexing, integer literals need to be converted to a slice explicitly. This means + +```swift +let a = NdArray.range(to: 10) +let _ = a[1] // does not work +let s1: NdArray = a[Slice(1)] // selects slice at index one along zeroth dimension +let a1: Double = a[[1]] // selects first element +``` + +More detailed examples on each slice type are provided in the sections below. + ### Single Slice -A single slice e.g. a row of a matrix is indexed by simple integer +A single slice e.g. a row of a matrix is indexed by a so called index slice `Slice(_: Int)`: ```swift let a = NdArray.ones([2, 2]) print(a) // [[1.0, 1.0], // [1.0, 1.0]] -a[1].set(0.0) +a[Slice(1)].set(0.0) print(a) // [[1.0, 1.0], // [0.0, 0.0]] -a[...][1].set(2.0) +a[0..., Slice(1)].set(2.0) print(a) // [[1.0, 2.0], // [0.0, 2.0]] @@ -113,7 +158,7 @@ use [element indexing](#element-manipulation) instead or use the `Vector` subtyp ```swift let a = NdArray.range(to: 4) -print(a[0]) // [0.0] +print(a[Slice(0)]) // [0.0] print(a[[0]]) // 0.0 let v = Vector(a) print(v[0] as Double) // 0.0 @@ -122,14 +167,14 @@ print(v[[0]]) // 0.0 ### `UnboundedRange` Slices -Unbound ranges select all elements, this is helpful to access lower dimensions of a multidimensional array +Unbounded ranges select all elements, this is helpful to access lower dimensions of a multidimensional array ```swift let a = NdArray.ones([2, 2]) print(a) // [[1.0, 1.0], // [1.0, 1.0]] -a[...][1].set(0.0) +a[0..., Slice(1)].set(0.0) print(a) // [[1.0, 0.0], // [1.0, 0.0]] @@ -145,7 +190,7 @@ print(a) // [4.0, 5.0], // [6.0, 7.0], // [8.0, 9.0]] -a[..., 2].set(0.0) +a[0... ~ 2].set(0.0) print(a) // [[0.0, 0.0], // [2.0, 3.0], @@ -154,15 +199,19 @@ print(a) // [0.0, 0.0]] ``` +Due to a limitation in the type system, the true unbounded range operator `...` cannot be used. Instead, the +idiom `0...` +should be preferred to specify an unbound range. + ### `Range` and `ClosedRange` Slices -Ranges `n...range(to: 10) print(a[2..<4]) // [2.0, 3.0] print(a[2...4]) // [2.0, 3.0, 4.0] -print(a[2...4, 2]) // [2.0, 4.0] +print(a[2...4 ~ 2]) // [2.0, 4.0] ``` ### `PartialRangeFrom`, `PartialRangeUpTo` and `PartialRangeThrough` Slices @@ -179,13 +228,13 @@ print(a[4..., 2]) // [4.0, 6.0, 8.0] ## Element Manipulation -The syntax for indexing individual elements is by passing an (Swift) array as index. Passing indices individually cannot -be implemented, since Swift does not support varargs on subscript. +Individual elements can be indexed by passing a (Swift) array as index. In the future, there may be varargs support on +subscript to be able to pass indices directly. ```swift let a = NdArray.range(to: 12).reshaped([2, 2, 3]) a[[0, 1, 2]] -a[0, 1, 2] // does not work with Swift +a[0, 1, 2] // not supported yet ``` For efficient iteration of all indices consider using e.g. `apply`, `map` or `reduce`. @@ -214,7 +263,7 @@ Scaling every second element in a matrix by its row index could be done in the f ```swift let a = NdArray.ones([4, 3]) for i in 0...ones([4, 3]) for i in 0..(NdArray.range(to: 4).reshaped([2, 2])) @@ -389,7 +438,7 @@ print(try A.solve(x)) ## Pretty Printing -Multi dimensional arrays can be printed in a human friendly way. +Multidimensional arrays can be printed in a human friendly way. ```swift print(NdArray.ones([2, 3, 4])) @@ -419,19 +468,64 @@ When creating a new array from an existing one, no copy is made unless necessary let A = NdArray.ones(5) var B = NdArray(A) // no copy B = NdArray(copy: A) // copy explicitly required -B = NdArray(A[..., 2]) // no copy, but B will not be contiguous -B = NdArray(A[..., 2], order: .C) // copy, because otherwise new array will not have C ordering +B = NdArray(A[0... ~ 2]) // no copy, but B will not be contiguous +B = NdArray(A[0... ~ 2], order: .C) // copy, because otherwise new array will not have C ordering ``` -When using slices on an `NdArray` it returns a `NdArraySlice` object. This slice object is similar to an array but keeps -track how deeply it is sliced. +### Subtypes + +To be able to define operators for matrix vector multiplication and matrix matrix multiplication, subtypes like +`Matrix` and `Vector` are defined. Since no data is copied when creating a matrix or vector from an array, they can be +converted anytime, thereby making sure the shapes match requirements of the subtype. + +```swift +let a = NdArray.ones([2, 2]) +let b = NdArray.zeros(2) +let A = Matrix(a) // matrix from array without copy +let x = Vector(b) // vector from array without copy +let Ax = A * x; // matrix vector multiplication is defined +let _ = Vector(a) // Precondition failed: Cannot create vector with shape [2, 2]. Vector must have one dimension. +```` + +Furthermore, algorithms specific for subtypes like a matrix will be defined as method on the subtype, e.g. `solve` + +```swift +let A = Matrix(NdArray.range(to: 4).reshaped([2, 2])) +let x = Vector.ones(2) +print(try A.solve(x)) // [-1.0, 1.0] +``` + +## Numerical Backend + +Numerical operations are performed using [BLAS](http://www.netlib.org/blas), see also +[BLAS cheat sheet](http://www.netlib.org/blas/blasqr.pdf) for an overview and [LAPACK](http://www.netlib.org/lapack). +The functions of these libraries are provided by the +[Accelerate Framework](https://developer.apple.com/documentation/accelerate) and are available on most Apple platforms. + +## API Changes + +### TLDR + +To migrate from `<=0.3.0` to `0.4.0` you should upgrade to 0.4.0 and fix all compile warnings. Here are a few rules of +thumb: + +``` +a[...] => a[0...] // UnboundedRange is now expresed by 0... +a[..., 2] => a[0... ~ 2] // strides are now expressed by the stride operator ~ +a[...][3] => a[0..., Slice(3)] // multi dimensional slices are now created within one subscript call [] not many [][][] +``` + +### Removal of `NdArraySlice` + +Prior to version `0.4.0` using slices on an `NdArray` returned a `NdArraySlice` object. This slice object is similar to +an array but keeps track how deeply it is sliced. ```swift let A = NdArray.ones([2, 2, 2]) var B = A[...] // NdArraySlice with sliced = 1, i.e. one dimension has been sliced -B = A[...][..., 2] // NdArraySlice with sliced = 2, i.e. one dimension has been sliced -B = A[...][..., 2][..<1] // NdArraySlice with sliced = 3, i.e. one dimension has been sliced -B = A[...][..., 2][..<1][...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. +B = A[0...][0... ~ 2] // NdArraySlice with sliced = 2, i.e. one dimension has been sliced +B = A[0...][0... ~ 2][..<1] // NdArraySlice with sliced = 3, i.e. one dimension has been sliced +B = A[0...][0... ~ 2][..<1][0...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. ``` So it is recommended to convert to an `NdArray` after slicing before continuing to work with the data. @@ -440,7 +534,7 @@ So it is recommended to convert to an `NdArray` after slicing before continuing let A = NdArray.ones([2, 2, 2]) var B = NdArray(A[...]) // B has shape [2, 2, 2] B = NdArray(A[...][..., 2]) // B has shape [2, 1, 2] -B = NdArray(A[...][..., 2][..<1]) // B has shape [2, 1, 1] +B = NdArray(A[0...][0..., 2][..<1]) // B has shape [2, 1, 1] ``` When using slices to assign data, no type conversion is required. @@ -448,47 +542,31 @@ When using slices to assign data, no type conversion is required. ```swift let A = NdArray.ones([2, 2]) let B = NdArray.zeros(2) -A[...][0] = B[...] +A[0..., 0] = B[0...] print(A) // [[0.0, 1.0], // [0.0, 1.0]] ``` -### Subtypes - -To be able to define operators for matrix vector multiplication and matrix matrix multiplication, sub types like -`Matrix` and `Vector` are defined. Since no data is copied when creating a matrix or vector from an array, they can be -converted anytime, thereby making sure the shapes match requirements of the sub type. +These conversions are not necessary any more starting from version `0.4.0`. With the new slice API, based on the `Slice` +object, slices are obtained by ```swift -let a = NdArray.ones([2, 2]) -let b = NdArray.zeros(2) -let A = Matrix(a) // matrix from array without copy -let x = Vector(b) // vector from array without copy -let Ax = A * x; // matrix vector multiplication is defined -let _ = Vector(a) // Precondition failed: Cannot create vector with shape [2, 2]. Vector must have one dimension. -```` - -Furthermore algorithms specific for subtypes like a matrix will be defined as method on the subtype, e.g. `solve` - -```swift -let A = Matrix(NdArray.range(to: 4).reshaped([2, 2])) -let x = Vector.ones(2) -print(try A.solve(x)) // [-1.0, 1.0] +let A = NdArray.ones([2, 2, 2]) +var B = A[0...] // NdArray with sliced = 1, i.e. one dimension has been sliced +B = A[0..., 0... ~ 2] // NdArray with sliced = 2, i.e. one dimension has been sliced +B = A[0..., 0... ~ 2, ..<1] // NdArray with sliced = 3, i.e. one dimension has been sliced +B = A[0..., 0... ~ 2, ..<1, 0...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. ``` -## Numerical Backend - -Numerical operations are performed using [BLAS](http://www.netlib.org/blas), see also -[BLAS cheat sheet](http://www.netlib.org/blas/blasqr.pdf) for an overview and [LAPACK](http://www.netlib.org/lapack). -The functions of these libraries are provided by the -[Accelerate Framework](https://developer.apple.com/documentation/accelerate) and are available on most Apple platforms. +With this API, there is no subtypes returned when slicing, requiring to remember how many times the array was already +sliced. The old slice API is deprecated and will be removed in `0.5.0`. ## Not Implemented Some features are not implemented yet, but are planned for the near future. -* Elementwise multiplication of Double and Float arrays. Planned as `multiply(elementwiseBy, divide(elementwiseBy)` +* Elementwise multiplication of Double and Float arrays. Planned as `multiply(elementwiseBy), divide(elementwiseBy)` employing `vDSP_vmulD` Note that this can be done with help of `map` currently. @@ -496,7 +574,9 @@ Some features are not implemented yet, but are planned for the near future. Some features would be nice to have at some time but currently out of scope. -* Complex numbers (currently support for complex numbers is not planned) +* Complex number arithmetic (explicit support for complex numbers is not planned). One can create arrays for any type + though (`NdArray`), just arithmetic operations will not be defined. These could of course be added inside + application code. ## Docs diff --git a/Sources/NdArray/Equitable.swift b/Sources/NdArray/Equitable.swift index 9e687a1..cd25a23 100644 --- a/Sources/NdArray/Equitable.swift +++ b/Sources/NdArray/Equitable.swift @@ -50,7 +50,7 @@ extension NdArray: Equatable where T: Equatable { // make sure the array is not sliced let a = NdArray(lhs) let b = NdArray(rhs) - for i in 0..: NdArray { Cannot transpose matrix with shape \(shape) to matrix with shape \(out.shape). Precondition failed while trying to transpose \(debugDescription) to \(out.debugDescription). """) - out[...][...] = self.transposed()[...][...] + out[0..., 0...] = self.transposed()[0..., 0...] } } } @@ -167,11 +167,11 @@ public extension Matrix where T == Double { return B } // copy rhs to work space (thereby also making sure it is F contiguous) - B[...] = rhs[...] + B[0...] = rhs[0...] // copy self to A, since it is modified (thereby also making sure it is F contiguous) let A = Matrix(empty: shape, order: .F) - A[...] = self[...] + A[0...] = self[0...] var nrhs = __CLPK_integer(B.shape[1]) var ipiv: [__CLPK_integer] = [__CLPK_integer].init(repeating: 0, count: Int(n)) var lda: __CLPK_integer = __CLPK_integer(n) @@ -202,7 +202,7 @@ public extension Matrix where T == Double { Precondition failed while trying to solve \(debugDescription). """) let A = out ?? Matrix(empty: shape, order: .F) - A[...] = self[...] + A[0...] = self[0...] var ipiv = try A.luFactor() @@ -326,11 +326,11 @@ public extension Matrix where T == Float { return B } // copy rhs to work space (thereby also making sure it is F contiguous) - B[...] = rhs[...] + B[0...] = rhs[0...] // copy self to A, since it is modified (thereby also making sure it is F contiguous) let A = Matrix(empty: shape, order: .F) - A[...] = self[...] + A[0...] = self[0...] var nrhs = __CLPK_integer(B.shape[1]) var ipiv: [__CLPK_integer] = [__CLPK_integer].init(repeating: 0, count: Int(n)) var lda: __CLPK_integer = __CLPK_integer(n) @@ -361,7 +361,7 @@ public extension Matrix where T == Float { Precondition failed while trying to solve \(debugDescription). """) let A = out ?? Matrix(empty: shape, order: .F) - A[...] = self[...] + A[0...] = self[0...] var ipiv = try A.luFactor() diff --git a/Sources/NdArray/NdArray.swift b/Sources/NdArray/NdArray.swift index 8420e20..2cf85bf 100644 --- a/Sources/NdArray/NdArray.swift +++ b/Sources/NdArray/NdArray.swift @@ -99,7 +99,7 @@ open class NdArray: CustomDebugStringConvertible, self.count = a.count self.shape = a.shape self.strides = a.strides - precondition(self !== owner) + assert(self !== owner) } deinit { @@ -247,17 +247,20 @@ open class NdArray: CustomDebugStringConvertible, "\(self, style: .multiLine)" } - /// element access + /** + element access + */ public subscript(index: [Int]) -> T { get { data[flatIndex(index)] } set { - self.data[flatIndex(index)] = newValue + data[flatIndex(index)] = newValue } } /// full slice access + @available(*, deprecated, message: "prefer new slicing syntax a[0..., 0..., 0...] over old one a[...][...][...]") public subscript(r: UnboundedRange) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -268,6 +271,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range slice access + @available(*, deprecated, message: "prefer new slicing syntax a[0...42, 0...42, 0...42] over old one a[0...42][0...42][0...42]") public subscript(r: ClosedRange) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -278,6 +282,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range slice access + @available(*, deprecated, message: "prefer new slicing syntax a[...42, ...42, ...42] over old one a[...42][...42][...42]") public subscript(r: PartialRangeThrough) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -288,6 +293,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range slice access + @available(*, deprecated, message: "prefer new slicing syntax a[..<42, ..<42, ..<42] over old one a[..<42][..<42][..<42]") public subscript(r: PartialRangeUpTo) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -298,6 +304,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range slice access + @available(*, deprecated, message: "prefer new slicing syntax a[42..., 42.., 42..] over old one a[42...][42...][42...]") public subscript(r: PartialRangeFrom) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -308,6 +315,7 @@ open class NdArray: CustomDebugStringConvertible, } /// range slice access + @available(*, deprecated, message: "prefer new slicing syntax a[1..<42, 1..<42, 1..<42] over old one a[1..<42][1..<42][1..<42]") public subscript(r: Range) -> NdArray { get { NdArraySlice(self, sliced: 0)[r] @@ -318,6 +326,7 @@ open class NdArray: CustomDebugStringConvertible, } /// range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[1..<42 ~ 3, 1..<42 ~ 3, 1..<42 ~ 3] over old one a[1..<42, 3][1..<42, 3][1..<42, 3]") public subscript(r: Range, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -328,6 +337,7 @@ open class NdArray: CustomDebugStringConvertible, } /// closed range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[0...42 ~ 3, 0...42 ~ 3, 0...42 ~ 3] over old one a[0...42, 3][0...42, 3][0...42, 3]") public subscript(r: ClosedRange, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -338,6 +348,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[42... ~ 3, 42.. ~ 3, 42.. ~ 3] over old one a[42..., 3][42..., 3][42..., 3]") public subscript(r: PartialRangeFrom, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -348,6 +359,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[...42 ~ 3, ...42 ~ 3, ...42 ~ 3] over old one a[...42, 3][...42, 3][...42, 3]") public subscript(r: PartialRangeThrough, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -358,6 +370,7 @@ open class NdArray: CustomDebugStringConvertible, } /// partial range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[..<42 ~ 3, ..<42 ~ 3, ..<42 ~ 3] over old one a[..<42, 3][..<42, 3][..<42, 3]") public subscript(r: PartialRangeUpTo, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -368,6 +381,7 @@ open class NdArray: CustomDebugStringConvertible, } /// full range with stride + @available(*, deprecated, message: "prefer new slicing syntax a[0... ~ 3, 0... ~ 3, 0... ~ 3] over old one a[..., 3][..., 3][..., 3]") public subscript(r: UnboundedRange, stride: Int) -> NdArray { get { NdArraySlice(self, sliced: 0)[r, stride] @@ -378,6 +392,7 @@ open class NdArray: CustomDebugStringConvertible, } /// single slice access + @available(*, deprecated, message: "prefer new slicing syntax a[42, 42, 42] over old one a[42][42][42]") public subscript(i: Int) -> NdArray { get { precondition(!isEmpty) @@ -402,6 +417,47 @@ open class NdArray: CustomDebugStringConvertible, newValue.copyTo(self[i]) } } + + /** + slice access + */ + public subscript(slices: [Slice]) -> NdArray { + get { + var a = NdArraySlice(self, sliced: 0) + for (i, s) in slices.enumerated() { + switch s.sliceKind { + case .range(lowerBound: let lowerBound, upperBound: let upperBound, stride: let stride): + let stride = stride ?? 1 + let lowerBound = lowerBound ?? 0 + let upperBound = upperBound ?? shape[i] + a = a.subscr(lowerBound: lowerBound, upperBound: upperBound, stride: stride) + case .index(let i): + a = a.subscr(i) + if a.shape.isEmpty { + a.shape = [1] + a.strides = [1] + a.count = 1 + } + } + } + return NdArray(a) + } + set { + newValue.copyTo(self[slices]) + } + } + + /** + slice access + */ + public subscript(slices: Slice...) -> NdArray { + get { + self[slices] + } + set { + self[slices] = newValue + } + } } // extension helping to handle different memory alignments diff --git a/Sources/NdArray/NdArraySlice.swift b/Sources/NdArray/NdArraySlice.swift index 889b2e2..9cb3d0a 100644 --- a/Sources/NdArray/NdArraySlice.swift +++ b/Sources/NdArray/NdArraySlice.swift @@ -4,7 +4,7 @@ import Darwin -public class NdArraySlice: NdArray { +internal class NdArraySlice: NdArray { /// number of dimensions that have been sliced private var sliced: Int @@ -50,7 +50,7 @@ public class NdArraySlice: NdArray { private func subscr(_ r: UnboundedRange) -> NdArraySlice { let slice = NdArraySlice(self, sliced: sliced + 1) slice.sliceDescription = sliceDescription - slice.sliceDescription.append("[...]") + slice.sliceDescription.append("[0...]") return slice } @@ -141,6 +141,8 @@ public class NdArraySlice: NdArray { let slice = NdArraySlice(self, startIndex: [Int](repeating: 0, count: ndim), sliced: sliced + 1) slice.shape[sliced] = 0 slice.count = slice.len + slice.sliceDescription = sliceDescription + slice.sliceDescription.append("[\(r.lowerBound)..<\(r.upperBound)]") return slice } @@ -200,7 +202,7 @@ public class NdArraySlice: NdArray { /// partial range with stride public override subscript(r: PartialRangeFrom, stride: Int) -> NdArray { get { - precondition(stride > 0, "\(stride) > 0") + precondition(stride > 0, "\(stride) > 0, \(debugDescription)") let slice = self.subscr(r) slice.sliceDescription.removeLast() @@ -287,6 +289,34 @@ public class NdArraySlice: NdArray { } } + internal func subscr(lowerBound: Int, upperBound: Int, stride: Int) -> NdArraySlice { + precondition(stride > 0, "\(stride) > 0") + + let slice = self.subscr(lowerBound.. NdArraySlice { + precondition(!isEmpty) + + // set the index on the sliced dimension + var startIndex = [Int](repeating: 0, count: ndim) + startIndex[sliced] = i + + // here we reduce the shape, hence sliced stays the same + let slice = NdArraySlice(self, startIndex: startIndex, sliced: sliced) + slice.sliceDescription = sliceDescription + slice.sliceDescription.append("[\(i)]") + // drop shape and stride + slice.shape = Array(slice.shape[0.. Slice { + Slice(lowerBound: lowerBound, upperBound: upperBound + 1) +} + +public func ..< (lowerBound: Int, upperBound: Int) -> Slice { + Slice(lowerBound: lowerBound, upperBound: upperBound) +} + +public prefix func ..< (upperBound: Int) -> Slice { + Slice(upperBound: upperBound) +} + +public prefix func ... (upperBound: Int) -> Slice { + Slice(upperBound: upperBound + 1) +} + +public postfix func ... (lowerBound: Int) -> Slice { + Slice(lowerBound: lowerBound) +} + +precedencegroup StridePrecedence { + lowerThan: RangeFormationPrecedence +} + +/** + Operator to define strided slices + */ +infix operator ~: StridePrecedence + +/** + generate the strided slice from unstrided slice and stride + */ +public func ~ (s: Slice, stride: Int) -> Slice { + switch s.sliceKind { + case .range(lowerBound: let lowerBound, upperBound: let upperBound, stride: _): + return Slice(lowerBound: lowerBound, upperBound: upperBound, stride: stride) + case .index: + return s + } +} diff --git a/Sources/NdArray/Vector.swift b/Sources/NdArray/Vector.swift index fde97aa..486f36c 100644 --- a/Sources/NdArray/Vector.swift +++ b/Sources/NdArray/Vector.swift @@ -88,7 +88,7 @@ public extension Vector where T == Double { // make a copy sort it and copy back if array is not contiguous let cpy = Vector(copy: self) vDSP_vsortD(cpy.data, n, sortOrder) - self[...] = cpy[...] + self[0...] = cpy[0...] } } @@ -130,7 +130,7 @@ public extension Vector where T == Float { // make a copy sort it and copy back if array is not contiguous let cpy = Vector(copy: self) vDSP_vsort(cpy.data, n, sortOrder) - self[...] = cpy[...] + self[0...] = cpy[0...] } } diff --git a/Sources/NdArray/apply.swift b/Sources/NdArray/apply.swift index 2d2cb17..20e0aa6 100644 --- a/Sources/NdArray/apply.swift +++ b/Sources/NdArray/apply.swift @@ -27,7 +27,7 @@ public extension NdArray { // make sure the array is not sliced let a = NdArray(self) for i in 0...zeros([3]) - let b = NdArray(NdArray.zeros([6])[..., 2]) - let c = NdArray(NdArray.ones([6])[..., 2]) + let b = NdArray(NdArray.zeros([6])[0... ~ 2]) + let c = NdArray(NdArray.ones([6])[0... ~ 2]) XCTAssertEqual(a, a) XCTAssertEqual(a, b) XCTAssertNotEqual(a, c) @@ -52,8 +52,8 @@ class EquitableTests: XCTestCase { // 2d not contiguous do { let a = NdArray.zeros([2, 3]) - let b = NdArray(NdArray.zeros([2, 6])[...][..., 2]) - let c = NdArray(NdArray.ones([2, 6])[...][..., 2]) + let b = NdArray(NdArray.zeros([2, 6])[0..., 0... ~ 2]) + let c = NdArray(NdArray.ones([2, 6])[0..., 0... ~ 2]) XCTAssertEqual(a, a) XCTAssertEqual(a, b) XCTAssertNotEqual(a, c) @@ -64,8 +64,8 @@ class EquitableTests: XCTestCase { let a = NdArray.zeros([2, 3]) let b = NdArray.zeros([2, 3]) XCTAssertEqual(a, b) - XCTAssertNotEqual(a, b[...]) - XCTAssertEqual(a[...], b[...]) + XCTAssertNotEqual(a, b[Slice(0)]) + XCTAssertEqual(a[0...], b[0...]) } } } diff --git a/Tests/NdArrayTests/MatrixTestsDouble.swift b/Tests/NdArrayTests/MatrixTestsDouble.swift index 4b16dbb..eea2d96 100644 --- a/Tests/NdArrayTests/MatrixTestsDouble.swift +++ b/Tests/NdArrayTests/MatrixTestsDouble.swift @@ -53,7 +53,7 @@ class MatrixTestsDouble: XCTestCase { } // 2d not aligned do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -108,7 +108,7 @@ class MatrixTestsDouble: XCTestCase { } // 2d not aligned -> C contiguous do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -117,7 +117,7 @@ class MatrixTestsDouble: XCTestCase { } // 2d not aligned -> F contiguous do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -126,7 +126,7 @@ class MatrixTestsDouble: XCTestCase { } // 2d not aligned -> not aligned do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -180,7 +180,7 @@ class MatrixTestsDouble: XCTestCase { } // not aligned do { - let A = Matrix(NdArray.range(to: 4 * 2).reshaped([4, 2], order: .C)[..., 2]) + let A = Matrix(NdArray.range(to: 4 * 2).reshaped([4, 2], order: .C)[0... ~ 2]) let b = Vector(NdArray.ones([2])) let x1 = try A.solve(b) @@ -193,8 +193,8 @@ class MatrixTestsDouble: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -205,8 +205,8 @@ class MatrixTestsDouble: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -216,8 +216,8 @@ class MatrixTestsDouble: XCTestCase { // multiple rhs not aligned do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) - let B = Matrix(Matrix(NdArray.ones([2, 3]), order: .F)[...][..., 2]) - B[...][1].set(2.0) + let B = Matrix(Matrix(NdArray.ones([2, 3]), order: .F)[0..., 0... ~ 2]) + B[0..., Slice(1)].set(2.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -228,8 +228,8 @@ class MatrixTestsDouble: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = Matrix(empty: B.shape, order: .F) try A.solve(B, out: X1) diff --git a/Tests/NdArrayTests/MatrixTestsFloat.swift b/Tests/NdArrayTests/MatrixTestsFloat.swift index 972aedd..3fa66c0 100644 --- a/Tests/NdArrayTests/MatrixTestsFloat.swift +++ b/Tests/NdArrayTests/MatrixTestsFloat.swift @@ -53,7 +53,7 @@ class MatrixTestsFloat: XCTestCase { } // 2d not aligned do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -108,7 +108,7 @@ class MatrixTestsFloat: XCTestCase { } // 2d not aligned -> C contiguous do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -117,7 +117,7 @@ class MatrixTestsFloat: XCTestCase { } // 2d not aligned -> F contiguous do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -126,7 +126,7 @@ class MatrixTestsFloat: XCTestCase { } // 2d not aligned -> not aligned do { - let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2]) + let a = Matrix(NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2]) XCTAssertEqual(a.shape, [2, 3]) XCTAssertEqual(a.transposed().shape, [3, 2]) XCTAssertEqual(a.strides, [6, 1]) @@ -180,7 +180,7 @@ class MatrixTestsFloat: XCTestCase { } // not aligned do { - let A = Matrix(NdArray.range(to: 4 * 2).reshaped([4, 2], order: .C)[..., 2]) + let A = Matrix(NdArray.range(to: 4 * 2).reshaped([4, 2], order: .C)[0... ~ 2]) let b = Vector(NdArray.ones([2])) let x1 = try A.solve(b) @@ -193,8 +193,8 @@ class MatrixTestsFloat: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -205,8 +205,8 @@ class MatrixTestsFloat: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -216,8 +216,8 @@ class MatrixTestsFloat: XCTestCase { // multiple rhs not aligned do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) - let B = Matrix(Matrix(NdArray.ones([2, 3]), order: .F)[...][..., 2]) - B[...][1].set(2.0) + let B = Matrix(Matrix(NdArray.ones([2, 3]), order: .F)[0..., 0... ~ 2]) + B[0..., Slice(1)].set(2.0) let X1 = try A.solve(B) let Ai = try A.inverted() @@ -228,8 +228,8 @@ class MatrixTestsFloat: XCTestCase { do { let A = Matrix(NdArray.range(from: 1, to: 5).reshaped([2, 2], order: .C)) let B = Matrix(NdArray.ones([2, 3]), order: .F) - B[...][1].set(2.0) - B[...][2].set(3.0) + B[0..., Slice(1)].set(2.0) + B[0..., Slice(2)].set(3.0) let X1 = Matrix(empty: B.shape, order: .F) try A.solve(B, out: X1) diff --git a/Tests/NdArrayTests/NdArraySliceTests.swift b/Tests/NdArrayTests/NdArraySliceTests.swift index 8a24ca6..b9d298f 100644 --- a/Tests/NdArrayTests/NdArraySliceTests.swift +++ b/Tests/NdArrayTests/NdArraySliceTests.swift @@ -10,82 +10,6 @@ class NdArraySliceTests: XCTestCase { func testDebugDescription() { let a = NdArraySlice(NdArray.zeros([2, 3, 4]), sliced: 0) XCTAssert(a.debugDescription.contains("NdArraySlice(-, ")) - - // 1d - XCTAssert(a[...].debugDescription.contains("[...]")) - XCTAssert(a[...2].debugDescription.contains("[...2]")) - XCTAssert(a[..<2].debugDescription.contains("[..<2]")) - XCTAssert(a[1...2].debugDescription.contains("[1...2]")) - XCTAssert(a[1..<2].debugDescription.contains("[1..<2]")) - - // 2d - XCTAssert(a[...][...].debugDescription.contains("[...][...]")) - XCTAssert(a[...][...2].debugDescription.contains("[...][...2]")) - XCTAssert(a[...][..<2].debugDescription.contains("[...][..<2]")) - XCTAssert(a[...][1...2].debugDescription.contains("[...][1...2]")) - XCTAssert(a[...][1..<2].debugDescription.contains("[...][1..<2]")) - - XCTAssert(a[...2][...].debugDescription.contains("[...2][...]")) - XCTAssert(a[...2][...2].debugDescription.contains("[...2][...2]")) - XCTAssert(a[...2][..<2].debugDescription.contains("[...2][..<2]")) - XCTAssert(a[...2][1...2].debugDescription.contains("[...2][1...2]")) - XCTAssert(a[...2][1..<2].debugDescription.contains("[...2][1..<2]")) - - XCTAssert(a[..<2][...].debugDescription.contains("[..<2][...]")) - XCTAssert(a[..<2][...2].debugDescription.contains("[..<2][...2]")) - XCTAssert(a[..<2][..<2].debugDescription.contains("[..<2][..<2]")) - XCTAssert(a[..<2][1...2].debugDescription.contains("[..<2][1...2]")) - XCTAssert(a[..<2][1..<2].debugDescription.contains("[..<2][1..<2]")) - - XCTAssert(a[1...2][...].debugDescription.contains("[1...2][...]")) - XCTAssert(a[1...2][...2].debugDescription.contains("[1...2][...2]")) - XCTAssert(a[1...2][..<2].debugDescription.contains("[1...2][..<2]")) - XCTAssert(a[1...2][1...2].debugDescription.contains("[1...2][1...2]")) - XCTAssert(a[1...2][1..<2].debugDescription.contains("[1...2][1..<2]")) - - XCTAssert(a[1..<2][...].debugDescription.contains("[1..<2][...]")) - XCTAssert(a[1..<2][...2].debugDescription.contains("[1..<2][...2]")) - XCTAssert(a[1..<2][..<2].debugDescription.contains("[1..<2][..<2]")) - XCTAssert(a[1..<2][1...2].debugDescription.contains("[1..<2][1...2]")) - XCTAssert(a[1..<2][1..<2].debugDescription.contains("[1..<2][1..<2]")) - - // 1d strided - XCTAssert(a[..., 2].debugDescription.contains("[..., 2]")) - XCTAssert(a[...2, 2].debugDescription.contains("[...2, 2]")) - XCTAssert(a[..<2, 2].debugDescription.contains("[..<2, 2]")) - XCTAssert(a[1...2, 2].debugDescription.contains("[1...2, 2]")) - XCTAssert(a[1..<2, 2].debugDescription.contains("[1..<2, 2]")) - - // 2d strided - XCTAssert(a[..., 2][..., 2].debugDescription.contains("[..., 2][..., 2]")) - XCTAssert(a[..., 2][...2, 2].debugDescription.contains("[..., 2][...2, 2]")) - XCTAssert(a[..., 2][..<2, 2].debugDescription.contains("[..., 2][..<2, 2]")) - XCTAssert(a[..., 2][1...2, 2].debugDescription.contains("[..., 2][1...2, 2]")) - XCTAssert(a[..., 2][1..<2, 2].debugDescription.contains("[..., 2][1..<2, 2]")) - - XCTAssert(a[...2, 2][..., 2].debugDescription.contains("[...2, 2][..., 2]")) - XCTAssert(a[...2, 2][...2, 2].debugDescription.contains("[...2, 2][...2, 2]")) - XCTAssert(a[...2, 2][..<2, 2].debugDescription.contains("[...2, 2][..<2, 2]")) - XCTAssert(a[...2, 2][1...2, 2].debugDescription.contains("[...2, 2][1...2, 2]")) - XCTAssert(a[...2, 2][1..<2, 2].debugDescription.contains("[...2, 2][1..<2, 2]")) - - XCTAssert(a[..<2, 2][..., 2].debugDescription.contains("[..<2, 2][..., 2]")) - XCTAssert(a[..<2, 2][...2, 2].debugDescription.contains("[..<2, 2][...2, 2]")) - XCTAssert(a[..<2, 2][..<2, 2].debugDescription.contains("[..<2, 2][..<2, 2]")) - XCTAssert(a[..<2, 2][1...2, 2].debugDescription.contains("[..<2, 2][1...2, 2]")) - XCTAssert(a[..<2, 2][1..<2, 2].debugDescription.contains("[..<2, 2][1..<2, 2]")) - - XCTAssert(a[1...2, 2][..., 2].debugDescription.contains("[1...2, 2][..., 2]")) - XCTAssert(a[1...2, 2][...2, 2].debugDescription.contains("[1...2, 2][...2, 2]")) - XCTAssert(a[1...2, 2][..<2, 2].debugDescription.contains("[1...2, 2][..<2, 2]")) - XCTAssert(a[1...2, 2][1...2, 2].debugDescription.contains("[1...2, 2][1...2, 2]")) - XCTAssert(a[1...2, 2][1..<2, 2].debugDescription.contains("[1...2, 2][1..<2, 2]")) - - XCTAssert(a[1..<2, 2][..., 2].debugDescription.contains("[1..<2, 2][..., 2]")) - XCTAssert(a[1..<2, 2][...2, 2].debugDescription.contains("[1..<2, 2][...2, 2]")) - XCTAssert(a[1..<2, 2][..<2, 2].debugDescription.contains("[1..<2, 2][..<2, 2]")) - XCTAssert(a[1..<2, 2][1...2, 2].debugDescription.contains("[1..<2, 2][1...2, 2]")) - XCTAssert(a[1..<2, 2][1..<2, 2].debugDescription.contains("[1..<2, 2][1..<2, 2]")) } } diff --git a/Tests/NdArrayTests/OldReadmeExamples.swift b/Tests/NdArrayTests/OldReadmeExamples.swift new file mode 100644 index 0000000..1033440 --- /dev/null +++ b/Tests/NdArrayTests/OldReadmeExamples.swift @@ -0,0 +1,301 @@ +// +// Created by Daniel Strobusch on 13.01.20. +// + +import XCTest +import NdArray + +/** + Examples in the README.md before the new slice API was introduced. Keep while API is deprecated. + All examples should at least compile with warnings. + */ +class OldReadmeExamples: XCTestCase { + func testAliasing() { + let a = NdArray([9, 9, 0, 9]) + let b = NdArray(a) + a[[2]] = 9.0 + print(b) // [9.0, 9.0, 9.0, 9.0] + print(a.ownsData) // true + print(b.ownsData) // false + } + func testSlices() { + let a = NdArray.range(to: 10) + let b = NdArray(a[..., 2]) + print(a) // [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] + print(b) // [0.0, 2.0, 4.0, 6.0, 8.0] + print(b.strides) // [2] + b[...].set(0) + print(a) // [0.0, 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 9.0] + print(b) // [0.0, 0.0, 0.0, 0.0, 0.0] + } + func testSingleSlice() { + do { + let a = NdArray.ones([2, 2]) + print(a) + // [[1.0, 1.0], + // [1.0, 1.0]] + a[1].set(0.0) + print(a) + // [[1.0, 1.0], + // [0.0, 0.0]] + a[...][1].set(2.0) + print(a) + // [[1.0, 2.0], + // [0.0, 2.0]] + } + do { + let a = NdArray.range(to: 4) + print(a[0]) // [0.0] + print(a[[0]]) // 0.0 + let v = Vector(a) + print(v[0] as Double) // 0.0 + print(v[[0]]) // 0.0 + } + } + func testUnboundRange() { + do { + let a = NdArray.ones([2, 2]) + print(a) + // [[1.0, 1.0], + // [1.0, 1.0]] + a[...][1].set(0.0) + print(a) + // [[1.0, 0.0], + // [1.0, 0.0]] + } + do { + let a = NdArray.range(to: 10).reshaped([5, 2]) + print(a) + // [[0.0, 1.0], + // [2.0, 3.0], + // [4.0, 5.0], + // [6.0, 7.0], + // [8.0, 9.0]] + a[..., 2].set(0.0) + print(a) + // [[0.0, 0.0], + // [2.0, 3.0], + // [0.0, 0.0], + // [6.0, 7.0], + // [0.0, 0.0]] + } + } + func testRangeAndClosedRange() { + let a = NdArray.range(to: 10) + print(a[2..<4]) // [2.0, 3.0] + print(a[2...4]) // [2.0, 3.0, 4.0] + print(a[2...4, 2]) // [2.0, 4.0] + } + func testPartialRanges() { + let a = NdArray.range(to: 10) + print(a[..<4]) // [0.0, 1.0, 2.0, 3.0] + print(a[...4]) // [0.0, 1.0, 2.0, 3.0, 4.0] + print(a[4...]) // [4.0, 5.0, 6.0, 7.0, 8.0, 9.0] + print(a[4..., 2]) // [4.0, 6.0, 8.0] + } + func testScalars() { + let a = NdArray.ones([2, 2]) + a *= 2 + a /= 2 + a += 2 + a /= 2 + + var b: NdArray + b = a * 2 + b = a / 2 + b = a + 2 + b = a - 2 + } + func testBasicFunctions() { + do { + let a = NdArray.ones([2, 2]) + var b: NdArray + + b = abs(a) + + b = acos(a) + b = asin(a) + b = atan(a) + + b = cos(a) + b = sin(a) + b = tan(a) + + b = cosh(a) + b = sinh(a) + b = tanh(a) + + b = exp(a) + b = exp2(a) + + b = log(a) + b = log10(a) + b = log1p(a) + b = log2(a) + b = logb(a) + } + do { + let a = NdArray.range(from: -2, to: 2) + print(a) // [-2, -1, 0, 1] + print(abs(a)) // [2, 1, 0, 1] + } + } + func testLinearAlgebra() throws { + do { + let A = Matrix.ones([2, 2]) + let x = Vector.ones(2) + print(A * x) // [2.0, 2.0] + } + do { + let A = Matrix.ones([2, 2]) + let x = Matrix.ones([2, 2]) + print(A * x) + // [[2.0, 2.0], + // [2.0, 2.0]] + } + do { + let A = Matrix(NdArray.range(to: 4).reshaped([2, 2])) + print(try A.inverted()) + // [[-1.5, 0.5], + // [ 1.0, 0.0]] + } + do { + let A = Matrix(NdArray.range(to: 4).reshaped([2, 2])) + let x = Vector.ones(2) + print(try A.solve(x)) // [-1.0, 1.0] + } + do { + let A = Matrix(NdArray.range(to: 4).reshaped([2, 2])) + let x = Matrix.ones([2, 2]) + print(try A.solve(x)) + // [[-1.0, -1.0], + // [ 1.0, 1.0]] + } + } + func testReshape() { + let a = NdArray.range(to: 12) + print(a.reshaped([2, 6])) + // [[ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], + // [ 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]] + print(a.reshaped([2, 6], order: .F)) + // [[ 0.0, 2.0, 4.0, 6.0, 8.0, 10.0], + // [ 1.0, 3.0, 5.0, 7.0, 9.0, 11.0]] + print(a.reshaped([3, 4])) + // [[ 0.0, 1.0, 2.0, 3.0], + // [ 4.0, 5.0, 6.0, 7.0], + // [ 8.0, 9.0, 10.0, 11.0]] + print(a.reshaped([4, 3])) + // [[ 0.0, 1.0, 2.0], + // [ 3.0, 4.0, 5.0], + // [ 6.0, 7.0, 8.0], + // [ 9.0, 10.0, 11.0]] + print(a.reshaped([2, 2, 3])) + // [[[ 0.0, 1.0, 2.0], + // [ 3.0, 4.0, 5.0]], + // + // [[ 6.0, 7.0, 8.0], + // [ 9.0, 10.0, 11.0]]] + } + func testPrettyPrinting() { + print(NdArray.ones([2, 3, 4])) + // [[[1.0, 1.0, 1.0, 1.0], + // [1.0, 1.0, 1.0, 1.0], + // [1.0, 1.0, 1.0, 1.0]], + // + // [[1.0, 1.0, 1.0, 1.0], + // [1.0, 1.0, 1.0, 1.0], + // [1.0, 1.0, 1.0, 1.0]]] + print("this is a 2d array in one line \(NdArray.zeros([2, 2]), style: .singleLine)") + // this is a 2d array in one line [[0.0, 0.0], [0.0, 0.0]] + print("this is a 2d array in multi line format line \n\(NdArray.zeros([2, 2]), style: .multiLine)") + // this is a 2d array in multi line format line + // [[0.0, 0.0], + // [0.0, 0.0]] + } + func testTypes() { + do { + let A = NdArray.ones(5) + var B = NdArray(A) // no copy + B = NdArray(copy: A) // copy explicitly required + B = NdArray(A[..., 2]) // no copy, but B will not be contiguous + B = NdArray(A[..., 2], order: .C) // copy, because otherwise new array will not have C ordering + } + do { + let A = NdArray.ones([2, 2, 2]) + var B = A[...] // return NdArraySlice with sliced = 1, i.e. one dimension has been sliced + B = A[...][..., 2] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced + B = A[...][..., 2][...1] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced + // B = A[...][..., 2][...1][...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. + } + do { + let A = NdArray.ones([2, 2, 2]) + var B = NdArray(A[...]) // B has shape [2, 2, 2] + print(B.shape) + B = NdArray(A[...][..., 2]) // B has shape [2, 1, 2] + print(B.shape) + B = NdArray(A[...][..., 2][..<1]) // B has shape [2, 1, 1] + print(B.shape) + } + do { + let A = NdArray.ones([2, 2]) + let B = NdArray.zeros(2) + A[...][0] = B[...] + print(A) + // [[0.0, 1.0], + // [0.0, 1.0]] + } + do { + let a = NdArray.ones([2, 2]) + let b = NdArray.zeros(2) + let A = Matrix(a) // matrix from array without copy + let x = Vector(b) // vector from array without copy + let Ax = A * x; // matrix vector multiplication is defined + // let _ = Vector(a) // fails + } + } + func testElementIndexing() { + do { + let a = NdArray.ones(4).reshaped([2, 2]) + let b = a.map { + $0 * 2 + } // map to new array + print(b) + // [[2.0, 2.0], + // [2.0, 2.0]] + a.apply { + $0 * 3 + } // in place + print(a) + // [[3.0, 3.0], + // [3.0, 3.0]] + print(a.reduce(0) { + $0 + $1 + }) // 12.0 + } + do { + let a = NdArray.ones([4, 3]) + for i in 0...ones([4, 3]) + for i in 0...range(to: 10) - let b = NdArray(a[..., 2]) + let b = NdArray(a[0... ~ 2]) print(a) // [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] print(b) // [0.0, 2.0, 4.0, 6.0, 8.0] print(b.strides) // [2] - b[...].set(0) + b[0...].set(0) print(a) // [0.0, 1.0, 0.0, 3.0, 0.0, 5.0, 0.0, 7.0, 0.0, 9.0] print(b) // [0.0, 0.0, 0.0, 0.0, 0.0] } @@ -30,18 +30,18 @@ class ReadmeExamples: XCTestCase { print(a) // [[1.0, 1.0], // [1.0, 1.0]] - a[1].set(0.0) + a[Slice(1)].set(0.0) print(a) // [[1.0, 1.0], // [0.0, 0.0]] - a[...][1].set(2.0) + a[0..., Slice(1)].set(2.0) print(a) // [[1.0, 2.0], // [0.0, 2.0]] } do { let a = NdArray.range(to: 4) - print(a[0]) // [0.0] + print(a[Slice(0)]) // [0.0] print(a[[0]]) // 0.0 let v = Vector(a) print(v[0] as Double) // 0.0 @@ -54,7 +54,7 @@ class ReadmeExamples: XCTestCase { print(a) // [[1.0, 1.0], // [1.0, 1.0]] - a[...][1].set(0.0) + a[0..., Slice(1)].set(0.0) print(a) // [[1.0, 0.0], // [1.0, 0.0]] @@ -67,7 +67,7 @@ class ReadmeExamples: XCTestCase { // [4.0, 5.0], // [6.0, 7.0], // [8.0, 9.0]] - a[..., 2].set(0.0) + a[0... ~ 2].set(0.0) print(a) // [[0.0, 0.0], // [2.0, 3.0], @@ -80,14 +80,14 @@ class ReadmeExamples: XCTestCase { let a = NdArray.range(to: 10) print(a[2..<4]) // [2.0, 3.0] print(a[2...4]) // [2.0, 3.0, 4.0] - print(a[2...4, 2]) // [2.0, 4.0] + print(a[2...4 ~ 2]) // [2.0, 4.0] } func testPartialRanges() { let a = NdArray.range(to: 10) print(a[..<4]) // [0.0, 1.0, 2.0, 3.0] print(a[...4]) // [0.0, 1.0, 2.0, 3.0, 4.0] print(a[4...]) // [4.0, 5.0, 6.0, 7.0, 8.0, 9.0] - print(a[4..., 2]) // [4.0, 6.0, 8.0] + print(a[4... ~ 2]) // [4.0, 6.0, 8.0] } func testScalars() { let a = NdArray.ones([2, 2]) @@ -101,6 +101,7 @@ class ReadmeExamples: XCTestCase { b = a / 2 b = a + 2 b = a - 2 + _ = b } func testBasicFunctions() { do { @@ -129,6 +130,8 @@ class ReadmeExamples: XCTestCase { b = log1p(a) b = log2(a) b = logb(a) + + _ = b } do { let a = NdArray.range(from: -2, to: 2) @@ -213,29 +216,33 @@ class ReadmeExamples: XCTestCase { let A = NdArray.ones(5) var B = NdArray(A) // no copy B = NdArray(copy: A) // copy explicitly required - B = NdArray(A[..., 2]) // no copy, but B will not be contiguous - B = NdArray(A[..., 2], order: .C) // copy, because otherwise new array will not have C ordering + B = NdArray(A[0... ~ 2]) // no copy, but B will not be contiguous + B = NdArray(A[0... ~ 2], order: .C) // copy, because otherwise new array will not have C ordering + + _ = B } do { let A = NdArray.ones([2, 2, 2]) - var B = A[...] // return NdArraySlice with sliced = 1, i.e. one dimension has been sliced - B = A[...][..., 2] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced - B = A[...][..., 2][...1] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced - // B = A[...][..., 2][...1][...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. + var B = A[0...] // return NdArraySlice with sliced = 1, i.e. one dimension has been sliced + B = A[0..., 0... ~ 2] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced + B = A[0..., 0... ~ 2, ...1] // return NdArraySlice with sliced = 2, i.e. one dimension has been sliced + // B = A[0..., 0... ~ 2, ...1, 0...] // Precondition failed: Cannot slice array with ndim 3 more than 3 times. + + _ = B } do { let A = NdArray.ones([2, 2, 2]) - var B = NdArray(A[...]) // B has shape [2, 2, 2] + var B = NdArray(A[0...]) // B has shape [2, 2, 2] print(B.shape) - B = NdArray(A[...][..., 2]) // B has shape [2, 1, 2] + B = NdArray(A[0..., 0... ~ 2]) // B has shape [2, 1, 2] print(B.shape) - B = NdArray(A[...][..., 2][..<1]) // B has shape [2, 1, 1] + B = NdArray(A[0..., 0... ~ 2, ..<1]) // B has shape [2, 1, 1] print(B.shape) } do { let A = NdArray.ones([2, 2]) let B = NdArray.zeros(2) - A[...][0] = B[...] + A[0..., Slice(0)] = B[0...] print(A) // [[0.0, 1.0], // [0.0, 1.0]] @@ -247,6 +254,8 @@ class ReadmeExamples: XCTestCase { let x = Vector(b) // vector from array without copy let Ax = A * x; // matrix vector multiplication is defined // let _ = Vector(a) // fails + + _ = Ax } } func testElementIndexing() { @@ -271,7 +280,7 @@ class ReadmeExamples: XCTestCase { do { let a = NdArray.ones([4, 3]) for i in 0...ones([4, 3]) for i in 0..([1, 1, 2, 2, 3, 3])[..., 2]) + let a = Vector(Vector([1, 1, 2, 2, 3, 3])[0... ~ 2]) var v: [Int] = [] for ai in a { v.append(ai) @@ -49,7 +49,7 @@ class VectorSequenceTests: XCTestCase { } func testUnderestimatedCountShouldBeShape0WhenNotContiguous() { - let a = Vector(Vector([1, 1, 2, 2, 3, 3])[..., 2]) + let a = Vector(Vector([1, 1, 2, 2, 3, 3])[0... ~ 2]) XCTAssertEqual(a.underestimatedCount, a.shape[0]) XCTAssertEqual(a.underestimatedCount, 3) XCTAssertFalse(a.isContiguous) @@ -67,7 +67,7 @@ class VectorSequenceTests: XCTestCase { } func testWithContiguousStorageIfAvailableShouldNotCallBodyWhenNotContiguous() { - let a = Vector(Vector([1, 1, 2, 2, 3, 3])[..., 2]) + let a = Vector(Vector([1, 1, 2, 2, 3, 3])[0... ~ 2]) XCTAssertFalse(a.isContiguous) let r: Int? = a.withContiguousStorageIfAvailable { _ in // should not be called for non contiguous array diff --git a/Tests/NdArrayTests/VectorTestsDouble.swift b/Tests/NdArrayTests/VectorTestsDouble.swift index 296f87f..cac0925 100644 --- a/Tests/NdArrayTests/VectorTestsDouble.swift +++ b/Tests/NdArrayTests/VectorTestsDouble.swift @@ -43,7 +43,7 @@ class VectorTestsDouble: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) a.sort(order: .descending) XCTAssertEqual(a.dataArray, [4, 1, 2, 3, 0, 5]) a.sort(order: .ascending) @@ -66,7 +66,7 @@ class VectorTestsDouble: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) a.reverse() XCTAssertEqual(a.dataArray, [4, 1, 2, 3, 0, 5]) } @@ -85,7 +85,7 @@ class VectorTestsDouble: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.norm2(), 4.47213595499958, accuracy: 1e-15) } } @@ -103,13 +103,13 @@ class VectorTestsDouble: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.dot(a), 20) } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) - let b = Vector(Vector.range(to: 6)[1..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) + let b = Vector(Vector.range(to: 6)[1... ~ 2]) XCTAssertEqual(a.dot(b), 26) } } @@ -127,13 +127,13 @@ class VectorTestsDouble: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.dot(a), a * a) } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) - let b = Vector(Vector.range(to: 6)[1..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) + let b = Vector(Vector.range(to: 6)[1... ~ 2]) XCTAssertEqual(a.dot(b), b * a) } } diff --git a/Tests/NdArrayTests/VectorTestsFloat.swift b/Tests/NdArrayTests/VectorTestsFloat.swift index 7fe596b..412481c 100644 --- a/Tests/NdArrayTests/VectorTestsFloat.swift +++ b/Tests/NdArrayTests/VectorTestsFloat.swift @@ -43,7 +43,7 @@ class VectorTestsFloat: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) a.sort(order: .descending) XCTAssertEqual(a.dataArray, [4, 1, 2, 3, 0, 5]) a.sort(order: .ascending) @@ -66,7 +66,7 @@ class VectorTestsFloat: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) a.reverse() XCTAssertEqual(a.dataArray, [4, 1, 2, 3, 0, 5]) } @@ -85,7 +85,7 @@ class VectorTestsFloat: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.norm2(), 4.47213595499958, accuracy: 1e-15) } } @@ -103,13 +103,13 @@ class VectorTestsFloat: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.dot(a), 20) } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) - let b = Vector(Vector.range(to: 6)[1..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) + let b = Vector(Vector.range(to: 6)[1... ~ 2]) XCTAssertEqual(a.dot(b), 26) } } @@ -127,13 +127,13 @@ class VectorTestsFloat: XCTestCase { } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) XCTAssertEqual(a.dot(a), a * a) } // 1d not aligned do { - let a = Vector(Vector.range(to: 6)[..., 2]) - let b = Vector(Vector.range(to: 6)[1..., 2]) + let a = Vector(Vector.range(to: 6)[0... ~ 2]) + let b = Vector(Vector.range(to: 6)[1... ~ 2]) XCTAssertEqual(a.dot(b), b * a) } } diff --git a/Tests/NdArrayTests/applyTests.swift b/Tests/NdArrayTests/applyTests.swift index 9f0e24d..7b7c8bf 100644 --- a/Tests/NdArrayTests/applyTests.swift +++ b/Tests/NdArrayTests/applyTests.swift @@ -37,8 +37,8 @@ class applyTests: XCTestCase { do { let a = NdArray.range(from: -3, to: 3) let b = NdArray.range(from: -3, to: 3) - a[..., 2].apply { $0 * 2} - b[..., 2] *= 2 + a[0... ~ 2].apply { $0 * 2} + b[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, b.dataArray) } // 2d C contiguous @@ -61,16 +61,16 @@ class applyTests: XCTestCase { do { let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C) let b = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C) - a[1..., 2].apply { $0 * 2} - b[1..., 2] *= 2 + a[1... ~ 2].apply { $0 * 2} + b[1... ~ 2] *= 2 XCTAssertEqual(a.dataArray, b.dataArray) } // 2d not aligned do { let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C) let b = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C) - a[...][1..., 2].apply { $0 * 2} - b[...][1..., 2] *= 2 + a[0..., 1... ~ 2].apply { $0 * 2} + b[0..., 1... ~ 2] *= 2 XCTAssertEqual(a.dataArray, b.dataArray) } } diff --git a/Tests/NdArrayTests/arithmeticTestsDouble.swift b/Tests/NdArrayTests/arithmeticTestsDouble.swift index ff35374..a18ddcf 100644 --- a/Tests/NdArrayTests/arithmeticTestsDouble.swift +++ b/Tests/NdArrayTests/arithmeticTestsDouble.swift @@ -29,7 +29,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a * 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) @@ -57,9 +57,9 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a * 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -88,7 +88,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a / 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 / 2 })) @@ -116,9 +116,9 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a / 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 / 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -147,7 +147,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a + 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) @@ -175,9 +175,9 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a + 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -206,7 +206,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a - 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) @@ -234,9 +234,9 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a - 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -265,7 +265,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 1, 4, 3, 8, 5]) } // 2d C contiguous @@ -283,7 +283,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 2, 4, 3, 4, 5, 12, 14, 16, 9, 10, 11]) } } @@ -310,7 +310,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] /= 2 + a[0... ~ 2] /= 2 XCTAssertEqual(a.dataArray, [0, 1, 1, 3, 2, 5]) } // 2d C contiguous @@ -328,7 +328,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] /= 2 + a[0... ~ 2] /= 2 XCTAssertEqual(a.dataArray, [0.0, 0.5, 1.0, 3.0, 4.0, 5.0, 3.0, 3.5, 4.0, 9.0, 10.0, 11.0]) } } @@ -355,7 +355,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 1, 4, 3, 6, 5]) } // 2d C contiguous @@ -373,7 +373,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 3, 4, 3, 4, 5, 8, 9, 10, 9, 10, 11]) } } @@ -400,7 +400,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, 1, 0, 3, 2, 5]) } // 2d C contiguous @@ -418,7 +418,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, -1, 0, 3, 4, 5, 4, 5, 6, 9, 10, 11]) } } @@ -441,7 +441,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.max()!, 4) } // 2d C contiguous @@ -456,7 +456,7 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] XCTAssertEqual(a.max()!, 8) } } @@ -479,7 +479,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.min()!, 0) } // 2d C contiguous @@ -494,7 +494,7 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.min()!, 3) } } @@ -517,7 +517,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.sum(), 6) } // 2d C contiguous @@ -532,7 +532,7 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.sum(), 42) } } @@ -555,7 +555,7 @@ class arithmeticTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(a.product(), 15) } // 2d C contiguous @@ -570,7 +570,7 @@ class arithmeticTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.product(), 158400) } } @@ -597,7 +597,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [1, 1, 5, 3, 9, 5]) } // 2d C contiguous @@ -615,7 +615,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [3, 5, 7, 3, 4, 5, 15, 17, 19, 9, 10, 11]) } @@ -643,7 +643,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-1, 1, -1, 3, -1, 5]) } // 2d C contiguous @@ -661,7 +661,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-3, -3, -3, 3, 4, 5, -3, -3, -3, 9, 10, 11]) } } @@ -688,7 +688,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [1, 5, 9]) } // 2d C contiguous @@ -706,7 +706,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [3, 5, 7, 15, 17, 19]) } } @@ -733,7 +733,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-1, -1, -1]) } // 2d C contiguous @@ -751,7 +751,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-3, -3, -3, -3, -3, -3]) } } @@ -779,8 +779,8 @@ class arithmeticTestsDouble: XCTestCase { do { let a = NdArray.range(to: 6) let b = NdArray.range(to: 6) - a[..., 2].add(3, a[1..., 2]) - b[..., 2] = b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2]) + b[0... ~ 2] = b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } // 2d C contiguous @@ -799,8 +799,8 @@ class arithmeticTestsDouble: XCTestCase { do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) let b = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].add(3, a[1..., 2]) - b[..., 2] = b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2]) + b[0... ~ 2] = b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } } @@ -828,8 +828,8 @@ class arithmeticTestsDouble: XCTestCase { do { let a = NdArray.range(to: 6) let b = NdArray.range(to: 6) - a[..., 2].add(3, a[1..., 2], 5) - b[..., 2] = 5 * b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2], 5) + b[0... ~ 2] = 5 * b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } // 2d C contiguous @@ -848,8 +848,8 @@ class arithmeticTestsDouble: XCTestCase { do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) let b = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].add(3, a[1..., 2], 5) - b[..., 2] = 5 * b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2], 5) + b[0... ~ 2] = 5 * b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } } @@ -876,8 +876,8 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } // 2d C contiguous do { @@ -894,8 +894,8 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } } @@ -931,7 +931,7 @@ class arithmeticTestsDouble: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2].set(1) + a[0... ~ 2].set(1) XCTAssertEqual(a.dataArray, [1, 1, 1, 3, 1, 5]) } // 2d C contiguous @@ -949,7 +949,7 @@ class arithmeticTestsDouble: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].set(1) + a[0... ~ 2].set(1) XCTAssertEqual(a.dataArray, [1, 1, 1, 3, 4, 5, 1, 1, 1, 9, 10, 11]) } } diff --git a/Tests/NdArrayTests/arithmeticTestsFloat.swift b/Tests/NdArrayTests/arithmeticTestsFloat.swift index 3f5a7f3..8d4190c 100644 --- a/Tests/NdArrayTests/arithmeticTestsFloat.swift +++ b/Tests/NdArrayTests/arithmeticTestsFloat.swift @@ -29,7 +29,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a * 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) @@ -57,9 +57,9 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a * 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -88,7 +88,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a / 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 / 2 })) @@ -116,9 +116,9 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a / 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 / 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -147,7 +147,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a + 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) @@ -175,9 +175,9 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a + 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -206,7 +206,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a - 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) @@ -234,9 +234,9 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a - 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -265,7 +265,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 1, 4, 3, 8, 5]) } // 2d C contiguous @@ -283,7 +283,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 2, 4, 3, 4, 5, 12, 14, 16, 9, 10, 11]) } } @@ -310,7 +310,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] /= 2 + a[0... ~ 2] /= 2 XCTAssertEqual(a.dataArray, [0, 1, 1, 3, 2, 5]) } // 2d C contiguous @@ -328,7 +328,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] /= 2 + a[0... ~ 2] /= 2 XCTAssertEqual(a.dataArray, [0.0, 0.5, 1.0, 3.0, 4.0, 5.0, 3.0, 3.5, 4.0, 9.0, 10.0, 11.0]) } } @@ -355,7 +355,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 1, 4, 3, 6, 5]) } // 2d C contiguous @@ -373,7 +373,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 3, 4, 3, 4, 5, 8, 9, 10, 9, 10, 11]) } } @@ -400,7 +400,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, 1, 0, 3, 2, 5]) } // 2d C contiguous @@ -418,7 +418,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, -1, 0, 3, 4, 5, 4, 5, 6, 9, 10, 11]) } } @@ -441,7 +441,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.max()!, 4) } // 2d C contiguous @@ -456,7 +456,7 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] XCTAssertEqual(a.max()!, 8) } } @@ -479,7 +479,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.min()!, 0) } // 2d C contiguous @@ -494,7 +494,7 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.min()!, 3) } } @@ -517,7 +517,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.sum(), 6) } // 2d C contiguous @@ -532,7 +532,7 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.sum(), 42) } } @@ -555,7 +555,7 @@ class arithmeticTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(a.product(), 15) } // 2d C contiguous @@ -570,7 +570,7 @@ class arithmeticTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.product(), 158400) } } @@ -597,7 +597,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [1, 1, 5, 3, 9, 5]) } // 2d C contiguous @@ -615,7 +615,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [3, 5, 7, 3, 4, 5, 15, 17, 19, 9, 10, 11]) } @@ -643,7 +643,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-1, 1, -1, 3, -1, 5]) } // 2d C contiguous @@ -661,7 +661,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-3, -3, -3, 3, 4, 5, -3, -3, -3, 9, 10, 11]) } } @@ -688,7 +688,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [1, 5, 9]) } // 2d C contiguous @@ -706,7 +706,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [3, 5, 7, 15, 17, 19]) } } @@ -733,7 +733,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-1, -1, -1]) } // 2d C contiguous @@ -751,7 +751,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-3, -3, -3, -3, -3, -3]) } } @@ -779,8 +779,8 @@ class arithmeticTestsFloat: XCTestCase { do { let a = NdArray.range(to: 6) let b = NdArray.range(to: 6) - a[..., 2].add(3, a[1..., 2]) - b[..., 2] = b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2]) + b[0... ~ 2] = b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } // 2d C contiguous @@ -799,8 +799,8 @@ class arithmeticTestsFloat: XCTestCase { do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) let b = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].add(3, a[1..., 2]) - b[..., 2] = b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2]) + b[0... ~ 2] = b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } } @@ -828,8 +828,8 @@ class arithmeticTestsFloat: XCTestCase { do { let a = NdArray.range(to: 6) let b = NdArray.range(to: 6) - a[..., 2].add(3, a[1..., 2], 5) - b[..., 2] = 5 * b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2], 5) + b[0... ~ 2] = 5 * b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } // 2d C contiguous @@ -848,8 +848,8 @@ class arithmeticTestsFloat: XCTestCase { do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) let b = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].add(3, a[1..., 2], 5) - b[..., 2] = 5 * b[..., 2] + 3 * a[1..., 2] + a[0... ~ 2].add(3, a[1... ~ 2], 5) + b[0... ~ 2] = 5 * b[0... ~ 2] + 3 * a[1... ~ 2] XCTAssertEqual(a.dataArray, b.dataArray) } } @@ -876,8 +876,8 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } // 2d C contiguous do { @@ -894,8 +894,8 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } } @@ -931,7 +931,7 @@ class arithmeticTestsFloat: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2].set(1) + a[0... ~ 2].set(1) XCTAssertEqual(a.dataArray, [1, 1, 1, 3, 1, 5]) } // 2d C contiguous @@ -949,7 +949,7 @@ class arithmeticTestsFloat: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2].set(1) + a[0... ~ 2].set(1) XCTAssertEqual(a.dataArray, [1, 1, 1, 3, 4, 5, 1, 1, 1, 9, 10, 11]) } } diff --git a/Tests/NdArrayTests/arithmeticTestsInt.swift b/Tests/NdArrayTests/arithmeticTestsInt.swift index 20455df..9630121 100644 --- a/Tests/NdArrayTests/arithmeticTestsInt.swift +++ b/Tests/NdArrayTests/arithmeticTestsInt.swift @@ -25,7 +25,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a * 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) @@ -53,9 +53,9 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a * 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 * 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -84,7 +84,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a + 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) @@ -112,9 +112,9 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a + 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 + 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -143,7 +143,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] let b = a - 2 XCTAssertEqual(NdArray(copy: a).dataArray, NdArray.range(from: 0, to: 6, by: 2).dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) @@ -171,9 +171,9 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] let b = a - 2 - XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2].dataArray) + XCTAssertEqual(a.dataArray, NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2].dataArray) XCTAssertEqual(b.dataArray, NdArray(copy: a).dataArray.map({ $0 - 2 })) XCTAssertEqual(a.shape, b.shape) XCTAssert(b.isCContiguous) @@ -202,7 +202,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 1, 4, 3, 8, 5]) } // 2d C contiguous @@ -220,7 +220,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] *= 2 + a[0... ~ 2] *= 2 XCTAssertEqual(a.dataArray, [0, 2, 4, 3, 4, 5, 12, 14, 16, 9, 10, 11]) } } @@ -247,7 +247,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 1, 4, 3, 6, 5]) } // 2d C contiguous @@ -265,7 +265,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += 2 + a[0... ~ 2] += 2 XCTAssertEqual(a.dataArray, [2, 3, 4, 3, 4, 5, 8, 9, 10, 9, 10, 11]) } } @@ -292,7 +292,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, 1, 0, 3, 2, 5]) } // 2d C contiguous @@ -310,7 +310,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= 2 + a[0... ~ 2] -= 2 XCTAssertEqual(a.dataArray, [-2, -1, 0, 3, 4, 5, 4, 5, 6, 9, 10, 11]) } } @@ -333,7 +333,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.max()!, 4) } // 2d C contiguous @@ -348,7 +348,7 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[0... ~ 2] XCTAssertEqual(a.max()!, 8) } } @@ -371,7 +371,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.min()!, 0) } // 2d C contiguous @@ -386,7 +386,7 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.min()!, 3) } } @@ -409,7 +409,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(to: 6)[..., 2] + let a = NdArray.range(to: 6)[0... ~ 2] XCTAssertEqual(a.sum(), 6) } // 2d C contiguous @@ -424,7 +424,7 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.sum(), 42) } } @@ -447,7 +447,7 @@ class arithmeticTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(a.product(), 15) } // 2d C contiguous @@ -462,7 +462,7 @@ class arithmeticTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 1, to: 4 * 3 + 1).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(a.product(), 158400) } } @@ -489,7 +489,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [1, 1, 5, 3, 9, 5]) } // 2d C contiguous @@ -507,7 +507,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] += a[1..., 2] + a[0... ~ 2] += a[1... ~ 2] XCTAssertEqual(a.dataArray, [3, 5, 7, 3, 4, 5, 15, 17, 19, 9, 10, 11]) } @@ -535,7 +535,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-1, 1, -1, 3, -1, 5]) } // 2d C contiguous @@ -553,7 +553,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - a[..., 2] -= a[1..., 2] + a[0... ~ 2] -= a[1... ~ 2] XCTAssertEqual(a.dataArray, [-3, -3, -3, 3, 4, 5, -3, -3, -3, 9, 10, 11]) } } @@ -580,7 +580,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [1, 5, 9]) } // 2d C contiguous @@ -598,7 +598,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] + a[1..., 2] + let b = a[0... ~ 2] + a[1... ~ 2] XCTAssertEqual(b.dataArray, [3, 5, 7, 15, 17, 19]) } } @@ -625,7 +625,7 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-1, -1, -1]) } // 2d C contiguous @@ -643,7 +643,7 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = a[..., 2] - a[1..., 2] + let b = a[0... ~ 2] - a[1... ~ 2] XCTAssertEqual(b.dataArray, [-3, -3, -3, -3, -3, -3]) } } @@ -670,8 +670,8 @@ class arithmeticTestsInt: XCTestCase { // 1d not aligned do { let a = NdArray.range(to: 6) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } // 2d C contiguous do { @@ -688,8 +688,8 @@ class arithmeticTestsInt: XCTestCase { // 2d not aligned do { let a = NdArray.range(to: 4 * 3).reshaped([4, 3], order: .C) - let b = -a[..., 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[..., 2]).map({ -$0 }).dataArray) + let b = -a[0... ~ 2] + XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 2]).map({ -$0 }).dataArray) } } diff --git a/Tests/NdArrayTests/basic_functionsTestsDouble.swift b/Tests/NdArrayTests/basic_functionsTestsDouble.swift index 2d52f17..a2ee2ea 100644 --- a/Tests/NdArrayTests/basic_functionsTestsDouble.swift +++ b/Tests/NdArrayTests/basic_functionsTestsDouble.swift @@ -25,7 +25,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: -3, to: 3)[..., 2] + let a = NdArray.range(from: -3, to: 3)[0... ~ 2] XCTAssertEqual(abs(a).dataArray, [3, 1, 1]) } // 2d C contiguous @@ -40,7 +40,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(abs(a).dataArray, [2, 1, 0, 4, 5, 6]) } } @@ -62,7 +62,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(acos(a).dataArray, NdArray(a, order: .C).dataArray.map(acos)) } // 2d C contiguous @@ -77,7 +77,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(acos(a).dataArray, NdArray(a, order: .C).dataArray.map(acos)) } } @@ -99,7 +99,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(asin(a).dataArray, NdArray(a, order: .C).dataArray.map(asin)) } // 2d C contiguous @@ -114,7 +114,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(asin(a).dataArray, NdArray(a, order: .C).dataArray.map(asin)) } } @@ -136,7 +136,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(atan(a).dataArray, NdArray(a, order: .C).dataArray.map(atan)) } // 2d C contiguous @@ -151,7 +151,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(atan(a).dataArray, NdArray(a, order: .C).dataArray.map(atan)) } } @@ -173,7 +173,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(cos(a).dataArray, NdArray(a, order: .C).dataArray.map(cos)) } // 2d C contiguous @@ -188,7 +188,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(cos(a).dataArray, NdArray(a, order: .C).dataArray.map(cos)) } } @@ -210,7 +210,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(sin(a).dataArray, NdArray(a, order: .C).dataArray.map(sin)) } // 2d C contiguous @@ -225,7 +225,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(sin(a).dataArray, NdArray(a, order: .C).dataArray.map(sin)) } } @@ -247,7 +247,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(tan(a).dataArray, NdArray(a, order: .C).dataArray.map(tan)) } // 2d C contiguous @@ -262,7 +262,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(tan(a).dataArray, NdArray(a, order: .C).dataArray.map(tan)) } } @@ -284,7 +284,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(cosh(a).dataArray, NdArray(a, order: .C).dataArray.map(cosh)) } // 2d C contiguous @@ -299,7 +299,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(cosh(a).dataArray, NdArray(a, order: .C).dataArray.map(cosh)) } } @@ -321,7 +321,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(sinh(a).dataArray, NdArray(a, order: .C).dataArray.map(sinh)) } // 2d C contiguous @@ -336,7 +336,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(sinh(a).dataArray, NdArray(a, order: .C).dataArray.map(sinh)) } } @@ -358,7 +358,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(tanh(a).dataArray, NdArray(a, order: .C).dataArray.map(tanh)) } // 2d C contiguous @@ -373,7 +373,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(tanh(a).dataArray, NdArray(a, order: .C).dataArray.map(tanh)) } } @@ -395,7 +395,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log(a).dataArray, NdArray(a, order: .C).dataArray.map(log)) } // 2d C contiguous @@ -410,7 +410,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log(a).dataArray, NdArray(a, order: .C).dataArray.map(log)) } } @@ -432,7 +432,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log10(a).dataArray, NdArray(a, order: .C).dataArray.map(log10)) } // 2d C contiguous @@ -447,7 +447,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log10(a).dataArray, NdArray(a, order: .C).dataArray.map(log10)) } } @@ -469,7 +469,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log1p(a).dataArray, NdArray(a, order: .C).dataArray.map(log1p)) } // 2d C contiguous @@ -484,7 +484,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log1p(a).dataArray, NdArray(a, order: .C).dataArray.map(log1p)) } } @@ -506,7 +506,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log2(a).dataArray, NdArray(a, order: .C).dataArray.map(log2)) } // 2d C contiguous @@ -521,7 +521,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log2(a).dataArray, NdArray(a, order: .C).dataArray.map(log2)) } } @@ -543,7 +543,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(logb(a).dataArray, NdArray(a, order: .C).dataArray.map(logb)) } // 2d C contiguous @@ -558,7 +558,7 @@ class basic_functionsTestsDouble: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(logb(a).dataArray, NdArray(a, order: .C).dataArray.map(logb)) } } diff --git a/Tests/NdArrayTests/basic_functionsTestsFloat.swift b/Tests/NdArrayTests/basic_functionsTestsFloat.swift index 34e0299..ba0f39b 100644 --- a/Tests/NdArrayTests/basic_functionsTestsFloat.swift +++ b/Tests/NdArrayTests/basic_functionsTestsFloat.swift @@ -25,7 +25,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: -3, to: 3)[..., 2] + let a = NdArray.range(from: -3, to: 3)[0... ~ 2] XCTAssertEqual(abs(a).dataArray, [3, 1, 1]) } // 2d C contiguous @@ -40,7 +40,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(abs(a).dataArray, [2, 1, 0, 4, 5, 6]) } } @@ -62,7 +62,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(acos(a).dataArray, NdArray(a, order: .C).dataArray.map(acos)) } // 2d C contiguous @@ -77,7 +77,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(acos(a).dataArray, NdArray(a, order: .C).dataArray.map(acos)) } } @@ -99,7 +99,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(asin(a).dataArray, NdArray(a, order: .C).dataArray.map(asin)) } // 2d C contiguous @@ -114,7 +114,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(asin(a).dataArray, NdArray(a, order: .C).dataArray.map(asin)) } } @@ -136,7 +136,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(atan(a).dataArray, NdArray(a, order: .C).dataArray.map(atan)) } // 2d C contiguous @@ -151,7 +151,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(atan(a).dataArray, NdArray(a, order: .C).dataArray.map(atan)) } } @@ -173,7 +173,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(cos(a).dataArray, NdArray(a, order: .C).dataArray.map(cos)) } // 2d C contiguous @@ -188,7 +188,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(cos(a).dataArray, NdArray(a, order: .C).dataArray.map(cos)) } } @@ -210,7 +210,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(sin(a).dataArray, NdArray(a, order: .C).dataArray.map(sin)) } // 2d C contiguous @@ -225,7 +225,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(sin(a).dataArray, NdArray(a, order: .C).dataArray.map(sin)) } } @@ -247,7 +247,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(tan(a).dataArray, NdArray(a, order: .C).dataArray.map(tan)) } // 2d C contiguous @@ -262,7 +262,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(tan(a).dataArray, NdArray(a, order: .C).dataArray.map(tan)) } } @@ -284,7 +284,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(cosh(a).dataArray, NdArray(a, order: .C).dataArray.map(cosh)) } // 2d C contiguous @@ -299,7 +299,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(cosh(a).dataArray, NdArray(a, order: .C).dataArray.map(cosh)) } } @@ -321,7 +321,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(sinh(a).dataArray, NdArray(a, order: .C).dataArray.map(sinh)) } // 2d C contiguous @@ -336,7 +336,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(sinh(a).dataArray, NdArray(a, order: .C).dataArray.map(sinh)) } } @@ -358,7 +358,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = (NdArray.range(from: 1, to: 7) / 10)[..., 2] + let a = (NdArray.range(from: 1, to: 7) / 10)[0... ~ 2] XCTAssertEqual(tanh(a).dataArray, NdArray(a, order: .C).dataArray.map(tanh)) } // 2d C contiguous @@ -373,7 +373,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1..., 2] + let a = (NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C) / 100)[1... ~ 2] XCTAssertEqual(tanh(a).dataArray, NdArray(a, order: .C).dataArray.map(tanh)) } } @@ -395,7 +395,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log(a).dataArray, NdArray(a, order: .C).dataArray.map(log)) } // 2d C contiguous @@ -410,7 +410,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log(a).dataArray, NdArray(a, order: .C).dataArray.map(log)) } } @@ -432,7 +432,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log10(a).dataArray, NdArray(a, order: .C).dataArray.map(log10)) } // 2d C contiguous @@ -447,7 +447,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log10(a).dataArray, NdArray(a, order: .C).dataArray.map(log10)) } } @@ -469,7 +469,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log1p(a).dataArray, NdArray(a, order: .C).dataArray.map(log1p)) } // 2d C contiguous @@ -484,7 +484,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log1p(a).dataArray, NdArray(a, order: .C).dataArray.map(log1p)) } } @@ -506,7 +506,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(log2(a).dataArray, NdArray(a, order: .C).dataArray.map(log2)) } // 2d C contiguous @@ -521,7 +521,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(log2(a).dataArray, NdArray(a, order: .C).dataArray.map(log2)) } } @@ -543,7 +543,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: 1, to: 7)[..., 2] + let a = NdArray.range(from: 1, to: 7)[0... ~ 2] XCTAssertEqual(logb(a).dataArray, NdArray(a, order: .C).dataArray.map(logb)) } // 2d C contiguous @@ -558,7 +558,7 @@ class basic_functionsTestsFloat: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: 0, to: 4 * 3).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(logb(a).dataArray, NdArray(a, order: .C).dataArray.map(logb)) } } diff --git a/Tests/NdArrayTests/basic_functionsTestsInt.swift b/Tests/NdArrayTests/basic_functionsTestsInt.swift index 16e18e4..5c558d4 100644 --- a/Tests/NdArrayTests/basic_functionsTestsInt.swift +++ b/Tests/NdArrayTests/basic_functionsTestsInt.swift @@ -26,7 +26,7 @@ class basic_functionsTestsInt: XCTestCase { } // 1d not aligned do { - let a = NdArray.range(from: -3, to: 3)[..., 2] + let a = NdArray.range(from: -3, to: 3)[0... ~ 2] XCTAssertEqual(abs(a).dataArray, [3, 1, 1]) } // 2d C contiguous @@ -41,7 +41,7 @@ class basic_functionsTestsInt: XCTestCase { } // 2d not aligned do { - let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1..., 2] + let a = NdArray.range(from: -5, to: 4 * 3 - 5).reshaped([4, 3], order: .C)[1... ~ 2] XCTAssertEqual(abs(a).dataArray, [2, 1, 0, 4, 5, 6]) } } diff --git a/Tests/NdArrayTests/copyTests.swift b/Tests/NdArrayTests/copyTests.swift index cbc6b23..7ebc3ad 100644 --- a/Tests/NdArrayTests/copyTests.swift +++ b/Tests/NdArrayTests/copyTests.swift @@ -29,7 +29,7 @@ class copyTests: XCTestCase { func testInitCopyShouldCopyNonContiguousArray() { do { - let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C)[...][..., 2] + let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C)[0..., 0... ~ 2] let cpy = NdArray(copy: a) XCTAssert(cpy.ownsData) XCTAssertFalse(a.overlaps(cpy)) @@ -38,7 +38,7 @@ class copyTests: XCTestCase { XCTAssert(cpy.isCContiguous) } do { - let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F)[...][..., 2] + let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F)[0..., 0... ~ 2] let cpy = NdArray(copy: a) XCTAssert(cpy.ownsData) XCTAssertFalse(a.overlaps(cpy)) @@ -47,7 +47,7 @@ class copyTests: XCTestCase { XCTAssert(cpy.isCContiguous) // since the original array is not contiguous, the copy defaults to C } do { - let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C)[...][..., 2] + let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C)[0..., 0... ~ 2] let cpy = NdArray(copy: a, order: .F) XCTAssert(cpy.ownsData) XCTAssertFalse(a.overlaps(cpy)) @@ -56,7 +56,7 @@ class copyTests: XCTestCase { XCTAssert(cpy.isFContiguous) } do { - let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F)[...][..., 2] + let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F)[0..., 0... ~ 2] let cpy = NdArray(copy: a, order: .F) XCTAssert(cpy.ownsData) XCTAssertFalse(a.overlaps(cpy)) @@ -110,8 +110,8 @@ class copyTests: XCTestCase { func testCopyToShouldCopyToFContiguousArrayWhenSrcIsNotContiguousAndOutIsFContiguous() { let a = NdArray.range(to: 3 * 4).reshaped([3, 4], order: .F) - let a1 = a[...][..., 2] - let a2 = a[...][2...] + let a1 = a[0..., 0... ~ 2] + let a2 = a[0..., 2...] XCTAssertFalse(a1.isContiguous) XCTAssert(a2.isFContiguous) XCTAssert(a1.overlaps(a2)) diff --git a/Tests/NdArrayTests/initTests.swift b/Tests/NdArrayTests/initTests.swift index ed2beb8..0aee6f7 100644 --- a/Tests/NdArrayTests/initTests.swift +++ b/Tests/NdArrayTests/initTests.swift @@ -257,7 +257,7 @@ class initTests: XCTestCase { } func testInitShouldCreateCContiguousViewWhenSourceIsNotCContiguous() { - let a = NdArray.range(to: 5)[..., 2] + let a = NdArray.range(to: 5)[0... ~ 2] let b = NdArray(a, order: .C) XCTAssert(b.ownsData) XCTAssert(b.ownsData) @@ -276,7 +276,7 @@ class initTests: XCTestCase { } func testInitShouldCreateFContiguousViewWhenSourceIsNotCContiguous() { - let a = NdArray.range(to: 5)[..., 2] + let a = NdArray.range(to: 5)[0... ~ 2] let b = NdArray(a, order: .F) XCTAssert(b.ownsData) XCTAssert(b.ownsData) @@ -295,7 +295,7 @@ class initTests: XCTestCase { } func testInitShouldCreateCContiguousCopyWhenSourceIsNotCContiguous() { - let a = NdArray.range(to: 5)[..., 2] + let a = NdArray.range(to: 5)[0... ~ 2] let b = NdArray(copy: a, order: .C) XCTAssert(b.ownsData) XCTAssert(b.ownsData) @@ -314,7 +314,7 @@ class initTests: XCTestCase { } func testInitShouldCreateFContiguousCopyWhenSourceIsNotCContiguous() { - let a = NdArray.range(to: 5)[..., 2] + let a = NdArray.range(to: 5)[0... ~ 2] let b = NdArray(copy: a, order: .F) XCTAssert(b.ownsData) XCTAssert(b.ownsData) diff --git a/Tests/NdArrayTests/mapTests.swift b/Tests/NdArrayTests/mapTests.swift index 184d752..031b3e1 100644 --- a/Tests/NdArrayTests/mapTests.swift +++ b/Tests/NdArrayTests/mapTests.swift @@ -18,7 +18,7 @@ class mapTests: XCTestCase { XCTAssertEqual(c.dataArray, b.dataArray) } do { - let a = NdArray.zeros([2, 3])[..., 2] + let a = NdArray.zeros([2, 3])[0... ~ 2] let b = NdArray(copy: a) let c: NdArray = a.map { $0 * 2 diff --git a/Tests/NdArrayTests/reshapeTests.swift b/Tests/NdArrayTests/reshapeTests.swift index a232522..179d6de 100644 --- a/Tests/NdArrayTests/reshapeTests.swift +++ b/Tests/NdArrayTests/reshapeTests.swift @@ -66,7 +66,7 @@ class reshapeTests: XCTestCase { func testFlattenedShouldFlattenArrayWhenStrided() { var a = NdArray.range(to: 3 * 4) a.reshape([3, 4]) - a = NdArray(a[...][..., 3]) + a = NdArray(a[0..., 0... ~ 3]) let b = a.flattened() XCTAssertEqual(a.shape, [3, 2]) XCTAssertEqual(a.strides, [4, 3]) diff --git a/Tests/NdArrayTests/subscriptTests.swift b/Tests/NdArrayTests/subscriptTests.swift index 447ef54..60b6742 100644 --- a/Tests/NdArrayTests/subscriptTests.swift +++ b/Tests/NdArrayTests/subscriptTests.swift @@ -6,7 +6,7 @@ import XCTest // swiftlint:disable:next type_body_length -class NdArraySsubscriptTests: XCTestCase { +class NdArraySubscriptTests: XCTestCase { func testSubscriptShouldReturnElementWhenIndexed() { let a = NdArray([1, 2, 3]) XCTAssertEqual(a[[0]], 1) @@ -35,13 +35,13 @@ class NdArraySsubscriptTests: XCTestCase { func testSubscriptShouldReturnRowSliceWhen2dArray() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let r0: NdArray = a[0] + let r0: NdArray = a[Slice(0)] XCTAssertEqual(r0.strides, [1]) XCTAssertEqual(r0.shape, [3]) XCTAssertEqual(r0[[0]], 1) XCTAssertEqual(r0[[1]], 2) XCTAssertEqual(r0[[2]], 3) - let r1: NdArray = a[1] + let r1: NdArray = a[Slice(1)] XCTAssertEqual(r1.strides, [1]) XCTAssertEqual(r1.shape, [3]) XCTAssertEqual(r1[[0]], 4) @@ -51,13 +51,13 @@ class NdArraySsubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let r0: NdArray = a[0] + let r0: NdArray = a[Slice(0)] XCTAssertEqual(r0.strides, [2]) XCTAssertEqual(r0.shape, [3]) XCTAssertEqual(r0[[0]], 1) XCTAssertEqual(r0[[1]], 2) XCTAssertEqual(r0[[2]], 3) - let r1: NdArray = a[1] + let r1: NdArray = a[Slice(1)] XCTAssertEqual(r1.strides, [2]) XCTAssertEqual(r1.shape, [3]) XCTAssertEqual(r1[[0]], 4) @@ -69,17 +69,17 @@ class NdArraySsubscriptTests: XCTestCase { func testSubscriptShouldReturnColSliceWhen2dArray() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let c0: NdArray = a[...][0] + let c0: NdArray = a[0..., Slice(0)] XCTAssertEqual(c0.strides, [3]) XCTAssertEqual(c0.shape, [2]) XCTAssertEqual(c0[[0]], 1) XCTAssertEqual(c0[[1]], 4) - let c1: NdArray = a[...][1] + let c1: NdArray = a[0..., Slice(1)] XCTAssertEqual(c1.strides, [3]) XCTAssertEqual(c1.shape, [2]) XCTAssertEqual(c1[[0]], 2) XCTAssertEqual(c1[[1]], 5) - let c2: NdArray = a[...][2] + let c2: NdArray = a[0..., Slice(2)] XCTAssertEqual(c2.strides, [3]) XCTAssertEqual(c2.shape, [2]) XCTAssertEqual(c2[[0]], 3) @@ -88,17 +88,17 @@ class NdArraySsubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let c0: NdArray = a[...][0] + let c0: NdArray = a[0..., Slice(0)] XCTAssertEqual(c0.strides, [1]) XCTAssertEqual(c0.shape, [2]) XCTAssertEqual(c0[[0]], 1) XCTAssertEqual(c0[[1]], 4) - let c1: NdArray = a[...][1] + let c1: NdArray = a[0..., Slice(1)] XCTAssertEqual(c1.strides, [1]) XCTAssertEqual(c1.shape, [2]) XCTAssertEqual(c1[[0]], 2) XCTAssertEqual(c1[[1]], 5) - let c2: NdArray = a[...][2] + let c2: NdArray = a[0..., Slice(2)] XCTAssertEqual(c2.strides, [1]) XCTAssertEqual(c2.shape, [2]) XCTAssertEqual(c2[[0]], 3) @@ -149,20 +149,20 @@ class NdArraySsubscriptTests: XCTestCase { func testRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let b = a[...][0..<3] + let b = a[0..., 0..<3] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[...][0..<1] + let r0: NdArray = a[0..., 0..<1] XCTAssertEqual(r0.strides, [3, 1]) XCTAssertEqual(r0.shape, [2, 1]) XCTAssertEqual(r0[[0, 0]], 1) XCTAssertEqual(r0[[1, 0]], 4) - let r1: NdArray = a[...][1..<2] + let r1: NdArray = a[0..., 1..<2] XCTAssertEqual(r1.strides, [3, 1]) XCTAssertEqual(r1.shape, [2, 1]) XCTAssertEqual(r1[[0, 0]], 2) XCTAssertEqual(r1[[1, 0]], 5) - let r2: NdArray = a[...][2..<3] + let r2: NdArray = a[0..., 2..<3] XCTAssertEqual(r2.strides, [3, 1]) XCTAssertEqual(r2.shape, [2, 1]) XCTAssertEqual(r2[[0, 0]], 3) @@ -171,20 +171,20 @@ class NdArraySsubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let b = a[...][0..<3] + let b = a[0..., 0..<3] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[...][0..<1] + let r0: NdArray = a[0..., 0..<1] XCTAssertEqual(r0.strides, [1, 2]) XCTAssertEqual(r0.shape, [2, 1]) XCTAssertEqual(r0[[0, 0]], 1) XCTAssertEqual(r0[[1, 0]], 4) - let r1: NdArray = a[...][1..<2] + let r1: NdArray = a[0..., 1..<2] XCTAssertEqual(r1.strides, [1, 2]) XCTAssertEqual(r1.shape, [2, 1]) XCTAssertEqual(r1[[0, 0]], 2) XCTAssertEqual(r1[[1, 0]], 5) - let r2: NdArray = a[...][2..<3] + let r2: NdArray = a[0..., 2..<3] XCTAssertEqual(r2.strides, [1, 2]) XCTAssertEqual(r2.shape, [2, 1]) XCTAssertEqual(r2[[0, 0]], 3) @@ -227,33 +227,33 @@ class NdArraySsubscriptTests: XCTestCase { func testPartialRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - XCTAssertEqual(a[...][1...].shape, [2, 2]) - XCTAssertEqual(a[...][1...].strides, [3, 1]) - XCTAssertEqual(a[...][1...][[0, 0]], 2) + XCTAssertEqual(a[0..., 1...].shape, [2, 2]) + XCTAssertEqual(a[0..., 1...].strides, [3, 1]) + XCTAssertEqual(a[0..., 1...][[0, 0]], 2) - XCTAssertEqual(a[...][...1].shape, [2, 2]) - XCTAssertEqual(a[...][...1].strides, [3, 1]) - XCTAssertEqual(a[...][...1][[0, 0]], 1) + XCTAssertEqual(a[0..., ...1].shape, [2, 2]) + XCTAssertEqual(a[0..., ...1].strides, [3, 1]) + XCTAssertEqual(a[0..., ...1][[0, 0]], 1) - XCTAssertEqual(a[...][..<1].shape, [2, 1]) - XCTAssertEqual(a[...][..<1].strides, [3, 1]) - XCTAssertEqual(a[...][..<1][[0, 0]], 1) + XCTAssertEqual(a[0..., ..<1].shape, [2, 1]) + XCTAssertEqual(a[0..., ..<1].strides, [3, 1]) + XCTAssertEqual(a[0..., ..<1][[0, 0]], 1) } do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - XCTAssertEqual(a[...][1...].shape, [2, 2]) - XCTAssertEqual(a[...][1...].strides, [1, 2]) - XCTAssertEqual(a[...][1...][[0, 0]], 2) + XCTAssertEqual(a[0..., 1...].shape, [2, 2]) + XCTAssertEqual(a[0..., 1...].strides, [1, 2]) + XCTAssertEqual(a[0..., 1...][[0, 0]], 2) - XCTAssertEqual(a[...][...1].shape, [2, 2]) - XCTAssertEqual(a[...][...1].strides, [1, 2]) - XCTAssertEqual(a[...][...1][[0, 0]], 1) + XCTAssertEqual(a[0..., ...1].shape, [2, 2]) + XCTAssertEqual(a[0..., ...1].strides, [1, 2]) + XCTAssertEqual(a[0..., ...1][[0, 0]], 1) - XCTAssertEqual(a[...][..<1].shape, [2, 1]) - XCTAssertEqual(a[...][..<1].strides, [1, 2]) - XCTAssertEqual(a[...][..<1][[0, 0]], 1) + XCTAssertEqual(a[0..., ..<1].shape, [2, 1]) + XCTAssertEqual(a[0..., ..<1].strides, [1, 2]) + XCTAssertEqual(a[0..., ..<1][[0, 0]], 1) } } @@ -284,32 +284,32 @@ class NdArraySsubscriptTests: XCTestCase { func testClosedRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - XCTAssertEqual(a[...][1..<2].shape, [2, 1]) - XCTAssertEqual(a[...][1..<2].strides, [3, 1]) - XCTAssertEqual(a[...][1..<2][[0, 0]], 2) - XCTAssertEqual(a[...][1..<2][[1, 0]], 5) + XCTAssertEqual(a[0..., 1..<2].shape, [2, 1]) + XCTAssertEqual(a[0..., 1..<2].strides, [3, 1]) + XCTAssertEqual(a[0..., 1..<2][[0, 0]], 2) + XCTAssertEqual(a[0..., 1..<2][[1, 0]], 5) - XCTAssertEqual(a[...][1...2].shape, [2, 2]) - XCTAssertEqual(a[...][1...2].strides, [3, 1]) - XCTAssertEqual(a[...][1...2][[0, 0]], 2) - XCTAssertEqual(a[...][1...2][[0, 1]], 3) - XCTAssertEqual(a[...][1...2][[1, 0]], 5) - XCTAssertEqual(a[...][1...2][[1, 1]], 6) + XCTAssertEqual(a[0..., 1...2].shape, [2, 2]) + XCTAssertEqual(a[0..., 1...2].strides, [3, 1]) + XCTAssertEqual(a[0..., 1...2][[0, 0]], 2) + XCTAssertEqual(a[0..., 1...2][[0, 1]], 3) + XCTAssertEqual(a[0..., 1...2][[1, 0]], 5) + XCTAssertEqual(a[0..., 1...2][[1, 1]], 6) } do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - XCTAssertEqual(a[...][1..<2].shape, [2, 1]) - XCTAssertEqual(a[...][1..<2].strides, [1, 2]) - XCTAssertEqual(a[...][1..<2][[0, 0]], 2) - XCTAssertEqual(a[...][1..<2][[1, 0]], 5) + XCTAssertEqual(a[0..., 1..<2].shape, [2, 1]) + XCTAssertEqual(a[0..., 1..<2].strides, [1, 2]) + XCTAssertEqual(a[0..., 1..<2][[0, 0]], 2) + XCTAssertEqual(a[0..., 1..<2][[1, 0]], 5) - XCTAssertEqual(a[...][1...2].shape, [2, 2]) - XCTAssertEqual(a[...][1...2].strides, [1, 2]) - XCTAssertEqual(a[...][1...2][[0, 0]], 2) - XCTAssertEqual(a[...][1...2][[0, 1]], 3) - XCTAssertEqual(a[...][1...2][[1, 0]], 5) - XCTAssertEqual(a[...][1...2][[1, 1]], 6) + XCTAssertEqual(a[0..., 1...2].shape, [2, 2]) + XCTAssertEqual(a[0..., 1...2].strides, [1, 2]) + XCTAssertEqual(a[0..., 1...2][[0, 0]], 2) + XCTAssertEqual(a[0..., 1...2][[0, 1]], 3) + XCTAssertEqual(a[0..., 1...2][[1, 0]], 5) + XCTAssertEqual(a[0..., 1...2][[1, 1]], 6) } } @@ -317,117 +317,117 @@ class NdArraySsubscriptTests: XCTestCase { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) XCTAssertEqual(a[2..<2].shape, [0, 3]) XCTAssertEqual(a[2..<2].count, 0) - XCTAssertEqual(a[...][2..<2].shape, [2, 0]) - XCTAssertEqual(a[...][2..<2].count, 0) + XCTAssertEqual(a[0..., 2..<2].shape, [2, 0]) + XCTAssertEqual(a[0..., 2..<2].count, 0) XCTAssertEqual(a[2...2].shape, [0, 3]) XCTAssertEqual(a[2...2].count, 0) - XCTAssertEqual(a[...][3...3].shape, [2, 0]) - XCTAssertEqual(a[...][3...3].count, 0) + XCTAssertEqual(a[0..., 3...3].shape, [2, 0]) + XCTAssertEqual(a[0..., 3...3].count, 0) } func testStridesSliceAccessWhenRangeIsUnboundAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[..., 1].shape, [6]) - XCTAssertEqual(a[..., 1].strides, [1]) + XCTAssertEqual(a[0... ~ 1].shape, [6]) + XCTAssertEqual(a[0... ~ 1].strides, [1]) - XCTAssertEqual(a[..., 2].shape, [3]) - XCTAssertEqual(a[..., 2].strides, [2]) + XCTAssertEqual(a[0... ~ 2].shape, [3]) + XCTAssertEqual(a[0... ~ 2].strides, [2]) - XCTAssertEqual(a[..., 3].shape, [2]) - XCTAssertEqual(a[..., 3].strides, [3]) + XCTAssertEqual(a[0... ~ 3].shape, [2]) + XCTAssertEqual(a[0... ~ 3].strides, [3]) - XCTAssertEqual(a[..., 4].shape, [2]) - XCTAssertEqual(a[..., 4].strides, [4]) + XCTAssertEqual(a[0... ~ 4].shape, [2]) + XCTAssertEqual(a[0... ~ 4].strides, [4]) - XCTAssertEqual(a[..., 5].shape, [2]) - XCTAssertEqual(a[..., 5].strides, [5]) + XCTAssertEqual(a[0... ~ 5].shape, [2]) + XCTAssertEqual(a[0... ~ 5].strides, [5]) - XCTAssertEqual(a[..., 6].shape, [1]) - XCTAssertEqual(a[..., 6].strides, [6]) + XCTAssertEqual(a[0... ~ 6].shape, [1]) + XCTAssertEqual(a[0... ~ 6].strides, [6]) - XCTAssertEqual(a[..., 7].shape, [1]) - XCTAssertEqual(a[..., 7].strides, [7]) + XCTAssertEqual(a[0... ~ 7].shape, [1]) + XCTAssertEqual(a[0... ~ 7].strides, [7]) } func testStridesSliceAccessWhenRangeIsPartialToAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[..<5, 1].shape, [5]) - XCTAssertEqual(a[..<5, 1].strides, [1]) + XCTAssertEqual(a[..<5 ~ 1].shape, [5]) + XCTAssertEqual(a[..<5 ~ 1].strides, [1]) - XCTAssertEqual(a[..<5, 2].shape, [3]) - XCTAssertEqual(a[..<5, 2].strides, [2]) + XCTAssertEqual(a[..<5 ~ 2].shape, [3]) + XCTAssertEqual(a[..<5 ~ 2].strides, [2]) - XCTAssertEqual(a[..<5, 3].shape, [2]) - XCTAssertEqual(a[..<5, 3].strides, [3]) + XCTAssertEqual(a[..<5 ~ 3].shape, [2]) + XCTAssertEqual(a[..<5 ~ 3].strides, [3]) - XCTAssertEqual(a[..<7, 3].shape, [2]) - XCTAssertEqual(a[..<7, 3].strides, [3]) + XCTAssertEqual(a[..<7 ~ 3].shape, [2]) + XCTAssertEqual(a[..<7 ~ 3].strides, [3]) } func testStridesSliceAccessWhenRangeIsPartialThroughAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[...5, 1].shape, [6]) - XCTAssertEqual(a[...5, 1].strides, [1]) + XCTAssertEqual(a[...5 ~ 1].shape, [6]) + XCTAssertEqual(a[...5 ~ 1].strides, [1]) - XCTAssertEqual(a[...5, 2].shape, [3]) - XCTAssertEqual(a[...5, 2].strides, [2]) + XCTAssertEqual(a[...5 ~ 2].shape, [3]) + XCTAssertEqual(a[...5 ~ 2].strides, [2]) - XCTAssertEqual(a[...5, 3].shape, [2]) - XCTAssertEqual(a[...5, 3].strides, [3]) + XCTAssertEqual(a[...5 ~ 3].shape, [2]) + XCTAssertEqual(a[...5 ~ 3].strides, [3]) - XCTAssertEqual(a[...7, 3].shape, [2]) - XCTAssertEqual(a[...7, 3].strides, [3]) + XCTAssertEqual(a[...7 ~ 3].shape, [2]) + XCTAssertEqual(a[...7 ~ 3].strides, [3]) } func testStridesSliceAccessWhenRangeIsClosedAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[1...5, 1].shape, [5]) - XCTAssertEqual(a[1...5, 1].strides, [1]) - XCTAssertEqual(a[1...5, 1][[0]], 2) - XCTAssertEqual(a[1...5, 1][[1]], 3) + XCTAssertEqual(a[1...5 ~ 1].shape, [5]) + XCTAssertEqual(a[1...5 ~ 1].strides, [1]) + XCTAssertEqual(a[1...5 ~ 1][[0]], 2) + XCTAssertEqual(a[1...5 ~ 1][[1]], 3) - XCTAssertEqual(a[1...5, 2].shape, [3]) - XCTAssertEqual(a[1...5, 2].strides, [2]) - XCTAssertEqual(a[1...5, 2][[0]], 2) - XCTAssertEqual(a[1...5, 2][[1]], 4) + XCTAssertEqual(a[1...5 ~ 2].shape, [3]) + XCTAssertEqual(a[1...5 ~ 2].strides, [2]) + XCTAssertEqual(a[1...5 ~ 2][[0]], 2) + XCTAssertEqual(a[1...5 ~ 2][[1]], 4) - XCTAssertEqual(a[2...5, 3].shape, [2]) - XCTAssertEqual(a[2...5, 3].strides, [3]) - XCTAssertEqual(a[2...5, 3][[0]], 3) - XCTAssertEqual(a[2...5, 3][[1]], 6) + XCTAssertEqual(a[2...5 ~ 3].shape, [2]) + XCTAssertEqual(a[2...5 ~ 3].strides, [3]) + XCTAssertEqual(a[2...5 ~ 3][[0]], 3) + XCTAssertEqual(a[2...5 ~ 3][[1]], 6) - XCTAssertEqual(a[2...7, 3].shape, [2]) - XCTAssertEqual(a[2...7, 3].strides, [3]) - XCTAssertEqual(a[2...7, 3][[0]], 3) - XCTAssertEqual(a[2...7, 3][[1]], 6) + XCTAssertEqual(a[2...7 ~ 3].shape, [2]) + XCTAssertEqual(a[2...7 ~ 3].strides, [3]) + XCTAssertEqual(a[2...7 ~ 3][[0]], 3) + XCTAssertEqual(a[2...7 ~ 3][[1]], 6) } func testStridesSliceAccessWhenRangeIsPartialFromAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[0..., 1].shape, [6]) - XCTAssertEqual(a[0..., 1].strides, [1]) + XCTAssertEqual(a[0... ~ 1].shape, [6]) + XCTAssertEqual(a[0... ~ 1].strides, [1]) - XCTAssertEqual(a[1..., 2].shape, [3]) - XCTAssertEqual(a[1..., 2].strides, [2]) + XCTAssertEqual(a[1... ~ 2].shape, [3]) + XCTAssertEqual(a[1... ~ 2].strides, [2]) } func testStridesSliceAccessWhenRangeAndArray1d() { let a = NdArray([1, 2, 3, 4, 5, 6]) - XCTAssertEqual(a[0..<1, 1].shape, [1]) - XCTAssertEqual(a[0..<1, 1].strides, [1]) + XCTAssertEqual(a[0..<1 ~ 1].shape, [1]) + XCTAssertEqual(a[0..<1 ~ 1].strides, [1]) - XCTAssertEqual(a[1..<5, 2].shape, [2]) - XCTAssertEqual(a[1..<5, 2].strides, [2]) - XCTAssertFalse(a[1..<5, 2].isContiguous) + XCTAssertEqual(a[1..<5 ~ 2].shape, [2]) + XCTAssertEqual(a[1..<5 ~ 2].strides, [2]) + XCTAssertFalse(a[1..<5 ~ 2].isContiguous) } func testSliceAssignmentWhenOverlap() { do { let a = NdArray.range(to: 6) - let b = a[..., 2] - a[1..., 2] = b + let b = a[0... ~ 2] + a[1... ~ 2] = b XCTAssertEqual(a.dataArray, [0, 0, 2, 2, 4, 4]) } do { @@ -441,60 +441,60 @@ class NdArraySsubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguous() { do { let a = NdArray.range(to: 6) - a[2...4] = NdArray.zeros(3)[...] + a[2...4] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 5]) } do { let a = NdArray.range(to: 6) - a[2..<5] = NdArray.zeros(3)[...] + a[2..<5] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 5]) } do { let a = NdArray.range(to: 6) - a[..<5] = NdArray.zeros(5)[...] + a[..<5] = NdArray.zeros(5)[0...] XCTAssertEqual(a.dataArray, [0, 0, 0, 0, 0, 5]) } do { let a = NdArray.range(to: 6) - a[2...] = NdArray.zeros(4)[...] + a[2...] = NdArray.zeros(4)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 0]) } do { let a = NdArray.range(to: 6) - a[...2] = NdArray.zeros(3)[...] + a[...2] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 0, 0, 3, 4, 5]) } } func testSliceAssignmentWhenArrayIs1dNotContiguous() { do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2...4] = NdArray.zeros(3)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2...4] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2..<5] = NdArray.zeros(3)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2..<5] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[..<5] = NdArray.zeros(5)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[..<5] = NdArray.zeros(5)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 0, 0, 0, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2...] = NdArray.zeros(4)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2...] = NdArray.zeros(4)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 0]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[...2] = NdArray.zeros(3)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[...2] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 0, 0, 3, 4, 5]) } } @@ -502,60 +502,60 @@ class NdArraySsubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguousWithStrides() { do { let a = NdArray.range(to: 6) - a[2...4, 2] = NdArray.zeros(2)[...] + a[2...4 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArray.range(to: 6) - a[2..<5, 2] = NdArray.zeros(2)[...] + a[2..<5 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArray.range(to: 6) - a[..<5, 2] = NdArray.zeros(3)[...] + a[..<5 ~ 2] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArray.range(to: 6) - a[2..., 2] = NdArray.zeros(2)[...] + a[2... ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArray.range(to: 6) - a[...2, 2] = NdArray.zeros(2)[...] + a[...2 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 4, 5]) } } func testSliceAssignmentWhenArrayIs1dNotContiguousWithStrides() { do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2...4, 2] = NdArray.zeros(2)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2...4 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2..<5, 2] = NdArray.zeros(2)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2..<5 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[..<5, 2] = NdArray.zeros(3)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[..<5 ~ 2] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[2..., 2] = NdArray.zeros(2)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[2... ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArray(NdArray(empty: 12)[..., 2]) - a[...] = NdArray.range(to: 6)[...] - a[...2, 2] = NdArray.zeros(2)[...] + let a = NdArray(NdArray(empty: 12)[0... ~ 2]) + a[0...] = NdArray.range(to: 6)[0...] + a[...2 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 4, 5]) } } @@ -563,33 +563,33 @@ class NdArraySsubscriptTests: XCTestCase { func testSliceAssignmentWhenCopyFromSelfWithStrides() { do { let a = NdArray.range(to: 12) - a[..., 2] = a[1..., 2] + a[0... ~ 2] = a[1... ~ 2] XCTAssertEqual(NdArray(copy: a).dataArray, [1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11]) } do { let a = NdArray.range(to: 6 * 3).reshaped([6, 3]) - a[..., 2] = a[1..., 2] - XCTAssertEqual(NdArray(a[0]).dataArray, [3, 4, 5]) - XCTAssertEqual(NdArray(a[1]).dataArray, [3, 4, 5]) - XCTAssertEqual(NdArray(a[2]).dataArray, [9, 10, 11]) - XCTAssertEqual(NdArray(a[3]).dataArray, [9, 10, 11]) - XCTAssertEqual(NdArray(a[4]).dataArray, [15, 16, 17]) - XCTAssertEqual(NdArray(a[5]).dataArray, [15, 16, 17]) + a[0... ~ 2] = a[1... ~ 2] + XCTAssertEqual(NdArray(a[Slice(0)]).dataArray, [3, 4, 5]) + XCTAssertEqual(NdArray(a[Slice(1)]).dataArray, [3, 4, 5]) + XCTAssertEqual(NdArray(a[Slice(2)]).dataArray, [9, 10, 11]) + XCTAssertEqual(NdArray(a[Slice(3)]).dataArray, [9, 10, 11]) + XCTAssertEqual(NdArray(a[Slice(4)]).dataArray, [15, 16, 17]) + XCTAssertEqual(NdArray(a[Slice(5)]).dataArray, [15, 16, 17]) } } func testSliceAssignmentWhenArrayIsSlicedAndCopyFromSelfWithStrides() { do { let a = NdArraySlice(NdArray.range(to: 12), sliced: 0) - a[..., 2] = a[1..., 2] + a[0... ~ 2] = a[1... ~ 2] XCTAssertEqual(NdArray(copy: a).dataArray, [1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11]) } do { let a = NdArray.range(to: 3 * 4).reshaped([3, 4]) - a[...][..., 2] = a[...][1..., 2] - XCTAssertEqual(NdArray(a[0]).dataArray, [1, 1, 3, 3]) - XCTAssertEqual(NdArray(a[1]).dataArray, [5, 5, 7, 7]) - XCTAssertEqual(NdArray(a[2]).dataArray, [9, 9, 11, 11]) + a[0..., 0... ~ 2] = a[0..., 1... ~ 2] + XCTAssertEqual(NdArray(a[Slice(0)]).dataArray, [1, 1, 3, 3]) + XCTAssertEqual(NdArray(a[Slice(1)]).dataArray, [5, 5, 7, 7]) + XCTAssertEqual(NdArray(a[Slice(2)]).dataArray, [9, 9, 11, 11]) } } @@ -597,56 +597,56 @@ class NdArraySsubscriptTests: XCTestCase { do { let a = NdArray.zeros([2, 2]) let b = NdArray.ones([2, 2]) - a[0][...] = b[0][...] + a[Slice(0), 0...] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArray.zeros([2, 2]) let b = NdArray.ones([2, 2]) - a[0] = b[0] + a[Slice(0)] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArray.zeros([2, 2]) let b = NdArray.ones([2, 2]) - a[0] = b[0][...] + a[Slice(0)] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArray.zeros([2, 2]) let b = NdArray.ones([2, 2]) - a[...][0] = b[...][0] + a[0..., Slice(0)] = b[0..., Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArray.zeros([2, 2], order: .F) let b = NdArray.ones([2, 2]) - a[0][...] = b[0][...] + a[Slice(0), 0...] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArray.zeros([2, 2], order: .F) let b = NdArray.ones([2, 2]) - a[0][...] = b[0] + a[Slice(0), 0...] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArray.zeros([2, 2], order: .F) let b = NdArray.ones([2, 2]) - a[0] = b[0][...] + a[Slice(0)] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArray.zeros([2, 2], order: .F) let b = NdArray.ones([2, 2]) - a[0] = b[0] + a[Slice(0)] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArray.zeros([2, 2], order: .F) let b = NdArray.ones([2, 2]) - a[...][0] = b[...][0] + a[0..., Slice(0)] = b[0..., Slice(0)] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } } @@ -655,26 +655,26 @@ class NdArraySsubscriptTests: XCTestCase { do { let a = NdArray.range(to: 3 * 4 * 5).reshaped([3, 4, 5]) let b = NdArray.ones(3 * 4 * 5).reshaped([3, 4, 5]) - a[...][0][...] = b[...][1][...] - XCTAssertEqual(NdArray(copy: a[...][0]).dataArray, NdArray(copy: b[...][1]).dataArray) + a[0..., Slice(0), 0...] = b[0..., Slice(1), 0...] + XCTAssertEqual(NdArray(copy: a[0..., Slice(0)]).dataArray, NdArray(copy: b[0..., Slice(1)]).dataArray) } do { let a = NdArray.range(to: 3 * 4 * 5).reshaped([3, 4, 5]) let b = NdArray.ones(3 * 4 * 5).reshaped([3, 4, 5]) - a[...][..., 2][...] = b[...][..., 2][...] - XCTAssertEqual(NdArray(copy: a[...][..., 2]).dataArray, NdArray(copy: b[...][..., 2]).dataArray) + a[0..., 0... ~ 2, 0...] = b[0..., 0... ~ 2, 0...] + XCTAssertEqual(NdArray(copy: a[0..., 0... ~ 2]).dataArray, NdArray(copy: b[0..., 0... ~ 2]).dataArray) } } func testSliceAccess3d() { do { let a = NdArray.range(to: 2 * 2 * 3).reshaped([2, 2, 3]) - XCTAssertEqual(NdArray(copy: a[0], order: .C).dataArray, [0, 1, 2, 3, 4, 5]) + XCTAssertEqual(NdArray(copy: a[Slice(0)], order: .C).dataArray, [0, 1, 2, 3, 4, 5]) } do { let a = NdArray(NdArray.range(to: 2 * 2 * 3).reshaped([2, 2, 3]), order: .F) - XCTAssertEqual(NdArray(copy: a[0], order: .C).dataArray, [0, 1, 2, 3, 4, 5]) + XCTAssertEqual(NdArray(copy: a[Slice(0)], order: .C).dataArray, [0, 1, 2, 3, 4, 5]) } } } @@ -684,8 +684,8 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenOverlap() { do { let a = NdArraySlice(NdArray.range(to: 6)) - let b = a[..., 2] - a[1..., 2] = b + let b = a[0... ~ 2] + a[1... ~ 2] = b XCTAssertEqual(a.dataArray, [0, 0, 2, 2, 4, 4]) } do { @@ -699,60 +699,60 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguous() { do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2...4] = NdArray.zeros(3)[...] + a[2...4] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2..<5] = NdArray.zeros(3)[...] + a[2..<5] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[..<5] = NdArray.zeros(5)[...] + a[..<5] = NdArray.zeros(5)[0...] XCTAssertEqual(a.dataArray, [0, 0, 0, 0, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2...] = NdArray.zeros(4)[...] + a[2...] = NdArray.zeros(4)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 0, 0, 0]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[...2] = NdArray.zeros(3)[...] + a[...2] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 0, 0, 3, 4, 5]) } } func testSliceAssignmentWhenArrayIs1dNotContiguous() { do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2...4] = NdArray.zeros(3)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2...4] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2..<5] = NdArray.zeros(3)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2..<5] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[..<5] = NdArray.zeros(5)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[..<5] = NdArray.zeros(5)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 0, 0, 0, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2...] = NdArray.zeros(4)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2...] = NdArray.zeros(4)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 0, 0, 0]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[...2] = NdArray.zeros(3)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[...2] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 0, 0, 3, 4, 5]) } } @@ -760,60 +760,60 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguousWithStrides() { do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2...4, 2] = NdArray.zeros(2)[...] + a[2...4 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2..<5, 2] = NdArray.zeros(2)[...] + a[2..<5 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[..<5, 2] = NdArray.zeros(3)[...] + a[..<5 ~ 2] = NdArray.zeros(3)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2..., 2] = NdArray.zeros(2)[...] + a[2... ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 0, 5]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - a[...2, 2] = NdArray.zeros(2)[...] + a[...2 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(a.dataArray, [0, 1, 0, 3, 4, 5]) } } func testSliceAssignmentWhenArrayIs1dNotContiguousWithStrides() { do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2...4, 2] = NdArray.zeros(2)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2...4 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2..<5, 2] = NdArray.zeros(2)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2..<5 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[..<5, 2] = NdArray.zeros(3)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[..<5 ~ 2] = NdArray.zeros(3)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[2..., 2] = NdArray.zeros(2)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[2... ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 0, 5]) } do { - let a = NdArraySlice(NdArray(NdArray(empty: 12)[..., 2])) - a[...] = NdArray.range(to: 6)[...] - a[...2, 2] = NdArray.zeros(2)[...] + let a = NdArraySlice(NdArray(NdArray(empty: 12)[0... ~ 2])) + a[0...] = NdArray.range(to: 6)[0...] + a[...2 ~ 2] = NdArray.zeros(2)[0...] XCTAssertEqual(NdArray(copy: a).dataArray, [0, 1, 0, 3, 4, 5]) } } @@ -821,12 +821,12 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenCopyFromSelfWithStrides() { do { let a = NdArraySlice(NdArray.range(to: 12)) - a[..., 2] = a[1..., 2] + a[0... ~ 2] = a[1... ~ 2] XCTAssertEqual(NdArray(copy: a).dataArray, [1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11]) } do { let a = NdArraySlice(NdArray.range(to: 6 * 3).reshaped([6, 3])) - a[..., 2] = a[1..., 2] + a[0... ~ 2] = a[1... ~ 2] XCTAssertEqual(NdArray(a[0]).dataArray, [3, 4, 5]) XCTAssertEqual(NdArray(a[1]).dataArray, [3, 4, 5]) XCTAssertEqual(NdArray(a[2]).dataArray, [9, 10, 11]) @@ -839,12 +839,12 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIsSlicedAndCopyFromSelfWithStrides() { do { let a = NdArraySlice(NdArraySlice(NdArray.range(to: 12), sliced: 0)) - a[..., 2] = a[1..., 2] + a[0... ~ 2] = a[1... ~ 2] XCTAssertEqual(NdArray(copy: a).dataArray, [1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11]) } do { let a = NdArraySlice(NdArray.range(to: 3 * 4).reshaped([3, 4])) - a[...][..., 2] = a[...][1..., 2] + a[0..., 0... ~ 2] = a[0..., 1... ~ 2] XCTAssertEqual(NdArray(a[0]).dataArray, [1, 1, 3, 3]) XCTAssertEqual(NdArray(a[1]).dataArray, [5, 5, 7, 7]) XCTAssertEqual(NdArray(a[2]).dataArray, [9, 9, 11, 11]) @@ -855,56 +855,56 @@ class NdArraySliceSubscriptTests: XCTestCase { do { let a = NdArraySlice(NdArray.zeros([2, 2])) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0][...] = b[0][...] + a[Slice(0), 0...] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2])) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0] = b[0] + a[Slice(0)] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2])) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0] = b[0][...] + a[Slice(0)] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2])) let b = NdArraySlice(NdArray.ones([2, 2])) - a[...][0] = b[...][0] + a[0..., Slice(0)] = b[0..., Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2], order: .F)) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0][...] = b[0][...] + a[Slice(0), 0...] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2], order: .F)) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0][...] = b[0] + a[Slice(0), 0...] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2], order: .F)) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0] = b[0][...] + a[Slice(0)] = b[Slice(0), 0...] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2], order: .F)) let b = NdArraySlice(NdArray.ones([2, 2])) - a[0] = b[0] + a[Slice(0)] = b[Slice(0)] XCTAssertEqual(a.dataArray, [1, 0, 1, 0]) } do { let a = NdArraySlice(NdArray.zeros([2, 2], order: .F)) let b = NdArraySlice(NdArray.ones([2, 2])) - a[...][0] = b[...][0] + a[0..., Slice(0)] = b[0..., Slice(0)] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } } @@ -913,14 +913,14 @@ class NdArraySliceSubscriptTests: XCTestCase { do { let a = NdArraySlice(NdArray.range(to: 3 * 4 * 5).reshaped([3, 4, 5])) let b = NdArraySlice(NdArray.ones(3 * 4 * 5).reshaped([3, 4, 5])) - a[...][0][...] = b[...][1][...] - XCTAssertEqual(NdArray(copy: a[...][0]).dataArray, NdArray(copy: b[...][1]).dataArray) + a[0..., Slice(0), 0...] = b[0..., Slice(1), 0...] + XCTAssertEqual(NdArray(copy: a[0..., Slice(0)]).dataArray, NdArray(copy: b[0..., Slice(1)]).dataArray) } do { let a = NdArraySlice(NdArray.range(to: 3 * 4 * 5).reshaped([3, 4, 5])) let b = NdArraySlice(NdArray.ones(3 * 4 * 5).reshaped([3, 4, 5])) - a[...][..., 2][...] = b[...][..., 2][...] - XCTAssertEqual(NdArray(copy: a[...][..., 2]).dataArray, NdArray(copy: b[...][..., 2]).dataArray) + a[0..., 0... ~ 2, 0...] = b[0..., 0... ~ 2, 0...] + XCTAssertEqual(NdArray(copy: a[0..., 0... ~ 2]).dataArray, NdArray(copy: b[0..., 0... ~ 2]).dataArray) } } @@ -938,9 +938,8 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSingleElementSlice1d() { let a = NdArray.range(to: 4) - let s: NdArray = a[2] + let s: NdArray = a[Slice(2)] XCTAssertEqual(s.shape, [1]) XCTAssertEqual(s.dataArray, [2]) } - }