-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.js
More file actions
175 lines (158 loc) · 5.88 KB
/
matrix.js
File metadata and controls
175 lines (158 loc) · 5.88 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
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);
};
function showResult2D(title, containerId, matrix) {
const container = document.getElementById(containerId);
container.innerHTML = `<h3>${title}</h3>`;
const table = document.createElement('table');
matrix.forEach(row => {
const tableRow = document.createElement('tr');
row.forEach(cell => {
const tableCell = document.createElement('td');
tableCell.innerText = cell;
tableRow.appendChild(tableCell);
});
table.appendChild(tableRow);
});
container.appendChild(table);
};
function performOperation(operation) {
let matrix1 = getMatrixData2D('matrix1');
let matrix2 = getMatrixData2D('matrix2');
let result;
switch (operation) {
case 'add':
result = addMatrices(matrix1, matrix2);
break;
case 'subtract':
result = subtractMatrices(matrix1, matrix2);
break;
case 'multiply':
result = multiplyMatrices(matrix1, matrix2);
break;
default:
console.error('Unsupported operation');
return;
}
if (result) {
// Assuming your showResult function can handle displaying 2D arrays directly
showResult2D('The Result', 'result', result);
} else {
document.getElementById('result').innerHTML = "Operation cannot be performed with the given matrices.";
}
};
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;
};
function addMatrices(matrix1, matrix2) {
if (matrix1.length !== matrix2.length || matrix1[0].length !== matrix2[0].length) {
console.error("Matrices dimensions do not match for addition.");
return;
}
let result = [];
for (let i = 0; i < matrix1.length; i++) {
result[i] = [];
for (let j = 0; j < matrix1[i].length; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return result;
};
const subtractMatrices = function(matrix1, matrix2) {
if (matrix1.length !== matrix2.length || matrix1[0].length !== matrix2[0].length) {
console.error("Matrices dimensions do not match for subtraction.");
return;
}
let result = [];
for (let i = 0; i < matrix1.length; i++) {
result[i] = [];
for (let j = 0; j < matrix1[i].length; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return result;
};
const multiplyMatrices = (matrix1, matrix2) => {
if (matrix1[0].length !== matrix2.length) {
console.error("Matrices dimensions do not match for multiplication.");
return;
}
let result = new Array(matrix1.length).fill(0).map(() => new Array(matrix2[0].length).fill(0));
for (let i = 0; i < matrix1.length; i++) {
for (let j = 0; j < matrix2[0].length; j++) {
for (let k = 0; k < matrix1[0].length; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
};