Avatar
@_inlineable public static func multiply3<p>(a: Matrix<R, n, m>, b: Matrix<R, m, p>) -> Matrix<R, n, p> { assert(a.cols == b.rows, "Mismatching matrix size.") return Matrix<R, n, p>(rows: a.rows, cols: b.cols, type: (a.type == b.type) ? a.type : .Default) { (i, k) -> R in var x = R.zero for j in 0..<a.cols { x = x + a[i, j] * b[j, k] } return x } } @_inlineable public static func multiply4<p>(a: Matrix<R, n, m>, b: Matrix<R, m, p>) -> Matrix<R, n, p> { assert(a.cols == b.rows, "Mismatching matrix size.") return Matrix<R, n, p>(rows: a.rows, cols: b.cols, type: (a.type == b.type) ? a.type : .Default) { (i, k) -> R in sigma(0 ..< a.cols){ j in a[i, j] * b[j, k] } } }
8:23 AM
@_inlineable public func sigma<I, T: AdditiveGroup>(_ range: CountableRange<I>, _ f: (I) -> T) -> T { var s: T = T.zero var i = range.startIndex while i != range.endIndex { s = s + f(i) i = range.index(after: i) } return s }
8:23 AM
これでも10倍ぐらい遅い
8:23 AM
Σを関数化すれば map, reduce がなくなって Array を使わないのでいけると思ったけど駄目だった (edited)