539 lines
54 KiB
HTML
539 lines
54 KiB
HTML
|
<!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>
|