[Math] Move the multiplication to the * operator

*= must be done with a temporary variable first to avoid stepping on the other elements.
This commit is contained in:
Eryn Wells 2015-11-01 20:40:27 -08:00
parent 65823610c1
commit 541e7c38ef

View file

@ -270,19 +270,22 @@ public func -<T: Matrix>(lhs: T, rhs: T) -> T {
// MARK: Matrix-Matrix multiplication // MARK: Matrix-Matrix multiplication
public func *=<T: Matrix>(inout lhs: T, rhs: T) { public func *=<T: Matrix>(inout lhs: T, rhs: T) {
for i in 0..<T.dimension { let result = lhs * rhs
for j in 0..<T.dimension { for i in 0..<T.count {
// Each cell is Sigma(k=0, M)(lhs[ik] * rhs[kj] lhs[i] = result[i]
for k in 0..<T.dimension {
lhs[i,j] += lhs[i,k] * rhs[k,j]
}
}
} }
} }
public func *<T: Matrix>(lhs: T, rhs: T) -> T { public func *<T: Matrix>(lhs: T, rhs: T) -> T {
var out = lhs var out = T()
out *= rhs for i in 0..<T.dimension {
for j in 0..<T.dimension {
// Each cell is Sigma(k=0, M)(lhs[ik] * rhs[kj]
for k in 0..<T.dimension {
out[i,j] += lhs[i,k] * rhs[k,j]
}
}
}
return out return out
} }