// test_matinv.swift // matinv.swift should be imported, included below #if os(OSX) || os(iOS) // for libraries, portable for OSX and Linux import Foundation // or Playground #elseif os(Linux) import Glibc #endif print("test_matinv.swift running") var maxerr = 0.0 let n = 5 let m1 = [[1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 3.0, 4.0, 5.0, 1.0], [3.0, 4.0, 5.0, 1.0, 2.0], [4.0, 5.0, 1.0, 2.0, 3.0], [5.0, 4.0, 3.0, 2.0, 1.0]] var invm1 = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) var invinvm1 = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) print("m1=\(m1) \n") invm1 = matinv(m1) print("matinv(m1)=\(invm1) \n") invinvm1 = matinv(invm1) print("matinv(matinv(m1)) close m1=\(invinvm1) \n") print("check matinv(matinv(m1))-m1 for error") for i in 0.. [[Double]] { let n = a.count var inv = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) for i in 0.. abs_pivot { I_pivot = i J_pivot = j pivot = inv[row[i]][col[j]] } } } if abs(pivot) < 1.0E-10 { print("Matrix is singular !") return [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) } hold = row[k] row[k] = row[I_pivot] row[I_pivot] = hold hold = col[k] col[k] = col[J_pivot] col[J_pivot] = hold // reduce about pivot inv[row[k]][col[k]] = 1.0 / pivot for j in 0..