// matrix.swift should be importable, test included at bottom #if os(OSX) || os(iOS) import Darwin #elseif os(Linux) import Glibc #endif print("matrix.swift running, should be an import, test included") // vector functions // absolute value of a vector func vecabs(_ numbers: [Double]) -> Double { var total: Double = 0 for number in numbers { total += abs(number) } return total } // end vecabs // length of a vector, sum of squares func veclen(_ numbers: [Double]) -> Double { var total: Double = 0 for number in numbers { total += number*number } return total } // end veclen // vector difference errors average, RMS, max func vecerr(_ a: [Double], _ b: [Double]) { let n = a.count var sum = 0.0 var sumsq = 0.0 var adiff = 0.0 var max = abs(a[0]-b[0]) for i in 0..max { max = adiff } } sumsq = sqrt(sumsq/Double(n)) adiff = sum/Double(n) print("errors average=\(adiff), RMS=\(sumsq), max=\(max)") return } // end vecerr // vector dot product func vecdot(_ a: [Double], _ b: [Double]) -> Double { let n = a.count var dot = 0.0 for i in 0.. [Double] { let n = x.count var prod = [Double](repeating:0.0,count:n) var k = 0 var sign = 1.0 for m in 0..=n { k = k-n } prod[m] = prod[m] + x[j]+y[k] } prod[m] = sign*prod[m] sign = -sign } return prod } // end veccross // vector sum func vecadd(_ a: [Double], _ b: [Double]) -> [Double] { let n = a.count var sum = [Double](repeating:0.0,count:n) for i in 0.. [Double] { let n = a.count var dif = [Double](repeating:0.0,count:n) for i in 0.. [[Double]] { let mat = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) return mat } // end matmk // add two n by n matrices func matadd(_ n: Int,_ a: [[Double]], _ b: [[Double]]) -> [[Double]] { var sum = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) for i in 0.. [[Double]] { let nia = a.count // print("mataddv nia = \(nia)") let nja = a[0].count // print("mataddv nja = \(nja)") 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]] { var dif = [[Double]](repeating:[Double](repeating:0.0,count:n),count:n) for i in 0.. [[Double]] { let ni = a.count let nk = a[0].count let nm = b.count let nj = b[0].count if nk != nm || ni != nj { print("inconsistant matrix sizes a=\(ni),\(nk) b=\(nm),\(nj)") return [[Double]](repeating:[Double](repeating:0.0,count:nk),count:ni) } var prod = [[Double]](repeating:[Double](repeating:0.0,count:ni),count:nj) 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..