2017-11-26 04:17:56 +08:00
|
|
|
// Copyright © 2016 Wenbin Zhang, Mattt Thompson (http://mattt.me)
|
2016-02-14 09:42:11 +08:00
|
|
|
//
|
2017-09-19 12:51:03 +08:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
2016-02-14 09:42:11 +08:00
|
|
|
//
|
2017-09-19 12:51:03 +08:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
2016-02-14 09:42:11 +08:00
|
|
|
//
|
2017-09-19 12:51:03 +08:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
2016-02-14 09:42:11 +08:00
|
|
|
|
2017-09-19 13:29:11 +08:00
|
|
|
@testable import Surge
|
2017-11-26 04:17:56 +08:00
|
|
|
import XCTest
|
2016-02-14 09:42:11 +08:00
|
|
|
|
|
|
|
class MatrixTests: XCTestCase {
|
2017-09-25 14:07:45 +08:00
|
|
|
|
|
|
|
var matrix: Matrix<Double> = Matrix<Double>([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
|
2017-09-19 13:29:11 +08:00
|
|
|
|
|
|
|
func testInit() {
|
2017-09-19 23:04:40 +08:00
|
|
|
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])
|
2017-09-19 13:29:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testSubscriptRow() {
|
2017-09-25 14:07:45 +08:00
|
|
|
XCTAssertEqual(matrix[row: 0], [1, 2, 3, 4])
|
2016-02-14 09:42:11 +08:00
|
|
|
XCTAssertEqual(matrix[row: 1], [5, 6, 7, 8])
|
|
|
|
}
|
2017-09-25 14:07:45 +08:00
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testSubscriptColumn() {
|
2017-09-25 14:07:45 +08:00
|
|
|
XCTAssertEqual(matrix[column: 0], [1, 5, 9])
|
|
|
|
XCTAssertEqual(matrix[column: 1], [2, 6, 10])
|
2016-02-14 09:42:11 +08:00
|
|
|
}
|
2017-09-25 14:07:45 +08:00
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testSetRow() {
|
|
|
|
matrix[row: 0] = [13.0, 14.0, 15.0, 16.0]
|
2017-11-26 04:17:56 +08:00
|
|
|
XCTAssertTrue(matrix == Matrix<Double>([[13, 14, 15, 16], [5, 6, 7, 8], [9, 10, 11, 12]]))
|
2016-02-14 09:42:11 +08:00
|
|
|
}
|
2017-09-25 14:07:45 +08:00
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testSetColumn() {
|
2017-09-25 14:07:45 +08:00
|
|
|
matrix[column: 0] = [20, 30, 40]
|
|
|
|
XCTAssertEqual(matrix, Matrix<Double>([[20, 2, 3, 4], [30, 6, 7, 8], [40, 10, 11, 12]]))
|
2016-02-14 09:42:11 +08:00
|
|
|
}
|
2017-09-25 14:07:45 +08:00
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testMatrixPower() {
|
|
|
|
let expectedResult = Matrix<Double>([[1, 4, 9, 16], [25, 36, 49, 64], [81, 100, 121, 144]])
|
|
|
|
XCTAssertEqual(pow(matrix, 2), expectedResult)
|
|
|
|
}
|
2017-09-25 14:07:45 +08:00
|
|
|
|
2017-11-26 04:27:50 +08:00
|
|
|
func testMatrixAddition() {
|
|
|
|
let expectedResult: Matrix<Double> = Matrix<Double>([[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]])
|
|
|
|
XCTAssertEqual(matrix + matrix, expectedResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMatrixSubtraction() {
|
|
|
|
let expectedResult: Matrix<Double> = Matrix<Double>([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])
|
|
|
|
XCTAssertEqual(matrix - matrix, expectedResult)
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:42:11 +08:00
|
|
|
func testElementWiseMultiplication() {
|
2017-09-25 14:07:45 +08:00
|
|
|
let matrix2 = Matrix<Double>([[2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13]])
|
|
|
|
XCTAssertEqual(elmul(matrix, matrix2), Matrix<Double>([[2, 6, 12, 20], [30, 42, 56, 72], [90, 110, 132, 156]]))
|
2016-02-14 09:42:11 +08:00
|
|
|
}
|
|
|
|
}
|