<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Theory of Money</title>
<style>
body { font-family: 'Courier New', monospace; background: #0d1117; color: #c9d1d9; padding: 20px; }
.container { max-width: 95vw; margin: 0 auto; background: #161b22; border: 1px solid #30363d; padding: 25px; border-radius: 8px; }
.header-area { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #30363d; margin-bottom: 20px; padding-bottom: 10px; }
.tooltip { position: relative; display: inline-block; }
.tooltip .tooltiptext {
visibility: hidden; width: 280px; background-color: #30363d; color: #58a6ff;
text-align: center; border-radius: 4px; padding: 8px; position: absolute;
z-index: 10; bottom: 125%; left: 50%; margin-left: -140px; opacity: 0;
transition: opacity 0.2s; border: 1px solid #58a6ff; font-size: 0.85em; pointer-events: none;
}
.tooltip:hover .tooltiptext { visibility: visible; opacity: 1; }
.doc-link { color: #58a6ff; text-decoration: none; font-size: 0.9em; border: 1px solid #30363d; padding: 5px 10px; border-radius: 4px; }
.doc-link:hover { background: #30363d; }
.matrix-wrapper { overflow-x: auto; background: #0d1117; border: 1px solid #30363d; margin-bottom: 20px; padding: 10px; }
.panel { border: 1px solid #30363d; padding: 10px; background: #0d1117; margin-bottom: 20px;}
table { border-collapse: collapse; font-size: 0.8em; min-width: 100%; }
td, th { border: 1px solid #30363d; padding: 6px; text-align: center; min-width: 80px; }
th { background: #1c2128; color: #8b949e; font-size: 0.75em; position: sticky; top: 0; z-index: 2; }
.sticky-col { position: sticky; left: 0; background: #161b22 !important; z-index: 3; border-right: 2px solid #58a6ff; }
input { background: #161b22; border: 1px solid #30363d; color: #58a6ff; width: 100px; text-align: center; padding: 4px; }
input:focus { border-color: #58a6ff; outline: none; }
#valN { width: 60px; }
button { background: #238636; color: white; border: none; padding: 12px 20px; cursor: pointer; font-weight: bold; margin-bottom: 10px; text-transform: uppercase; border-radius: 4px;}
button.secondary { background: #30363d; margin-left: 10px; }
button.accent { background: #8957e5; margin-left: 10px; }
.out-val { color: #f0883e; font-weight: bold; }
.warning { color: #f85149; font-weight: bold; padding: 10px; border: 1px solid #f85149; margin-bottom: 20px; display: none; }
h3 { color: #888; font-size: 0.8em; margin: 5px 0; border-bottom: 1px solid #30363d; padding-bottom: 5px;}
.section-head { text-transform: uppercase; }
.label-tag { font-size: 0.7em; color: #58a6ff; font-weight: bold; display: block; margin-bottom: 5px; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-bottom: 20px; }
.result-scroll-box { max-height: 500px; overflow-y: auto; border: 1px solid #30363d; margin-top: 10px; }
.check-box { background: #1c2128; border: 1px solid #58a6ff; padding: 15px; border-radius: 4px; margin-top: 20px; }
.check-row { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 0.9em; }
</style>
</head>
<body>
<div class="container">
<div class="header-area">
<div class="section-head" style="font-weight: bold; letter-spacing: 1px;">Theory of Money</div>
<div class="tooltip">
<a href="http://k3qu5kyz6zhz5w64m5ez22iqcrw7rci7dccmjuofmoilhpx3gqfip4ad.onion/?page_id=TheoryOfMoney" target="_blank" class="doc-link">Theory Reference & Documentation</a>
<span class="tooltiptext">by tor browser https://www.torproject.org</span>
</div>
</div>
<div id="stabilityWarning" class="warning">STABILITY ERROR: ρ(T) ≥ 1.</div>
<div id="c0Warning" class="warning">ENERGY ERROR: c₀ < 0 (G insufficient for production). FK = <span id="errFK"></span></div>
<div class="panel">
<div style="display:flex; gap: 20px; flex-wrap: wrap; align-items: flex-end;">
<div><label class="label-tag">number of factors (m):</label> <input type="number" id="valN" value="3" min="1" max="50" onchange="init()"></div>
<div><label class="label-tag">power of energy sector (J):</label> <input type="text" id="valJ" value="10000"></div>
<div><label class="label-tag">money volume (V):</label> <input type="text" id="valV" value="21000000"></div>
<div><label class="label-tag">energy loss rate (q):</label> <input type="text" id="valQ" value="0.005"></div>
<div><label class="label-tag">energy gain factor (G):</label> <input type="text" id="valG" value="5.0"></div>
<div style="flex-grow: 1; text-align: right;">
<button class="accent" onclick="randomizeData()">RANDOM DATA</button>
<button class="secondary" onclick="exportData()">EXPORT JSON</button>
<button class="secondary" onclick="document.getElementById('fileInput').click()">IMPORT JSON</button>
<input type="file" id="fileInput" style="display:none" accept=".json" onchange="importData(event)">
</div>
</div>
</div>
<div class="matrix-wrapper"><span class="label-tag">Vector Inputs (F, E, s or t)</span><div id="vectorInputs"></div></div>
<div class="matrix-wrapper"><span class="label-tag">amount of j-th factor for 1 unit of i-th factor production (T)</span><div id="divT"></div></div>
<button style="width: 100%;" onclick="calculate()">MARKET CLEARING CALCULATION</button>
<div id="results" style="display:none;">
<div class="summary-grid">
<div class="panel"><h3>Total Harvested Energy (GJ)</h3><div id="resGJ" class="out-val"></div></div>
<div class="panel"><h3>Economic Gain (1/c₀)</h3><div id="resInvC0" class="out-val"></div></div>
<div class="panel"><h3>Buffer Size (B)</h3><div id="resB" class="out-val"></div></div>
<div class="panel"><h3>ρ(T)</h3><div id="resRho" class="out-val"></div></div>
<div class="panel"><h3>FK</h3><div id="resFK" class="out-val"></div></div>
<div class="panel"><h3>Energy Cost (c₀)</h3><div id="resC0" class="out-val"></div></div>
<div class="panel"><h3>Fee Param (a)</h3><div id="resA" class="out-val"></div></div>
<div class="panel"><h3>Money Purchasing Power (H)</h3><div id="resH" class="out-val"></div></div>
<div class="panel"><h3>Total Data Size (Σ Dᵢ sᵢ) or Total Time-to-Sale (Σ Dᵢ tᵢ)</h3><div id="resTotalS" class="out-val"></div></div>
<div class="panel"><h3>Total Energy Cost (Σ Dᵢ Eᵢ c₀)</h3><div id="sumEnergyCost" class="out-val"></div></div>
<div class="panel"><h3>Total Buffer Fee (Σ Dᵢ sᵢ a)</h3><div id="sumEnergyFee" class="out-val"></div></div>
<div class="panel"><h3>Total Energy Sector Buying (Σ J Fᵢ Pᵢ)</h3><div id="sumSectorBuying" class="out-val"></div></div>
<div class="panel"><h3>Energy Cost (coin)</h3><div id="coinC0" class="out-val"></div></div>
<div class="panel"><h3>Total Energy Cost (coin)</h3><div id="coinTotalEnergyCost" class="out-val"></div></div>
<div class="panel"><h3>Total Buffer Fee (coin)</h3><div id="coinTotalEnergyFee" class="out-val"></div></div>
<div class="panel"><h3>Total Energy Sector Buying (coin)</h3><div id="coinTotalSectorBuying" class="out-val"></div></div>
</div>
<h3 class="section-head">4. Layout: C, P, K</h3>
<div class="matrix-wrapper result-scroll-box" id="resPart4"></div>
<h3 class="section-head">5. Notional and Flows of Factors</h3>
<div class="matrix-wrapper result-scroll-box" id="resPart5"></div>
<h3 class="section-head">6. FLOW OF ENERGY</h3>
<div class="matrix-wrapper result-scroll-box" id="resPart6"></div>
<h3 class="section-head">7. FLOW OF MONEY</h3>
<div class="matrix-wrapper result-scroll-box" id="resPart7"></div>
<h3 class="section-head">8. ENERGY CONSUMPTION BREAKDOWN</h3>
<div class="matrix-wrapper result-scroll-box" id="resPart8"></div>
<div class="check-box">
<h3 class="section-head">CONVERGENCE CHECK: Energy Balance</h3>
<div class="check-row"><span>Total Factor Fee Flow (Σ Dᵢ sᵢ a):</span><span id="checkFees" class="out-val">--</span></div>
<div class="check-row"><span>Maintenance Energy Drain (qB):</span><span id="checkDrain" class="out-val">--</span></div>
<div class="check-row" style="border-top: 1px solid #30363d; padding-top: 10px; margin-top: 5px;"><span>System Status:</span><span id="checkStatus" style="color: #3fb950; font-weight: bold;">CONVERGED (Steady State)</span></div>
</div>
</div>
</div>
<script>
const MathUtils = {
identity(n) { return Array.from({length:n},(_,i)=>Array.from({length:n},(_,j)=>i===j?1:0)); },
subtract(A,B) { return A.map((r,i)=>r.map((v,j)=>v-B[i][j])); },
invert(M) {
let n=M.length, inv=this.identity(n), tmp=M.map(r=>[...r]);
for(let i=0;i<n;i++){
let p=tmp[i][i]; if(Math.abs(p)<1e-15) return null;
for(let j=0;j<n;j++){ tmp[i][j]/=p; inv[i][j]/=p; }
for(let k=0;k<n;k++){ if(k!==i){ let f=tmp[k][i]; for(let j=0;j<n;j++){ tmp[k][j]-=f*tmp[i][j]; inv[k][j]-=f*inv[i][j]; }}}
}
return inv;
},
dot(v1, v2) { return v1.reduce((sum, v, i) => sum + v * v2[i], 0); },
getSpectralRadius(matrix) {
const n = matrix.length; let v = Array(n).fill(1), radius = 0;
for (let iter = 0; iter < 100; iter++) {
let nextV = Array(n).fill(0);
for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) nextV[i] += matrix[i][j] * v[j];
let norm = Math.sqrt(nextV.reduce((s, x) => s + x * x, 0)) || 1;
radius = norm / (Math.sqrt(v.reduce((s, x) => s + x * x, 0)) || 1);
v = nextV.map(x => x / norm);
}
return radius;
}
};
function init() {
const m = Math.min(parseInt(document.getElementById('valN').value) || 1, 50);
let vHtml = "<table><tr><th>Vector</th>" + Array(m).fill(0).map((_,i)=>`<th>Factor ${i+1}</th>`).join('') + "</tr>";
vHtml += "<tr><td>F (Energy Prod)</td>" + Array(m).fill(0).map((_,i)=>`<td><input type="text" id="f_${i}" value="0.1"></td>`).join('') + "</tr>";
vHtml += "<tr><td>E (Energy Demand)</td>" + Array(m).fill(0).map((_,i)=>`<td><input type="text" id="e_${i}" value="100"></td>`).join('') + "</tr>";
vHtml += "<tr><td>s (Bytes) or t (Time-to-Sale)</td>" + Array(m).fill(0).map((_,i)=>`<td><input type="text" id="s_${i}" value="256"></td>`).join('') + "</tr></table>";
document.getElementById('vectorInputs').innerHTML = vHtml;
let tHtml = "<table><tr><th>i \\ j</th>" + Array(m).fill(0).map((_,j)=>`<th>j=${j+1}</th>`).join('') + "</tr>";
for(let i=0; i<m; i++) {
tHtml += `<tr><th>i=${i+1}</th>`;
for(let j=0; j<m; j++) tHtml += `<td><input type="text" id="t_${i}_${j}" value="${i===j?0.1:0.02}"></td>`;
tHtml += "</tr>";
}
document.getElementById('divT').innerHTML = tHtml + "</table>";
}
function randomizeData() {
const m = parseInt(document.getElementById('valN').value);
for(let i=0; i<m; i++) {
document.getElementById(`f_${i}`).value = (Math.random() * 0.2).toFixed(3);
document.getElementById(`e_${i}`).value = (Math.random() * 200 + 50).toFixed(1);
document.getElementById(`s_${i}`).value = Math.floor(Math.random() * 512 + 64);
for(let j=0; j<m; j++) {
const val = (i === j) ? (Math.random() * 0.2).toFixed(3) : (Math.random() * (0.5/m)).toFixed(3);
document.getElementById(`t_${i}_${j}`).value = val;
}
}
calculate();
}
function exportData() {
const m = parseInt(document.getElementById('valN').value);
const data = {
m, J: document.getElementById('valJ').value, V: document.getElementById('valV').value,
q: document.getElementById('valQ').value, G: document.getElementById('valG').value,
F: Array.from({length: m}, (_, i) => document.getElementById(`f_${i}`).value),
E: Array.from({length: m}, (_, i) => document.getElementById(`e_${i}`).value),
s: Array.from({length: m}, (_, i) => document.getElementById(`s_${i}`).value),
T: Array.from({length: m}, (_, i) => Array.from({length: m}, (_, j) => document.getElementById(`t_${i}_${j}`).value))
};
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const a = document.createElement('a'); a.href = URL.createObjectURL(blob);
a.download = `theory_of_money_m${m}.json`; a.click();
}
function importData(event) {
const reader = new FileReader();
reader.onload = (e) => {
const data = JSON.parse(e.target.result);
const m = data.m || data.n;
document.getElementById('valN').value = m;
init();
document.getElementById('valJ').value = data.J; document.getElementById('valV').value = data.V;
document.getElementById('valQ').value = data.q; document.getElementById('valG').value = data.G;
data.F.forEach((v, i) => { if(document.getElementById(`f_${i}`)) document.getElementById(`f_${i}`).value = v; });
data.E.forEach((v, i) => { if(document.getElementById(`e_${i}`)) document.getElementById(`e_${i}`).value = v; });
data.s.forEach((v, i) => { if(document.getElementById(`s_${i}`)) document.getElementById(`s_${i}`).value = v; });
data.T.forEach((row, i) => row.forEach((v, j) => { if(document.getElementById(`t_${i}_${j}`)) document.getElementById(`t_${i}_${j}`).value = v; }));
calculate();
};
reader.readAsText(event.target.files[0]);
}
function calculate() {
const m = parseInt(document.getElementById('valN').value);
const J = parseFloat(document.getElementById('valJ').value), V = parseFloat(document.getElementById('valV').value);
const q = parseFloat(document.getElementById('valQ').value), G = parseFloat(document.getElementById('valG').value);
const GJ = G * J;
let F_vec = [], E = [], S = [], T = [];
for(let i=0; i<m; i++) {
F_vec.push(parseFloat(document.getElementById(`f_${i}`).value) || 0);
E.push(parseFloat(document.getElementById(`e_${i}`).value) || 0);
S.push(parseFloat(document.getElementById(`s_${i}`).value) || 0);
T[i] = []; for(let j=0; j<m; j++) T[i][j] = parseFloat(document.getElementById(`t_${i}_${j}`).value) || 0;
}
const rho = MathUtils.getSpectralRadius(T);
document.getElementById('resRho').innerText = rho.toFixed(6);
if (rho >= 1) { document.getElementById('stabilityWarning').style.display = 'block'; document.getElementById('results').style.display = 'none'; return; }
document.getElementById('stabilityWarning').style.display = 'none';
const ImT_Inv = MathUtils.invert(MathUtils.subtract(MathUtils.identity(m), T));
const K = ImT_Inv.map(row => MathUtils.dot(row, E)), FK = MathUtils.dot(F_vec, K);
const denom = G - 1 - FK;
if (denom <= 0) { document.getElementById('errFK').innerText = FK.toFixed(6); document.getElementById('c0Warning').style.display = 'block'; document.getElementById('results').style.display = 'none'; return; }
document.getElementById('c0Warning').style.display = 'none';
const c0 = 1 / denom, invC0 = 1 / c0;
const D = Array(m).fill(0); for(let j=0; j<m; j++) for(let i=0; i<m; i++) D[j] += J * F_vec[i] * ImT_Inv[i][j];
const totalS = MathUtils.dot(D, S);
const a_fee = invC0 / (totalS / J), H = J / (c0 * q * V), B = J / (c0 * q), qB = q * B;
let tECost = 0, tEFee = 0, tSBuy = 0;
const P = ImT_Inv.map(row => MathUtils.dot(row, E.map((e, i) => e * c0 + S[i] * a_fee)));
for(let i=0; i<m; i++){ tECost += D[i] * E[i] * c0; tEFee += D[i] * S[i] * a_fee; tSBuy += J * F_vec[i] * P[i]; }
document.getElementById('resGJ').innerText = GJ.toFixed(2);
document.getElementById('resInvC0').innerText = invC0.toFixed(6);
document.getElementById('resB').innerText = B.toFixed(4);
document.getElementById('resFK').innerText = FK.toFixed(6);
document.getElementById('resC0').innerText = c0.toFixed(8);
document.getElementById('resA').innerText = a_fee.toExponential(6);
document.getElementById('resH').innerText = H.toExponential(6);
document.getElementById('resTotalS').innerText = totalS.toFixed(4);
document.getElementById('sumEnergyCost').innerText = tECost.toFixed(4);
document.getElementById('sumEnergyFee').innerText = tEFee.toFixed(4);
document.getElementById('sumSectorBuying').innerText = tSBuy.toFixed(4);
document.getElementById('coinC0').innerText = (c0 / H).toExponential(6);
document.getElementById('coinTotalEnergyCost').innerText = (tECost / H).toFixed(6);
document.getElementById('coinTotalEnergyFee').innerText = (tEFee / H).toFixed(6);
document.getElementById('coinTotalSectorBuying').innerText = (tSBuy / H).toFixed(6);
let t4 = "<table><tr><th>Factor</th><th>C (Energy Cost)</th><th>P (Price)</th><th>K (Requirement)</th></tr>";
for(let i=0; i<m; i++) t4 += `<tr><td>${i+1}</td><td>${(K[i]*c0).toFixed(4)}</td><td>${P[i].toFixed(4)}</td><td>${(K[i]).toFixed(4)}</td></tr>`;
document.getElementById('resPart4').innerHTML = t4 + "</table>";
let t5 = "<table><tr><th class='sticky-col'>i \\ j</th><th>D (Production)</th><th>Energy Demand</th><th>Fee to Buffer</th><th>Net Output</th><th colspan='"+m+"'>Factor Requirements (D_i * T_{i,j})</th></tr>";
for(let i=0; i<m; i++) {
let cons = 0; for(let j=0; j<m; j++) cons += D[j]*T[j][i];
t5 += `<tr><td class='sticky-col'>${i+1}</td><td>${D[i].toFixed(4)}</td><td>${(D[i]*E[i]).toFixed(4)}</td><td>${(D[i]*S[i]*a_fee).toFixed(4)}</td><td>${(D[i]-cons).toFixed(4)}</td>`;
for(let j=0; j<m; j++) t5 += `<td>${(D[i]*T[i][j]).toFixed(4)}</td>`;
t5 += "</tr>";
}
document.getElementById('resPart5').innerHTML = t5 + "</table>";
let t6 = "<table><tr><th class='sticky-col'>i \\ j</th><th>Purchased by Energy Sector</th><th>Gross Sale (D_i * P_i)</th><th>Energy Cost</th><th>Fee to Buffer</th><th colspan='"+m+"'>i-th factor buying from j-th factor</th></tr>";
for(let i=0; i<m; i++) {
t6 += `<tr><td class='sticky-col'>${i+1}</td><td>${(J*F_vec[i]*P[i]).toFixed(4)}</td><td>${(D[i]*P[i]).toFixed(4)}</td><td>${(D[i]*E[i]*c0).toFixed(4)}</td><td>${(D[i]*S[i]*a_fee).toFixed(4)}</td>`;
for(let j=0; j<m; j++) t6 += `<td>${(D[i]*T[i][j]*P[j]).toFixed(4)}</td>`;
t6 += "</tr>";
}
document.getElementById('resPart6').innerHTML = t6 + "</table>";
let t7 = "<table><tr><th class='sticky-col'>i \\ j</th><th>Purchased by Energy Sector</th><th>Gross Sale (D_i * P_i / H)</th><th>Buying Energy</th><th>Money Fee</th><th colspan='"+m+"'>Buying from Supply Chain</th></tr>";
for(let i=0; i<m; i++) {
t7 += `<tr><td class='sticky-col'>${i+1}</td><td>${(J*F_vec[i]*P[i]/H).toFixed(6)}</td><td>${(D[i]*P[i]/H).toFixed(6)}</td><td>${(D[i]*E[i]*c0/H).toFixed(6)}</td><td>${(D[i]*S[i]*a_fee/H).toFixed(6)}</td>`;
for(let j=0; j<m; j++) t7 += `<td>${(D[i]*T[i][j]*P[j]/H).toFixed(6)}</td>`;
t7 += "</tr>";
}
document.getElementById('resPart7').innerHTML = t7 + "</table>";
let t8 = "<table><tr><th>Sector/Component</th><th>Energy Value</th><th>% of Harvest (GJ)</th></tr>";
t8 += `<tr><td>Energy Sector Operating Power (J)</td><td>${J.toFixed(4)}</td><td>${(J/GJ*100).toFixed(4)}%</td></tr><tr><td>Buffer Maintenance Drain (qB)</td><td>${qB.toFixed(4)}</td><td>${(qB/GJ*100).toFixed(4)}%</td></tr>`;
let sysT = J + qB;
for(let i=0; i<m; i++) { let ec = D[i] * E[i]; sysT += ec; t8 += `<tr><td>Factor ${i+1} Production (D_${i+1} E_${i+1})</td><td>${ec.toFixed(4)}</td><td>${(ec/GJ*100).toFixed(4)}%</td></tr>`; }
t8 += `<tr style="border-top: 2px solid #58a6ff; font-weight: bold;"><td style="color: #58a6ff;">Total Energy</td><td style="color: #58a6ff;">${sysT.toFixed(4)}</td><td style="color: #58a6ff;">${(sysT/GJ*100).toFixed(2)}%</td></tr>`;
document.getElementById('resPart8').innerHTML = t8 + "</table>";
document.getElementById('checkFees').innerText = tEFee.toFixed(4);
document.getElementById('checkDrain').innerText = qB.toFixed(4);
document.getElementById('results').style.display = 'block';
}
window.onload = init;
</script>
</body>
</html>
|