如何使用 sympy.lambdify 和 Max 函数来替换 numpy.maximum 而不是 numpy.amax? [英] How do I use sympy.lambdify with Max function to substitute numpy.maximum instead of numpy.amax?

查看:36
本文介绍了如何使用 sympy.lambdify 和 Max 函数来替换 numpy.maximum 而不是 numpy.amax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sp.Max(x, 0) 对大型解析表达式进行 lambdify.我想使用 numpy 来向量化我的计算,所以 x 将是一个数组.我需要 x 和 0 的元素最大值.不过,默认情况下,sympy 将 sp.Max 更改为 np.amax.它沿轴找到最大值,这不是我需要的.lambdify 中的modules"关键字不像我预期的那样工作.我试过了:

I'm trying to lambdify big analytic expression with sp.Max(x, 0) inside. I want to use numpy to vectorize my calculations, so x is going to be an array. I need element-wise maximum values of x and 0. Still, sympy changes sp.Max to np.amax by default. It finds maximum along the axis, that's not what I need. "modules" keyword in lambdify doesn't work as I expect. I've tried:

import numpy as np
import sympy as sp

arr = np.array([1, 2, 3])
expr = sp.sin(x) + sp.Max(x, 0)
f = sp.lambdify(x, expr, modules=[{'Max': np.maximum}, 'numpy'])  # docs say, priority of modules matters
help(f)

它给出:

Help on function _lambdifygenerated:
_lambdifygenerated(x)
    Created with lambdify. Signature:

    func(x)

    Expression:

    sin(x) + Max(0, x)

    Source code:

    def _lambdifygenerated(x):
        return (sin(x) + amax((0,x)))


    Imported modules:

sp.Max 由于某种原因更改为 amax.

sp.Max changed to amax for some reason.

如果 'numpy' 未包含在 'modules' 列表中,它会跳过所有其他函数.我还尝试在列表中交换 dict 和 'numpy',但没有帮助.请澄清一下,有什么问题吗?这是 sympy 中的错误吗?

If 'numpy' is not included into 'modules' list, it simply skips all other functions. I've also tried to swap dict and 'numpy' in list, but it haven't helped. Please, clarify, what's wrong? Is it a bug in sympy?

推荐答案

当使用 lambdify 创建用于矢量化工作的 numpy 函数时,通常会出现 微妙问题,尤其是当变量 (x) 和常量 (0) 混合在一起.

When using lambdify to create numpy functions intended to work vectorized, there often are subtle problems, especially when variables (x) and constants (0) are mixed.

在这种情况下,sp.max 假设它所有可能的许多参数都是单个值.np.amax 获取一个扁平数组的最大值.np.maximum 获取两个数组的元素最大值.这里的问题是常量 0 不会自动扩展为 numpy 数组.

In this case, sp.max supposes all of its possible many parameters being single values. np.amax gets the maximum of one flattened array. np.maximum gets the element-wise maximum of two arrays. The problem here is that the constant 0 doesn't automatically get expanded to a numpy array.

我的解决方法是将 sp.max 替换为基于 sp.Piecewise 的自定义函数.请注意,如果 sp.max 的参数超过 2 个,您将需要一个单独的函数.

My workaround would be to replace sp.max with a custom function based on sp.Piecewise. Note that you would need a separate function if there would be more than 2 arguments to sp.max.

import numpy as np
import sympy as sp
from sympy.abc import x

def sympy_max2(a, b):
    return sp.Piecewise((b, a < b), (a, True))

arr = np.array([11, 22, 33, -1, -2])
expr = sp.sin(x) + sympy_max2(0, x)
f = sp.lambdify(x, expr, modules=['numpy'])

print(f(arr)) # [10.00000979 21.99114869 33.99991186 -0.84147098 -0.90929743]

这篇关于如何使用 sympy.lambdify 和 Max 函数来替换 numpy.maximum 而不是 numpy.amax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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