summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/hybrid-latex/examples/example-01.tex
blob: 08919ec095bef23d48f3a13b9ee8af83dbecd7f3 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
\documentclass[12pt]{pylatex}
\usepackage{examples}

\begin{document}

\section*{Elementary maths}

\vspace{-5pt}

This is a collection of basic mathematical computations using {\tt\small sympy}. The main purpose is to demonstrate the use of {\tt\small\verb|\py|} and {\tt\small\verb|\py*|}. Note that {\tt\small sympy 1.1.1} appears unable to simplify $\tanh(\log(x))$ (compare {\tt\small rhs.108} shown below against {\tt\small ans.108} shown in the \href{../../mathematica/examples/example-01.pdf}{Mathematica} examples). Note also the separate computaions for the left and right hand sides of results 108, 109 and 110.

\vspace{-10pt}

\begin{minipage}[t]{0.65\textwidth}
\begin{python}
   from sympy import *
   x, y, z, a, b, c = symbols('x y z a b c')
   ans = expand((a+b)**3)                                  # py (ans.101,ans)
   ans = factor(-2*x+2*x+a*x-x**2+a*x**2-x**3)             # py (ans.102,ans)
   ans = solve(x**2-4, x)                                  # py (ans.103,ans)
   ans = solve([2*a-b - 3, a+b+c - 1,-b+c - 6],[a,b,c])    # py (ans.104,ans)
   ans = N(pi,50)                                          # py (ans.105,ans)
   ans = apart(1/((1 + x)*(5 + x)))                        # py (ans.106,ans)
   ans = together((1/(1 + x) - 1/(5 + x))/4)               # py (ans.107,ans)
   ans = simplify(tanh(log(x)))                            # py (rhs.108,ans)
   ans = simplify(tanh(I*x))                               # py (rhs.109,ans)
   ans = simplify(sinh(3*x) - 3*sinh(x) - 4*(sinh(x))**3)  # py (rhs.110,ans)
   ans = tanh(log(x))                                      # py (lhs.108,ans)
   ans = tanh(UnevaluatedExpr(I*x))                        # py (lhs.109,ans)
   ans = sinh(3*x) - 3*sinh(x) - 4*(sinh(x))**3            # py (lhs.110,ans)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.101}\\
      &\py*{ans.102}\\
      &\py*{ans.103}\\
      &\py*{ans.104}\\
      &\py*{ans.105}\\
      &\py*{ans.106}\\
      &\py*{ans.107}\\
      \py{lhs.108} &= \Py{rhs.108}\\
      \py{lhs.109} &= \Py{rhs.109}\\
      \py{lhs.110} &= \Py{rhs.110}
   \end{align*}
\end{latex}
\end{minipage}

\vspace{-10pt}

\begin{align*}
   &\py*{ans.101}\\
   &\py*{ans.102}\\
   &\py*{ans.103}\\
   &\py*{ans.104}\\
   &\py*{ans.105}\\
   &\py*{ans.106}\\
   &\py*{ans.107}\\
   \py{lhs.108} &= \Py{rhs.108}\\
   \py{lhs.109} &= \Py{rhs.109}\\
   \py{lhs.110} &= \Py{rhs.110}
\end{align*}

\clearpage

\section*{Linear Algebra}

\vspace{-10pt}

\begin{minipage}[t]{0.65\textwidth}
\begin{python}
   from sympy import linsolve
   lamda = Symbol('lamda')
   mat  = Matrix([[2,3], [5,4]])                   # py (ans.201,mat)
   eig1 = mat.eigenvects()[0][0]                   # 1st eigenvalue
   eig2 = mat.eigenvects()[1][0]                   # 2nd eigenvalue
   v1   = mat.eigenvects()[0][2][0]                # 1st eigenvector
   v2   = mat.eigenvects()[1][2][0]                # 2nd eigenvector
   eig  = simplify(Matrix([eig1,eig2]))            # py (ans.202,eig)
   vec  = simplify(5*Matrix([]).col_insert(0,v1)
                               .col_insert(1,v2))  # py (ans.203,vec)
   det  = expand((mat - lamda * eye(2)).det())     # py (ans.204,det)
   rhs  = Matrix([[3],[7]])                        # py (ans.205,rhs)
   ans  = list(linsolve((mat,rhs),x,y))[0]         # py (ans.206,ans)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.201}\\
      &\py*{ans.202}\\
      &\py*{ans.203}\\
      &\py*{ans.204}\\
      &\py*{ans.205}\\
      &\py*{ans.206}
   \end{align*}
\end{latex}
\end{minipage}

\begin{align*}
   &\py*{ans.201}\\
   &\py*{ans.202}\\
   &\py*{ans.203}\\
   &\py*{ans.204}\\
   &\py*{ans.205}\\
   &\py*{ans.206}
\end{align*}

\clearpage

\section*{Limits}

\vspace{-10pt}

\begin{minipage}[t]{0.65\textwidth}
\begin{python}
   n, dx = symbols('n dx')
   ans = limit(sin(4*x)/x,x,0)                  # py (ans.301,ans)
   ans = limit(2**x/x,x,oo)                     # py (ans.302,ans)
   ans = limit(((x+dx)**2 - x**2)/dx, dx,0)     # py (ans.303,ans)
   ans = limit((4*n + 1)/(3*n - 1),n,oo)        # py (ans.304,ans)
   ans = limit((1+(a/n))**n,n,oo)               # py (ans.305,ans)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.301}\\
      &\py*{ans.302}\\
      &\py*{ans.303}\\
      &\py*{ans.304}\\
      &\py*{ans.305}
   \end{align*}
\end{latex}
\end{minipage}

\begin{align*}
   &\py*{ans.301}\\
   &\py*{ans.302}\\
   &\py*{ans.303}\\
   &\py*{ans.304}\\
   &\py*{ans.305}
\end{align*}

\section*{Series}

\vspace{-10pt}

\begin{minipage}[t]{0.65\textwidth}
\begin{python}
   ans = series((1 + x)**(-2), x, 1, 6)         # py (ans.401,ans)
   ans = series(exp(x), x, 0, 6)                # py (ans.402,ans)
   ans = Sum(1/n**2, (n,1,50)).doit()           # py (ans.403,ans)
   ans = Sum(1/n**4, (n,1,oo)).doit()           # py (ans.404,ans)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.401}\\
      &\py*{ans.402}\\
      &\py*{ans.403}\\
      &\py*{ans.404}
   \end{align*}
\end{latex}
\end{minipage}

\begin{align*}
   &\py*{ans.401}\\
   &\py*{ans.402}\\
   &\py*{ans.403}\\
   &\py*{ans.404}
\end{align*}

\clearpage

\section*{Calculus}

This example shows how {\tt\small\verb|\Py|} can be used to set the equation tag on the far right hand side.

\begin{minipage}[t]{0.65\textwidth}
\begin{python}
   ans = diff(x*sin(x),x)                                    # py (ans.501,ans)
   ans = diff(x*sin(x),x).subs(x,pi/4)                       # py (ans.502,ans)
   ans = integrate(2*sin(x)**2, (x,a,b))                     # py (ans.503,ans)
   ans = Integral(2*exp(-x**2), (x,0,oo))                    # py (lhs.504,ans)
   ans = ans.doit()                                          # py (ans.504,ans)
   ans = Integral(Integral(x**2 + y**2, (y,0,x)), (x,0,1))   # py (lhs.505,ans)
   ans = ans.doit()                                          # py (ans.505,ans)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.501}\\
      &\py*{ans.502}\\
      &\py*{ans.503}\\
       \py{lhs.504}&=\Py{ans.504}\\
       \py{lhs.505}&=\Py{ans.505}
   \end{align*}
\end{latex}
\end{minipage}

\begin{align*}
   &\py*{ans.501}\\
   &\py*{ans.502}\\
   &\py*{ans.503}\\
    \py{lhs.504}&=\Py{ans.504}\\
    \py{lhs.505}&=\Py{ans.505}
\end{align*}

\clearpage

\section*{Differential equations}

\vspace{-10pt}

\begin{minipage}[t]{0.70\textwidth}
\begin{python}
   y = Function('y')
   C1, C2 = symbols('C1 C2')

   ode = Eq(y(x).diff(x) + y(x), 2*a*sin(x))
   sol = expand(dsolve(ode,y(x)).rhs)                              # py (ans.601,sol)
   cst = solve([sol.subs(x,0)],dict=True)
   sol = sol.subs(cst[0])                                          # py (ans.602,sol)

   ode = Eq(y(x).diff(x,2) + y(x), 0)
   sol = expand(dsolve(ode,y(x)).rhs)                              # py (ans.603,sol)
   cst = solve([sol.subs(x,0),sol.diff(x).subs(x,0)-1],dict=True)
   sol = sol.subs(cst[0])                                          # py (ans.604,sol)

   ode = Eq(y(x).diff(x,2) + 5*y(x).diff(x) - 6*y(x), 0)
   sol = expand(dsolve(ode,y(x)).rhs)                              # py (ans.605,sol)
   sol = sol.subs({C1:2,C2:3})                                     # py (ans.606,sol)
\end{python}
\end{minipage}
\hskip 1cm
\begin{minipage}[t]{0.30\textwidth}
\begin{latex}
   \begin{align*}
      &\py*{ans.601}\\
      &\py*{ans.602}\\
      &\py*{ans.603}\\
      &\py*{ans.604}\\
      &\py*{ans.605}\\
      &\py*{ans.606}
   \end{align*}
\end{latex}
\end{minipage}

\begin{align*}
   &\py*{ans.601}\\
   &\py*{ans.602}\\
   &\py*{ans.603}\\
   &\py*{ans.604}\\
   &\py*{ans.605}\\
   &\py*{ans.606}
\end{align*}

\end{document}