Let's prove the addition theorem of trigonometric functions by replacing the function with a function in SymPy (≠ substitution)

subs and replace

SymPy's subs and replace are similar non-functions of substitution and replacement.

from sympy import symbols, sin, cos, exp, I, sqrt, expand, init_printing
init_printing()
x = symbols('x')
f =  sin(x)+sin(x**2)

The following example substitutes cos (x) for sin (x).

f.subs(sin(x), cos(x))
\sin{\left (x^{2} \right )} + \cos{\left (x \right )}

Obviously, in this case, sin (x ** 2) is not replaced by cos (x ** 2). I don't know if this is the intended specification, but .subs (sin, cos) replaces sin with cos.

f.subs(sin, cos)
\cos{\left (x \right )} + \cos{\left (x^{2} \right )}

However, .subs (sin, sqrt) does not replace sin with sqrt.

f.subs(sin, sqrt)
\sin{\left (x \right )} + \sin{\left (x^{2} \right )}

I'm not sure, but I feel that sympy.core.function.FunctionClass can be replaced with each other.

for func in [sin, cos, sqrt]:
    print(func.__class__)
<class 'sympy.core.function.FunctionClass'>
<class 'sympy.core.function.FunctionClass'>
<class 'function'>

Originally, I think that replace is the original usage to replace a function with a function.

f.replace(sin, cos)
\cos{\left (x \right )} + \cos{\left (x^{2} \right )}
f.replace(sin, sqrt)
\sqrt{x} + \sqrt{x^{2}}

You can use your own function or lambda expression as the argument of replace.

f.replace(sin, lambda t: cos(t**2)) # sin(□)Cos(□**2)Replace with
\cos{\left (x^{2} \right )} + \cos{\left (x^{4} \right )}

From Euler's formula

Euler's formula $ e^{\theta i} = \cos\theta + i\sin\theta\tag{1} $ In, if $ \ theta $ is replaced with $-\ theta $, then from $ \ cos (-\ theta) = \ cos (\ theta), \ \ sin (-\ theta) =-\ sin \ theta $ $ e^{-\theta i} = \cos\theta - i\sin\theta\tag{2} $ is. Therefore, from $ (1) + (2) $ $ e^{\theta i} + e^{-\theta i} = 2\cos\theta\ \Longleftrightarrow\ \cos\theta = \frac{e^{\theta i} + e^{-\theta i}}{2} $ From $ (1)-(2) $ $ e^{\theta i} - e^{-\theta i} = 2i\sin\theta\ \Longleftrightarrow\ \sin\theta = \frac{e^{\theta i} - e^{-\theta i}}{2i} $ Holds. That is, as is well known, $ \ cos $ and $ \ sin $ can be expressed by exponential functions.

cos2exp = lambda t: (exp(t*I) + exp(-t*I))/2
sin2exp = lambda t: (exp(t*I) - exp(-t*I))/(2*I)
(sin(x)+cos(x)).replace(cos, cos2exp).replace(sin, sin2exp)
- \frac{i}{2} \left(e^{i x} - e^{- i x}\right) + \frac{e^{i x}}{2} + \frac{1}{2} e^{- i x}

By using this, various formulas of trigonometric functions can be proved (confirmed).

alpha, beta = symbols(r'\alpha \beta')
A = sin(alpha+beta)
B = sin(alpha)*cos(beta) + cos(alpha)*sin(beta)
expand(A.replace(sin, sin2exp).replace(cos, cos2exp))
- \frac{i}{2} e^{i \alpha} e^{i \beta} + \frac{i}{2} e^{- i \alpha} e^{- i \beta}
expand(B.replace(sin, sin2exp).replace(cos, cos2exp))
- \frac{i}{2} e^{i \alpha} e^{i \beta} + \frac{i}{2} e^{- i \alpha} e^{- i \beta}

Now, ʻA = B`, that is $ \sin(\alpha+\beta) = \sin\alpha\cos\beta + \cos\alpha\sin\beta $ Was confirmed.

Recommended Posts

Let's prove the addition theorem of trigonometric functions by replacing the function with a function in SymPy (≠ substitution)
Get the caller of a function in Python
[AWS] Let's run a unit test of Lambda function in the local environment
Let's make a leap in the manufacturing industry by utilizing the Web in addition to Python
The value of meta when specifying a function with no return value in Dask dataframe apply
Let's introduce the library currently used by engineers with about 3 years of experience in Django
Let's summarize the basic functions of TensorFlow by creating a neural network that learns XOR gates
Process the contents of the file in order with a shell script
Find the optimal value of a function with a genetic algorithm (Part 2)
[Statistics] Grasp the image of the central limit theorem with a graph
If you give a list with the default argument of the function ...
Read the standard output of a subprocess line by line in Python
A function that measures the processing time of a method in python
Create a video player with PySimpleGUI + OpenCV 3 Addition of mask function
Create a function to get the contents of the database in Go
A memo organized by renaming the file names in the folder with python
Generate a list packed with the number of days in the current month.
Calculate the probability of being a squid coin with Bayes' theorem [python]
Receive a list of the results of parallel processing in Python with starmap
Find the minimum value of a function by particle swarm optimization (PSO)
I made a mistake in fetching the hierarchy with MultiIndex of pandas
Let's claim the possibility of pyenv-virtualenv in 2021
Let's calculate Godel's β function with SymPy
Function to extract the maximum and minimum values ​​in a slice with Go
Feel free to write a test with nose (in the case of + gevent)
To output a value even in the middle of a cell with Jupyter Notebook
Approximation by the least squares method of a circle with two fixed points
Let's draw the voltage of the digital multimeter 34461A of the measuring instrument Keysight with CircuitPython
How to get a list of files in the same directory with python