Skip to content

Added closure-based initializers for Matrix & Vector and corresponding unit test #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Sources/Surge/Linear Algebra/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
self.grid = grid
}

public init(rows: Int, columns: Int, _ closure: (_ row: Int, _ column: Int) throws -> Scalar) rethrows {
var grid: [Scalar] = []
grid.reserveCapacity(rows * columns)

for row in 0..<rows {
for column in 0..<columns {
grid.append(try closure(row, column))
}
}

self.init(rows: rows, columns: columns, grid: grid)
}

public static func identity(size: Int) -> Matrix<Scalar> {
return self.diagonal(rows: size, columns: size, repeatedValue: 1.0)
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Surge/Linear Algebra/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public struct Vector<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
self.dimensions = scalars.count
self.scalars = scalars
}

public init(dimensions: Int, _ closure: (Int) throws -> Scalar) rethrows {
var scalars: [Scalar] = []
scalars.reserveCapacity(dimensions)

for index in 0..<dimensions {
scalars.append(try closure(index))
}

self.init(scalars)
}
}

// MARK: - ExpressibleByArrayLiteral
Expand Down
16 changes: 16 additions & 0 deletions Tests/SurgeTests/MatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ class MatrixTests: XCTestCase {
XCTAssertEqual(actual, expected)
}

func test_init_rows_columns_closure() {
typealias Scalar = Double

let columns = 4
let actual: Matrix<Scalar> = Matrix(rows: 3, columns: columns) { row, column in
Scalar((row * columns) + column)
}
let expected: Matrix<Scalar> = [
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
]

XCTAssertEqual(actual, expected)
}

func test_init_arrayLiteral() {
typealias Scalar = Double

Expand Down
23 changes: 23 additions & 0 deletions Tests/SurgeTests/VectorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ import XCTest
class VectorTests: XCTestCase {
// MARK: - Initialization

func test_init_dimensions_repeatedValue() {
typealias Scalar = Double

let dimensions = 5
let repeatedValue: Scalar = 42

let actual: Vector<Scalar> = Vector(dimensions: dimensions, repeatedValue: repeatedValue)

let expected: Vector<Scalar> = [42, 42, 42, 42, 42]

XCTAssertEqual(actual, expected)
}

func test_init() {
typealias Scalar = Double

Expand All @@ -36,6 +49,16 @@ class VectorTests: XCTestCase {
XCTAssertEqual(lhs.scalars, values)
}

func test_init_dimensions_closure() {
typealias Scalar = Double

let dimensions = 5
let actual: Vector<Scalar> = Vector(dimensions: dimensions) { Scalar($0) }
let expected: Vector<Scalar> = [0, 1, 2, 3, 4]

XCTAssertEqual(actual, expected)
}

// MARK: - Subscript

func test_subscript() {
Expand Down