-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
181 lines (168 loc) · 3.49 KB
/
Matrix.java
File metadata and controls
181 lines (168 loc) · 3.49 KB
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
package matrix;
public class Matrix {
public Node _head = new Node(0);
public float _default;
public int _m;
public int _n;
public Matrix(float def, int m, int n){
_m = m;
_n = n;
_default = def;
}
public Matrix(int m, int n){
_m = m;
_n = n;
_default = 0;
}
public boolean does_intersect(Node i, Node j){
while(i.get_down() != null){
Node r = j;
while (r.get_down() != null){
if(r == i){
return true;
}
r = r.get_right();
}
i = i.get_down();
}
return false;
}
public float get_intersection(Node col, Node row){
while(col != null){
Node r = row;
while(r != null){
if(r == col){
return r.get_contents();
}
r = r.get_right();
}
col = col.get_down();
}
return _default;
}
public float get_val(int i, int j){
if(i == 0 || j == 0){
//throw index error
return -1;
}
if(i > _m || j > _n){
//throw index error
return -1;
}
// If matrix is only default.
if(_head.get_right() == null && _head.get_down() == null){
return _default;
}
Node x_node = _head;
Node y_node = _head;
while(x_node.get_contents() != i){
if(x_node.get_right() == null){
return _default;
}
x_node = x_node.get_right();
}
while(y_node.get_contents() != j){
if(y_node.get_down() == null){
return _default;
}
y_node = y_node.get_down();
}
return get_intersection(x_node, y_node);
}
public void set_val(float i, float j, float val){
Node new_node = new Node(val);
if(_head.get_right() == null || _head.get_down() == null){
Node new_right = new Node(i);
_head._right = new_right;
Node new_down = new Node(j);
_head._down = new_down;
new_right.set_down(new_node);
new_down.set_right(new_node);
}
else{
// Go to indexed column
Node right = _head;
Node down = _head;
while(right.get_contents() < i){right = right.get_right();}
int intersect_count = 0;
while(down != null){
if(does_intersect(right, down) && down.get_contents() < j){
intersect_count ++;
}
down = down.get_down();
}
int x = 1;
while(x < intersect_count){
right = right.get_down();
x ++;
}
new_node.set_down(right.get_down());
right.set_down(new_node);
// Go to indexed row
down = _head;
right = _head;
while(down.get_contents() < j){down = down.get_down();}
intersect_count = 0;
while(right != null){
if(does_intersect(right, down) && down.get_contents() < i){
intersect_count ++;
}
right = right.get_right();
}
x = 1;
while(x < intersect_count){
down = down.get_right();
x ++;
}
new_node.set_right(down.get_right());
down.set_right(new_node);
}
return;
}
public Matrix get_row(int row){
Matrix output_matrix = new Matrix(_m, 1);
int x = 0;
while(x < _m){
output_matrix.set_val(1, x, get_val(row, x));
}
return output_matrix;
}
public void set_row(Node row, float val){
while(row.get_right() != null){
row = row.get_right();
row.set_contents(val);
}
}
public String print_matrix(){
int x = 1;
int y = 1;
String output = "";
while(y != _n){
x = 1;
while(x != _m){
output += get_val(x, y);
output += " ";
x ++;
}
output += "\n";
y ++;
}
return output;
}
public String print_node_pointers(){
String output = "";
Node y = _head;
Node x;
while(y != null){
x = y;
while(x != null){
output += x.get_contents();
output += " ";
x = x.get_right();
}
output += "\n";
y = y.get_down();
}
return output;
}
}