Compare commits
No commits in common. "pages" and "main" have entirely different histories.
33 changed files with 727 additions and 1114 deletions
7
.gitignore
vendored
7
.gitignore
vendored
|
@ -1,4 +1,9 @@
|
|||
# kdevelop
|
||||
.kde*
|
||||
*.kdev4
|
||||
bin/*
|
||||
# other
|
||||
*.zip
|
||||
*.wasm
|
||||
*.lst
|
||||
*.map
|
||||
*.o
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
|
||||
</META>
|
||||
<title>Lagrange
|
||||
</title>
|
||||
<style>body {background-color: rgb(192,255,255);} h2 {color: rgb(64,0,192);} h3 {color: rgb(192,0,0);} table {color: rgb(128,0,128);}
|
||||
</style>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({
|
||||
extensions: ["tex2jax.js","TeX/AMSmath.js"],
|
||||
jax: ["input/TeX","output/HTML-CSS"],
|
||||
tex2jax: {inlineMath: [['$','$']]},
|
||||
displayAlign: "left"});
|
||||
</script>
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>Automatický výpočet Hamiltonových rovnic z lagranžiánu v pythonu pomocí sympy - vyzkoušené příklady.
|
||||
</h1>
|
||||
<p>
|
||||
Tento jednoduchý skript řeší poměrně otravný problém, který
|
||||
je však snadno algoritmizovatelný. Jsou to jen opakované parciální derivace,
|
||||
jejichž výsledky jsou dosazovány do daných výrazů. Protože se přitom člověk
|
||||
snadno splete (hlavně ve znaménku), je lepší to nechat na programu.
|
||||
Stačí nadefinovat potřebné symboly pro konstanty a časově závislé proměnné
|
||||
(zobecněné souřadnice), pomocí nich pak vyjádřit lagranžián a o zbytek se postará python.
|
||||
|
||||
</p>
|
||||
<div><hr>
|
||||
<h2>Řešený problém : Railgun
|
||||
</h2>
|
||||
<h3>1.1. Zadaný lagranžián :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[L = \frac{m \left(\dot{x}\right)^{2}}{2} + \frac{\mu \left(\dot{Q}\right)^{2}}{2} x - \frac{Q^{2}}{2 C}\]
|
||||
</p>
|
||||
<h3>1.2. Lagrangeovy rovnice :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[\left [ m \ddot{x} - \frac{\mu \left(\dot{Q}\right)^{2}}{2} = 0, \quad \mu x \ddot{Q} + \mu \dot{Q} \dot{x} + \frac{Q}{C} = 0\right ]\]
|
||||
</p>
|
||||
<h3>1.3. Hamiltonova funkce :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[H = \frac{p^{2}_{Q}}{2 \mu x} + \frac{p^{2}_{x}}{2 m} + \frac{Q^{2}}{2 C}\]
|
||||
</p>
|
||||
<h3>1.4. Hamiltonovy kanonické rovnice :
|
||||
</h3>
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td class="formulaDsp">\[\dot{x}=\frac{p_{x}}{m}\qquad\]</td>
|
||||
<td class="formulaDsp">\[\dot{p}_{x}=\frac{p^{2}_{Q}}{2 \mu x^{2}}\qquad\]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="formulaDsp">\[\dot{Q}=\frac{p_{Q}}{\mu x}\qquad\]</td>
|
||||
<td class="formulaDsp">\[\dot{p}_{Q}=- \frac{Q}{C}\qquad\]</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div><hr>
|
||||
<h2>Řešený problém : Pohyb planety
|
||||
</h2>
|
||||
<h3>2.1. Zadaný lagranžián :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[L = \frac{G M}{r} m + \frac{m}{2} \left(r^{2} \left(\dot{\phi}\right)^{2} + \left(\dot{r}\right)^{2}\right)\]
|
||||
</p>
|
||||
<h3>2.2. Lagrangeovy rovnice :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[\left [ \frac{G M}{r^{2}} - r \left(\dot{\phi}\right)^{2} + \ddot{r} = 0, \quad r^{2} \ddot{\phi} + 2 r \dot{\phi} \dot{r} = 0\right ]\]
|
||||
</p>
|
||||
<h3>2.3. Hamiltonova funkce :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[H = - \frac{G M}{r} m + \frac{p^{2}_{\phi}}{2 m r^{2}} + \frac{p^{2}_{r}}{2 m}\]
|
||||
</p>
|
||||
<h3>2.4. Hamiltonovy kanonické rovnice :
|
||||
</h3>
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td class="formulaDsp">\[\dot{r}=\frac{p_{r}}{m}\qquad\]</td>
|
||||
<td class="formulaDsp">\[\dot{p}_{r}=- \frac{G M}{r^{2}} m + \frac{p^{2}_{\phi}}{m r^{3}}\qquad\]</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="formulaDsp">\[\dot{\phi}=\frac{p_{\phi}}{m r^{2}}\qquad\]</td>
|
||||
<td class="formulaDsp">\[\dot{p}_{\phi}=0\qquad\]</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div><hr>
|
||||
<h2>Řešený problém : Harmonický oscilátor
|
||||
</h2>
|
||||
<h3>3.1. Zadaný lagranžián :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[L = - \frac{m \omega^{2}}{2} x^{2} + \frac{m \left(\dot{x}\right)^{2}}{2}\]
|
||||
</p>
|
||||
<h3>3.2. Lagrangeovy rovnice :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[\left [ \omega^{2} x + \ddot{x} = 0\right ]\]
|
||||
</p>
|
||||
<h3>3.3. Hamiltonova funkce :
|
||||
</h3>
|
||||
<p class="formulaDsp">\[H = \frac{m \omega^{2}}{2} x^{2} + \frac{p^{2}_{x}}{2 m}\]
|
||||
</p>
|
||||
<h3>3.4. Hamiltonovy kanonické rovnice :
|
||||
</h3>
|
||||
<table align="center">
|
||||
<tr>
|
||||
<td class="formulaDsp">\[\dot{x}=\frac{p_{x}}{m}\qquad\]</td>
|
||||
<td class="formulaDsp">\[\dot{p}_{x}=- m \omega^{2} x\qquad\]</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,62 +0,0 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>CALC</title>
|
||||
<style>
|
||||
tr, td { border-collapse: collapse; }
|
||||
table { width:100%; border-collapse: collapse; text-align: center; }
|
||||
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
|
||||
textarea.emscripten { font-family: monospace; font-size: 16px; width: 100%; overflow-x: scroll; white-space: pre; background: black; color: rgb(0,255,0);}
|
||||
.frame1 { width: 96%; margin: 0; padding: 10px; background-color: #FFFFC0; border: 10px solid #F0C0F0; }
|
||||
.canvas { width: 100%; height: 67vh; background-color: black; }
|
||||
.b { color:#800080; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/javascript" src="index.js"></script>
|
||||
<div class="frame1"><canvas id="canvas" class="canvas"></canvas></div>
|
||||
<div class="frame1"><input id="buttonTest" type="button" value="Submit"></div>
|
||||
<div class="frame1">
|
||||
<table><tr>
|
||||
<td width="40%"><textarea class="emscripten" id="input" rows="25" spellcheck="false"></textarea></td>
|
||||
<td><textarea readonly class="emscripten" id="stdout" rows="25" spellcheck="false"></textarea></td>
|
||||
</tr></table>
|
||||
</div>
|
||||
<script type="text/x-mathjax-config">MathJax.Hub.Config({
|
||||
extensions: ["tex2jax.js"],
|
||||
jax: ["input/TeX","output/HTML-CSS"],
|
||||
tex2jax: {inlineMath: [['$','$']]},
|
||||
displayAlign: "left"});
|
||||
</script>
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js"></script>
|
||||
<div class="frame1">
|
||||
<h2>Solver pro soustavu Hamiltonových rovnic.</h2>
|
||||
<p>Nebudu se tady zabývat tím, co to jsou Hamiltonovy rovnice, na to je teoretická mechanika.
|
||||
Pro tento účel je to soustava diferenciálních rovnic, převedených do tvaru
|
||||
$$ \dot q_1 = f_1 (q_1, p_1, ..., q_i, p_i) $$
|
||||
$$ \dot p_1 = g_1 (q_1, p_1, ..., q_i, p_i) $$
|
||||
$$ ... $$
|
||||
$$ \dot q_i = f_i (q_1, p_1, ..., q_i, p_i) $$
|
||||
$$ \dot p_i = g_i (q_1, p_1, ..., q_i, p_i) $$
|
||||
kde $ \dot q_i, \dot p_i $ jsou derivace zobecněných souřadnic a hybností podle času. Celkově
|
||||
je to tedy sudý počet rovnic 2*i. Tyto rovnice zapíšeme jednoduše ve správném pořadí a se správným
|
||||
pořadím proměnných ve tvaru, který používá javascript, stanovíme 2*i počátečních podmínek a stanovíme
|
||||
co se má vykreslit. Osy to nemá, každý graf jde od minima do maxima, je to jen pro představu, jak funkce
|
||||
vypadá. Objekt, vstupující do rovnic má tvar {t:t, w:[q1, q2 ... qn, p1, p2, ... pn]}, t značí čas.
|
||||
Default příklad je harmonický oscilátor, lze použít i
|
||||
<b class="b" onclick="getFile('nvcr.txt');">nelineární oscilátor</b> nebo pro příklad
|
||||
<b class="b" onclick="getFile('lorenz.txt');">Lorenzův atraktor</b> a nakonec
|
||||
<b class="b" onclick="getFile('railgun.txt');">railgun</b> s disipací energie.
|
||||
Stačí kliknout na zvýrazněný text
|
||||
a stisknout tlačítko Submit. Parametry simulace lze jednoduše měnit. Připadá mi to o hodně jednodušší
|
||||
popsat takto problém textem než vymýšlet celé IDE s mnoha parametry. Parametry funkce solve (krok, počet_kroků, počáteční_podmínky, seznam_funkcí),
|
||||
plot (výsledek_ze_solve, popis_os, barva). Objekt popis_os má tvar {x:[list], y:[list]}, přičemž list je buď 't' nebo 'w', číslo_proměnné.
|
||||
</p>
|
||||
<p>Vlastní solver používá metodu Runge-Kutta 4.řádu, je poměrně přesná a není to o moc složitější
|
||||
než prostý Euler. Pokud hledáte ty Hamiltonovy rovnice, pomůže tento <a href="lagrange.html" target="_blank">skript</a>,
|
||||
jeho výstup je pak zhruba <a href="equations.html" target="_blank">tento</a>. A to je vše.
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,126 +0,0 @@
|
|||
var Outs, Inps, Btnt, Canvas;
|
||||
// async/await z příkladu na webu
|
||||
window.onload = async function() {
|
||||
Outs = document.getElementById('stdout');
|
||||
Inps = document.getElementById('input');
|
||||
Btnt = document.getElementById('buttonTest');
|
||||
Canvas = document.getElementById('canvas');
|
||||
Outs.value = '';
|
||||
await getFile ('osc.txt');
|
||||
Btnt.onclick = () => {
|
||||
Outs.value = '';
|
||||
run ();
|
||||
};
|
||||
};
|
||||
function print (str) {
|
||||
Outs.value += str;
|
||||
}
|
||||
async function getFile (name) {
|
||||
console.log(name);
|
||||
const response = await fetch (name);
|
||||
if (!response.ok) return;
|
||||
const bytes = await response.arrayBuffer();
|
||||
const array = new Uint8Array(bytes);
|
||||
const decoder = new TextDecoder();
|
||||
Inps.value = decoder.decode(bytes);
|
||||
return false;
|
||||
}
|
||||
///////////////////////////////////////////////
|
||||
function run () {
|
||||
|
||||
let code = Inps.value;
|
||||
try {
|
||||
eval (code);
|
||||
} catch (err) {
|
||||
print('Error: ' + err.message + '\n');
|
||||
}
|
||||
}
|
||||
function solve (hh, steps, data, pf) {
|
||||
let p = data;
|
||||
|
||||
const N = pf.length;
|
||||
const hal = 0.5 * hh;
|
||||
const hhs = hh / 6.0;
|
||||
let min = {t:+1.0e100, w:[]};
|
||||
for (let k=0; k<N; k++) { min.w[k] = min.t; }
|
||||
let max = {t:-1.0e100, w:[]};
|
||||
for (let k=0; k<N; k++) { max.w[k] = max.t; }
|
||||
|
||||
let result = new Array(steps + 1);
|
||||
if (p.t < min.t) min.t = p.t;
|
||||
for (let k=0; k<N; k++) { if (p.w[k] < min.w[k]) min.w[k] = p.w[k]; }
|
||||
if (p.t > max.t) max.t = p.t;
|
||||
for (let k=0; k<N; k++) { if (p.w[k] > max.w[k]) max.w[k] = p.w[k]; }
|
||||
result [0] = new Object (structuredClone(p));
|
||||
for (let index=1; index<result.length; index++) {
|
||||
let k1 = [];
|
||||
for (let k=0; k<N; k++) { k1[k] = pf[k] (p); }
|
||||
let p2 = structuredClone(p); p2.t += hal;
|
||||
for (let k=0; k<N; k++) { p2.w[k] += hal * k1[k]; }
|
||||
let k2 = [];
|
||||
for (let k=0; k<N; k++) { k2[k] = pf[k](p2); }
|
||||
let p3 = structuredClone(p); p3.t += hal;
|
||||
for (let k=0; k<N; k++) { p3.w[k] += hal * k2[k]; }
|
||||
let k3 = [];
|
||||
for (let k=0; k<N; k++) { k3[k] = pf[k](p3); }
|
||||
let p4 = structuredClone(p); p4.t += hh;
|
||||
for (let k=0; k<N; k++) { p4.w[k] += hh * k3[k]; }
|
||||
let k4 = [];
|
||||
for (let k=0; k<N; k++) { k4[k] = pf[k](p4); }
|
||||
p.t += hh;
|
||||
for (let k=0; k<N; k++) { p.w[k] += hhs * (k1[k] + 2.0 * k2[k] + 2.0 * k3[k] + k4[k]); }
|
||||
|
||||
let np = structuredClone (p);
|
||||
if (np.t < min.t) min.t = np.t;
|
||||
for (let k=0; k<N; k++) { if (np.w[k] < min.w[k]) min.w[k] = np.w[k]; }
|
||||
if (np.t > max.t) max.t = np.t;
|
||||
for (let k=0; k<N; k++) { if (np.w[k] > max.w[k]) max.w[k] = np.w[k]; }
|
||||
result [index] = new Object (np);
|
||||
}
|
||||
initCanvas ()
|
||||
return {data:result, m:min, p:max};
|
||||
}
|
||||
function getValue (pt, si) {
|
||||
if (si.length > 1) { // w
|
||||
return pt[si[0]][si[1]];
|
||||
} else { // t
|
||||
return pt[si[0]];
|
||||
}
|
||||
}
|
||||
function initCanvas () {
|
||||
const width = canvas.clientWidth;
|
||||
const height = canvas.clientHeight;
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
}
|
||||
function plot (r, po, color) {
|
||||
print('plot:' + JSON.stringify(po) + '\n');
|
||||
const p0 = r.data[0];
|
||||
print('min: ' + JSON.stringify(r.m) + '\n');
|
||||
print('max: ' + JSON.stringify(r.p) + '\n');
|
||||
const xmin = getValue (r.m, po.x);
|
||||
const xmax = getValue (r.p, po.x);
|
||||
const ymin = getValue (r.m, po.y);
|
||||
const ymax = getValue (r.p, po.y);
|
||||
const xzom = canvas.width / (xmax - xmin);
|
||||
const yzom = canvas.height / (ymin - ymax);
|
||||
const xofs = xmin * canvas.width / (xmin - xmax);
|
||||
const yofs = ymax * canvas.height / (ymax - ymin);
|
||||
//console.log (xmin, xmax, ymin, ymax);
|
||||
const ctx = canvas.getContext("2d");
|
||||
ctx.beginPath();
|
||||
const x0 = xzom * getValue(p0, po.x) + xofs;
|
||||
const y0 = yzom * getValue(p0, po.y) + yofs;
|
||||
ctx.moveTo(x0, y0);
|
||||
for (let n=1; n<r.data.length; n++) {
|
||||
const p = r.data[n];
|
||||
const x = xzom * getValue(p, po.x) + xofs;
|
||||
const y = yzom * getValue(p, po.y) + yofs;
|
||||
ctx.lineTo(x, y);
|
||||
}
|
||||
ctx.lineWidth = 3;
|
||||
ctx.strokeStyle = color;
|
||||
ctx.stroke();
|
||||
}
|
|
@ -1,538 +0,0 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||
"http://www.w3.org/TR/html4/strict.dtd">
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title></title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
||||
<style type="text/css">
|
||||
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
|
||||
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
|
||||
pre { line-height: 125%; }
|
||||
body .hll { background-color: #ffffcc }
|
||||
body { background: #f8f8f8; }
|
||||
body .c { color: #408080; font-style: italic } /* Comment */
|
||||
body .err { border: 1px solid #FF0000 } /* Error */
|
||||
body .k { color: #008000; font-weight: bold } /* Keyword */
|
||||
body .o { color: #666666 } /* Operator */
|
||||
body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
|
||||
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
|
||||
body .cp { color: #BC7A00 } /* Comment.Preproc */
|
||||
body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
|
||||
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
|
||||
body .cs { color: #408080; font-style: italic } /* Comment.Special */
|
||||
body .gd { color: #A00000 } /* Generic.Deleted */
|
||||
body .ge { font-style: italic } /* Generic.Emph */
|
||||
body .gr { color: #FF0000 } /* Generic.Error */
|
||||
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
|
||||
body .gi { color: #00A000 } /* Generic.Inserted */
|
||||
body .go { color: #888888 } /* Generic.Output */
|
||||
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
|
||||
body .gs { font-weight: bold } /* Generic.Strong */
|
||||
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
|
||||
body .gt { color: #0044DD } /* Generic.Traceback */
|
||||
body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
|
||||
body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
|
||||
body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
|
||||
body .kp { color: #008000 } /* Keyword.Pseudo */
|
||||
body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
|
||||
body .kt { color: #B00040 } /* Keyword.Type */
|
||||
body .m { color: #666666 } /* Literal.Number */
|
||||
body .s { color: #BA2121 } /* Literal.String */
|
||||
body .na { color: #7D9029 } /* Name.Attribute */
|
||||
body .nb { color: #008000 } /* Name.Builtin */
|
||||
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
|
||||
body .no { color: #880000 } /* Name.Constant */
|
||||
body .nd { color: #AA22FF } /* Name.Decorator */
|
||||
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
|
||||
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
|
||||
body .nf { color: #0000FF } /* Name.Function */
|
||||
body .nl { color: #A0A000 } /* Name.Label */
|
||||
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
|
||||
body .nt { color: #008000; font-weight: bold } /* Name.Tag */
|
||||
body .nv { color: #19177C } /* Name.Variable */
|
||||
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
|
||||
body .w { color: #bbbbbb } /* Text.Whitespace */
|
||||
body .mb { color: #666666 } /* Literal.Number.Bin */
|
||||
body .mf { color: #666666 } /* Literal.Number.Float */
|
||||
body .mh { color: #666666 } /* Literal.Number.Hex */
|
||||
body .mi { color: #666666 } /* Literal.Number.Integer */
|
||||
body .mo { color: #666666 } /* Literal.Number.Oct */
|
||||
body .sb { color: #BA2121 } /* Literal.String.Backtick */
|
||||
body .sc { color: #BA2121 } /* Literal.String.Char */
|
||||
body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
|
||||
body .s2 { color: #BA2121 } /* Literal.String.Double */
|
||||
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
|
||||
body .sh { color: #BA2121 } /* Literal.String.Heredoc */
|
||||
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
|
||||
body .sx { color: #008000 } /* Literal.String.Other */
|
||||
body .sr { color: #BB6688 } /* Literal.String.Regex */
|
||||
body .s1 { color: #BA2121 } /* Literal.String.Single */
|
||||
body .ss { color: #19177C } /* Literal.String.Symbol */
|
||||
body .bp { color: #008000 } /* Name.Builtin.Pseudo */
|
||||
body .vc { color: #19177C } /* Name.Variable.Class */
|
||||
body .vg { color: #19177C } /* Name.Variable.Global */
|
||||
body .vi { color: #19177C } /* Name.Variable.Instance */
|
||||
body .il { color: #666666 } /* Literal.Number.Integer.Long */
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2></h2>
|
||||
|
||||
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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
|
||||
176
|
||||
177
|
||||
178
|
||||
179
|
||||
180
|
||||
181
|
||||
182
|
||||
183
|
||||
184
|
||||
185
|
||||
186
|
||||
187
|
||||
188
|
||||
189
|
||||
190
|
||||
191
|
||||
192
|
||||
193
|
||||
194
|
||||
195
|
||||
196
|
||||
197
|
||||
198
|
||||
199
|
||||
200
|
||||
201
|
||||
202
|
||||
203
|
||||
204
|
||||
205
|
||||
206
|
||||
207
|
||||
208
|
||||
209
|
||||
210
|
||||
211
|
||||
212
|
||||
213
|
||||
214
|
||||
215
|
||||
216
|
||||
217
|
||||
218
|
||||
219
|
||||
220
|
||||
221
|
||||
222
|
||||
223
|
||||
224
|
||||
225
|
||||
226
|
||||
227</pre></div></td><td class="code"><div class="highlight"><pre><span class="ch">#!/usr/bin/env python</span>
|
||||
<span class="c1"># -*- coding: utf-8 -*-</span>
|
||||
|
||||
<span class="n">description</span> <span class="o">=</span> <span class="s2">"""</span>
|
||||
<span class="s2">Tento jednoduchý skript řeší poměrně otravný problém, který</span>
|
||||
<span class="s2">je však snadno algoritmizovatelný. Jsou to jen opakované parciální derivace,</span>
|
||||
<span class="s2">jejichž výsledky jsou dosazovány do daných výrazů. Protože se přitom člověk</span>
|
||||
<span class="s2">snadno splete (hlavně ve znaménku), je lepší to nechat na programu.</span>
|
||||
<span class="s2">Stačí nadefinovat potřebné symboly pro konstanty a časově závislé proměnné</span>
|
||||
<span class="s2">(zobecněné souřadnice), pomocí nich pak vyjádřit lagranžián a o zbytek se postará python.</span>
|
||||
<span class="s2">"""</span>
|
||||
<span class="kn">from</span> <span class="nn">sympy.core</span> <span class="kn">import</span> <span class="n">mul</span><span class="p">,</span> <span class="n">add</span>
|
||||
<span class="kn">from</span> <span class="nn">sympy.physics.vector.printing</span> <span class="kn">import</span> <span class="n">vlatex</span>
|
||||
<span class="kn">from</span> <span class="nn">sympy.physics.mechanics</span> <span class="kn">import</span> <span class="n">dynamicsymbols</span>
|
||||
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Symbol</span><span class="p">,</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">Derivative</span><span class="p">,</span>
|
||||
<span class="n">Eq</span><span class="p">,</span> <span class="n">pprint</span><span class="p">,</span> <span class="n">solve</span><span class="p">,</span> <span class="n">latex</span><span class="p">,</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">expand</span><span class="p">)</span>
|
||||
|
||||
<span class="c1">################# HTML dekorace ###############################################</span>
|
||||
<span class="k">class</span> <span class="nc">Tag</span><span class="p">:</span>
|
||||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">n</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">v</span>
|
||||
<span class="k">def</span> <span class="nf">to_str</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="k">return</span> <span class="s1">' '</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s1">'=</span><span class="se">\"</span><span class="s1">'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="s1">'</span><span class="se">\"</span><span class="s1">'</span>
|
||||
|
||||
<span class="k">class</span> <span class="nc">Element</span><span class="p">:</span>
|
||||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">n</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">v</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">childs</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="mi">0</span>
|
||||
<span class="k">def</span> <span class="nf">addE</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
|
||||
<span class="k">def</span> <span class="nf">addT</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">tags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
|
||||
<span class="k">def</span> <span class="nf">addF</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
|
||||
<span class="n">t</span> <span class="o">=</span> <span class="n">Tag</span> <span class="p">(</span><span class="s1">'class'</span><span class="p">,</span><span class="s1">'formulaDsp'</span><span class="p">)</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
|
||||
<span class="n">n</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'p'</span><span class="p">,</span> <span class="s1">'</span><span class="se">\\</span><span class="s1">['</span> <span class="o">+</span> <span class="n">p</span> <span class="o">+</span> <span class="s1">'</span><span class="se">\\</span><span class="s1">]'</span><span class="p">)</span>
|
||||
<span class="n">n</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">to_str</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indent</span><span class="p">()</span>
|
||||
<span class="n">s</span><span class="o">+=</span> <span class="s1">'<'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
|
||||
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tags</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">n</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||
<span class="n">s</span><span class="o">+=</span> <span class="s1">'>'</span>
|
||||
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">!=</span> <span class="bp">None</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span>
|
||||
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">n</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||
<span class="n">s</span><span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indent</span><span class="p">()</span>
|
||||
<span class="n">s</span><span class="o">+=</span> <span class="s1">'</'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s1">'>'</span>
|
||||
<span class="k">return</span> <span class="n">s</span>
|
||||
<span class="k">def</span> <span class="nf">cal</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
|
||||
<span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">k</span>
|
||||
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="p">:</span> <span class="n">n</span><span class="o">.</span><span class="n">cal</span> <span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span>
|
||||
<span class="k">def</span> <span class="nf">indent</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||
<span class="n">s</span> <span class="o">=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">*</span> <span class="s1">' '</span>
|
||||
<span class="k">return</span> <span class="n">s</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">set_root</span> <span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">root</span><span class="p">):</span>
|
||||
<span class="n">styl</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'style'</span><span class="p">,</span> <span class="s1">'body {background-color: rgb(192,255,255);} h2 {color: rgb(64,0,192);} h3 {color: rgb(192,0,0);} table {color: rgb(128,0,128);}'</span><span class="p">)</span>
|
||||
<span class="n">scfg</span> <span class="o">=</span> <span class="s1">'MathJax.Hub.Config({</span><span class="se">\n</span><span class="s1"> extensions: [</span><span class="se">\"</span><span class="s1">tex2jax.js</span><span class="se">\"</span><span class="s1">,"TeX/AMSmath.js"],</span><span class="se">\n</span><span class="s1"> jax: [</span><span class="se">\"</span><span class="s1">input/TeX</span><span class="se">\"</span><span class="s1">,'</span>
|
||||
<span class="n">scfg</span><span class="o">+=</span> <span class="s1">'</span><span class="se">\"</span><span class="s1">output/HTML-CSS</span><span class="se">\"</span><span class="s1">],</span><span class="se">\n</span><span class="s1"> tex2jax: {inlineMath: [[</span><span class="se">\'</span><span class="s1">$</span><span class="se">\'</span><span class="s1">,</span><span class="se">\'</span><span class="s1">$</span><span class="se">\'</span><span class="s1">]]},</span><span class="se">\n</span><span class="s1"> displayAlign: </span><span class="se">\"</span><span class="s1">left</span><span class="se">\"</span><span class="s1">});'</span>
|
||||
<span class="n">head</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'head'</span><span class="p">)</span>
|
||||
<span class="n">meta</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'META'</span><span class="p">)</span>
|
||||
<span class="n">titl</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'Lagrange'</span><span class="p">)</span>
|
||||
<span class="n">s1</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'script'</span><span class="p">,</span> <span class="n">scfg</span><span class="p">)</span>
|
||||
<span class="n">s2</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'script'</span><span class="p">)</span>
|
||||
<span class="n">s1</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'type'</span><span class="p">,</span><span class="s1">'text/x-mathjax-config'</span><span class="p">))</span>
|
||||
<span class="n">s2</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'type'</span><span class="p">,</span><span class="s1">'text/javascript'</span><span class="p">))</span>
|
||||
<span class="n">s2</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'src'</span><span class="p">,</span><span class="s1">'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js'</span><span class="p">))</span>
|
||||
<span class="n">meta</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'HTTP-EQUIV'</span><span class="p">,</span><span class="s1">'CONTENT-TYPE'</span><span class="p">))</span>
|
||||
<span class="n">meta</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'CONTENT'</span><span class="p">,</span><span class="s1">'text/html; charset=utf-8'</span><span class="p">))</span>
|
||||
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>
|
||||
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">titl</span><span class="p">)</span>
|
||||
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">styl</span><span class="p">)</span>
|
||||
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">s1</span><span class="p">)</span>
|
||||
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span>
|
||||
<span class="n">e</span><span class="o">.</span><span class="n">addE</span> <span class="p">(</span><span class="n">head</span><span class="p">)</span>
|
||||
<span class="n">body</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'body'</span><span class="p">)</span>
|
||||
<span class="n">body</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
|
||||
<span class="n">e</span><span class="o">.</span><span class="n">addE</span> <span class="p">(</span><span class="n">body</span><span class="p">)</span>
|
||||
<span class="n">e</span><span class="o">.</span><span class="n">cal</span><span class="p">()</span>
|
||||
<span class="k">def</span> <span class="nf">html_head</span> <span class="p">():</span>
|
||||
<span class="n">s</span> <span class="o">=</span> <span class="s1">'<!DOCTYPE html PUBLIC </span><span class="se">\"</span><span class="s1">-//W3C//DTD XHTML 1.0 Transitional//EN</span><span class="se">\"</span><span class="s1"> </span><span class="se">\"</span><span class="s1">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</span><span class="se">\"</span><span class="s1">>'</span>
|
||||
<span class="k">return</span> <span class="n">s</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">CreateHTML</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">html</span><span class="p">):</span>
|
||||
<span class="n">s</span> <span class="o">=</span> <span class="n">html_head</span> <span class="p">()</span>
|
||||
<span class="n">r</span> <span class="o">=</span> <span class="n">Element</span> <span class="p">(</span><span class="s1">'html'</span><span class="p">)</span>
|
||||
<span class="n">set_root</span> <span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>
|
||||
<span class="n">s</span> <span class="o">+=</span> <span class="n">r</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||
<span class="nb">file</span> <span class="o">=</span> <span class="nb">open</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="s1">'w'</span><span class="p">)</span>
|
||||
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||||
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">output</span> <span class="p">(</span><span class="n">counter</span><span class="p">,</span> <span class="n">problem</span><span class="p">,</span> <span class="n">L</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">hce</span><span class="p">,</span> <span class="n">lce</span><span class="p">):</span>
|
||||
<span class="n">html</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'div'</span><span class="p">,</span> <span class="s1">'<hr>'</span><span class="p">)</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h2'</span><span class="p">,</span><span class="s1">'Řešený problém : {0:s}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">problem</span><span class="p">)))</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.1. Zadaný lagranžián :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="s1">'L = '</span> <span class="o">+</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">L</span><span class="p">)]);</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.2. Lagrangeovy rovnice :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="n">vlatex</span><span class="p">(</span><span class="n">lce</span><span class="p">)]);</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.3. Hamiltonova funkce :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="s1">'H = '</span> <span class="o">+</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">H</span><span class="p">)]);</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.4. Hamiltonovy kanonické rovnice :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||
<span class="n">table</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'table'</span><span class="p">)</span>
|
||||
<span class="n">table</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'align'</span><span class="p">,</span><span class="s1">'center'</span><span class="p">))</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">hce</span><span class="p">:</span>
|
||||
<span class="n">td</span> <span class="o">=</span> <span class="s1">''</span>
|
||||
<span class="k">for</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="n">td</span> <span class="o">+=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1"><td class="formulaDsp"></span><span class="se">\\</span><span class="s1">[{0:s}={1:s}\qquad</span><span class="se">\\</span><span class="s1">]</td>'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">vlatex</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">])))</span>
|
||||
<span class="n">tr</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'tr'</span><span class="p">,</span> <span class="n">td</span><span class="p">)</span>
|
||||
<span class="n">table</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">tr</span><span class="p">)</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">table</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">html</span>
|
||||
<span class="c1">#################### Vlastní výpočty ##########################################</span>
|
||||
<span class="n">t</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'t'</span><span class="p">)</span> <span class="c1"># Globální symbol pro čas</span>
|
||||
<span class="c1"># vykrácení konstantou uděláme ručně, ale nemusí to fungovat, v podstatě jde jen o zbytečné hmotnosti</span>
|
||||
<span class="k">def</span> <span class="nf">parse</span> <span class="p">(</span><span class="n">ex</span><span class="p">):</span>
|
||||
<span class="n">ex</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||
<span class="k">if</span> <span class="n">ex</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">add</span><span class="o">.</span><span class="n">Add</span><span class="p">:</span> <span class="k">return</span> <span class="n">ex</span>
|
||||
<span class="n">e1</span> <span class="o">=</span> <span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="k">if</span> <span class="n">e1</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">mul</span><span class="o">.</span><span class="n">Mul</span><span class="p">:</span> <span class="k">return</span> <span class="n">ex</span>
|
||||
<span class="n">p</span> <span class="o">=</span> <span class="n">e1</span><span class="o">.</span><span class="n">args</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||
<span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">:</span>
|
||||
<span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">mul</span><span class="o">.</span><span class="n">Mul</span><span class="p">:</span> <span class="k">break</span>
|
||||
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">:</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">b</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
|
||||
<span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># TODO: or plus power of expression</span>
|
||||
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span> <span class="p">(</span><span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
|
||||
<span class="n">z</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||
<span class="k">if</span> <span class="n">y</span> <span class="o">==</span> <span class="n">l</span><span class="p">:</span> <span class="n">z</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
|
||||
<span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">z</span><span class="p">:</span> <span class="n">ex</span> <span class="o">=</span> <span class="n">ex</span> <span class="o">/</span> <span class="n">e</span>
|
||||
<span class="n">ex</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||
<span class="n">ex</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">ex</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">lagrange</span> <span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="n">coord</span><span class="p">):</span>
|
||||
<span class="n">lce</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coord</span><span class="p">:</span>
|
||||
<span class="n">ex</span> <span class="o">=</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">-</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># výraz pro Lagrangeovu rovnici</span>
|
||||
<span class="n">ex</span> <span class="o">=</span> <span class="n">parse</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span> <span class="c1"># vykrátit případné konstanty (lze i ručně)</span>
|
||||
<span class="n">le</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="n">ex</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="c1"># a udělat z toho rovnici</span>
|
||||
<span class="n">lce</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">le</span><span class="p">)</span>
|
||||
<span class="n">pprint</span><span class="p">(</span><span class="n">lce</span><span class="p">)</span>
|
||||
<span class="k">return</span> <span class="n">lce</span>
|
||||
|
||||
<span class="k">def</span> <span class="nf">compute</span> <span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">Counter</span><span class="p">):</span>
|
||||
<span class="n">res</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
|
||||
<span class="n">coord__x</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||
<span class="n">coord__p</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||||
<span class="n">L</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
|
||||
<span class="n">pprint</span> <span class="p">(</span><span class="n">L</span><span class="p">)</span> <span class="c1"># Pro kontrolu</span>
|
||||
<span class="n">coord_dp</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__p</span><span class="p">:</span> <span class="n">coord_dp</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
|
||||
<span class="n">coord_dx</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__x</span><span class="p">:</span> <span class="n">coord_dx</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
|
||||
<span class="n">lce</span> <span class="o">=</span> <span class="n">lagrange</span> <span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="nb">zip</span><span class="p">(</span><span class="n">coord__x</span><span class="p">,</span> <span class="n">coord_dx</span><span class="p">))</span>
|
||||
<span class="n">E</span> <span class="o">=</span> <span class="o">-</span><span class="n">L</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord_dx</span><span class="p">:</span> <span class="n">E</span> <span class="o">+=</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">*</span> <span class="n">p</span>
|
||||
<span class="n">E</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">E</span><span class="p">)</span>
|
||||
<span class="c1">#pprint (E) # Energie</span>
|
||||
<span class="n">coord_ps</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord_dx</span><span class="p">:</span> <span class="n">coord_ps</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
|
||||
<span class="n">coord__h</span> <span class="o">=</span> <span class="nb">zip</span> <span class="p">(</span><span class="n">coord__p</span><span class="p">,</span> <span class="n">coord_ps</span><span class="p">)</span>
|
||||
<span class="n">eqs</span> <span class="o">=</span> <span class="p">[]</span> <span class="c1"># Legendreova duální transformace</span>
|
||||
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__h</span><span class="p">:</span> <span class="n">eqs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Eq</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
|
||||
<span class="n">sol</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">eqs</span><span class="p">):</span> <span class="n">sol</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">solve</span> <span class="p">([</span><span class="n">p</span><span class="p">],</span> <span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]]))</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sol</span><span class="p">):</span> <span class="n">E</span> <span class="o">=</span> <span class="n">E</span><span class="o">.</span><span class="n">subs</span> <span class="p">(</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">p</span><span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||
<span class="n">H</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">E</span><span class="p">)</span>
|
||||
<span class="n">H</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">H</span><span class="p">)</span> <span class="c1"># O něco čitelněji</span>
|
||||
<span class="n">pprint</span> <span class="p">(</span><span class="n">H</span><span class="p">)</span> <span class="c1"># Hamiltonova funkce</span>
|
||||
<span class="n">hce</span> <span class="o">=</span> <span class="p">[]</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coord__x</span><span class="p">):</span>
|
||||
<span class="n">eqx</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="o">+</span><span class="n">H</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">coord__p</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">-</span> <span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
|
||||
<span class="n">eqx</span> <span class="o">=</span> <span class="n">solve</span> <span class="p">([</span><span class="n">eqx</span><span class="p">],[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||
<span class="n">eqx</span> <span class="o">=</span> <span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">eqx</span><span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span>
|
||||
<span class="n">eqp</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="o">-</span><span class="n">H</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">coord__x</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">-</span> <span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
|
||||
<span class="n">eqp</span> <span class="o">=</span> <span class="n">solve</span> <span class="p">([</span><span class="n">eqp</span><span class="p">],[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||
<span class="n">eqp</span> <span class="o">=</span> <span class="p">[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">eqp</span><span class="p">[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span>
|
||||
<span class="n">hce</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">eqx</span><span class="p">,</span> <span class="n">eqp</span><span class="p">])</span>
|
||||
<span class="n">pprint</span> <span class="p">(</span><span class="n">hce</span><span class="p">)</span> <span class="c1"># Hamiltonovy kanonické rovnice</span>
|
||||
<span class="k">return</span> <span class="n">output</span> <span class="p">(</span><span class="n">Counter</span><span class="p">,</span> <span class="n">res</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">hce</span><span class="p">,</span> <span class="n">lce</span><span class="p">)</span>
|
||||
|
||||
<span class="c1">######################## Uživatelská část #####################################</span>
|
||||
<span class="k">def</span> <span class="nf">entry1</span> <span class="p">():</span>
|
||||
<span class="c1">##### Zadání #####</span>
|
||||
<span class="n">m</span><span class="p">,</span><span class="n">mu</span><span class="p">,</span><span class="n">C</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'m mu C'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||
<span class="n">x</span><span class="p">,</span> <span class="n">Q</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'x Q'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||
<span class="n">px</span><span class="p">,</span><span class="n">pQ</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_x p_Q'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||
<span class="c1"># Lagranžián</span>
|
||||
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">Q</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">C</span><span class="p">)</span>
|
||||
<span class="c1">#############################################################################</span>
|
||||
<span class="k">return</span> <span class="p">[[</span><span class="n">x</span><span class="p">,</span> <span class="n">Q</span><span class="p">],</span> <span class="p">[</span><span class="n">px</span><span class="p">,</span> <span class="n">pQ</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Railgun'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||
<span class="k">def</span> <span class="nf">entry2</span> <span class="p">():</span> <span class="c1"># Z učebnice - pohyb planety kolem Slunce, vychází</span>
|
||||
<span class="n">G</span><span class="p">,</span><span class="n">m</span><span class="p">,</span><span class="n">M</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'G m M'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||
<span class="c1">##### Zadání #####</span>
|
||||
<span class="n">r</span><span class="p">,</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'r phi'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||
<span class="n">pr</span><span class="p">,</span> <span class="n">pphi</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_r p_phi'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||
<span class="c1"># Lagranžián</span>
|
||||
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">/</span><span class="mi">2</span> <span class="o">*</span> <span class="p">((</span><span class="n">r</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">((</span><span class="n">phi</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)))</span> <span class="o">+</span> <span class="p">((</span><span class="n">G</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">M</span><span class="p">)</span> <span class="o">/</span> <span class="n">r</span><span class="p">)</span>
|
||||
<span class="c1">#############################################################################</span>
|
||||
<span class="k">return</span> <span class="p">[[</span><span class="n">r</span><span class="p">,</span> <span class="n">phi</span><span class="p">],</span> <span class="p">[</span><span class="n">pr</span><span class="p">,</span> <span class="n">pphi</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Pohyb planety'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||
<span class="k">def</span> <span class="nf">entry3</span> <span class="p">():</span> <span class="c1"># Z učebnice - harmonický oscilátor, vychází</span>
|
||||
<span class="c1">##### Zadání #####</span>
|
||||
<span class="n">m</span><span class="p">,</span><span class="n">omega</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'m omega'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||
<span class="n">x</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'x'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||
<span class="n">px</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_x'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||
<span class="c1"># Lagranžián</span>
|
||||
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">m</span> <span class="o">*</span> <span class="n">omega</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
|
||||
<span class="c1">#############################################################################</span>
|
||||
<span class="k">return</span> <span class="p">[[</span><span class="n">x</span><span class="p">],</span> <span class="p">[</span><span class="n">px</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Harmonický oscilátor'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||
<span class="c1">###############################################################################</span>
|
||||
|
||||
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
||||
<span class="n">computed</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">,</span> <span class="n">entry3</span><span class="p">]</span> <span class="c1"># Co vše se bude počítat</span>
|
||||
<span class="n">html</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'div'</span><span class="p">)</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h1'</span><span class="p">,</span><span class="s1">'Automatický výpočet Hamiltonových rovnic z lagranžiánu v pythonu pomocí sympy - vyzkoušené příklady.'</span><span class="p">))</span>
|
||||
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'p'</span><span class="p">,</span> <span class="n">description</span><span class="p">))</span>
|
||||
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">computed</span><span class="p">,</span> <span class="n">start</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> <span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">compute</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span>
|
||||
<span class="n">CreateHTML</span><span class="p">(</span><span class="s1">'equations.html'</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span> <span class="c1"># převod do lidsky čitelné formy (mathjax latex v html)</span>
|
||||
|
||||
</pre></div>
|
||||
</td></tr></table></body>
|
||||
</html>
|
|
@ -1,29 +0,0 @@
|
|||
let bc = {t:0.0, w:[0.0, 1.0, 10.0]}
|
||||
const omega = 27.0;
|
||||
const sigma = 10.0;
|
||||
const beta = 8.0 / 3.0;
|
||||
let pf = [
|
||||
(q) => {
|
||||
const x = q.w[0];
|
||||
const y = q.w[1];
|
||||
return sigma * (y - x);
|
||||
},
|
||||
(q) => {
|
||||
const x = q.w[0];
|
||||
const y = q.w[1];
|
||||
const z = q.w[2];
|
||||
return x * (omega - z) - y;
|
||||
},
|
||||
(q) => {
|
||||
const x = q.w[0];
|
||||
const y = q.w[1];
|
||||
const z = q.w[2];
|
||||
return x * y - beta * z;
|
||||
},
|
||||
];
|
||||
let r = solve (0.01, 2000, bc, pf);
|
||||
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||
plot (r, {x:['t'], y:['w', 2]}, '#0000ff');
|
||||
plot (r, {x:['w', 0], y:['w', 2]}, '#00ffff');
|
||||
delete r.data; /* ukliď po sobě */
|
|
@ -1,22 +0,0 @@
|
|||
let bc = {t:0.0, w:[0.1, 0.0]}
|
||||
const C = 1.0;
|
||||
const L = 1.0;
|
||||
const G = 0.2;
|
||||
const U0= 1.0;
|
||||
let pf = [
|
||||
(o) => {
|
||||
const u = o.w[0];
|
||||
const v = o.w[1];
|
||||
return -(v + G*u*(u*u - U0*U0)) / C;
|
||||
},
|
||||
(o) => {
|
||||
const u = o.w[0];
|
||||
return u / L;
|
||||
},
|
||||
];
|
||||
let r = solve (0.02, 4000, bc, pf);
|
||||
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||
plot (r, {x:['w', 0], y:['w', 1]}, '#00ffff');
|
||||
delete r.data; /* ukliď po sobě */
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
/* Počáteční podmínky */
|
||||
let bc = {t:0.0, w:[1.0, 0.0]}
|
||||
/* Konstanty */
|
||||
const f = 6.2831853
|
||||
const fq = f * f;
|
||||
/* Hamiltonovy rovnice */
|
||||
let pf = [
|
||||
(o) => {
|
||||
return o.w[1];
|
||||
},
|
||||
(o) => {
|
||||
return - fq * o.w[0];
|
||||
},
|
||||
];
|
||||
let r = solve (0.01, 100, bc, pf); /* řešení */
|
||||
/* Grafy */
|
||||
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||
plot (r, {x:['w', 0], y:['w', 1]}, '#00ffff');
|
||||
delete r.data; /* ukliď po sobě */
|
|
@ -1,33 +0,0 @@
|
|||
const m = 0.0007, mu = 1.2e-6, C = 0.020, U0 = 250.0, R = 0.01;
|
||||
/* {t:t , w:[ x, p_x, Q , p_Q]}*/
|
||||
let bc = {t:0.0, w:[0.2, 0.0, C * U0, 0.0]}
|
||||
let ls = [
|
||||
(z) => {
|
||||
const p_x = z.w[1];
|
||||
return p_x / m;
|
||||
},
|
||||
(z) => {
|
||||
const x = z.w[0];
|
||||
const p_Q = z.w[3];
|
||||
const d = p_Q / x;
|
||||
return 0.5 * d * d / mu;
|
||||
},
|
||||
(z) => {
|
||||
const x = z.w[0];
|
||||
const p_Q = z.w[3];
|
||||
return p_Q / (mu * x);
|
||||
},
|
||||
(z) => {
|
||||
const x = z.w[0];
|
||||
const Q = z.w[2];
|
||||
const p_Q = z.w[3];
|
||||
const Rp = - (R * p_Q) / (mu * x);
|
||||
return Rp - Q/C;
|
||||
},
|
||||
];
|
||||
let r = solve (5.0e-7, 2000, bc, ls); /* řešení */
|
||||
|
||||
plot (r, {x:['t'], y:['w', 1]}, '#00ff00');
|
||||
plot (r, {x:['t'], y:['w', 3]}, '#ff0000');
|
||||
delete r.data; /* ukliď po sobě */
|
||||
|
9
LICENSE
Normal file
9
LICENSE
Normal file
|
@ -0,0 +1,9 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Kizarm
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
33
Makefile
Normal file
33
Makefile
Normal file
|
@ -0,0 +1,33 @@
|
|||
CC = clang
|
||||
CX = clang++
|
||||
CODE ?= wasm
|
||||
|
||||
MOBJS = calc.yy.o calc.tab.o calculator.o
|
||||
|
||||
ifeq ($(CODE),wasm)
|
||||
include wasm.mk
|
||||
else
|
||||
include unix.mk
|
||||
endif
|
||||
|
||||
OBJS = $(MOBJS) $(MAIN)
|
||||
CFLAGS += -Wno-unused-function
|
||||
|
||||
all: $(PR)
|
||||
%.o: %.cpp
|
||||
$(CX) -std=c++14 -c $(CFLAGS) -fno-exceptions -fno-rtti $< -o $@
|
||||
%.o: %.c
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
calc.tab.o: calc.tab.c
|
||||
$(CX) -std=c++14 -c $(CFLAGS) -Wno-deprecated -Wno-writable-strings -fno-exceptions -fno-rtti $< -o $@
|
||||
$(PR): $(OBJS) $(WALIB)
|
||||
$(LD) $(LFLAGS) $(OBJS) -o $(PR) $(LDLIBS)
|
||||
calc.yy.cpp: calc.l calc.tab.h
|
||||
flex calc.l
|
||||
calc.tab.c calc.tab.h: calc.y
|
||||
bison -d calc.y
|
||||
clean:
|
||||
rm -f *.o *.yy.* *.tab.*
|
||||
distclean: clean
|
||||
rm -f $(PR)
|
||||
.PHONY: all clean distclean
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# Calculator
|
||||
|
||||
bare bone webassembly modul
|
|
@ -45,10 +45,6 @@
|
|||
MIT <a href="https://code.nolog.cz/Kizarm/Calculator" target="_blank">přikládám</a>. Pro kompilaci je použit jen
|
||||
clang a jím kompilovaná C-čková knihovna <a href="https://sourceware.org/newlib/" target="_blank">newlib</a>.
|
||||
</p>
|
||||
<p>Že jde něco takového napsat v čistém javascriptu, viz <a href="./Hamilton/hamilton.html">zde</a>.
|
||||
Není to dokonalé, ale funguje to. Python by na to byl asi lepší, ale tady není potřeba nic jiného
|
||||
než prohlížeč.
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
10
bin/test.txt
Normal file
10
bin/test.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
{vlnový balík}
|
||||
max=10
|
||||
x=-max,max,1000
|
||||
a=1/4
|
||||
z=10
|
||||
omega=2*pi
|
||||
phi=omega/4
|
||||
z * sin (omega*x + phi) * exp (-(x*a)^2)
|
||||
z * exp (-(x*a)^2) {kladná obálka}
|
||||
-z * exp (-(x*a)^2) {záporná obálka}
|
31
calc.l
Normal file
31
calc.l
Normal file
|
@ -0,0 +1,31 @@
|
|||
%{
|
||||
#include <stdlib.h>
|
||||
#include "calc.tab.h"
|
||||
#include "calculator.h"
|
||||
%}
|
||||
%option outfile="calc.yy.cpp" header-file="calc.yy.h"
|
||||
%option case-sensitive
|
||||
%option array
|
||||
|
||||
D [0-9]
|
||||
E [Ee][+-]?{D}+
|
||||
|
||||
%%
|
||||
pi { return MATHPI; }
|
||||
sin { return FCESIN; }
|
||||
cos { return FCECOS; }
|
||||
exp { return FCEEXP; }
|
||||
log { return FCELOG; }
|
||||
[a-zA-Z]+ { yylval.svalue = strdup (yytext); return IDENT; }
|
||||
{D}+ { yylval.svalue = strdup (yytext); return INTEGER; }
|
||||
{D}*\.{D}+({E})? { yylval.svalue = strdup (yytext); return DOUBLE; }
|
||||
{D}*\.{D}*({E})? { yylval.svalue = strdup (yytext); return DOUBLE; }
|
||||
\{.*\}
|
||||
[ \t]+ /* ignored */
|
||||
\n { return ENDLINE; }
|
||||
. { return yytext[0]; }
|
||||
%%
|
||||
void prevent_unused (void) {
|
||||
(void) yyunput;
|
||||
/* (void) input; */
|
||||
}
|
68
calc.y
Normal file
68
calc.y
Normal file
|
@ -0,0 +1,68 @@
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include "calculator.h"
|
||||
int yylex (void);
|
||||
int yyerror (char *);
|
||||
%}
|
||||
%language "C"
|
||||
%union {
|
||||
char * svalue;
|
||||
double dvalue;
|
||||
class Expression * evalue;
|
||||
}
|
||||
|
||||
%token ENDLINE
|
||||
%token DOUBLE
|
||||
%token INTEGER
|
||||
%token MATHPI
|
||||
%token FCESIN
|
||||
%token FCECOS
|
||||
%token FCEEXP
|
||||
%token FCELOG
|
||||
%token IDENT
|
||||
%token TEXT
|
||||
%left '+' '-'
|
||||
%left '*' '/'
|
||||
%left '^'
|
||||
|
||||
%type <svalue> ENDLINE DOUBLE INTEGER IDENT line lines
|
||||
%type <evalue> expr
|
||||
%type <dvalue> constnt
|
||||
%start input
|
||||
%%
|
||||
|
||||
input: /* empty */
|
||||
| lines
|
||||
;
|
||||
lines: line
|
||||
| lines line
|
||||
;
|
||||
line: ENDLINE
|
||||
| IDENT '=' expr ENDLINE { addVariable ($1, $3->eval()); free ($1); }
|
||||
| IDENT '=' expr ',' expr ',' expr ENDLINE { addRange ($1, $3->eval(), $5->eval(), $7->eval()); free ($1); }
|
||||
| expr ENDLINE { addExpression ($1); }
|
||||
| error ENDLINE { printf ("what:%s\n", $$); }
|
||||
;
|
||||
constnt: DOUBLE { $$ = str_to_double ($1); free ($1); }
|
||||
| INTEGER { $$ = str_to_long ($1); free ($1); }
|
||||
| MATHPI { $$ = 3.14159265359; }
|
||||
;
|
||||
expr: constnt { $$ = new Constant ($1); }
|
||||
| IDENT { $$ = new Variable ($1); free ($1); }
|
||||
| '-' expr { $$ = new Unary ($2, UNARY_MINUS); }
|
||||
| expr '+' expr { $$ = new Binary ($1, $3, BINARY_PLUS); }
|
||||
| expr '-' expr { $$ = new Binary ($1, $3, BINARY_MINUS);}
|
||||
| expr '*' expr { $$ = new Binary ($1, $3, BINARY_MULT); }
|
||||
| expr '/' expr { $$ = new Binary ($1, $3, BINARY_DIV); }
|
||||
| expr '^' expr { $$ = new Binary ($1, $3, BINARY_POW); }
|
||||
| '(' expr ')' { $$ = $2; }
|
||||
| FCESIN '(' expr ')' { $$ = new Unary ($3, UNARY_SIN); }
|
||||
| FCECOS '(' expr ')' { $$ = new Unary ($3, UNARY_COS); }
|
||||
| FCEEXP '(' expr ')' { $$ = new Unary ($3, UNARY_EXP); }
|
||||
| FCELOG '(' expr ')' { $$ = new Unary ($3, UNARY_LOG); }
|
||||
;
|
||||
%%
|
||||
int yyerror(char * err) {
|
||||
printf("Error:\"%s\"\n", err);
|
||||
return 0;
|
||||
}
|
86
calculator.cpp
Normal file
86
calculator.cpp
Normal file
|
@ -0,0 +1,86 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "calculator.h"
|
||||
|
||||
double str_to_double (const char * str) {
|
||||
return strtod (str, NULL);
|
||||
}
|
||||
long str_to_long (const char * str) {
|
||||
return strtol (str, NULL, 10);
|
||||
}
|
||||
|
||||
static double u_minus (const double x) { return -x; }
|
||||
const unary_function unary_function_table [UNARY_max] = {
|
||||
u_minus,
|
||||
sin, cos, exp, log,
|
||||
};
|
||||
static double b_plus (const double a, const double b) { return a + b; }
|
||||
static double b_minus (const double a, const double b) { return a - b; }
|
||||
static double b_multiply (const double a, const double b) { return a * b; }
|
||||
static double b_divide (const double a, const double b) { return b !=0.0 ? a / b : 1.0; }
|
||||
const binary_function binary_function_table [BINARY_max] = {
|
||||
b_plus, b_minus, b_multiply, b_divide, pow,
|
||||
};
|
||||
static CommonData * main_data = nullptr;
|
||||
void addVariable (const char * str, const double x) {
|
||||
printf ("\"%s\"\t= %g\n", str, x);
|
||||
if (main_data->list == nullptr) {
|
||||
main_data->list = new VariableList (str, x);
|
||||
return;
|
||||
}
|
||||
main_data->list->add(str, x);
|
||||
}
|
||||
void addRange (const char * str, const double x, const double e, const double s) {
|
||||
printf ("\"%s\"\t= [%g:%g],%g\n", str, x, e, s);
|
||||
if (main_data->list == nullptr) {
|
||||
main_data->list = new VariableList (str, x, e, s);
|
||||
return;
|
||||
}
|
||||
main_data->list->add(str, x, e, s);
|
||||
}
|
||||
static Range zero = {0.0, 0.0, 0.0};
|
||||
Variable::Variable(const char * name) {
|
||||
data = main_data->list->find (name);
|
||||
if (!data) {
|
||||
printf("variable \"%s\" not assigned, zero used\n", name);
|
||||
data = & zero;
|
||||
}
|
||||
}
|
||||
static bool first = false;
|
||||
extern void emitData (double * x, double * y, const int len, const bool first);
|
||||
void addExpression (Expression * e) {
|
||||
if (main_data->root) delete main_data->root;
|
||||
main_data->root = e;
|
||||
|
||||
VariableList * vl = main_data->list->find_ranged();
|
||||
if (!vl) {
|
||||
printf("f() = %g\n", e->eval());
|
||||
return;
|
||||
}
|
||||
Range * r = vl->get();
|
||||
const double x0 = r->value, x1 = r->end, s = fabs (r->step);
|
||||
int n = (x1 - x0) / s;
|
||||
if (n <= 0) return;
|
||||
double * vx = new double [n + 1];
|
||||
double * vy = new double [n + 1];
|
||||
for (int i=0; i<n+1; i++) {
|
||||
const double x = x0 + (double) i * s;
|
||||
r->value = x;
|
||||
const double y = e->eval();
|
||||
vx[i] = x; vy[i] = y;
|
||||
}
|
||||
r->value = x0;
|
||||
// printf("add expression first=%d, n=%d\n", (int) first, n);
|
||||
emitData (vx, vy, n+1, first);
|
||||
if (first) first = false;
|
||||
delete [] vx; delete [] vy;
|
||||
}
|
||||
void initData () {
|
||||
first = true;
|
||||
main_data = new CommonData();
|
||||
}
|
||||
void finiData () {
|
||||
delete main_data;
|
||||
}
|
||||
|
121
calculator.h
Normal file
121
calculator.h
Normal file
|
@ -0,0 +1,121 @@
|
|||
/* inclusion guard */
|
||||
#ifndef __CALCULATOR_H__
|
||||
#define __CALCULATOR_H__
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
extern double str_to_double (const char * str);
|
||||
extern long str_to_long (const char * str);
|
||||
extern void addVariable (const char * str, const double x);
|
||||
extern void addRange (const char * str, const double x, const double e, const double s);
|
||||
|
||||
extern void initData ();
|
||||
extern void finiData ();
|
||||
|
||||
class Expression {
|
||||
public:
|
||||
virtual double eval () = 0;
|
||||
virtual ~Expression () {}
|
||||
};
|
||||
class Constant : public Expression {
|
||||
double data;
|
||||
public:
|
||||
explicit Constant (const double x) : data (x) {}
|
||||
explicit Constant (const long x) : data (x) {}
|
||||
double eval () override { /*printf("c=%g", data);*/ return data; }
|
||||
virtual ~Constant() {}
|
||||
};
|
||||
extern void addExpression (Expression * e);
|
||||
enum UNARY_FNCS {
|
||||
UNARY_MINUS = 0, UNARY_SIN, UNARY_COS, UNARY_EXP, UNARY_LOG, UNARY_max
|
||||
};
|
||||
enum BINARY_FNCS {
|
||||
BINARY_PLUS = 0, BINARY_MINUS, BINARY_MULT, BINARY_DIV, BINARY_POW, BINARY_max
|
||||
};
|
||||
typedef double (*unary_function) (const double);
|
||||
typedef double (*binary_function) (const double, const double);
|
||||
extern const unary_function unary_function_table [UNARY_max];
|
||||
extern const binary_function binary_function_table [BINARY_max];
|
||||
class Unary : public Expression {
|
||||
Expression * m_expr;
|
||||
unary_function m_f;
|
||||
public:
|
||||
explicit Unary (Expression * e, const UNARY_FNCS nf) : m_expr(e), m_f (unary_function_table[nf]) {}
|
||||
double eval () override { /*printf("(*%p)(%p)", m_f, m_expr);*/ return (m_f (m_expr->eval())); }
|
||||
virtual ~Unary () { delete m_expr; }
|
||||
};
|
||||
class Binary : public Expression {
|
||||
Expression * m_l, * m_r;
|
||||
binary_function m_f;
|
||||
public:
|
||||
explicit Binary (Expression * l, Expression * r, const BINARY_FNCS nf) : m_l(l), m_r(r), m_f(binary_function_table[nf]) {}
|
||||
double eval () override { /*printf("{%p}[%p]{%p}", m_l, m_f, m_r);*/ return (m_f (m_l->eval(), m_r->eval())); }
|
||||
virtual ~Binary() { delete m_l; delete m_r; }
|
||||
};
|
||||
struct Range {
|
||||
double value, end, step;
|
||||
};
|
||||
class VariableList {
|
||||
VariableList * next;
|
||||
char * name;
|
||||
Range data;
|
||||
public:
|
||||
explicit VariableList (const char * _name, const double _value) : next(nullptr) {
|
||||
name = strdup (_name);
|
||||
data.value = _value;
|
||||
data.end = _value;
|
||||
data.step = 0.0;
|
||||
}
|
||||
explicit VariableList (const char * _name, const double _value, const double _end, const double steps) : next(nullptr) {
|
||||
name = strdup (_name);
|
||||
if (_value > _end) {
|
||||
data.value = _end;
|
||||
data.end = _value;
|
||||
} else {
|
||||
data.value = _value;
|
||||
data.end = _end;
|
||||
}
|
||||
if (steps != 0.0) data.step = (data.end - data.value) / steps;
|
||||
else data.step = 1.0;
|
||||
}
|
||||
~VariableList () {
|
||||
if (next) delete next;
|
||||
free (name);
|
||||
}
|
||||
void add (const char * _name, const double _value) {
|
||||
if (!next) next = new VariableList (_name, _value);
|
||||
else next->add (_name, _value);
|
||||
}
|
||||
void add (const char * _name, const double _value, const double _end, const double steps) {
|
||||
if (!next) next = new VariableList (_name, _value, _end, steps);
|
||||
else next->add (_name, _value, _end, steps);
|
||||
}
|
||||
Range * find (const char * _name) {
|
||||
if (!strcmp (name, _name)) return & data;
|
||||
if (next) return next->find (_name);
|
||||
return nullptr;
|
||||
}
|
||||
VariableList * find_ranged () {
|
||||
if (data.step) return this;
|
||||
if (next) return next->find_ranged();
|
||||
return nullptr;
|
||||
}
|
||||
Range * get () { return & data; }
|
||||
};
|
||||
class Variable : public Expression {
|
||||
Range * data;
|
||||
public:
|
||||
explicit Variable (const char * name);
|
||||
double eval() override { return data->value; }
|
||||
virtual ~Variable () {}
|
||||
};
|
||||
struct CommonData {
|
||||
VariableList * list;
|
||||
Expression * root;
|
||||
explicit CommonData() : list(nullptr), root(nullptr) {}
|
||||
~CommonData () {
|
||||
if (list) delete list;
|
||||
if (root) delete root;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* __CALCULATOR_H__ */
|
132
chata/index.html
132
chata/index.html
|
@ -1,132 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>MQTT</title>
|
||||
<style>
|
||||
body {background:rgb(255,255,196);}
|
||||
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
|
||||
textarea.emscripten { font-family: monospace; font-size: 18px; width: 100%; overflow-x: scroll;
|
||||
white-space: pre; background: black; color: rgb(0,255,0);}
|
||||
.button {
|
||||
background-color: #04AA6D; border: none; color: white; padding: 15px 32px; text-align: center;
|
||||
text-decoration: none; display: inline-block; font-size: 48px; width: 100%; }
|
||||
</style>
|
||||
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Dálkové ovládání chaty.</h1>
|
||||
<table>
|
||||
<tr>
|
||||
<td><div><label for="pass">Password : </label><input type="password" id="pass" name="password" minlength="8" required /></div>
|
||||
</td><td><button class="button" id="ConnButton" onClick="Connection();">Connect</button></td>
|
||||
</tr><tr>
|
||||
<td><button class="button" id="MainButton" onClick="chPower(Main);" disabled>Hlavní vypínač</button></td>
|
||||
<td><button class="button" id="RefrButton" onClick="chPower(Refr);" disabled>Lednice</button></td>
|
||||
</tr><tr>
|
||||
<td colspan=2><textarea class="emscripten" id="output" rows="10" spellcheck="false"></textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
<script>
|
||||
const onColor = '#00ff00';
|
||||
const ofColor = '#ff0000';
|
||||
const TopElem = 'chata/';
|
||||
const InpTops = TopElem + 'status/';
|
||||
const OutTops = TopElem + 'command/';
|
||||
const ConnBtn = document.getElementById('ConnButton');
|
||||
const TextOut = document.getElementById('output');
|
||||
const url = 'wss://8d84362aa39a41c38a465a2edd8bcae4.s1.eu.hivemq.cloud:8884/mqtt'
|
||||
// Create an MQTT client instance
|
||||
const options = {
|
||||
// Clean session
|
||||
clean: true,
|
||||
connectTimeout: 4000,
|
||||
// Authentication
|
||||
clientId: 'js-' + Date.now().toString(16) + '-client',
|
||||
username: 'Kizarm',
|
||||
password: '********',
|
||||
}
|
||||
var Connected = false;
|
||||
var Main = {status:false, btn:document.getElementById('MainButton'), tstr:'mainpw'};
|
||||
var Refr = {status:false, btn:document.getElementById('RefrButton'), tstr:'refrig'};
|
||||
var client;
|
||||
function ManagePwd () {
|
||||
const pass = localStorage.getItem('mqtt_password');
|
||||
if (pass) document.getElementById('pass').value = pass;
|
||||
};
|
||||
function SetPwd () {
|
||||
const pass = localStorage.getItem('mqtt_password');
|
||||
if (!pass || pass.length === 0) {
|
||||
localStorage.setItem('mqtt_password', document.getElementById('pass').value);
|
||||
}
|
||||
}
|
||||
|
||||
ManagePwd ();
|
||||
function Connection() {
|
||||
if (!mqtt) {
|
||||
TextOut.value = 'MQTT not supported !!!\n'; return;
|
||||
}
|
||||
options.password = document.getElementById('pass').value;
|
||||
// console.log (options);
|
||||
if (Connected) {
|
||||
Connected = false;
|
||||
client.end()
|
||||
ConnBtn.innerHTML = 'Connect';
|
||||
Main.btn.disabled = true;
|
||||
Refr.btn.disabled = true;
|
||||
TextOut.value = "Disconnected\n";
|
||||
} else {
|
||||
client = mqtt.connect(url, options);
|
||||
client.on('connect', function () {
|
||||
Connected = true;
|
||||
TextOut.value += 'Connected to ' + url + '\n';
|
||||
ConnBtn.innerHTML = 'Disconnect';
|
||||
Main.btn.disabled = false;
|
||||
Refr.btn.disabled = false;
|
||||
SetPwd ();
|
||||
// Subscribe to a topic
|
||||
client.subscribe(InpTops + '#', function (err) {
|
||||
if (!err) {
|
||||
// Publish a message to a topic
|
||||
client.publish(OutTops, '???');
|
||||
}
|
||||
})
|
||||
})
|
||||
function ChangeStatus (relay, tmsg) {
|
||||
if (tmsg === '0') {
|
||||
relay.status = false;
|
||||
relay.btn.style.background = ofColor;
|
||||
} else {
|
||||
relay.status = true;
|
||||
relay.btn.style.background = onColor;
|
||||
}
|
||||
}
|
||||
// Receive messages
|
||||
client.on('message', function (topic, message) {
|
||||
const tstr = topic.toString();
|
||||
const tmsg = message.toString();
|
||||
// message is Buffer
|
||||
TextOut.value += tstr + ' : ' + tmsg + '\n';
|
||||
if (tstr === InpTops + Main.tstr) {
|
||||
ChangeStatus (Main, tmsg);
|
||||
} else if (tstr === InpTops + Refr.tstr) {
|
||||
ChangeStatus (Refr, tmsg);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
function chPower (relay) {
|
||||
if (relay.status) {
|
||||
relay.status = false;
|
||||
client.publish(OutTops + relay.tstr, '0');
|
||||
TextOut.value += relay.tstr + ' off\n';
|
||||
} else {
|
||||
relay.status = true;
|
||||
client.publish(OutTops + relay.tstr, '1');
|
||||
TextOut.value += relay.tstr + ' on\n';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
46
heap.c
Normal file
46
heap.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include "heap.h"
|
||||
extern void IMPORT(memoryGrow) (const int block);
|
||||
extern char __heap_base;
|
||||
typedef __SIZE_TYPE__ size_t;
|
||||
|
||||
static const char * _HEAP_START = &__heap_base;
|
||||
char * _HEAP_MAX = &__heap_base;
|
||||
|
||||
void * sbrk (unsigned long size) {
|
||||
static const char * heap_ptr;
|
||||
const char * old_heap_ptr;
|
||||
static unsigned int init_sbrk = 0;
|
||||
/* heap_ptr is initialized to HEAP_START */
|
||||
if (init_sbrk == 0) {
|
||||
heap_ptr = _HEAP_START;
|
||||
init_sbrk = 1;
|
||||
}
|
||||
old_heap_ptr = heap_ptr;
|
||||
/* Tohle je jen zkusmo, uvidíme, zatím se zdá, že to chodí.
|
||||
* Těžko říct, co to udělá, když dojde paměť, ale pár MiB to zvládne.
|
||||
*/
|
||||
if ((heap_ptr + size) > _HEAP_MAX) {
|
||||
const int blocks = (((heap_ptr + size) - _HEAP_MAX) >> 16) + 1;
|
||||
memoryGrow (blocks);
|
||||
_HEAP_MAX += blocks << 16;
|
||||
}
|
||||
heap_ptr += size;
|
||||
return (void *)old_heap_ptr;
|
||||
}
|
||||
/* Následující je někde použito v parseru, ale nemusí to moc fungovat.
|
||||
*/
|
||||
int write(int fd, const void * b, size_t l) {
|
||||
if ((fd == 1) || (fd == 2)) printout(b, l);
|
||||
return l;
|
||||
}
|
||||
// formátování pro long double asi nebudu používat
|
||||
extern void exit (int x)__attribute__((noreturn));
|
||||
void _exit (int x) __attribute__ ((noreturn));
|
||||
void _exit (int x) { exit(x); }
|
||||
double __trunctfdf2(long double a) { return (double)(a); }
|
||||
int isatty () { return 1; }
|
||||
|
||||
void close (int fd) {}
|
||||
int read (int fd, void * b, size_t l) { return l; }
|
||||
size_t lseek(int fd, size_t offset, int whence) { return 0; }
|
||||
int fstat(int fd, void * statbuf) { return 0; }
|
15
heap.h
Normal file
15
heap.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef _HEAP_H
|
||||
#define _HEAP_H
|
||||
#define EXPORT(name) __attribute__((used, export_name(#name))) name
|
||||
// "imports" odpovídá importObject.imports v JS, default je to importObject.env
|
||||
#define IMPORT(name) __attribute__((import_module("imports"),import_name(#name))) name
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif //__cplusplus
|
||||
extern void IMPORT(printout) (const char * ptr, const int len); // external javascript function
|
||||
extern void * sbrk (unsigned long size);
|
||||
extern char * _HEAP_MAX;
|
||||
#ifdef __cplusplus
|
||||
};
|
||||
#endif //__cplusplus
|
||||
#endif // _HEAP_H
|
BIN
module.wasm
BIN
module.wasm
Binary file not shown.
23
newdel.cpp
Normal file
23
newdel.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
|
||||
void* operator new (unsigned long size) {
|
||||
return calloc(1,size);
|
||||
}
|
||||
void operator delete (void* p) {
|
||||
free (p);
|
||||
}
|
||||
|
||||
void* operator new[] (unsigned long size) {
|
||||
return calloc(1,size);
|
||||
}
|
||||
|
||||
void operator delete[] (void* p) {
|
||||
free (p);
|
||||
}
|
||||
|
||||
void operator delete (void* p, unsigned size) {
|
||||
(void) size;
|
||||
free (p);
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>rotace</title>
|
||||
<style>
|
||||
@keyframes rotate {
|
||||
from { transform: rotate(0deg); }
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
.rotimg {
|
||||
animation-name: rotate;
|
||||
animation-duration: 4.0s;
|
||||
animation-iteration-count: infinite;
|
||||
animation-timing-function: linear;
|
||||
}
|
||||
body { width: 96%; margin: 0; padding: 10px; background-color: #FFFFC0; border: 10px solid #F0C0F0; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p align="center"><img class="rotimg" src="spiral.png"></p>
|
||||
</body>
|
||||
</html>
|
Binary file not shown.
Before Width: | Height: | Size: 184 KiB |
3
symbols.txt
Normal file
3
symbols.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
printout
|
||||
memoryGrow
|
||||
drawPoints
|
13
test.txt
13
test.txt
|
@ -1,10 +1,3 @@
|
|||
{vlnový balík}
|
||||
max=10
|
||||
x=-max,max,1000
|
||||
a=1/4
|
||||
z=10
|
||||
omega=2*pi
|
||||
phi=omega/4
|
||||
z * sin (omega*x + phi) * exp (-(x*a)^2)
|
||||
z * exp (-(x*a)^2) {kladná obálka}
|
||||
-z * exp (-(x*a)^2) {záporná obálka}
|
||||
x=2,8,20
|
||||
y=1
|
||||
y*x^2 + 5*y*x + 1
|
||||
|
|
59
unix.cpp
Normal file
59
unix.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "calc.yy.h"
|
||||
#include "calculator.h"
|
||||
|
||||
extern void yyparse ();
|
||||
extern "C" int yywrap () {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char * from_file (const char * filename) {
|
||||
struct stat statbuf;
|
||||
int r = stat (filename, & statbuf);
|
||||
if (r) return nullptr;
|
||||
char * buffer = (char*) malloc (statbuf.st_size + 1);
|
||||
FILE * in = fopen (filename,"r");
|
||||
r = fread (buffer, 1, statbuf.st_size, in);
|
||||
if (r != statbuf.st_size) {
|
||||
free (buffer);
|
||||
fclose (in);
|
||||
return nullptr;
|
||||
}
|
||||
buffer [r] = '\0';
|
||||
fclose (in);
|
||||
return buffer;
|
||||
}
|
||||
void emitData (double * x, double * y, const int len) {
|
||||
for (int n=0; n<len; n++) {
|
||||
printf("f(%g) = %g\n", x[n], y[n]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
static const char * multipliers [] = {"f","p","n","μ","m","","k","M","G","T","P"};
|
||||
static void test () {
|
||||
const int ofs = 5 * 3;
|
||||
for (int n=-15; n<18; n++) {
|
||||
div_t dt = div (n + ofs, 3);
|
||||
printf("n=%d, q=%d, r=%d (%d)\t%g %s\n", n, dt.quot, dt.rem, dt.quot * 3 + dt.rem - ofs,
|
||||
pow(10.0, dt.rem), multipliers[dt.quot]);
|
||||
}
|
||||
}
|
||||
*/
|
||||
int main () {
|
||||
char * buffer = from_file("test.txt");
|
||||
if (!buffer) return 1;
|
||||
|
||||
initData();
|
||||
YY_BUFFER_STATE result = yy_scan_string(buffer);
|
||||
yyparse();
|
||||
yy_delete_buffer(result);
|
||||
free (buffer);
|
||||
finiData();
|
||||
|
||||
//test();
|
||||
return 0;
|
||||
}
|
||||
|
7
unix.mk
Normal file
7
unix.mk
Normal file
|
@ -0,0 +1,7 @@
|
|||
MAIN = unix.o
|
||||
LD = clang++
|
||||
PR = test
|
||||
CFLAGS = -Wall -Oz -g -I. -I./lib
|
||||
LFLAGS =
|
||||
LDLIBS =
|
||||
|
123
wasm.cpp
Normal file
123
wasm.cpp
Normal file
|
@ -0,0 +1,123 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "calculator.h"
|
||||
#include "heap.h"
|
||||
#include "wasm.h"
|
||||
extern "C" void EXPORT(init) (const int memlen);
|
||||
extern "C" int EXPORT(cAlloc) (int len);
|
||||
extern "C" void EXPORT(compute) (char * ptr, int len);
|
||||
extern "C" void EXPORT(resizeCanvas)(int x, int y);
|
||||
extern "C" void IMPORT(drawPoints) (float * x, float * y, int len, char * json, int jl);
|
||||
extern "C" void __wasm_call_ctors ();
|
||||
extern "C++" {
|
||||
typedef struct yy_buffer_state * YY_BUFFER_STATE;
|
||||
extern int yyparse();
|
||||
extern YY_BUFFER_STATE yy_scan_string(const char * str);
|
||||
extern void yy_delete_buffer(YY_BUFFER_STATE buffer);
|
||||
};
|
||||
extern "C" int yywrap () {
|
||||
return 1;
|
||||
}
|
||||
void init (const int memlen) {
|
||||
_HEAP_MAX = reinterpret_cast<char*>(memlen);
|
||||
__wasm_call_ctors();
|
||||
printf("Module initialized\n\n");
|
||||
}
|
||||
int cAlloc (int len) {
|
||||
return int (malloc(len + 1));
|
||||
}
|
||||
void compute (char * ptr, int len) {
|
||||
ptr [len] = '\0';
|
||||
|
||||
initData();
|
||||
YY_BUFFER_STATE result = yy_scan_string(ptr);
|
||||
yyparse();
|
||||
yy_delete_buffer(result);
|
||||
free (ptr);
|
||||
finiData();
|
||||
}
|
||||
static Canvas canvas;
|
||||
void resizeCanvas (int x, int y) {
|
||||
printf("resizeCanvas: x=%d, y=%d\n", x, y);
|
||||
canvas.width = x;
|
||||
canvas.height = y;
|
||||
}
|
||||
static int CreateJSON (char * json, const int json_max, const Canvas & canvas,
|
||||
Stepping & sx, Stepping & sy) {
|
||||
int n = 0;
|
||||
const uint32_t xColor = 0xffff00;
|
||||
const uint32_t yColor = 0x00ffff;
|
||||
const double wd = 4.0; // šířka čárky na osách v px
|
||||
const double pos0x = canvas.xt(0.0) > 0 and canvas.xt(0.0) < canvas.width ? canvas.xt(0.0) : 0.0;
|
||||
const double pos0y = canvas.yt(0.0) > 0 and canvas.yt(0.0) < canvas.height ? canvas.yt(0.0) : canvas.height;
|
||||
n += snprintf(json + n, json_max - n, "{ \"name\":\"axes\", ");
|
||||
n += snprintf(json + n, json_max - n, "\"x\":{ \"color\":\"#%06X\", \"w\":%d, ", xColor, 2);
|
||||
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], ", 0.0, pos0y);
|
||||
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", canvas.width, pos0y);
|
||||
n += snprintf(json + n, json_max - n, "\"y\": { \"color\":\"#%06X\", \"w\":%d, ", yColor, 2);
|
||||
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], ", pos0x, 0.0);
|
||||
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, \"xdots\": [", pos0x, canvas.height);
|
||||
for (double dx=sx.b; dx<sx.e; dx+=sx.s) {
|
||||
n += snprintf(json + n, json_max - n, "{ \"color\":\"#%06X\", \"w\":%d, \"lbl\":\"%s\", ", xColor, 2, sx.ing(dx));
|
||||
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], " , canvas.xt(dx), pos0y - wd);
|
||||
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", canvas.xt(dx), pos0y + wd);
|
||||
}
|
||||
json [n-2] = ' '; // přepiš poslední čárku, jinak json nefunguje
|
||||
n += snprintf(json + n, json_max - n, "], \"ydots\": [\n");
|
||||
for (double dy=sy.b; dy<sy.e; dy+=sy.s) {
|
||||
n += snprintf(json + n, json_max - n, "{ \"color\":\"#%06X\", \"w\":%d, \"lbl\":\"%s\", ", yColor, 2, sy.ing(dy));
|
||||
n += snprintf(json + n, json_max - n, "\"b\": [%g,%g], " , pos0x - wd, canvas.yt(dy));
|
||||
n += snprintf(json + n, json_max - n, "\"e\": [%g,%g] }, ", pos0x + wd, canvas.yt(dy));
|
||||
}
|
||||
json [n-2] = ' ';
|
||||
n += snprintf(json + n, json_max - n, "] }\n");
|
||||
// printf("%d\n", n);
|
||||
return n;
|
||||
}
|
||||
void emitData (double * x, double * y, const int len, const bool first) {
|
||||
int n = 0;
|
||||
const int json_max = 0x8000; // string bude poměrně dlouhý
|
||||
char * json = new char [json_max];
|
||||
if (first) {
|
||||
double ymin = 1.0e100, ymax = -1.0e100;
|
||||
for (int n=0; n<len; n++) {
|
||||
if (y[n] < ymin) ymin = y[n];
|
||||
if (y[n] > ymax) ymax = y[n];
|
||||
// printf("f(%g) = %g\n", x[n], y[n]);
|
||||
}
|
||||
const double xmin = x[0];
|
||||
const double xmax = x[len - 1];
|
||||
printf("xmin = %g, xmax = %g, ymin = %g, ymax = %g\n", xmin, xmax, ymin, ymax);
|
||||
canvas.xofset = xmin;
|
||||
canvas.yofset = ymin;
|
||||
canvas.xscale = canvas.width / (xmax - xmin);
|
||||
canvas.yscale = canvas.height / (ymax - ymin);
|
||||
Stepping sx (xmin, xmax), sy (ymin, ymax);
|
||||
// sx.f(); sy.f();
|
||||
|
||||
n += CreateJSON (json, json_max, canvas, sx, sy);
|
||||
} else {
|
||||
n += snprintf(json + n, json_max - n, "{ \"name\":\"unknown\" }");
|
||||
}
|
||||
/* Pro kreslení na canvas se používají 2 metody:
|
||||
* 1. Přímé předání dat pomocí ukazatelů.
|
||||
* Ukázalo se, že typ int nestačí, zaokrouhlení je kostrbaté, double je zbytečně velký,
|
||||
* takže stačí float (32.bit), je to normováno na velikost canvas, takže tam nejsou skoky v řádech
|
||||
* 2. Z dat uděláme JSON a ten zpracuje javascript. Není to moc efektivní, ale funguje.
|
||||
* (3. zde nepoužito) Vytvoříme přímo javascript zdroják, který to celé vykreslí
|
||||
* a ten pak spustíme v javascriptu pomocí eval(). Funguje také, ale je to divné.
|
||||
* */
|
||||
float * ix = new float [len]; // float32 stačí
|
||||
float * iy = new float [len];
|
||||
for (int k=0; k<len; k++) {
|
||||
ix[k] = canvas.xt (x[k]); // přepočteno na velikost canvasu
|
||||
iy[k] = canvas.yt (y[k]);
|
||||
// printf("f(%d) = %d\n", ix[n], iy[n]);
|
||||
}
|
||||
drawPoints (ix, iy, len, json, n);
|
||||
delete [] ix; delete [] iy;
|
||||
delete [] json;
|
||||
}
|
61
wasm.h
Normal file
61
wasm.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
#ifndef _WASM_H_DEF
|
||||
#define _WASM_H_DEF
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
struct Canvas {
|
||||
double width, height;
|
||||
double xscale, yscale;
|
||||
double xofset, yofset;
|
||||
float xt (const double x) const {
|
||||
return float (xscale * (x - xofset));
|
||||
}
|
||||
float yt (const double y) const {
|
||||
return float (height - yscale * (y - yofset));
|
||||
}
|
||||
};
|
||||
static const char * multipliers [] = {"f","p","n","μ","m","","k","M","G","T","P"};
|
||||
struct Stepping {
|
||||
double b,e,s;
|
||||
char fmtbuf [16];
|
||||
explicit Stepping (const double from, const double to) {
|
||||
double lp;
|
||||
const double z = log10 (fabs (to - from));
|
||||
const double fp = modf (z, & lp);
|
||||
if (fp < 0.30103) s = 1.0;
|
||||
else if (fp > 0.69897) s = 5.0;
|
||||
else s = 2.0;
|
||||
int ip = int (lp) - 1;
|
||||
if (z < 0.0) ip -= 1;
|
||||
s *= ::pow (10.0, double (ip));
|
||||
do {
|
||||
const int k = int (fabs(to - from) / s);
|
||||
b = round (from / s) * s;
|
||||
e = round (to / s) * s;
|
||||
if (k > 50) s *= 10.0; // prevence přeplnění osy, nevím proč, ale tohle to odstraní
|
||||
else break; // patrně log10() nedává přesně to, co bych čekal
|
||||
} while (true);
|
||||
}
|
||||
void f () {printf("stepping = %g, b = %g, e = %g\n", s, b, e);}
|
||||
char * ing (const double x, int n=0) {
|
||||
if (fabs(x) < 0.5 * s) {
|
||||
fmtbuf[n++] = ' ';
|
||||
fmtbuf[n++] = '\0';
|
||||
return fmtbuf;
|
||||
}
|
||||
if (x < 0.0) {
|
||||
fmtbuf[n++] = '-';
|
||||
return ing (-x, n);
|
||||
}
|
||||
if (x > 0.0) {
|
||||
double ip;
|
||||
const double fp = modf(log10(x), & ip);
|
||||
int pi = ip, ofs = 5 * 3;
|
||||
if (pi < -ofs) pi = -ofs;
|
||||
if (pi > 18 ) pi = 18;
|
||||
const div_t dt = div(pi + ofs, 3);
|
||||
n += snprintf (fmtbuf + n, 16 - n, "%g%s", ::pow (10.0, fp + double (dt.rem)), multipliers [dt.quot]);
|
||||
}
|
||||
return fmtbuf;
|
||||
}
|
||||
};
|
||||
#endif // _WASM_H_DEF
|
20
wasm.mk
Normal file
20
wasm.mk
Normal file
|
@ -0,0 +1,20 @@
|
|||
MAIN = wasm.o
|
||||
MOBJS+=heap.o newdel.o
|
||||
LD = wasm-ld-10
|
||||
TARGET = --target=wasm32-unknown-unknown
|
||||
PR = bin/module.wasm
|
||||
# Cesta k newlib
|
||||
LPATH = ${HOME}/local/wasm32-none-eabi
|
||||
CFLAGS = -Wall -Oz -flto -I. -I$(LPATH)/include $(TARGET)
|
||||
#CFLAGS+= --sysroot=${HOME}/local/wasm32-none-eabi
|
||||
CFLAGS+= -ffunction-sections -fdata-sections
|
||||
#CFLAGS+= -Wno-incompatible-library-redeclaration
|
||||
# Pro clang by muselo ještě přibýt -nostartfiles $(TARGET) a LFLAGS by bylo -Wl,
|
||||
LFLAGS = --no-entry --import-memory --lto-O3 --gc-sections
|
||||
# vetsi stack znamena zvetsit i WebAssembly.Memory({ initial: n 64K block })
|
||||
#LFLAGS+= -z stack-size=1048576
|
||||
#LFLAGS+= --print-gc-sections
|
||||
#LFLAGS+= --allow-undefined
|
||||
LFLAGS+= --allow-undefined-file=symbols.txt
|
||||
LDLIBS = -L$(LPATH)/lib -lc -lm
|
||||
|
Loading…
Reference in a new issue