-
Notifications
You must be signed in to change notification settings - Fork 255
/
matmul.swift
66 lines (57 loc) · 1.54 KB
/
matmul.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Written by Kajal Sinha; distributed under the MIT license
func matgen(_ n: Int, _ seed: Double) -> [[Double]] {
var a = Array(repeating: Array<Double>(repeating: 0, count: n), count: n)
let divideBy = Double(n)
let tmp = seed / divideBy / divideBy
for i in 0..<n {
for j in 0..<n {
a[i][j] = tmp * Double(i - j) * Double(i + j)
}
}
return a
}
func matmul(_ a : [[Double]], b : [[Double]]) ->[[Double]] {
let m = a.count
let n = a[0].count
let p = b[0].count
var x = Array(repeating: Array<Double>(repeating: 0, count: p), count: m)
var c = Array(repeating: Array<Double>(repeating: 0, count: n), count: p)
// Transpose
for i in 0..<n {
for j in 0..<p {
c[j][i] = b[i][j];
}
}
for i in 0..<m {
for j in 0..<p {
var s = 0.0
for k in 0..<n{
s += a[i][k] * c[j][k];
}
x[i][j] = s;
}
}
return x
}
func calc(_ n: Int) -> Double {
let size = n / 2 * 2
let a = matgen(size, 1.0)
let b = matgen(size, 2.0)
let x = matmul(a, b: b)
return x[size / 2][size / 2]
}
@main
struct Matmul {
static func main() {
let n = CommandLine.argc > 1 ? Int(CommandLine.arguments[1])! : 100
let left = calc(101)
let right = -18.67
if abs(left - right) > 0.1 {
fatalError("\(left) != \(right)")
}
notify_with_pid("Swift")
let results = calc(n)
notify("stop")
print(results)
}
}