From af9372fd6e357fb9fe347feefac46ca803fcfeef Mon Sep 17 00:00:00 2001 From: Daniel Strobusch <1847260+dastrobu@users.noreply.github.com> Date: Mon, 21 Feb 2022 06:28:18 +0100 Subject: [PATCH 1/2] remove varargs on subscripts to avoid subtle bugs when compiling code that worked with version prior to `0.3.0`. Example: a[...][..<3] this would use the old style API on the first subscript, but then the new style API on the second subscript, resulting in a completely differently shaped array since `[..<3]` would ignore the slice on the first dimension. --- README.md | 114 ++-- Sources/NdArray/Matrix.swift | 14 +- Sources/NdArray/NdArray.swift | 12 - Sources/NdArray/Vector.swift | 4 +- Sources/NdArray/apply.swift | 4 +- Sources/NdArray/copy.swift | 2 +- Sources/NdArray/string.swift | 2 +- Tests/NdArrayTests/EquitableTests.swift | 12 +- Tests/NdArrayTests/MatrixTestsDouble.swift | 26 +- Tests/NdArrayTests/MatrixTestsFloat.swift | 26 +- Tests/NdArrayTests/ReadmeExamples.swift | 50 +- Tests/NdArrayTests/VectorSequenceTests.swift | 6 +- Tests/NdArrayTests/VectorTestsDouble.swift | 18 +- Tests/NdArrayTests/VectorTestsFloat.swift | 18 +- Tests/NdArrayTests/applyTests.swift | 12 +- .../NdArrayTests/arithmeticTestsDouble.swift | 100 +-- Tests/NdArrayTests/arithmeticTestsFloat.swift | 100 +-- Tests/NdArrayTests/arithmeticTestsInt.swift | 70 +-- .../basic_functionsTestsDouble.swift | 60 +- .../basic_functionsTestsFloat.swift | 60 +- .../basic_functionsTestsInt.swift | 4 +- Tests/NdArrayTests/copyTests.swift | 12 +- Tests/NdArrayTests/initTests.swift | 8 +- Tests/NdArrayTests/mapTests.swift | 2 +- Tests/NdArrayTests/reshapeTests.swift | 2 +- Tests/NdArrayTests/subscriptTests.swift | 586 +++++++++--------- 26 files changed, 658 insertions(+), 666 deletions(-) diff --git a/README.md b/README.md index e7423de..5d6d217 100644 --- a/README.md +++ b/README.md @@ -13,34 +13,35 @@ 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) - - [Slices and the Stride Operator `~`](#slices-and-the-stride-operator-) - - [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) + - [Slices and the Stride Operator `~`](#slices-and-the-stride-operator-) + - [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) - [API Changes](#api-changes) - - [TLDR](#tldr) - - [Removal of `NdArraySlice`](#removal-of-ndarrayslice) + - [TLDR](#tldr) + - [Removal of `NdArraySlice`](#removal-of-ndarrayslice) - [Not Implemented](#not-implemented) - [Out of Scope](#out-of-scope) - [Docs](#docs) @@ -87,11 +88,11 @@ Like NumPy's ndarray, slices and strides can be created. ```swift let a = NdArray.range(to: 10) -let b = NdArray(a[0... ~ 2]) // every second element +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[0...].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] ``` @@ -106,24 +107,24 @@ 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] +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)] + 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 @@ -146,11 +147,11 @@ let a = NdArray.ones([2, 2]) print(a) // [[1.0, 1.0], // [1.0, 1.0]] -a[Slice(1)].set(0.0) +a[[Slice(1)]].set(0.0) print(a) // [[1.0, 1.0], // [0.0, 0.0]] -a[0..., Slice(1)].set(2.0) +a[[0..., Slice(1)]].set(2.0) print(a) // [[1.0, 2.0], // [0.0, 2.0]] @@ -161,7 +162,7 @@ use [element indexing](#element-manipulation) instead or use the `Vector` subtyp ```swift let a = NdArray.range(to: 4) -print(a[Slice(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 @@ -177,7 +178,7 @@ let a = NdArray.ones([2, 2]) print(a) // [[1.0, 1.0], // [1.0, 1.0]] -a[0..., Slice(1)].set(0.0) +a[[0..., Slice(1)]].set(0.0) print(a) // [[1.0, 0.0], // [1.0, 0.0]] @@ -193,7 +194,7 @@ print(a) // [4.0, 5.0], // [6.0, 7.0], // [8.0, 9.0]] -a[0... ~ 2].set(0.0) +a[[0... ~ 2]].set(0.0) print(a) // [[0.0, 0.0], // [2.0, 3.0], @@ -212,9 +213,9 @@ 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.0, 3.0] +print(a[[2...4]]) // [2.0, 3.0, 4.0] +print(a[[2...4 ~ 2]]) // [2.0, 4.0] ``` ### `PartialRangeFrom`, `PartialRangeUpTo` and `PartialRangeThrough` Slices @@ -223,16 +224,16 @@ Partial ranges `....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]]) // [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] ``` ## Element Manipulation 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. +subscript to be able to pass indices directly [#44](https://github.com/dastrobu/NdArray/issues/44). ```swift let a = NdArray.range(to: 12).reshaped([2, 2, 3]) @@ -266,7 +267,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(5) var B = NdArray(A) // no copy B = NdArray(copy: A) // copy explicitly required -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 = 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 ``` ### Subtypes @@ -509,13 +510,16 @@ The functions of these libraries are provided by the ### 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: +To migrate from `<=0.3.0` to `0.4.0` upgrade to `0.4.0` first and fix all compile warnings. Do not skip `0.4.0`, since +this can result in undesired behaviour (`a[0..., 2]` will be interpreted as "take slice along zeroth and first +dimension" from `0.5.0` instead of "take slice along zeroth dimension with stride 2" `<=0.3.0`). + +Here are a few rules of thumb to fix compile warnings after upgrading to `0.4.0`: ``` -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 [][][] +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` @@ -556,10 +560,10 @@ object, slices are obtained by ```swift 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. +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. ``` With this API, there is no subtypes returned when slicing, requiring to remember how many times the array was already diff --git a/Sources/NdArray/Matrix.swift b/Sources/NdArray/Matrix.swift index e24c58d..2161699 100644 --- a/Sources/NdArray/Matrix.swift +++ b/Sources/NdArray/Matrix.swift @@ -102,7 +102,7 @@ open class Matrix: NdArray { Cannot transpose matrix with shape \(shape) to matrix with shape \(out.shape). Precondition failed while trying to transpose \(debugDescription) to \(out.debugDescription). """) - out[0..., 0...] = self.transposed()[0..., 0...] + 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[0...] = rhs[0...] + 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[0...] = self[0...] + 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[0...] = self[0...] + 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[0...] = rhs[0...] + 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[0...] = self[0...] + 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[0...] = self[0...] + A[[0...]] = self[[0...]] var ipiv = try A.luFactor() diff --git a/Sources/NdArray/NdArray.swift b/Sources/NdArray/NdArray.swift index 2cf85bf..24a3ded 100644 --- a/Sources/NdArray/NdArray.swift +++ b/Sources/NdArray/NdArray.swift @@ -446,18 +446,6 @@ open class NdArray: CustomDebugStringConvertible, 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/Vector.swift b/Sources/NdArray/Vector.swift index 486f36c..fe234f9 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[0...] = cpy[0...] + 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[0...] = cpy[0...] + self[[0...]] = cpy[[0...]] } } diff --git a/Sources/NdArray/apply.swift b/Sources/NdArray/apply.swift index 20e0aa6..f459f8b 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])[0... ~ 2]) - let c = NdArray(NdArray.ones([6])[0... ~ 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])[0..., 0... ~ 2]) - let c = NdArray(NdArray.ones([2, 6])[0..., 0... ~ 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[Slice(0)]) - XCTAssertEqual(a[0...], b[0...]) + XCTAssertNotEqual(a, b[[Slice(0)]]) + XCTAssertEqual(a[[0...]], b[[0...]]) } } } diff --git a/Tests/NdArrayTests/MatrixTestsDouble.swift b/Tests/NdArrayTests/MatrixTestsDouble.swift index eea2d96..d4b56d0 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0..., Slice(1)].set(2.0) - B[0..., Slice(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[0..., Slice(1)].set(2.0) - B[0..., Slice(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)[0..., 0... ~ 2]) - B[0..., Slice(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[0..., Slice(1)].set(2.0) - B[0..., Slice(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 3fa66c0..1004c8c 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0..., Slice(1)].set(2.0) - B[0..., Slice(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[0..., Slice(1)].set(2.0) - B[0..., Slice(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)[0..., 0... ~ 2]) - B[0..., Slice(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[0..., Slice(1)].set(2.0) - B[0..., Slice(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/ReadmeExamples.swift b/Tests/NdArrayTests/ReadmeExamples.swift index dadbc67..4f0286f 100644 --- a/Tests/NdArrayTests/ReadmeExamples.swift +++ b/Tests/NdArrayTests/ReadmeExamples.swift @@ -16,11 +16,11 @@ class ReadmeExamples: XCTestCase { } func testSlices() { let a = NdArray.range(to: 10) - let b = NdArray(a[0... ~ 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[0...].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[Slice(1)].set(0.0) + a[[Slice(1)]].set(0.0) print(a) // [[1.0, 1.0], // [0.0, 0.0]] - a[0..., Slice(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[Slice(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[0..., Slice(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[0... ~ 2].set(0.0) + a[[0... ~ 2]].set(0.0) print(a) // [[0.0, 0.0], // [2.0, 3.0], @@ -78,16 +78,16 @@ class ReadmeExamples: XCTestCase { } 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] + 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] + 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]) @@ -216,33 +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[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 = 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[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 + 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[0...]) // B has shape [2, 2, 2] + var B = NdArray(A[[0...]]) // B has shape [2, 2, 2] print(B.shape) - B = NdArray(A[0..., 0... ~ 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[0..., 0... ~ 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..., Slice(0)] = B[0...] + A[[0..., Slice(0)]] = B[[0...]] print(A) // [[0.0, 1.0], // [0.0, 1.0]] @@ -280,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])[0... ~ 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])[0... ~ 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])[0... ~ 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 cac0925..de479df 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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 412481c..248ffac 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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 7b7c8bf..5d441cf 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[0... ~ 2].apply { $0 * 2} - b[0... ~ 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[0..., 1... ~ 2].apply { $0 * 2} - b[0..., 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 a18ddcf..bb1654c 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2]) - b[0... ~ 2] = b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2]) - b[0... ~ 2] = b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2], 5) - b[0... ~ 2] = 5 * b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2], 5) - b[0... ~ 2] = 5 * b[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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[0... ~ 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[0... ~ 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 8d4190c..fb26c2d 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2]) - b[0... ~ 2] = b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2]) - b[0... ~ 2] = b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2], 5) - b[0... ~ 2] = 5 * b[0... ~ 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[0... ~ 2].add(3, a[1... ~ 2], 5) - b[0... ~ 2] = 5 * b[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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[0... ~ 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[0... ~ 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 9630121..e0bb39c 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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[0... ~ 2] - XCTAssertEqual(b.dataArray, NdArray(copy: a[0... ~ 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 a2ee2ea..b51f45f 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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 ba0f39b..3c0b385 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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 5c558d4..281f264 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)[0... ~ 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 7ebc3ad..2181383 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)[0..., 0... ~ 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)[0..., 0... ~ 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)[0..., 0... ~ 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)[0..., 0... ~ 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[0..., 0... ~ 2] - let a2 = a[0..., 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 0aee6f7..f46aa15 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)[0... ~ 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)[0... ~ 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)[0... ~ 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)[0... ~ 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 031b3e1..368799c 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])[0... ~ 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 179d6de..e346272 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[0..., 0... ~ 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 60b6742..ce80a47 100644 --- a/Tests/NdArrayTests/subscriptTests.swift +++ b/Tests/NdArrayTests/subscriptTests.swift @@ -35,13 +35,13 @@ class NdArraySubscriptTests: XCTestCase { func testSubscriptShouldReturnRowSliceWhen2dArray() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let r0: NdArray = a[Slice(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[Slice(1)] + let r1: NdArray = a[[Slice(1)]] XCTAssertEqual(r1.strides, [1]) XCTAssertEqual(r1.shape, [3]) XCTAssertEqual(r1[[0]], 4) @@ -51,13 +51,13 @@ class NdArraySubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let r0: NdArray = a[Slice(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[Slice(1)] + let r1: NdArray = a[[Slice(1)]] XCTAssertEqual(r1.strides, [2]) XCTAssertEqual(r1.shape, [3]) XCTAssertEqual(r1[[0]], 4) @@ -69,17 +69,17 @@ class NdArraySubscriptTests: XCTestCase { func testSubscriptShouldReturnColSliceWhen2dArray() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let c0: NdArray = a[0..., Slice(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[0..., Slice(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[0..., Slice(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 NdArraySubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let c0: NdArray = a[0..., Slice(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[0..., Slice(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[0..., Slice(2)] + let c2: NdArray = a[[0..., Slice(2)]] XCTAssertEqual(c2.strides, [1]) XCTAssertEqual(c2.shape, [2]) XCTAssertEqual(c2[[0]], 3) @@ -109,16 +109,16 @@ class NdArraySubscriptTests: XCTestCase { func testRangeSubscriptShouldReturnRowSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let b = a[0..<2] + let b = a[[0..<2]] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[0..<1] + let r0: NdArray = a[[0..<1]] XCTAssertEqual(r0.strides, [3, 1]) XCTAssertEqual(r0.shape, [1, 3]) XCTAssertEqual(r0[[0, 0]], 1) XCTAssertEqual(r0[[0, 1]], 2) XCTAssertEqual(r0[[0, 2]], 3) - let r1: NdArray = a[1..<2] + let r1: NdArray = a[[1..<2]] XCTAssertEqual(r1.strides, [3, 1]) XCTAssertEqual(r1.shape, [1, 3]) XCTAssertEqual(r1[[0, 0]], 4) @@ -128,16 +128,16 @@ class NdArraySubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let b = a[0..<2] + let b = a[[0..<2]] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[0..<1] + let r0: NdArray = a[[0..<1]] XCTAssertEqual(r0.strides, [1, 2]) XCTAssertEqual(r0.shape, [1, 3]) XCTAssertEqual(r0[[0, 0]], 1) XCTAssertEqual(r0[[0, 1]], 2) XCTAssertEqual(r0[[0, 2]], 3) - let r1: NdArray = a[1..<2] + let r1: NdArray = a[[1..<2]] XCTAssertEqual(r1.strides, [1, 2]) XCTAssertEqual(r1.shape, [1, 3]) XCTAssertEqual(r1[[0, 0]], 4) @@ -149,20 +149,20 @@ class NdArraySubscriptTests: XCTestCase { func testRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - let b = a[0..., 0..<3] + let b = a[[0..., 0..<3]] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[0..., 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[0..., 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[0..., 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 NdArraySubscriptTests: XCTestCase { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - let b = a[0..., 0..<3] + let b = a[[0..., 0..<3]] XCTAssertEqual(b.strides, a.strides) XCTAssertEqual(b.shape, a.shape) - let r0: NdArray = a[0..., 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[0..., 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[0..., 2..<3] + let r2: NdArray = a[[0..., 2..<3]] XCTAssertEqual(r2.strides, [1, 2]) XCTAssertEqual(r2.shape, [2, 1]) XCTAssertEqual(r2[[0, 0]], 3) @@ -195,244 +195,244 @@ class NdArraySubscriptTests: XCTestCase { func testPartialRangeSubscriptShouldReturnRowSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - XCTAssertEqual(a[1...].shape, [1, 3]) - XCTAssertEqual(a[1...].strides, [3, 1]) - XCTAssertEqual(a[1...][[0, 0]], 4) + XCTAssertEqual(a[[1...]].shape, [1, 3]) + XCTAssertEqual(a[[1...]].strides, [3, 1]) + XCTAssertEqual(a[[1...]][[0, 0]], 4) - XCTAssertEqual(a[...1].shape, [2, 3]) - XCTAssertEqual(a[...1].strides, [3, 1]) - XCTAssertEqual(a[...1][[0, 0]], 1) + XCTAssertEqual(a[[...1]].shape, [2, 3]) + XCTAssertEqual(a[[...1]].strides, [3, 1]) + XCTAssertEqual(a[[...1]][[0, 0]], 1) - XCTAssertEqual(a[..<1].shape, [1, 3]) - XCTAssertEqual(a[..<1].strides, [3, 1]) - XCTAssertEqual(a[..<1][[0, 0]], 1) + XCTAssertEqual(a[[..<1]].shape, [1, 3]) + XCTAssertEqual(a[[..<1]].strides, [3, 1]) + XCTAssertEqual(a[[..<1]][[0, 0]], 1) } do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - XCTAssertEqual(a[1...].shape, [1, 3]) - XCTAssertEqual(a[1...].strides, [1, 2]) - XCTAssertEqual(a[1...][[0, 0]], 4) + XCTAssertEqual(a[[1...]].shape, [1, 3]) + XCTAssertEqual(a[[1...]].strides, [1, 2]) + XCTAssertEqual(a[[1...]][[0, 0]], 4) - XCTAssertEqual(a[...1].shape, [2, 3]) - XCTAssertEqual(a[...1].strides, [1, 2]) - XCTAssertEqual(a[...1][[0, 0]], 1) + XCTAssertEqual(a[[...1]].shape, [2, 3]) + XCTAssertEqual(a[[...1]].strides, [1, 2]) + XCTAssertEqual(a[[...1]][[0, 0]], 1) - XCTAssertEqual(a[..<1].shape, [1, 3]) - XCTAssertEqual(a[..<1].strides, [1, 2]) - XCTAssertEqual(a[..<1][[0, 0]], 1) + XCTAssertEqual(a[[..<1]].shape, [1, 3]) + XCTAssertEqual(a[[..<1]].strides, [1, 2]) + XCTAssertEqual(a[[..<1]][[0, 0]], 1) } } func testPartialRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - XCTAssertEqual(a[0..., 1...].shape, [2, 2]) - XCTAssertEqual(a[0..., 1...].strides, [3, 1]) - XCTAssertEqual(a[0..., 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[0..., ...1].shape, [2, 2]) - XCTAssertEqual(a[0..., ...1].strides, [3, 1]) - XCTAssertEqual(a[0..., ...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[0..., ..<1].shape, [2, 1]) - XCTAssertEqual(a[0..., ..<1].strides, [3, 1]) - XCTAssertEqual(a[0..., ..<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[0..., 1...].shape, [2, 2]) - XCTAssertEqual(a[0..., 1...].strides, [1, 2]) - XCTAssertEqual(a[0..., 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[0..., ...1].shape, [2, 2]) - XCTAssertEqual(a[0..., ...1].strides, [1, 2]) - XCTAssertEqual(a[0..., ...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[0..., ..<1].shape, [2, 1]) - XCTAssertEqual(a[0..., ..<1].strides, [1, 2]) - XCTAssertEqual(a[0..., ..<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) } } func testClosedRangeSubscriptShouldReturnRowSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - XCTAssertEqual(a[1...2].shape, [1, 3]) - XCTAssertEqual(a[1...2].strides, [3, 1]) - XCTAssertEqual(a[1...2][[0, 0]], 4) + XCTAssertEqual(a[[1...2]].shape, [1, 3]) + XCTAssertEqual(a[[1...2]].strides, [3, 1]) + XCTAssertEqual(a[[1...2]][[0, 0]], 4) - XCTAssertEqual(a[1...3].shape, [1, 3]) - XCTAssertEqual(a[1...3].strides, [3, 1]) - XCTAssertEqual(a[1...3][[0, 0]], 4) + XCTAssertEqual(a[[1...3]].shape, [1, 3]) + XCTAssertEqual(a[[1...3]].strides, [3, 1]) + XCTAssertEqual(a[[1...3]][[0, 0]], 4) } do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .F) - XCTAssertEqual(a[1...2].shape, [1, 3]) - XCTAssertEqual(a[1...2].strides, [1, 2]) - XCTAssertEqual(a[1...2][[0, 0]], 4) + XCTAssertEqual(a[[1...2]].shape, [1, 3]) + XCTAssertEqual(a[[1...2]].strides, [1, 2]) + XCTAssertEqual(a[[1...2]][[0, 0]], 4) - XCTAssertEqual(a[1...3].shape, [1, 3]) - XCTAssertEqual(a[1...3].strides, [1, 2]) - XCTAssertEqual(a[1...3][[0, 0]], 4) + XCTAssertEqual(a[[1...3]].shape, [1, 3]) + XCTAssertEqual(a[[1...3]].strides, [1, 2]) + XCTAssertEqual(a[[1...3]][[0, 0]], 4) } } func testClosedRangeSubscriptShouldReturnColSlices() { do { let a = NdArray([[1, 2, 3], [4, 5, 6]], order: .C) - 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[[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[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) + 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[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[[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[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) + 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) } } func testRangeSubscriptShouldEmptyShapeWhenRangeIsEmpty() { 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[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[[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[0..., 3...3].shape, [2, 0]) - XCTAssertEqual(a[0..., 3...3].count, 0) + XCTAssertEqual(a[[2...2]].shape, [0, 3]) + XCTAssertEqual(a[[2...2]].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[0... ~ 1].shape, [6]) - XCTAssertEqual(a[0... ~ 1].strides, [1]) + XCTAssertEqual(a[[0... ~ 1]].shape, [6]) + XCTAssertEqual(a[[0... ~ 1]].strides, [1]) - XCTAssertEqual(a[0... ~ 2].shape, [3]) - XCTAssertEqual(a[0... ~ 2].strides, [2]) + XCTAssertEqual(a[[0... ~ 2]].shape, [3]) + XCTAssertEqual(a[[0... ~ 2]].strides, [2]) - XCTAssertEqual(a[0... ~ 3].shape, [2]) - XCTAssertEqual(a[0... ~ 3].strides, [3]) + XCTAssertEqual(a[[0... ~ 3]].shape, [2]) + XCTAssertEqual(a[[0... ~ 3]].strides, [3]) - XCTAssertEqual(a[0... ~ 4].shape, [2]) - XCTAssertEqual(a[0... ~ 4].strides, [4]) + XCTAssertEqual(a[[0... ~ 4]].shape, [2]) + XCTAssertEqual(a[[0... ~ 4]].strides, [4]) - XCTAssertEqual(a[0... ~ 5].shape, [2]) - XCTAssertEqual(a[0... ~ 5].strides, [5]) + XCTAssertEqual(a[[0... ~ 5]].shape, [2]) + XCTAssertEqual(a[[0... ~ 5]].strides, [5]) - XCTAssertEqual(a[0... ~ 6].shape, [1]) - XCTAssertEqual(a[0... ~ 6].strides, [6]) + XCTAssertEqual(a[[0... ~ 6]].shape, [1]) + XCTAssertEqual(a[[0... ~ 6]].strides, [6]) - XCTAssertEqual(a[0... ~ 7].shape, [1]) - XCTAssertEqual(a[0... ~ 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[0... ~ 2] - a[1... ~ 2] = b + let b = a[[0... ~ 2]] + a[[1... ~ 2]] = b XCTAssertEqual(a.dataArray, [0, 0, 2, 2, 4, 4]) } do { let a = NdArray.range(to: 6) - let b = a[1..<6] + let b = a[[1..<6]] a[0..<5] = b XCTAssertEqual(a.dataArray, [1, 2, 3, 4, 5, 5]) } @@ -441,60 +441,60 @@ class NdArraySubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguous() { do { let a = NdArray.range(to: 6) - a[2...4] = NdArray.zeros(3)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + a[[...2]] = NdArray.zeros(3)[[0...]] XCTAssertEqual(a.dataArray, [0, 0, 0, 3, 4, 5]) } } func testSliceAssignmentWhenArrayIs1dNotContiguous() { do { - let a = NdArray(NdArray(empty: 12)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2...4] = NdArray.zeros(3)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2..<5] = NdArray.zeros(3)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[..<5] = NdArray.zeros(5)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2...] = NdArray.zeros(4)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[...2] = NdArray.zeros(3)[0...] + 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 NdArraySubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguousWithStrides() { do { let a = NdArray.range(to: 6) - a[2...4 ~ 2] = NdArray.zeros(2)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2...4 ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2..<5 ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[..<5 ~ 2] = NdArray.zeros(3)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[2... ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2]) - a[0...] = NdArray.range(to: 6)[0...] - a[...2 ~ 2] = NdArray.zeros(2)[0...] + 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 NdArraySubscriptTests: XCTestCase { func testSliceAssignmentWhenCopyFromSelfWithStrides() { do { let a = NdArray.range(to: 12) - a[0... ~ 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[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]) + 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[0... ~ 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[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]) + 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 NdArraySubscriptTests: XCTestCase { do { let a = NdArray.zeros([2, 2]) let b = NdArray.ones([2, 2]) - a[Slice(0), 0...] = b[Slice(0), 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[Slice(0)] = b[Slice(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[Slice(0)] = b[Slice(0), 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..., Slice(0)] = b[0..., Slice(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[Slice(0), 0...] = b[Slice(0), 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[Slice(0), 0...] = b[Slice(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[Slice(0)] = b[Slice(0), 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[Slice(0)] = b[Slice(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..., Slice(0)] = b[0..., Slice(0)] + a[[0..., Slice(0)]] = b[[0..., Slice(0)]] XCTAssertEqual(a.dataArray, [1, 1, 0, 0]) } } @@ -655,26 +655,26 @@ class NdArraySubscriptTests: 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..., Slice(0), 0...] = b[0..., Slice(1), 0...] - XCTAssertEqual(NdArray(copy: a[0..., Slice(0)]).dataArray, NdArray(copy: b[0..., Slice(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[0..., 0... ~ 2, 0...] = b[0..., 0... ~ 2, 0...] - XCTAssertEqual(NdArray(copy: a[0..., 0... ~ 2]).dataArray, NdArray(copy: b[0..., 0... ~ 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[Slice(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[Slice(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,13 +684,13 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenOverlap() { do { let a = NdArraySlice(NdArray.range(to: 6)) - let b = a[0... ~ 2] - a[1... ~ 2] = b + let b = a[[0... ~ 2]] + a[[1... ~ 2]] = b XCTAssertEqual(a.dataArray, [0, 0, 2, 2, 4, 4]) } do { let a = NdArraySlice(NdArray.range(to: 6)) - let b = a[1..<6] + let b = a[[1..<6]] a[0..<5] = b XCTAssertEqual(a.dataArray, [1, 2, 3, 4, 5, 5]) } @@ -699,60 +699,60 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenArrayIs1dContiguous() { do { let a = NdArraySlice(NdArray.range(to: 6)) - a[2...4] = NdArray.zeros(3)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2...4] = NdArray.zeros(3)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2..<5] = NdArray.zeros(3)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[..<5] = NdArray.zeros(5)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2...] = NdArray.zeros(4)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[...2] = NdArray.zeros(3)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2...4 ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2..<5 ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[..<5 ~ 2] = NdArray.zeros(3)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[2... ~ 2] = NdArray.zeros(2)[0...] + 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)[0... ~ 2])) - a[0...] = NdArray.range(to: 6)[0...] - a[...2 ~ 2] = NdArray.zeros(2)[0...] + 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,30 +821,30 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSliceAssignmentWhenCopyFromSelfWithStrides() { do { let a = NdArraySlice(NdArray.range(to: 12)) - a[0... ~ 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[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]) - 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(NdArraySlice(NdArray.range(to: 12), sliced: 0)) - a[0... ~ 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[0..., 0... ~ 2] = a[0..., 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[Slice(0), 0...] = b[Slice(0), 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[Slice(0)] = b[Slice(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[Slice(0)] = b[Slice(0), 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..., Slice(0)] = b[0..., Slice(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[Slice(0), 0...] = b[Slice(0), 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[Slice(0), 0...] = b[Slice(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[Slice(0)] = b[Slice(0), 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[Slice(0)] = b[Slice(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..., Slice(0)] = b[0..., Slice(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..., Slice(0), 0...] = b[0..., Slice(1), 0...] - XCTAssertEqual(NdArray(copy: a[0..., Slice(0)]).dataArray, NdArray(copy: b[0..., Slice(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[0..., 0... ~ 2, 0...] = b[0..., 0... ~ 2, 0...] - XCTAssertEqual(NdArray(copy: a[0..., 0... ~ 2]).dataArray, NdArray(copy: b[0..., 0... ~ 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,7 +938,7 @@ class NdArraySliceSubscriptTests: XCTestCase { func testSingleElementSlice1d() { let a = NdArray.range(to: 4) - let s: NdArray = a[Slice(2)] + let s: NdArray = a[[Slice(2)]] XCTAssertEqual(s.shape, [1]) XCTAssertEqual(s.dataArray, [2]) } From c745154b4079bb06b724f7bb25831b0802e12522 Mon Sep 17 00:00:00 2001 From: dastrobu Date: Mon, 21 Feb 2022 05:31:22 +0000 Subject: [PATCH 2/2] chore(docs): update TOC --- README.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5d6d217..842bef4 100644 --- a/README.md +++ b/README.md @@ -13,35 +13,34 @@ 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) - - [Slices and the Stride Operator `~`](#slices-and-the-stride-operator-) - - [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) + - [Slices and the Stride Operator `~`](#slices-and-the-stride-operator-) + - [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) - [API Changes](#api-changes) - - [TLDR](#tldr) - - [Removal of `NdArraySlice`](#removal-of-ndarrayslice) + - [TLDR](#tldr) + - [Removal of `NdArraySlice`](#removal-of-ndarrayslice) - [Not Implemented](#not-implemented) - [Out of Scope](#out-of-scope) - [Docs](#docs)