-> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements: [Float] = [] elements.reserveCapacity(numCols * numRows) for r in 0..<numRows { for c in 0..<numCols { var e: Float = 0.0 for i in 0..<n { e += self[r, i] * tensor[i, c] // ͜͜Ͱ `subscript` Λར༻ } elements.append(e) } } return Tensor(shape: [numRows, numCols], elements: elements) } }
-> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements: [Float] = [] elements.reserveCapacity(numCols * numRows) for r in 0..<numRows { for c in 0..<numCols { var e: Float = 0.0 for i in 0..<n { e += self.elements[r * n + i] * tensor.elements[i * numCols + c] // ߦྻͱͯ͠ܭࢉ } elements.append(e) } } return Tensor(shape: [numRows, numCols], elements: elements) } }
-> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] var elements = [Float](count: numCols * numRows, repeatedValue: 0.0) for r in 0..<numRows { for i in 0..<n { // ͜͜ͱ let e = self.elements[r * n + i] for c in 0..<numCols { // ͕͜͜ೖΕସΘͬͨ elements[r * numCols + c] += e * tensor.elements[i * numCols + c] } } } return Tensor(shape: [numRows, numCols], elements: elements) } }
-> Tensor { precondition(shape.count == 2) precondition(tensor.shape.count == 2) let n = shape[1] precondition(n == tensor.shape[0]) let numRows = shape[0] let numCols = tensor.shape[1] // `Array` ΛϙΠϯλʹม let leftHead = UnsafeMutablePointer<Float>(self.elements) let rightHead = UnsafeMutablePointer<Float>(tensor.elements) let elements = [Float](count: numCols * numRows, repeatedValue: 0.0) for r in 0..<numRows { for i in 0..<n { var pointer = UnsafeMutablePointer<Float>(elements) + r * numCols let left = leftHead[r * n + i] var rightPointer = rightHead + i * numCols for _ in 0..<numCols { // ͜͜Ͱ `Array` ͷΠϯσοΫεʹΑΔΞυϨεܭࢉ͕ݮͬͯߴԽ pointer.memory += left * rightPointer.memory pointer += 1 rightPointer += 1 } } } return Tensor(shape: [numRows, numCols], elements: elements) } }