-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrixLinkedList.go
202 lines (184 loc) · 3.96 KB
/
SparseMatrixLinkedList.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import "fmt"
// Storing a sparse matrix
type Node struct {
row, col int
value float64
next *Node
}
type SparseMatrix struct {
head *Node
rows, cols int
}
/**
* Create a new sparse matrix
*/
func NewSparseMatrix(rows, cols int) *SparseMatrix {
return &SparseMatrix{rows: rows, cols: cols}
}
/**
* Add a new value to the sparse matrix
*/
func (m *SparseMatrix) Add(row, col int, value float64) {
node := &Node{row: row, col: col, value: value}
if m.head == nil {
m.head = node
} else {
current := m.head
for current.next != nil {
current = current.next
}
current.next = node
}
}
/**
* Get the value at a specific row and column
*/
func (m *SparseMatrix) Get(row, col int) float64 {
current := m.head
for current != nil {
if current.row == row && current.col == col {
return current.value
}
current = current.next
}
return 0
}
/**
* Check if the sparse matrix is empty
*/
func (m *SparseMatrix) IsEmpty() bool {
return m.head == nil
}
/**
* Get size of the sparse matrix (rows and columns)
*/
func (m *SparseMatrix) Size() (int, int) {
return m.rows, m.cols
}
/**
* Get the number of rows in the sparse matrix
*/
func (m *SparseMatrix) Rows() int {
return m.rows
}
/**
* Get the number of columns in the sparse matrix
*/
func (m *SparseMatrix) Cols() int {
return m.cols
}
/**
* Has a value at a specific row and column
*/
func (m *SparseMatrix) Has(row, col int) bool {
current := m.head
for current != nil {
if current.row == row && current.col == col {
return true
}
current = current.next
}
return false
}
/**
* Has an index in the real matrix
*/
func (m *SparseMatrix) HasIndex(index int) bool {
current := m.head
for current != nil {
if current.row*m.cols+current.col == index {
return true
}
current = current.next
}
return false
}
/**
* Print the sparse matrix
*/
func (m *SparseMatrix) Print() {
for i := 0; i < m.rows; i++ {
for j := 0; j < m.cols; j++ {
fmt.Printf("%f ", m.Get(i, j))
}
fmt.Println()
}
}
/**
* Get the value at a specific index, if not found return nil
*/
func (m *SparseMatrix) GetIndex(index int) *float64 {
current := m.head
for current != nil {
if current.row*m.cols+current.col == index {
return ¤t.value
}
current = current.next
}
return nil
}
/**
* + Operator
*/
func (m *SparseMatrix) AddMatrix(m2 *SparseMatrix) *SparseMatrix {
if m.rows != m2.rows || m.cols != m2.cols {
return nil
}
result := NewSparseMatrix(m.rows, m.cols)
current := m.head
for current != nil {
result.Add(current.row, current.col, current.value+m2.Get(current.row, current.col))
current = current.next
}
return result
}
/**
* - Operator
*/
func (m *SparseMatrix) SubMatrix(m2 *SparseMatrix) *SparseMatrix {
if m.rows != m2.rows || m.cols != m2.cols {
return nil
}
result := NewSparseMatrix(m.rows, m.cols)
current := m.head
for current != nil {
result.Add(current.row, current.col, current.value-m2.Get(current.row, current.col))
current = current.next
}
return result
}
/**
* * Operator
*/
func (m *SparseMatrix) MulMatrix(m2 *SparseMatrix) *SparseMatrix {
if m.cols != m2.rows {
return nil
}
result := NewSparseMatrix(m.rows, m2.cols)
current := m.head
for current != nil {
for i := 0; i < m2.cols; i++ {
result.Add(current.row, i, result.Get(current.row, i)+current.value*m2.Get(current.col, i))
}
current = current.next
}
return result
}
/**
* / Operator
*/
func (m *SparseMatrix) DivMatrix(m2 *SparseMatrix) *SparseMatrix {
if m.cols != m2.rows {
return nil
}
result := NewSparseMatrix(m.rows, m2.cols)
current := m.head
for current != nil {
for i := 0; i < m2.cols; i++ {
result.Add(current.row, i, result.Get(current.row, i)+current.value/m2.Get(current.col, i))
}
current = current.next
}
return result
}