Python 代码生成 LaTeX 数学公式:latexify 示例 examples

文中内容仅限技术学习与代码实践参考,市场存在不确定性,技术分析需谨慎验证,不构成任何投资建议。

latexify 示例

本 notebook 提供了多个使用 latexify 的示例。

更多细节请参阅官方文档。

如有任何疑问,请在 issue tracker 中提出。

安装 latexify

# 运行下方示例前请先重启运行时。
%pip install latexify-py
Collecting latexify-py
  Downloading latexify_py-0.4.2-py3-none-any.whl (38 kB)
Collecting dill>=0.3.2 (from latexify-py)
  Downloading dill-0.3.7-py3-none-any.whl (115 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 115.3/115.3 kB 5.7 MB/s eta 0:00:00
Installing collected packages: dill, latexify-py
Successfully installed dill-0.3.7 latexify-py-0.4.2

latexify 引入你的代码

import math  # 可选
import numpy as np  # 可选
import latexify

latexify.__version__
'0.4.2'

示例

@latexify.function
def solve(a, b, c):
  return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)

print(solve(1, 4, 3))  # 函数调用照常工作。
print(solve)  # 打印函数会显示底层 LaTeX 源码。
solve  # 在 notebook 中直接写函数名会渲染表达式。

# 把底层 LaTeX 源码写入文件。
with open("compiled.tex", "w") as fp:
  print(solve, file=fp)
-1.0
\mathrm{solve}(a, b, c) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}
# latexify.expression 用法类似,但输出时不带函数签名。
@latexify.expression
def solve(a, b, c):
  return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)

solve

− b + b 2 − 4 a c 2 a \displaystyle \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a} 2ab+b24ac

# latexify.get_latex 可直接获取底层 LaTeX 表达式。
def solve(a, b, c):
  return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)

latexify.get_latex(solve)
'\\mathrm{solve}(a, b, c) = \\frac{-b + \\sqrt{ b^{2} - 4 a c }}{2 a}'
@latexify.function
def sinc(x):
  if x == 0:
    return 1
  else:
    return math.sin(x) / x

sinc

s i n c ( x ) = { 1 , i f   x = 0 sin ⁡ x x , o t h e r w i s e \displaystyle \mathrm{sinc}(x) = \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \frac{\sin x}{x}, & \mathrm{otherwise} \end{array} \right. sinc(x)={1,xsinx,if x=0otherwise

# elif 或嵌套 else-if 会被展开。
@latexify.function
def fib(x):
  if x == 0:
    return 0
  elif x == 1:
    return 1
  else:
    return fib(x-1) + fib(x-2)

fib

f i b ( x ) = { 0 , i f   x = 0 1 , i f   x = 1 f i b ( x − 1 ) + f i b ( x − 2 ) , o t h e r w i s e \displaystyle \mathrm{fib}(x) = \left\{ \begin{array}{ll} 0, & \mathrm{if} \ x = 0 \\ 1, & \mathrm{if} \ x = 1 \\ \mathrm{fib} \mathopen{}\left( x - 1 \mathclose{}\right) + \mathrm{fib} \mathopen{}\left( x - 2 \mathclose{}\right), & \mathrm{otherwise} \end{array} \right. fib(x)= 0,1,fib(x1)+fib(x2),if x=0if x=1otherwise

# 部分数学符号会被自动转换。
@latexify.function(use_math_symbols=True)
def greek(alpha, beta, gamma, Omega):
  return alpha * beta + math.gamma(gamma) + Omega

greek

g r e e k ( α , β , γ , Ω ) = α β + Γ ( γ ) + Ω \displaystyle \mathrm{greek}(\alpha, \beta, \gamma, \Omega) = \alpha \beta + \Gamma \mathopen{}\left( \gamma \mathclose{}\right) + \Omega greek(α,β,γ,Ω)=αβ+Γ(γ)+Ω

# 函数名、参数、变量均可替换。
identifiers = {
    "my_function": "f",
    "my_inner_function": "g",
    "my_argument": "x",
}

@latexify.function(identifiers=identifiers)
def my_function(my_argument):
    return my_inner_function(my_argument)

my_function

f ( x ) = g ( x ) \displaystyle f(x) = g \mathopen{}\left( x \mathclose{}\right) f(x)=g(x)

# 赋值语句可合并为一个表达式。
@latexify.function(reduce_assignments=True)
def f(a, b, c):
    discriminant = b**2 - 4 * a * c
    numerator = -b + math.sqrt(discriminant)
    denominator = 2 * a
    return numerator / denominator

f

f ( a , b , c ) = − b + b 2 − 4 a c 2 a \displaystyle f(a, b, c) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a} f(a,b,c)=2ab+b24ac

# 支持矩阵。
@latexify.function(reduce_assignments=True, use_math_symbols=True)
def transform(x, y, a, b, theta, s, t):
  cos_t = math.cos(theta)
  sin_t = math.sin(theta)
  scale = np.array([[a, 0, 0], [0, b, 0], [0, 0, 1]])
  rotate = np.array([[cos_t, -sin_t, 0], [sin_t, cos_t, 0], [0, 0, 1]])
  move = np.array([[1, 0, s], [0, 1, t], [0, 0, 1]])
  return move @ rotate @ scale @ np.array([[x], [y], [1]])

transform

t r a n s f o r m ( x , y , a , b , θ , s , t ) = [ 1 0 s 0 1 t 0 0 1 ] ⋅ [ cos ⁡ θ − sin ⁡ θ 0 sin ⁡ θ cos ⁡ θ 0 0 0 1 ] ⋅ [ a 0 0 0 b 0 0 0 1 ] ⋅ [ x y 1 ] \displaystyle \mathrm{transform}(x, y, a, b, \theta, s, t) = \begin{bmatrix} 1 & 0 & s \\ 0 & 1 & t \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} a & 0 & 0 \\ 0 & b & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} transform(x,y,a,b,θ,s,t)= 100010st1 cosθsinθ0sinθcosθ0001 a000b0001 xy1

# latexify.algorithmic 会生成 algorithmic 环境而非方程。
@latexify.algorithmic
def fib(x):
  if x == 0:
    return 0
  elif x == 1:
    return 1
  else:
    return fib(x-1) + fib(x-2)

fib

f u n c t i o n   f i b ( x ) i f   x = 0 r e t u r n   0 e l s e i f   x = 1 r e t u r n   1 e l s e r e t u r n   f i b ( x − 1 ) + f i b ( x − 2 ) e n d   i f e n d   i f e n d   f u n c t i o n \begin{array}{l} \mathbf{function} \ \mathrm{fib}(x) \\ \hspace{1em} \mathbf{if} \ x = 0 \\ \hspace{2em} \mathbf{return} \ 0 \\ \hspace{1em} \mathbf{else} \\ \hspace{2em} \mathbf{if} \ x = 1 \\ \hspace{3em} \mathbf{return} \ 1 \\ \hspace{2em} \mathbf{else} \\ \hspace{3em} \mathbf{return} \ \mathrm{fib} \mathopen{}\left( x - 1 \mathclose{}\right) + \mathrm{fib} \mathopen{}\left( x - 2 \mathclose{}\right) \\ \hspace{2em} \mathbf{end \ if} \\ \hspace{1em} \mathbf{end \ if} \\ \mathbf{end \ function} \end{array} function fib(x)if x=0return 0elseif x=1return 1elsereturn fib(x1)+fib(x2)end ifend ifend function

# 另一个示例:latexify.algorithmic 支持常规控制流。
@latexify.algorithmic
def collatz(x):
  n = 0
  while x > 1:
    n = n + 1
    if x % 2 == 0:
      x = x // 2
    else:
      x = 3 * x + 1
  return n

collatz

f u n c t i o n   c o l l a t z ( x ) n ← 0 w h i l e   x > 1 n ← n + 1 i f   x % 2 = 0 x ← ⌊ x 2 ⌋ e l s e x ← 3 x + 1 e n d   i f e n d   w h i l e r e t u r n   n e n d   f u n c t i o n \begin{array}{l} \mathbf{function} \ \mathrm{collatz}(x) \\ \hspace{1em} n \gets 0 \\ \hspace{1em} \mathbf{while} \ x > 1 \\ \hspace{2em} n \gets n + 1 \\ \hspace{2em} \mathbf{if} \ x \mathbin{\%} 2 = 0 \\ \hspace{3em} x \gets \left\lfloor\frac{x}{2}\right\rfloor \\ \hspace{2em} \mathbf{else} \\ \hspace{3em} x \gets 3 x + 1 \\ \hspace{2em} \mathbf{end \ if} \\ \hspace{1em} \mathbf{end \ while} \\ \hspace{1em} \mathbf{return} \ n \\ \mathbf{end \ function} \end{array} function collatz(x)n0while x>1nn+1if x%2=0x2xelsex3x+1end ifend whilereturn nend function

风险提示与免责声明
本文内容基于公开信息研究整理,不构成任何形式的投资建议。历史表现不应作为未来收益保证,市场存在不可预见的波动风险。投资者需结合自身财务状况及风险承受能力独立决策,并自行承担交易结果。作者及发布方不对任何依据本文操作导致的损失承担法律责任。市场有风险,投资须谨慎。

你可能感兴趣的:(Python 代码生成 LaTeX 数学公式:latexify 示例 examples)