Skip to content

Instantly share code, notes, and snippets.

@mgttt
Last active July 7, 2023 15:29
Show Gist options
  • Save mgttt/85985a1a9573918f59baf106a39e2d5f to your computer and use it in GitHub Desktop.
Save mgttt/85985a1a9573918f59baf106a39e2d5f to your computer and use it in GitHub Desktop.
interesting maths with python
approximation function for natural logarithm
# ln=(x,a=2**32)=>a*x**(1/a)-a
#ln=lambda x,a=1<<32:a*x**(1/a)-a
ln=lambda x,a=1<<32:a*(x**(1/a)-1)

>>> numpy.log(400) - ln(400)
-6.776018235399306e-08

经典(牛顿引力) $F = G \frac{m_1 m_2}{r^2}$

广义相对论 $G_{\mu\nu} = \frac{8 \pi G}{c^4} T_{\mu\nu}$ $G_{\mu\nu}$ 爱因斯坦张量 $T_{\mu\nu}$ 能量-动量张量 G 是牛顿的引力常数 c 是光速


$$T = \begin{pmatrix} T_{00} & T_{01} & T_{02} & T_{03} \\\ T_{10} & T_{11} & T_{12} & T_{13} \\\ T_{20} & T_{21} & T_{22} & T_{23} \\\ T_{30} & T_{31} & T_{32} & T_{33} \end{pmatrix}$$
T = \begin{pmatrix}
T_{00} & T_{01} & T_{02} & T_{03} \\
T_{10} & T_{11} & T_{12} & T_{13} \\
T_{20} & T_{21} & T_{22} & T_{23} \\
T_{30} & T_{31} & T_{32} & T_{33}
\end{pmatrix}

$$ \begin{aligned} & \phi(x,y) = \phi \left(\sum_{i=1}^n x_ie_i, \sum_{j=1}^n y_je_j \right) = \sum_{i=1}^n \sum_{j=1}^n x_i y_j \phi(e_i, e_j) = \\ & (x_1, \ldots, x_n) \left( \begin{array}{ccc} \phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \\ \vdots & \ddots & \vdots \\ \phi(e_n, e_1) & \cdots & \phi(e_n, e_n) \end{array} \right) \left( \begin{array}{c} y_1 \\ \vdots \\ y_n \end{array} \right) \end{aligned} $$

$$
\begin{aligned}
  & \phi(x,y) = \phi \left(\sum_{i=1}^n x_ie_i, \sum_{j=1}^n y_je_j \right)
  = \sum_{i=1}^n \sum_{j=1}^n x_i y_j \phi(e_i, e_j) = \\
  & (x_1, \ldots, x_n) \left( \begin{array}{ccc}
      \phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \\
      \vdots & \ddots & \vdots \\
      \phi(e_n, e_1) & \cdots & \phi(e_n, e_n)
    \end{array} \right)
  \left( \begin{array}{c}
      y_1 \\
      \vdots \\
      y_n
    \end{array} \right)
\end{aligned}
$$
@mgttt
Copy link
Author

mgttt commented Jul 1, 2023

黎曼函数
$$\zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s}$$
\zeta(s) = \sum_{n=1}^{\infty} \frac{1}{n^s}

@mgttt
Copy link
Author

mgttt commented Jul 2, 2023

$${\begin{aligned}z&=x+iy=|z|(\cos \varphi +i\sin \varphi )=re^{i\varphi },\\{\bar {z}}&=x-iy=|z|(\cos \varphi -i\sin \varphi )=re^{-i\varphi },\end{aligned}}$$

@mgttt
Copy link
Author

mgttt commented Jul 2, 2023

equivalence of euler formula and matrix transform

from sympy import symbols, Matrix, acos, cos, sin, I, exp, re, im

a2v = lambda *a:Matrix(a)

norm = lambda V:V.norm()

# i.e. Z.dot(R) 
dot_product = lambda Z,R:Matrix.dot(Z,R)

# quick angle (not checking zero v yet)
angle_func = lambda Z,R:acos(dot_product(Z,R)/(norm(Z)*norm(R)))

# the transformation matrix
def find_M1(Z,R):
  angle = angle_func(Z,R)
  return Matrix([[cos(angle), -sin(angle)], [sin(angle), cos(angle)]])

def find_M2(Z,R):
  angle = angle_func(Z,R)
  complex_angle = angle * I
  exp_angle = exp(complex_angle)
  return Matrix([[ re(exp_angle), -im(exp_angle)],[im(exp_angle), re(exp_angle)]])

v1 = a2v(1,2)
v2 = a2v(2,3)
assert find_M1(v1,v2) == find_M2(v1,v2)

@mgttt
Copy link
Author

mgttt commented Jul 7, 2023

Fourier Transform: F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt
$$F(\omega) = \int_{-\infty}^{\infty} f(t) e^{-i\omega t} dt$$
Discrete Fourier Transform (DFT): X[k] = \sum_{n=0}^{N-1} x[n] e^{-i \frac{2\pi}{N} nk}
$$X[k] = \sum_{n=0}^{N-1} x[n] e^{-i \frac{2\pi}{N} nk}$$

@mgttt
Copy link
Author

mgttt commented Jul 7, 2023

Laplace Transform 拉普拉斯变换 F(s) = \mathcal{L}{f(t)} = \int_{0}^{\infty} f(t) e^{-st} dt
$$F(s) = \mathcal{L}{f(t)} = \int_{0}^{\infty} f(t) e^{-st} dt$$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment