-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1836 from saketh-05/master
Added EigenValue and EigenVector Calculator
- Loading branch information
Showing
7 changed files
with
245 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Eigenvalue and Eigenvector Calculator</title> | ||
<link rel="stylesheet" href="style.css" /> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h2>Largest Eigen Value and Eigen Vector Calculator</h2> | ||
<form id="matrix-form"> | ||
<label for="dimension">Enter matrix dimension (N x N):</label> | ||
<input type="number" id="dimension" min="2" max="10" value="2" /> | ||
<button type="button" onclick="generateMatrixInput()"> | ||
Generate Matrix Input | ||
</button> | ||
</form> | ||
<div id="matrix-input"></div> | ||
<button id="calculate" style="display: none" onclick="calculateEigen()"> | ||
Calculate | ||
</button> | ||
<div id="results"></div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
function generateMatrixInput() { | ||
const dimension = document.getElementById("dimension").value; | ||
const matrixInputDiv = document.getElementById("matrix-input"); | ||
matrixInputDiv.innerHTML = ""; // Clear previous inputs | ||
|
||
for (let i = 0; i < dimension; i++) { | ||
const rowDiv = document.createElement("div"); | ||
for (let j = 0; j < dimension; j++) { | ||
const input = document.createElement("input"); | ||
input.type = "number"; | ||
input.id = `matrix-${i}-${j}`; | ||
input.value = Math.floor(Math.random() * 10); // Default random values for fun | ||
rowDiv.appendChild(input); | ||
} | ||
matrixInputDiv.appendChild(rowDiv); | ||
} | ||
|
||
document.getElementById("calculate").style.display = "inline-block"; | ||
} | ||
|
||
function calculateEigen() { | ||
const dimension = document.getElementById("dimension").value; | ||
const matrix = []; | ||
|
||
// Read matrix values from inputs | ||
for (let i = 0; i < dimension; i++) { | ||
const row = []; | ||
for (let j = 0; j < dimension; j++) { | ||
const value = parseFloat( | ||
document.getElementById(`matrix-${i}-${j}`).value | ||
); | ||
row.push(value); | ||
} | ||
matrix.push(row); | ||
} | ||
|
||
// Perform power iteration to find largest eigenvalue and eigenvector | ||
let eigenvalue = 0; | ||
let eigenvector = new Array(dimension).fill(1); | ||
const tolerance = 1e-6; | ||
const maxIterations = 1000; | ||
|
||
for (let iter = 0; iter < maxIterations; iter++) { | ||
let newEigenvector = multiplyMatrixVector(matrix, eigenvector); | ||
let norm = Math.max(...newEigenvector.map(Math.abs)); | ||
newEigenvector = newEigenvector.map((val) => val / norm); | ||
|
||
if (Math.abs(norm - eigenvalue) < tolerance) { | ||
eigenvalue = norm; | ||
eigenvector = newEigenvector; | ||
break; | ||
} | ||
|
||
eigenvalue = norm; | ||
eigenvector = newEigenvector; | ||
} | ||
|
||
displayResults(eigenvalue, eigenvector); | ||
} | ||
|
||
function multiplyMatrixVector(matrix, vector) { | ||
const result = []; | ||
for (let i = 0; i < matrix.length; i++) { | ||
let sum = 0; | ||
for (let j = 0; j < vector.length; j++) { | ||
sum += matrix[i][j] * vector[j]; | ||
} | ||
result.push(sum); | ||
} | ||
return result; | ||
} | ||
|
||
function displayResults(eigenvalue, eigenvector) { | ||
const resultsDiv = document.getElementById("results"); | ||
resultsDiv.innerHTML = `<p><strong>Largest Eigenvalue:</strong> ${eigenvalue.toFixed(6)}</p>`; | ||
resultsDiv.innerHTML += `<p class="value"><strong>Corresponding Eigenvector:</strong></br>[${eigenvector | ||
.map((val) => val.toFixed(6)) | ||
.join(", ")}]</p>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
body { | ||
font-family: "Arial", sans-serif; | ||
background: rgb(29,29,124); | ||
background: linear-gradient(90deg, rgba(29,29,124,0.9641106442577031) 30%, rgba(0,23,28,1) 90%); | ||
display: flex; | ||
justify-content: center; | ||
position: relative; | ||
color: #473434; | ||
} | ||
|
||
.container { | ||
background-color: #ffffff; | ||
padding: 30px; | ||
border-radius: 15px; | ||
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); | ||
width: 500px; | ||
text-align: center; | ||
position: absolute; | ||
top: 85px; | ||
} | ||
|
||
h1 { | ||
margin-bottom: 20px; | ||
font-size: 24px; | ||
color: #0056b3; | ||
} | ||
|
||
form { | ||
margin-bottom: 20px; | ||
} | ||
|
||
label { | ||
font-size: 16px; | ||
color: #333; | ||
} | ||
|
||
input[type="number"] { | ||
width: 60px; | ||
padding: 5px; | ||
margin: 10px 5px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
font-size: 16px; | ||
} | ||
|
||
button { | ||
padding: 10px 15px; | ||
margin-top: 20px; | ||
font-size: 16px; | ||
color: #fff; | ||
background-color: #007bff; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
transition: background-color 0.3s ease; | ||
} | ||
#matrix-form { | ||
display: flex; | ||
align-items: center; | ||
flex-direction: column; | ||
justify-content: center; | ||
} | ||
button:hover { | ||
background-color: #0056b3; | ||
} | ||
|
||
#matrix-input { | ||
margin-top: 20px; | ||
} | ||
|
||
#results { | ||
margin-top: 30px; | ||
background-color: #f9f9f9; | ||
padding: 15px; | ||
border-radius: 10px; | ||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
#results p { | ||
font-size: 16px; | ||
margin: 10px 0; | ||
} | ||
.value{ | ||
display: flex; | ||
justify-content: space-between; | ||
flex-direction: column; | ||
} | ||
@media screen and (max-width: 530px) { | ||
.container { | ||
width: 70%; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters