[Note] Rewrite exponential function ⇆ trigonometric function with Sympy.rewrite ()

As the title says, it is an article as a personal memorandum. It would be greatly appreciated if you could point out any obvious mistakes.

Also, I'm currently in the process of changing jobs (Boss)

About Sympy

SymPy 1.5 documentation

Sympy is a library that allows you to perform symbol calculations on Python, and you can programmatically transform expressions using paper notebooks and pens to find solutions to equations. I am paying attention to it when learning by following the process of deriving the theory explained in academic books and technical books, or when it is necessary to prepare necessary mathematical formulas for convenience. Sympy.rewrite() sympy.core.basic.Basic.rewrite This method is used to re-express the specified function into another form of function. Exponential function ⇆ Trigonometric function is often transformed in the calculation process of wave or complex function, but sympy.simplify () of formula simplification It seems to be convenient to use in combination with /simplify/simplify.html) etc.

example

Consider the situation where the complex function w of the following equation is divided into a real part and an imaginary part. (U and α are constants) $ w = U e^{-i \alpha } \tag{1} $ For equation (1) $ u = Re[w],  v = -Im[w] $ Then, let's express equation (1) as follows. $ w = u - iv \tag{2} $ Find u and v in this equation (2).

For equation (1), rewrite the exponential function as the sum of real and imaginary terms using Euler's formula of the following equation. $ e^{i \theta } = \cos{\theta} + i\sin{\theta} \tag{3} $ Apply equation (3) to equation (1), $ w = U (\cos{\alpha} - i\sin{\alpha}) \tag{4} $ Therefore, $ u = Re\left[w\right]=U\cos{\alpha},$$v = -Im\left[w\right]=U\sin{\alpha} $

Write in Sympy

#Import libraries and modules
import sympy as sp
from sympy import * 

#Constant U,Define symbols for α
U, alpha = symbols('U alpha')

#Define function w
w = Function("w")
w = U*exp(-I*alpha)

display

print(w)  #w is displayed as a character
w         #Display w in mathematical typeface

result スクリーンショット 2019-12-25 17.53.11.png Here, rewrite () is used to re-express using trigonometric functions (sin, cos).

w.rewrite(exp, sin, cos)

result スクリーンショット 2019-12-25 17.57.14.png You can also use expand () to expand an expression.

expand(w.rewrite(exp, sin, cos))

result スクリーンショット 2019-12-25 17.59.54.png

Furthermore, using re () will specify the real and imaginary parts of the formula.

re(expand(w.rewrite(exp, sin, cos)))

result スクリーンショット 2019-12-25 18.10.41.png

Summary

I made a lot of calculation mistakes by hand, so I came across Sympy when I wondered if I could substitute Python for it. (It's been about 3 days since I met at the time of writing ...) It seems that it can be used for checking in each part of the calculation process and for self-catering of examples, so I thought that if I met when I was a student, I would have made a lot of progress. Learning notes are not bulky paper notes, but if you write them in .ipynb using Jupyter and manage them on Github, you can record the learning process, and it is convenient because you can use it for drafting articles in this way. There is also Notability of the handwritten note application (rather for taking notes with figures such as lectures), but in the end there was a problem that it was clogged with calculation mistakes, so the existence of a library like Sympy is helpful. I will get used to handling it more in the future.


As I mentioned at the beginning, as of the end of 2019, I am in the process of changing jobs to an engineer. If you think "What's this guy !?", you can follow Lapras, Wantedly, etc. from the linked account, so I'd be happy if you could see various information. Twitter_DM(@RiSE_blackbird)

Recommended Posts

[Note] Rewrite exponential function ⇆ trigonometric function with Sympy.rewrite ()
Note for formatting numbers with python format function