Fix formatting
This commit is contained in:
parent
20487a63da
commit
f077725674
|
@ -39,11 +39,11 @@ class MatrixTests: XCTestCase {
|
|||
func getMatrix() -> Matrix<Float> {
|
||||
return self.matrixFloat
|
||||
}
|
||||
|
||||
|
||||
func getMatrix() -> Matrix<Double> {
|
||||
return self.matrixDouble
|
||||
}
|
||||
|
||||
|
||||
func test_init() {
|
||||
let m1 = Matrix([[1.0, 2.0]])
|
||||
XCTAssertEqual(m1.grid, [1.0, 2.0])
|
||||
|
@ -64,33 +64,33 @@ class MatrixTests: XCTestCase {
|
|||
|
||||
func test_subscript_row_set() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
var matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
matrix[row: 0] = [13.0, 14.0, 15.0, 16.0]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[13, 14, 15, 16],
|
||||
[5, 6, 7, 8],
|
||||
[9, 10, 11, 12],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix, expected)
|
||||
}
|
||||
|
||||
func test_subscript_column_set() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
var matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
matrix[column: 0] = [20, 30, 40]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[20, 2, 3, 4],
|
||||
[30, 6, 7, 8],
|
||||
[40, 10, 11, 12],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix, expected)
|
||||
}
|
||||
|
||||
|
@ -101,139 +101,139 @@ class MatrixTests: XCTestCase {
|
|||
|
||||
func test_add_matrix_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
let expected: Matrix<Float> = [
|
||||
[2, 4, 6, 8],
|
||||
[10, 12, 14, 16],
|
||||
[18, 20, 22, 24],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix + matrix, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_add_matrix_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[2, 4, 6, 8],
|
||||
[10, 12, 14, 16],
|
||||
[18, 20, 22, 24],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix + matrix, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_sub_matrix_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
let expected: Matrix<Float> = [
|
||||
[0, 0, 0, 0],
|
||||
[0, 0, 0, 0],
|
||||
[0, 0, 0, 0],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix - matrix, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_sub_matrix_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let matrix: Matrix<Scalar> = getMatrix()
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[0, 0, 0, 0],
|
||||
[0, 0, 0, 0],
|
||||
[0, 0, 0, 0],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(matrix - matrix, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_mul_scalar_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Scalar = 2
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[4, 8, 12],
|
||||
[20, 24, 28],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs * rhs, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_mul_scalar_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Scalar = 2
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[4, 8, 12],
|
||||
[20, 24, 28],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs * rhs, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_mul_matrix_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 4],
|
||||
[10, 12],
|
||||
[18, 20],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[44, 56, 68],
|
||||
[140, 184, 228],
|
||||
[236, 312, 388],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs * rhs, expected)
|
||||
}
|
||||
|
||||
|
||||
func test_mul_matrix_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 4],
|
||||
[10, 12],
|
||||
[18, 20],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[44, 56, 68],
|
||||
[140, 184, 228],
|
||||
[236, 312, 388],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs * rhs, expected)
|
||||
}
|
||||
|
||||
|
@ -271,192 +271,192 @@ class MatrixTests: XCTestCase {
|
|||
|
||||
func test_div_matrix_scalar_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Scalar = 2
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[5, 6, 7],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs / rhs, expected, accuracy: 1e-6)
|
||||
}
|
||||
|
||||
|
||||
func test_div_matrix_scalar_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 4, 6],
|
||||
[10, 12, 14],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Scalar = 2
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[5, 6, 7],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs / rhs, expected, accuracy: 1e-8)
|
||||
}
|
||||
|
||||
|
||||
func test_div_matrix_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 3],
|
||||
[2, 2],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[-1, 3/2],
|
||||
[1, -1],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[10, 12],
|
||||
[8, 10],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs / rhs, expected, accuracy: 1e-5)
|
||||
}
|
||||
|
||||
|
||||
func test_div_matrix_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 3],
|
||||
[2, 2],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Matrix<Scalar> = [
|
||||
[-1, 3/2],
|
||||
[1, -1],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[10, 12],
|
||||
[8, 10],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(lhs / rhs, expected, accuracy: 1e-8)
|
||||
}
|
||||
|
||||
|
||||
func test_pow_matrix_scalar_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Scalar = 2
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = .init(
|
||||
lhs.map { row in
|
||||
row.map { pow($0, rhs) }
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
// FIXME: Replace with `**` operator:
|
||||
XCTAssertEqual(pow(lhs, rhs), expected, accuracy: 1e-5)
|
||||
}
|
||||
|
||||
|
||||
func test_pow_matrix_scalar_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let rhs: Scalar = 2
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = .init(
|
||||
lhs.map { row in
|
||||
row.map { pow($0, rhs) }
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
// FIXME: Replace with `**` operator:
|
||||
XCTAssertEqual(pow(lhs, rhs), expected, accuracy: 1e-8)
|
||||
}
|
||||
|
||||
|
||||
func test_exp_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = .init(
|
||||
lhs.map { row in
|
||||
row.map { exp($0) }
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
XCTAssertEqual(exp(lhs), expected, accuracy: 1e-5)
|
||||
}
|
||||
|
||||
|
||||
func test_exp_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = .init(
|
||||
lhs.map { row in
|
||||
row.map { exp($0) }
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
XCTAssertEqual(exp(lhs), expected, accuracy: 1e-8)
|
||||
}
|
||||
|
||||
|
||||
func test_sum_matrix_rows_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
// FIXME: replace with row iterator:
|
||||
let expected: Matrix<Scalar> = [
|
||||
[6],
|
||||
[15],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(sum(lhs, axies: .row), expected, accuracy: 1e-5)
|
||||
}
|
||||
|
||||
|
||||
func test_sum_matrix_columns_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
// FIXME: replace with column iterator:
|
||||
let expected: Matrix<Scalar> = [
|
||||
[5, 7, 9],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(sum(lhs, axies: .column), expected, accuracy: 1e-5)
|
||||
}
|
||||
|
||||
|
||||
func test_inv_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 3],
|
||||
[2, 2],
|
||||
|
@ -466,96 +466,96 @@ class MatrixTests: XCTestCase {
|
|||
[-1, 1.5],
|
||||
[1, -1],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(inv(lhs), expected, accuracy: 1e-8)
|
||||
}
|
||||
|
||||
|
||||
func test_inv_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[2, 3],
|
||||
[2, 2],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[-1, 1.5],
|
||||
[1, -1],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(inv(lhs), expected, accuracy: 1e-6)
|
||||
}
|
||||
|
||||
|
||||
func test_tranpose_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[1, 4],
|
||||
[2, 5],
|
||||
[3, 6],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(transpose(lhs), expected)
|
||||
}
|
||||
|
||||
|
||||
func test_tranpose_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Matrix<Scalar> = [
|
||||
[1, 4],
|
||||
[2, 5],
|
||||
[3, 6],
|
||||
]
|
||||
|
||||
|
||||
XCTAssertEqual(transpose(lhs), expected)
|
||||
}
|
||||
|
||||
|
||||
func test_det_matrix_double() {
|
||||
typealias Scalar = Double
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2],
|
||||
[5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Scalar = 6 - 10
|
||||
|
||||
|
||||
XCTAssertEqual(det(lhs), expected)
|
||||
}
|
||||
|
||||
|
||||
func test_det_matrix_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let lhs: Matrix<Scalar> = [
|
||||
[1, 2],
|
||||
[5, 6],
|
||||
]
|
||||
|
||||
|
||||
let expected: Scalar = 6 - 10
|
||||
|
||||
|
||||
XCTAssertEqual(det(lhs), expected)
|
||||
}
|
||||
|
||||
|
||||
func test_mul_empty_float() {
|
||||
typealias Scalar = Float
|
||||
|
||||
|
||||
let x: Matrix<Scalar> = [
|
||||
[1]
|
||||
]
|
||||
let y: Matrix<Scalar> = [
|
||||
[]
|
||||
]
|
||||
|
||||
|
||||
let result = x * y
|
||||
|
||||
XCTAssertEqual(result.rows, 1)
|
||||
|
|
Loading…
Reference in New Issue