-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
131 lines (109 loc) · 2.9 KB
/
Matrix.java
File metadata and controls
131 lines (109 loc) · 2.9 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
// Creating a matrix of specific rows and columns
class Matrix extends Sequence{
private Sequence row;
private Sequence matrix;
private MyInteger initial;
private int rows;
private int cols;
void Set(int rowsize, int colsize, int value){
Sequence seq;
MyInteger newInt = new MyInteger();
newInt.Set(value);
int limit = rowsize - 1;
for (int j = 0; j <= rowsize; j++){
if(j == rowsize){
seq = (Sequence)matrix.index(j);
seq.node_index(colsize, newInt);
}
}
}
int Get(int rowsize, int colsize){
int getValue = 0;
Sequence seq;
seq = (Sequence)this.matrix.index(rowsize);
getValue = ((MyInteger)seq.index(colsize)).Get();
return getValue;
}
Matrix(int rowsize, int colsize){
matrix = new Sequence();
initial = new MyInteger();
initial.Set(0);
rows = rowsize;
cols = colsize;
for (int i = 0; i < rowsize; i++) {
row = new Sequence();
for (int j = 0; j < colsize; j++){
row.add(initial,j);
}
matrix.add(row, i);
}
}
void Print(){
if(!(rows == 0 || cols == 0)){
Sequence new_row = new Sequence();
for (int i = 0; i < rows; i++){
new_row = (Sequence)matrix.index(i);
new_row.Print();
System.out.println("");
}
}
}
Matrix Sum(Matrix mat) {
int totrows;
int totcols;
if(this.rows < mat.rows){
totrows = mat.rows;
}
else{
totrows = this.rows;
}
if(this.cols < mat.cols){
totcols = mat.cols;
}
else{
totcols = this.cols;
}
Matrix total = new Matrix(totrows, totcols);
Sequence seq1;
Sequence seq2;
int value1;
int value2;
for (int i = 0; i< rows; i++) {
seq1 = (Sequence)this.matrix.index(i);
seq2 = (Sequence)mat.matrix.index(i);
for (int j = 0; j < cols; j++){
value1 = ((MyInteger)seq1.index(j)).Get();
value2 = ((MyInteger)seq2.index(j)).Get();
total.Set(i, j, value1 + value2);
}
}
return total;
}
Matrix Product(Matrix mat){
Matrix total = new Matrix(this.rows, mat.cols);
if(!(this.cols == mat.rows)){
System.out.println("Matrix dimensions incompatible for Product");
Matrix x = new Matrix(0,0);
return x;
}
Sequence seq1, seq2, seq3;
int value1, value2, value3;
int p = 0;
for (int i = 0; i < this.rows; i++) {
seq1 = (Sequence)this.matrix.index(i);
seq2 = (Sequence)mat.matrix.index(i);
for (int j = 0; j < mat.cols; j++){
for (int k = 0; k < this.cols; k++){
seq3 = (Sequence)mat.matrix.index(k);
value1 = ((MyInteger)seq1.index(k)).Get();
value2 = ((MyInteger)seq2.index(j)).Get();
value3 = ((MyInteger)seq3.index(j)).Get();
p = p + (value1*value3);
}
total.Set(i, j, p);
p = 0;
}
}
return total;
}
}