-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix.go
157 lines (126 loc) · 3.09 KB
/
matrix.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"math"
"sync"
)
type FloatingPoint interface {
~float32 | ~float64
}
type Matrix[T FloatingPoint] struct {
Rows, Cols int
Data []T
}
func NewMatrix[T FloatingPoint](rows, cols int) *Matrix[T] {
m := &Matrix[T]{
Rows: rows,
Cols: cols,
Data: make([]T, rows*cols),
}
return m
}
func InitMatrixWithData[T FloatingPoint](rows, cols int, data []T) *Matrix[T] {
m := NewMatrix[T](rows, cols)
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
m.Set(i, j, data[m.Index(i, j)])
}
}
return m
}
func (m Matrix[T]) Transpose() *Matrix[T] {
c_data := make([]T, m.Size())
ptr := 0
for i := 0; i < m.Cols; i++ {
for j := 0; j < m.Rows; j++ {
c_data[ptr] = m.At(j, i)
ptr++
}
}
return InitMatrixWithData[T](m.Cols, m.Rows, c_data)
}
func (m Matrix[T]) Index(i, j int) int {
return i*m.Cols + j;
}
func (m *Matrix[T]) Set(i, j int, v T) {
m.Data[m.Index(i, j)] = v
}
func (m Matrix[T]) At(i, j int) T {
return m.Data[m.Index(i, j)]
}
func (m Matrix[T]) Size() int {
return m.Rows * m.Cols
}
func (a Matrix[T]) Equals(b *Matrix[T]) bool {
if a.Rows != b.Rows || a.Cols != b.Cols {
return false
}
for i := 0; i < a.Size(); i++ {
if math.Abs(float64(a.Data[i] - b.Data[i])) > 0.01 {
return false
}
}
return true
}
func (a Matrix[T]) NaiveMult(b *Matrix[T]) *Matrix[T] {
if a.Cols == b.Rows {
c_data := make([]T, a.Cols * b.Rows)
ptr := 0
for i := 0; i < a.Rows; i++ {
for j := 0; j < b.Cols; j++ {
var sum T = 0.0
for k := 0; k < a.Cols; k++ {
sum += a.At(i, k) * b.At(k, j)
}
c_data[ptr] = sum
ptr++
}
}
return InitMatrixWithData(a.Rows, b.Cols, c_data)
} else {
panic("matrices are the wrong size for multiplication")
}
}
func (a Matrix[T]) TransposeMult(b *Matrix[T]) *Matrix[T] {
if a.Cols == b.Rows {
c_data := make([]T, a.Cols * b.Rows)
ptr := 0
t := b.Transpose()
for i := 0; i < a.Rows; i++ {
for j := 0; j < b.Cols; j++ {
var sum T = 0.0
for k := 0; k < a.Cols; k++ {
sum += a.At(i, k) * t.At(j, k)
}
c_data[ptr] = sum
ptr++
}
}
return InitMatrixWithData(a.Rows, b.Cols, c_data)
} else {
panic("matrices are the wrong size for multiplication")
}
}
func (a Matrix[T]) TransposeMultParallel(b *Matrix[T]) *Matrix[T] {
if a.Cols != b.Rows {
panic("matrices are the wrong size for multiplication")
}
c_data := make([]T, a.Rows*b.Cols)
t := b.Transpose()
var wg sync.WaitGroup
for i := 0; i < a.Rows; i++ {
wg.Add(1) // Add a count to the WaitGroup for the new goroutine
go func(i int) {
defer wg.Done() // Decrease the count when the goroutine completes
ptr := i * b.Cols
for j := 0; j < b.Cols; j++ {
var sum T = 0.0
for k := 0; k < a.Cols; k++ {
sum += a.At(i, k) * t.At(j, k)
}
c_data[ptr+j] = sum
}
}(i)
}
wg.Wait() // Wait for all goroutines to complete
return InitMatrixWithData(a.Rows, b.Cols, c_data)
}