diff --git a/Source/Matrix.swift b/Source/Matrix.swift index d974d4e2..cff0b579 100644 --- a/Source/Matrix.swift +++ b/Source/Matrix.swift @@ -38,14 +38,11 @@ public struct Matrix where Scalar: FloatingPoint, Scalar: ExpressibleByF } public init(_ contents: [[Scalar]]) { - let m: Int = contents.count - let n: Int = contents[0].count - let repeatedValue: Scalar = 0.0 - - self.init(rows: m, columns: n, repeatedValue: repeatedValue) + self.init(rows: contents.count, columns: contents[0].count, repeatedValue: 0.0) for (i, row) in contents.enumerated() { - grid.replaceSubrange(i*n ... i*n+Swift.min(m, row.count), with: row) + precondition(row.count == columns, "All rows should have the same number of columns") + grid.replaceSubrange(i*columns ..< (i + 1)*columns, with: row) } } diff --git a/Tests/SurgeTests/MatrixTests.swift b/Tests/SurgeTests/MatrixTests.swift index 9fa5e186..9bededb0 100644 --- a/Tests/SurgeTests/MatrixTests.swift +++ b/Tests/SurgeTests/MatrixTests.swift @@ -27,8 +27,11 @@ class MatrixTests: XCTestCase { var matrix : Matrix = Matrix([[1, 2, 3, 4], [5,6,7,8], [9, 10, 11, 12]]) func testInit() { - let m = Matrix([[1.0, 2.0]]) - XCTAssertEqual(m.grid, [1.0, 2.0]) + let m1 = Matrix([[1.0, 2.0]]) + XCTAssertEqual(m1.grid, [1.0, 2.0]) + + let m2 = Matrix([[1, 1], [1, -1]]) + XCTAssertEqual(m2.grid, [1, 1, 1, -1]) } func testSubscriptRow() {