Skip to content

Commit

Permalink
Fix Matrix init bug, for real now
Browse files Browse the repository at this point in the history
Fix #57
  • Loading branch information
alejandro-isaza committed Sep 19, 2017
1 parent f1037d7 commit 5e9418d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 3 additions & 6 deletions Source/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ public struct Matrix<Scalar> 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)
}
}

Expand Down
7 changes: 5 additions & 2 deletions Tests/SurgeTests/MatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class MatrixTests: XCTestCase {
var matrix : Matrix<Double> = Matrix<Double>([[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() {
Expand Down

0 comments on commit 5e9418d

Please sign in to comment.