Sympy:如何将乘积的对数简化为对数之和? [英] Sympy: how to simplify logarithm of product into sum of logarithms?

查看:41
本文介绍了Sympy:如何将乘积的对数简化为对数之和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了激发这个问题,sympy.concrete 有一些有效的工具来处理符号和.为了将这些工具应用于符号产品,必须取对数.但是,直接取对数并不会自动给出转换:

import sympy as spsp.init_printing() # 将数学显示为乳胶z = sp.Symbol('z')j,k = sp.symbols('j,k')Prod = sp.Product( (z + sp.sqrt(1-4*j*z**2))**(-1), (j,1,k) )sp.log(产品)

给予

在所有可能的变化中:

sp.log(Prod)sp.log(Prod).expand()sp.log(Prod).simplify()sp.expand_log(sp.log(Prod),force=True)

<块引用>

问题.如何将其转换为对数之和?

相关:

display(concrete_expand_log(expr))

To motivate the question, sympy.concrete has some efficient tools to manipulate symbolic sums. In order to apply these tools to symbolic products, one has to take a logarithm. However, straightforward taking the logarithm doesn't automatically give the transformation:

import sympy as sp
sp.init_printing() # display math as latex
z = sp.Symbol('z')
j,k = sp.symbols('j,k')
Prod = sp.Product( (z + sp.sqrt(1-4*j*z**2))**(-1), (j,1,k) )
sp.log(Prod)

gives

in all possible variations:

sp.log(Prod)
sp.log(Prod).expand()
sp.log(Prod).simplify()
sp.expand_log(sp.log(Prod),force=True)

Question. How to convert it into sum of logarithms?

Related:

How to simplify logarithm of exponent in sympy?

解决方案

Assuming that there is no standard function with desired behaviour yet, I wrote my own, mimicking the behaviour of

sp.expand_log(expr, force=True)

This code recursively goes over expression trying to locate patterns log(product) and replaces them by sum(log). This also supports multi-index summation.

Code.

def concrete_expand_log(expr, first_call = True):
    import sympy as sp
    if first_call:
        expr = sp.expand_log(expr, force=True)
    func = expr.func
    args = expr.args
    if args == ():
        return expr
    if func == sp.log:
        if args[0].func == sp.concrete.products.Product:
            Prod = args[0]
            term = Prod.args[0]
            indices = Prod.args[1:]
            return sp.Sum(sp.log(term), *indices)
    return func(*map(lambda x:concrete_expand_log(x, False), args))

Example.

import sympy as sp
from IPython.display import display
sp.init_printing() # display math as latex
z = sp.Symbol('z')
j,k,n = sp.symbols('j,k,n')
Prod = sp.Product( (z + sp.sqrt(1-4*j*z**2))**(-1), (j,0,k))
expr = sp.log(z**(n-k) * (1 - sp.sqrt((1 - 4*(k+2)*z**2)/(1-4*(k+1)*z**2)) ) * Prod)
display(expr)

display(concrete_expand_log(expr))

这篇关于Sympy:如何将乘积的对数简化为对数之和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆