// matrix2.swift // Numerical matrix examples #if os(OSX) || os(iOS) import Darwin #elseif os(Linux) import Glibc #endif // add two n by m matrices func matadd(_ a: [[Double]], _ b: [[Double]]) -> [[Double]] { let nia = a.count let nja = a[0].count if nia != b.count || nja != b[0].count { return [[Double]](repeating:[Double](repeating:0.0,count:nja),count:nia) } var sum = [[Double]](repeating:[Double](repeating:0.0,count:nja),count:nia) 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.. 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..