Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
AltmannPeter authored Jul 28, 2023
1 parent e36c5dd commit 4abf128
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
let analysisData = [];

function calculateBitVectorAnalysis() {
const coinBiasInput = document.getElementById("coin-bias");
const coinBias = parseFloat(coinBiasInput.value.trim());

if (isNaN(coinBias) || coinBias < 0 || coinBias > 100) {
document.getElementById("result").textContent = "Please enter a valid probability of 1 (between 0 and 100%).";
} else {
// Check for duplicate entries
const exists = analysisData.some(data => data.bias === coinBias);
if (exists) {
document.getElementById("result").textContent = "This value already exists in the table.";
return;
}

const probabilityOfOne = coinBias / 100;
// Function to initialize the table with sample data
function initializeTableWithSampleData() {
const sampleValues = [0.5, 1, 1.5, 2, 2.5, 3, 5, 10];
sampleValues.forEach((bias) => {
const probabilityOfOne = bias / 100;
const probabilityOfZero = 1 - probabilityOfOne;
const shannonEntropy = -(probabilityOfOne * Math.log2(probabilityOfOne) + probabilityOfZero * Math.log2(probabilityOfZero));
const formattedEntropy = shannonEntropy.toLocaleString(undefined, { maximumFractionDigits: 4 });

// Calculate the number of bits required to represent the bit vector
const numBits = 1048576;
const numBits = 1 << 20;

// Generate the bit vector
const bitVector = generateBitVector(numBits, probabilityOfOne);

// Gzip compress the bit vector
// Gzip compress the bit vector with maximum compression (level 9)
const compressedData = pako.gzip(bitVector, { level: 9 });

// Get the size of the compressed data in bytes
const compressedSizeInBytes = compressedData.length;

analysisData.push({ bias: coinBias, entropy: shannonEntropy, compressedSize: compressedSizeInBytes });

// Sort the table and update
analysisData.sort((a, b) => b.entropy - a.entropy);
updateTable();

document.getElementById("result").textContent = ""; // Clear any previous messages
coinBiasInput.value = ""; // Clear the input field after adding to the table
}
analysisData.push({ bias: bias, entropy: shannonEntropy, compressedSize: compressedSizeInBytes });
});
}

// Function to generate a random bit vector
function generateBitVector(length, probabilityOfOne) {
const bitVector = new Uint8Array(length);
const threshold = probabilityOfOne * 256;
Expand All @@ -53,6 +36,13 @@ function generateBitVector(length, probabilityOfOne) {
return bitVector;
}

// Call the function to initialize the table with sample data
initializeTableWithSampleData();

function calculateBitVectorAnalysis() {
// Rest of the code remains the same...
}

function updateTable() {
const tableBody = document.getElementById("analysis-table");
tableBody.innerHTML = "";
Expand Down

0 comments on commit 4abf128

Please sign in to comment.