// test_matdet test matrix determinant // matdet.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_matdet.swift running") 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 det = 0.0 print("m1=\(m1) \n") det = matdet(m1) print("determinant=\(det) \n") print("test_matdet.swift finished") func matdet(_ a: [[Double]]) -> Double { let n=a.count var D = 1.0 // determinant var B = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) // working matrix var row = [Int](repeating:0,count:n) // row interchange indicies var hold = 0 var I_pivot = 0 // pivot indicies var pivot = 0.0 // pivot element value var abs_pivot = 0.0 if a[0].count != n { print("Error in Matrix.determinant, inconsistent array sizes.") } // build working matrix for i in 0.. abs_pivot { I_pivot = i pivot = B[row[i]][k] abs_pivot = abs(pivot) } } // have pivot, interchange row indicies if I_pivot != k { hold = row[k] row[k] = row[I_pivot] row[I_pivot] = hold D = -D } // check for near singular if abs_pivot < 1.0E-20 { return abs_pivot } else { D = D * pivot // reduce about pivot for j in k+1..