@@ -38,7 +38,7 @@ class LapackTests: XCTestCase {
3838
3939 let n = 4
4040 var P : [ Double ] = Array ( repeating: 0 , count: n * n)
41- for i in 0 ..< p. count {
41+ for i in 0 ..< p. count {
4242 // i iterates columns of P (in row major)
4343 // p[i] indicates which element in the column must be set to one, to create the permutation matrix
4444 P [ i + p[ i] * n] = 1.0
@@ -72,6 +72,9 @@ class LapackTests: XCTestCase {
7272 ]
7373 // convert A to col major
7474 var At = A . mtrans ( m: 2 , n: 2 )
75+ let b : [ Double ] = [
76+ 1.0 , 1.0
77+ ]
7578 // B in row major
7679 let B : [ Double ] = [
7780 1.0 , 2.0 , 3.0 ,
@@ -84,21 +87,65 @@ class LapackTests: XCTestCase {
8487 - 2.0 , 1.0 ,
8588 1.5 , - 0.5 ,
8689 ]
87- // x1 is in row major
88- let x1 = Ainv . mmul ( B: B, m: 2 , n: 3 , p: 2 )
90+ let x1 = Ainv . mmul ( B: b, m: 2 , n: 1 , p: 2 )
91+ // X1 is in row major
92+ let X1 = Ainv . mmul ( B: B, m: 2 , n: 3 , p: 2 )
8993
90- // solution is stored row major in Bt
94+ // solution is stored col major in Bt
9195 try At . gesv ( B: & Bt)
92- let x2 = Bt . mtrans ( m: 2 , n: 3 )
96+ let X2 = Bt . mtrans ( m: 2 , n: 3 )
97+
98+ XCTAssertEqual ( x1 [ 0 ] , X1 [ 0 ] , accuracy: 1e-15 )
99+ XCTAssertEqual ( x1 [ 1 ] , X1 [ 3 ] , accuracy: 1e-15 )
100+ XCTAssertEqual ( X1, X2, accuracy: 1e-15 )
101+ }
102+
103+ func testGtsvDouble( ) throws {
104+ // A in row major
105+ let A : [ Double ] = [
106+ 1.0 , 1.0 , 0.0 , 0.0 ,
107+ - 1.0 , 2.0 , 2.0 , 0.0 ,
108+ 0.0 , - 2.0 , 3.0 , 3.0 ,
109+ 0.0 , 0.0 , - 3.0 , 4.0 ,
110+ ]
111+ // convert A to col major
112+ var At = A . mtrans ( m: 4 , n: 4 )
113+
114+ // diagonals of A
115+ var d : [ Double ] = [ 1.0 , 2.0 , 3.0 , 4.0 , ]
116+ var du : [ Double ] = [ 1.0 , 2.0 , 3.0 , ]
117+ var dl : [ Double ] = [ - 1.0 , - 2.0 , - 3.0 , ]
118+
119+ // B in row major
120+ let B : [ Double ] = [
121+ 1.0 , 2.0 ,
122+ 1.0 , 2.0 ,
123+ 1.0 , 2.0 ,
124+ 1.0 , 2.0 ,
125+ ]
126+ // B in col major
127+ var Bt = B . mtrans ( m: 2 , n: 4 )
128+ // make a copy of Bt
129+ var Ct = Bt
130+
131+ // solve with general solver
132+ // solution is stored col major in Ct
133+ try At . gesv ( B: & Ct)
134+ let X1 = Ct . mtrans ( m: 4 , n: 2 )
135+
136+ // solution is stored col major in Bt
137+ try d. gtsv ( nrhs: 2 , dl: & dl, du: & du, B: & Bt)
138+ let X2 = Bt . mtrans ( m: 4 , n: 2 )
93139
94- XCTAssertEqual ( x1 , x2 , accuracy: 1e-15 )
140+ XCTAssertEqual ( X1 , X2 , accuracy: 1e-15 )
95141 }
96142
97143 static var allTests : [ ( String , ( LapackTests ) -> ( ) throws -> Void ) ] {
98144 return [
99145 ( " testGetrfDouble " , testGetrfDouble) ,
100146 ( " testGetriDouble " , testGetriDouble) ,
101147 ( " testGesvDouble " , testGesvDouble) ,
148+ ( " testGtsvDouble " , testGtsvDouble) ,
102149 ]
103150 }
104151}
0 commit comments