如何在Python Sympy Latex中构建组合表达式?

时间:2023-02-02 00:11:11

I'm trying to construct a hypergeometric pdf equation, using the nCr expression with parentheses (n over r) using Python's sympy latex package. Anyone know how to do this? Thanks, Joel

我正在尝试构建一个超几何pdf方程,使用带有圆括号的nCr表达式(n over r)使用Python的sympy latex包。有人知道怎么做吗?谢谢,乔尔

1 个解决方案

#1


2  

I think you're looking for binomial:

我想你正在寻找二项式:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n, r = sympy.symbols("n, r", real=True, positive=True)
print latex(binomial(n, r) # outputs {\binom{n}{r}}
binomial(n, r)

This will output nCr (nicely) with parenthesis in an IPython shell or Jupyter notebook.

这将在IPython shell或Jupyter笔记本中输出带有括号的nCr(很好)。

If you want an actual value to be evaluated, you can do:

如果要评估实际值,可以执行以下操作:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n = 4
r = 2
binomial(n, r) # outputs 6

If you want the symbols 4 and 2 to be displayed, try:

如果要显示符号4和2,请尝试:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n, r = sympy.symbols("4, 2", real=True, positive=True)
binomial(n, r)

#1


2  

I think you're looking for binomial:

我想你正在寻找二项式:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n, r = sympy.symbols("n, r", real=True, positive=True)
print latex(binomial(n, r) # outputs {\binom{n}{r}}
binomial(n, r)

This will output nCr (nicely) with parenthesis in an IPython shell or Jupyter notebook.

这将在IPython shell或Jupyter笔记本中输出带有括号的nCr(很好)。

If you want an actual value to be evaluated, you can do:

如果要评估实际值,可以执行以下操作:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n = 4
r = 2
binomial(n, r) # outputs 6

If you want the symbols 4 and 2 to be displayed, try:

如果要显示符号4和2,请尝试:

from sympy import binomial, latex
sympy.init_printing(use_latex='mathjax')
n, r = sympy.symbols("4, 2", real=True, positive=True)
binomial(n, r)