Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Gauss-Jordan Elimination Calculator #1805

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Calculators/Gauss-Jordan Elimination Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gauss-Jordan Elimination Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Gauss-Jordan Elimination Calculator</h1>
<form id="matrixForm">
<label for="rows">Rows:</label>
<input type="number" id="rows" name="rows" min="1" required>
<label for="columns">Columns:</label>
<input type="number" id="columns" name="columns" min="1" required> <br/>
<button type="button" onclick="generateMatrix()">Generate Matrix</button>
<button type="button" onclick="resetMatrix()">Reset</button>
</form>
<div id="matrixContainer"></div>
<button id="solveButton" onclick="solveMatrix()" style="display:none;">Solve</button>
<div id="resultContainer"></div>
<script src="script.js"></script>
</body>
</html>
174 changes: 174 additions & 0 deletions Calculators/Gauss-Jordan Elimination Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
function generateMatrix() {
const rows = document.getElementById('rows').value;
const columns = document.getElementById('columns').value;
const matrixContainer = document.getElementById('matrixContainer');
matrixContainer.innerHTML = '';

for (let i = 0; i < rows; i++) {
const row = document.createElement('div');
for (let j = 0; j < columns; j++) {
const input = document.createElement('input');
input.type = 'number';
input.id = `matrix_${i}_${j}`;
input.required = true;
row.appendChild(input);
}
matrixContainer.appendChild(row);
}

document.getElementById('solveButton').style.display = 'block';
}

function resetMatrix() {
document.getElementById('matrixForm').reset();
document.getElementById('matrixContainer').innerHTML = '';
document.getElementById('resultContainer').innerHTML = '';
document.getElementById('solveButton').style.display = 'none';
}

function solveMatrix() {
const rows = document.getElementById('rows').value;
const columns = document.getElementById('columns').value;
let matrix = [];

for (let i = 0; i < rows; i++) {
matrix[i] = [];
for (let j = 0; j < columns; j++) {
matrix[i][j] = parseFloat(document.getElementById(`matrix_${i}_${j}`).value);
}
}

const result = gaussJordan(matrix);
displayResult(result);
}

function gaussJordan(matrix) {
const rows = matrix.length;
const columns = matrix[0].length;

for (let i = 0; i < rows; i++) {
let maxEl = Math.abs(matrix[i][i]);
let maxRow = i;
for (let k = i + 1; k < rows; k++) {
if (Math.abs(matrix[k][i]) > maxEl) {
maxEl = Math.abs(matrix[k][i]);
maxRow = k;
}
}

for (let k = i; k < columns; k++) {
let tmp = matrix[maxRow][k];
matrix[maxRow][k] = matrix[i][k];
matrix[i][k] = tmp;
}

for (let k = i + 1; k < rows; k++) {
let c = -matrix[k][i] / matrix[i][i];
for (let j = i; j < columns; j++) {
if (i === j) {
matrix[k][j] = 0;
} else {
matrix[k][j] += c * matrix[i][j];
}
}
}
}

for (let i = rows - 1; i >= 0; i--) {
for (let k = i - 1; k >= 0; k--) {
let c = -matrix[k][i] / matrix[i][i];
for (let j = 0; j < columns; j++) {
if (i === j) {
matrix[k][j] = 0;
} else {
matrix[k][j] += c * matrix[i][j];
}
}
}
}

for (let i = 0; i < rows; i++) {
let c = matrix[i][i];
for (let j = 0; j < columns; j++) {
matrix[i][j] = matrix[i][j] / c;
}
}

return matrix;
}

function displayResult(matrix) {
const resultContainer = document.getElementById('resultContainer');
resultContainer.innerHTML = '<h2>Result:</h2>';
matrix.forEach(row => {
const rowDiv = document.createElement('div');
row.forEach(value => {
const span = document.createElement('span');
span.textContent = `${value.toFixed(2)} `;
rowDiv.appendChild(span);
});
resultContainer.appendChild(rowDiv);
});

displaySolutions(matrix);
}

function displaySolutions(matrix) {
const resultContainer = document.getElementById('resultContainer');
const rows = matrix.length;
const columns = matrix[0].length;
let solutions = [];

for (let i = 0; i < rows; i++) {
let isZeroRow = true;
for (let j = 0; j < columns - 1; j++) {
if (matrix[i][j] !== 0) {
isZeroRow = false;
break;
}
}
if (isZeroRow && matrix[i][columns - 1] !== 0) {
resultContainer.innerHTML += '<p>No solution exists.</p>';
return;
}
}

for (let i = 0; i < columns - 1; i++) {
solutions[i] = 'Free variable';
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns - 1; j++) {
if (matrix[i][j] === 1) {
solutions[j] = matrix[i][columns - 1].toFixed(3);
break;
}
}
}

resultContainer.innerHTML += '<h2>Solutions:</h2>';
solutions.forEach((solution, index) => {
const solutionDiv = document.createElement('div');
solutionDiv.textContent = `x${index + 1} = ${solution}`;
resultContainer.appendChild(solutionDiv);
});
}

function fraction(num) {
let str = num.toString();
if (str.includes('.')) {
let len = str.split('.')[1].length;
let denominator = Math.pow(10, len);
let numerator = num * denominator;
let gcd = getGCD(numerator, denominator);
return `${numerator / gcd}/${denominator / gcd}`;
}
return num.toString();
}

function getGCD(a, b) {
if (!b) {
return a;
}
return getGCD(b, a % b);
}
31 changes: 31 additions & 0 deletions Calculators/Gauss-Jordan Elimination Calculator/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
body {
font-family: Arial, sans-serif;
margin: 20px;
text-align: center;
display: flex;
min-height: 100vh;
flex-direction: column;
align-items: center;
/* background-color: red; */
background-image: linear-gradient(to bottom right, rgb(247, 131, 151), rgb(255, 248, 197), rgb(0, 208, 255));
}

form, #matrixContainer, #resultContainer {
margin-top: 20px;
}

input[type="number"] {
width: 50px;
padding: 7px;
margin: 5px;
}

button {
margin-top: 10px;
padding: 5px 10px;
font-size: 16px;
border-radius: 20px;
border: 1px solid black;
color: rgb(255, 255, 255);
background-color: rgb(255, 129, 175);
}
68 changes: 57 additions & 11 deletions Calculators/Matrix-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,68 @@
<div id="matrixCalculator">
<h2>Matrix Calculator</h2>

<label for="matrixSize">Matrix Size:</label>
<select id="matrixSize" onchange="createMatrixInputs()">
<option value="2" class="size">2 x 2</option>
<option value="3" class="size">3 x 3</option>
<option value="4" class="size">4 x 4</option>
</select>
<div class="input-box">
<h2>MatrixA</h2>
<div>
<label for="matrixRows">Rows:</label>
<select id="matrixARows" onchange="createMatrixInputs()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<label for="matrixCols">Columns:</label>
<select id="matrixACols" onchange="createMatrixInputs()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>

<!-- <br> -->
<h2>
MatrixB
</h2>

<!-- <br> -->
<div>
<label for="matrixBRows">Rows:</label>
<select id="matrixBRows" onchange="createMatrixInputs()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<label for="matrixBCols">Columns:</label>
<select id="matrixBCols" onchange="createMatrixInputs()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>

<!-- <br> -->
<!-- <div>

</div> -->

</div>

<br>
<div id="matrixInputs">
<!-- Matrix A -->
<h3>Matrix A</h3>
<div id="matrixA"></div>

<!-- Matrix B -->
<h3>Matrix B</h3>
<div id="matrixB"></div>

Expand All @@ -45,12 +94,9 @@ <h3>Matrix B</h3>
<h3>Result</h3>
<div id="result"></div>
<button onclick="resetForm()">Reset</button>

</div>

<script src="script.js"></script>

</div>
</body>

</html>
Loading