-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.js
More file actions
179 lines (162 loc) · 6.19 KB
/
matrix.js
File metadata and controls
179 lines (162 loc) · 6.19 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
function generateMatrices() {
createMatrix('The 1st Matrix', 'matrix1', document.getElementById('matrix1Rows').value, document.getElementById('matrix1Cols').value);
createMatrix('The 2nd Matrix','matrix2', document.getElementById('matrix2Rows').value, document.getElementById('matrix2Cols').value);
}
const createMatrix = (title, containerId, rows, cols) => {
let container = document.getElementById(containerId);
container.innerHTML = ''; // Clear previous content
let table = document.createElement('table');
for (let i = 0; i < rows; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < cols; j++) {
let td = document.createElement('td');
let input = document.createElement('input');
input.type = 'number';
input.value = Math.floor(Math.random() * 100); // Random value between 0 and 99
td.appendChild(input);
tr.appendChild(td);
}
table.appendChild(tr);
}
let caption = table.createCaption();
caption.textContent = title;
container.appendChild(table);
};
const showResult = (title, containerId, rows, cols, dataArray) => {
let container = document.getElementById(containerId);
container.innerHTML = ''; // Clear previous content
let table = document.createElement('table');
for (let i = 0; i < rows; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < cols; j++) {
let td = document.createElement('td');
let span = document.createElement('span');
// Calculate the index in the dataArray based on current row and column
let index = i * cols + j;
if (index < dataArray.length) {
span.innerHTML = dataArray[index];
}
td.appendChild(span);
tr.appendChild(td);
}
table.appendChild(tr);
}
let caption = table.createCaption();
caption.textContent = title;
container.appendChild(table);
};
const showResult2D = (title, containerId, dataArray) => {
let container = document.getElementById(containerId);
container.innerHTML = ''; // Clear previous content
let table = document.createElement('table');
for (let i = 0; i < dataArray.length; i++) {
let tr = document.createElement('tr');
for (let j = 0; j < dataArray[i].length; j++) {
let td = document.createElement('td');
let span = document.createElement('span');
span.innerHTML = dataArray[i][j];
td.appendChild(span);
tr.appendChild(td);
}
table.appendChild(tr);
}
let caption = table.createCaption();
caption.textContent = title;
container.appendChild(table);
};
function performOperation(operation) {
let matrix1 = getMatrixData2D('matrix1');
let matrix2 = getMatrixData2D('matrix2');
console.log("1st Matrix",matrix1);
console.log("2nd Matrix", matrix2);
console.log("Operation", operation);
let result;
if (operation === 'add') {
result = addMatrices(matrix1, matrix2);
} else if (operation === 'subtract') {
result = subtractMatrices(matrix1, matrix2);
} else if (operation === 'multiply') {
result = multiplyMatrices(matrix1, matrix2);
}
if (result) {
showResult2D('The Result', 'matrix3', result);
} else {
alert('Matrix operation is not possible due to incompatible dimensions.');
}
}
const getMatrixData1D = function (matrixId) {
let matrixData = [];
let inputs = document.querySelectorAll(`#${matrixId} input`);
inputs.forEach(input => {
matrixData.push(parseInt(input.value, 10));
});
return matrixData;
};
const getMatrixData2D = function (matrixId) {
let matrixData = [];
let rows = parseInt(document.getElementById(matrixId + 'Rows').value, 10);
let cols = parseInt(document.getElementById(matrixId + 'Cols').value, 10);
let inputs = document.querySelectorAll(`#${matrixId} input`);
for (let i = 0; i < rows; i++) {
let rowData = [];
for (let j = 0; j < cols; j++) {
// Calculate index in the flat list of inputs
let index = i * cols + j;
if (index < inputs.length) {
rowData.push(parseInt(inputs[index].value, 10));
} else {
rowData.push(0); // Default value if input is missing
}
}
matrixData.push(rowData);
}
return matrixData;
};
// Add your matrix calculation functions here
// The functions must check the posibility of calculation too.
function addMatrices(matrix1, matrix2){
if (matrix1.length !== matrix2.length || matrix1[0].length !== matrix2[0].length) {
return null; // Return null if dimensions are incompatible
}
let result = [];
for (let i = 0; i < matrix1.length; i++) {
let row = [];
for (let j = 0; j < matrix1[0].length; j++) {
row.push(matrix1[i][j] + matrix2[i][j]);
}
result.push(row);
}
return result;
}
const subtractMatrices = function (matrix1, matrix2) {
if (matrix1.length !== matrix2.length || matrix1[0].length !== matrix2[0].length) {
return null; // Return null if dimensions are incompatible
}
let result = [];
for (let i = 0; i < matrix1.length; i++) {
let row = [];
for (let j = 0; j < matrix1[0].length; j++) {
row.push(matrix1[i][j] - matrix2[i][j]);
}
result.push(row);
}
return result;
};
const multiplyMatrices = (matrix1, matrix2) => {
if (matrix1[0].length !== matrix2.length) {
return null; // Return null if dimensions are incompatible
}
let result = [];
for (let i = 0; i < matrix1.length; i++) {
let row = [];
for (let j = 0; j < matrix2[0].length; j++) {
let sum = 0;
for (let k = 0; k < matrix1[0].length; k++) {
sum += matrix1[i][k] * matrix2[k][j];
}
row.push(sum);
}
result.push(row);
}
return result;
};